Skip to content

Your First Filesystem

This chapter creates a btrfs filesystem and proves it holds real files. The outcome: a filesystem you made, inspected three different ways, with its contents read back out and checked byte for byte. The lab did every step of this chapter against image files, because its sandbox could not mount anything — and that turns out to be a fine way to learn, because the offline tools shown here are the same ones chapter 11 uses for rescue.

Making one

On a real machine you point mkfs.btrfs at a spare device (identified twice, chapter 3's rule):

sudo mkfs.btrfs -L mydata /dev/sdX

The lab pointed it at a plain file instead — a file is a perfectly good pretend disk for everything except mounting:

truncate -s 300M single.img
mkfs.btrfs -L labfs single.img

-L sets the label, the human name. The report that follows (transcript t01) is worth reading once in full: the label and UUID, the filesystem size, and the starting profiles — data single, metadata and system DUP, the single-disk defaults from chapter 2. Check: your report ends with a device table naming the device or file you gave it. If it names anything else, stop.

Meeting it again, offline

Three commands introduce a filesystem without mounting it:

btrfs filesystem show single.img
btrfs inspect-internal dump-super single.img
btrfs check --readonly single.img

filesystem show gives the summary: label, UUID, devices, space used. dump-super prints the superblock — the identity card from chapter 2 — including the checksum type (transcript t02 compares crc32c's 4-byte sums with sha256's 32-byte ones). check --readonly walks every tree and reports whether the filesystem is internally consistent; on the lab's new filesystem it ended with the sentence you want to see in transcript t01: found 147456 bytes used, no error found.

Putting files in, taking files out

A mounted filesystem takes files the usual way — you copy them in, and chapter 5 reorganises them. Unmounted, there are two honest tricks, and the lab ran both (transcript t03):

mkfs.btrfs -L filledfs --rootdir fixture filled.img

--rootdir copies a directory tree into the filesystem as mkfs creates it. And to read an unmounted filesystem's files back:

mkdir restored-all
btrfs restore -i filled.img restored-all

btrfs restore is the salvage tool: it walks the trees and writes the files out to an ordinary directory. The lab compared the restored tree with the original — diff -r silent, and every sha256 hash matching. That round trip is this chapter's real check: the bytes you put in are the bytes you get back, proven without a mount. Restore returns in chapter 11, where the filesystems it reads are damaged ones; learn it here, where nothing is at stake and the destination directory merely needs to exist first — the one trap the lab fell into, kept in the transcript.