domenica 3 febbraio 2013

How-To install Linux Mint Debian Edition on Btrfs using subvolumes (only 5 steps, MAY work on ubuntu too!)

This post will guide you through the installation of LMDE on Btrfs filesystem.
The goal is to have an high performing filesystem under you installation with the ability to take live snapshots of both your / and your /home folders in an independent way so you can rollback your system or data just by defining wich snapshot you want to use.
NOTE: this may work on ubuntu too (anyway check to have btrfs-tools installed first)

STEP 1: Create partitions and install system

Boot from LiveCD and define a swap partition and a btrfs partition (in this tutorial will be swap=sda1, btrfs=sda2):

Install LMDE normally and do NOT reboot at the end.

STEP 2: Mount Btrfs and create subvolumes for / and /home

Open terminal and create a mountpoint and mount btrfs volume:
sudo su
mkdir /mnt/btrfs
mount /dev/sda2 /mnt/btrfs/ 
Create two subvolumes (one for / and one for /home):
/sbin/btrfs subvolume create /mnt/btrfs/root-sub
/sbin/btrfs subvolume create /mnt/btrfs/home-sub

STEP 3: Move installation to the root-sub and home folders to the home-sub subvolumes

Then you have to copy all the installed system under the root-sub subvolume:
rsync -avz --exclude=root-sub --exclude=home-sub /mnt/btrfs/ /mnt/btrfs/root-sub/
wait till is done (5-10 minutes maybe).
Copy the home folder to the home-sub subvolume:
rsync -avz /mnt/btrfs/home/ /mnt/btrfs/home-sub/
this will take just a few seconds (only few config files there) then you can erase all the directories in the btrfs root volume (in order to keep just the one you've copied into root-sub):
cd /mnt/btrfs/
rm -r !(root-sub|home-sub)

STEP 4: Define default subvolume for /

Now you can set a default subvolume to be mounted when the device is called for mounting, in our case will be root-sub (this makes GRUB able to access directly our root-sub without modifyng default grub options).
You can check the subvolumes IDs:
/sbin/btrfs subvolume list /mnt/btrfs/
you get an output similar to this:
ID 257 top level 5 path root-sub
ID 258 top level 5 path home-sub
now set the root-sub subvolume (check your ID, in this case it is 257 for root-sub, not shure that can be the same for you, anyway..):
/sbin/btrfs subvolume set-default 257 /mnt/btrfs/  

STEP 5: Set /etc/fstab to mount subvolumes correctly

We are now going to disable the fsck at boot because at time of writing is still under heavy development, so I think that can be risky to set it automatically at boot, and moreover there's a missing symbolic link that will stop boot process.
So let's modify the fstab of your newly installed system:
pluma /mnt/btrfs/root-sub/etc/fstab
modify the line where / mountpoint is defined (usually the last line in the fstab file) by replacing the last "1" (pass) with a "0" to disable fsck at boot, it has to look like this:
# /dev/sda2
UUID=xxxx_whatever_xxxxxx    /    btrfs    defaults    0    0
then add a line to mount the home-sub subvolume in /home folder:
# home subvolume
/dev/sda2    /home    btrfs    defaults,subvol=home-sub    0    0
Save and close.

NOTE: you can use UUID if you prefer just copy it from the line where / mountpoint is defined

Enjoy Btrfs!

And... yes, you're good to go: you can reboot and enjoy your Btrfs powered LMDE installation, now you have 2 different subvolumes for / and /home and you can take indipendent snapshots.

Here some commands you may need:

  • to create a / snapshot:
    sudo btrfs subvolume snapshot / /root-snapshot-1
    or an /home snapshot:
    sudo btrfs subvolume snapshot /home /home/home-snapshot-1
  • to delete the snapshots:
    sudo btrfs subvolume delete /root-snapshot-1
    sudo btrfs subvolume delete /home/home-snapshot-1 
  • to list all subvolumes and snapshots in your btrfs:
    sudo btrfs subvolume list /
  • to set a new default snapshot for root (if you want to rollback your system) you have to set:
    /sbin/btrfs subvolume set-default XXX /mnt/btrfs/  
    where XXX is the ID of the snapshot (you can check it by listing the subvolumes as described in previous point), then reboot
  • to roll back (hardcore way) your home folder you have to choose the snapshot name (not ID) by listing it and set the correct mount option in /etc/fstab using:
    where XXXXX is the volume name, in the case of the home snapshot we took in this example (just few lines above) the line in /etc/fstab will be:
    /dev/sda2    /home    btrfs    defaults,subvol=home-sub/home-snapshot-1    0    0

Tuning tips

  • To force transparent compression on all managed files, in order to increase your usable storage space and increase write/read performance, you can use this mount option
  • If you are on SSD drive you may want to enable discard/TRIM to mantain r/w performance over time by clearing to 0 the deleted blocks. You just add this other mount option:
  • If you are running kernel 3.2 (or newer) and you use a single SSD drive (not playing around with mixed devices and RAID array) the following mount options can be used to improve performance (ssd,space_cache,noatime,compress-force=zlib), mantain performance over time (discard), reduce fragmentation (ssd_spread) and enable autorecovery upon mount (recovery):
An example of fstab line to enable compression and TRIM on the /home subvolume:
/dev/sda2    /home    btrfs    defaults,subvol=home-sub,discard,compress-force=zlib    0    0