Linux Disk & Storage Management Cheatsheet
A practical reference for managing Linux disks and storage systems, covering block device inspection, MBR/GPT partitioning, cloud/VPS volume expansion with growpart, the complete LVM stack (Physical Volumes, Volume Groups, Logical Volumes), filesystem formatting (ext4/XFS), swap allocation, and emergency troubleshooting for full disks.
Block Device & Disk Space Inspection
# List all block devices in a tree hierarchy
lsblk
lsblk -f # Include filesystem type, label, and UUID
lsblk -o NAME,SIZE,FSTYPE,TYPE,MOUNTPOINTS,UUID
# Check filesystem disk space usage (human-readable)
df -h
df -hT # Show filesystem types (ext4, xfs, btrfs)
df -i # Check Inode usage (critical when disk appears full but space remains)
# Estimate directory space usage
du -sh /var/log # Total size of a specific directory
du -sh * | sort -hr # Show size of files and folders in current dir, sorted
du -ahx / | sort -rh | head -20 # Top 20 largest files and directories in one filesystem
# Partition table and device information
sudo fdisk -l # Detailed view of all disks and partition tables
sudo blkid # List UUIDs and filesystem types for all block devices
# Rescan SCSI bus (detect enlarged virtual disks in Cloud/VMs without rebooting)
echo 1 | sudo tee /sys/class/block/sda/device/rescan
# Or rescan all SCSI controller hosts:
for host in /sys/class/scsi_host/host*/scan; do echo "- - -" | sudo tee $host; doneDisk Partitioning (fdisk & parted)
Note: Use
fdiskfor standard MBR/GPT disks (< 2TB). For large disks (> 2TB), GPT withpartedis strongly recommended.
Using fdisk (MBR / GPT)
sudo fdisk /dev/sdb
# Interactive commands:
# m - Display help menu
# p - Print current partition table
# n - Create a new partition (primary/extended, first/last sectors)
# t - Change partition type (e.g., 8e for Linux LVM)
# d - Delete a partition
# w - Write changes to disk and exit
# q - Quit without saving changesUsing parted (GPT for Disks > 2TB)
sudo parted /dev/sdb
# Interactive or non-interactive subcommands:
mklabel gpt # Create a new GPT partition table (erases existing data)
mkpart primary ext4 0% 100% # Create a single partition using 100% of the disk
print # Display partition status
align-check optimal 1 # Verify partition 1 is optimally aligned for I/O performance
quit # Exit
# Non-interactive one-liner:
sudo parted -s /dev/sdb mklabel gpt mkpart primary ext4 0% 100%Cloud & VPS Disk Expansion (growpart & Filesystem Resize)
When resizing a virtual disk in cloud providers (AWS EBS, DigitalOcean, GCP, Linode, Proxmox, Hetzner), the underlying virtual disk expands, but the partition table and filesystem do not grow automatically. Use growpart to expand the partition online with zero downtime.
1. Install growpart
# Debian / Ubuntu
sudo apt update && sudo apt install -y cloud-guest-utils
# RHEL / CentOS / Rocky / AlmaLinux
sudo dnf install -y cloud-utils-growpart2. Run growpart on Target Partition
Notice: There is a space between the disk device name (/dev/sda or /dev/vda) and the partition number (1, 2, etc.).
# Check disk name and partition numbers
lsblk
# Expand root partition (e.g., /dev/sda partition 1)
sudo growpart /dev/sda 1
# VirtIO disk (KVM / QEMU / Proxmox)
sudo growpart /dev/vda 1
# NVMe disk (note the partition number is separated by space even with 'p1' device names)
# Target device: /dev/nvme0n1p1
sudo growpart /dev/nvme0n1 13. Expand the Filesystem (Online Resize)
Run the appropriate command matching your filesystem type:
# For ext4 filesystems (use the partition device path):
sudo resize2fs /dev/sda1
# For XFS filesystems (use the mount point directory, NOT the device name!):
sudo xfs_growfs /
sudo xfs_growfs /mnt/data
# Verify newly expanded capacity
df -h4. Expanding Partitions under LVM
If the expanded partition is an LVM Physical Volume:
# 1. Expand the physical partition holding the PV
sudo growpart /dev/sda 2
# 2. Resize the LVM Physical Volume
sudo pvresize /dev/sda2
# 3. Extend the Logical Volume and resize the underlying filesystem (-r) in one step
sudo lvextend -l +100%FREE /dev/mapper/ubuntu--vg-ubuntu--lv -rLogical Volume Manager (LVM)
LVM provides flexible storage allocation across multiple layers: Disk/Partition > Physical Volume (PV) > Volume Group (VG) > Logical Volume (LV) > Filesystem.
1. Physical Volume (PV)
# Create PV on whole disks or partitions
sudo pvcreate /dev/sdb
sudo pvcreate /dev/sdc1
sudo pvcreate /dev/sdd /dev/sde # Multiple disks at once
# Display PV status
sudo pvs # Compact summary
sudo pvdisplay # Detailed allocation and Physical Extent (PE) info
sudo pvscan # Scan system for all PVs
# Resize & remove PV
sudo pvresize /dev/sdb # Re-read disk size after hardware expansion
sudo pvremove /dev/sdb # Wipe LVM label from disk2. Volume Group (VG)
# Create a new VG
sudo vgcreate vg_data /dev/sdb
sudo vgcreate -s 16M vg_data /dev/sdb # Custom PE size (default 4MB)
# Display VG status
sudo vgs # Summary of total and available free space
sudo vgdisplay vg_data # Detailed VG metadata
# Extend or reduce VG capacity
sudo vgextend vg_data /dev/sdc # Add new PV to the VG
sudo vgreduce vg_data /dev/sdc # Remove PV from VG (ensure data has moved first)
# Activation & maintenance
sudo vgchange -a y vg_data # Activate all LVs in the VG
sudo vgrename vg_data vg_storage # Rename VG
sudo vgcfgbackup vg_data # Backup VG metadata to /etc/lvm/backup/3. Logical Volume (LV)
# Create a new LV
sudo lvcreate -L 50G -n lv_app vg_data # 50GB LV named lv_app
sudo lvcreate -l 100%FREE -n lv_data vg_data # Allocate all remaining VG free space
sudo lvcreate -l 50%VG -n lv_backup vg_data # Allocate 50% of total VG size
# Display LV status
sudo lvs # Compact LV summary
sudo lvdisplay /dev/vg_data/lv_app # Detailed LV info
# Extend LV (Online & Safe)
# Use -r to automatically resize the underlying filesystem (ext4 / XFS)
sudo lvextend -L +20G /dev/vg_data/lv_app -r
sudo lvextend -l +100%FREE /dev/vg_data/lv_app -r
# Reduce LV - EXT4 ONLY (XFS DOES NOT SUPPORT SHRINKING!)
# 1. Unmount partition first
sudo umount /mnt/app
# 2. Check filesystem integrity
sudo e2fsck -f /dev/vg_data/lv_app
# 3. Shrink filesystem and LV together (-r handles both)
sudo lvreduce -L -10G /dev/vg_data/lv_app -r
# 4. Remount
sudo mount /dev/vg_data/lv_app /mnt/app
# Remove an LV
sudo lvremove /dev/vg_data/lv_app4. LVM Snapshots
Snapshots record Copy-On-Write changes for instant point-in-time backups before system upgrades or maintenance.
# Create a 10GB snapshot of an origin LV
sudo lvcreate -L 10G -s -n lv_app_snap /dev/vg_data/lv_app
# Monitor snapshot space consumption (snap_percent)
sudo lvs -a -o lv_name,lv_size,origin,snap_percent
# Merge / restore snapshot back to origin volume
sudo lvconvert --merge /dev/vg_data/lv_app_snap
# (Note: Merge completes when the volume is deactivated/remounted or on next reboot)
# Delete snapshot
sudo lvremove /dev/vg_data/lv_app_snap5. Online Storage Migration (pvmove)
Move data off a failing or retiring disk to a new disk live without stopping running services:
# Move all data from /dev/sdb to other PVs in the same VG
sudo pvmove /dev/sdb
# Move data specifically to target disk /dev/sdc
sudo pvmove /dev/sdb /dev/sdc
# Report progress every 5 seconds
sudo pvmove -i 5 /dev/sdbFilesystem Formatting & Repair
Initialize a filesystem on a newly created partition or Logical Volume before mounting.
# Format as ext4 (Stable, flexible, supports shrinking)
sudo mkfs.ext4 -L DATA /dev/sdb1
sudo mkfs.ext4 -L APPDATA /dev/vg_data/lv_app
# Format as XFS (Default on RHEL/Rocky, optimized for high throughput and large files)
sudo mkfs.xfs -L DATA /dev/sdb1
# Format as FAT32 / NTFS (Cross-platform compatibility for USB drives)
sudo mkfs.vfat -F 32 /dev/sdb1
sudo mkfs.ntfs -f /dev/sdb1
# Filesystem Check & Repair (Must be unmounted first!)
sudo fsck -fy /dev/sdb1 # Auto-check and repair filesystem
sudo e2fsck -f /dev/sdb1 # ext4 check
sudo xfs_repair /dev/sdb1 # XFS repairMounting & Persistent /etc/fstab
# Manual mounting
sudo mkdir -p /mnt/storage
sudo mount /dev/sdb1 /mnt/storage
sudo mount -o ro /dev/sdb1 /mnt/storage # Mount read-only
# Unmounting
sudo umount /mnt/storage
sudo umount -l /mnt/storage # Lazy unmount (detaches immediately when busy)
sudo fuser -vm /mnt/storage # Check processes holding the mount point
sudo fuser -km /mnt/storage # Force-kill processes locking the mount point
# Retrieve partition UUID for persistent mounting
sudo blkid /dev/sdb1
# Example output: /dev/sdb1: UUID="3fa201b2-..." TYPE="ext4"
# Add persistent configuration in /etc/fstab
# Format: <file system> <mount point> <type> <options> <dump> <pass>
# UUID=3fa201b2-... /mnt/storage ext4 defaults,noatime 0 2
# Test fstab syntax WITHOUT rebooting (Prevents unbootable servers!)
sudo mount -aSWAP Space Management
Swap space acts as emergency virtual memory, preventing Out-Of-Memory (OOM) kernel panics.
# Check active swap allocations
swapon --show
free -h
# 1. Allocate a 4GB swap file
sudo fallocate -l 4G /swapfile
# Fallback if fallocate is unsupported on your filesystem:
# sudo dd if=/dev/zero of=/swapfile bs=1M count=4096 status=progress
# 2. Restrict file permissions to root only
sudo chmod 600 /swapfile
# 3. Format as swap area
sudo mkswap /swapfile
# 4. Activate swap
sudo swapon /swapfile
# 5. Persist across reboots in /etc/fstab (add line):
# /swapfile none swap sw 0 0
# Adjust Swappiness (tendency of kernel to swap anonymous memory)
# 0 = avoid swapping unless desperate, 10-20 = recommended for servers, 60 = desktop default
cat /proc/sys/vm/swappiness
sudo sysctl vm.swappiness=10
# Persist in /etc/sysctl.d/99-swappiness.conf:
# vm.swappiness = 10
# Disable all swap (required for Kubernetes nodes):
sudo swapoff -aEmergency Troubleshooting for Full Disks (100% Full)
When root or data partitions hit 100%, services crash, socket creation fails, and logs cannot be appended.
# 1. Determine if the issue is raw capacity (GB) or Inode exhaustion
df -h
df -i
# If Inodes are 100% full, find directories containing millions of small files:
sudo find / -xdev -printf '%h\n' | sort | uniq -c | sort -k1 -n | tail -20
# 2. Locate the largest files across the filesystem
sudo find / -xdev -type f -size +500M -exec ls -lh {} +
# 3. Find deleted files held open by running processes (unlinked open file descriptors)
# Common scenario: a large log was deleted with 'rm', but the daemon is still writing to it
sudo lsof +L1
# Or:
sudo lsof | grep deleted
# Solution: Truncate the file descriptor without restarting the service:
# Example: PID = 1234, file descriptor = 4:
sudo truncate -s 0 /proc/1234/fd/4
# 4. Clean up bloated systemd journal logs
sudo journalctl --disk-usage
sudo journalctl --vacuum-time=3d # Retain only the last 3 days of logs
sudo journalctl --vacuum-size=500M # Cap journal storage to 500MB
# 5. Clear package manager caches
sudo apt clean # Debian / Ubuntu
sudo dnf clean all # RHEL / CentOS / RockyDisk Health & Speed Benchmarking
# Physical drive health monitoring (S.M.A.R.T.)
sudo smartctl -H /dev/sda # Overall health assessment (PASSED / FAILED)
sudo smartctl -a /dev/sda # Complete SMART attributes, error log, and temperature
# Raw buffer/cache read speed test
sudo hdparm -Tt /dev/sda
# Quick sequential write speed test (dd sync)
dd if=/dev/zero of=/tmp/speedtest bs=1G count=1 oflag=dsync
rm -f /tmp/speedtest
# High-performance I/O benchmark with fio (Random 4K Read/Write)
sudo apt install -y fio
fio --name=randwrite --ioengine=libaio --iodepth=16 --rw=randwrite --bs=4k --direct=1 --size=512M --numjobs=2 --runtime=30 --group_reporting