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".
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