Partition Tables
What is a Partition Table?
Before an operating system can use a storage device (such as a hard drive or SSD), it must define how the disk space is divided. This structure is called a partition table.
You can think of a partition table as the table of contents of a hard drive. It tells the system:
- How many sections (partitions) exist on the disk.
- Where each partition begins and ends.
- What type of data each partition contains (Linux filesystem, swap space, etc.).
- Which partition is bootable.
The two most common partition table formats are MBR and GPT.
MBR (Master Boot Record)
The Master Boot Record (MBR) is the traditional disk partitioning system used on older PC hardware.
It occupies the first 512-byte sector of the disk and contains both the partition table and the initial bootloader code.
Key Characteristics
- Location: Stores partition information and bootloader code in the first 512-byte sector of the disk.
- Partition Limit: Supports only 4 primary partitions. To create more partitions, one primary partition must be converted into an extended partition that contains multiple logical partitions.
- Disk Size Limit: Supports disks only up to 2 TB.
- Boot Process: The first stage of the bootloader is stored directly in the MBR sector.
- Robustness: There is no backup of the MBR. If the sector becomes corrupted, the disk may become unreadable.
GPT (GUID Partition Table)
The GUID Partition Table (GPT) is the modern partitioning scheme designed to overcome the limitations of MBR. It is commonly used with UEFI (Unified Extensible Firmware Interface) systems.
Key Characteristics
- Location: GPT stores a primary partition table at the beginning of the disk and a backup copy at the end of the disk.
- Partition Limit: Supports up to 128 partitions by default in Linux without the need for extended or logical partitions.
- Disk Size Limit: Supports disks up to approximately 9.4 Zettabytes (about 9.4 billion terabytes).
- Boot Process: Used with modern UEFI systems. Bootloaders are stored in a special partition called the EFI System Partition (ESP).
- Robustness: GPT uses CRC32 checksums to detect corruption and includes a backup copy of the partition table for recovery.
The Three-Step Process for Storage
Before a Linux system can use a new disk, it must go through three important steps:
- Partitioning: Divide the disk into logical sections.
- Formatting: Create a filesystem on a partition.
- Mounting: Attach the filesystem to a directory in the Linux filesystem tree.
The commands below are the essential tools used for these steps.
Partitioning Tools
fdisk (Legacy MBR & Basic GPT)
Purpose: A classic menu-driven tool used for creating and managing disk partitions. It works best with MBR and simple GPT partition tables.
# Start fdisk on a disk sudo fdisk /dev/sdb
Inside the fdisk interface you can use these commands:
n : Create a new partition d : Delete a partition p : Print the partition table t : Change partition type w : Write changes and exit q : Quit without saving
Important: Changes only take effect after pressing w.
parted (Advanced Partitioning Tool)
Purpose: A more powerful and scriptable partitioning tool. It works better with large disks and advanced GPT partitioning.
# Start parted sudo parted /dev/sdb
Example commands inside parted:
(parted) print (parted) mklabel gpt (parted) mkpart primary ext4 1MiB 5GiB (parted) quit
You can also run it non-interactively:
sudo parted /dev/sdb print
Advantage: It allows human-readable sizes such as MiB and GiB and properly aligns partitions for modern disks.
Formatting (Creating a Filesystem)
mkfs (Make Filesystem)
The mkfs command creates a filesystem on a partition.
# Basic syntax sudo mkfs -t filesystem_type /dev/partition
Examples:
# Create ext4 filesystem sudo mkfs -t ext4 /dev/sdb1 # Short version sudo mkfs.ext4 /dev/sdb1 # Create XFS filesystem sudo mkfs -t xfs /dev/sdb1 # Create FAT32 filesystem sudo mkfs -t vfat /dev/sdb1
Warning: Formatting will erase all data on the partition. Always verify the device name before running the command.
Mounting and Unmounting
mount (Attach a Filesystem)
The mount command attaches a filesystem to a directory called a
mount point.
# Basic syntax sudo mount /dev/partition_name /mount/point # Example sudo mount /dev/sdb1 /mnt
After mounting, files saved in the mount directory are stored on that disk partition.
# Mount in read-only mode sudo mount -o ro /dev/sdb1 /mnt # View mounted filesystems mount
umount (Detach a Filesystem)
The umount command removes the mounted filesystem from the directory tree.
# Unmount using device name sudo umount /dev/sdb1 # Or unmount using mount point sudo umount /mnt
Always unmount a filesystem before removing a drive to avoid data corruption.
If you see the error target is busy, it means a process is using the filesystem.
You may need to exit the directory or find the process using:
lsof /mnt
Checking Disk Usage
df (Disk Free)
The df command shows available and used space on mounted filesystems.
df -h
Example Output:
Filesystem Size Used Avail Use% Mounted on /dev/sda1 20G 8.1G 11G 44% / tmpfs 1.6G 0 1.6G 0% /dev/shm /dev/sdb1 9.8G 37M 9.2G 1% /mnt
Use% quickly shows how full the disk is.
du (Disk Usage)
The du command calculates disk usage for files and directories.
# Show sizes of current directory du -h # Show total size summary du -sh # Check specific directory size du -sh /var/log # Find largest directories du -h . | sort -h
-s: Show summary only.-h: Human-readable format.