How to Mount a Hard Disk on Ubuntu 24.04 (fdisk, mkfs, fstab)

When you attach a new data disk to a server, lsblk shows it, but you cannot use it yet. It has no partition and no file system. A manual mount does not survive a reboot. A wrong /etc/fstab entry can drop the system into emergency mode at boot. This post shows the full procedure on Ubuntu 24.04: check the new disk, create a GPT partition, format it as ext4, mount it, and add it to fstab by UUID. It also shows how nofail and the device timeout affect the boot process.

The examples use a 2 GB image file on a loop device (/dev/loop5) as the new disk. On a real disk, use /dev/sdb (partition /dev/sdb1) or /dev/nvme1n1 (partition /dev/nvme1n1p1) instead.

$ sudo truncate -s 2G /var/tmp/disk2.img
$ sudo losetup -fP --show /var/tmp/disk2.img
/dev/loop5

Check the new disk

A disk without partitions has no child entries in lsblk. fdisk -l shows no partition table.

$ lsblk /dev/loop5
NAME  MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
loop5   7:5    0   2G  0 loop

$ sudo fdisk -l /dev/loop5
Disk /dev/loop5: 2 GiB, 2147483648 bytes, 4194304 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

Create a partition (fdisk)

In fdisk, type g (new GPT table), n (new partition, press Enter three times for the defaults), and w (write). The example sends the same keys through a pipe.

$ printf 'g\nn\n\n\n\nw\n' | sudo fdisk /dev/loop5

Welcome to fdisk (util-linux 2.39.3).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table.
Created a new DOS (MBR) disklabel with disk identifier 0xb5bdefb3.

Command (m for help): Created a new GPT disklabel (GUID: 530C323E-8DBD-4A44-9A42-139707542B4F).

Command (m for help): Partition number (1-128, default 1): First sector (2048-4194270, default 2048): Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-4194270, default 4192255):
Created a new partition 1 of type 'Linux filesystem' and of size 2 GiB.

Command (m for help): The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

$ lsblk /dev/loop5
NAME      MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
loop5       7:5    0   2G  0 loop
└─loop5p1 259:0    0   2G  0 part

For scripts, use parted because it does not need interactive input. Use GPT by default, because MBR does not support disks larger than 2 TiB.

sudo parted -s /dev/sdb mklabel gpt mkpart data ext4 1MiB 100%

Create the file system

You can use the label from -L later as LABEL=data. The UUID in the output is the value for fstab.

$ sudo mkfs.ext4 -L data /dev/loop5p1
mke2fs 1.47.0 (5-Feb-2023)
Discarding device blocks: done
Creating filesystem with 523776 4k blocks and 131072 inodes
Filesystem UUID: f24a53fb-f307-4cf5-9f57-3e544a6b111e
Superblock backups stored on blocks:
	32768, 98304, 163840, 229376, 294912

Allocating group tables: done
Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done

$ sudo blkid /dev/loop5p1
/dev/loop5p1: LABEL="data" UUID="f24a53fb-f307-4cf5-9f57-3e544a6b111e" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="13edb1c8-39ff-41f1-8e9b-8aa42dc8dbfe"

Right after you format the partition, lsblk -f can show empty fields. lsblk reads the udev database, which is not updated yet. Use blkid, or run sudo udevadm trigger and check again.

$ lsblk -f /dev/loop5
NAME      FSTYPE FSVER LABEL UUID FSAVAIL FSUSE% MOUNTPOINTS
loop5
└─loop5p1

Mount the partition

Create the mount point and mount the partition. root owns the top-level directory of a new file system, so change the owner if a regular user must write to it.

$ sudo mkdir -p /data
$ sudo mount /dev/loop5p1 /data

$ df -hT /data
Filesystem     Type  Size  Used Avail Use% Mounted on
/dev/loop5p1   ext4  2.0G   24K  1.9G   1% /data

