23

Currently I have Linux Mint installed on my PC with a USB hard drive partition mounted as /home. This is working well.

If I install a second USB hard drive, is there any chance Linux will get confused between the two, and try mount the second hard drive's partition as /home on boot? That would be bad.

Coming from Windows, I've seen it happen often that drive letters are not "remembered" correctly causing all sorts of issues.

I guess the main question is: How does Linux actually know which USB hard drive is /dev/sdb and which is /media/misha/my_2nd_drive?

  • with a USB hard drive partition mounted as /home Can you please explain how you did this? – A.L Sep 22 '15 at 12:43
  • I did it during Linux Mint installation. At that point it's quite easy. My SSD appeared as sda and the USB hard drive appeared as sdb. I put / and swap on sda, and /home on sdb. But I wouldn't know how to change an existing installation's /home mount, too new for that! – misha256 Sep 22 '15 at 20:40
  • The answers say what you should do, but I don't think they directly say that sd[a-z] are simply named in detection order. No attempt at all is made at keeping the names the same. It's typically deterministic (same kernel on same HW will give the same naming), but a new kernel with an update to the SATA-controller driver could make it scan the SATA drives in the opposite order. Instead of even trying to rename block devices (as is done with nameif or w/e for ethernet device names, usually with udev rules), UUID, label, and id (by drive serial num) names exist. – Peter Cordes Sep 23 '15 at 9:08
  • I've seen servers with SAS disks where rebooting would usually lead to a different ordering of sd[a-z]: the probing of disks is done simultaneously (in parallel) and whatever disk happens to be detected first becomes sda, etc. – wurtel Sep 24 '15 at 9:28
26

Usually the location of the USB port (Bus/Device) determines the order it's detected on. However, don't rely on this.

Each file system has a UUID which stands for universally unique identifier (FAT and NTFS use a slightly different scheme, but they also have an identifier that can be used as a UUID). You can rely on the (Linux) UUID to be unique. For more information about UUIDs, see this Wikipedia article.

Use the disk UUID as a mount argument. To find out what the UUID is, run this:

$ sudo blkid /dev/sdb1

(blkid needs to read the device, hence it needs root powers, hence the sudo. If you've already become root, the sudo is not needed.)

You can then use that UUID in /etc/fstab like this:

UUID=7e839ad8-78c5-471f-9bba-802eb0edfea5 /home ext4 defaults 0 2

There can then be no confusion about what disk is to be mounted on /home.

For manual mounting you can use /dev/disk/by-uuid/.....

  • 1
    Awesome, I didn't even know about fstab until now (that's how new I am to Linux). I've looked at my fstab file and everything is already identified by UUID. The Linux Mint installer did good. – misha256 Sep 22 '15 at 9:48
  • You can improve this answer by explaining briefly what the UUID is, and why it's unique which make it a reliable identifier. – A.L Sep 22 '15 at 12:44
10

If your fstab references partitions as /dev/sdx, then adding a new drive can certainly confuse the system. The recent practice, however, is to use Partition UUIDs to identify the partitions.

If your system has udev installed (must be the case for 2.6+ Linux kernels), you can use persistent naming in fstab. By default, partitions are identified via UUIDs:

/dev/disk/by-uuid/31f8eb0d-612b-4805-835e-0e6d8b8c5591

You can, however create a meaningful partition label like "home" and reference your partition like this:

/dev/disk/by-label/home

Those IDs can be used as a replacement for /dev/sdx in fstab.

  • I wish I could mark your answer correct too. Wurtel was in a touch earlier. But thank you anyway, you get my +1. – misha256 Sep 22 '15 at 9:55
  • @misha256 The irony is that I don't even use UUIDs on my systems (because the output from df and mount becomes unreadable). Internal HDDs have quite high chances of being detected first, and in the same order every time. – Dmitry Grigoryev Sep 22 '15 at 10:05
  • @DmitryGrigoryev I think that must only have been true when UUIDs were first introduced. My /boot, for example, is identified by UUID in /etc/fstab, but it appears as /dev/sda2 in the output from both df and mount. – seumasmac Sep 22 '15 at 18:34
  • 1
    If you use labels, you can use LABEL=home in your fstab. I like to use labels like t-home for the home directory on my machine called tesla. So if I stick the disk in another system, there won't be two filesystems with the same label. This is why Linux installers generate random UUIDs and use them in fstab, instead of having default labels that would give most systems the same labels for their FSes. @misha256, you might want to think about this if you label your FSes. – Peter Cordes Sep 22 '15 at 22:52
  • 1
    @PeterCordes I do indeed use labes, and I have a naming scheme aimed at making these labels unique. – Dmitry Grigoryev Sep 23 '15 at 8:17

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.