ubuntu, updated
Format and mount
Put a file system on a partition or volume, mount it, and make the mount survive reboots with /etc/fstab.
Add a new data disk: the full recipe
lsblk # 1. find the disk, e.g. sdb
sudo parted -s /dev/sdb mklabel gpt mkpart data ext4 0% 100% # 2. one partition
sudo mkfs.ext4 -L data /dev/sdb1 # 3. format (label "data")
sudo mkdir -p /data # 4. create the mount point
sudo blkid /dev/sdb1 # 5. copy the UUID
sudoedit /etc/fstab # 6. add a line (below)
sudo systemctl daemon-reload && sudo mount -a # 7. mount everything from fstab
df -h /data # 8. check
For LVM, use /dev/data-vg/data-lv instead of /dev/sdb1 (see LVM volumes).
Formatting
| File system | Command |
|---|---|
| ext4 | sudo mkfs.ext4 -L LABEL /dev/sdb1 |
| XFS | sudo mkfs.xfs -L LABEL /dev/sdb1 |
| exFAT (USB sticks) | sudo mkfs.exfat /dev/sdb1 |
| Swap | sudo mkswap /dev/sdb2 && sudo swapon /dev/sdb2 |
Formatting erases everything on the device. Not sure which one to pick? File systems compared.
/etc/fstab
Each line is one mount that happens at boot:
# device mount point type options dump pass
UUID=3f2a9c1e-7b4d-4e1a-9c2f-1a2b3c4d5e6f /data ext4 defaults,nofail 0 2
/dev/data-vg/logs-lv /var/log/app xfs defaults 0 2
| Field | Meaning |
|---|---|
| device | Use UUID=... for partitions (stable), or the /dev/VG/LV path for LVM |
| options | defaults is fine. Add nofail for disks that may be missing, so boot doesn’t stop. Add noatime to save writes. |
| dump | Always 0 |
| pass | Order of the file system check at boot: 0 none, 1 root only, 2 everything else |
Always test before rebooting. A broken fstab line can drop the server into emergency mode:
sudo findmnt --verify # checks fstab for errors
sudo mount -a # mounts everything; errors show up now, not at boot
Mount and unmount by hand
| What | Command |
|---|---|
| Mount (until reboot) | sudo mount /dev/sdb1 /data |
| Mount everything in fstab | sudo mount -a |
| Unmount | sudo umount /data |
| Remount read-only | sudo mount -o remount,ro /data |
| Mount an ISO | sudo mount -o loop file.iso /mnt |
| Network share (SMB) | sudo mount -t cifs //server/share /mnt -o username=USER (needs cifs-utils) |
“target is busy”
You can’t unmount while something uses the folder, even a shell that has cd’d into it.
sudo fuser -vm /data # who uses it?
sudo lsof +f -- /data # same, more detail
Stop those processes (or cd out), then unmount again.