21.4 mkfs: Creating Filesystems (ext4, xfs, btrfs)
Alright, let’s get our hands dirty. You’ve got a disk partition sitting there, empty, raw, and utterly useless. It’s a blank slate, and mkfs (short for ‘make filesystem’) is your chisel. This is where we turn that inert block of storage into something your operating system can actually have a conversation with.
Think of it this way: partitioning is like drawing the rooms on an architectural blueprint. Running mkfs is the construction crew showing up to actually build the walls, install the electrical wiring, and lay down the floorboards in each room. Each filesystem type (ext4, XFS, btrfs) is a different crew with their own specialties, quirks, and… well, sometimes their own bizarre ideas about how a house should be built.
The Core Command: It’s All About the Target
The first rule is don’t get it wrong. The mkfs command requires root privileges and a target device. The target is not a directory (like /mnt/data); it’s the actual partition device itself (like /dev/sdb1). Blow this, and you can annihilate your system. No pressure.
The basic syntax is:
sudo mkfs -t <fstype> /dev/target_device
For example, to create an ext4 filesystem on the first partition of a second disk:
sudo mkfs -t ext4 /dev/sdb1
Pro tip: Always double-check the device identifier using lsblk or fdisk -l before you press Enter. I once almost mkfs’d a disk that had my client’s backups on it because I was on autopilot. The coffee hadn’t kicked in. Learn from my near-disaster.
Meet the Filesystem Crew: ext4, XFS, and btrfs
You don’t just use mkfs; you use a specific variant of it. mkfs.ext4, mkfs.xfs, and mkfs.btrfs are all separate tools living on your system. The mkfs -t ext4 command is just a convenient way to call the mkfs.ext4 binary.
Crafting an ext4 Filesystem: The Old Reliable
ext4 is the default workhorse for most Linux distributions. It’s like a Toyota Camry: profoundly competent, incredibly reliable, and not exactly thrilling. Its key strength is predictability.
The most common option you’ll use is -L to set a filesystem label. This is way more useful than it sounds, as it lets you reference this disk in /etc/fstab by a meaningful name (LABEL=MyData) instead of a fickle device name (/dev/sdb1) which can change if you add more disks.
sudo mkfs.ext4 -L "MyData" /dev/sdb1
This command will churn away and then give you a summary. Pay attention to the “block size” (usually 4KiB). This is the smallest chunk of space any file can take up. A 1-byte file costs you 4KiB. A 4097-byte file costs you 8KiB. This is why a disk with millions of tiny files can have “wasted” space—it’s not really wasted, it’s just the cost of doing business with a block-based filing system.
Building an XFS Filesystem: The Speed Demon
XFS is the filesystem you choose when you’re dealing with large files and big, parallel workloads—think video editing, database servers, or scientific data. It’s built for high throughput. Its design is based on allocation groups, which are like mini-filesystems within the main one, allowing for serious parallelism.
The most important XFS-specific option is -L for label (again) and -d for data section parameters. A classic tuning option is su and sw for stripe unit and width if you’re formatting a RAID array or a disk with unusual physical characteristics. For 99% of use cases, the defaults are excellent.
sudo mkfs.xfs -L "BigMedia" /dev/sdb1
XFS has a… interesting… design choice: you can’t shrink an XFS filesystem. You can grow it live all day long, but shrinking requires a complete backup, wipe, remake, and restore. The designers clearly bet that storage only ever gets added, never removed. A bold strategy, Cotton.
Creating a btrfs Filesystem: The Future is Weird
btrfs (pronounced “Butter F S” or “Better F S,” pick your fighter) is the cool new kid on the block. It’s not just a filesystem; it’s a storage management platform with built-in superpowers: copy-on-write, snapshots, built-in RAID, compression, and subvolumes.
Creating one is simple:
sudo mkfs.btrfs -L "SnapshotsRUs" /dev/sdb1
The magic isn’t in the mkfs command itself; it’s in what you do after with btrfs subcommands. The most revolutionary concept here is that you’ll mount the entire filesystem and then manage subvolumes inside it, which behave like first-class citizens but are far more flexible than traditional directories.
Here’s the absurd part: the mkfs.btrfs command will happily let you create a filesystem on multiple disks at once for a RAID setup. This is both incredibly powerful and a fantastic way to confuse the hell out of yourself if you don’t know what you’re doing. Tread carefully.
sudo mkfs.btrfs -d raid1 -m raid1 /dev/sdb1 /dev/sdc1
This one command creates a RAID1 (mirrored) setup for both your data (-d) and metadata (-m). That’s a lot of abstraction in a single line.
The Universal Gotchas and Best Practices
- There’s No Undo:
mkfsis a destructive operation. It obliterates any existing data on that partition. There is no confirmation dialog. The command assumes you know what you’re doing. It’s a trust exercise with very high stakes. - Mounting is Mandatory: Creating the filesystem doesn’t make it available. You must mount it to a directory (
mount /dev/sdb1 /mnt/data) and then add an entry to/etc/fstabto make it permanent. - Choose Your Fighter Wisely: Need rock-solid stability and everyone understands it? ext4. Need massive throughput on big files? XFS. Want to play with the shiny new features like snapshots and compression? btrfs. Don’t use btrfs on a production database server just because it’s cool unless you truly understand its trade-offs.
Now go forth and format. Just, for the love of Linus, make sure you’re formatting the right disk.