$ findmnt /data
TARGET SOURCE       FSTYPE OPTIONS
/data  /dev/loop5p1 ext4   rw,relatime

$ sudo chown $USER:$USER /data
$ touch /data/hello && ls -l /data
total 16
-rw-rw-r-- 1 noble noble     0 Oct 18 16:25 hello
drwx------ 2 root  root  16384 Oct 18 16:25 lost+found

Add the disk to fstab to keep it after a reboot

Device names such as /dev/sdb1 can change when the disk order changes, so use the UUID. Back up the original file before you edit it.

# <device>                                <mount point> <type> <options>          <dump> <fsck>
UUID=f24a53fb-f307-4cf5-9f57-3e544a6b111e /data         ext4   defaults,nofail  0      2
FieldValueMeaning
DeviceUUID=… / LABEL=… / PARTUUID=…Get it from blkid. Do not use /dev/sdX
Mount point/dataA directory that you created before
Typeext4, xfs, vfat …TYPE in blkid
Optionsdefaults,nofaildefaults=rw,suid,dev,exec,auto,nouser,async. nofail continues the boot if the device is missing
dump0Not used in practice
fsck0 / 1 / 21 is for the root file system only, 2 for data disks, 0 for no check

After you edit fstab, run daemon-reload first, because systemd generates mount units from fstab. Then use mount -a to mount all fstab entries that are not mounted yet. This tests the syntax without a reboot.

$ sudo cp /etc/fstab /etc/fstab.bak
$ sudo umount /data
$ echo 'UUID=f24a53fb-f307-4cf5-9f57-3e544a6b111e /data ext4 defaults,nofail 0 2' | sudo tee -a /etc/fstab
UUID=f24a53fb-f307-4cf5-9f57-3e544a6b111e /data ext4 defaults,nofail 0 2

$ sudo systemctl daemon-reload
$ sudo mount -a
$ findmnt /data
TARGET SOURCE       FSTYPE OPTIONS
/data  /dev/loop5p1 ext4   rw,relatime

$ systemctl list-units --type=mount | grep data
  data.mount                    loaded active mounted /data

Check fstab with findmnt –verify

Run findmnt --verify before a reboot to find errors. The example adds an entry with a UUID that does not exist.

$ sudo findmnt --verify

0 parse errors, 0 errors, 1 warning
none
   [W] non-bind mount source /swap.img is a directory or regular file

$ echo 'UUID=0000-typo /backup ext4 defaults 0 2' | sudo tee -a /etc/fstab
UUID=0000-typo /backup ext4 defaults 0 2

$ sudo findmnt --verify

0 parse errors, 2 errors, 2 warnings
none
   [W] non-bind mount source /swap.img is a directory or regular file
/backup
   [E] unreachable on boot required target: No such file or directory
   [E] unreachable on boot required source: UUID=0000-typo
   [W] your fstab has been modified, but systemd still uses the old version;
       use 'systemctl daemon-reload' to reload

You can ignore the /swap.img warning, because it is the default Ubuntu swap file. If an entry has an [E] error and no nofail, the system boots into emergency mode.

nofail and the device timeout

The next example reboots without the loop device to simulate a removed disk. Because of nofail, the boot completes, but systemd waits 90 seconds for the device by default.

$ systemctl status data.mount --no-pager | head -6
○ data.mount - /data
     Loaded: loaded (/etc/fstab; generated)
     Active: inactive (dead)
      Where: /data
       What: /dev/disk/by-uuid/f24a53fb-f307-4cf5-9f57-3e544a6b111e
       Docs: man:fstab(5)

