Configuring Bcache
As an example, the following steps describe how to configure an SDB drive as a bcache drive.
Creating a Bcache Drive for the Data Drive
- Delete the drive data, create a bcache drive, and format the bcache drive to the ext4 file system format.
wipefs -a /dev/sdb --force make-bcache -B /dev/sdb --wipe-bcache mkfs.ext4 /dev/[bcache]
- Modify the bcache drive parameters.
- Obtain the actual parameter of max_sectors_kb (maximum request size allowed by the device).
cat /sys/block/sdb/queue/max_sectors_kb
The command output is the maximum number of sectors of the data drive. The following is an example:
1024
- Obtain the actual parameter of read_ahead_kb (size of the read-ahead data).
cat /sys/block/sdb/queue/read_ahead_kb
The command output is the size of the read-ahead data. The following is an example:128
- Set max_sectors_kb and read_ahead_kb based on the obtained parameters.
echo Maximum number of sectors on a data drive > /sys/block/[bcache]/queue/max_sectors_kb
echo Size of data to be read ahead > /sys/block/[bcache]/queue/read_ahead_kb
For Maximum number of sectors on a data drive, size of data to be read ahead, and [bcache], note the following:
- Maximum number of sectors on a data drive and size of data to be read ahead are subject to the actual values. Different data drives may have different parameters.
- [bcache] is subject to the content of the /sys/block/ directory. All bcache devices in the directory must be processed in the same way.
- Obtain the actual parameter of max_sectors_kb (maximum request size allowed by the device).
Creating a Bcache Drive for the RAM Drive
- Create a RAM drive.
modprobe brd rd_nr=12 rd_size=$((1048576))
In the command, 12 indicates the number of cache drives, and 1048576 indicates the cache drive size (1 GB), which is appropriate for general big data scenarios.
- Format the cache drive into a bcache drive.
make-bcache --wipe-bcache -b 262144 -C /dev/ram0
In the command, 262144 indicates that the block size of the cache drive is 256 KB.
- Mount the cache drive to the bcache drive.
echo $uuid > /sys/block/[bcache]/bcache/attach
In the command, the value of $uuid is the value of uuid corresponding to cset.uuid in the command output. Show the value of $uuid:bcache-super-show /dev/ram0
- Modify the bcache policy.
echo writearound > /sys/block/[bcache]/bcache/cache_mode
echo 1 > /sys/block/[bcache]/bcache/clear_stats
echo 0 > /sys/block/[bcache]/bcache/readahead
echo 10 > /sys/block/[bcache]/bcache/writeback_percent
echo 0 > /sys/block/[bcache]/bcache/cache/congested_read_threshold_us
echo 0 > /sys/block/[bcache]/bcache/cache/congested_write_threshold_us
echo 1 > /sys/block/[bcache]/bcache/read_bypass
echo lru > /sys/block/[bcache]/bcache/cache/cache0/cache_replacement_policy
[bcache] is subject to the content of the /sys/block/ directory. All bcache devices in the directory must be processed in the same way.
After a cache drive is created for the RAM drive, the system allocates a memory space to the bcache. If services require a large memory space, increase the service memory space to prevent the services from failing due to insufficient memory. For example, Spark k-means consumes a large amount of memory. Adjust the memory resource configuration of Yarn to ensure that services will not fail due to insufficient memory.
Creating a Bcache Drive for the Cache SSD
- Ensure that there is an idle cache SSD.
For example:

- Format the cache drive into a bcache drive.
make-bcache --wipe-bcache -b 262144 -C /dev/nvme0n1p1
echo /dev/nvme0n1p1 > /sys/fs/bcache/register
In the command, 262144 indicates that the block size of the cache drive is 256 KB.
- Mount the cache drive to the bcache drive.
echo $uuid > /sys/block/[bcache]/bcache/attach
In the command, the value of $uuid is the value of uuid corresponding to cset.uuid in the command output. Show the value of $uuid:bcache-super-show /dev/nvme0n1p1
- Modify the bcache policy.
echo writeback > /sys/block/[bcache]/bcache/cache_mode echo 1 > /sys/block/[bcache]/bcache/clear_stats echo 0 > /sys/block/[bcache]/bcache/readahead echo 10 > /sys/block/[bcache]/bcache/writeback_percent echo 0 > /sys/block/[bcache]/bcache/cache/congested_read_threshold_us echo 0 > /sys/block/[bcache]/bcache/cache/congested_write_threshold_us echo 0 > /sys/block/[bcache]/bcache/cache/read_bypass echo 0 > /sys/block/[bcache]/bcache/sequential_cutoff echo lru > /sys/block/[bcache]/bcache/cache/cache0/cache_replacement_policy
- Modify the queue directory parameters of the bcache.In the following command, set the value of max_sectors_kb to a proper value based on the back-end drive configuration.
echo 1024 > /sys/block/[bcache]/queue/max_sectors_kb
[bcache] is subject to the content of the /sys/block/ directory. All bcache devices in the directory must be processed in the same way.
Mounting the Bcache Drive of the Data Drive
After creating the bcache drive, in /etc/fstab, delete the configuration of mounting the data drive upon system boot. Otherwise, the data drive is formatted into a non-bcache drive after the server is rebooted.
mount /dev/[bcache]/ data/[data]