Btrfs - Your Professional Linux Storage

A Filesystem And Your Operational Toolset

Ops & Tactics

You've read about ZFS. Snapshots. Compression.

Btrfs is another filesystem and volume manager for Linux.
It gives you flexibility - you create and delete subvolumes on the go.
Subvolumes share the filesystem's storage pool - rather than requiring fixed partition sizes.

You don't only get checksums and copy-on-write - you get an operational toolset.

Snapshots - Frozen Moments Of Time

Snapshots are fast.
They're initially small.
It's recommended to take snapshots on a separate top level subvolume.

The example system is simple:

ID 256 gen 6339 top level 5 path @rootfs
ID 257 gen 6244 top level 5 path @snapshots

They're mounted to / and /.snapshots:

/      /dev/vda2[/@rootfs] btrfs  rw,noatime,compress=zstd:3,discard=async,space_cache=v2,subvolid=256,subvol=/@rootfs
/.snapshots /dev/vda2[/@snapshots] btrfs  rw,noatime,compress=zstd:3,discard=async,space_cache=v2,subvolid=257,subvol=/@snapshots

The /var/lib/machines and every container under it are children subvolumes of /.

Btrfs snapshots are not recursive - they don't contain the children subvolumes.
They don't contain parents either.

If you want to take a snapshot of /?

It will not contain:

  • /var/lib/machines/
  • /var/lib/machines/db

Practical Snapshot Example

You run nspawn containers. Each container is a separate subvolume.
You want to move your database container to a new host.
On the remote the filesystem structure is the same.

Stop the database container and take a read-only snapshot of it:

sudo machinectl stop db
sudo btrfs subvolume snapshot -r /var/lib/machines/db /.snapshots/db-backup
  • The -r option creates a read-only snapshot.
  • The snapshot is taken to the /.snapshots/ top-level subvolume.

It will contain the container's filesystem.

Send & Receive

Send and receive transfer read-only subvolume snapshots to another location.
It can be a local mount, or a remote system.

Transfer the database container's snapshot to a remote machine:

btrfs send /.snapshots/db-backup | \
    ssh backup@10.10.0.5 \
    'sudo btrfs receive /.snapshots/'

The snapshot will be streamed to the other machine's /.snapshots subvolume.

The /etc/systemd/nspawn/db.nspawn file isn't part of the container's snapshot.
It's a file on a separate parent subvolume.
It must be tranferred separately.

Clone the received snapshot on 10.10.0.5 to a new nspawn instance:

sudo btrfs subvolume snapshot \
     /.snapshots/db-backup \
     /var/lib/machines/db

If you moved the db.nspawn configuration to the right place, you can start the container:

sudo machinectl start db

—

Alternatively you can create a new subvolume for the nspawn configs.

Move the original directory:

sudo mv /etc/systemd/nspawn/ /etc/systemd/nspawn-flat

Create the subvolume:

sudo btrfs subvolume create /etc/systemd/nspawn/

Move the .nspawn configuration files to the new subvolume:

sudo mv /etc/systemd/nspawn-flat/* /etc/systemd/nspawn/

You can send the configurations along with the containers.

Final Whisper

Btrfs is not just another filesystem.

It provides:

  • Subvolumes
  • Copy-on-write
  • Compression
  • Snapshots
  • Checksums
  • Quotas
  • Send/receive

Your storage operations are calculated. Precise. Predictable.