clean usb automount with udev
Having just had to rebuild my sheevaplug box I realised I couldn’t remember how I’d hacked automounting my USB hard drive.
Googling led me to the same blog entries that I’d come across last time, none of which were as clean as I’d like. After re-reading Daniel Drake’s udev tutorial I realised I could just run the (un)mount command directly from udev whenever a block device was added or removed.
So, all that is needed is an fstab entry and a udev rule for mounting/unmounting like so:
# cat /etc/fstab
LABEL=backup /mnt/backup ext3 noauto,defaults
# cat /etc/udev/rules.d/99-try-mount.rules
ACTION=="add", SUBSYSTEM=="block", RUN+="/bin/mount /dev/%k"
ACTION=="remove", SUBSYSTEM=="block", RUN+="/bin/umount -l /dev/%k"
With these in place, anytime a new block device is added or remove, udev tries to (un)mount it. I set labels on the filesystems so that I don’t need to worry about the fact that the device name might change between uses (of course I could use udev to ensure that they always had the same device name, but this seemed easier).