Tip: using 'udev' to assign sane /dev symbolic links for devices

By default, USB devices are detected easily on modern Linuxes.

However, the device's naming depends on what else you have plugged in -- it may be /dev/sda1, /dev/sda2, dev/sdb1, etc. To work around this, you need to use 'udev' to assign it a /dev node when it's plugged in.

Normally, you write a rule for the specific bus, model, etc. -- but wouldn't it be nicer to write a rule once, and have each type of device automatically supported thereafter?

Here's how. Create the file '/etc/udev/rules.d/by_model.rules', containing these lines:

# http://taint.org/wk/TipUdevByModel
BUS=="usb", SYMLINK+="by_model_%s{model}"

The results:

: jm 155...; ls -l /dev/by_model_*
total 0
lrwxrwxrwx  1 root root 7 Oct 26 12:52 by_model_iPod -> ../sda1
lrwxrwxrwx  1 root root 7 Oct 26 12:49 by_model_Storage -> ../sdb1

sweet! Now to get the distro maintainers to implement this by default.

** Update 1 **

Update: initially I used a subdir of /dev, but it appears using a subdirectory of /dev is unreliable. This works reliably though:

# http://taint.org/wk/TipUdevByModel
BUS="usb", SYMLINK+="by_model_%s{model}"

in other words, "by_model_iPod" etc. It's not quite as pretty, though.

** Update 2 **

Update 2, Jan 10 2006: this doesn't work if the device is partitioned with multiple partitions. I have no solution as yet, apart from writing a hand-written udev rule as described here: http://www.reactivated.net/writing_udev_rules.html#usbstorage-extra

** Update 3 **

Apr 4 2007: note that current versions of udev require "BUS=="usb"" instead of "BUS="usb"". updated...

TipUdevByModel (last edited 2007-04-04 19:06:41 by 213-202-139-46)