How to use UUID unique identifiers to consistently mount partitions

botond published 2019. 12. 23., h - 17:25 time

Content

 

Introductory

A Universally Unique Identifier (UUID) a unique identifier that Linux systems also use to identify different volumes / partitions so that the mountings on different block devices do not change even if the connection order of the hard disks in the computer changes, such as during a maintenance. In this description, we will look at how to use UUIDs in fstab instead of the device names in our file so that we can mount our partitions in a consistent way.

 

 

Attach partitions based on their device names

Automatically assign device names

Block devices on the hard disks and their partitions installed in the computer a / Dev available in a directory. For example, a hard drive in today's computer configuration that is placed on the very first (0) SATA port sDA device name, which can be accessed via / dev / sda. If you create the first partition on your hard disk, that is the partition sda1 will be named. And the second partition is sda2 and so on. When we mount the second hard drive to the next SATA port, that is sdb and the partitions on it are named sdb1, sdb2, sdb3, and so on.

Reading device names

To use partitions, you need to know the names of these devices to manage them. For example, these names are lsblk read:

lsblk

The command gives an output similar to the following:

NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0 111,8G  0 disk 
├─sda1   8:1    0  95,9G  0 part /
├─sda2   8:2    0     1K  0 part 
└─sda5   8:5    0  15,9G  0 part [SWAP]
sdb      8:16   0 894,3G  0 disk 
├─sdb1   8:17   0   512M  0 part 
├─sdb2   8:18   0  55,9G  0 part 
└─sdb3   8:19   0  18,6G  0 part 
sdc      8:32   0 931,5G  0 disk 
├─sdc1   8:33   0 404,9G  0 part /mnt/drive-d
└─sdc2   8:34   0 526,7G  0 part /mnt/websites
sdd      8:48   0 465,8G  0 disk 
└─sdd1   8:49   0 465,8G  0 part 

Hard disks and the partitions on them are beautiful. But if we want more information about our devices, then it is Fdisk command's list switch to query as root:

fdisk -l

This gives a more detailed output, of which I will only highlight the sdd device that I will mount now:

[...]

Disk /dev/sdd: 465,8 GiB, 500107862016 bytes, 976773168 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: dos
Disk identifier: 0x5cd681f4

Device     Boot Start       End   Sectors   Size Id Type
/dev/sdd1        2048 976773119 976771072 465,8G  7 HPFS/NTFS/exFAT

Automatically mount partitions in the fstab file

If you want to mount a new partition automatically when the machine starts, all you have to do is put the appropriate line in your fstab file. To do this, open the file:

sudo nano /etc/fstab

Then, insert a line with the name of your device:

/dev/sdd1      /mnt/hdd_500    ntfs    defaults 0  2

So here you will enter / dev / sdd1 as your device name, followed by the mount point, followed by the file system. The following sections may remain by default.

Then run the mount command that executes the mountings configured in the fstab file, so you don't have to restart your computer:

sudo mount -a

If we configure our line correctly, this command will not output. Then look at the lsblk command on our devices again:

lsblk

then we can see the difference: the device already shows the mount point:

NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0 111,8G  0 disk 
├─sda1   8:1    0  95,9G  0 part /
├─sda2   8:2    0     1K  0 part 
└─sda5   8:5    0  15,9G  0 part [SWAP]
sdb      8:16   0 894,3G  0 disk 
├─sdb1   8:17   0   512M  0 part 
├─sdb2   8:18   0  55,9G  0 part 
└─sdb3   8:19   0  18,6G  0 part 
sdc      8:32   0 931,5G  0 disk 
├─sdc1   8:33   0 404,9G  0 part /mnt/drive-d
└─sdc2   8:34   0 526,7G  0 part /mnt/websites
sdd      8:48   0 465,8G  0 disk 
└─sdd1   8:49   0 465,8G  0 part /mnt/hdd_500

This will allow you to use the partition on your new hard drive, which will be available immediately after the machine is rebooted.

All of this works nicely until you come to a computer for service, refurbishment, expansion, etc., and we vary the hard drives in the machine. In this case, the sdX names also change. Thus, when the machine is restarted, devices mounted in this way will not be mounted at best, e.g. they are mounted to a changed file system, or in the worst case, to the wrong location, which can even lead to data loss, depending on the previous role of that mount point.

To avoid this and mount our drives consistently, we use UUIDs.

 

 

Attach partitions using UUIDs

Using UUIDs, you can avoid the problem mentioned above, so you can always change the order in which your HDDs are connected to your computer. The installers of the various Linux distributions have already taken care of this, for example, the partitions created and used during the installation of Debian and Ubuntu are already configured by UUID in the fstab file. Example of an Ubuntu fstab file:

Ubuntu 18.04 LTS (Bionic Beaver) - Fstab file using UUID

And a Debian 10 Buster fstab file:

Debian 10 (Buster) - Fstab file using UUID

The file is a bit more eloquent here, and even the information about the device names of the mounted partitions during installation was included.

In order to be able to mount partitions that you have added yourself in this way, you must first read the UUID of that device.

Read UUIDs

UUIDs can be obtained in several ways, the easiest being a BLKI command. First, run the lsblk command to see a list of devices with their dimensions, and then the blkid command to find the UUIDs of the devices:

lsblk
sudo blkid

For me this output is:

Output of lsblk and blkid commands

Now that you have your UUIDs, you just need to make a small change to the fstab file.

Apply UUID to the fstab file

We need to modify our previously configured fstab entry:

/dev/sdd1      /mnt/hdd_500    ntfs    defaults 0  2

In the output of the blkid command, look for the UUID part of your device and modify the fstab file accordingly:

UUID=06FFD55A2EDBE8B2      /mnt/hdd_500    ntfs    defaults 0  2

Let's save it, and you're done.

 

 

Attach partitions using tags

If we are already here, it is worth mentioning that there is such an option that we can also attach devices to the partitions based on the given labels. Although these tags are more eloquent than a UUID, there is a chance that one might give the same tag to two different devices. So I prefer to use the UUID, but we'll quickly get through the use of tags if we already have that option.

Read and set tags

Unlike the first two methods, here we can now not only read the labels, but also name our devices ourselves. There are several commands for this purpose, each of which can only handle specific file system (s). For example:

With these commands, make sure you never use them on mounted file systems!

Remaining with my own example, I select the command that matches the NTFS file system of my previously configured partition, in this case ntfslabel. I first unmount the file system, tentatively read the label of the device, which is still empty, then set a custom label for me and read it again. All this on the screen in one:

Configure the NTFS partition label with the ntfslabel command

Set the label in the fstab file

Next, I change the last setting of my / etc / fstab file as follows:

#UUID=06FFD55A2EDBE8B2      /mnt/hdd_500    ntfs    defaults 0  2
LABEL=toshiba_hdd_500_gb    /mnt/hdd_500    ntfs    defaults 0  2

I comment on the previous UUID setting, and on a new line, LABEL = I modify the beginning of the line.

Here, of course, everyone should use a label that is as sure as possible unique within their own computer. So it is advisable to include the HDD manufacturer, size, other info. I also write everything in lower case, so the chances of making a mistake are reduced. So if we look at our fstab file, we immediately see which of our drives is connected to our system.

When you're done, run mount:

mount -a

And from now on, the device will be attached based on the label.

 

 

Conclusion

These would be the different mount methods for the partitions in the fstab file. With the last two, you can be sure that changing or upgrading the HDDs on your computer will not hurt your head because of changes in the mount points, because the same units are always plugged into the same mount point.