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:

sudoapt-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:

sudomount/dev/sdb /mybtrfs

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

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.

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 00

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:

Hi, I am a newbie. I am having a new PC delivered next week with the option of adding the two HHD drives ( 1TB & 500 GB ) from my old PC. I want to run either Ubuntu or Mint [a bit undecided] and I would like to put Zentyal on an Ubuntu server for a small charity I am starting up, onthe 500GB I think. So 2 x 1TB & 1 x 500GB Do I use btrfs? and how do I do it? Your thought would be appreciated.