Skip to main content

Disk Management and Partitioning

In this lesson, we'll explore how Linux manages storage devices and how you can work with disks and partitions. Building on your knowledge of file systems and command line fundamentals, you'll learn to view disk usage, create partitions, format file systems, and mount storage devices.

Learning Goals:

  • View disk usage and available storage
  • Understand disk partitioning concepts
  • Create and manage partitions using fdisk
  • Format partitions with different file systems
  • Mount and unmount file systems
  • Configure automatic mounting via /etc/fstab

Understanding Disk Usage

Before making changes to disks, let's first learn how to check current disk usage and available storage.

Checking Disk Space

The df command displays disk space usage for all mounted file systems:

Check disk usage
df -h

The -h flag shows sizes in human-readable format (KB, MB, GB). You'll see output like:

Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1 20G 12G 7.1G 63% /
/dev/sda2 50G 15G 33G 31% /home

Analyzing Directory Sizes

To see which directories are using the most space, use du:

Check directory sizes
du -h --max-depth=1 /home
tip

Use du -sh /path/to/directory for a quick summary of a directory's total size without listing subdirectories.

Disk Partitioning Fundamentals

Partitioning divides a physical disk into logical sections. Each partition can have its own file system and mount point.

Viewing Disk Information

Use lsblk to list all block devices and their partitions:

List block devices
lsblk

Output shows the disk hierarchy:

NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda 8:0 0 100G 0 disk
├─sda1 8:1 0 20G 0 part /
├─sda2 8:2 0 50G 0 part /home
└─sda3 8:3 0 30G 0 part

Working with Partitions

Using fdisk for Partition Management

fdisk is a command-line utility for disk partitioning. Let's work with a sample disk:

Start fdisk session
sudo fdisk /dev/sdb

Inside fdisk, you can use these commands:

  • n - create new partition
  • d - delete partition
  • p - print partition table
  • w - write changes and exit
  • q - quit without saving
warning

Always double-check which disk you're modifying with fdisk. Writing changes to the wrong disk can destroy your system. Use lsblk first to identify the correct device.

Creating a New Partition

Here's a typical workflow for creating a partition:

Create partition example
# Start fdisk on target disk
sudo fdisk /dev/sdb

# Inside fdisk:
# Press 'n' for new partition
# Choose partition type (primary/extended)
# Accept default partition number
# Set first and last sectors (or size like +10G)
# Press 'w' to write changes

Formatting Partitions

After creating partitions, you need to format them with a file system.

Common File System Types

Format with ext4 file system
sudo mkfs.ext4 /dev/sdb1
Format with XFS file system
sudo mkfs.xfs /dev/sdb1
Format for swap space
sudo mkswap /dev/sdb2
sudo swapon /dev/sdb2

Mounting File Systems

Manual Mounting

To temporarily mount a file system:

Mount partition to directory
sudo mkdir /mnt/mydisk
sudo mount /dev/sdb1 /mnt/mydisk

To unmount:

Unmount file system
sudo umount /mnt/mydisk

Automatic Mounting with /etc/fstab

For permanent mounting, edit /etc/fstab:

Edit fstab file
sudo nano /etc/fstab

Add a line like:

/dev/sdb1   /mnt/mydisk   ext4   defaults   0   2
note

The fstab format is: device mountpoint filesystem options dump pass

  • dump: backup utility flag (0=no backup)
  • pass: fsck order (0=no check, 1=root, 2=other)

Testing fstab Entries

Before rebooting, test your fstab entry:

Test fstab configuration
sudo mount -a

This mounts all file systems in fstab without errors.

Practical Example: Adding a New Disk

Let's walk through a complete example of adding and configuring a new disk:

Complete disk setup workflow
# 1. List available disks
lsblk

# 2. Create partition on new disk
sudo fdisk /dev/sdb
# Use: n, p, 1, Enter, Enter, w

# 3. Format the partition
sudo mkfs.ext4 /dev/sdb1

# 4. Create mount point
sudo mkdir /data

# 5. Add to fstab
echo "/dev/sdb1 /data ext4 defaults 0 2" | sudo tee -a /etc/fstab

# 6. Mount the new file system
sudo mount -a

Common Pitfalls

  • Mounting to non-empty directories: Files in the mount directory become inaccessible until unmounted
  • Wrong device in fstab: Using device names like /dev/sdb1 can change after reboot; consider using UUIDs instead
  • Forgetting to format: Creating partitions without formatting leaves them unusable
  • Not testing fstab: Always run mount -a after editing fstab to catch errors
  • Unmounting busy file systems: Ensure no processes are using the mount point before unmounting

Summary

You've learned how to manage disks and partitions in Linux. Key skills include checking disk usage with df and du, creating partitions with fdisk, formatting with file systems like ext4 and XFS, and configuring both manual and automatic mounting. Remember to always verify disk devices before making changes and test fstab entries to prevent boot issues.

Quiz

Show quiz
  1. Which command shows disk space usage for mounted file systems in human-readable format?
  2. What is the purpose of the mkswap command?
  3. Why should you run mount -a after editing /etc/fstab?
  4. What happens to existing files in a directory when you mount a file system to it?
  5. Which fdisk command writes partition changes to disk?

Answers:

  1. df -h
  2. To format a partition as swap space
  3. To test the fstab configuration and catch errors before reboot
  4. They become inaccessible until the file system is unmounted
  5. w (write)