Ubuntu 26.04 Tutorial / Prepare an Additional Disk and Configure fstab Mounts
Configure persistent mounting by connecting the actual filesystem UUID to a mountpoint. Visible files alone do not prove writes reach the intended disk; test reboot and missing-device conditions too.
Identify the device before any formatting
lsblk -o NAME,SIZE,TYPE,FSTYPE,LABEL,UUID,MOUNTPOINTS
findmnt -rno SOURCE,TARGET,FSTYPE,OPTIONS
The mounting exercise starts with a separate practice disk that already has a filesystem. Compare capacity, label, and UUID and exclude root, boot, and existing backup devices. If you have only a new empty disk, complete the preparation below first. Do not format a disk whose existing data needs recovery.
Prepare a new empty lab disk with ext4
Attaching a new VDI differs from preparing an ext4 filesystem. To create a new empty 8 GiB practice disk in VirtualBox, shut down the VM and use Settings → Storage → the storage controller’s add-hard-disk action to create a new virtual disk. Do not select an existing data disk or a physical host disk.
Use the script below from the Server console only for one new empty VDI. It needs util-linux lsblk/wipefs/fdisk, udevadm, and e2fsprogs mkfs.ext4. Check tools with command -v and review package sources if installation is needed. First match the new disk’s capacity, model, and serial in the hypervisor with its /dev/disk/by-id path; stop if uncertain.
Within fdisk, use g for a GPT table and n for a new partition. Default number/start/end values can be used, but review the target and range with p before saving with w. Cancel with q before w if wrong. This is not a procedure for reinitializing data disks; existing signatures, partitions, or mounts stop the script.
Skip this initialization for a disk that already has a filesystem and data to preserve; verify its device and UUID in the mounting exercise below. Save the complete script as ~/prepare-empty-lab-disk.sh and run bash ~/prepare-empty-lab-disk.sh /dev/disk/by-id/ACTUAL_NEW_DISK. It requires retyping the actual device before partitioning and again before filesystem creation. Record ext4, label lab-data, and UUID afterward; configuration rollback cannot recover formatted original data.
set -euo pipefail
if [ "$#" -ne 1 ]; then
printf 'Usage: bash prepare-empty-lab-disk.sh /dev/disk/by-id/ACTUAL_NEW_DISK\n' >&2; exit 2
fi
case "$1" in /dev/disk/by-id/*) ;; *) printf 'Use the verified by-id path.\n' >&2; exit 2 ;; esac
disk=$(readlink -e -- "$1")
test -b "$disk"
if [ "$(lsblk -dnro TYPE "$disk")" != disk ] || [ "$(lsblk -dnro RO "$disk")" != 0 ]; then
printf 'Not a writable whole disk.\n' >&2; exit 1
fi
if [ "$(lsblk -nr -o NAME "$disk" | wc -l)" -ne 1 ]; then
printf 'Partitions or dependent devices already exist; stop.\n' >&2; exit 1
fi
if lsblk -nr -o MOUNTPOINTS "$disk" | grep -q '[^[:space:]]'; then
printf 'A mount is present; stop.\n' >&2; exit 1
fi
signatures=$(sudo wipefs --no-act --noheadings --output TYPE "$disk")
if [ -n "$signatures" ]; then
printf 'Existing signatures: %s; stop.\n' "$signatures" >&2; exit 1
fi
lsblk -o NAME,SIZE,TYPE,MODEL,SERIAL,FSTYPE,MOUNTPOINTS "$disk"
read -r -p "Type the verified empty disk path ($disk): " confirmed
if [ "$confirmed" != "$disk" ]; then exit 1; fi
sudo fdisk "$disk"
sudo udevadm settle
mapfile -t partitions < <(lsblk -nrpo NAME,TYPE "$disk" | awk '$2 == "part" {print $1}')
if [ "${#partitions[@]}" -ne 1 ]; then
printf 'Expected one newly created partition; inspect before formatting.\n' >&2; exit 1
fi
partition=${partitions[0]}
if findmnt -rn --source "$partition" >/dev/null; then
printf 'The partition is mounted; stop.\n' >&2; exit 1
fi
signatures=$(sudo wipefs --no-act --noheadings --output TYPE "$partition")
if [ -n "$signatures" ]; then printf 'Partition already has a signature; stop.\n' >&2; exit 1; fi
lsblk -o NAME,SIZE,TYPE,FSTYPE,MOUNTPOINTS "$disk"
read -r -p "Type the new partition to format as ext4 ($partition): " confirmed
if [ "$confirmed" != "$partition" ]; then exit 1; fi
sudo mkfs.ext4 -L lab-data "$partition"
sudo blkid "$partition"
lsblk -f "$disk"
If a separate ext4 practice disk is already prepared, skip initialization and start with UUID identification. Do not write to the same virtual disk from multiple VMs. Use the verified partition UUID for persistent mounting below.
The fdisk manual explains partition editing and disk targets.
The mke2fs manual describes mkfs.ext4 filesystem creation and its effects.
Understand system mounts and service dependencies in fstab
Manage persistent Server mounts through the actual UUID, mountpoint, filesystem, and options in /etc/fstab. Directory creation or one successful mount differs from mounting after reboot, so check backups, validation, and reboot separately. For service data, design dependencies preventing writes when the required mount fails.
Failure policy differs between essential service data and optional removable storage. nofail can permit boot to continue but does not prevent an app from writing into an empty local mount directory when the disk is absent.
Back up and prepare a mountpoint
(
set -eu
backup=$(sudo mktemp /root/fstab-backup.XXXXXX)
sudo cp -a /etc/fstab "$backup"
printf 'Backup: %s\n' "$backup"
sudo mkdir -p /srv/lab-data
)
If the target is already used or contains files, preserve them and choose an appropriate empty location. Mounting can hide existing local files underneath. Creating a directory does not mount the disk.
Add one entry with the actual UUID
Open sudoedit /etc/fstab, preserve existing entries, and add one line. The example assumes an ext4 practice filesystem; replace the UUID placeholder with the actual value and do not use the ext4 type for another filesystem.
UUID=REPLACE_WITH_VERIFIED_UUID /srv/lab-data ext4 defaults,nofail,x-systemd.device-timeout=10s 0 2
This example treats the device as optional at boot. Essential service storage needs a policy and dependencies that prevent service startup when mounting fails. Paths containing spaces also require fstab-specific escaping.
(
set -eu
sudo findmnt --verify --verbose
sudo systemctl daemon-reload
sudo mount /srv/lab-data
findmnt --mountpoint /srv/lab-data
)
Resolve verification errors first and mount only the intended entry. Targeting one mountpoint limits impact compared with mounting every fstab entry. Match findmnt’s actual source and filesystem to the verified UUID.
Write only after verifying the mount
expected_uuid=REPLACE_WITH_VERIFIED_UUID
actual_uuid=$(findmnt -rn --mountpoint /srv/lab-data -o UUID || true)
if [ "$actual_uuid" != "$expected_uuid" ]; then
printf 'Expected disk is not mounted; stop the write job.\n' >&2
exit 1
fi
printf 'Verified the expected mounted filesystem.\n'
Save the check as a separate Bash script such as ~/check-lab-mount.sh, substitute the real UUID, and run bash ~/check-lab-mount.sh. Failure exits the script with 1; subsequent writes must depend on success. Test a practice file and reopen it after reboot. Test device absence only after safe shutdown or completion of storage activity.
On failure, restore only the changed fstab entry through the console and validate again. Also verify that backup or service jobs stop rather than write locally when the expected disk is absent.
The systemd.mount reference explains fstab options and service dependencies.




