Linux Disk Management

Comprehensive guide to managing storage devices in Linux, including disk partitioning, filesystem creation, mounting, and maintenance. Learn essential commands and best practices for storage administration.

⚠️ Warning: Disk management operations can result in data loss. Always backup important data before making changes to disk partitions or filesystems.

Overview

Linux disk management involves several key concepts and tools:

  • Block devices - Physical storage devices (HDDs, SSDs, USB drives)
  • Partitions - Logical divisions of storage devices
  • Filesystems - Data organization structures (ext4, xfs, btrfs)
  • Mount points - Directory locations where filesystems are accessed
  • Partition tables - MBR (legacy) or GPT (modern) partition schemes

Essential Commands

Command Purpose Example
lsblk List block devices lsblk -f
fdisk Partition management (MBR) fdisk /dev/sda
parted Advanced partitioning (GPT/MBR) parted /dev/sda
mkfs Create filesystems mkfs.ext4 /dev/sda1
mount Mount filesystems mount /dev/sda1 /mnt
umount Unmount filesystems umount /mnt
df Show disk usage df -h
du Directory usage du -sh /home

Listing Disks and Partitions

View all block devices

lsblk

Shows tree view of all block devices and their mount points

Show filesystem information

lsblk -f

Displays filesystem types, UUIDs, and labels

List partition tables

sudo fdisk -l

Shows detailed partition information for all disks

Show disk usage

df -h

Displays mounted filesystems with human-readable sizes

Creating Partitions

Using fdisk (MBR partition table)

sudo fdisk /dev/sdb # Interactive commands: # n - create new partition # p - primary partition # 1 - partition number # [Enter] - first sector (default) # +10G - partition size # w - write changes

Creates a 10GB primary partition on /dev/sdb

Using parted (GPT partition table)

sudo parted /dev/sdb (parted) mklabel gpt (parted) mkpart primary ext4 0% 10GB (parted) quit

Creates GPT partition table and 10GB partition

Non-interactive parted

sudo parted /dev/sdb mklabel gpt sudo parted /dev/sdb mkpart primary ext4 0% 100%

Creates GPT table and partition using entire disk

Formatting Partitions

Create ext4 filesystem

sudo mkfs.ext4 /dev/sdb1

Formats partition with ext4 filesystem

Create ext4 with label

sudo mkfs.ext4 -L "MyData" /dev/sdb1

Creates ext4 filesystem with volume label

Create XFS filesystem

sudo mkfs.xfs /dev/sdb1

Formats partition with XFS filesystem

Create FAT32 filesystem

sudo mkfs.fat -F32 /dev/sdb1

Creates FAT32 filesystem (useful for USB drives)

Mounting Filesystems

Mount partition temporarily

sudo mkdir /mnt/mydisk sudo mount /dev/sdb1 /mnt/mydisk

Creates mount point and mounts partition

Mount with specific options

sudo mount -o rw,noatime /dev/sdb1 /mnt/mydisk

Mounts with read-write and noatime options

Unmount filesystem

sudo umount /mnt/mydisk

Safely unmounts the filesystem

Permanent mounting (/etc/fstab)

# Add to /etc/fstab: /dev/sdb1 /mnt/mydisk ext4 defaults 0 2 # Or using UUID: UUID=12345678-1234-1234-1234-123456789012 /mnt/mydisk ext4 defaults 0 2

Configures automatic mounting at boot

Disk Usage Monitoring

Show filesystem usage

df -h

Displays used and available space for mounted filesystems

Show directory sizes

du -sh /home/*

Shows size of each directory in /home

Find large files

find /home -type f -size +100M -exec ls -lh {} \;

Finds files larger than 100MB in /home

Monitor disk usage in real-time

watch -n 1 'df -h'

Updates disk usage display every second

Advanced Operations

Resize ext4 filesystem

# Unmount first sudo umount /dev/sdb1 # Check filesystem sudo e2fsck -f /dev/sdb1 # Resize filesystem sudo resize2fs /dev/sdb1

Resizes ext4 filesystem to use all available partition space

Check filesystem integrity

sudo fsck /dev/sdb1

Checks and repairs filesystem errors

Clone disk with dd

sudo dd if=/dev/sda of=/dev/sdb bs=64K conv=noerror,sync status=progress

Creates exact copy of disk (use with extreme caution)

⚠️ Warning: The dd command can overwrite data permanently. Double-check source (if) and destination (of) devices before running.

Best Practices

Safety Guidelines
  • Always backup important data before disk operations
  • Use lsblk to verify device names before partitioning
  • Test mount operations before adding to /etc/fstab
  • Use UUIDs instead of device names in /etc/fstab for stability
  • Unmount filesystems properly before removing devices
Performance Tips
  • Use GPT partition tables for disks larger than 2TB
  • Align partitions to 1MB boundaries for SSD performance
  • Consider noatime mount option to reduce write operations
  • Use appropriate filesystem for your use case (ext4, xfs, btrfs)
  • Monitor disk health with SMART tools

Common Issues and Solutions

Device is busy error

# Find processes using the device sudo lsof /dev/sdb1 # Or use fuser sudo fuser -m /dev/sdb1

Identifies processes preventing unmount

Partition table corrupted

# Try to repair with parted sudo parted /dev/sdb rescue # Or recreate partition table sudo parted /dev/sdb mklabel gpt

Attempts to recover or recreate partition table

Filesystem errors

# Force filesystem check sudo fsck -f /dev/sdb1 # Automatic repair sudo fsck -y /dev/sdb1

Checks and repairs filesystem corruption

See also