BTRFS as root filesystem

System overview

  1. /dev/sda1 4.08GB ext4 “/” used 1.56GB, free 2.53GB
  2. /dev/sda2 10.91GB extended
    1. /dev/sda5 524MB swap
    2. /dev/sda6 10.40GB ext4 “/home” 321MB used, 10.09GB free

Boot from live-cd

Btrfs support is heavily developed in Linux Kernel. So do not use old kernel.

I was using gparted-live-0.22.0-1-amd64.iso (NOTE: to match your existing system architecture (i586, amd64, etc)) Which contains:

  1. Kernel 3.16.0-4-586
  2. btrfs utils version 3.17

Update to kernel 4

Edit /etc/apt/sources and add sid release

apt-get update
apt-get install linux-image-amd64
apt-get install btrfs-tools
update-initramfs -u
  • ERROR: Warning: /sbin/fsck.btrfs doesn't exist, can't install to initramfs, ignoring.
  • EXPLANATION: “This is going to be changed back in the next version of btrfs-tools.”
  • SOLUTION: ln -s /bin/fsck.btrfs /sbin/fsck.btrfs

Check btrfs-tools version

/bin/btrfs version

Converting dirs into subvolumes

To make backups, snapshots, moving much easier and faster, every directory you want to backup by snapshot should be a subvolume. Also root filesystem should be created in separate subvolume not in btrfs root.

To distinguish volumes from directories we will use @ character at beginning of subvolume name. This is Ubuntu naming convention for subvolumes.

Proposed subvolumes structure (where / is BTRFS root)

  • /@
  • /@home
  • /@var

Mount BTRFS filesystem (BTRFS root) to /mnt/btrfs.

root

  • take a snapshot of root volume. Name of snapshot is '@'
    • btrfs subvolume snapshot /mnt/btrfs @
  • delete files on BTRFS root (/mnt/btrfs)
  • make @ subvolume a default root
    • update fstab:
      /etc/fstab
      UUID=739e6086-d925-4bdb-94f5-26d8c10dc171	/ btrfs defaults,subvol=@
  • OR
    • get @ subvolume id btrfs subvolume list /mnt/btrfs
    • set default subvolume to use when system is mounted btrfs subvolume set-default 256 /mnt/btrfs

subdirectories

  • On BTRFS root create a new subvolume
    • btrfs subvolume create /mnt/btrfs/@home
  • move files into the new subvolume as if it were a directory
    • mv /mnt/btrs/@/home/* /mnt/btrfs/@home/
    • cp –reflink is much faster on BTRFS
  • remove old directory
    • rmdir /mnt/btrfs/@/home
  • update /etc/fstab to mount @home as /home

fstab

Do not use errors=remount-ro option which is not valid for BTRFS. Example working fstab file:

/etc/fstab
UUID=ed730508-753e-4a7f-ac1e-bf8646f7bd63	/		btrfs		defaults,subvol=@,compress=no				0	0
UUID=ed730508-753e-4a7f-ac1e-bf8646f7bd63	/var		btrfs		defaults,subvol=@var,compress=no			0	0
UUID=ed730508-753e-4a7f-ac1e-bf8646f7bd63	/var/log	btrfs		defaults,subvol=@varlog,compress=no			0	0
UUID=ed730508-753e-4a7f-ac1e-bf8646f7bd63	/home		btrfs		defaults,subvol=@home,compress=no			0	0
UUID=ed730508-753e-4a7f-ac1e-bf8646f7bd63	/mnt/btrfs	btrfs		noauto,defaults,subvolid=5,compress=no			0	0

To prevent systemd to halt system during startup if device is not found (nofail and x-systemd.device-timeout):

/etc/fstab
UUID=<the_device_uuid> /mount/point btrfs nofail,x-systemd.device-timeout=10,noatime,compress=lzo,autodefrag 0 0

NOTE: do not use autodefrag - it consumes lots of cpu