How to Use Btrfs On Newly Installed Disks

Btrfs (pronounced ‘Butter F S’) is an advanced filesystem for Linux which can work across multiple hard disks and supports different fault tolerance models like RAID 0, RAID 1 and RAID 10. Btrfs has been in development since 2008 and it is what is known as a “copy on write” filesystem which means that when the data changes in a block, then the block will be copied a new block written to the disk with the changes incorporated. This means that blocks are never modified but rather new blocks are created and the old blocks are later reused. This has advantages for performance especially when ensuring consistency and integrity (even after a power interruption).

Assuming you have an already running Linux system (probably using ext4) but you want to add some hard drives and use Btrfs, this is what you would need to do. First you need to install the Btrfs tools. On Ubuntu:

sudo apt-get install btrfs-tools

Assuming that /dev/sda is the main disk with Linux installed on it and you want to use two new disks /dev/sdb and /dev/sdc, the next step is to create the btrfs filesystem on these disks.

sudo mkfs.btrfs -m raid1 -d raid1 /dev/sdb /dev/sdc

The -d raid1 tells btrfs to use RAID 1 mirroring for the data. This means that there will be at least two copies of every bit of data, each on a different device. It is actually possible to use more than two hard disks in a RAID 1 mirroring configuration. In such cases, btrfs will ensure that at least one other disk has a copy of the data. The information about the data including the filename and file permissions etc is stored in what is known as the metadata. The -m raid1 options tell btrfs to use RAID 1 mirroring for the metadata as well. Like the mirroring for the data, using mirroring for the metadata will ensure that the essential information about the files is stored on at least two disks. If any of the disks have existing partition tables (and possibly data) then use the -f option to force mkfs.btrfsto overwrite.

Now that the filesystem has been created, it can be mounted using the normal mount command:

sudo mount /dev/sdb /mybtrfs

Where /mybtrfs is the directory where you want to mount the filesystem.

btrfs-mount-and-df

At this point, the new filesystem will be listed by the df -h command. On my test system, /dev/sdb and /dev/sdc are 100 GB each. On a traditional RAID 1 system, the resulting filesystem would be listed as just 100 GB as one disk is used to duplicate the data. This is not so with btrfs. Because disks of different sizes can be used in any combination, btrfs shows the total size. There is however a command which gives more details:

sudo btrfs filesystem df /mybtrfs

This will show the true disk usage for a btrfs filesystem including the space taken by the actual data on filesystem and the metadata.

btrfs-filesystem-df

The “sudo btrfs filesystem show” is also useful as it will list the individual devices used in the filesystem along with their total size and space used.

The final step is to edit the /etc/fstab file to automatically mount the btrfs filesystem at boot. To do that edit the file and added the following line:

/dev/sdb   /mybtrfs   btrfs   defaults   0   0

You can get the universally unique identifier for the btrfs filesystem using the sudo btrfs filesystem show command. To mount using the uuid rather than the device name would make the /etc/fstab line something like this:

UUID=7911f7a9-cd03-48ef-8dca-27550d6039f4 /mybtrfs btrfs defaults 0 0

If you have any more question, feel free to ask in the comment below.