Linux Labs EspañolES

Storage: disks, LVM and RAID Labs

Partition, format and mount a disk by UUID

Foundation20 minYour VMRHEL and Debian families

Objective

A GPT disk with one aligned partition carries XFS labeled "lab", mounted at /srv/lab from an fstab entry that uses its UUID and cannot block the boot.

Requirements

  • A VM where you have sudo. The lab creates disk images under /var/tmp and attaches them as loop devices, so no spare disk is needed.

Setup

bash
sudo truncate -s 2G /var/tmp/lab-disk1.img
sudo losetup -fP --show /var/tmp/lab-disk1.img    # prints the device, for example /dev/loop0
lsblk /dev/loop0

Steps

  1. Create a GPT label and one partition from 1 MiB to the end. Check the alignment.
  2. Create XFS with the label "lab" on the partition.
  3. Add an fstab line for /srv/lab by UUID, with options that keep a missing device from stopping the boot.
  4. Validate fstab, reload systemd and mount everything.

Verification

bash
findmnt -o TARGET,SOURCE,FSTYPE,OPTIONS /srv/lab
lsblk -f /dev/loop0
findmnt --verify
grep /srv/lab /etc/fstab
Show the solution
bash
D=/dev/loop0      # the device losetup printed
sudo parted -s $D mklabel gpt mkpart lab xfs 1MiB 100%
sudo parted -s $D align-check optimal 1
sudo mkfs.xfs -L lab "$D"p1
UUID=$(sudo blkid -s UUID -o value "$D"p1)
sudo mkdir -p /srv/lab
echo "UUID=$UUID /srv/lab xfs defaults,nofail,x-systemd.device-timeout=10s 0 0" | sudo tee -a /etc/fstab
findmnt --verify --verbose
sudo systemctl daemon-reload
sudo mount -a

Loop devices are not attached at boot, so after a reboot this mount is skipped. With nofail, that skip is harmless, which is exactly the point.

Clean up:

bash
sudo umount /srv/lab
sudo sed -i '\#/srv/lab#d' /etc/fstab && sudo systemctl daemon-reload
sudo losetup -d /dev/loop0 && sudo rm /var/tmp/lab-disk1.img