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:
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:
du -h --max-depth=1 /home
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:
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:
sudo fdisk /dev/sdb
Inside fdisk, you can use these commands:
n- create new partitiond- delete partitionp- print partition tablew- write changes and exitq- quit without saving
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:
# 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
sudo mkfs.ext4 /dev/sdb1
sudo mkfs.xfs /dev/sdb1
sudo mkswap /dev/sdb2
sudo swapon /dev/sdb2
Mounting File Systems
Manual Mounting
To temporarily mount a file system:
sudo mkdir /mnt/mydisk
sudo mount /dev/sdb1 /mnt/mydisk
To unmount:
sudo umount /mnt/mydisk
Automatic Mounting with /etc/fstab
For permanent mounting, edit /etc/fstab:
sudo nano /etc/fstab
Add a line like:
/dev/sdb1 /mnt/mydisk ext4 defaults 0 2
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:
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:
# 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/sdb1can change after reboot; consider using UUIDs instead - Forgetting to format: Creating partitions without formatting leaves them unusable
- Not testing fstab: Always run
mount -aafter 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
- Which command shows disk space usage for mounted file systems in human-readable format?
- What is the purpose of the
mkswapcommand? - Why should you run
mount -aafter editing/etc/fstab? - What happens to existing files in a directory when you mount a file system to it?
- Which
fdiskcommand writes partition changes to disk?
Answers:
df -h- To format a partition as swap space
- To test the fstab configuration and catch errors before reboot
- They become inaccessible until the file system is unmounted
w(write)