Friday, November 30, 2012

Setting up multi device btrfs on Ubuntu

Simply getting / to work with btrfs used to be more complicated. I managed to get something working in Ubuntu 11.04 but it wasn't something you'd want to maintain. It required patches and stuff which means everytime grub or the kernel got updated you'd have to get your hands dirty all over again.

On Ubuntu 12.10, things are way easier. Things could certainly be better but it's not awfully hard to do either.

Here, I'll be converting my Ubuntu 12.10 which uses ext4 to btrfs, then adding a 2nd hard disk into the btrfs volume. It works and is much simpler than you might think.

Converting ext4 to btrfs

There's a good post over at HowtoForge which is the reference for what I'm posting here. Their explanation is much more detailed so you might want to check it out if you're into that.

First, you'll want to install btrfs-tools on your Ubuntu setup.

~$ sudo apt-get install btrfs-tools

Then, you'll want to boot into a LiveCD/LiveUSB Ubuntu to proceed with the conversion. You can't convert / while it's in use. If you need the Ubuntu ISO you can download it from the website.

Here, I'll be assuming /dev/sda is the hard disk with Ubuntu installed so you'll want to change that accordingly.

~$ sudo fsck -f /dev/sda1
~$ sudo btrfs-convert /dev/sda1

The conversion will take a while so sit back and have a coffee.

~$ sudo mount /dev/sda1 /mnt
~$ for fs in proc sys dev dev/pts; do sudo mount --bind $fs /mnt/$fs; done
~$ sudo chroot /mnt

It's a good idea to check your progress before proceeding. Make sure all's right with the world.

~# ls -l

You should have a folder called ext2_saved. This is the subvolume automatically created in case you want to rollback you filesystem. Even if you're using ext4 it'll still say ext2_saved so don't worry about it.

Next, you'll want the UUID for the newly converted btrfs volume.

~# blkid /dev/sda1

You should get something like the following:

/dev/sdb1: UUID="87c16261-2d2a-4f35-b2f4-6b4490540545" UUID_SUB="222b7cde-52f7-46dc-bdde-2d47874b4670" TYPE="btrfs"

Copy down the UUID string (without the quotes).

~# nano /etc/fstab

Edit /etc/fstab, commenting out the line that loads your / then writing a new one similar to it. Use the UUID you copied, set options to defaults instead of errors=remount-ro.

Next is to remove the annoying error: sparse file not allowed message that btrfs boot volumes are known for.

~# nano /etc/grub.d/00_header

Comment out line #93. Just add a # in front of the line. It should look something like this:

function recordfail {
  set recordfail=1
  #if [ -n "\${have_grubenv}" ]; then if [ -z "\${boot_once}" ]; then save_env recordfail; fi; fi
}

Finally, grub.

~# update-grub
~# grub-install /dev/sda
~# exit

You should be able to reboot into your newly converted btrfs volume.

Adding a volume to btrfs

Now that btrfs is up and running you can easily add a 2nd hard disk to it. You'll need to format the hard disk as a btrfs volume first. Ubuntu comes with a disk management tool so that shouldn't be hard.

~$ sudo btrfs device add /dev/sdb1 /
~$ sudo btrfs balance start /

I'm assuming the 2nd hard disk is /dev/sdb with a single btrfs partition called /dev/sdb1. The above command should add the volume and balance data across both.

Depending on how much data is on the volumes, it'll take some time to reallocate everything. But that's about it.