Playing with BTRFS

0 Flares Twitter 0 Facebook 0 LinkedIn 0 Email -- 0 Flares ×

In my previous post, I talked about BTRFS, a modern and exciting filesystem for Linux. In this new post, I’m going to give you a quick walkthrough on what you can do with it.

Create a new BTRFS filesystem

In my lab, I’ve created a CentOS 7 virtual machine with 4 disks:

The first disk, sda, contains the operating system and has been automatically formatted by CentOS installer using ext4. The other 3 disks are completely empty. One great feature of btrfs is that you can create btrfs file systems on unformatted hard drives, so you don’t have to use tools like fdisk to partition them in advance. To create a btrfs file system on /dev/sdb, /dev/sdc, and /dev/sdd, we simply run:

without any additional switch for different options, the default configuration is RAID0 for data, and RAID1 for metadata.
As you can see, the 3 disks, each with 70 GiB of space, where grouped to create a new filesystem that has now 210 GiB of space. As usual, the filesystem can be mounted manually with:

note that we used one of the disks of the btrfs stripe to do the mount. It’s not really important which one we call, since btrfs reads automatically the configuration of the entire tree, and the final result is that the entire filesystem is mounted and we can check its availability:
and status:
As you can see, data are configured in RAID0, and in fact the df command returns us 210GB as the total space (3 times 70GB), while Metadata are in RAID1. We may want to use the 3 disks to act as a Raid1 device and not as Raid0, so we should change the RAID profile. This can be done with the command:

Now also Data is configured in RAID1. And if we check the available mountpoint:

we can see that the size has already become 105GB, half the original space. RAID1 is already applied and enforce, and the best thing is that we have done the conversion without ever unmounting the filesystem! In the options of the balance command, dconvert is for converting data (d), but you can also use mconvert to change the protection level of metadata.

Add another device

Now, if at some point we are running out of space, BTRFS can also be used to expand dinamically the filesystem. By the way, there’s a nice way to test out and fill a filesystem: I’ve found this page with a nice bash script that generates multiple files with random size by simply setting some parameters in the script. I’m copying the script here just in case the page may disappear in the future. Be careful with the MAXSIZE parameter, this is bytes, so you want to use a large number otherwise it will take ages to fill the disk. In my modified version, I’m using up to 100MB per file.



We just need to save this script in the btrfs partition, and run it multiple times until the filesystem is completely filled:

Meanwhile, the partition is completely filled:

Now, to fix the problem and regain some free space, first we need to add a new device to the virtual machine:

Then, we add /dev/sde to the btrfs filesystem:

The disk is now part of the btrfs tree:

And we immediately see the new size and available space:

What we are still missing is the RAID1 balanced distribution of the chunks, as the total size of the volume is indeed now 140G (4 disks, 70G each, divided by two by the RAID1 option) but as you can see from the previous output, the first three disks are completely full while the new one is empty, and the effective free space is only 5.9M. We can rebalance the chunks distribution in BTRFS. This can be done incrementally, by using the option dusage. 5 for example means btrfs will try to relocate chunks with less than 5% of usage:

You may want to increase the value at each run, you will see new chunks will be rebalanced at each run. If you can fee up just a little bit of space, you can even choose 100 for the value. You can also monitor the progress with a command like this:
After some time, the final result will be like this:

See? Now each disk is consuming the same amount of space. The balance can take some time, depending on the amount of chunks that need to be relocated, and the speed of the disks. And before you ask, yes btrfs can do the rebalancing automatically, if you have at least version 3.18; on CentOS sadly it’s still at version 3.16…

Final notes

Those are few examples of what you can do with BTRFS, but the new filesystem and its tools really allow for many more operations.

There are many tutorials all over the Internet, and the best way to learn more about BTRFS is to build a small virtual machine like I did to play with it.


2 thoughts on “Playing with BTRFS

  1. Nice article. I have a question though. When you added 3 devices in raid1 btrfs decided to use it as raid5 since total available space is equal to one you’d get with classic raid5? If so, is there a way to use third device as a third copy or hot spare?

Comments are closed.