How to mount a drive by UUID

How to mount a drive by UUID

This is good to know.

Typically in Linux drives are mounted by device and mount point in /etc/fstab like so:

/dev/sda1   /mnt/backups   ext3   defaults   0 0

Lets say you have an external drive that shows up as /dev/sda1 mounted at /mnt/mountpoint1
and
and internal drive that shows up as /dev/sdb1 mounted at /mnt/mountpoint2

What happens if the external drive is off or fails. When the computer boots it finds what was /dev/sdb1 but now shows up as /dev/sda1 and mounts it at /mnt/mountpoint1 instead of where you want it at /mnt/mounpoint2.

The way to get around this is to mount drives by UUID which are always unique.

To find a UUID of your devices do:

# blkid

Which should look something like this:

# blkid
/dev/mapper/VolGroup00-LogVol01: TYPE="swap"
/dev/mapper/VolGroup00-LogVol00: UUID="39d65932-4c90-4715-be8c-242224a1aaaa" TYPE="ext3"
/dev/hdd1: LABEL="/boot" UUID="89be8219-b081-4c92-a2c8-93a46220084f" TYPE="ext3"
/dev/hdc1: TYPE="ntfs"
/dev/VolGroup00/LogVol00: UUID="39d65932-4c90-4715-be8c-242224a1aaaa" SEC_TYPE="ext2" TYPE="ext3"
/dev/VolGroup00/LogVol01: TYPE="swap"
/dev/hdc2: TYPE="ntfs"
/dev/cdrom: LABEL="CDROM" TYPE="iso9660"
/dev/sda1: UUID="9263eae3-bdba-4707-89bb-a8e0fd4a1c29" TYPE="ext3"
/dev/sdb5: LABEL="seadisk" UUID="1580-DF9C" TYPE="vfat"
/dev/sdc1: LABEL="mybook" UUID="16D5-0EF8" TYPE="vfat"


Well that works for some devices but as you can see /dev/hdc1 & /dev/hdc2 are not showing up. Another way of finding UUID is by doing:

ls -l /dev/disk/by-uuid/

Which shows me the following:

# ls -l /dev/disk/by-uuid/
total 0
lrwxrwxrwx 1 root root 10 Nov 21 15:16 1580-DF9C -> ../../sdb5
lrwxrwxrwx 1 root root 10 Nov 21 15:15 16D5-0EF8 -> ../../sdc1
lrwxrwxrwx 1 root root 10 Nov 21 15:15 89be8219-b081-4c92-a2c8-93a46220084f -> ../../hdd1
lrwxrwxrwx 1 root root 10 Nov 21 15:15 9263eae3-bdba-4707-89bb-a8e0fd4a1c29 -> ../../sda1
lrwxrwxrwx 1 root root 10 Nov 21 15:15 96FC7BDBFC7BB3D9 -> ../../hdc2
lrwxrwxrwx 1 root root 10 Nov 21 15:15 EA24DA9C24DA6AD9 -> ../../hdc1

Now I have the UUIDs for the devices, what do I do with them?

The answer is to add the devices to /etc/fstab by UUID like this:

UUID=9263eae3-bdba-4707-89bb-a8e0fd4a1c29   /mnt/backups   ext3  defaults   0 0

Instead of:

/dev/sdb1   /mnt/backups   ext3   defaults   0 0

This way if the order of the drives ever changes you still get them mounted in the correct locations.

randomness