$ journalctl -b | grep -E 'dev-disk-by.*uuid-f24a53fb|data.mount' | head -5
Oct 18 16:26:44 noble systemd[1]: Expecting device dev-disk-by\x2duuid-f24a53fb\x2df307\x2d4cf5\x2d9f57\x2d3e544a6b111e.device - /dev/disk/by-uuid/f24a53fb-f307-4cf5-9f57-3e544a6b111e...
Oct 18 16:28:19 noble systemd[1]: dev-disk-by\x2duuid-f24a53fb\x2df307\x2d4cf5\x2d9f57\x2d3e544a6b111e.device: Job dev-disk-by\x2duuid-f24a53fb\x2df307\x2d4cf5\x2d9f57\x2d3e544a6b111e.device/start timed out.
Oct 18 16:28:19 noble systemd[1]: Timed out waiting for device dev-disk-by\x2duuid-f24a53fb\x2df307\x2d4cf5\x2d9f57\x2d3e544a6b111e.device - /dev/disk/by-uuid/f24a53fb-f307-4cf5-9f57-3e544a6b111e.
Oct 18 16:28:19 noble systemd[1]: Dependency failed for systemd-fsck@dev-disk-by\x2duuid-f24a53fb\x2df307\x2d4cf5\x2d9f57\x2d3e544a6b111e.service - File System Check on /dev/disk/by-uuid/f24a53fb-f307-4cf5-9f57-3e544a6b111e.
Oct 18 16:28:19 noble systemd[1]: Dependency failed for data.mount - /data.

For removable disks or disks that can be missing, set a shorter wait with x-systemd.device-timeout. With the same condition, systemd stops waiting after 5 seconds.

$ tail -1 /etc/fstab
UUID=f24a53fb-f307-4cf5-9f57-3e544a6b111e /data ext4 defaults,nofail,x-systemd.device-timeout=5s 0 2

$ journalctl -b | grep -E 'Expecting device dev-disk-by.x2duuid-f24a|Timed out waiting' | cut -c1-80
Oct 18 16:28:17 noble systemd[1]: Expecting device dev-disk-by\x2duuid-f24a53fb\
Oct 18 16:28:22 noble systemd[1]: Timed out waiting for device dev-disk-by\x2duu

Unmount the partition

If a shell or process uses the mount point, umount fails with target is busy. Use fuser -vm to find the process.

$ cd /data && sudo umount /data
umount: /data: target is busy.

$ sudo fuser -vm /data
                     USER        PID ACCESS COMMAND
/data:               root     kernel mount /data
                     noble     27251 ..c.. bash
                     root      27426 ..c.. sudo

$ cd ~ && sudo umount /data && findmnt /data; echo "exit=$?"
exit=1

c in the ACCESS column means that the process uses the directory as its current directory. When the shell leaves the directory, the unmount succeeds.

Troubleshooting

SymptomCauseAction
The mount is gone after a rebootYou ran mount but did not add the disk to fstabAdd it to fstab by UUID and test with mount -a
The system boots into emergency modeThe fstab device is missing or the UUID has a typo, and there is no nofailRun findmnt –verify before a reboot. Use nofail for data disks
The boot hangs for about 90 secondsEven with nofail, systemd waits for the device until the default timeoutSet a shorter wait, for example x-systemd.device-timeout=5s
A change in fstab has no effectsystemd uses mount units that it made from the old fstabsudo systemctl daemon-reload
A different partition is mounted after the disk order changesThe entry uses a /dev/sdX nameUse UUID or PARTUUID
Data is lost after mkfsYou formatted a disk that was in useCheck with lsblk -f and blkid that FSTYPE is empty first
lsblk -f does not show the UUIDThe udev database is not updated yetUse blkid, or update it with udevadm trigger
umount: target is busyA process uses the mount point as its current directory or has open files on itFind it with fuser -vm or lsof +f — /data

Conclusion

To add a new disk, follow these steps: check it with lsblk, create a GPT partition with fdisk or parted, run mkfs, and mount it. Then add it to fstab by UUID, and run daemon-reload, mount -a, and findmnt --verify. For a disk that can be missing, add nofail and a short x-systemd.device-timeout so that a missing disk does not block the boot.

References

Leave a Reply