0

I need to anticipate the device name (sdX) that the next mass storage device (sda, sdb, sdc, …) will receive from the SCSI driver.

If I remove one device and connect another one, I noticed that the newly connected device will not take the name of the removed device, but will use the alphabetically following device name.

Where is this information stored? How long is it stored? How can I use this information in order to anticipate the device name that the next device will receive?

  • Is the purpose of all this to mount specific devices at specific mount points? – Geeb Apr 23 '15 at 10:23
  • 1
    This sounds like an X/Y problem. If you just want to find out how to use the device, use UUIDs or filesystem labels. – Chris Down Apr 23 '15 at 11:31
0

I think you need to look at udev rules processing and device node creation. Default udev rules are located in /lib/udev/rules.d but you can overwrite them in /etc/udev/rules.d

0

I think Linux uses the first available letter. However that might depend on kernel versions and on device types.

It is extremely unlikely that you need to predict the next letter. You wouldn't be able to do anything useful with that anyway: what if two devices are added before your program has time to react? Linux has a mechanism to control the names of devices: udev. You can write udev rules to recognize devices of a specific type, brand or model, or with filesystems having a specific label or UUID, and run a script when a matching device is inserted. See Map IDE drive to /dev/hda and Perform an action when a wireless keyboard is connected for examples.

The default udev rules automatically create links to recognize block devices by their model and serial number (in /dev/disk/by-id) and filesystems by their label and serial number (in /dev/disk/by-label and /dev/disk/by-serial). Chances are that this is enough for what you want to do.

0

If I remove one device and connect another one, I noticed that the newly connected device will not take the name of the removed device, but will use the alphabetically following device name.

No, that depends on timing and if ANYTHING has the old device open. On my system, if I insert a USB key, remove it, and reinsert it after a few seconds, it will probably get the same device. If I mount it after insertion, and do NOT umount it, then on reinsert it will get the next device.

The letter itself isn't as crucial as the actual device major/minor. If the old one is still in use AT ALL, the kernel will use another entry.

This can be seen very evidently on SCSI/SAS tape, where the kernel expects some operations to take 30+ seconds. If the link is slightly flakey, the kernel will still have /dev/st0 open, and the hardware will still be busy when it show up again (and the kernel log will show device init as taking a long time before showing up as the next device).

From the comments, WHY do you need to predict the next device? In many cases, it's probably better to listen to udev (udevadm monitor, or many other ways) for the next device appearing, and use that by identity, rather than any specific letter.

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.