Cookie Consent by TermsFeed

BTRFS Best Practices

I've been using BTRFS for years and I've had few problems. Some of them are small and can be avoided with some smart improvements.

The following advices are based on my experience. You can find posts against those options online. Your mileage may vary.

Fragmentation, speed and compression

If using rotating disks, use space_cache and autodefrag options. The latter can slow down writes, but keep the disk in a usable state for a long time. Otherwise, fragmentation can become a problem.

Generally speaking, I use compression. Modern cpus are much much faster than rotating disks, so compressing data before writing saves space and, often, speed.

Filesystem stats and status

Have a look, from time to time, at file system status with btrfs fi df mountpoint.

An example:

Data, RAID1: total=1.42TiB, used=1.40TiB
System, RAID1: total=32.00MiB, used=256.00KiB
Metadata, RAID1: total=12.00GiB, used=4.68GiB
GlobalReserve, single: total=512.00MiB, used=0.00B

Numbers are quite similar (except for metadata), so no action is needed. Sometimes they're different, and you can face a strange situation: you see a lot of free space, but you can't write anymore as disk seems to be full.

This can happen when total (allocated) reaches the space of the disk, but used is lower. So you have space, but you can't use it. A balance will fix it.

Perform some manual operations

Ok, professional file systems should be able to auto heal and auto cleanup. ZFS, for example, autoscrubs data from time to time. BTRFS still lacks some of those automatic operations, so it's suggested to perform some maintenance. I run them via cron.

My daily job is something like this:

/bin/btrfs fi ba start  -dusage=50 -dlimit=2 -musage=50 -mlimit=4 mountpoint

This operation will check and rebalance only a subset of blocks, meeting specific conditions. This will assure that the operation will be only a few minutes long. Scheduling it every day means, in time, having a clean situation

My weekly job is something like this:

/bin/btrfs scrub start -B -d -c 2 -n 4 mountpoint

It performs a scrub, so checks blocks and detects (and, if possible, corrects) errors. It's a longer operation, so I don't perform it every day.

I've been using BTRFS since 2012 and, thanks to those operations, it still rocks. I haven't lost a bit (even if I've had some problems, with earlier versions) and its capabilities helped me in many situations.

I'm also using btrfs for my desktop PC, in order to have snapshots and consistent backups.

Stefano Marinelli

Stefano Marinelli