Jun
7
Debian: Naming USB devices with uDev
June 7, 2009 | 1 Comment
I use three different small backup drives as three backup volumes. I turn them on on sunday, the backup scripts runs overnight, and I turn them off on monday morning. I’ve shared the backup script in a previous post.
The problem is that the backup script starts by mounting the backup drives. It expects them to have the same device name in /dev everytime. For example, it expects backup1 to be /dev/sde1. It turns out that these assigned device names can randomly change. Sometimes, backup1 appears as /dev/sdh1. The solution, in this case, is to assign a persistent name to each device. That is what udev is for. Udev will be called when a drive is plugged. If the plugged drive match a udev rule, it will be assigned the name defined in the rule.
The first step is to find criterias that will allow to identify a single drive and then create a rule for it.
Run the following command for the drives you need to name (replace sde1 by what you need):
1 | udevinfo -a -p $(udevinfo -q path -n /dev/sde1) |
In my case, I get the following result:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | looking at device '/block/sde/sde1': KERNEL=="sde1" <strong> SUBSYSTEM=="block"</strong> DRIVER=="" ATTR{start}=="63" ATTR{size}=="2930272002" ATTR{stat}==" 48 1558 3506 228 19 0 38 248 0 156 476"looking at parent device '/block/sde': KERNELS=="sde" <strong> SUBSYSTEMS=="block"</strong> DRIVERS=="" ATTRS{range}=="16" ATTRS{removable}=="0" ATTRS{size}=="2930277168" ATTRS{capability}=="12" ATTRS{stat}==" 60 1573 3722 504 19 0 38 248 0 348 752"looking at parent device '/devices/pci0000:00/0000:00:02.1/usb2/2-4/2-4:1.0/host4/target4:0:0/4:0:0:1': KERNELS=="4:0:0:1" <strong> SUBSYSTEMS=="scsi"</strong> DRIVERS=="sd" ATTRS{device_blocked}=="0" ATTRS{type}=="0" ATTRS{scsi_level}=="3" <strong><code> ATTRS{vendor}=="ST315003" ATTRS{model}=="41AS " |
1 2 3 4 5 6 7 8 9 10 11 12 13 | ATTRS{rev}==" " ATTRS{state}=="running" ATTRS{timeout}=="30" ATTRS{iocounterbits}=="32" ATTRS{iorequest_cnt}=="0x57" ATTRS{iodone_cnt}=="0x57" ATTRS{ioerr_cnt}=="0x0" ATTRS{modalias}=="scsi:t-0x00" ATTRS{evt_media_change}=="0" ATTRS{queue_depth}=="1" ATTRS{queue_type}=="none" ATTRS{max_sectors}=="240" ... |
I have outlined the unique values and criterias that will allow me to identify backup1. I also outlined the subsystem lines that will be used in the udev rule.
The rule that will match this drive is:
1 | SUBSYSTEM=="block", SUBSYSTEMS=="block", SUBSYSTEMS=="scsi", ATTRS{vendor}=="ST315003", ATTRS{model}=="41AaS", NAME="backup1" |
More information about udev rules can be found online.
That done, you can test our rule by launching the following command:
1 | udevadm test $(udevinfo -q path -n /dev/sde1) --force |
If the rule matched the drive, you can ls /dev/disk/by-label and find backup1 in the list.
You can now edit the backup script and replace mount /dev/sde1 by mount /dev/disk/by-label/backup1. The latter should always refer to the right drive.
Thanks to the authors of the following two guides that lead me to this solution:
Tagged with: Debian, drive, udev, usb
Liked this page?
Subscribe to the RSS feed or sign up for the newsletter now.
Debian: Naming USB devices with uDev http://tinyurl.com/of6kyq