How to setup bcache?

I've tried to set up bcache in Ubuntu 14.04 with a fresh install and an empty /home partition (/dev/sdb2) as a backing device. I have a running system on my SSD and I'd like to use its /dev/sda2 partition to cache my /home . I've always managed to sort my ubuntu problems out from forums but I'm struggling to understand the basics here and need help:

  1. What account do I need to be logged in when I set up bcache in the first place? Obviously the user created during install on /dev/sdb2 has to be logged out for /home to be unmounted, so I set up a temporary user account on a separate partition to do it. This seems too clumsy already.

  2. make-bcache -B and -C both tell me that there are non-bcache superblocks on the device so I did sudo wipefs -a on both of the devices. The partitions then appear as unformatted without an UUID in GParted. Is this normal?

  3. If so, what happens to fstab and /home at boot? Do I need to use bcache's UUID in fstab? I did but the system didn't find the /home partition upon next boot.

  4. Both echo /dev/sdb2 > /sys/fs/bcache/register and echo /dev/sda2 > /sys/fs/bcache/register return 'Permission denied' messages; as I understand /sys cannot be written to.

    So echo /dev/sdb2 | sudo tee /sys/fs/bcache/register seems to have worked.

Can someone verify this is the proper way to do it in Ubuntu?

I seem to have managed to create, register and attach the backing drive but according to the tail /sys/block/bcache0/bcache/stats_total/* I only have bypassed data. My whole setup is quite a mess now so any clarification would be greatly appreciated.


With Ubuntu 15.10, alex's answer is no longer correct. The initramfs image is locked in the installer and will not be automatically re-generated when the system is installed. Thus one will have a bcache installation, but it won't be able to boot. I've written a more verbose description of the following process elsewhere; however, here are the basics:

1. Partitioning

Boot into the Ubuntu live-cd and create the following partition scheme with fdisk or gparted. Assuming /dev/sda is your SSD and /dev/sdb is your HDD:

/dev/sda1 - 1024 MB, EXT4, used for /boot (grub/grub2 doesn't support bcache)
/dev/sda2 - any format, for cache
/dev/sdb1 - EFI partition (if your machine needs it)
/dev/sdb2 - swap
/dev/sdb3 - any format, backing partition

2. Create the bcache device

Open up the terminal, wipe the file systems on the backing and caching device:

sudo wipefs -a /dev/sda2
sudo wifefs -a /dev/sdb3

Install bcache-tools and create the bcache device:

sudo apt-get update
sudo apt-get install bcache-tools
sudo make-bcache -B /dev/sdb3 -C /dev/sda2
sudo mkfs.ext4 /dev/bcache0

3. Install Ubuntu

Install the operating system using the custom partitioner as follows:

/dev/bcache0 - format EXT4, use as /
/dev/sda1    - format EXT4, use as /boot
/dev/sdb1    - EFI partition (if your machine needs it)
/dev/sdb2    - swap

DO NOT REBOOT after the installer completes!

4. Install bcache on the new installation

Using a chroot we install bcache-tools on the new installation. Setup the environment and enable DNS resolution

sudo mount /dev/bcache0 /mnt
sudo mount /dev/sda1 /mnt/boot
sudo mount --bind /dev /mnt/dev
sudo mount --bind /proc /mnt/proc
sudo mount --bind /sys /mnt/sys

For Ubuntu 17.10 or newer you also need to copy over the resolv.conf file so DNS resolution works within the chroot environment.

sudo mv /mnt/etc/resolv.conf /mnt/etc/resolv.conf.backup
sudo cp /etc/resolv.conf /mnt/etc/resolv.conf

Enter into the chroot and do the installation

sudo chroot /mnt
sudo apt-get update
sudo apt-get install bcache-tools

During the installation you should notice the initramfs is re-generated and installed to the (chroot-ed) /boot partition.

5. Cleanup

Cleanly dismount the filesystem and reboot.

exit
sudo mv /mnt/etc/resolv.conf.backup /mnt/etc/resolv.conf # for 17.10+ only
sudo umount /mnt/sys
sudo umount /mnt/proc
sudo umount /mnt/dev
sudo umount /mnt/boot
sudo umount /mnt
sudo reboot

After some trial and error I've arrived at the following solution for a clean install:

  • Partition the HDD the way you like (assuming /dev/sdb2 is the space you want for your /home).
  • # apt-get install bcache-tools
  • # make-bcache -B /dev/sdb2 -C /dev/sda2 (This creates /dev/bcache0, but the UI installer can't use it yet.)
  • # mkfs.ext4 /dev/bcache0 (This makes the installer see a "partition /dev/bcache0" under "device /dev/bcache0" and allows you to use it as "/home" during installation.)
  • Run the installer as usual, be sure to select advanced disk partition options.
  • The installer creates initrd image with the bcache kernel module included and loaded due to that we've installed bcache-tools earlier, so after reboot it just works.
  • After booting the installed system, install bcache-tools once again, so that further updates don't cripple the boot process by omitting the necessary bcache kernel module from initrd image.