Filesystems
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.
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%)
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 | +----------------------------------------------------------+