16. Disk Management
Whether you are attaching new EBS volumes in AWS or racking physical hard drives in a datacenter, a SysAdmin must know how to partition, format, mount, and manage storage devices in Linux.
16.1 Block Devices & Hardware
Section titled “16.1 Block Devices & Hardware”In Linux, hardware devices are represented as special files located in the /dev (device) directory. Storage drives are known as Block Devices because data is read and written in fixed-size blocks rather than character-by-character.
Storage Types and Naming Conventions
Section titled “Storage Types and Naming Conventions”- HDD / SATA SSDs: Spinning disks or standard SATA solid-state drives.
- Named:
/dev/sda,/dev/sdb,/dev/sdc(whereais the first drive,bis the second).
- Named:
- NVMe (Non-Volatile Memory Express): High-speed flash storage attached directly to the PCIe bus.
- Named:
/dev/nvme0n1,/dev/nvme0n2(where0is the controller, andn1is the namespace/drive).
- Named:
- Virtual / Cloud Disks: Common in AWS EC2, KVM, and QEMU virtual machines.
- Named:
/dev/vda,/dev/xvda.
- Named:
16.2 Partitioning Schemes (MBR vs GPT)
Section titled “16.2 Partitioning Schemes (MBR vs GPT)”Raw disks are divided into logical slices called Partitions before filesystems are created.
| Feature | MBR (Master Boot Record) | GPT (GUID Partition Table) | |||| | Max Disk Capacity | 2 Terabytes (2 TB) | 9.4 Zettabytes (practically unlimited) | | Max Primary Partitions | 4 primary (or 3 primary + 1 extended) | 128 partitions (standard default) | | Partition Table Redundancy | None (single sector at LBA 0) | Primary at disk start, backup at disk end | | Integrity Checks | None | CRC32 checksums of header and partition tables | | Boot Mode Compatibility | Legacy BIOS | UEFI (Unified Extensible Firmware Interface) |
Partitions append a number to the block device name: /dev/sda1 is the first partition on /dev/sda; /dev/nvme0n1p1 is the first partition on NVMe drive /dev/nvme0n1.
16.3 The fdisk Command Deep Dive
Section titled “16.3 The fdisk Command Deep Dive”fdisk is a foundational command-line partitioning utility. Historically used for MBR disks, modern versions of fdisk (from util-linux) also support GPT.
Basic Syntax
Section titled “Basic Syntax”sudo fdisk [options] <device>Example target: /dev/sdb or /dev/nvme0n1.
Command-Line Options
Section titled “Command-Line Options”| Option | Description |
|||
| -l | List partition tables for all or specified block devices |
| -u | Display partition sizes in sectors instead of cylinders |
| -s <partition> | Display partition size in blocks |
| -c | Switch DOS-compatible mode on or off |
| -h, --help | Display usage information |
Interactive Menu Commands
Section titled “Interactive Menu Commands”Running sudo fdisk /dev/sdb opens an interactive menu prompt (Command (m for help):):
| Command | Action | Description |
||||
| m | Help | Display the full list of interactive commands |
| p | Print | Print current in-memory partition table |
| n | New | Create a new partition |
| d | Delete | Delete an existing partition |
| t | Type | Change partition system type (e.g., Linux, LVM, Swap) |
| a | Bootable | Toggle bootable / active flag on a partition (MBR) |
| w | Write | Write changes to disk partition table and exit |
| q | Quit | Quit without saving changes |
Step-by-Step: Creating a Partition
Section titled “Step-by-Step: Creating a Partition”- Inspect attached disks:
Terminal window sudo fdisk -l - Open the disk in
fdisk:Terminal window sudo fdisk /dev/sdb - Type
nand press Enter. - Select partition type (
pfor primary,efor extended in MBR). - Specify the partition number (default:
1). - Press Enter to accept the default first sector (maintains sector alignment).
- Specify the size in the last sector prompt using human-readable syntax:
+10Gfor a 10 GB partition. - Type
pto verify the partition table in memory. - Type
wto commit changes to the physical disk.
Step-by-Step: Deleting a Partition
Section titled “Step-by-Step: Deleting a Partition”- Open the disk:
sudo fdisk /dev/sdb. - Type
d. - If multiple partitions exist, enter the partition number to remove (e.g.,
1). - Type
wto write the updated table.
Informing the Kernel with partprobe
Section titled “Informing the Kernel with partprobe”When you modify a partition table on a live system, the kernel may still retain the old in-memory table. Use partprobe to force the kernel to reread the partition table without rebooting:
sudo partprobe /dev/sdbPartitioning Alternatives
Section titled “Partitioning Alternatives”parted: Modern tool designed for GPT disks and disks larger than 2 TB; supports non-interactive CLI scripting.gdisk: Specifically tailored for GPT partition tables (mimicsfdisksyntax).cfdisk: Curses-based interactive visual partition editor, ideal for fast manual setups.
16.4 Device Identification: lsblk & blkid
Section titled “16.4 Device Identification: lsblk & blkid”Before making any disk modifications, identify device topology and identifiers.
1. lsblk (List Block Devices)
Section titled “1. lsblk (List Block Devices)”Displays block device hierarchies, sizes, partition types, and current mount points:
# Tree view of block deviceslsblk
# Include filesystem types and UUIDslsblk -f2. blkid (Block ID)
Section titled “2. blkid (Block ID)”Prints UUIDs (Universally Unique Identifiers) and filesystem labels:
sudo blkidAlways reference UUIDs rather than /dev/sdX names in permanent configuration files to prevent device name drift across reboots.
16.5 Mounting Filesystems
Section titled “16.5 Mounting Filesystems”A raw partition cannot store files until it is formatted with a filesystem and mounted into the directory hierarchy.
Formatting with mkfs
Section titled “Formatting with mkfs”Format /dev/sdb1 with an ext4 or xfs filesystem:
sudo mkfs.ext4 /dev/sdb1# orsudo mkfs.xfs /dev/sdb1Temporary Mounting (mount & umount)
Section titled “Temporary Mounting (mount & umount)”Attach the formatted partition to an empty directory (mount point):
# Create mount directorysudo mkdir -p /mnt/data
# Mount partitionsudo mount /dev/sdb1 /mnt/data
# Verify mountdf -h | grep /mnt/data
# Unmount filesystemsudo umount /mnt/dataPermanent Mounting (/etc/fstab)
Section titled “Permanent Mounting (/etc/fstab)”To ensure partitions mount automatically across reboots, add an entry to /etc/fstab:
# <file system> <mount point> <type> <options> <dump> <pass>UUID=3a2b1c4d-5e6f-7a8b-9c0d-1e2f3a4b5c6d /mnt/data ext4 defaults,noatime 0 2Test /etc/fstab changes immediately without rebooting:
# Mount all filesystems mentioned in /etc/fstabsudo mount -aIf mount -a returns errors, fix /etc/fstab immediately; a syntax error in /etc/fstab can cause the system to drop into emergency recovery mode on the next boot!
16.6 Monitoring Disk Space: df & du
Section titled “16.6 Monitoring Disk Space: df & du”df(Disk Free): Displays capacity, used space, available space, and percentage utilization per mounted filesystem.Terminal window # Human-readable formatdf -h# Display Inode usage (catches 'No space left on device' due to inode exhaustion)df -ihdu(Disk Usage): Calculates storage consumed by specific directories and files.Terminal window # Check total size of a directorysudo du -sh /var/log# Find the 10 largest directories on the root filesystemsudo du -h --max-depth=1 /var 2>/dev/null | sort -hr | head -n 10
16.7 Storage Operations Quick Reference
Section titled “16.7 Storage Operations Quick Reference”| Task | Primary Command | Notes |
||||
| List block devices | lsblk -f | Shows layout, fs type, mount points |
| Check UUIDs | sudo blkid | Mandatory for /etc/fstab |
| Partition disk | sudo fdisk /dev/sdX | Interactive menu (n, d, p, w) |
| Reload partition table | sudo partprobe | Informs kernel without reboot |
| Format partition | sudo mkfs.ext4 /dev/sdX1 | Writes filesystem metadata |
| Mount partition | sudo mount /dev/sdX1 /mnt/dir | Temporary mount |
| Unmount partition | sudo umount /mnt/dir | Ensure directory is not in use |
| Test fstab syntax | sudo mount -a | Verifies /etc/fstab safely |
| Check filesystem free space | df -h | System-wide disk usage |
| Inspect folder sizes | sudo du -sh /path/* | Identifies space consumers |