Effective disk management is a cornerstone of maintaining healthy, performant, and reliable Linux systems. Whether you’re a system administrator, developer, or hobbyist, understanding how to manage disks, partitions, and filesystems in Linux is critical for ensuring data integrity, optimizing storage usage, and troubleshooting issues. This guide will break down the fundamentals of Linux disk management, from core concepts like disks and partitions to essential tools and best practices. By the end, you’ll be equipped to partition disks, format filesystems, monitor storage usage, and avoid common pitfalls.
Table of Contents
- Fundamental Concepts
- Disks and Block Devices
- Partitions (MBR vs. GPT)
- Filesystems
- Mount Points
- Essential Disk Management Tools
lsblk: List Block Devicesfdisk/parted: Partition Disksmkfs: Create Filesystemsmount/umount: Mount/Unmount Filesystemsdf/du: Check Disk Usagefsck: Check and Repair Filesystems- LVM (Logical Volume Manager) Basics
- Common Practices
- Partitioning a New Disk
- Resizing Partitions
- Monitoring Disk Usage
- Best Practices
- Backup Before Changes
- Use GPT Over MBR
- Encrypt Sensitive Data
- Avoid Overfilling Disks
- Conclusion
- References
Fundamental Concepts
Disks and Block Devices
In Linux, storage devices (HDDs, SSDs, NVMe drives, USBs) are treated as block devices. These are accessed via special files in /dev/, e.g., /dev/sda (first SATA disk), /dev/nvme0n1 (first NVMe drive), or /dev/mmcblk0 (SD card).
Block devices are divided into partitions (e.g., /dev/sda1, the first partition on /dev/sda), which are then formatted with a filesystem to store data.
Partitions: MBR vs. GPT
Partitions are logical divisions of a disk. Two partition table formats dominate:
| Feature | MBR (Master Boot Record) | GPT (GUID Partition Table) |
|---|---|---|
| Max Disk Size | 2 TiB | 9.4 ZB (virtually unlimited) |
| Max Partitions | 4 primary (or 3 primary + 1 extended) | 128 (default; configurable) |
| Recovery | No built-in redundancy | Includes CRC checks and backup GPT |
| Compatibility | Older systems (BIOS) | Modern systems (UEFI/BIOS) |
Recommendation: Use GPT for all new disks, as it supports larger sizes and more partitions.
Filesystems
A filesystem organizes data on a partition. Common Linux filesystems include:
| Filesystem | Use Case | Max File Size | Max Partition Size |
|---|---|---|---|
ext4 | Default for many Linux distros (stable, fast) | 16 TiB | 1 EiB |
XFS | High-throughput, large files (e.g., databases) | 8 EiB | 8 EiB |
Btrfs | Advanced features (snapshots, RAID) | 16 EiB | 16 EiB |
tmpfs | In-memory (temporary storage, e.g., /tmp) | RAM-dependent | RAM-dependent |
Mount Points
Linux uses a single directory tree, where all partitions/filesystems are “mounted” to specific directories (mount points). For example:
/: Root filesystem (required)./home: User data (often on a separate partition)./mnt/data: Temporary mount for external drives.
Permanent mounts are defined in /etc/fstab.
Essential Disk Management Tools
lsblk: List Block Devices
lsblk (list block devices) shows all disks, partitions, and their mount points.
Example:
lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 238.5G 0 disk
├─sda1 8:1 0 512M 0 part /boot/efi
├─sda2 8:2 0 200G 0 part /
└─sda3 8:3 0 38.5G 0 part /home
sdb 8:16 0 100G 0 disk
└─sdb1 8:17 0 100G 0 part /mnt/backup
NAME: Device path (e.g.,sda,sdb1).SIZE: Capacity of the device/partition.MOUNTPOINT: Where the partition is mounted (if any).
parted: Partition Disks (GPT/MBR)
parted is a powerful tool for creating/modifying partition tables (supports GPT and MBR).
Example: Create a GPT partition on /dev/sdb
- Launch
partedfor the disk:sudo parted /dev/sdb - Create a GPT partition table:
(parted) mklabel gpt - Create a primary partition (use
100%to use all space):(parted) mkpart primary ext4 1MiB 100% - Verify:
(parted) print Model: ATA VBOX HARDDISK (scsi) Disk /dev/sdb: 107GB Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags: Number Start End Size File system Name Flags 1 1049kB 107GB 107GB primary - Exit:
(parted) quit
mkfs: Create Filesystems
After partitioning, format the partition with mkfs (make filesystem).
Examples:
- Create an
ext4filesystem:sudo mkfs.ext4 /dev/sdb1 # sdb1 is the new partition - Create an
XFSfilesystem:sudo mkfs.xfs /dev/sdb1
mount/umount: Mount/Unmount Filesystems
Temporarily mount a partition to a directory (e.g., /mnt/data):
sudo mkdir /mnt/data
sudo mount /dev/sdb1 /mnt/data
To unmount (ensure no files are in use):
sudo umount /mnt/data
Persistent Mounts with /etc/fstab
To mount partitions automatically at boot, add an entry to /etc/fstab. Use blkid to get the partition’s UUID (more reliable than /dev/sdb1, which can change):
blkid /dev/sdb1 # Output: /dev/sdb1: UUID="abc123..." TYPE="ext4"
Add to /etc/fstab:
UUID=abc123... /mnt/data ext4 defaults 0 2
defaults: Use default mount options (rw, suid, dev, exec, auto, nouser, async).0: Dump frequency (0 = disable).2: Fsck order (2 = check after root filesystem; 0 = skip).
Test the entry:
sudo mount -a # Mounts all fstab entries; no output = success
df/du: Check Disk Usage
df -h: Show disk space usage (human-readable):df -h Filesystem Size Used Avail Use% Mounted on /dev/sda2 197G 50G 137G 27% / /dev/sdb1 99G 60M 94G 1% /mnt/datadu -sh /path: Show directory size (summarize, human-readable):du -sh /var/log # 2.3G /var/log
fsck: Check and Repair Filesystems
fsck (filesystem check) repairs corrupted filesystems. Never run on mounted filesystems (risk of data loss).
Example (check ext4 partition):
sudo umount /dev/sdb1 # Unmount first
sudo fsck.ext4 -f /dev/sdb1 # -f = force check even if "clean"
LVM Basics (Logical Volume Manager)
LVM lets you create flexible “logical volumes” (LVs) that span multiple disks. Key steps:
- Create physical volumes (PVs) from disks/partitions:
sudo pvcreate /dev/sdb1 /dev/sdc1 # Use two disks for flexibility - Create a volume group (VG) from PVs:
sudo vgcreate my_vg /dev/sdb1 /dev/sdc1 # VG named "my_vg" - Create a logical volume (LV) from the VG:
sudo lvcreate -L 150G -n my_lv my_vg # 150GB LV named "my_lv" - Format and mount the LV:
sudo mkfs.ext4 /dev/my_vg/my_lv sudo mount /dev/my_vg/my_lv /mnt/lvm_data
Common Practices
Partitioning a New Disk: Step-by-Step
- Identify the disk with
lsblk(e.g.,/dev/sdb). - Create a GPT partition table with
parted. - Create a partition and format it with
mkfs. - Mount temporarily with
mount, then add tofstabfor persistence.
Resizing a Partition
Example: Expand an ext4 partition (requires unmounting):
- Use
partedto resize the partition (ensure free space exists):sudo parted /dev/sdb (parted) resizepart 1 # Resize partition 1 (parted) 100% # Use all free space - Resize the filesystem to fill the partition:
sudo resize2fs /dev/sdb1 # For ext4; use xfs_growfs for XFS
Monitoring Disk Usage
- Set up alerts for high disk usage (e.g., 90% full) with tools like
Nagiosor simple scripts:# Example: Alert if /mnt/data exceeds 90% df -h /mnt/data | awk 'NR==2 {gsub("%",""); if($5>90) print "ALERT: /mnt/data is " $5 "% full"}'
Best Practices
- Backup Data First: Always back up critical data before partitioning, formatting, or resizing disks.
- Use GPT: Avoid MBR unless supporting legacy systems.
- Encrypt Sensitive Data: Use
LUKS(Linux Unified Key Setup) to encrypt partitions:sudo cryptsetup luksFormat /dev/sdb1 # Encrypt partition sudo cryptsetup open /dev/sdb1 my_crypt # Open encrypted partition sudo mkfs.ext4 /dev/mapper/my_crypt # Format the decrypted volume - Regularly Check Filesystems: Run
fsckduring maintenance (e.g., boot from a live CD for root partitions). - Separate Partitions: Split
/(root),/home, and/varto isolate user data and logs from system files. - Avoid Overfilling: Keep disks below 90% usage to prevent performance degradation and corruption.
Conclusion
Linux disk management is a critical skill for maintaining robust systems. By mastering tools like lsblk, parted, mkfs, and LVM, and following best practices like backups and GPT partitioning, you can ensure efficient storage usage, data safety, and system reliability.
Start small—practice partitioning a USB drive or virtual disk—and gradually tackle advanced tasks like LVM or encryption. The Linux man pages and community documentation are invaluable resources as you learn!