My journey with Btrfs part 1

Btrfs is a new file system for Linux. A file system is used to manage how files are stored in a computer. Btrfs goal is to provide advanced features such as pooling or snapshot with a focus on fault tolerance, repair and easy administration. I possess at home a NAS, which contains all my data. But this data is stored on several hard-drives, each being totally separate. And if one of them were to die tomorrow the data stored on it would be lost. With Btrfs I will be able to combine my hard-drives to show up as a single one. I could add more storage in the future and if one them end up dying I should be able to restore the lost data. When said like this it sounds magical, in reality it's not. When configured for it, Btrfs distribute data across the drives it possesses in  such a way that, if the data get lost or corrupted, it can restore it from the others disk. In my case I will use raid 5 which can handle the lost or corruption of just one spinning platter (I badly need more synonyms ... ) . While I'll write this post for people to follow closely the same steps as me, this is not a "how-to Btrfs raid 5". I do not know whether or not I will be successful in my quest. This is more of a documentary, and if I managed to succeed at the end, then you can follow the step that I took. I do not bear any responsibility for the issues that you might encounter. Before starting, I needed to check the version of the kernel I was using. The Btrfs feature that I want to use, raid 5 was present in Linux, but experimental, as there weren't any tools for repairing or replacing a device. The version of Linux required to be able to repair our array of drive is 3.19 . To check which version of the kernel you are running, execute this command:
 uname -r
