How do I control /dev names

sofasurfer

Active Member
Joined
May 24, 2022
Messages
345
Reaction score
121
Credits
3,041
My main ssd is sdb while my backup ssd is sda. I want my main ssd to be on sda and my backup on sdb. How do I control this? What determine sda, sdb or sdc? Is it the plug its on or the order of installation? How can I identify them so I am sure not to get them confused?
 


Can't you tell the difference by the location they are mounted to which you can see? Otherwise you can use UUID and optionally also LABEL to mount them.
 
Write it on a piece of paper and sticky tape it to the bottom lip of the monitor.
 
It used to be a linear, port based enumeration, but AFAIK the kernel does not give such a guarantee anymore. It can happen you swap sata ports or bios boot order to get /dev/sda, /dev/sdb to your preference, next month you alter/create a partition and on reboot the order suddenly swaps. It happens with NVME a lot, requiring to prepare a new paper to sticky tape each time.

The best may be to make sure the system is using UUID in /etc/fstab (most distros should automatically do that these days) and apply a LABEL for everyday use.
 
My main ssd is sdb while my backup ssd is sda. I want my main ssd to be on sda and my backup on sdb. How do I control this? What determine sda, sdb or sdc? Is it the plug its on or the order of installation? How can I identify them so I am sure not to get them confused?
As a preliminary: the "sd" of /dev/sda naming is derived from "scsi disk" or "scsi drive" which came to be used in device names after "hd" of /dev/hda (for "hard disk" or "hard drive"), was changed years ago when the kernel software was changed to make everything look like a scsi disk. Check out the libata software if interested.

To the actual issue, here are some notes from the linux-scsi mailing list in my notes:
Beginning with kernel 5.3 the order in which SCSI devices (which includes HDDs)
are probed and named has become non-deterministic. This is a result of a patch
that was submitted to add asynchronous device probing (specifically, commit
f049cf1a7b6737c75884247c3f6383ef104d255a). Previously, devices would always be
probed in the order in which they exist on the bus, resulting in the first
device being named 'sda', the second device 'sdb', and so on. .... (so)
Normally you can't rely on device names being consistent between boots

The situation is not a difficult one to manage because one can decide which disk will hold the operating system, and which may be a data disk, and once installed, those installations do not change, rather the change is only in the device name in the form of /dev/sd# which the kernel allocates non-deterministically.

I guess you are familiar with the changes, but for clarification and to enable explanation, the following examples will do.

In this example, the first boot up shows that the disk with the root partition and the system are on
/dev/sda
Code:
$ lsblk
NAME        MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
sda           8:0    0 931.5G  0 disk
├─sda1        8:1    0   512M  0 part /boot/efi
├─sda2        8:2    0     1G  0 part /boot
└─sda3        8:3    0   930G  0 part /
sdb           8:16   0   1.8T  0 disk
└─sdb1        8:17   0   1.8T  0 part /mnt/data
sr0          11:0    1  1024M  0 rom
The following shows the same machine on another boot where the root partition and the system are on
/dev/sdb:
Code:
$ lsblk
NAME        MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
sda           8:16   0   1.8T  0 disk
└─sda1        8:17   0   1.8T  0 part /mnt/data
sdb           8:0    0 931.5G  0 disk
├─sdb1        8:1    0   512M  0 part /boot/efi
├─sdb2        8:2    0     1G  0 part /boot
└─sdb3        8:3    0   930G  0 part /
sr0          11:0    1  1024M  0 rom
The disks have not changed, only the names. It's quite invisible to the user of the machine if they are just using it without looking into the names. In normal usage of the machine there are no functional consequences in terms of performance.

The disks in modern times are actually identified by the system with UUID numbers which can be seen in the
/etc/fstab file which shows all the disks which are mounted or to be mounted for the system. Significantly, the UUID can show the user which disk is allocated to /dev/sda or /dev/sdb, or any other disk.

