Prismatic
Prismatic

Reputation: 3348

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 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

Upvotes: 2

Views: 3423

Answers (2)

jlewis42
jlewis42

Reputation: 923

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.

Upvotes: 1

emreakyilmaz
emreakyilmaz

Reputation: 137

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.

Upvotes: 0

Related Questions