Linux

Using bcache with existing data (SSD caching in Linux)

This is a toutorial on how to install BCache on Linux. You should read through the whole thing before beginning.

Edit: If you want to work with a caching system that is designed to be applied to a drive that is in use check out EnhanceIO. You can Google for instructions for your distribution.

Many computers have SSD drives for speed. You can use them as a regular partition. Or you may want to use it for caching. Often  SSD drives are built right  into the hard drive explicitly for this purpose. Bcache is a tool for Linux to facilitate this as it does not happen automatically. Its not as easy as Windows but as with anything with Linux it gives you a lot more flexibility. The consensus all over the internet was that you could not install BCache on an existing system. I found a way around this limmitation and I wanted to share it.

It took me hours to get this to work. At one point my home partition was unrecognizable and wouldn’t boot up. After a eureka moment I realized I might have a way to install Bcache with existing data. I was doubtful but to my surprise it worked.

The big secret? Shrink your partition in GParted and create a blank one. I’ll explain how this works.

Note: All commands below should be run as a superuser

  1. If you have kernel version 3.10 or greater you should have bcache. If not, install it from here http://evilpiepirate.org/git/linux-bcache.git. You will also need bcache-tools, which you can find here:  http://evilpiepirate.org/git/bcache-tools.git. Install these as well.
  2. Make sure you have the kernel module by loading it as a superuser. Run
    modprobe bcache
  3. You will need GParted or some other program that can shrink and create partitions. (http://gparted.org). It is a live CD. You should understand how to use it otherwise this is probably too advanced as it requires basic knowledge of partitions.
  4. Once you load GParted you will want to shrink the partition you want to cache. You can leave about 100mb of free space. You need more empty space on the drive than the amount used by the partition you want to cache.
  5. Format the empty space as ext4 and do not set a mount point. On your SSD drive format the entire drive as ext4 and do not set a mount point. Then restart your computer.
  6. Boot back into your regular OS.
  7. Find the devices that you will use for “caching” and “backing”.  (These are the terms used by the developer) You can do this by running the command
    lsblk

    First set up the backing device (the empty partition on your hard drive.
    The command for this is

    make-bcache -B /dev/sdXX  (Replace XX)
    

    If it asks you the run additional commands run them as well.
    Then do the same thing for your caching partition.

    make-bcache -C /dev/sdXX (Replace XX)
  8. Check if /dev/bcache exists. If it does skip the next step.  Otherwise you do not use udev and you will need to run the following commands:
      echo /dev/sdb > /sys/fs/bcache/register
      echo /dev/sdc > /sys/fs/bcache/register
  9. Format your new bcache device:
     mkfs.ext4 /dev/bcache0
  10. Make a folder somewhere and mount your newly created bcache device. e.g.:
    mkdir /newhome 
    mount /dev/bcache0 /newhome
  11. Get the UUID of your cache. It is a folder in /sys/fs/bcache/
    Run the following command with the UUID

      echo  > /sys/block/bcache0/bcache/attach
  12. Now go to where you mounted your bcache. Check to see that you can create files. Now copy all the files in the partition you shrunk originally.
    cp -rva /home/* /newhome
  13. Restart your computer and make sure everything still works. Now we are going to edit fstab so your system uses the mirror you created and not the old copy of the data.  Create a file on your cached drive. If everything works you should see that everything has flipped and that file is in the original directory e.g. created in /newhome but then can be seen in /home
    We are going to add a line in fstab to mount the cached drive. We are also going to swap mount points with the original drive. This is the line I appended to /etc/fstab. Alter the mount point for your needs.

    /dev/bcache0		/home		ext4		defaults		0 0 
    

    The mount point (newhome) was swapped in this line:

    /dev/disk/by-id/ata-ST1000LM024_HN-M101MBB_S2RQJ9EC522009-part6		/newhome	ext4	acl,user_xattr,noatime 1 2
  14. Save and restart your computer. At this point I saw that the file I created in /newhome now resided in /home. This and all my other /home data was coming from the cached drive.

Thats all there is too it. Hopefully this can save you a few hours because it took me the better part of a day. The developer has a ton of info here:  http://evilpiepirate.org/git/linux-bcache.git/plain/Documentation/bcache.txt?h=bcache-dev

Leave a comment and let me know if this works for you.