Apart from the UUID there are multiple ways of identifying the disks and they are all in the /dev filesystem which can be seen here:
Code:
$ ls -al /dev/disk
total 0
drwxr-xr-x  8 root root  160 Sep 14 07:24 .
drwxr-xr-x 18 root root 3720 Sep 14 07:24 ..
drwxr-xr-x  2 root root   80 Sep 14 07:24 by-designator
drwxr-xr-x  2 root root  140 Sep 14 07:24 by-diskseq
drwxr-xr-x  2 root root  300 Sep 14 07:24 by-id
drwxr-xr-x  2 root root  100 Sep 14 07:24 by-partuuid
drwxr-xr-x  3 root root  180 Sep 14 07:24 by-path
drwxr-xr-x  2 root root  100 Sep 14 07:24 by-uuid

To look at identification by UUID:
Code:
$ ls -al /dev/disk/by-uuid
<snip>
lrwxrwxrwx 1 root root  15 Sep 14 07:24 1c2xxxx8-3xx6-4a8b-b9fa-4fxxxxx39534 -> ../../sda3
lrwxrwxrwx 1 root root  15 Sep 14 07:24 12345678-1234-1234-4321-098765432123 -> ../../sdb1
<snip>

The UUID numbers show the disks that are linked to the respective /dev/sd# names.

These same UUID numbers will be the ones that appear in the /etc/fstab file to show the particular disks which are mounted at boot time. Whilst years ago when the /dev/sd# identifications were stable, they appeared in
/etc/fstab, but that's no longer workable in most distros because of the variability, hence they default to the UUID numbers.

A straightforward means of telling which disk is /dev/sda and which is /dev/sdb is to use the serial numbers in the output of the lsblk command. First, one can become acquainted with the disks' serial numbers, so for example there might be a Western Digital with serial number 1234 and a Seagate with serial number 9876, so one can run the following command and get something like the output shown (in this example with the made-up numbers):
Code:
$ lsblk -o NAME,SERIAL
NAME    SERIAL
sda     1234
└─sda1
sdb     9876
├─sdb1
├─sdb2
└─sdb3
sr0
The results show which disk is /dev/sda and which is /dev/sdb, as long as one is aware of the serial numbers which are usually written on the disks somewhere. In this case the Western Digital is shown as /dev/sda etc.

To the point of:
I want my main ssd to be on sda and my backup on sdb.
it should be clear that this is unnecessary because one has the means of never losing track of the names of the disks (shown above), so it's a rather profitless exercise which would require extra software to achieve that and would provide information of no technical functional value.

Note, I haven't dealt with using e2label which is another means of identification.
 
Last edited:
It seems to me that it depends on which cable it is attached to. I plagged in my main drive and it said it was sdb. So I moved it to the other cable and it said sda.
 
It seems to me that it depends on which cable it is attached to. I plagged in my main drive and it said it was sdb. So I moved it to the other cable and it said sda.
The question arises as to how consistent is your finding that a particular cable arrangement always correlates with a particular device name. Perhaps you can check the matter after 10 or more boots and see if the apparent correlation exists. That would be interesting I think.
 
Is the second disk listed as boot drive option in the bios/uefi? (just the device) it would make sense to never swap, if the bios can't identify it as boot device anyway.
yes one is system drive with EFI partition another is data drive.
not sure if "boot drive" applies to UEFI, isn't that BIOS thing with MBR?
 
Well, if you have an EFI boot menu (e.g. F12), are both drives listed?
 
In my case sda / sdb are always same disks, even after system reinstall.
Actually no, I've seen them swapped but only in very rare cases, there is a post of mine somewhere on forums about setting e.g. /dev/sdb in /etc/fstab instead of using UUID= which resulted if failure to mount file system on boot when names change.
I can't find that post.

So @osprey is correct, names are non-deterministic although name change isn't 50:50 chance in my case for some reason.
 


Follow Linux.org

Staff online


Latest posts

Top