dotlinux guide

Linux Disk Management 101: Tools and Techniques

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

  1. Fundamental Concepts
    • Disks and Block Devices
    • Partitions (MBR vs. GPT)
    • Filesystems
    • Mount Points
  2. Essential Disk Management Tools
    • lsblk: List Block Devices
    • fdisk/parted: Partition Disks
    • mkfs: Create Filesystems
    • mount/umount: Mount/Unmount Filesystems
    • df/du: Check Disk Usage
    • fsck: Check and Repair Filesystems
    • LVM (Logical Volume Manager) Basics
  3. Common Practices
    • Partitioning a New Disk
    • Resizing Partitions
    • Monitoring Disk Usage
  4. Best Practices
    • Backup Before Changes
    • Use GPT Over MBR
    • Encrypt Sensitive Data
    • Avoid Overfilling Disks
  5. Conclusion
  6. 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:

FeatureMBR (Master Boot Record)GPT (GUID Partition Table)
Max Disk Size2 TiB9.4 ZB (virtually unlimited)
Max Partitions4 primary (or 3 primary + 1 extended)128 (default; configurable)
RecoveryNo built-in redundancyIncludes CRC checks and backup GPT
CompatibilityOlder 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:

FilesystemUse CaseMax File SizeMax Partition Size
ext4Default for many Linux distros (stable, fast)16 TiB1 EiB
XFSHigh-throughput, large files (e.g., databases)8 EiB8 EiB
BtrfsAdvanced features (snapshots, RAID)16 EiB16 EiB
tmpfsIn-memory (temporary storage, e.g., /tmp)RAM-dependentRAM-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

  1. Launch parted for the disk:
    sudo parted /dev/sdb
  2. Create a GPT partition table:
    (parted) mklabel gpt
  3. Create a primary partition (use 100% to use all space):
    (parted) mkpart primary ext4 1MiB 100%
  4. 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
  5. Exit:
    (parted) quit

mkfs: Create Filesystems

After partitioning, format the partition with mkfs (make filesystem).

Examples:

  • Create an ext4 filesystem:
    sudo mkfs.ext4 /dev/sdb1  # sdb1 is the new partition
  • Create an XFS filesystem:
    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/data
  • du -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:

  1. Create physical volumes (PVs) from disks/partitions:
    sudo pvcreate /dev/sdb1 /dev/sdc1  # Use two disks for flexibility
  2. Create a volume group (VG) from PVs:
    sudo vgcreate my_vg /dev/sdb1 /dev/sdc1  # VG named "my_vg"
  3. Create a logical volume (LV) from the VG:
    sudo lvcreate -L 150G -n my_lv my_vg  # 150GB LV named "my_lv"
  4. 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

  1. Identify the disk with lsblk (e.g., /dev/sdb).
  2. Create a GPT partition table with parted.
  3. Create a partition and format it with mkfs.
  4. Mount temporarily with mount, then add to fstab for persistence.

Resizing a Partition

Example: Expand an ext4 partition (requires unmounting):

  1. Use parted to resize the partition (ensure free space exists):
    sudo parted /dev/sdb
    (parted) resizepart 1  # Resize partition 1
    (parted) 100%  # Use all free space
  2. 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 Nagios or 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

  1. Backup Data First: Always back up critical data before partitioning, formatting, or resizing disks.
  2. Use GPT: Avoid MBR unless supporting legacy systems.
  3. 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
  4. Regularly Check Filesystems: Run fsck during maintenance (e.g., boot from a live CD for root partitions).
  5. Separate Partitions: Split / (root), /home, and /var to isolate user data and logs from system files.
  6. 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!

References