BTRFS maintenance under Linux systems (and some tips)

BTRFS it's an excellent filesystem. I have no doubt of that. However, it requires maintenance and "first configuration setup" in order to stay fast and clean (as it tends to fragment over time without the appropiate setup). Keeping BTRFS clean, shiny and without fragmentation is very simple, you just need to follow these steps once every time you perform a clean installation of a Linux O.S. with BTRFS partitions on it:

NOTE: I will use Fedora Linux as base O.S. for my examples here, but the same tips are valid for any other distro with very few changes in some commands (such as the package manager one, for installing the needed software). Also, it's fine to apply these tips on an already installed and operative Linux machine (doesn't matter if it is not an actual fresh install, but is better to apply these tweaks since the beginning rather than later).

Before doing anything...

You'll have to install the "Btrfs tools/utilities" package for your distro. In fedora, it can be installed using the following command:

sudo yum -y install btrfs-progs

And in other distros (such as ubuntu for example) the package it's called btrfs-tools if I'm not mistaken.

1) Mount Options

We'll need to add some mount options to our /etc/fstab, these are the ones I recommend:


NOTE: If you have an SSD instead of an HDD, also add the ssd mount option to the above array. You can read what each of these options do directly in the BTRFS Wiki.

I've had many BTRFS (/) partitions over time and I've noticed that without these options boot times start to increase very much over time, With these options (mainly because of the autodefrag one and the compression applied to the FS), system boot time remains very fast/reduced over time with no problems. Please note that you have to use those flags on every BTRFS partition on your system, look an example:

2) Some Cleanup

The next thing We'll do is defrag the BTRFS partitions on our system (ahead in step 3); So if your system has been up without a fresh reinstall in some time, it'll take a lot defragging unless we do some cleaning first, I recommend doing the following before a defrag:

su -
yum-complete-transaction --cleanup-only
package-cleanup --cleandupes
yum -y update
rm -rf /var/lib/yum/yumdb/*
rm -rf /var/lib/yum/history/*
rm -rf /var/cache/*
rm -rf /var/log/*
rm -rf ~/.local/share/Trash/*
rm -rf /root/.local/share/Trash/*
rm -f /var/lib/rpm/__db*
rpm --rebuilddb
yum history new; yum history new
yum clean all
yum makecache

This will ensure you the process takes a lot less (at least on Fedora/CentOS/RHEL based systems); Please note that in other distros (Not using YUM or RPM packages) you'll have to omit the yum/rpm specific parts.

3) Recursive Defrag

This can be done with the following command:

find / -xdev -type f -print -exec btrfs filesystem defrag '{}' \;

(Remember to run it once for every BTRFS partition on your system, replacing / with the correct partition mount point).

After this it's done, reboot your computer please.

4) Defrag & Compress

Then we'll run:

btrfs filesystem defragment -v -clzo /

Once for every BTRFS partition on our system, replacing / with the correct mount point in each case.

After this other defrag it's done, we just reboot our computer once more and we're good to go. No more maintenance needed for our BTRFS partitions... We'll just have to enjoy the cool features like the snapshots and such from now on.