Consistent USB device adapter naming on Linux

0

I have a USB port into which I insert all sorts of different USB disks over time. The kernel does not consistently allocate the same device for it, so I have to go hunt after each insert. How can I always get the same device name?

Note that driving this from the UUID or such does not work, because I use the same port for different disks. It's not the disk I want to consistently name, it's the current disk that's in a consistent port.

I had been hoping there would be some kind of /sys/…/controllerX/…/portY/… path that identifies the USB port, rather than the disk, but hunting for it I did not find one.

Johannes Ernst

Posted 2018-08-13T22:10:34.117

Reputation: 775

Answers

2

/dev/disk/by-path/ is what you seek.

E.g. my pendrive in a "certain" USB port was:

pci-0000:03:00.0-usb-0:1:1.0-scsi-0:0:0:0 -> ../../sdc

After disconnecting and plugging into another port it was:

pci-0000:03:00.0-usb-0:2:1.0-scsi-0:0:0:0 -> ../../sdc

And yet in another port:

pci-0000:00:1d.0-usb-0:1.2:1.0-scsi-0:0:0:0 -> ../../sdc

I left it there, plugged another pendrive into the "certain" port. Now these two are:

pci-0000:00:1d.0-usb-0:1.2:1.0-scsi-0:0:0:0 -> ../../sdc
pci-0000:03:00.0-usb-0:1:1.0-scsi-0:0:0:0 -> ../../sdd

Note the sdd (another pendrive) has the same "by-path" name the sdc had when it was in the "certain" USB port. The full path in the filesystem is

/dev/disk/by-path/pci-0000:03:00.0-usb-0:1:1.0-scsi-0:0:0:0

in my case. Its first partition is

/dev/disk/by-path/pci-0000:03:00.0-usb-0:1:1.0-scsi-0:0:0:0-part1

I believe this solves your problem.

Kamil Maciorowski

Posted 2018-08-13T22:10:34.117

Reputation: 38 429

Duh, I didn't think of looking under /dev. – Johannes Ernst – 2018-08-13T23:36:59.160