Install bcache on existing system

A few days ago I successfully install bcache to my system Ubuntu 14.04. It was easy, but there is a lot confusing information on the Internet, that's why I decided to write this tutorial.

Is it safe for my data?

Yes it is safe. During installation all data will be copied to new partition.

Why do I need bcache?

My Ubuntu installed on SSD. Eventualy my root (/) partition became larger than SSD (Low disk space on "filesystem root"). I want my computer work as fast as it can, that's why I need bcache. bcache supports multiple backing devices and one caching, that mean one SSD could work as cache for multiple partitions. For me it is /home and /.

My configuration before installation

My laptop ASUS ZENBOOK UX32VD. My system configuration before installation:

  1. /dev/sdb - SSD
    • /dev/sdb1/ - 1GB /boot
    • /dev/sdb2/ - 22GB /
  2. /dev/sda - HDD
    • /dev/sda1 - 321G /home
    • /dev/sda2 - 1G this is for backup my /boot
    • /dev/sda3 - 30G this is for backup my /

What to do ?

  1. Because I already have /dev/sda2 and /dev/sda3 for backing up I don't need to create new partitions. But if you don't have them, create new partition on HDD for you new root partition.
  2. Download Ubuntu 14.10 image, and create USB bootable Ubuntu. Important note: this Ubuntu image must have the same bit as your Ubuntu (64bit for 64bit and 32bit for 32bit). This is because we are going to run chroot.
  3. Boot from USB, choose Try Ubuntu without installing.

    • install bcache tools, it should be in 14.10 repositories by default

      sudo add-apt-repository "deb http://archive.ubuntu.com/ubuntu $(lsb_release -sc) universe"
      sudo apt-get update
      sudo apt-get install bcache-tools
      
    • Make new root partition (in my case /dev/sda3) backing device

      wipefs -a /dev/sda3
      make-bcache -B /dev/sda3
      mkfs.ext4 /dev/bcache0
      
  4. Mount your old / to /media/root_old, and your new root partition as /media/root_new. Copy all data from old to new (rsync copy):

    mkdir /media/root_old
    mkdir /media/root_new
    mount /dev/sdb2 /media/root_old
    mount /dev/bcache /media/root_new
    rsync -avxHAXW --progress --numeric-ids /media/root_old /media/root_new
    
  5. get UUID of /dev/bcache0 device

    ls -l /dev/disk/by-uuid/ | grep bcache0
    
  6. chroot to new root (/media/root_new) to edit fstab and to update grub:

    # I have to mount `/boot` because it is on separate partition.
    mount /dev/sdb1 /media/root_new/boot
    # prepare chroot
    for i in /sys /dev /run /proc; do mount --bind "$i" "/media/root_new$i";done
    chroot /media/root_new
    

    substitute UUID for / in /etc/fstab/ to UUID that you obtained on step 5

    #vim /etc/fstab or #nano /etc/fstab
    

    now update grub

    update-grub
    

    exit chroot and unmount everything we mount to /media/root_new

    exit
    umount --recursive /media/root_new
    

    Now reboot to new created partition.

  7. On this step you should be able to boot from new root partition, you will see it in grub menu. If you succesfully boot from new partition, proceed to next step. If not may be repeat step 6, and check UUID you obtained on step 5.

  8. Create caching device from old root. This will erase everything from your old root partition

    wipefs -a /dev/sdb2
    make-bcache -C /dev/sdb2
    

    find your caching device UUID:

    ls /sys/fs/bcache/
    

    now use this UUID to attach caching device to backing device

    echo <UUID> > /sys/block/bcache0/bcache/attach
    
  9. Check if caching is started and working:

    $ tail /sys/block/bcache0/bcache/stats_total/*
    ==> /sys/block/bcache0/bcache/stats_total/bypassed <==
    8.1G
    
    
    ==> /sys/block/bcache0/bcache/stats_total/cache_bypass_hits <==
    352707
    
    
    ==> /sys/block/bcache0/bcache/stats_total/cache_bypass_misses <==
    0
    
    
    ==> /sys/block/bcache0/bcache/stats_total/cache_hit_ratio <==
    61
    
    
    ==> /sys/block/bcache0/bcache/stats_total/cache_hits <==
    187802
    
    
    ==> /sys/block/bcache0/bcache/stats_total/cache_miss_collisions <==
    407
    
    
    ==> /sys/block/bcache0/bcache/stats_total/cache_misses <==
    116714
    
    
    ==> /sys/block/bcache0/bcache/stats_total/cache_readaheads <==
    0
    

Conclusion

I made a simple bcache test. I was trying to copy .iso image of Ubuntu using dd command:

# sync && dd if=/home/c0rp/image.iso of=/image.iso bs=512k oflag=direct

I did this test a few times with bcache turned off and turned on. Here is average copying speed.

  • bcache turned on: 250-300 MB/s
  • bcache turned off: 70-110 MB/s

I was turning on/off bcache by using this commands:

$ sudo su - -c 'echo none > /sys/block/bcache0/bcache/cache_mode'
$ sudo su - -c 'echo writeback > /sys/block/bcache0/bcache/cache_mode' 

Sources