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).

Billylegota
  • 161
  • 4

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.

u1686_grawity
  • 426,297
  • 64
  • 894
  • 966
  • All of my persistent disks are mounted by UUID, or via LVM (which uses the UUID of the PV underneath). That includes /boot. After all, what happens if you plug in another hard drive from another Linux system (which you may well do at some point)? In any case you should never count on the `sd*` names being consistent. – Michael Hampton Jul 28 '15 at 01:36
  • So I could just use a shell script to look at the UUID of a HDD and mount it to a certain point based on its UUID right? EX: if uuid == xxxxxxxxxx then mount /dev/sdb/ whatever/my/mount/point/is – Billylegota Jul 28 '15 at 17:27
  • @Billylegota: Huh? No. You can use UUIDs directly in /etc/fstab. I even gave two examples for exactly that. What do you need a shell script for? – u1686_grawity Jul 28 '15 at 18:07
  • @grawity: Sorry, didn't see that part. Just saw the tree of /dev/disk and said "looks like a job for bash" ;D – Billylegota Jul 28 '15 at 18:32
  • Is there a way to consistently get the disk instead of the partition? /dev/sda instead of /dev/sda1? – sep332 Jun 17 '16 at 19:00
  • @sep332: There are by-id and by-path names for both. (With some udev rule adjustments there could be by-diskuuid names as well.) – u1686_grawity Jun 18 '16 at 09:56