Auto-mount using udev

I have an entry for my new shiny USB drive in /etc/fstab. But I like my USB drive to auto-mount when it is plugged in (and unmount when appropriate). Here comes udev.

In /etc/udev/rules.d/50-usb.rules:

SUBSYSTEM=="block", DEVTYPE=="disk", DEVNAME=="/dev/", ACTION=="add", RUN+="/bin/mount -a"
SUBSYSTEM=="block", DEVTYPE=="disk", DEVNAME=="/dev/", ACTION=="remove", RUN+="/bin/umount /dev/%k"

To list the environment variables available for matching in udev, use udevadm monitor --environment