使用 libudev 获取 Linux 中已安装驱动器的信息
我使用 libudev 来监视存储设备(USB 密钥等),以便当它们被修改时我会在程序中收到通知。 Libudev 告诉您设备是否已添加、删除等,并为您提供设备节点。例如,如果我在系统中添加 SD 卡,libudev 会告诉我添加了“/dev/mmcblk0p1”。发生这种情况时,我也想获取设备的挂载点。因此,我检查 /proc/mounts 以查看设备已映射到的安装路径。
虽然这似乎工作正常,但我不确定 libudev 是否在安装设备之前、安装设备之后或两者之间的某个位置向我发送了设备已添加的信号(这意味着我要检查 /proc/在我收到信号后立即安装文件是一种不可靠的方法)。
所以我的问题是有谁知道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 tells you whether or not a device has been added,removed,etc and gives you the device node. For example, if I add an SD card in my system, libudev will tell me that "/dev/mmcblk0p1" was added. 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.
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).
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?
If it isn't, is there another way to get the mount path given the device node that is reliable?
Regards,
Pris
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不能保证,但我正在做一些非常相似的事情,而且它似乎也对我有用。我监听块/磁盘添加事件,找到相应的块/分区设备,然后解析 /proc/mounts 查找开发节点。
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.
您只想获取安装路径吗?或者您想将该存储设备安装到特定的安装点吗?如果您想将存储设备安装到特定路径,则不需要捕获信号。您可以为特定设备定义规则。您可以在 etc/udev/rules.d 文件中定义这些规则。
您可以查看 http://www.reactivated.net/writing_udev_rules.html 了解更多信息。
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.
You can check http://www.reactivated.net/writing_udev_rules.html for more information.