简体   繁体   English

使用libudev在Linux中获取有关已安装驱动器的信息

[英]Getting information on mounted drives in Linux with libudev

I use libudev to monitor storage devices (usb keys, etc) so that when they are modified I get a notification in my program. 我使用libudev监视存储设备(usb密钥等),以便在修改存储设备时在程序中收到通知。 Libudev tells you whether or not a device has been added,removed,etc and gives you the device node. Libudev告诉您是否已添加,删除设备等,并为您提供设备节点。 For example, if I add an SD card in my system, libudev will tell me that "/dev/mmcblk0p1" was added. 例如,如果我在系统中添加SD卡,libudev会告诉我添加了“ / dev / mmcblk0p1”。 When this happens, I want to get the mount point of the device as well. 发生这种情况时,我也想获取设备的挂载点。 So I check /proc/mounts to see what mount path the device has been mapped to. 因此,我检查/ proc / mounts来查看设备已映射到的安装路径。

While this seems to work okay, I'm not sure whether libudev sends me the signal that a device has been added before it mounts the device, after it mounts the device or somewhere in between (which would mean that me checking the /proc/mounts file right after I get the signal is an unreliable method). 虽然这似乎可以正常工作,但我不确定libudev是否在安装设备之前,安装设备之后或介于两者之间的某个地方向我发送了已添加设备的信号(这意味着我正在检查/ proc /我收到信号后立即挂载文件是不可靠的方法)。

So my question is does anyone know when libudev sends the signal, and whether or not the method I use to get the device's mount path is reliable? 所以我的问题是,有人知道libudev何时发送信号,并且我用来获取设备的安装路径的方法是否可靠?

If it isn't, is there another way to get the mount path given the device node that is reliable? 如果不是,给定可靠的设备节点,是否还有另一种方法来获取安装路径?

Regards, 问候,

Pris PRIS

I can't give a guarantee, but I'm doing something very similar and it seems to work for me too. 我不能保证,但是我做的事情非常相似,这似乎也对我有用。 I listen for block/disk add events, find corresponding block/partition devices and then parse /proc/mounts looking for the dev node. 我监听块/磁盘添加事件,找到相应的块/分区设备,然后解析/ proc / mounts寻找dev节点。

Do you want to just get the mount path? 您是否只想获取安装路径? or do you want to mount that storage device to a specific mount point? 还是要将该存储设备挂载到特定的挂载点? If you want to mount a storage device to a specific path, you don't need to catch a signal. 如果要将存储设备安装到特定路径,则无需捕获信号。 You can define rules for a certain devices. 您可以为某些设备定义规则。 You can define these rules inside the etc/udev/rules.d file. 您可以在etc / udev / rules.d文件中定义这些规则。

You can check http://www.reactivated.net/writing_udev_rules.html for more information. 您可以检查http://www.reactivated.net/writing_udev_rules.html了解更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM