How to operate becache

Recently I just try to add bcache with zfs and ceph. So I just wirte down it.

Install bcache-tools

Since bcache implement in Linux kernel. So it will default enabled. We just need to install bcache-tools to operate bcache.

apt install -y bcache-tools

Configure bcache

# make a cache disk for bcache .It must be a ssd disk.
make-bcache -C /dev/nvme1n1 

# make a disk as backend disk.It must be a slow sata/sas disk.
make-bcache -B /dev/sda
#if you have a lot of disks like me.your can add backend disk as this.
make-bcache -B /dev/sd{a..f}

###Check bcache status

lsblk

Add bcache manually

#show `cset.uuid
root@xa-autotest-hci01:~# bcache-super-show /dev/sda
sb.magic		ok
sb.first_sector		8 [match]
sb.csum			ED8FD98E20A2E91E [match]
sb.version		1 [backing device]

dev.label		(empty)
dev.uuid		e7b350fe-3496-47e2-a276-5faa5637f8a5
dev.sectors_per_block	1
dev.sectors_per_bucket	1024
dev.data.first_sector	16
dev.data.cache_mode	1 [writeback]
dev.data.cache_state	1 [clean]

cset.uuid		d3fcd781-a10a-453a-96dd-6eba13abfc5b

####Add cache disk

echo "d3fcd781-a10a-453a-96dd-6eba13abfc5b" > /sys/block/bcache0/bcache/attach

if you have a lot of disk, you can add it with for loop.

for i in range{0..5}; do `echo "d3fcd781-a10a-453a-96dd-6eba13abfc5b" > /sys/block/bcache$(expr $i)/bcache/attach`; done

Delete cache disk

if you want to delete bcache disk

echo "d3fcd781-a10a-453a-96dd-6eba13abfc5b" > /sys/block/bcache0/bcache/detach
# delete a lot disk 
for i in range{0..5}; do `echo "d3fcd781-a10a-453a-96dd-6eba13abfc5b" > /sys/block/bcache$(expr $i)/bcache/detach`; done

unrigister cache disk

umount /dev/bcach0
echo 1 > /sys/block/bcache0/bcache/stop

If you onle have one nvme disk and you can add them all in one make-bcache -C /dev/nvme1n1 -B /dev/sd{a..f} –writeback


## Change bcache write policy

```bash
make-bcache -B/dev/

Manually resigter udev disk

echo /dev/sdb > /sys/fs/bcache/register

Change bcache write mode

When you run command like make-bcache -C /dev/nvme1n1 -B /dev/sda it will be set write through mode. If you have UPS and you can change it to writeback mode.

echo writeback > /sys/block/bcache0/bcache/cache_mode

Check bcache status

cat /sys/block/bcache0/bcache/state
no cache: this means you have not attached a caching device to your backing bcache device;
clean: this means everything is ok. The cache is clean;
dirty: this means everything is setup fine and that you have enabled writeback and that the cache is dirty;
inconsistent: you are in trouble because the backing device is not in sync with the caching device;

bcache-status
lsblk

bcache optimized

#modify cache policy 
cat /sys/block/bcache0/bcache/cache_mode
echo writeback > /sys/block/bcache0/bcache/cache_mode
#Disable ip track
echo 0 > /sys/fs/bcache/$CacheSetUUID/congested_read_threshold_us
echo 0 > /sys/fs/bcache/$CacheSetUUID/congested_write_threshold_us

disable bcache

#check `CACHE_SET_UUID`
ls /sys/fs/bcache
#Unregister ssd cache disk
echo 1 >/sys/fs/bcache/06e33354-7c21-4c52-990d-1a653617ab20/unregister

#disable backend disk
echo 1 > /sys/block/sdb/bcache/stop
#write back cache to backend disk
echo 0 > /sys/block/bcache0/bcache/writeback_percent

##Reference