Mounting, disks, and storage management
Storage devices must be partitioned, formatted, and mounted before use. Linux provides flexible tools for managing disks, volumes, and space.
Block devices and naming
Disks appear as block devices in /dev: SATA and USB disks as /dev/sda, /dev/sdb, and so on, with partitions /dev/sda1, /dev/sda2; NVMe drives as /dev/nvme0n1 with partitions /dev/nvme0n1p1. Because names can change between boots, configuration usually refers to file systems by UUID or label instead. lsblk lists block devices and their mount points; blkid shows UUIDs and types.
Partitioning and formatting
Tools such as fdisk, gdisk, and parted create and edit partition tables. A partition is then formatted with a file system, for example mkfs.ext4 /dev/sdb1 or mkfs.xfs /dev/sdb1, which writes the file system's structures. Formatting erases what was there, so double-checking the device name before running mkfs is essential.
Mounting
mount /dev/sdb1 /data attaches a file system to the directory /data, the mount point; umount /data detaches it, which fails if processes are still using it. findmnt and mount without arguments list mounted file systems. Mount options control behaviour: ro (read-only), noexec (no program execution), nosuid, and noatime among others.
Persistent mounts with fstab
/etc/fstab lists file systems to mount at boot, one per line: the device (preferably UUID=...), mount point, type, options, a dump flag, and an fsck order. For example: UUID=4f1c... /data ext4 defaults,noatime 0 2. A mistake in fstab can stop a system from booting normally, so test with mount -a before rebooting; systemd also generates mount units from it.
Disk usage
df -h shows used and free space per mounted file system; du -sh directory shows how much space a directory tree uses, and du -h --max-depth=1 /var helps locate large subdirectories. A full root file system can break logging, updates, and services, so space is monitored. File systems can also run out of inodes even with free space, shown by df -i.
Logical volume management
LVM adds flexibility: physical volumes (disks or partitions) form volume groups, from which logical volumes are carved and formatted. Logical volumes can be resized, often while mounted, spread across disks, and snapshotted. A common setup puts / and /home on logical volumes so space can be shifted later with lvextend and a file-system resize.
RAID, swap, and network storage
Software RAID with mdadm combines disks for redundancy or speed: RAID 1 mirrors, RAID 5 and 6 add parity, RAID 10 stripes mirrors. Swap space, a partition or file, lets the kernel move inactive memory pages to disk when RAM is short; swapon --show lists it. Network file systems such as NFS and SMB mount remote storage into the tree like local disks.
Worked example
Adding a new 1 TB disk: lsblk shows it as /dev/sdb with no partitions. gdisk creates one partition, /dev/sdb1; mkfs.ext4 /dev/sdb1 formats it; mkdir /data and mount /dev/sdb1 /data attach it; blkid gives its UUID, which is added to /etc/fstab with options defaults. Running mount -a confirms the fstab line works, and df -h /data reports a size of roughly 916 GiB: a 1 TB disk holds about 931 GiB, and file-system metadata takes some of that, while ext4's default 5% reserve for the administrator makes the available figure a little lower still.
Key takeaways
Disks appear as block devices such as /dev/sda and /dev/nvme0n1, with partitions numbered; refer to them by UUID because names can change. fdisk, gdisk, and parted partition; mkfs formats, erasing data. mount attaches file systems to mount points with options such as ro and noexec; umount detaches. /etc/fstab defines boot-time mounts; test with mount -a. df shows free space and inodes; du finds large directories. LVM groups disks into resizable logical volumes with snapshots. mdadm provides software RAID; swap extends memory; NFS and SMB mount network storage.
Flashcard
Device accessed in blocks, such as a disk
Check your understanding
Why should /etc/fstab refer to a disk by UUID rather than /dev/sdb1?
