Linux Disk Management & Storage
Published: December 2023 | Topic: Storage Infrastructure for DevOps
Storage management is critical for system performance, data integrity, and resource optimization in DevOps environments. Understanding how Linux handles disks, partitions, filesystems, and volumes enables you to design scalable storage architectures, optimize performance, and ensure data availability.
Why Storage Management Matters for DevOps
Effective storage management provides:
- Data Persistence: Ensure data survives reboots and system failures
- Performance Optimization: Configure storage for optimal read/write speeds
- Resource Management: Efficiently allocate and monitor disk space
- Scalability: Easily expand storage as needs grow
- Data Protection: Implement redundancy and backup strategies
- Multi-tenancy: Isolate storage resources for different applications/users
- Automation: Script storage provisioning and management
Linux Storage Hierarchy
/dev/sda, /dev/nvme0n1
/dev/sda1, /dev/sda2
ext4, xfs, btrfs
/, /home, /var
1. Understanding Disks, Partitions, and Filesystems
What is a Disk?
Disk (Block Device): A physical or virtual storage device that provides raw storage space. In Linux, disks appear as block devices in the /dev directory.
Types of Disks in Linux:
- Hard Disk Drives (HDD): Traditional mechanical drives -
/dev/sdX(e.g., /dev/sda, /dev/sdb) - Solid State Drives (SSD): Faster flash-based storage - also
/dev/sdX - NVMe Drives: High-performance PCIe-based SSDs -
/dev/nvmeXnY(e.g., /dev/nvme0n1) - Virtual Disks: Disk images, LVM volumes, cloud volumes
/dev/sda # First SATA/SCSI disk
/dev/sdb # Second SATA/SCSI disk
/dev/nvme0n1 # First NVMe namespace on first controller
/dev/vda # First virtual disk (VMs)
/dev/mmcblk0 # First SD/MMC card (Raspberry Pi)
What are Partitions?
Partition: A logical division of a physical disk. Partitions allow you to segment a disk into multiple, independent sections that can be formatted with different filesystems and used for different purposes.
Why Partition?
- Separation of Concerns: Keep system files separate from user data
- Multiple Filesystems: Use different filesystems for different needs
- Dual Booting: Install multiple operating systems
- Performance: Optimize different partitions for different workloads
- Safety: Isolate critical data from system crashes
MBR (Master Boot Record) - Legacy
- Maximum Partitions: 4 primary, or 3 primary + 1 extended
- Maximum Disk Size: 2TB
- Partition Table: Stored in first sector of disk
- Compatibility: Works with older systems
- Partition IDs: 1-4 for primary, 5+ for logical
/dev/sda1 # First primary partition
/dev/sda2 # Second primary partition
/dev/sda5 # First logical partition
/dev/sda6 # Second logical partition
GPT (GUID Partition Table) - Modern
- Maximum Partitions: 128 (typically)
- Maximum Disk Size: 9.4 ZB (zettabytes)
- Partition Table: Multiple copies for redundancy
- Compatibility: Requires UEFI firmware
- Partition IDs: UUIDs (Globally Unique Identifiers)
/dev/sda1 # First partition
/dev/sda2 # Second partition
/dev/sda14 # Can have many partitions
# Also identified by UUIDs
What is a Filesystem?
Filesystem: A method and data structure that an operating system uses to control how data is stored and retrieved on a storage device. It organizes files into directories, manages permissions, and tracks which blocks of storage belong to which files.
Filesystem Components:
- Superblock: Contains metadata about the filesystem (size, block count, etc.)
- Inode Table: Stores metadata about files (permissions, ownership, timestamps)
- Data Blocks: Actual file content storage
- Directory Structure: Organizes files into hierarchical directories
- Journal: Tracks changes for crash recovery (in journaling filesystems)
Common Linux Filesystems
| Filesystem | Best For | Max File Size | Max Volume Size | Journaling | Features |
|---|---|---|---|---|---|
| ext4 | General purpose, default on many distros | 16TB | 1EB (exabyte) | Yes | Stable, mature, good performance |
| XFS | Large files, high performance | 8EB | 8EB | Yes | Excellent for large files, online resizing |
| btrfs | Advanced features, snapshots | 16EB | 16EB | Yes | Snapshots, compression, RAID, checksums |
| ZFS | Enterprise storage, data integrity | 16EB | 256 quadrillion ZB | Copy-on-write | Advanced RAID, compression, deduplication |
| FAT32 | USB drives, cross-platform | 4GB | 2TB | No | Universal compatibility |
| NTFS | Windows compatibility | 16EB | 16EB | Yes | Windows native, ACL support |
Disk to Directory: The Complete Journey
/dev/sda
MBR or GPT
/dev/sda1, /dev/sda2
ext4, xfs, etc.
/, /home, /var
Example: When you save a file to /home/user/document.txt, it gets written to a specific block on /dev/sda2 through the ext4 filesystem layer.
Key Concepts Summary
- Block Device: Raw storage device that reads/writes in fixed-size blocks
- Partition: Logical division of a disk - like rooms in a house
- Filesystem: Organization system for files - like a filing system for documents
- Mounting: Making a filesystem accessible at a specific directory
- Inode: Data structure storing file metadata (everything except name and content)
- Block: Smallest unit of storage allocation (typically 4KB)
- Journaling: Technique to prevent filesystem corruption during crashes
2. Mounting and Unmounting Drives
What is Mounting?
Mounting: The process of making a filesystem accessible at a specific point in the Linux directory tree (called a mount point). When a filesystem is mounted, its contents become accessible through that directory.
Why Mounting is Necessary:
- Unified Directory Tree: Linux has a single root (
/) hierarchy - Access Control: Different filesystems can have different permissions
- Filesystem Independence: Each filesystem maintains its own structure
- Dynamic Attachment: Removable media can be connected/disconnected
- Resource Management: Different partitions can be mounted with specific options
The Mounting Process Explained
How Mounting Works
Before Mounting:
$ ls /mnt/data
(empty)
# /dev/sdb1 exists but isn't accessible
$ cat /dev/sdb1
(binary data, not readable)
After Mounting:
$ sudo mount /dev/sdb1 /mnt/data
# Now /mnt/data shows files from /dev/sdb1
$ ls /mnt/data
file1.txt file2.txt photos/
# Files are accessible normally
$ cat /mnt/data/file1.txt
Hello from the mounted filesystem!
Mount Points Explained
Mount Point: A directory where a filesystem is attached. When a filesystem is mounted, its root directory becomes the mount point directory's contents.
Important Mount Points in Linux:
- / - Root filesystem (always mounted)
- /boot - Boot loader and kernel files
- /home - User home directories (often separate partition)
- /var - Variable data (logs, caches, databases)
- /tmp - Temporary files
- /mnt - Temporary mount points (manual mounting)
- /media - Removable media (auto-mounted by desktop environments)
- /opt - Optional application software
Mount Configuration: /etc/fstab
/etc/fstab (Filesystem Table): A configuration file that defines how and where filesystems should be mounted automatically at boot time. It contains static information about filesystems.
# device mount_point fs_type options dump pass
UUID=1234-5678 / ext4 defaults 0 1
UUID=abcd-efgh /home ext4 defaults 0 2
/dev/cdrom /media/cd auto noauto,ro 0 0
//server/share /mnt/share cifs credentials=/etc/samba/creds 0 0
# Field explanations:
# 1. Device: Can be /dev/sdX, UUID=..., LABEL=..., or network path
# 2. Mount point: Directory where filesystem will be mounted
# 3. Filesystem type: ext4, xfs, nfs, cifs, auto (autodetect)
# 4. Options: Mount options (comma-separated)
# 5. Dump: Backup utility flag (0=no backup, 1=backup)
# 6. Pass: fsck order (0=no check, 1=root, 2=other filesystems)
Common Mount Options
| Option | Description | Use Case |
|---|---|---|
| defaults | rw, suid, dev, exec, auto, nouser, async | General purpose |
| rw / ro | Read-write / Read-only | Data safety, removable media |
| noexec | Prevent execution of binaries | Security for data partitions |
| nosuid | Ignore SUID/SGID bits | Security |
| nodev | Don't interpret device files | Security for network mounts |
| async / sync | Asynchronous / Synchronous writes | Performance vs data safety |
| atime / noatime | Update / Don't update access times | Performance optimization |
| discard | Enable TRIM for SSDs | SSD optimization |
| nofail | Don't fail boot if missing | Optional mounts |
Real-World Mounting Scenarios
$ sudo mkdir -p /mnt/usb
$ sudo mount /dev/sdb1 /mnt/usb
# Usually auto-mounted to /media/user/LABEL
# Scenario 2: Mount with specific options
$ sudo mount -o ro,noexec,nosuid /dev/sdc1 /mnt/secure
# Scenario 3: Mount network share (NFS)
$ sudo mkdir -p /mnt/nfs
$ sudo mount -t nfs server:/export/path /mnt/nfs
# Scenario 4: Mount Windows share (CIFS/SMB)
$ sudo mkdir -p /mnt/windows
$ sudo mount -t cifs //server/share /mnt/windows -o username=user,password=pass
# Scenario 5: Mount ISO file
$ sudo mkdir -p /mnt/iso
$ sudo mount -o loop ubuntu.iso /mnt/iso
# Scenario 6: Remount with different options
$ sudo mount -o remount,ro /mnt/data
# Scenario 7: Unmount safely
$ sudo umount /mnt/data
# or if busy:
$ sudo umount -l /mnt/data # Lazy unmount
⚠️ Important Mounting Considerations
- Never unmount a busy filesystem: Ensure no processes are using it
- Use UUIDs in fstab: Device names (/dev/sda1) can change between boots
- Check filesystem before mounting: Use fsck on unclean filesystems
- Understand mount options: Wrong options can cause data loss or security issues
- Unmount removable media before disconnecting: Prevents data corruption
- Test fstab entries: Use
mount -ato test without rebooting
3. Checking Disk Usage (df, du)
Why Monitor Disk Usage?
Monitoring disk usage is critical for:
- Capacity Planning: Predicting when you'll run out of space
- Performance: Full filesystems can cause system slowdowns
- Troubleshooting: Identifying what's consuming space
- Cost Management: Cloud storage costs money
- Compliance: Some regulations require monitoring storage usage
- Cleanup: Identifying unnecessary files for deletion
The df Command: Filesystem Usage
df (Disk Free): Reports filesystem disk space usage. It shows how much space is used and available on mounted filesystems.
$ df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda1 10474496 5242880 5231616 50% /
tmpfs 819200 0 819200 0% /dev/shm
/dev/sdb1 102625272 1234567 101390705 2% /home
# Human-readable format
$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 10G 5.0G 5.0G 50% /
tmpfs 800M 0 800M 0% /dev/shm
/dev/sdb1 98G 1.2G 97G 2% /home
# Show filesystem type
$ df -hT
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda1 ext4 10G 5.0G 5.0G 50% /
tmpfs tmpfs 800M 0 800M 0% /dev/shm
/dev/sdb1 xfs 98G 1.2G 97G 2% /home
# Show inodes instead of blocks
$ df -i
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/sda1 655360 12345 643015 2% /
# Inode exhaustion can occur even with free space!
The du Command: Directory Usage
du (Disk Usage): Estimates file and directory space usage. It calculates the total disk space used by files and directories recursively.
$ du /var/log
412 /var/log/apt
824 /var/log
# Numbers are in kilobytes (default)
# Human-readable format
$ du -h /var/log
412K /var/log/apt
824K /var/log
# Summary total only
$ du -sh /var/log
824K /var/log
# Show all files/directories with human-readable sizes
$ du -ah /var/log | head -20
# Sort by size (largest first)
$ du -h /var | sort -rh | head -10
1.2G /var/lib/docker
824M /var/log
412M /var/cache
...
# Exclude certain patterns
$ du -h --exclude="*.log" /var/log
Understanding df vs du Output Differences
df Output
Shows: Filesystem-level usage (allocated blocks)
Includes: Space reserved for root (usually 5%)
Shows: Deleted files still held by processes
Best for: Checking overall filesystem capacity
Filesystem Size Used Avail Use%
/dev/sda1 10G 8G 1.4G 85%
du Output
Shows: File-level usage (actual file sizes)
Excludes: Reserved space
Excludes: Deleted files still held by processes
Best for: Finding what's consuming space
6.5G /home
1.2G /var
500M /usr
Why df and du Might Show Different Numbers
Example Scenario: A large file is deleted while a process still has it open.
$ dd if=/dev/zero of=largefile bs=1M count=1024
# Check usage with du
$ du -sh .
1.1G .
# Check usage with df
$ df -h .
Filesystem Size Used Avail Use%
/dev/sda1 10G 6G 3.4G 64%
# A process opens the file and holds it
$ tail -f largefile &
[1] 12345
# Delete the file (but process still has it open)
$ rm largefile
# Now check again:
$ du -sh .
12K . # Shows actual files
$ df -h .
Filesystem Size Used Avail Use%
/dev/sda1 10G 6G 3.4G 64% # Still shows space used!
# Space won't be freed until process closes file
$ kill 12345
$ df -h .
Filesystem Size Used Avail Use%
/dev/sda1 10G 5G 4.4G 53% # Now shows correct usage
Advanced Disk Usage Analysis
Finding Large Files
$ find / -type f -size +100M 2>/dev/null
# Find and list with sizes
$ find / -type f -size +100M -exec ls -lh {} \; 2>/dev/null
# Find and sort by size
$ find / -type f -exec du -h {} \; 2>/dev/null | sort -rh | head -20
Monitoring Tools
$ ncdu /
# Navigate with arrow keys, delete with 'd'
# pydf - Python df with colors
$ pydf
# diskus - Fast disk usage analyzer
$ diskus /path
# baobab - Graphical disk usage analyzer
$ baobab
Inode Usage
$ df -i
# Find directories with many files
$ find / -type d -print0 | xargs -0 ls -1 | wc -l
# Count files in directory
$ find /path -type f | wc -l
# Small files can exhaust inodes even with free space!
Complete Disk Usage Analysis Workflow
$ df -hT
# Identify which filesystem is full
# Step 2: Check inode usage (if df shows 100% but du shows space)
$ df -i
# Step 3: Find largest directories
$ du -h --max-depth=1 / | sort -rh
# or use ncdu for interactive analysis
$ ncdu /
# Step 4: Drill down into large directories
$ du -h --max-depth=1 /var | sort -rh
$ du -h --max-depth=1 /var/log | sort -rh
# Step 5: Find large files
$ find /var -type f -size +100M -exec ls -lh {} \; 2>/dev/null
# Step 6: Check for deleted files still in use
$ lsof +L1
# Shows files with link count less than 1 (deleted but open)
# Step 7: Check log rotation configuration
$ ls -lh /var/log/*.log
$ cat /etc/logrotate.conf
$ ls /etc/logrotate.d/
4. Disk Management Tools: fdisk, lsblk, blkid
lsblk: List Block Devices
lsblk (List Block Devices): Displays information about all available block devices (disks, partitions, LVM volumes) in a tree-like format. It shows relationships between devices and their partitions.
$ 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 38G 0 part
sdb 8:16 0 1.8T 0 disk
└─sdb1 8:17 0 1.8T 0 part /data
nvme0n1 259:0 0 476.9G 0 disk
├─nvme0n1p1 259:1 0 512M part
└─nvme0n1p2 259:2 0 476.4G part /home
# Show additional information
$ lsblk -f
# Shows filesystem type, label, UUID
$ lsblk -o NAME,SIZE,TYPE,MOUNTPOINT,FSTYPE,LABEL,UUID
# Custom output columns
$ lsblk -p
# Prints full device paths (/dev/sda instead of sda)
$ lsblk -d
# List only disks (no partitions)
blkid: Block Device Attributes
blkid (Block ID): Displays attributes (UUID, LABEL, TYPE) of block devices. It reads information from filesystem superblocks and partition tables.
$ sudo blkid
/dev/sda1: UUID="abcd-1234" TYPE="vfat" PARTUUID="1111-2222"
/dev/sda2: UUID="12345678-abcd-efgh" TYPE="ext4" PARTUUID="3333-4444"
/dev/sdb1: LABEL="DataDrive" UUID="98765432-ijkl-mnop" TYPE="xfs"
/dev/nvme0n1p1: UUID="aaaa-bbbb" TYPE="swap" PARTLABEL="swap"
# Query specific device
$ sudo blkid /dev/sda2
# Show only UUIDs
$ sudo blkid -s UUID
# Show only filesystem type
$ sudo blkid -s TYPE
# Why UUIDs are important:
# 1. Persistent across reboots
# 2. Don't change when adding/removing disks
# 3. Used in /etc/fstab for reliable mounting
fdisk: Partition Table Manipulation
fdisk: A command-line utility for creating and manipulating partition tables on disk devices. It supports both MBR and GPT partition tables.
⚠️ fdisk Warning
fdisk can destroy data! Always backup important data before partitioning. Changes don't take effect until you write them with the w command.
$ sudo fdisk /dev/sdb
Welcome to fdisk (util-linux 2.34).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): m
Help menu appears with all commands
# Common fdisk commands:
p # Print partition table
n # Create new partition
d # Delete partition
t # Change partition type
w # Write changes and exit
q # Quit without saving
g # Create new GPT partition table
o # Create new MBR partition table
Modern Alternatives to fdisk
parted
Description: GNU Partition Editor, supports larger disks and more filesystems
$ sudo parted /dev/sdb
(parted) print
(parted) mklabel gpt
(parted) mkpart primary ext4 1MiB 100GiB
(parted) quit
# Non-interactive mode
$ sudo parted /dev/sdb mklabel gpt
$ sudo parted /dev/sdb mkpart primary ext4 1MiB 100GiB
gdisk
Description: GPT fdisk, specifically for GPT partition tables
$ sudo gdisk /dev/nvme0n1
Command: ?
# Shows available commands
# Convert MBR to GPT without data loss
$ sudo gdisk /dev/sda
Command: r # Recovery/transformation menu
Recovery/transformation command: g # Convert to GPT
Command: w # Write changes
Complete Disk Setup Workflow
$ lsblk
# Find new disk (e.g., /dev/sdb with no partitions)
# Step 2: Create partition table (GPT)
$ sudo parted /dev/sdb mklabel gpt
# Step 3: Create partition
$ sudo parted /dev/sdb mkpart primary ext4 1MiB 100%
# 1MiB alignment for optimal performance
# Step 4: Create filesystem
$ sudo mkfs.ext4 /dev/sdb1
# Or for XFS: sudo mkfs.xfs /dev/sdb1
# Step 5: Get UUID for fstab
$ sudo blkid /dev/sdb1
/dev/sdb1: UUID="1234-5678" TYPE="ext4"
# Step 6: Create mount point
$ sudo mkdir -p /mnt/data
# Step 7: Add to /etc/fstab
$ echo "UUID=1234-5678 /mnt/data ext4 defaults 0 2" | sudo tee -a /etc/fstab
# Step 8: Mount and verify
$ sudo mount -a
$ df -h /mnt/data
$ lsblk -f /dev/sdb1
5. Swap Memory and File
What is Swap?
Swap: Space on a storage device (disk or SSD) that is used as virtual memory when physical RAM (Random Access Memory) is full. It allows the system to handle more memory demands than physically available RAM.
How Swap Works:
- Memory Pages: RAM is divided into pages (typically 4KB)
- Page Out: When RAM is full, less-used pages are moved to swap
- Page In: When needed, pages are moved back from swap to RAM
- Swappiness: Kernel parameter controlling tendency to use swap
- Swap Cache: Pages already in swap but also in RAM (clean pages)
Types of Swap in Linux
Swap Partition
Description: Dedicated disk partition for swap
Pros: Better performance, no filesystem overhead
Cons: Fixed size, harder to resize
$ sudo mkswap /dev/sda3
$ sudo swapon /dev/sda3
Swap File
Description: Regular file used as swap space
Pros: Easy to create/resize, no partition needed
Cons: Slightly slower, filesystem overhead
$ sudo fallocate -l 2G /swapfile
$ sudo chmod 600 /swapfile
$ sudo mkswap /swapfile
$ sudo swapon /swapfile
Swap Management Commands
Checking Swap Status
$ free -h
total used free shared buff/cache available
Mem: 15Gi 5.2Gi 8.1Gi 1.2Gi 2.1Gi 8.9Gi
Swap: 2.0Gi 512Mi 1.5Gi
$ swapon --show
NAME TYPE SIZE USED PRIO
/swapfile file 2G 512M -1
/dev/sda3 partition 4G 0B -2
$ cat /proc/swaps
Filename Type Size Used Priority
/swapfile file 2097148 524288 -1
/dev/sda3 partition 4194304 0 -2
Managing Swap
$ sudo swapon /swapfile
$ sudo swapon /dev/sda3
# Disable swap
$ sudo swapoff /swapfile
$ sudo swapoff -a # Disable all swap
# Change swappiness (0-100)
$ cat /proc/sys/vm/swappiness
60 # Default value
$ sudo sysctl vm.swappiness=10
# Lower value = less likely to use swap
# Make permanent in /etc/sysctl.conf
Swap Size Guidelines
| RAM Size | Recommended Swap | Notes |
|---|---|---|
| < 2GB | 2x RAM | For systems with very little RAM |
| 2GB - 8GB | = RAM size | General purpose servers |
| 8GB - 64GB | 4GB - 8GB | Modern systems, minimum 4GB |
| > 64GB | 4GB minimum | Depends on workload, hibernation needs |
| Hibernation | RAM size + 10-20% | Enough to store RAM contents |
⚠️ Swap Performance Considerations
- SSD vs HDD: Swap on SSD is much faster than HDD
- Swappiness: High values can cause premature swapping ("swap thrashing")
- Memory Pressure: Heavy swap usage indicates insufficient RAM
- Latency: Disk access is thousands of times slower than RAM
- Wear on SSDs: Frequent swapping can reduce SSD lifespan
Complete Swap Setup Example
# Step 1: Allocate space for swap file
$ sudo fallocate -l 4G /swapfile
# Or if fallocate doesn't work:
$ sudo dd if=/dev/zero of=/swapfile bs=1M count=4096
# Step 2: Secure the swap file
$ sudo chmod 600 /swapfile
# Step 3: Format as swap space
$ sudo mkswap /swapfile
Setting up swapspace version 1, size = 4 GiB (4294963200 bytes)
no label, UUID=abcd1234-ef56-7890-gh12-ij34567890kl
# Step 4: Enable swap file
$ sudo swapon /swapfile
# Step 5: Verify swap is active
$ swapon --show
$ free -h
# Step 6: Make permanent in /etc/fstab
$ echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
# Step 7: Optimize swappiness (optional)
$ echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf
$ sudo sysctl -p
6. LVM (Logical Volume Manager) Basics
What is LVM?
LVM (Logical Volume Manager): A storage management system that creates an abstraction layer between physical storage devices and logical volumes. It allows flexible disk space management by pooling multiple physical disks into volume groups, which can then be divided into logical volumes.
Benefits of LVM:
- Flexible Resizing: Easily resize logical volumes online
- Storage Pooling: Combine multiple disks into single volume groups
- Snapshots: Create point-in-time copies for backups
- Striping/Mirroring: RAID-like functionality
- Thin Provisioning: Allocate more space than physically available
- Easy Migration: Move data between physical disks
LVM Architecture: Three Layers
LVM Structure
/dev/sda1, /dev/sdb
Raw storage devices
vg_data, vg_system
Storage pool
lv_home, lv_root
Virtual partitions
Example: Multiple physical disks (PVs) are combined into a volume group (VG), which is then divided into logical volumes (LVs) that get formatted and mounted.
LVM Components Explained
Physical Volume (PV)
A physical storage device (disk, partition, RAID array) that has been initialized for LVM use.
$ sudo pvcreate /dev/sdb
$ sudo pvcreate /dev/sdc1
# Display physical volumes
$ sudo pvs
$ sudo pvdisplay
Volume Group (VG)
A pool of one or more physical volumes. Storage is allocated from this pool to logical volumes.
$ sudo vgcreate vg_data /dev/sdb /dev/sdc1
# Display volume groups
$ sudo vgs
$ sudo vgdisplay
Logical Volume (LV)
A virtual partition created from a volume group. This is what gets formatted with a filesystem and mounted.
$ sudo lvcreate -n lv_home -L 50G vg_data
# -n: name, -L: size
# Display logical volumes
$ sudo lvs
$ sudo lvdisplay
Common LVM Operations
Extending LVM
$ sudo pvcreate /dev/sdd
$ sudo vgextend vg_data /dev/sdd
# Extend logical volume
$ sudo lvextend -L +20G /dev/vg_data/lv_home
# -L +20G: add 20GB
# -L 100G: set to 100GB total
# Resize filesystem (ext4 example)
$ sudo resize2fs /dev/vg_data/lv_home
Reducing LVM
$ sudo umount /home
$ sudo e2fsck -f /dev/vg_data/lv_home
$ sudo resize2fs /dev/vg_data/lv_home 30G
# Reduce logical volume
$ sudo lvreduce -L 30G /dev/vg_data/lv_home
# Remount
$ sudo mount /home
Snapshots
$ sudo lvcreate -s -n lv_home_snap -L 10G /dev/vg_data/lv_home
# -s: snapshot, -L: snapshot size
# Mount snapshot read-only
$ sudo mkdir /mnt/snapshot
$ sudo mount -o ro /dev/vg_data/lv_home_snap /mnt/snapshot
# Restore from snapshot
$ sudo umount /home
$ sudo lvconvert --merge /dev/vg_data/lv_home_snap
Complete LVM Setup Example
# Step 1: Create partitions (optional, can use whole disks)
$ sudo parted /dev/sdb mklabel gpt
$ sudo parted /dev/sdb mkpart primary 1MiB 100%
$ sudo parted /dev/sdb set 1 lvm on
# Step 2: Create physical volumes
$ sudo pvcreate /dev/sdb1
$ sudo pvcreate /dev/sdc # Using whole disk
# Step 3: Create volume group
$ sudo vgcreate vg_storage /dev/sdb1 /dev/sdc
# Step 4: Create logical volumes
$ sudo lvcreate -n lv_web -L 100G vg_storage
$ sudo lvcreate -n lv_db -L 200G vg_storage
$ sudo lvcreate -n lv_backup -l 100%FREE vg_storage
# -l 100%FREE uses all remaining space
# Step 5: Create filesystems
$ sudo mkfs.ext4 /dev/vg_storage/lv_web
$ sudo mkfs.xfs /dev/vg_storage/lv_db
# Step 6: Mount logical volumes
$ sudo mkdir -p /web /db /backup
$ sudo mount /dev/vg_storage/lv_web /web
$ sudo mount /dev/vg_storage/lv_db /db
# Step 7: Add to /etc/fstab using UUIDs
$ sudo blkid | grep vg_storage
# Copy UUIDs and add to /etc/fstab
7. Disk Quotas
What are Disk Quotas?
Disk Quotas: A system that limits the amount of disk space or number of files (inodes) that users or groups can consume on a filesystem. Quotas help prevent any single user or group from consuming all available disk space.
Types of Quotas:
- User Quotas: Limit disk usage per user
- Group Quotas: Limit disk usage per group
- Project Quotas: Limit usage per project/directory (XFS only)
- Soft Limit: Warns user but allows grace period
- Hard Limit: Absolute limit, no writes allowed beyond this
- Grace Period: Time allowed to reduce usage after hitting soft limit
Quota Implementation Methods
Traditional Quotas (ext2/3/4)
Filesystems: ext2, ext3, ext4
Tools: quota, repquota, quotacheck
UUID=... /home ext4 defaults,usrquota,grpquota 0 2
# Check and create quota files
$ sudo quotacheck -cug /home
$ sudo quotaon /home
XFS Quotas
Filesystems: XFS (native quota support)
Tools: xfs_quota
UUID=... /home xfs defaults,usrquota,grpquota 0 2
# Enable quotas after mount
$ sudo xfs_quota -x -c "enable" /home
# -x: expert mode, -c: command
Setting Up Quotas (ext4 Example)
$ sudo apt install quota # Debian/Ubuntu
$ sudo yum install quota # RHEL/CentOS
# Step 2: Enable quotas in /etc/fstab
$ sudo nano /etc/fstab
# Add usrquota,grpquota to mount options for /home
UUID=... /home ext4 defaults,usrquota,grpquota 0 2
# Step 3: Remount or reboot
$ sudo mount -o remount /home
# Step 4: Create quota database files
$ sudo quotacheck -cug /home
# Creates aquota.user and aquota.group
# Step 5: Turn on quotas
$ sudo quotaon /home
# Step 6: Set quotas for a user
$ sudo edquota -u username
# Opens editor with quota settings:
Disk quotas for user username (uid 1001):
Filesystem blocks soft hard inodes soft hard
/dev/sdb1 0 0 0 0 0 0
# Set soft/hard limits (in kilobytes)
Quota Management Commands
User Quota Management
$ sudo edquota -u username
# Set quota for multiple users
$ sudo edquota -p templateuser user1 user2 user3
# Set grace period (default 7 days)
$ sudo edquota -t
# Check user quota
$ quota -u username
$ sudo quota -u username # See other users
Reporting and Maintenance
$ sudo repquota -a
$ sudo repquota /home
# Update quota database
$ sudo quotacheck -avug
# -a: all filesystems, -v: verbose, -u: user, -g: group
# Turn quotas off/on
$ sudo quotaoff /home
$ sudo quotaon /home
XFS Quota Management
$ sudo xfs_quota -x /home
xfs_quota> help
xfs_quota> report -u
xfs_quota> limit bsoft=1g bhard=1.2g username
xfs_quota> limit isoft=1000 ihard=2000 username
# One-liner commands
$ sudo xfs_quota -x -c "report -u" /home
$ sudo xfs_quota -x -c "limit bhard=2g username" /home
Complete Quota Setup for Web Hosting Server
# Step 1: Setup quotas on /home partition
$ sudo apt install quota
$ echo "UUID=$(blkid -s UUID -o value /dev/vg_data/lv_home) /home ext4 defaults,usrquota,grpquota 0 2" | sudo tee -a /etc/fstab
$ sudo mount -o remount /home
$ sudo quotacheck -cug /home
$ sudo quotaon /home
# Step 2: Create template user with standard quotas
$ sudo edquota -u templateuser
# Set: blocks soft=1G hard=1.2G, inodes soft=10000 hard=12000
# Step 3: Apply template to all web users
$ sudo edquota -p templateuser user1 user2 user3 user4
# Step 4: Set grace period to 14 days
$ sudo edquota -t
# Set time units to 14days
# Step 5: Setup daily quota reports via cron
$ sudo crontab -e
# Add: 0 2 * * * /usr/sbin/repquota -a | mail -s "Daily Quota Report" admin@example.com
# Step 6: Monitor quota usage
$ sudo repquota -a
$ sudo repquota -s /home # Human-readable sizes
Best Practices for Disk Quotas
- Set realistic limits: Based on actual user needs
- Use soft limits with grace periods: Gives users time to clean up
- Monitor regularly: Use automated reporting
- Educate users: Tell them about quotas and how to check usage
- Test quota enforcement: Verify limits work as expected
- Consider project quotas for XFS: More flexible than user/group quotas
- Document quota policies: Clear rules for requesting quota increases
- Regular maintenance: Run quotacheck periodically
Storage Automation for DevOps
Automated Storage Management with Ansible
---
- name: Configure storage infrastructure
hosts: storage_servers
become: yes
tasks:
- name: Install storage utilities
package:
name:
- lvm2
- xfsprogs
- e2fsprogs
- quota
state: present
- name: Create physical volumes
lvg:
vg: vg_data
pvs: /dev/sdb, /dev/sdc
state: present
- name: Create volume group
lvg:
vg: vg_data
pvs: /dev/sdb, /dev/sdc
state: present
- name: Create logical volumes
lvol:
vg: vg_data
lv: lv_web
size: 50G
state: present
- name: Create filesystem on logical volume
filesystem:
fstype: ext4
dev: /dev/vg_data/lv_web
- name: Create mount point
file:
path: /web
state: directory
owner: root
group: root
mode: "0755"
- name: Mount filesystem
mount:
path: /web
src: /dev/vg_data/lv_web
fstype: ext4
state: mounted
opts: defaults
- name: Setup disk quotas
block:
- name: Add quota options to fstab
lineinfile:
path: /etc/fstab
regexp: "^/dev/vg_data/lv_web"
line: "/dev/vg_data/lv_web /web ext4 defaults,usrquota,grpquota 0 2"
- name: Remount with quotas
command: mount -o remount /web
- name: Initialize quota database
command: quotacheck -cug /web
- name: Enable quotas
command: quotaon /web
- name: Set user quotas
shell: |
setquota -u webuser 1000000 1200000 10000 12000 /web
when: "'webuser' in ansible_facts.get('users', {})"
- name: Setup swap file
swap_file:
path: /swapfile
size: 4G
state: present
Disk Management Command Cheat Sheet
Disk Information
$ lsblk -f # With filesystem info
$ sudo blkid # Show UUIDs and types
$ df -h # Disk free (human readable)
$ df -i # Inode usage
$ du -sh /path # Directory usage summary
Partition Management
$ sudo fdisk /dev/sdX # Edit partition table
$ sudo parted -l # List partitions (GPT)
$ sudo gdisk /dev/sdX # GPT partition editor
Filesystem Operations
$ sudo mkfs.xfs /dev/sdX1 # Create XFS filesystem
$ sudo fsck /dev/sdX1 # Check and repair filesystem
$ sudo resize2fs /dev/sdX1 # Resize ext filesystem
$ sudo xfs_growfs /mount # Grow XFS filesystem
Mount Operations
$ sudo umount /mnt # Unmount filesystem
$ mount -a # Mount all in fstab
$ findmnt # Show mounted filesystems
$ cat /etc/fstab # Show auto-mount config
LVM Operations
$ sudo vgs # List volume groups
$ sudo lvs # List logical volumes
$ sudo lvextend -L +10G /dev/vg/lv # Extend LV
$ sudo vgextend vg /dev/sdd # Add PV to VG
Swap Operations
$ sudo swapon /swapfile # Enable swap file
$ sudo swapoff /swapfile # Disable swap file
$ free -h # Memory and swap summary
Quota Operations
$ sudo quotaon /home # Enable quotas
$ sudo edquota -u user # Edit user quotas
$ quota -u user # Check user quota
$ sudo repquota /home # Report all quotas
Practice Scenarios for DevOps Engineers
- A production server's root filesystem is 95% full. What's your step-by-step approach to identify the cause and resolve it?
- You need to add 500GB of storage to a database server. Compare using a new partition vs LVM. Which would you choose and why?
- Users are complaining about slow system performance. How would you determine if swap is being overused and what would you do?
- You need to migrate data from an old disk to a new larger disk without downtime. What methods would you consider?
- A web hosting client has exceeded their disk quota but claims they've deleted files. What could be happening and how would you investigate?
- How would you automate the setup of a new storage server with LVM, multiple filesystems, and quotas?
- You need to increase the size of a database partition that's using XFS filesystem. What's the process?
- What monitoring would you implement to proactively detect and alert on storage issues?
- Compare ext4, XFS, and btrfs for a containerized application storage backend. Which would you choose and why?
- How would you implement backup snapshots for a critical application using LVM?
Key Takeaways
- Understand the hierarchy: Physical disks → partitions → filesystems → mount points
- Choose appropriate filesystems: ext4 for general use, XFS for large files, btrfs for advanced features
- Monitor proactively: Use df, du, and monitoring tools to prevent full filesystems
- Use LVM for flexibility: Enables easy resizing, snapshots, and storage pooling
- Configure appropriate swap: Balance between performance and out-of-memory protection
- Implement quotas for multi-user systems: Prevent any user from consuming all disk space
- Automate storage management: Use Ansible or other tools for consistent configurations
- Understand performance implications: SSD vs HDD, filesystem choices, mount options
- Plan for growth: Design storage architectures that can scale with needs
- Backup before changes: Always backup data before partitioning, resizing, or filesystem changes
Mastering Linux disk management is essential for building reliable, performant, and scalable infrastructure. These skills enable you to optimize storage performance, prevent outages from full disks, and design storage solutions that meet application requirements while being maintainable and cost-effective.
No comments:
Post a Comment