The Logic of the Ledger: ext4, XFS, and Btrfs
·TechSoftware Development

The Logic of the Ledger: ext4, XFS, and Btrfs

Choosing the right floor for your data. Master the formatting of Linux partitions. Learn the differences between the reliable ext4, the high-performance XFS, and the next-gen Btrfs. Understand the 'Label' and 'UUID' identity system.

Filesystems: Formatting the Disk

In the previous lesson, we created the "Rooms" (Partitions) on our disk. But a room with just a dirt floor isn't useful. You need to put down a floor and a filing system so you can save and find your documents.

In Linux, this is called Formatting or "Making a Filesystem."

A filesystem is a digital ledger that tracks which bits on the physical platter belong to which file. Linux supports dozens of filesystems, but in the modern server world, three kings rule.


1. The Three Kings of Linux Filesystems

I. ext4 (Fourth Extended Filesystem)

  • The Standard: This is the default for Ubuntu and Debian.
  • Why use it? It is arguably the most tested and reliable filesystem on the planet. It is simple, fast, and handles power failures gracefully using a "Journal."
  • Best for: Standard desktops and low-to-medium load servers.

II. XFS

  • The Workhorse: This is the default for Red Hat (RHEL).
  • Why use it? It was designed for "High-Performance" and massive files. It handles parallel operations better than ext4. It cannot be "shrunk" (only grown), but it is rock solid for big databases.
  • Best for: Enterprise servers and extremely large storage arrays.

III. Btrfs (B-Tree Filesystem)

  • The Next-Gen: Used by Fedora and SUSE.
  • Why use it? It includes features usually reserved for high-end hardware: Snapshots, Compression, and Self-Healing. If a file gets corrupted, Btrfs can detect it and fix it automatically from a mirror.
  • Best for: Servers where data integrity is the #1 priority.

2. Practical: Formatting a Partition

Once you have a partition (like /dev/sdb1), you format it using the mkfs (Make FileSystem) command.

# Format as ext4 (Reliable)
sudo mkfs.ext4 /dev/sdb1

# Format as XFS (High Performance)
sudo mkfs.xfs /dev/sdb1

# Format as Btrfs (Snapshots and Checksums)
sudo mkfs.btrfs /dev/sdb1

3. The Identity Problem: UUID vs. Device Name

In the old days, we mounted disks by their name: mount /dev/sdb1 /data.

The Danger: If you unplug one disk and plug in another, Linux might rename the disks. /dev/sdb1 might become /dev/sdc1, and your server will fail to boot or, worse, mount the wrong disk to the wrong folder.

The Solution: The UUID. The UUID is a unique 128-bit string assigned to the filesystem when it is formatted. It never changes, even if you move the disk to a different server.

# Find the UUID of your disks
lsblk -f
# OR
sudo blkid

4. Manual Mounting: Bringing the Disk Online

Formatting isn't enough. You must "Attach" the filesystem to a folder in your hierarchy.

# Create the attachment point (Mount Point)
sudo mkdir /mnt/data

# Mount the disk manually
sudo mount /dev/sdb1 /mnt/data

# Verify it is working
df -h

5. Metadata: Labels and Optimization

You can give your disk a human-readable name (a Label). This is helpful when you are staring at a server with 24 identical drives.

# Give an ext4 partition a label
sudo e2label /dev/sdb1 BACKUP_DISK

# Check information about an ext4 filesystem
sudo tune2fs -l /dev/sdb1

6. Example: Filesystem Health Monitor (Python)

A filesystem isn't static. It can "Frag" or run out of "Inodes" (the limit on how many files you can have, even if you have GBs of space). Here is a Python script that audits the technical health of your filesystems.

import shutil

def check_disk_health(path="/"):
    """
    Checks total, used, and free space on a path.
    """
    total, used, free = shutil.disk_usage(path)
    
    usage_percent = (used / total) * 100
    
    print(f"--- Health Report for {path} ---")
    print(f"Total Capacity: {total // (2**30)} GB")
    print(f"Used Space:     {used // (2**30)} GB ({usage_percent:.1f}%)")
    print(f"Free Space:     {free // (2**30)} GB")
    
    if usage_percent > 90:
        print("[!!!] ALERT: Critical Space Shortage!")
    elif usage_percent > 75:
        print("[WA] Warning: Usage is exceeding 75%.")
    else:
        print("[OK] Space is healthy.")

if __name__ == "__main__":
    check_disk_health("/mnt/data")

7. Professional Tip: Check 'Reserved Blocks'

By default, ext4 reserves 5% of your disk space for the 'root' user. On a 10TB disk, that's 500GB of wasted space that you can't use for your data! If the disk is just for media or backups, you can reduce this reserve to 1%.

# Reduce reserved blocks to 1% to save space
sudo tune2fs -m 1 /dev/sdb1

8. Summary

Formatting is the act of preparing the disk's ledger.

  • ext4 is the reliable veteran.
  • XFS is the modern enterprise workhorse.
  • Btrfs is the visionary architect with snapshots and self-healing.
  • UUIDs are the only safe way to identify disks in a production system.
  • mkfs creates the ledger, and mount opens it.

In the next lesson, we will learn how to make these mounts Permanent so they survive a reboot: Mastering /etc/fstab.

Quiz Questions

  1. Why is XFS preferred over ext4 for very large files like database images?
  2. What is the benefit of using a UUID over a device path like /dev/sdb1 in your configuration?
  3. What happens to the data on a partition when you run a mkfs command on it?

Continue to Lesson 3: Persistent Mounts—Mastering /etc/fstab.

Subscribe to our newsletter

Get the latest posts delivered right to your inbox.

Subscribe on LinkedIn