Bcache
Bcache is a Linux kernel block layer cache. It allows one or more fast storage devices (like SSDs) to be used as a cache for slower storage devices (like HDDs). This can significantly improve the performance of the slower storage devices by caching frequently accessed data on the faster devices.
Related, see my LVM guide here to learn about lvmcache, which is a similar caching mechanism that works with LVM volumes.
Documentation
Installation
apt install bcache-toolsUsage
make-bcache -B /dev/sdd -C /dev/sdcThis command creates a bcache device by using /dev/sdd as the backing device (the slower storage) and /dev/sdc as the caching device (the faster storage). After running this command, you can use the new bcache device for your storage needs, and it will automatically cache data from the backing device to improve performance.
Encryption
cryptsetup luksFormat /dev/bcache0
cryptsetup open /dev/bcache0 bcache0_cryptThis example shows how to encrypt the bcache device using LUKS. The first command formats the bcache device with LUKS encryption, and the second command opens the encrypted device, making it available for use under the name bcache0_crypt. You can then use bcache0_crypt as you would any other block device, and it will be encrypted on disk.
Crypttab Configuration
/etc/crypttab
# Fields are: name, underlying device, passphrase, cryptsetup options.
# Mount /dev/bcache0 as /dev/mapper/bcache0_crypt and prompt for the passphrase at boot time.
bcache0_crypt /dev/bcache0 none luksThis line in the /etc/crypttab file configures the system to automatically open the encrypted bcache device at boot time, prompting the user for the passphrase.
Filesystem Creation
mkfs.btrfs /dev/mapper/bcache0_cryptAfter opening the encrypted bcache device, you can create a filesystem on it. In this example, we are creating a Btrfs filesystem on the encrypted bcache device. You can replace btrfs with any other supported filesystem type if you prefer. Once the filesystem is created, you can mount it and start using it for your data storage needs.
Performance considerations
echo writeback > /sys/block/bcache0/bcache/cache_modeThis command sets the cache mode of the bcache device to "writeback". In writeback mode, data is written to the cache device first and then later flushed to the backing device. This can improve performance for write operations, but it also means that there is a risk of data loss if the cache device fails before the data is flushed to the backing device. It's important to choose the appropriate cache mode based on your performance needs and risk tolerance. Other cache modes include "writethrough", where data is written to both the cache and backing devices simultaneously, and "writearound", where data is written directly to the backing device without being cached.
Bcache Status
cat /sys/block/bcache0/bcache/stateThis command displays the current state of the bcache device. The output will show information about the cache device, backing device, cache mode, and other relevant details. This can be useful for monitoring the performance and health of your bcache setup, as well as for troubleshooting any issues that may arise.
cat /sys/block/bcache0/bcache/cache_modeThis command displays the current cache mode of the bcache device. The output will indicate whether the cache mode is set to "writeback", "writethrough", or "writearound".
bcache-super-show
➜ ~ sudo bcache-super-show /dev/sdc
sb.magic ok
sb.first_sector 8 [match]
sb.csum E7736DAC856031A1 [match]
sb.version 1 [backing device]
dev.label (empty)
dev.uuid e6a6c2ce-036c-4f55-afef-ac236cb27da1
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 f6678635-bbb3-45ac-b114-b23396bcf5f4
➜ ~ sudo bcache-super-show /dev/sdd
sb.magic ok
sb.first_sector 8 [match]
sb.csum CF5E65A00231C087 [match]
sb.version 3 [cache device]
dev.label (empty)
dev.uuid 2db071f6-a3f7-4a0e-bf18-48b5a1208b7f
dev.sectors_per_block 1
dev.sectors_per_bucket 1024
dev.cache.first_sector 1024
dev.cache.cache_sectors 234439680
dev.cache.total_sectors 234440704
dev.cache.ordered yes
dev.cache.discard no
dev.cache.pos 0
dev.cache.replacement 0 [lru]
cset.uuid f6678635-bbb3-45ac-b114-b23396bcf5f4The bcache-super-show command displays detailed information about the bcache superblock for both the cache device and the backing device. This includes information such as the magic number, version, UUIDs, sectors per block, sectors per bucket, cache mode, cache state, and other relevant details. This can be useful for diagnosing issues with your bcache setup or for verifying that the devices are configured correctly.
bcache-status
Quickly check the status of your bcache devices with this script. It provides a summary of the cache device, backing device, cache mode, and other relevant information in a human-readable format.
python3 ./bcache-status
--- bcache ---
Device /dev/bcache0 (252:0)
UUID f6678635-bbb3-45ac-b114-b23396bcf5f4
Block Size 0.50KiB
Bucket Size 512.00KiB
Congested? False
Read Congestion 2.0ms
Write Congestion 20.0ms
Total Cache Size 111.79GiB
Total Cache Used 4.47GiB (3%)
Total Cache Unused 107.32GiB (96%)
Dirty Data 0B (0%)
Evictable Cache 107.32GiB (96%)
Replacement Policy [lru] fifo random
Cache Mode writethrough [writeback] writearound none
Total Hits 4033 (21%)
Total Misses 14361
Total Bypass Hits 183 (58%)
Total Bypass Misses 130
Total Bypassed 3.50GiB