I have a bunch of usb ports and hubs. When you attach usb thumb drives, Linux assigns them to virtual block devices on a first-come first-serve basis. It creates sdd, sde, sdf, and so on, regardless of which port I plug the drives into. We copy files onto these drives and when there is an error, it is impossible for me to tell which drive the error occured on.
What I'd like to do is configure udev so that it maps physical hardware devices/ports to specific virtual devices. That is, port 1 => sdd, port 2 => sde, port 3 => sdf, etc. This would allow me to tell which thumbdrive the error occured on.
I've found a few resources (e.g. http://reactivated.net/writing_udev_rules.html), from which, I can tell that this can be done. However I have no experience with such low level configurations. They are confusing to me.
Is there another reference that might make things clearer? Or perhaps some software which does it for me? Or a library that makes it easier?
/dev/disk/by-path
will contain consistent names that will map to specific USB ports (at least they do on my box).Also check out
/dev/disk/by-id
and/dev/disk/by-uuid
for ways to access specific devices regardless of which path is used to access them.Are these drives being automounted? What tool are you using for auto-mounting? Are you recreating the filesystem each time they are mounted, or are the partitions/filesystem pretty stable?
If you uniquely label the filesystems usually you can mount the filesystem by the label instead of the device name.
When you create a filesystem a UUID is assigned to the filesystem. You could put a label of some sort on the USB device that corresponds to the UUID.
The most typical use case would be to match by filesystem uuid, but it you're writing whole-disk-identical devices, that won't do. Instead, you can match by model and serial number. An example (untested)
udev
rule for this is:To see what
ATTR
values exist for a given device now known as/dev/sdz
, runudevinfo -n /dev/sdz -a
. To see whatENV
values exist, runudevinfo -n /dev/sdz --query=env
.If you want to match by port rather than by serial number, look up parent devices in the document you cite. I suspect something like
KERNELS=="usb42"
would work, but I haven't tried.I am using this hack with two Python scripts for a similar purpose:
So this allows me to access drives via the label:
Hope that anybody will find this helpful!