Friday, June 4, 2010

udev rules

finally got a udev rule in /etc/udev/rules.d/ so it will run my script on a hotplug event. first i had to run udevadm info --attribute-walk --name /dev/sdc1 to dump out a list of potential attributes to search. since this spits out info on the whole device chain, i found out that the section to look at was /devices/pci0000:00/0000:00:1d.7/usb1/1-7/1-7:1.0 (i tried matching on a lower level, but my script was executed twice with different env vars.) then i put a rule line into /etc/udev/rules.d/91-myhotplug.rules (91 because i want it to run after everything else) like this:
ACTION=="add", SUBSYSTEM=="usb", ATTRS{modalias}=="usb:v0...50", RUN+="/home/user/svn/bin/hotplugscript"
had to put in a sleep 6 before doing anything in the newly mounted fs because it seems to take about 5 s to settle down. (probably should loop-and-test in production-level code.) oh, and one other handy thing i googled up: use blockdev --flushbufs /dev/sdc1, for example, to flush one block device. sync just flushes them all. i have the whole bash script in brackets with a trailing & to make it fork off and return immediately; that's important because otherwise i found it hangs or dies at the sleep and nothing after that gets executed. and i did find that a umount after business is done in the udev script causes no problems.

No comments: