0

I have a server and one of it's jobs is to clone images created by DD to one of 3 targets:

  1. USB > SATA 2
  2. USB > CF (Compact Flash)
  3. USB > CFAST

I have written bash scripts that allows the user to select the type of image they want to clone and it does everything for them, it works great with one exception.

The device IDs of these USB Storage devices changes each time the system reboots, and what was once the system disk /dev/sda after a reboot can become /dev/sde which my script is for example assigned to CF Cloning.

So how can I force a USB device to use a specific device ID whenever it is connected or whenever the device is rebooted.

For example,

  1. USB > SATA 2) /dev/sdx
  2. USB > CF (Compact Flash) /dev/sdy
  3. USB > CFAST /dev/sdz

Or is there an alternative solution where I can refer to the USB device when using dd to read or write to it ??

Thanks

Edit

I tried creating a rule but it did not seem to work:

# lsusb | grep Sony
Bus 001 Device 002: ID 054c:01bd Sony Corp. MRW62E Multi-Card Reader/Writer

# udevadm info --name=/dev/sdb --attribute-walk | grep 054c  
    ATTRS{idVendor}=="054c"
# udevadm info --name=/dev/sdb --attribute-walk | grep 01bd
    ATTRS{idProduct}=="01bd"

Created a file:

nano /etc/udev/rules.d/10-usb.rules

ACTION=="add", ATTRS{idProduct}=="01bd", ATTRS{idVendor}=="054c", SYMLINK+="sonycf"

Then tried activating it:

udevadm trigger

But /dev/sonycf does not appear in my devices.

Edit 2

Thanks @grawity

ACTION=="add", SUBSYSTEM=="block", ATTRS{idProduct}=="01bd", ATTRS{idVendor}=="054c", SYMLINK+="sonycf"

udevadm trigger --action=add --subsystem-match=block

Works a treat :-)

  • You probably need to write udevrules which will assign a symlink such as /dev/sdX to a particular device based on its VID and PID. – fpmurphy Feb 25 '19 at 3:45
  • Thanks for helping, would you mind giving me an example on how I could do this ? – crankshaft Feb 25 '19 at 6:14
2

The /dev/sd* device names are always assigned sequentially, and there is no option to change that.

Instead, use the symlinks that udev creates for you under /dev/disk:

$ tree /dev/disk
/dev/disk
├── by-id
│   ├── ata-Samsung_S3Z9NB0KA26420K -> ../../sda
│   ├── ata-Samsung_S3Z9NB0KA26420K-part1 -> ../../sda1
│   ├── wwn-0x50025408c38ebf36 -> ../../sda
│   └── wwn-0x50025408c38ebf36-part1 -> ../../sda1
├── by-label
│   ├── EFI -> ../../sda1
├── by-partlabel
│   ├── EFI -> ../../sda1
│   └── sys -> ../../sda3
├── by-partuuid
│   ├── b5b9724a-d13f-4d97-8bff-955bf012d7ad -> ../../sda1
│   └── cd1df94d-de5b-4e4e-9874-8bac9dc5835e -> ../../sda3
├── by-path
│   ├── pci-0000:00:1f.2-ata-1 -> ../../sda
│   ├── pci-0000:00:1f.2-ata-1-part1 -> ../../sda1
│   └── pci-0000:00:1f.2-ata-1-part2 -> ../../sda2
└── by-uuid
    ├── 7b6fad01-577a-4877-88f3-9762697e4993 -> ../../sda3
    └── BD26-0E7C -> ../../sda1
  • by-id and by-path links are based on physical devices (by their serial number or by the port they're connected at);
  • by-partuuid and by-partlabel are based on the partition table (GPT) information;
  • by-uuid and by-label are based on information embedded in the filesystem itself.

Your own attempt at creating a rule seems okay, but you should use SUBSYSTEM=="block" (and maybe even KERNEL=="sd*") to specify that it needs to match only the block device and not any of its parents. Without doing this, the rule would match the parent "scsi LUN" device, and its "scsi target" device, (and its parent "scsi host" device, and its parent "usb interface" device, and its parent "usb device" device...) none of which have any device nodes under /dev or any relationship to /dev/sdb.

Additionally, note that udevadm trigger by default synthesizes "change" events, which won't match your rule because it only checks for "add". Try either udevadm trigger --action=add --subsystem-match=block or change the rule to ACTION=="add|change".

  • Thanks, but how would I use this in the dd command when dd expects a /dev/sdx ?? -m I cannot dd if=ata-Samsung_S3Z9NB0KA26420K of=mydisk.img ?? – crankshaft Feb 25 '19 at 6:48
  • 1
    @crankshaft dd doesn't care. /dev/disk/by-id/ata-Samsung_S3Z9NB0KA26420K is as good as /dev/sda. – Kamil Maciorowski Feb 25 '19 at 6:58
  • 1
    No, dd doesn't expect anything in particular – the parameters for if= and of= are just filenames, nothing else. You can use any name from /dev or from anywhere else. – user1686 Feb 25 '19 at 7:00

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.