BTRFS - Tips and tricks
I thought that Btrfs doesn't get all too much love here, so I decided to show you a few things you can do with btrfs.
I do not go into technical details all too much, as this is mainly meant for curious newcomers to Btrfs.
Please also note that most of the commands below need sudo, I haven't written it down explicitly.
Do not hesitate to correct mistakes or make suggestions for additional tips and tricks.
Further information can be found in the usual places, i.e. Wikipedia, Arch Wiki, official Btrfs Wiki, and of course the manpages.
mkfs.btrfs - filesystem creation
Generally you do not need any additional options to pass to mkfs.btrfs
, as the defaults are fine.
However it's recommended to pass a label regardless of the filesystem; just use the -L name-of-label
parameter.
By default, the "single" profile is used for both data and metadata, except on rotational drives where metadata is duplicated (profile name "dup") for performance reasons (less head seeks).
Also note that btrfs supports partitionless drives, i.e. instead of formatting a partition, you can format the entire block device.
This is NOT recommended for root due to possible problems with GRUB, but for other purposes it is actually helpful, because you don't have to mess with partitions anymore, thus eliminating a layer of complexity.
You can still create "subdivisions" with subvolumes.
Btrfs has its own RAID support.
If you have two empty drives you can very easily create a RAID1 with
mkfs.btrfs -d raid1 -m raid1 /dev/sda /dev/sdb
For partitions, just replace /dev/sda with /dev/sdaX and /dev/sdb with /dev/sdbX.
Mount options
There are a few interesting btrfs specific mount options:
-
compress=lzo|zlib|zstd
: this enables compression for newly created files. Three algorithms are available: zlib, lzo and zstd. Zstd needs at least kernel 4.14 and currently doesn't work on root because Manjaro's GRUB lacks support. -
autodefrag
: as the names implies, this enables automatic defragmentation. Not needed for SSDs. -
ssd
andnossd
: btrfs autodetects SSDs based on the sysfs "rotational" entry. You can however still explicitly enable or disable SSD optimisation with these options. -
subvol
andsubvolid
: enables mounting of a specific subvolume, either by passing the path or the subvolume ID.
There are more mount options but those are out of scope and generally not needed.
btrfs filesystem usage
You certainly are used to using the df
command to show free space of a partition with conventional filesystems.
However df is not very accurate for btrfs partitions.
Instead, it's highly recommended to use btrfs filesystem usage /path
instead, which gives detailed info about filesystem usage.
Expand space
Imagine you have your /home
on btrfs and you start running out of space.
You decide to get a new drive to have more free space available.
With btrfs, it's very easy to add that new drive to your /home
.
-
Create an unformatted partition on the new drive, let's assume it's
/dev/sdb1
. -
Run the following command:
btrfs device add /dev/sdb1 /home
-
Now just issue
btrfs filesystem usage /home
and verify that free and total space have increased accordingly. -
You can now just continue using /home as always, but since you have two drives now, you can add a little redundancy. You can use RAID1 for metadata only with the following command:
btrfs balance start -mconvert=raid1 /home
This will mirror the metadata on each drive, so if metadata on one drive gets corrupted, btrfs will automatically use metadata from the good drive. You will lose a little space, but not much.
Reflink copying
Due to the copy-on-write nature of btrfs, you can easily create copies of existing files which initially do not occupy additional free space (until modified).
This also means that the copy is created near instantly.
This can be done by using the --reflink parameter with cp, i.e. cp --reflink source destination
.
Disabling COW (copy-on-write) for files and directories
In some cases it is useful to disable COW, for example for partitions that hold VirtualBox VDIs.
This can be achieved with chattr +C name-of-file
(or name-of-directory).
Note that it will only be disabled for newly created files, not for existing files!
If set on an (empty) directory, it will apply to all newly created files within that directory.
Subvolumes and Snapshots
Let's say that you mounted your newly created btrfs partition /dev/sdb1
to /mnt
.
You can now create a subvolume with btrfs subvolume create /mnt/name-of-subvolume
, and list all the subvolumes of that path with btrfs subvolume list -ap /mnt
, and the output could be as follows
ID 260 gen 13 parent 5 top level 5 path name-of-subvolume
Note the "parent" ID number: the root parent ID is always 5, this is the default (root) subvolume for every btrfs partition that is active by default.
Let's create a few files in the first subvolume:
cd /mnt/name-of-subvolume && for i in {1..9}; do touch file_$i; done
And now, let's create a snapshot of that subvolume with btrfs subvolume snapshot /mnt/name-of-subvolume /mnt/snapshot1
. (Pass the -r
option to create a read-only snapshot.)
Now delete one of the files we just created with rm file_4
.
Oh noz! That was an important file!
Don't despair, you have a snapshot and you can recover file_4. Just go to /mnt/snapshot1, the location where the snapshot was created, and you'll see that file_4 is there. You can just copy it back.
Another possibility is to mount the snapshot directly.
For that, you need the ID of the snapshot which you can get with the list command as mentioned above.
Then just mount it by passing the subvolid option with
mount /dev/sdb1 /mnt2 -o subvolid=262
The snapshot is now available under /mnt2
.
Note that only subvolumes can be snapshot.
No fsck??
Btrfs currently has no fully working fsck, but due to the nature of btrfs, it isn't as strictly needed as with conventional filesystems.
The name of the btrfs fsck utility is btrfs check -p /dev/sdb1
.
While it can detect problems, it cannot (yet) repair them. That's why by default it is run in read-only mode, no data being modified.
You can force enable the repair mode with --repair
, but it is highly dangerous! Do not use!
Btrfs uses checksums for both data and metadata.
In order to verify the checksums, the command btrfs scrub
is used (see below).
Balance and Scrub
See here: [btrfs] Balance & Scrub
Reliability
Difficult subject.
I've been using btrfs since its early days, when it was still considered "beta", and in all those years, I haven't had a single data loss despite several power outages and hard freezes.
However, other people haven't had as much luck.
Btrfs is still under heavy development and while the on-disk format has been finalised since years, many features like transparent encryption and in-band deduplication are not implemented yet. RAID5/6 are implemented but experimental and should not be used.
There are two recovery utilities provided by btrfs-utils: btrfs rescue
and btrfs restore
.
The first one can be used to recover superblocks and the chunk tree, the second one to recover lost files.
Send and Receive
btrfs is able to create a binary diff between two snapshots with btrfs send
, and which can be replayed (or "patched") with btrfs receive
.
As I haven't used these features yet, I cannot say much about it, but now you know that the feature exists. Edit (by @eugen-b): Just have a look at the post about send|receive by @TomZ below.
Debug information
btrfs also provides a utility to get internal low-level information, the most important being info about the superblock:
btrfs inspect-internal dump-super /dev/sdb1
and about the chunk tree:
btrfs inspect-internal tree-stats /dev/sdb1
Deduplication
While in-band deduplication is not yet available, offline dedup is.
It is not provided by btrfs-utils, but you need a third party tool from AUR, e.g. duperemove
or bedup
.
Deduplication in a broad sense simply means removal of redundant data, which can be especially useful on COW filesystems.
For the moment being, don't worry about it.
Personal note:
I hope that Calamares gets more btrfs related options in the future.
With Architect you can already use more of the potential of btrfs, like creating subvolumes and setting mount options.