How To Get Mounted Drive UUID for /etc/fstab on Debian 12

How To Get Mounted Drive UUID for /etc/fstab on Debian 12

Overview

In modern Linux systems, with systemd, drive mapping by name only (e.g /dev/sda1) is not a good idea, as these mappings change from time to time. Instead, it is better to get the UUID for the drive, and have /etc/fstab mount that instead. It guarantees the correct drive is mounted to the correct location.

Get the Desired Drive UUID

In this example, we have an iscsi mounted drive mounted to /srv/storage. The problem here is that on successive reboots, it is not guaranteed to be at /dev/sdb1.

# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# systemd generates mount units based on this file, see systemd.mount(5).
# Please run 'systemctl daemon-reload' after making changes here.
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
/dev/mapper/root-root /               ext4    errors=remount-ro 0       1
/dev/mapper/root-boot /boot           ext4    defaults        0       2
/dev/mapper/root-log /var/log        ext4    defaults        0       2
/dev/sr0        /media/cdrom0   udf,iso9660 user,noauto     0       0
/dev/sdb1	/srv/storage	ext4	_netdev 	0	0
192.168.76.8:/mnt/media			  /srv/nas02     	  nfs	  rw,hard,rsize=8192,wsize=8192,proto=tcp	0	0

To address this, we will first need the UUID for this mounted drive.

sudo blkid /dev/sdb1

/dev/sdb1: UUID="e5f1b9ec-afc9-4212-8e4d-e803032e4401" BLOCK_SIZE="4096" TYPE="ext4" PARTLABEL="primary" PARTUUID="abb3941e-68b9-44e8-8d14-3467164fa6ea"

Use the Drive UUID in /etc/fstab

We will take this UUID, and use it to tell fstab this is the drive we want mounted at /srv/storage

# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# systemd generates mount units based on this file, see systemd.mount(5).
# Please run 'systemctl daemon-reload' after making changes here.
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
/dev/mapper/root-root /               ext4    errors=remount-ro 0       1
/dev/mapper/root-boot /boot           ext4    defaults        0       2
/dev/mapper/root-log /var/log        ext4    defaults        0       2
/dev/sr0        /media/cdrom0   udf,iso9660 user,noauto     0       0
UUID=e5f1b9ec-afc9-4212-8e4d-e803032e4401	/srv/storage	ext4	_netdev 	0	0
192.168.76.8:/mnt/media			  /srv/nas02     	  nfs	  rw,hard,rsize=8192,wsize=8192,proto=tcp	0	0

After this change has been made, reload systemd and run mount -a to ensure the mount is good. We can then use df -H to see the mounted drive.

sudo systemctl daemon-reload
sudo mount -a
df -H -x tmpfs
Filesystem               Size  Used Avail Use% Mounted on
udev                     4.2G     0  4.2G   0% /dev
/dev/mapper/root-root     32G  7.2G   23G  25% /
/dev/mapper/root-boot    964M  149M  749M  17% /boot
/dev/mapper/root-log     9.8G   61M  9.2G   1% /var/log
192.168.76.8:/mnt/media   48T   25T   24T  52% /srv/nas02
/dev/sdb1                1.1T   94G  933G  10% /srv/storage