Since I was running Debian stable (also known as Jessie) I had to upgrade to testing. I did the upgrade before writing this post. Once I made sure I was running a more up-to-date version of Linux I had to install the tools to manage Btrfs.
root@vaur:~# aptitude install btrfs-tools The following NEW packages will be installed: btrfs-tools packages upgraded, 1 newly installed, 0 to remove and 0 not upgraded. Need to get 519 kB of archives. After unpacking 3,692 kB will be used. Get: 1 http://ftp.fr.debian.org/debian testing/main amd64 btrfs-tools amd64 4.4-1 [519 kB] Fetched 519 kB in 1s (291 kB/s) (Reading database ... 146996 files and directories currently installed.) Preparing to unpack .../btrfs-tools_4.4-1_amd64.deb ... Unpacking btrfs-tools (4.4-1) ... Processing triggers for libc-bin (2.21-9) ... Processing triggers for man-db (2.7.5-1) ... Setting up btrfs-tools (4.4-1) ... Processing triggers for libc-bin (2.21-9) ... root@vaur:~#
Before doing anything else I need to identify which hard-drives I'm going to use for my array. The one that I want to work on are my two 2TB drives that are in my system. For a Btrfs raid 5 you need at least two devices.
root@vaur:~# fdisk -l Disk /dev/sda: 298.1 GiB, 320072933376 bytes, 625142448 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disklabel type: dos Disk identifier: 0x0000ef64 Device     Boot    Start       End   Sectors   Size Id Type /dev/sda1  *        2048  19531775  19529728   9.3G 83 Linux /dev/sda2       19533822 625141759 605607938 288.8G  5 Extended /dev/sda5       19533824  36102143  16568320   7.9G 82 Linux swap / Solaris /dev/sda6       36104192 625141759 589037568 280.9G 83 Linux Disk /dev/sde: 3.7 TiB, 4000787030016 bytes, 7814037168 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 4096 bytes I/O size (minimum/optimal): 4096 bytes / 4096 bytes Disklabel type: gpt Disk identifier: 330FCD63-59EA-4EAF-AD76-BA224132321D Device     Start        End    Sectors  Size Type /dev/sde1   2048 7814037134 7814035087  3.7T Linux filesystem Disk /dev/sdc_ 931.5 GiB, 1000204886016 bytes, 1953525168 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disklabel type: dos Disk identifier: 0x0009a54b Device     Boot Start        End    Sectors   Size Id Type /dev/sdc1          63 1953520064 1953520002 931.5G 83 Linux Disk /dev/sdd: 1.8 TiB, 2000398934016 bytes, 3907029168 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 4096 bytes I/O size (minimum/optimal): 4096 bytes / 4096 bytes Disklabel type: dos Disk identifier: 0x000658b4 Device     Boot Start        End    Sectors  Size Id Type /dev/sdd1        2048 3907024895 3907022848  1.8T 83 Linux Disk /dev/sdb: 1.8 TiB, 2000398934016 bytes, 3907029168 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 4096 bytes I/O size (minimum/optimal): 4096 bytes / 4096 bytes Disklabel type: dos Disk identifier: 0x000c4ecd Device     Boot Start        End    Sectors  Size Id Type /dev/sdb1  *     2048 3907028991 3907026944  1.8T 83 Linux root@vaur:~#
The two devices that I want to use are known to Linux as /dev/sdb and /dev/sdd . I can now format them to Btrfs. This will delete any data I have on those. Everything that they contain has already been transferred to my 4TB drive.
root@vaur:~# mkfs.btrfs /dev/sdb /dev/sdd btrfs-progs v4.4 See http://btrfs.wiki.kernel.org for more information. /dev/sdb appears to contain a partition table (dos). Use the -f option to force overwrite. root@vaur:~#
The command warns me that the devices contain partition table. It didn't format to prevent any unwanted lose of data. Since I know I don't care about that data, I'll force it to execute.
root@vaur:~# mkfs.btrfs -f /dev/sdb /dev/sdd btrfs-progs v4.4 See http://btrfs.wiki.kernel.org for more information. Label:              (null) UUID:               db02f075-f89f-45e5-9d3d-c9a12995268f Node size:          16384 Sector size:        4096 Filesystem size:    3.64TiB Block group profiles: Data:             RAID0             2.01GiB Metadata:         RAID1             1.01GiB System:           RAID1            12.00MiB SSD detected:       no Incompat features:  extref, skinny-metadata Number of devices:  2 Devices: ID        SIZE  PATH 1     1.82TiB  /dev/sdb 2     1.82TiB  /dev/sdd root@vaur:~#
Linux successfully created a new Btrfs file system on my computer with the charming name of "db02f075-f89f-45e5-9d3d-c9a12995268f". The total size is 3.64TiB. Data are stored in raid 0. Meaning that Btrfs doesn't do any type of redundancy on it. Metadata is stored in raid 1. Metadata is the information about the files stored such as where is located the file on the drives, what size it is, what is the name of the file, etc. I don't know what System is for, I'm guessing it's some information for Btrfs. So by default, Btrfs makes a raid 0. And I have a confession to make, I misread the guide that I was following. I thought that I first needed to format my drives and then put them in a raid 5 configuration. In reality things are much simpler. To have data in raid 5 I just need to add the option -d raid5.
root@vaur:~# mkfs.btrfs -f -d raid5 /dev/sdb /dev/sdd btrfs-progs v4.4 See http://btrfs.wiki.kernel.org for more information. Label:              (null) UUID:               2b820b02-e391-4f7a-9d9c-335f5abae4f5 Node size:          16384 Sector size:        4096 Filesystem size:    3.64TiB Block group profiles: Data:             RAID5             1.01GiB Metadata:         RAID1             1.01GiB System:           RAID1            12.00MiB SSD detected:       no Incompat features:  extref, raid56, skinny-metadata Number of devices:  2 Devices: ID        SIZE  PATH 1     1.82TiB  /dev/sdb 2     1.82TiB  /dev/sdd root@vaur:~#
Let's check the current state of the drives:
root@vaur:~# btrfs filesystem show /dev/sdb Label: none  uuid: 2b820b02-e391-4f7a-9d9c-335f5abae4f5 Total devices 2 FS bytes used 112.00KiB devid    1 size 1.82TiB used 2.01GiB path /dev/sdb devid    2 size 1.82TiB used 2.01GiB path /dev/sdd root@vaur:~#
It is worth noting that from now on using /dev/sdb is the same as using /dev/sdd. They both point to my Btrfs file system. Let's mount it and see what df as to say about it.
root@vaur:~# mount /dev/sdb /media/vaur-disk root@vaur:~# df -h /dev/sdb Filesystem      Size  Used Avail Use% Mounted on /dev/sdb        3.7T   17M  3.7T   1% /media/vaur-disk root@vaur:~#
I can now use it like any file system, I'll copy a file on it and see what Btrfs does with it.
vaur@vaur:~$ cp -v /media/vaur-content/Videos/Internet/Benjamin\ Bayart/les-dangers-dinternet.avi /media/vaur-disk/ '/media/vaur-content/Videos/Internet/Benjamin Bayart/les-dangers-dinternet.avi' -> '/media/vaur-disk/les-dangers-dinternet.avi' root@vaur:/home/vaur# btrfs filesystem show Label: none  uuid: 2b820b02-e391-4f7a-9d9c-335f5abae4f5 Total devices 2 FS bytes used 895.42MiB devid    1 size 1.82TiB used 2.01GiB path /dev/sdb devid    2 size 1.82TiB used 2.01GiB path /dev/sdd root@vaur:/home/vaur#
Well ... looks like btrfs used the 2.01GiB already allocated on my two hard-drives to store this file. I'll need a bigger file to see any effect on it. I used an old image I made of my ssd. The file is 233 GiB. The transfer took a bit of times. I also noticed that copying used a bit of cpu, which is to be expected. In the next post I will do some benchmarks.
vaur@vaur:~$ cp /media/vaur-content/backup/vaur-ssd-10-12-2013.img /media/vaur-disk/ vaur@vaur:~$
Now let's see how Btrfs used my hard-drives.
root@vaur:/home/vaur# btrfs filesystem show /dev/sdb Label: none  uuid: 2b820b02-e391-4f7a-9d9c-335f5abae4f5 Total devices 2 FS bytes used 234.06GiB devid    1 size 1.82TiB used 253.01GiB path /dev/sdb devid    2 size 1.82TiB used 253.01GiB path /dev/sdd root@vaur:/home/vaur#
I have to say, this is rather odd. To store 234 GiB it used 253 GiB on both devices. It's behaving like a raid 1 when it should be a raid 5. I guess this is time to ask for some help on irc. I'll head to #btrfs@irc.freenode.org and make a new post once I've got more information.                

This article is my 2nd oldest. It is 1559 words long, and it’s got 0 comments for now.