Linux File Systems
Chapter 11: Linux File Systems
Section titled “Chapter 11: Linux File Systems”Overview
Section titled “Overview”This chapter covers Linux file systems - types, characteristics, and how to choose the right one for your needs.
Why This Matters in DevOps/SRE
Section titled “Why This Matters in DevOps/SRE”File system knowledge is critical for DevOps and SRE engineers. Proper file system selection, monitoring, and management directly impacts application performance, data reliability, and system uptime. Understanding file systems helps you make informed decisions about storage, backup strategies, and capacity planning.
┌─────────────────────────────────────────────────────────────────────────────┐│ FILE SYSTEMS IN DEVOPS │├─────────────────────────────────────────────────────────────────────────────┤│ ││ ┌─────────────────────────────────────────────────────────────────────┐ ││ │ OPERATIONAL IMPACT │ ││ │ │ ││ │ • Application Performance - FS choice affects I/O throughput │ ││ │ • Data Reliability - Journaling prevents corruption │ ││ │ • Capacity Planning - Monitor usage to prevent disk exhaustion │ ││ │ • Backup Strategy - Understanding FS structure for restores │ ││ │ • Incident Response - Disk issues cause most production outages │ ││ │ │ ││ └─────────────────────────────────────────────────────────────────────┘ ││ ││ ┌─────────────────────────────────────────────────────────────────────┐ ││ │ DEVOPS TASKS │ ││ │ │ ││ │ • Select appropriate filesystem for workloads │ ││ │ • Configure monitoring alerts for disk usage │ ││ │ • Implement proper backup and recovery procedures │ ││ │ • Set up disk quotas for multi-user environments │ ││ │ • Configure encryption for sensitive data │ ││ │ │ ││ └─────────────────────────────────────────────────────────────────────┘ ││ ││ ┌─────────────────────────────────────────────────────────────────────┐ ││ │ COMMON INCIDENTS │ ││ │ │ ││ │ • Disk full - Application crashes, log failures │ ││ │ • Filesystem corruption - Service downtime │ ││ │ • Inode exhaustion - Even with free disk space │ ││ │ • Permission issues - Application access denied │ ││ │ • NFS mount hangs - Network storage problems │ ││ │ │ ││ └─────────────────────────────────────────────────────────────────────┘ ││ │└─────────────────────────────────────────────────────────────────────────────┘11.1 File System Types
Section titled “11.1 File System Types”Common Linux File Systems
Section titled “Common Linux File Systems”Linux supports numerous file systems, each with different characteristics and use cases.
+------------------------------------------------------------------+| Linux File System Types |+------------------------------------------------------------------+
File Systems | +---------------------+---------------------+ | | | v v v +----------+ +----------+ +----------+ | Native | | Journaling| | Network | | Linux FS | | FS | | FS | +----------+ +----------+ +----------+ | | | v v v +----------+ +----------+ +----------+ | ext2 | | ext3 | | NFS | | ext4 | | ext4 | | CIFS/SMB| | btrfs | | xfs | | sshfs | | xfs | | jfs | | | | jfs | | reiserfs | | | +----------+ +----------+ +----------+Local File Systems
Section titled “Local File Systems”| File System | Type | Journal | Max File Size | Max Volume |
|---|---|---|---|---|
| ext2 | Native | No | 2TB | 4TB |
| ext3 | Native | Yes | 2TB | 4TB |
| ext4 | Native | Yes | 16TB | 1EB |
| xfs | Native | Yes | 8EB | 8EB |
| btrfs | Native | Yes | 16EB | 16EB |
| jfs | Native | Yes | 8PB | 8PB |
| reiserfs | Native | Yes | 8TB | 16TB |
Network File Systems
Section titled “Network File Systems”| File System | Protocol | Use Case |
|---|---|---|
| NFS | TCP/UDP | Unix/Linux file sharing |
| CIFS/SMB | SMB | Windows file sharing |
| sshfs | SSH | Secure remote mounting |
Btrfs Features
Section titled “Btrfs Features”+------------------------------------------------------------------+| Btrfs Features |+------------------------------------------------------------------+
+-------------+ | Btrfs | | (Modern) | +-------------+ | +---------------------+---------------------+ | | | v v v +----------+ +----------+ +----------+ | Snapshots| | Copy on | | Data | | & Clones | | Write | | Compression | +----------+ +----------+ +----------+ | | | +---------------------+---------------------+ | +---------------------+---------------------+ | | | v v v +----------+ +----------+ +----------+ | RAID | | Online | | Subvolumes | | Support | | FS Check| | & Quotas | +----------+ +----------+ +----------+
+------------------------------------------------------------------+| Feature | Description |+----------------------+---------------------------------------------+| Snapshots | Point-in-time copies of filesystem || Copy-on-Write | Efficient cloning, no data duplication || Compression | Transparent zstd/zlib compression || RAID Support | Native RAID 0,1,5,6,10 support || Online FS Check | fsck while mounted (scrub) || Subvolumes | Separate mountable filesystem trees || Quotas | Group-based quota support || Deduplication | Block-level deduplication (optional) || Encryption | Native fs-level encryption (experimental) |+----------------------+--------------------------------------------+Choosing a File System
Section titled “Choosing a File System”+------------------------------------------------------------------+| File System Selection Guide |+------------------------------------------------------------------+
Workload Type Recommended FS Reason +----------------------+ +-------------+ +-------------------+ | General purpose | | ext4 | | Widely supported | +----------------------+ +-------------+ +-------------------+ | Large files/db | | xfs | | High performance | +----------------------+ +-------------+ +-------------------+ | Modern features | | btrfs | | Snapshots, etc | +----------------------+ +-------------+ +-------------------+ | Network sharing | | nfs | | Standard protocol | +----------------------+ +-------------+ +-------------------+ | Windows sharing | | cifs/smb | | Native to Windows | +----------------------+ +-------------+ +-------------------+- ext4: Best for general purpose use, widely supported
- xfs: Excellent for large files and high-performance workloads
- btrfs: Modern features like snapshots, compression, deduplication
- xfs: Ideal for databases and large file storage
- NFS: Network file sharing in enterprise environments
11.2 Creating and Managing File Systems
Section titled “11.2 Creating and Managing File Systems”Creating File Systems
Section titled “Creating File Systems”+------------------------------------------------------------------+| Creating File Systems |+------------------------------------------------------------------+
Physical Disk Format Mount +----------+ +----------+ +----------+ | /dev/sdb1|---------->| mkfs.ext4|---------->| /mnt/data| +----------+ +----------+ +----------+
Commands: +----------------------------------------------------------+ | mkfs.ext4 /dev/sdb1 - Create ext4 FS | | mkfs.xfs /dev/sdb1 - Create xfs FS | | mkfs.btrfs /dev/sdb1 - Create btrfs FS | | mkfs.ext4 -L "data" /dev/sdb1 - With label | +----------------------------------------------------------+# Create ext4 file systemmkfs.ext4 /dev/sdb1
# Create xfs file systemmkfs.xfs /dev/sdb1
# Create btrfs file systemmkfs.btrfs /dev/sdb1
# With specific optionsmkfs.ext4 -L "data" -m 0 /dev/sdb1Checking File Systems
Section titled “Checking File Systems”# Check ext file system (read-only recommended)fsck.ext4 /dev/sdb1
# Check xfs file systemxfs_repair /dev/sdb1
# Check btrfs file systembtrfs check /dev/sdb1Mounting File Systems
Section titled “Mounting File Systems”+------------------------------------------------------------------+| Mount Process |+------------------------------------------------------------------+
Device Mount Point File System +--------+ +----------+ +----------+ |/dev/sdb1| -------->| /mnt/data| -------->| ext4 | +--------+ +----------+ +----------+
Options: rw, ro, noexec, nosuid, nodev, sync, async# Mount temporarilymount /dev/sdb1 /mnt/data
# Mount with specific optionsmount -o rw,noexec,nosuid /dev/sdb1 /mnt/data
# View mount optionsmount | grep /dev/sdb1
# Unmountumount /mnt/dataPersistent Mounts (/etc/fstab)
Section titled “Persistent Mounts (/etc/fstab)”# <file system> <mount point> <type> <options> <dump> <pass>/dev/sdb1 /mnt/data ext4 defaults 0 2LABEL=data /mnt/data ext4 defaults 0 2UUID=xxxxxxxx-xxxx-xxxx /mnt/data ext4 defaults 0 2
+----------------------------------------------------------+| Options: || defaults = rw, suid, dev, exec, auto, nouser, async || noauto = Don't mount at boot || user = Allow user to mount || noexec = No executables allowed || nosuid = Ignore suid/sgid bits |+----------------------------------------------------------+11.3 Disk Quotas
Section titled “11.3 Disk Quotas”Setting Up Quotas
Section titled “Setting Up Quotas”+------------------------------------------------------------------+| Setting Up Quotas |+------------------------------------------------------------------+
Step 1: Enable in /etc/fstab +----------------------------------------------------------+ | /dev/sdb1 /mnt/data ext4 defaults,usrquota,grpquota | +----------------------------------------------------------+
Step 2: Remount +----------------------------------------------------------+ | mount -o remount /mnt/data | +----------------------------------------------------------+
Step 3: Create quota files +----------------------------------------------------------+ | quotacheck -cug /mnt/data | +----------------------------------------------------------+
Step 4: Enable quotas +----------------------------------------------------------+ | quotaon /mnt/data | +----------------------------------------------------------+# Install quota toolsapt-get install quotayum install quota
# Enable quotas in /etc/fstab/dev/sdb1 /mnt/data ext4 defaults,usrquota,grpquota 0 2
# Remount filesystemmount -o remount /mnt/data
# Create quota filesquotacheck -cug /mnt/data
# Enable quotasquotaon /mnt/dataManaging User Quotas
Section titled “Managing User Quotas”# Set user quotaedquota -u username
# Set group quotaedquota -g groupname
# View user quotaquota -u username
# Check quota usagerepquota -a11.4 Disk Usage Analysis
Section titled “11.4 Disk Usage Analysis”du Command
Section titled “du Command”+------------------------------------------------------------------+| Disk Usage Analysis |+------------------------------------------------------------------+
du -sh /home = Show total size of /home du -h /home = Show all directories with sizes du -h /home | sort -rh = Sort by size (largest first) du -shc /home/* = Show each item + total
Example Output: +----------------------------------------------------------+ | 4.0G /home/user/documents | | 2.1G /home/user/downloads | | 1.5G /home/user/images | | 512M /home/user/videos | +----------------------------------------------------------+# Directory size summarydu -sh /home
# All directories with sizesdu -h /home
# Sorted by sizedu -h /home | sort -rh
# Total disk usagedu -shc /home/*df Command
Section titled “df Command”# Human readabledf -h
# File system typedf -Th
# Inode informationdf -i11.5 LVM (Logical Volume Manager)
Section titled “11.5 LVM (Logical Volume Manager)”LVM Architecture
Section titled “LVM Architecture”+------------------------------------------------------------------+| LVM Architecture |+------------------------------------------------------------------+
Physical Disks Physical Volumes Volume Groups +----------+ +------------+ +--------------+ | /dev/sdb | -------->| pvcreate | ------>| vgcreate | | /dev/sdc | | /dev/sdb1 | | vg_data | +----------+ +------------+ +--------------+
| | v v +------------+ +--------------+ | pvs | | lvs | | pvdisplay | | lvcreate | +------------+ +--------------+ | v +--------------+ | /dev/mapper/ | | vg_data- | | lv_data | +--------------+Basic LVM Commands
Section titled “Basic LVM Commands”# Physical Volumepvcreate /dev/sdb1pvdisplay /dev/sdb1pvs
# Volume Groupvgcreate vg_data /dev/sdb1vgdisplay vg_datavgs
# Logical Volumelvcreate -L 10G -n lv_data vg_datalvdisplay /dev/vg_data/lv_datalvsLVM Operations
Section titled “LVM Operations”+------------------------------------------------------------------+| LVM Operations |+------------------------------------------------------------------+
Extend Volume Group Extend Logical Volume +-------------------+ +-----------------------+ | vgextend vg_data | | lvextend -L +5G | | /dev/sdc1 | | /dev/vg_data/lv_data| +-------------------+ +-----------------------+ | v +-----------------------+ | resize2fs | | /dev/vg_data/lv_data| +-----------------------+
Create Snapshot Remove Snapshot +-------------------+ +-------------------+ | lvcreate -L 1G | | lvremove | | -s -n snap | | /dev/vg_data/snap| | /dev/vg_data/lv | +-------------------+ +-------------------+# Extend Volume Groupvgextend vg_data /dev/sdc1
# Extend Logical Volumelvextend -L +5G /dev/vg_data/lv_dataresize2fs /dev/vg_data/lv_data
# Reduce Logical Volumeumount /mnt/datae2fsck -f /dev/vg_data/lv_dataresize2fs /dev/vg_data/lv_data 5Glvreduce -L 5G /dev/vg_data/lv_datamount /mnt/data
# Create snapshotlvcreate -L 1G -s -n snap_data /dev/vg_data/lv_data11.6 File System Encryption
Section titled “11.6 File System Encryption”LUKS (Linux Unified Key Setup)
Section titled “LUKS (Linux Unified Key Setup)”+------------------------------------------------------------------+| LUKS Encryption Flow |+------------------------------------------------------------------+
Encrypted Opened Mounted Container Container Filesystem +----------+ +----------+ +----------+ | /dev/sdb1|-------->| luksOpen|-------->| /dev/ | | (encrypted) | cryptsetup| | mapper | +----------+ +----------+ +----------+ | v +----------+ | Keyslot | | (pasword)| +----------+# Create encrypted containercryptsetup luksFormat /dev/sdb1
# Open encrypted containercryptsetup luksOpen /dev/sdb1 encrypted_data
# Create file systemmkfs.ext4 /dev/mapper/encrypted_data
# Mountmount /dev/mapper/encrypted_data /mnt/data
# Closeumount /mnt/datacryptsetup luksClose encrypted_data11.7 Troubleshooting
Section titled “11.7 Troubleshooting”Common Issues
Section titled “Common Issues”+------------------------------------------------------------------+| Troubleshooting Guide |+------------------------------------------------------------------+
Problem Diagnosis Solution +------------------+ +------------------+ +----------------+ | Filesystem Full | | df -h | | du -sh /* | +------------------+ +------------------+ +----------------+
+------------------+ +------------------+ +----------------+ | Inode Exhaustion| | df -i | | Find & delete | +------------------+ +------------------+ +----------------+
+------------------+ +------------------+ +----------------+ | Corrupted FS | | fsck.ext4 | | Backup & format| +------------------+ +------------------+ +----------------+
+------------------+ +------------------+ +----------------+ | Mount Fails | | dmesg | tail | | Check device | +------------------+ +------------------+ +----------------+-
Filesystem Full
Terminal window df -h # Check usagedu -sh /* # Find large directories -
Inode Exhaustion
Terminal window df -i # Check inode usagefind / -type f | wc -l # Count files -
Corrupted Filesystem
Terminal window # Always unmount firstumount /dev/sdb1# Check and repairfsck.ext4 -fy /dev/sdb1 -
Mount Failures
Terminal window # Check devicefdisk -l# Check filesystemfile -s /dev/sdb1# Check mount optionsdmesg | tail
11.8 Best Practices
Section titled “11.8 Best Practices”+------------------------------------------------------------------+| Best Practices Checklist |+------------------------------------------------------------------+
[ ] Always backup before formatting [ ] Use appropriate file system for workload [ ] Monitor disk usage regularly [ ] Enable and configure quotas in multi-user environments [ ] Use LVM for flexibility in storage management [ ] Implement encryption for sensitive data [ ] Schedule regular filesystem checks [ ] Keep spare disk space available (10-20%)
Monitoring Commands: +----------------------------------------------------------+ | df -h | Disk space usage | | du -sh | Directory size | | df -i | Inode usage | | smartctl | Disk health | +----------------------------------------------------------+- Always backup before formatting
- Use appropriate file system for workload
- Monitor disk usage regularly
- Enable and configure quotas in multi-user environments
- Use LVM for flexibility in storage management
- Implement encryption for sensitive data
- Schedule regular filesystem checks
- Keep spare disk space available (10-20%)
Common Mistakes & Anti-Patterns
Section titled “Common Mistakes & Anti-Patterns”1. Using ext2 for Production Systems
Section titled “1. Using ext2 for Production Systems”WRONG:
# Using non-journaling filesystemmkfs.ext2 /dev/sdb1mount /dev/sdb1 /dataCORRECT:
# Use ext4 or xfs for productionmkfs.ext4 /dev/sdb1# ORmkfs.xfs /dev/sdb1Why: ext2 has no journaling - power failures cause filesystem corruption and long fsck times.
2. Not Monitoring Inode Usage
Section titled “2. Not Monitoring Inode Usage”WRONG:
# Only checking disk spacedf -hFilesystem Size Used Avail Use% Mounted on/dev/sda1 100G 50G 50G 50% /
# Missing inode check - can be exhausted even with free space!df -iFilesystem Inodes IUsed IFree IUse% Mounted on/dev/sda1 6553600 6553600 0 100% /CORRECT:
# Always check bothdf -hdf -i
# For finding inode-heavy directoriesfind / -xdev -printf '%h\n' | sort | uniq -c | sort -k1 -nr | head -20Why: Inode exhaustion prevents new file creation even with free disk space.
3. Improper LVM Resizing
Section titled “3. Improper LVM Resizing”WRONG:
# Reducing LV while mounted (dangerous!)lvreduce -L -10G /dev/vg00/lv_data
# Not extending filesystem after LV extensionlvextend -L +10G /dev/vg00/lv_data# Filesystem still shows old size!CORRECT:
# Extend filesystem AFTER extending LVext4fs /dev/vg00/lv_dataresize2fs /dev/vg00/lv_data
# For xfsxfs_growfs /mount/pointWhy: Must extend filesystem to use new LV space; never reduce mounted filesystems.
4. Ignoring Disk I/O Performance
Section titled “4. Ignoring Disk I/O Performance”WRONG:
# Using default mount options for all filesystemsmount /dev/sdb1 /dataCORRECT:
# Optimized mount options for datamount -o defaults,noatime,nodiratime,barrier=1 /dev/sdb1 /data
# For database workloadsmount -o defaults,noatime,nodiratime,barrier=1,data=journal /dev/sdb1 /dataWhy: Default options aren’t optimal; noatime improves performance significantly.
5. Not Setting Up Disk Quotas
Section titled “5. Not Setting Up Disk Quotas”WRONG:
# No quota enforcement - users can fill diskmount /dev/sdb1 /homeCORRECT:
# Enable quotas in /etc/fstab/dev/sdb1 /home ext4 defaults,usrquota,grpquota 0 2
# Initialize quota filesquotacheck -cum /home
# Set quotasedquota usernameWhy: Without quotas, one user can fill filesystem and cause outage for everyone.
Interview Questions
Section titled “Interview Questions”┌─────────────────────────────────────────────────────────────────────────────┐│ FILESYSTEM INTERVIEW QUESTIONS │├─────────────────────────────────────────────────────────────────────────────┤│ ││ Q1: What is the difference between ext3, ext4, and xfs? ││ ││ A1: ││ - ext3: Journaling filesystem, max 16TB, backward compatible with ext2 ││ - ext4: Extended from ext3, supports 1EB, delayed allocation, extents ││ - xfs: High-performance, scalable, excellent for large files, online defrag││ ││ ─────────────────────────────────────────────────────────────────────────── ││ ││ Q2: What is journaling in filesystems? ││ ││ A2: ││ - Journal records metadata changes before writing to disk ││ - Prevents corruption on power failure ││ - Quick recovery (seconds vs minutes/hours for fsck) ││ - Three modes: journal, ordered, writeback ││ ││ ─────────────────────────────────────────────────────────────────────────── ││ ││ Q3: How do you resize an LVM logical volume? ││ ││ A3: ││ 1. Extend LV: lvextend -L +10G /dev/vg/lv ││ 2. Extend filesystem: resize2fs /dev/vg/lv (ext4) ││ OR xfs_growfs /mount/point (xfs) ││ For reduction: umount -> fsck -> lvreduce -> resize2fs -> mount ││ ││ ─────────────────────────────────────────────────────────────────────────── ││ ││ Q4: What causes inode exhaustion? ││ ││ A4: ││ - Many small files (log files, cache, sessions) ││ - Not related to disk space ││ - Check with: df -i ││ - Solution: clean up files or increase inode limit ││ ││ ─────────────────────────────────────────────────────────────────────────── ││ ││ Q5: How do you recover from a filesystem corruption? ││ ││ A5: ││ 1. Unmount filesystem ││ 2. Run fsck -y /dev/sdX ││ 3. Check logs for cause ││ 4. Restore from backup if needed ││ 5. Consider switching to journaling filesystem ││ ││ ─────────────────────────────────────────────────────────────────────────── ││ ││ Q6: What are extents in filesystems? ││ ││ A6: ││ - Contiguous blocks of storage ││ - Reduces metadata overhead ││ - Improves performance for large files ││ - Supported in ext4 and xfs ││ ││ ─────────────────────────────────────────────────────────────────────────── ││ ││ Q7: How does noatime improve performance? ││ ││ A7: ││ - Disables access time updates on read ││ - Significantly reduces I/O operations ││ - Use nodiratime for directories ││ - Recommended: defaults,noatime,nodiratime ││ ││ ─────────────────────────────────────────────────────────────────────────── ││ ││ Q8: What is the difference between hard links and symbolic links? ││ ││ A8: ││ - Hard link: same inode, cannot cross filesystem, cannot link dirs ││ - Symbolic link: separate file, pointer to path, can cross filesystem ││ - Deleting original: hard link works, symlink breaks ││ ││ ─────────────────────────────────────────────────────────────────────────── ││ ││ Q9: How do you monitor filesystem health? ││ ││ A9: ││ - smartctl -a /dev/sdX for S.M.A.R.T. status ││ - tune2fs -l /dev/sdX for filesystem info ││ - dumpe2fs for superblock details ││ - Monitor with: nagios, zabbix, prometheus node exporter ││ ││ ─────────────────────────────────────────────────────────────────────────── ││ ││ Q10: What is a filesystem's journal? ││ ││ A10: ││ - Special area recording pending changes ││ - Commits changes to main filesystem after journal write ││ - Enables fast recovery on crash ││ - Three modes: journal (all data), ordered (metadata only), writeback ││ │└─────────────────────────────────────────────────────────────────────────────┘Quick Reference
Section titled “Quick Reference”+------------------------------------------------------------------+| Quick Reference |+------------------------------------------------------------------+
File System Commands: +----------------------------------------------------------+ | mkfs.ext4 /dev/sdb1 | Create ext4 FS | | fsck.ext4 /dev/sdb1 | Check/repair ext4 | | mount /dev/sdb1 /mnt/data | Mount filesystem | | umount /mnt/data | Unmount | | df -h | Show disk usage | | du -sh /dir | Directory size | +----------------------------------------------------------+
LVM Commands: +----------------------------------------------------------+ | pvcreate /dev/sdb1 | Create PV | | vgcreate vg_name /dev/sdb1| Create VG | | lvcreate -L 10G -n lv_name | Create LV | | lvextend -L +5G /dev/vg/lv | Extend LV | +----------------------------------------------------------+
Quota Commands: +----------------------------------------------------------+ | edquota -u user | Edit user quota | | quota -u user | View user quota | | repquota -a | Report all quotas | +----------------------------------------------------------+