17. Filesystems
While we covered how to mount a disk previously, understanding what filesystem you are mounting is just as important. The filesystem dictates how data is physically laid out, indexed, cached, and recovered after an unexpected crash.
17.1 Common Linux Filesystems
Section titled “17.1 Common Linux Filesystems”Choosing the right filesystem depends heavily on your workload (e.g., standard web servers vs. massive database clusters).
ext4 (Fourth Extended Filesystem)
Section titled “ext4 (Fourth Extended Filesystem)”The default, battle-tested standard for Debian and Ubuntu distributions.
- Pros: Incredibly stable, mature, and excellent backward compatibility.
- Cons: Inode capacity is fixed at creation time; struggles with multi-petabyte scalability compared to newer filesystems.
A highly scalable, 64-bit journaling filesystem. It is the default for RHEL, CentOS, Rocky Linux, and AlmaLinux.
- Pros: Excels at handling large files, high concurrent I/O, and dynamic inode allocation (perfect for databases).
- Cons: You cannot shrink an XFS filesystem (you can only grow it).
Btrfs (B-Tree Filesystem)
Section titled “Btrfs (B-Tree Filesystem)”A modern, advanced copy-on-write (CoW) filesystem designed for fault tolerance and easy administration.
- Pros: Built-in volume management, software RAID, and instantaneous snapshots.
- Cons: Higher CPU/memory overhead; historically had stability issues with some RAID 5/6 configurations.
tmpfs (Temporary Filesystem)
Section titled “tmpfs (Temporary Filesystem)”A special memory-backed filesystem that lives entirely in RAM.
- Pros: Blazing fast read/write speeds with zero disk latency.
- Cons: Volatile (all data is completely wiped when the server reboots). Often used for
/tmp,/run, or caching tiers.
Windows Filesystems (Linux Perspective)
Section titled “Windows Filesystems (Linux Perspective)”- FAT / exFAT: Universally supported across Linux, Windows, and macOS. Used heavily for USB flash media, but lacks native Linux file permissions and ownership.
- NTFS: The native Windows filesystem. Linux can read and write to NTFS (via the
ntfs-3gdriver), but it does not support native Linux POSIX permissions.
17.2 Formatting and Checking
Section titled “17.2 Formatting and Checking”Before mounting, a raw partition must be formatted with a filesystem structure. If a system crashes, the filesystem might need to be checked for integrity.
Formatting (mkfs)
Section titled “Formatting (mkfs)”The mkfs (Make Filesystem) command acts as a frontend for distribution-specific filesystem builders:
# Format a partition as ext4sudo mkfs.ext4 /dev/sdb1
# Format a partition as XFSsudo mkfs.xfs /dev/sdc1Filesystem Checks (fsck)
Section titled “Filesystem Checks (fsck)”If a server loses power unexpectedly, the filesystem metadata can become corrupted. Linux normally runs fsck automatically during boot if it detects an unclean shutdown flag.
[!CAUTION] NEVER run
fsckon a mounted filesystem! Checking or repairing a live filesystem will cause severe metadata corruption and permanent data loss. Always unmount the volume first.
# Unmount the partition firstsudo umount /dev/sdb1
# Check and repair an ext4 filesystem (-y automatically fixes errors)sudo fsck.ext4 -y /dev/sdb117.3 Advanced Mounting & /etc/fstab
Section titled “17.3 Advanced Mounting & /etc/fstab”As covered in the Storage chapter, /etc/fstab makes mounts survive reboots. However, there are advanced mount options you can pass to tune security and performance.
The Importance of UUIDs
Section titled “The Importance of UUIDs”Device paths like /dev/sdb1 are not guaranteed to remain static across reboots or disk reconnections. Always query the block UUID using blkid and reference it in /etc/fstab:
# /etc/fstab entry format:# <Device/UUID> <Mount Point> <Type> <Options> <Dump> <Pass>UUID=a1b2c3d4-e5f6-7890-1234-56789abcdef0 /var/lib/mysql xfs defaults 0 2Mount Options
Section titled “Mount Options”The <Options> column in /etc/fstab controls filesystem behavior:
defaults: The standard set (impliesrw,suid,dev,exec,auto,nouser, andasync).ro: Mounts the filesystem as Read-Only.noexec: Prevents binaries and scripts from executing on this filesystem (strongly recommended for/tmpand/var/logto mitigate malware execution).nosuid: Disables SUID and SGID privilege escalation bits on this volume.nodev: Prevents the interpretation of character or block device files.noatime: Disables updating the “Access Time” timestamp every time a file is read. This dramatically reduces disk write I/O and extends SSD longevity.
# Example of manually mounting a disk as read-only and no-executesudo mount -o ro,noexec,noatime /dev/sdb1 /mnt/secure_data17.4 Inode Architecture & Management
Section titled “17.4 Inode Architecture & Management”An inode (index node) is a fundamental data structure in Linux filesystems (ext4, XFS) that represents a filesystem object (file, directory, socket, named pipe).
Metadata Stored Inside an Inode
Section titled “Metadata Stored Inside an Inode”- File ownership: User ID (UID) and Group ID (GID)
- Permissions: Read, Write, Execute mode bits and ACL references
- Timestamps: Access time (
atime), Modification time (mtime), Change time (ctime), Creation/Birth time (crtime) - File size: Size in bytes
- Disk block pointers: Direct and indirect pointers mapping physical disk sectors holding file content
- Link count: Number of hard links pointing to this inode
[!IMPORTANT] What an Inode Does NOT Store: An inode does not store the filename or the actual file content. Filenames are stored inside directory blocks as key-value pairs (Directory Entry:
filename→inode number).
Inode Inspection Commands
Section titled “Inode Inspection Commands”# Check inode capacity and usage across all mounted filesystemsdf -i
# View the inode number of a specific filels -i filename.txt
# View inode information of a directory itselfls -idl /var/log/
# View detailed inode metadata (size, blocks, links, timestamps)stat /etc/passwdExample df -i output:
Filesystem Inodes IUsed IFree IUse% Mounted on/dev/sda1 1310720 452100 858620 35% //dev/sdb1 655360 655360 0 100% /var/spool/postfixInode Exhaustion: The “No Space Left on Device” Myth
Section titled “Inode Exhaustion: The “No Space Left on Device” Myth”Each file or directory consumes exactly one inode. If a filesystem creates millions of tiny files (such as PHP session caches, mail queues, or unrotated application logs), it will run out of available inodes even when multiple gigabytes of disk space remain free.
Symptoms of Inode Exhaustion
Section titled “Symptoms of Inode Exhaustion”- Error message:
No space left on devicewhen trying to save files or write logs, even thoughdf -hshows plenty of free capacity. - Applications crash unexpectedly, database transactions roll back, or cron jobs fail to run.
df -ireports 100% IUse%.
Identifying Culprit Directories Consuming Inodes
Section titled “Identifying Culprit Directories Consuming Inodes”Run a recursive find pipeline to count files per top-level directory without crossing filesystem boundaries:
# Find directories containing the largest number of filessudo find /var -xdev -type f | cut -d/ -f2-3 | sort | uniq -c | sort -nr | head -n 10Freeing Up Inodes
Section titled “Freeing Up Inodes”- Remove expired session files and stale temporary data in
/tmpand/var/tmp. - Flush unneeded mail queues:
postsuper -d ALL(Postfix). - Purge rotated log files older than retention policy:
sudo find /var/log -type f -name "*.gz" -mtime +30 -delete. - Remove orphaned packages and cached package archives (
apt cleanordnf clean all).
Managing Inode Capacity
Section titled “Managing Inode Capacity”1. During Filesystem Creation (ext4)
Section titled “1. During Filesystem Creation (ext4)”On ext2/3/4 filesystems, the total number of inodes is fixed at format time based on the inode density (bytes-per-inode ratio):
# Create ext4 with higher inode density (1 inode per 8192 bytes - ideal for mail/web servers)sudo mkfs.ext4 -i 8192 /dev/sdb1
# Explicitly set the total number of inodes using -Nsudo mkfs.ext4 -N 2000000 /dev/sdb1- Lower bytes-per-inode (
-i): Creates more inodes (for systems hosting millions of tiny files). - Higher bytes-per-inode (
-i): Creates fewer inodes (saving metadata overhead for large video/database storage).
2. After Filesystem Creation
Section titled “2. After Filesystem Creation”- ext4: The inode count cannot be increased on an existing ext4 filesystem. You must back up data, reformat with
mkfs.ext4 -i, and restore. - XFS: Allocates inodes dynamically on demand. Growing an XFS volume automatically expands inode capacity:
Terminal window sudo xfs_growfs /mount/point
Summary: Key Inode Commands
Section titled “Summary: Key Inode Commands”| Command | Operational Purpose |
|---|---|
df -i |
Display total, used, and free inodes per filesystem |
ls -i filename |
Display the inode number mapped to a file |
ls -idl directory/ |
Display the inode number and metadata of a directory |
stat filename |
Inspect comprehensive inode metadata (timestamps, links, block counts) |
mkfs.ext4 -i <bytes> |
Tune inode density (bytes per inode) during ext4 filesystem creation |
xfs_growfs /mount/point |
Expand XFS filesystem size and dynamic inode headroom |