Linux Filesystem and Disk Management
# Linux Filesystem and Disk Management ## Introduction Filesystem and disk management are fundamental Linux system administration skills. Understanding filesystems, partitioning, mounting, and disk
# Linux Filesystem and Disk Management
Introduction
Filesystem and disk management are fundamental Linux system administration skills. Understanding filesystems, partitioning, mounting, and disk health monitoring enables administrators to provision storage, troubleshoot I/O issues, and maintain system reliability.
This tutorial covers Linux filesystems (ext4, XFS, Btrfs), partitioning with fdisk and parted, mounting filesystems, /etc/fstab configuration, filesystem creation and repair, LVM integration, and disk health monitoring with SMART tools.
By the end of this guide, you will be able to manage Linux filesystems and disks from partitioning through ongoing health monitoring.
Technical Background
Linux Filesystem Types
| Filesystem | Journaling | Max File Size | Max Volume | Best For | |------------|------------|---------------|------------|----------| | ext4 | Yes | 16 TiB | 1 EiB | General purpose, root filesystem | | XFS | Yes | 8 EiB | 8 EiB | Large files, high-performance workloads | | Btrfs | Yes | 16 EiB | 16 EiB | Snapshots, copy-on-write, RAID | | swap | No | - | - | Virtual memory |
Filesystem Hierarchy Standard (FHS)
| Directory | Purpose | |-----------|---------| | / | Root filesystem | | /boot | Bootloader files, kernels | | /bin | Essential user binaries | | /sbin | Essential system binaries | | /etc | Configuration files | | /home | User home directories | | /lib | Shared libraries | | /media | Removable media mount points | | /mnt | Temporary mount points | | /opt | Optional application software | | /proc | Kernel and process information | | /root | Root user home directory | | /run | Runtime data | | /sbin | System administration binaries | | /srv | Service data | | /tmp | Temporary files | | /usr | User programs and data | | /var | Variable data (logs, mail, spool) |
Block Devices
| Device | Description | |--------|-------------| | /dev/sda | First SATA/SATA/SAS disk | | /dev/nvme0n1 | First NVMe disk | | /dev/vda | First virtual disk (KVM) | | /dev/sda1 | First partition on /dev/sda |
Prerequisites
- Linux system with root or sudo access
- Available disk or partition for practice
- Backup of important data before disk operations
- Understanding of filesystem concepts
Architecture
Disk Partitioning Layout
Disk: /dev/sda (500 GB)
├── /dev/sda1 (512 MB) → EFI System Partition (ESP)
├── /dev/sda2 (1 GB) → /boot (ext4)
├── /dev/sda3 (498 GB) → LVM Physical Volume
│ ├── vg00-root (50 GB) → / (ext4)
│ ├── vg00-home (100 GB) → /home (ext4)
│ └── vg00-var (50 GB) → /var (ext4)
└── Free space remainingMount Process
Block Device (/dev/sda1)
↓
Filesystem (ext4)
↓
Mount Point (/boot)
↓
Accessible Directory TreeStep-by-Step Implementation
Step 1: List Block Devices and Partitions
**Purpose**: Identify available disks and existing partitions.
**Commands**:
# List all block devices
lsblk
# List block devices with filesystem info
lsblk -f
# List all disks in detail
fdisk -l
# List disks by size
lsblk -d -o NAME,SIZE,TYPE,MODEL**Expected output**: List of disks, partitions, mount points, and filesystems.
**Verification**:
lsblk
# Shows disk hierarchy with mount pointsStep 2: Create Partitions with fdisk
**Purpose**: Create partitions on a new disk.
**Commands**:
# Identify target disk
lsblk
# Partition the disk (replace /dev/sdb with your disk)
sudo fdisk /dev/sdb**fdisk interactive commands**:
- `n` - Create new partition
- `p` - Primary partition
- `1` - Partition number
- Press Enter for default first/last sector
- `t` - Change partition type
- `83` - Linux filesystem type
- `w` - Write changes and exit
**Example session**:
sudo fdisk /dev/sdb
Command (m for help): n
Partition type: p
Partition number: 1
First sector: (default)
Last sector: +50G
Command (m for help): w**Expected output**: New partition created on disk.
**Verification**:
lsblk /dev/sdb
# Shows new partition /dev/sdb1Step 3: Create Filesystem on Partition
**Purpose**: Format partition with a filesystem.
**Commands**:
# Create ext4 filesystem
sudo mkfs.ext4 /dev/sdb1
# Create XFS filesystem
sudo mkfs.xfs /dev/sdb1
# Create Btrfs filesystem
sudo mkfs.btrfs /dev/sdb1
# Check filesystem
sudo fsck.ext4 /dev/sdb1**Explanation**: `mkfs.ext4` is the most common choice for general-purpose Linux filesystems. XFS is preferred for large files and high-performance workloads.
**Expected output**: Filesystem created on partition.
**Verification**:
lsblk -f /dev/sdb1
# Shows filesystem type and UUIDStep 4: Mount Filesystem
**Purpose**: Make filesystem accessible at a mount point.
**Commands**:
# Create mount point
sudo mkdir -p /mnt/data
# Mount filesystem
sudo mount /dev/sdb1 /mnt/data
# Verify mount
df -h /mnt/data
mount | grep /mnt/data**Explanation**: Mounting attaches a filesystem to a directory in the directory tree. Files on the device become accessible through that directory.
**Expected output**: Filesystem mounted; files accessible at mount point.
**Verification**:
mount | grep /dev/sdb1
# Shows /dev/sdb1 mounted on /mnt/dataStep 5: Configure /etc/fstab for Automatic Mounting
**Purpose**: Ensure filesystem mounts automatically at boot.
**Commands**:
# Get UUID of partition
blkid /dev/sdb1
# Edit /etc/fstab
sudo nano /etc/fstab**Example /etc/fstab entry**:
UUID=xxxx-xxxx-xxxx-xxxx /mnt/data ext4 defaults,noatime 0 2**Fields**: UUID | Mount point | Filesystem type | Options | Dump | Pass
**Expected output**: Filesystem mounts automatically at boot.
**Verification**:
# Test fstab without rebooting
sudo mount -a
# No errors means fstab is validStep 6: Check and Repair Filesystem
**Purpose**: Verify and repair filesystem integrity.
**Commands**:
# Unmount filesystem before checking
sudo umount /mnt/data
# Check filesystem (ext4)
sudo fsck.ext4 /dev/sdb1
# Check and automatically repair
sudo fsck.ext4 -y /dev/sdb1
# Check XFS filesystem
sudo xfs_repair /dev/sdb1**Warning**: Never run fsck on a mounted filesystem. It can cause data corruption.
**Expected output**: Filesystem checked; errors reported or repaired.
**Verification**:
# Remount and verify data accessible
sudo mount /dev/sdb1 /mnt/data
ls /mnt/dataStep 7: Monitor Disk Health with SMART
**Purpose**: Monitor disk health and predict failures.
**Commands**:
# Install smartmontools
sudo apt install smartmontools
# Enable SMART
sudo smartctl -s on /dev/sda
# View SMART information
sudo smartctl -a /dev/sda
# Run short self-test
sudo smartctl -t short /dev/sda
# View test results
sudo smartctl -a /dev/sda | grep -A 20 "Self-test log"
# Run long self-test (takes longer)
sudo smartctl -t long /dev/sda**Key SMART attributes**:
- Reallocated_Sector_Ct: Number of bad sectors remapped
- Reallocated_Event_Count: Number of remapping events
- Current_Pending_Sector: Sectors waiting to be remapped
- Offline_Uncorrectable: Uncorrectable errors
**Expected output**: SMART data shows disk health status.
**Verification**:
sudo smartctl -H /dev/sda
# Shows: PASSED (disk healthy) or FAILED (disk failing)Step 8: Monitor Disk Usage and I/O
**Purpose**: Track disk space usage and I/O performance.
**Commands**:
# Check disk space usage
df -h
# Check inode usage
df -i
# Check directory sizes
du -sh /var/log
du -sh /home/*
# Monitor I/O in real-time
iostat -x 1
# Identify processes using disk
iotop
# Check disk I/O statistics
cat /proc/diskstats**Expected output**: Disk usage and I/O statistics displayed.
**Verification**:
df -h
# Shows filesystem usage percentagesConfiguration Examples
/etc/fstab Example
# /etc/fstab: static file system information
#
# <file system> <mount point> <type> <options> <dump> <pass>
UUID=a1b2c3d4-e5f6-7890-abcd-ef1234567890 / ext4 defaults,noatime 0 1
UUID=f5e4d3c2-b1a0-9876-5432-10fedcba9876 /boot ext4 defaults 0 2
UUID=98765432-10ab-cdef-1234-567890abcdef none swap sw 0 0
UUID=11223344-5566-7788-99aa-bbccddeeff00 /mnt/data ext4 defaults,noatime 0 2Mount Options Reference
| Option | Description | |--------|-------------| | defaults | Default options (rw, suid, dev, exec, auto, nouser, async) | | noatime | Don't update access time (improves performance) | | relatime | Update access time only if modified (recommended default) | | noexec | Don't allow binary execution | | nosuid | Don't allow setuid bits | | ro | Read-only mount | | rw | Read-write mount | | user | Allow ordinary users to mount | | auto | Auto-mount at boot |
SMART Monitoring Script (`/usr/local/bin/disk-health-check.sh`)
#!/bin/bash
# Disk health check script
for disk in /dev/sda /dev/sdb; do
if [ -b "$disk" ]; then
echo "=== $disk ==="
smartctl -H "$disk"
smartctl -A "$disk" | grep -E "Reallocated|Pending|Offline"
echo ""
fi
doneCron Job for Daily Disk Health Check
# /etc/cron.d/disk-health
0 2 * * * root /usr/local/bin/disk-health-check.sh | mail -s "Disk Health Report" admin@ityantra.labVerification
Filesystem Verification Commands
lsblk -f -- List filesystems and UUIDs
df -h -- Disk space usage
df -i -- Inode usage
mount -- List mounted filesystems
cat /proc/mounts -- Current mount information
fsck -N /dev/sdb1 -- Dry-run filesystem checkExpected Output
NAME FSTYPE FSVER LABEL UUID FSAVAIL FSUSE% MOUNTPOINT
sda
├─sda1 ext4 1.0 a1b2c3d4-... 18.5G 5% /
├─sda2 ext4 1.0 f5e4d3c2-... 931.5M 2% /boot
└─sda3 swap 1.0 98765432-... [SWAP]
sdb
└─sdb1 ext4 1.0 11223344-... 450.2G 1% /mnt/dataTroubleshooting
| Symptom | Possible Cause | Diagnostic Command | Corrective Action | Verification | |---------|---------------|-------------------|-------------------|--------------| | Filesystem not mounted | Missing fstab entry or wrong UUID | `cat /etc/fstab`; `blkid` | Fix fstab entry; run `mount -a` | Filesystem mounted | | Read-only filesystem | Filesystem errors detected | `dmesg \| grep -i error` | Run fsck to repair | Filesystem remounted read-write | | Disk full | Too many files or large logs | `df -h`; `du -sh /*` | Delete unnecessary files; expand disk | Space available | | Inode exhausted | Too many small files | `df -i` | Delete files; recreate filesystem with more inodes | Inodes available | | SMART pre-fail | Disk failing soon | `smartctl -a /dev/sda` | Backup data; replace disk | New disk healthy | | Slow disk I/O | Fragmentation or failing disk | `iostat -x 1` | Check SMART; consider SSD upgrade | I/O performance improved | | Cannot mount filesystem | Wrong filesystem type | `lsblk -f` | Use correct mount command or mkfs | Filesystem mounts | | fstab syntax error | Typo in /etc/fstab | `mount -a` | Fix fstab syntax; test with mount -a | No errors |
Best Practices
1. **Use LVM**: Provides flexible volume management, snapshots, and online resizing. 2. **Use UUIDs in /etc/fstab**: Device names can change; UUIDs are stable. 3. **Enable noatime**: Reduces write operations for read-heavy workloads. 4. **Monitor SMART regularly**: Detect failing disks before data loss. 5. **Keep /var on separate partition**: Prevents log growth from filling root filesystem. 6. **Use noatime on SSDs**: Reduces wear on SSD cells. 7. **Test backups regularly**: Verify filesystem backups are restorable. 8. **Leave free space**: Filesystems perform better with 10-20% free space. 9. **Use journaling filesystems**: ext4 and XFS protect against corruption from crashes. 10. **Document disk layout**: Record partition scheme, filesystems, and mount points.
Conclusion
Linux filesystem and disk management encompasses partitioning, filesystem creation, mounting, fstab configuration, filesystem checking, and disk health monitoring. Mastery of these skills enables administrators to provision storage reliably, troubleshoot I/O issues, and maintain system availability. Regular monitoring with SMART tools and df/du helps prevent disk-related outages before they impact production services.
References
- Linux Filesystem Hierarchy Standard (https://refspecs.linuxfoundation.org/FHS/)
- ext4 Documentation (https://ext4.wiki.kernel.org/)
- XFS Documentation (https://xfs.org/)
- Btrfs Documentation (https://btrfs.wiki.kernel.org/)
- smartmontools Documentation (https://www.smartmontools.org/)
- util-linux fdisk Documentation (https://man7.org/linux/man-pages/man8/fdisk.8.html)