Configure systems to mount file systems at boot by universally unique ID (UUID) or label – RHCSA Objective Preparation

While in the desktop world we rarely change our hard drive – and that mostly indicated by hardware failure – in the server world it isn’t uncommon for the underlying storage environment to change over time.

In a SAN (Storage Area Network) environment, for High Availability, a server can reach it’s storage trough many paths, in reality distributed and mirrored to multiple disks in the storage network. If some paths change, the server needs to identify the “disk” again. That’s why it is recommended to use special identifiers set on the device, and mount by these identifiers, not by device name which may change. In this part of RHCSA exam preparation tutorial, we’ll add a new disk to our test machine, and configure mounting by UUID (Universally Unique IDentifier) and by label.

In this tutorial you will learn:

  • How to get UUID of a given device
  • How to get and set label of a device
  • How to mount device by UUID
  • How to mount device by label

Mounting filesystem by label

Mounting filesystem by label.

Software Requirements and Conventions Used

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Red Hat Enterprise Linux 8.0
Software util-linux 2.32.1
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Introduction of the setup

Our test machine is a simple virtual machine running RHEL 8. The operating system is installed on /dev/sda, a 15 GB virtual hard drive. To have a free disk to be used, we added another virtual disk to the machine, which appears as /dev/sdb with the size of 2 GB. We partitioned the disk using fdisk, creating a single partition that occupies the whole new disk. We created an ext4 filesystem on top of that partition, using mkfs.ext4. This will be our demonstration setup.

How to get UUID of a given device

To list the UUID of the devices present in our system, we’ll open a terminal, and use blkid:

# blkid
/dev/sda1: UUID="eef3b378-5272-45f4-ab41-97eb48bda63f" TYPE="xfs" PARTUUID="3c939719-01"
/dev/sda2: UUID="rfezEa-GlgW-jWUX-Zixs-Ydw0-EsZS-nk3JDH" TYPE="LVM2_member" PARTUUID="3c939719-02"
/dev/sr0: UUID="2019-10-10-18-52-14-12" LABEL="VBox_GAs_6.0.14" TYPE="iso9660"
/dev/mapper/rhel-root: UUID="9ba9c1f7-40d7-4eb2-a66b-7b27905d8011" TYPE="xfs"
/dev/mapper/rhel-swap: UUID="c08948ec-2320-4155-92d5-2c9364ccb99b" TYPE="swap"
/dev/sdb: UUID="17c1210c-8a88-42d6-b394-03f491415d5c" TYPE="ext4"

The bold output indicates our new disk, and also the UUID we searched for.



How to get and set label of a device

We can print our device’s label with e2label. In our current state, the output may be misleading, because no label is set so far for this device.

# e2label /dev/sdb

The empty line does not indicate error, but that the label attribute is not set on this device yet. The same utility is capable of setting the label. The syntax is e2label <devicename> <label>:

# e2label /dev/sdb "small_disk"

Another query will show the new label set:

# e2label /dev/sdb                                                                                                                                                                       
small_disk

Also, the blkid utility we used earlier will also present the newly set label:

# blkid | grep sdb
/dev/sdb: LABEL="small_disk" UUID="17c1210c-8a88-42d6-b394-03f491415d5c" TYPE="ext4"

How to mount device by UUID

Using UUID to mount is the recommended way, so on a default RHEL8 install, we can already find an example on how to do this. If we did not modify the disk options on install, the boot device will most likely be mounted by UUID. The configuration for mounting is in the /etc/fstab file, which we can print for this example.

# cat /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Mon Oct 28 13:22:28 2019
#
# Accessible filesystems, by reference, are maintained under '/dev/disk/'.
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
/dev/mapper/rhel-root   /                       xfs     defaults        0 0
UUID=eef3b378-5272-45f4-ab41-97eb48bda63f /boot                   xfs     defaults        0 0
/dev/mapper/rhel-swap   swap                    swap    defaults        0 0

From the above output we can see the syntax we need to use in order to mount our disk by UUID. Using a text editor, we can add the following new fstab entry to the end of the file:

UUID=17c1210c-8a88-42d6-b394-03f491415d5c /mnt/new_disk ext4    defaults        0 0

Please note that the above example uses the same UUID we listed with blkid in the previous section. It will be different on any other system.

We’ll also need the mount point to exist, so we’ll create the directory we mentioned in the above entry:

# mkdir /mnt/new_disk


With the path to mount, the way to mount, and the filesystem to mount ready, all we need to do is mount the device:

# mount /mnt/new_disk/

On successful mount, we can find our 2 GB ext4 filesystem on the specified mount point. The mount command without arguments will list all mounted filesystems, and we can use grep to find the line we are particulary interested in.

# mount | grep sdb
/dev/sdb on /mnt/new_disk type ext4 (rw,relatime,seclabel)

We can also use the df utility to check the mounted filesystem:

# df -h /mnt/new_disk/
Filesystem      Size  Used Avail Use% Mounted on
/dev/sdb        2.0G  6.0M  1.8G   1% /mnt/new_disk

Mounting filesystem by using UUID as identifier in /etc/fstab

Finally, we can unmount the filesystem to prepare for the next step:

# umount /mnt/new_disk


How to mount device by label

Mounting the filesystem by label is practically the same. With the label already set, we can reference it in /etc/fstab. If doing this step with the same device, remember to remove the UUID reference added in the previous step before adding another that uses label:

LABEL=small_disk /mnt/new_disk  ext4    defaults        0 0

We can mount the device the same way as with UUID, by specifying the mount point:

# mount /mnt/new_disk/

This is doable because of the fstab entry that the mount command uses. We can also mount by label directly using the -L flag:

# mount -L small_disk /mnt/new_disk

We used the same disk with both the UUID and label option, so it’s contents will be the same. We can write data on it, unmount it, then mount again using the other method, the data on it will remain the same. Note that this would be the case if the disk where given to another machine (that may already have a /dev/sdb device).

Exercises

  1. Create a labeled disk, add some test data, then remove the disk from the system and add it to another machine. Can the new machine see the data on the disk?
  2. Create a UUID mount point, then umount the filesystem, re-create it, and check the UUID. Can you use the same fstab entry for the new filesystem?
  3. Create a label mount point, then unmount the filesystem, re-create it, and label it with the same name. Is the label-based fstab entry still valid?
  4. Try to label two different disks the same name. Can you mount them?