I have some UDEV rules for a RAID array; 10 hard disks are assigned names from /dev/sda to /dev/sdj.

The disks can be absent at any time.

How do I write a UDEV rule to prevent a device such as an arbitrary USB from taking up one of the missing hard disk names?

For example, if my second hard disk /dev/sdb is unplugged for whatever reason, and then later someone plugs in a USB device in a completely different port, I need to usb to not become /dev/sdb.

I can think of a way to map each available USB port on the system to it's own name, but there must be a cleaner way? I am thinking of something like this, where I say "if it's a USB device, assign it a name from k to z:

KERNEL=="sd?", SUBSYSTEM=="block", DEVPATH="*usb", NAME="sd[k-z]", GOTO="END_20_PERSISTENT_DISK"

KERNEL=="sd?*", ENV{DEVTYPE}=="partition", SUBSYSTEM=="block", DEVPATH=="*usb*", NAME="sd[k-z]%n"

The system is intended to be fully autonomous, accepting any arbitrary USB or hard disk device - this is why I am using DEVPATHs versus labels and uuids.

  • 2
    Don't use device names like /dev/sda that are likely to be used by other devices. Change your rules to create /dev/myraida through /dev/myraidj or similar. – cjm May 22 '15 at 19:27

Plug your usb drive and use

udevadm info -a -p /sys/block/sd* 

where * is your disk assigned number(sda1,sda3,sdb2,etc)

Then wrote a udev rule like this

KERNEL=="sd*", SUBSYSTEMS==" block", ATTRS{serial}=="**************", SYMLINK+="sdb%n"

Of course edit this based on udevadm info

Another good solution is to use DISK_UID or LABEL for mount,then /dev/sd* become not important,you will mount using LABEL or DISK_UUID I prefer LABEL,here's an example of my fstab

LABEL=memoria     swap             swap        defaults         0   0
LABEL=root        /                ext4        defaults,acl     1   1
LABEL=utenti      /home            ext4        defaults,acl     1   2

utenti and root are lvm volumes,and /dev/sd* is not important for use LABEL use e2label command on your partition

  • Unfortunately I can't use labels or uuids to identify devices - this system is intended for use as a portable, autonomous rig where clients can plug in arbitrary USB devices at any time for data offloads from the RAID array. I can't use labels as I would have to update the udev rules any time a client wants to use a new disk. – sg90 May 22 '15 at 15:23
  • Try first solution,use udevadm info and create a rule – elbarna May 22 '15 at 15:51
  • For the time being I'll write a udev rule for every USB port on my motherboard - the crux of this question was whether there is a better way to do this, ie. how to write udev rules to not get a particular name. If it was possible to do in a single rule vs a rule for each USB port device path. – sg90 May 22 '15 at 15:54
  • I'm not sure,but try to search google for udev regex negation – elbarna May 22 '15 at 16:15
  • I couldn't find any information for using regex on the name or symlink assignments, it appears regex is only available for the selectors ie. LABEL, DEVPATH etc. However I think I may have found a way to circumvent this whole issue - I will just name the hard disks used in the raid to things other than sda,sdb, ie. hd1,hd2 - this way any new USB device can take up these default names without disrupting the raid manager or any other scripts in use. – sg90 May 22 '15 at 17:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.