5

Does Linux attempt to give a drive the same letter each time?
For example, if was to connect two drives to my computer, then disconnect them and reconnect them in reverse order would sdb and sdc correspond to the same physical drives?

This is what I want:

First:  
    Plug in HDD A, which becomes sdb.
    Plug in HDD B, which becomes sdc.


Then:
    Plug in HDD B, which becomes sdc.
    Plug in HDD A, which becomes sdb.

The question is does Linux do this automatically? If not how would I do something like this? (I want to have three drives that can be initialized in any order but still mounted to the same point).

1 Answers1

12

No, the sd* names are assigned sequentially, based on which disk was detected first.

If you need a persistent name, udev already provides them based on several properties such as filesystem labels / UUIDs; partition labels / UUIDs (GPT only); disk attachment paths; SCSI WWNs; and so on.

Take a look at /dev/disk:

┌ rain ~ 
┘ tree /dev/disk/
/dev/disk/
├── by-id (hardware-based ID)
│   ├── ata-SlimtypeDVD_A_DS8A5SH_012160166091 -> ../../sr0
│   ├── ata-ST9640320AS_5WX1ZH91 -> ../../sda
│   ├── ata-ST9640320AS_5WX1ZH91-part7 -> ../../sda7
│   ├── mmc-SD4GB_0x0054b5cf -> ../../mmcblk0
│   ├── mmc-SD4GB_0x0054b5cf-part1 -> ../../mmcblk0p1
│   ├── wwn-0x5000c5002f0e9ce1 -> ../../sda
│   ├── wwn-0x5000c5002f0e9ce1-part1 -> ../../sda1
│   └── …
├── by-label (name encoded in filesystem header)
│   ├── keycard -> ../../mmcblk0p1
│   ├── raindows -> ../../sda6
│   ├── rainhome -> ../../sda5
│   └── …
├── by-partlabel (name encoded in GPT partition table)
│   ├── Arch -> ../../sda4
│   ├── EFI -> ../../sda1
│   ├── home -> ../../sda5
│   ├── swap -> ../../sda8
│   └── …
├── by-partuuid (UUID encoded in GPT partition table)
│   ├── 14420948-2cea-4de7-b042-40f67c618660 -> ../../sda4
│   ├── 1c737f60-8667-4d1a-9c92-5f5caf69be60 -> ../../sda3
│   ├── 267bbb83-0bb5-48b8-aa4c-ffe328328f5b -> ../../sda5
│   └── …
└── by-uuid (UUID encoded in filesystem header)
    ├── 0C5C17E25C17C57C -> ../../sda7
    ├── 413b42fe-77f7-41d0-8d40-a7578f70995d -> ../../sda4
    ├── 4b30e8db-563e-4947-8d41-f242d94a6d3a -> ../../mmcblk0p1
    ├── 8594cc4c-9c42-436a-8723-9a0611b1f97d -> ../../sda5
    └── …

You can use them as such:

/dev/disk/by-label/arch_boot  /boot  ext4  rw,auto  0  1

In fstab, an alternative syntax also works for label and uuid fields:

LABEL=arch_boot               /boot  ext4  rw,auto  0  1

Note: In some older Linux distributions, various udev rules attempt to make the sd* names persistent. But it cannot work reliably; often the "rename" fails because another disk got assigned the desired name. This function was removed in later udev versions. Do not rely on sd* names being persistent, even if they seem to be.

grawity
  • 501,077