How do I remove the cache device from bcache?

I used bcache only in a writethrough configuration, and IIRC even then bcache doesn't like at all if the cache device vanishes while the machine is running. Expect the bcache device to stall completely if that happens.

I haven't tried to remove the cache device while the machine is powered down, so I can't say anything about that. I do think though that bcache is still pretty touchy, so I'd recommend that you try that with a VM or a physical test machine first.


To safely remove the cache device, you can detach the cache set from the bcache device:

echo <cache-set-uuid> > /sys/block/bcache0/bcache/detach

To determine the necessary cache set UUID, look in /sys/fs/bcache/:

host ~ # ll /sys/fs/bcache/
total 0
drwxr-xr-x 7 root root    0 Feb 19 00:11 eb99feda-fac7-43dc-b89d-18765e9febb6
--w------- 1 root root 4096 Feb 19 00:11 register
--w------- 1 root root 4096 Feb  7 07:17 register_quiet

So for example in this case, run:

echo eb99feda-fac7-43dc-b89d-18765e9febb6 > /sys/block/bcache0/bcache/detach

The state file should say no cache after that:

host ~ # cat /sys/block/bcache0/bcache/state
no cache

Suppose you've set up successfully a bcache, you are already working on it, put there a lot of important data too big to simply backup and start over, when you realized, that you'd better replace the caching device. This is how you can do it. This solution is based on a VM trials.

Lets say we are talking about the device /dev/bcache0, the new cache device is /dev/sdf1 and the backing device is /dev/md1. All commands done by root.

  1. Make sure that nothing is using that bcache0 device.
  2. Do in any order
    1. Remove the cache device just as Martin von Wittich wrote, by echoing setUUID into /sys/block/bcache0/bcache/detach. If you want to repartition the caching device, you need to reboot, because bcache still locks the partitions unless you unregister it.
    2. format- new cache device with make-bcache -C /dev/sdf1 and take a note of the setUUID of that device.
  3. Register our backing device with the new cache set:

    echo [setUUID of new cache device] >/sys/block/bcache0/bcache/attach

No need to reboot.


One-liner:

echo $(ls -d /sys/fs/bcache/*-*-* | cut -f5 -d/) > /sys/block/bcache0/bcache/detach

Then wait until cached clears:

while true; do if [[ $(cat /sys/block/bcache0/bcache/state) != "dirty" ]]; 
then echo "DONE"; break; 
else cat /sys/block/bcache0/bcache/dirty_data; sleep 30; fi; done