Bcache is a cache for Linux. It can be used when you have a normal HDD and a faster SSD in your computer to let the SSD or part of the SSD act as a cache for the HDD to improve overall system performance. There is plenty of information on how to resize the backing device (the partition on the HDD) but I couldn’t find any information on how to resize the caching device without losing the data on the backing device. However a couple of weeks ago I was able to achieve this and I now want to share what I still remember of it.
I write the following from memory and I am not entirely sure that this is exactly what I did. Follow these instructions at your own risk and make a backup of your data (you should have a backup of your data anyway). If you find my explanation to be vague, refer to the Arch Linux wiki or ask a question in the comments.
1. Detach the backing device from your caching device
# echo 1 > /sys/block/sdX/sdX[Y]/bcache/detach
Doing this will make Bcache ignore the caching device and forward every hard disk access to the backing device and the old caching device is no longer needed.
2. Resize the caching device
Now we can actually resize the caching device. The filesystem on the caching device itself can’t be resized so we will just resize the underlying partition and create a new caching device on the partition. I use LVM so what I did was something like the following:
# lvresize -L -20G VolumeGroup/cache
If you don’t use LVM it could be a little bit more difficult to resize the partition but there are plenty of partitioning tools out there so I will assume that you can find out how to do it.
3. Creating a new caching device
The last step destroyed the bcache file system on the partition so we have to create a new one.
# make-bcache -C /dev/VolumeGroup/cache
The above command will create a new caching device on /dev/VolumeGroup/cache.
4. Reattach the backing device to the caching device
Now we can put it all together again. To reattach the backing device we first have to find out the cset.uuid of the caching device. Assuming that the caching device is located at /dev/VolumeGroup/cache we can do this with
bcache-super-show /dev/VolumeGroup/cache | grep cset.uuid
The backing device then can be reattached with
# echo cset.uuid > /sys/block/bcache0/bcache/attach
assuming that your caching device is bcache0.
Now the cache should be working properly. You can check this by executing
# cat /sys/block/bcache0/bcache/state
If the above has the output „clean“ then you have successfully resized your caching device.