6

I've tried echoing to detach and stop. The device will remove itself, but will show up again on reboot. One time on reboot, it restored the mdadm raid I had as a backing device!

The other time I disabled the ramdrive that it was paired with, did a detach. And /dev/bcache0 came back up again after reboot.

There is no unregister under

/sys/fs/block/bcache

I've also looked in /sys/fs/bcache... /sys/block/md0/md0p1/bcache

for this non existent unregister.

only register and register-quiet

I even uninstalled bcache-tools, and /dev/bcache0 still shows up after reboot and is caching /dev/md0!

thistleknot
  • 222
  • 1
  • 3
  • 9

1 Answers1

8

If you want to permanently destroy the bcache volume, you need to wipe the bcache superblock from the underlying device. This operation is not exposed through the sysfs interface. So:

  1. Stop the bcache device as usual with echo 1 > /sys/block/<device>/bcache/stop. On newer kernels this may fail with "Permission denied". In such a case, you would need to stop the device by its UUID as explained here:

    ls -la /sys/block/<device>/bcache/set 
    # lrwxrwxrwx 1 root root 0 Jun 19 18:42 /sys/block/<device>/bcache/set -> ../../../../../../../../fs/bcache/<UUID>
    # Note: UUID is something like "89f4c92a-7fae-4d04-ab3c-7c1dd41fa1a5"
    
    echo 1 > /sys/fs/bcache/<UUID>/stop
    
  2. Wipe the superblock with head -c 1M /dev/zero > /dev/<device>. (If you have a sufficiently new version of util-linux, you can use wipefs instead, which is more precise in wiping the bcache signature: wipefs -a /dev/<device>.) Obviously, you need to be careful to select the right device because this is a destructive operation that will wipe the header of the device. Take note that you will no longer have access to any data in the bcache volume!

muru
  • 69,900
  • 13
  • 192
  • 292
Vincent Yu
  • 496
  • 4
  • 11
  • I tried to bcache a ramdisk to an lvm. The lvm consisted of md0 and an ssd. The md0 is what keeps showing as having bcache0 assigned when I run lsblk. I believe I've tried wipefs -a and stopped the mdadm as well as the ramdrive. Where is it reading the superblock from if I removed these devices on reboot? Shouldn't uninstalling prevent lsblk from showing bcache0? – thistleknot Aug 24 '15 at 22:15
  • Older versions of `wipefs` do not recognize the bcache signature, so try zeroing the first megabyte of `md0` instead (again, be sure that there is *nothing* in `md0` that you want to preserve). `mdadm` and bcache use udev rules to start automatically, so stopping them will not prevent them from coming up automatically again. On Ubuntu 14.04, uninstalling bcache-tools will remove the udev rules, but other distributions may behave differently. In any case, the kernel will happily manage bcache devices without bcache-tools, since that package contains only the userspace tools. – Vincent Yu Aug 25 '15 at 02:52
  • thank you, worked. Had to go through a new install anyways, but noticed after installing bcache again on the new install, /dev/bcache0 showed up. Ran the commands on both the backing and caching (probably just needed caching) and issue went away. – thistleknot Aug 30 '15 at 17:30
  • @VincentYu This works after reboot. But how do you get rid of /sys/block/bcache0 and /dev/bcache0 without reboot? Also you can't `modprobe -r bcache` until reboot. – Peter Nov 23 '16 at 22:46
  • @Peter I'm not sure. According to section H of the [kernel documentation](https://github.com/torvalds/linux/blob/master/Documentation/bcache.txt) and from what I remember, `echo 1 > stop` also unregisters the device and removes the references in `/sys/` and `/dev/`, but I guess that didn't work for you. I suggest trying the other methods in that section. – Vincent Yu Nov 24 '16 at 03:47
  • 5
    **WARNING**: On linux-4.17.3 `echo 1 > /sys/block/bcache0/bcache/stop` resulted in the backing device being removed, but leaving a dangling reference. After subsequently removing the cache devices my `/dev/bcache0` and `/sys/block/bcache0/` directory remain. I think you are supposed to first *detach* it now via `echo 1 > /sys/block/bcache0/bcache/detach`. Either way, this appears to be a bug in this kernel as `/sys/block/bcache0` remains, but it has no `bcache` subdirectory and no obvious way to remove it. – Daniel Santos Jun 28 '18 at 00:19
  • ^Is this still in kernel v5? Seeing this issue now. – sfxworks Jun 11 '21 at 16:46
  • This "dangling reference" situation also occurs when the bcache device is still in use, e.g. if it is mounted as a filesystem. In this case, stop using it (unmount the filesystem in the example) and the bcache device and the folder in sys/block disappear. – Joachim Wagner Sep 30 '22 at 08:53