Skip to content

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.

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.

  • HDD / SATA SSDs: Spinning disks or standard SATA solid-state drives.
    • Named: /dev/sda, /dev/sdb, /dev/sdc (where a is the first drive, b is the second).
  • NVMe (Non-Volatile Memory Express): High-speed flash storage attached directly to the PCIe bus.
    • Named: /dev/nvme0n1, /dev/nvme0n2 (where 0 is the controller, and n1 is the namespace/drive).
  • Virtual / Cloud Disks: Common in AWS EC2, KVM, and QEMU virtual machines.
    • Named: /dev/vda, /dev/xvda.

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.

fdisk is a foundational command-line partitioning utility. Historically used for MBR disks, modern versions of fdisk (from util-linux) also support GPT.

Terminal window
sudo fdisk [options] <device>

Example target: /dev/sdb or /dev/nvme0n1.

| 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 |

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 |

  1. Inspect attached disks:
    Terminal window
    sudo fdisk -l
  2. Open the disk in fdisk:
    Terminal window
    sudo fdisk /dev/sdb
  3. Type n and press Enter.
  4. Select partition type (p for primary, e for extended in MBR).
  5. Specify the partition number (default: 1).
  6. Press Enter to accept the default first sector (maintains sector alignment).
  7. Specify the size in the last sector prompt using human-readable syntax: +10G for a 10 GB partition.
  8. Type p to verify the partition table in memory.
  9. Type w to commit changes to the physical disk.
  1. Open the disk: sudo fdisk /dev/sdb.
  2. Type d.
  3. If multiple partitions exist, enter the partition number to remove (e.g., 1).
  4. Type w to write the updated table.

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:

Terminal window
sudo partprobe /dev/sdb
  • 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 (mimics fdisk syntax).
  • cfdisk: Curses-based interactive visual partition editor, ideal for fast manual setups.

Before making any disk modifications, identify device topology and identifiers.

Displays block device hierarchies, sizes, partition types, and current mount points:

Terminal window
# Tree view of block devices
lsblk
# Include filesystem types and UUIDs
lsblk -f

Prints UUIDs (Universally Unique Identifiers) and filesystem labels:

Terminal window
sudo blkid

Always reference UUIDs rather than /dev/sdX names in permanent configuration files to prevent device name drift across reboots.

A raw partition cannot store files until it is formatted with a filesystem and mounted into the directory hierarchy.

Format /dev/sdb1 with an ext4 or xfs filesystem:

Terminal window
sudo mkfs.ext4 /dev/sdb1
# or
sudo mkfs.xfs /dev/sdb1

Attach the formatted partition to an empty directory (mount point):

Terminal window
# Create mount directory
sudo mkdir -p /mnt/data
# Mount partition
sudo mount /dev/sdb1 /mnt/data
# Verify mount
df -h | grep /mnt/data
# Unmount filesystem
sudo umount /mnt/data

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 2

Test /etc/fstab changes immediately without rebooting:

Terminal window
# Mount all filesystems mentioned in /etc/fstab
sudo mount -a

If 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!

  • df (Disk Free): Displays capacity, used space, available space, and percentage utilization per mounted filesystem.
    Terminal window
    # Human-readable format
    df -h
    # Display Inode usage (catches 'No space left on device' due to inode exhaustion)
    df -ih
  • du (Disk Usage): Calculates storage consumed by specific directories and files.
    Terminal window
    # Check total size of a directory
    sudo du -sh /var/log
    # Find the 10 largest directories on the root filesystem
    sudo du -h --max-depth=1 /var 2>/dev/null | sort -hr | head -n 10

| 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 |