Most desktop Linux users have probably heard of a “Copy on Write” filesystem like ZFS or Btrfs, and along with that, the benefits of those CoW filesystems. Compression, built-in RAID functionality, and snapshot capabilities make them incredibly advanced and modern filesystems. But how do you get started with one of these filesystems? Given that Btrfs is fully FOSS and built into the Linux Kernel, that’s a great place to start. Here we walk you through our beginner’s guide to Btrfs.
Btrfs Support Under Linux
One of the great things about Btrfs over ZFS is that Btrfs is already in-tree, meaning it’s already in the Linux Kernel. There’s nothing to install, nothing to configure – you can just use it to your heart’s content. That being said, you should check your various distro’s support for Btrfs, as some are better than others. It’s been part of the mainline Kernel since 2.6.29, and we’re closing in on 5.10, so it’ll probably be fine.
Creating a Btrfs Partition
The first thing you’ll want to do is use the trusty mkfs
command to make a partition you’ll be using for your Btrfs work. If you’re not comfortable using the terminal, you can use GParted to manage your partitions. This is nice if you like to see your partitions before you write them to disk.
To make a Btrfs partition, just identify your disk or partition and use the mkfs.btrfs
command. The system I’m demonstrating on has three disks, and I’ll be choosing “/dev/vdb1” for this first one. You may have to specify the -f
option if there’s some kind of existing partition table there.
Once that’s complete, you’ll want to decide where you’re going to mount that subvolume. This will depend on what you’re using it for: if it’s for a game library, you could make a directory in your “/home” directory called Games and mount it at “~/Games.” If it’s for some kind of backup solution, you may want to mount it in the “/mnt” directory for ease of management. I’ll be making a directory called “/mnt/btrfs” to keep things simple.
I’ll mount my “/dev/vdb” Btrfs device on that mount point using the following command:
sudo mount -t btrfs /dev/vdb /mnt/btrfs
The specific devices and mount points will vary depending on exactly what you’re doing.
Creating a Btrfs Subvolume
In Btrfs, a subvolume is just a directory where the Btrfs is able to manipulate in its special CoW ways. To do this, you’ll use the subvolume create
command under btrfs.
btrfs subvolume create /mnt/btrfs/backups
I’m making a subvolume for backups of this system, but you can name it anything you want. You can use this directory just like you would any other directory, just knowing that it will benefit from the tools embedded in Btrfs.
Snapshotting a Btrfs Subvolume
One of the great parts of Btrfs is the built-in snapshotting capabilities. There are some tools for it, like Snapper, but the btrfs
command itself has a great subvolume snapshot
subcommand. I have a file called “btrfs-test.txt” in my “/mnt/btrfs/backups” directory.
To snapshot this directory (or any Btrfs subvolume, for that matter), the command syntax is btrfs subvolume snapshot /PATH/TO/SUBVOLUME /PATH/TO/SUBVOLUME/SNAPSHOT
. For my case, I’ll run the following command:
btrfs subvolume snapshot /mnt/btrfs/backups /mnt/btrfs/backups/snapshots/
If you’re trying to keep track of your data, I’d recommend date-stamping the snapshot directory so you know when something happened.
You can also use the -r
flag to make the snapshot read-only, which is important for the btrfs send
and receive
functions that we’ll cover later.
If you navigate to whatever directory you sent your snapshot to, you’ll find all the same data that was in the directory you took a snapshot of all neatly in the same structure. It’s a neat trick to make sure your data is protected in the event of some kind of disaster.
You could use this to take snapshots of your whole system and use the btrfs send
and btrfs receive
commands to send it to another btrfs device. That command would look like this:
sudo btrfs send /mnt/btrfs/backups/ro-snapshot/ | btrfs receive /mnt/btrfs-send-test/
This is great if you have a Btrfs device on a NAS or a RAID array in your system and want to take a snapshot of your operating system and send it over there. You can mount one of those snapshots with the mount
command, which will give you access to that data again.
If you enjoyed this article about Btrfs, make sure to check out some of our other Linux content, like our articles on how to build a new PC for Linux, 8 tools to easily create your own Linux distro, and 12 of the best games for Linux in 2020.
Our latest tutorials delivered straight to your inbox