2013-12-09 · in Tech Notes · 260 words

I have a variety of USB storage devices that I want to connect to my Linux machines, using various filesystems and partitioning schemes: MP3 players, digital recorders, a multi-format cardreader, USB-to-IDE/SATA adaptors, and lots of USB flash sticks. The typical way of dealing with this is to use a volume manager daemon to mount devices automatically as they appear. However, I don't like that approach, since I don't always want to actually mount the filesystem — for example, if I'm trying to rescue data from a faulty device.

Instead, I configure Linux's device manager udev to give the different devices predictable device names when they're detected, and mount them (or not) as required. My /etc/udev/rules.d/65-stick.rules file contains rules which match individual devices and create appropriate symlinks under /dev/disk. For example, my Rockbox MP3 player's internal storage appears as /dev/disk/rockbox/int; this lets me use a specific mountpoint for it, so my sync-player script can't accidentally overwrite a different device.

To write a rule like this, you need to find something unique in udev's information about the device that it can match. Handily, you can ask udev to show you all the information it has about a device:

$ udevadm info --query=all --name=/dev/sdb
...
E: ID_SERIAL=Rockbox_Internal_Storage_1A8B754950EEF07FB00000000-0:0
...

Each rule jumps to the stick_end label at the end of the file if it matches. There's a default rule before that label for generic USB storage devices such as flash sticks, which just get called /dev/disk/usba at the moment. I'd like it to give multiple memory sticks distinct names, but that's a scripting problem for a different day.