Search code examples
linuxbashudev

udev/Bash script to get usb info


I have written a udev rule that call a script to send email when a usb drive is attached/removed. so far its working fine. following is my udev rule and send email script:

SUBSYSTEM=="usb", ACTION=="add", RUN+="/u/usb_added %b"
SUBSYSTEM=="usb", ACTION=="remove", RUN+="/u/usb_removed %b"

But in sendemail script i also want to include detail of usb drive (usb size, label, mounted on and time of attachment). But I dont know how to get this information. can anyone please help me either from udev or to write a script in bash to read log/messages and fetch this information.


Solution

  • You an use the udevmonitor tool to get the environment variables that the uevent emitted. From these information, you can locate where your block device locates under /sys/ and get further information. You can also use blkid tool to get the information with the DEVNAME exported by uevent.