centos上配置bcache并测试

时间:2024-04-14 13:08:56

在这篇https://blog.****.net/Agoni_xiao/article/details/82973202讲过如何编译内核生成bcache模块,现在安装bcache-tools搭建测试环境。

一、安装bcache-tools

1.安装依赖libblkid-devel

yum install libblkid-devel -y

2.下载bcache-tools安装

$ git clone https://github.com/g2p/bcache-tools.git
$ cd bcache-tools
$ make && make install

验证bcache-tools安装是否正确:在命令行敲bcache按tab键看是否能够联想或者直接输入 make-bcache是否有命令提示

二、搭建bcache

bcache的原理是用高速的SSD盘为后端HHD盘做缓存,当用户有写IO时,先写到前端高速SSD缓存立即返回,当缓存中数据达到水位线时,开始刷到后端HHD盘;当用户有读IO时,先去缓存读,读到便立即返回,未读到便去后端HHD机械盘找,这里会涉及到读命中的概念。

centos上配置bcache并测试

1.先创建后端低速磁盘

make-bcache -B /dev/sda1

centos上配置bcache并测试

创建后,后端磁盘sda的分区sda1下面出现bcache0

2.创建前段高速盘bcache

make-bcache -C /dev/sdb1

centos上配置bcache并测试

3.查看新建的bcache UUID并初始化

cd /sys/fs/bcache

[[email protected]r120 ~]# cd /sys/fs/bcache/
[[email protected] bcache]# ls
8a5c3a4d-9444-4767-84f1-77d71441776d  register  register_quiet

初始化bcache0

centos上配置bcache并测试

4.查看新建的bcache uuid并将前后端关联

bcache-super-show /dev/sdb1(查看前端)

[[email protected] bcache]# bcache-super-show /dev/sdb1
sb.magic		ok
sb.first_sector		8 [match]
sb.csum			93E3593CC63081FB [match]
sb.version		3 [cache device]

dev.label		(empty)
dev.uuid		1a1ee81b-d033-415a-8db0-1232508eb8d0
dev.sectors_per_block	1
dev.sectors_per_bucket	1024
dev.cache.first_sector	1024
dev.cache.cache_sectors	104856576
dev.cache.total_sectors	104857600
dev.cache.ordered	yes
dev.cache.discard	no
dev.cache.pos		0
dev.cache.replacement	0 [lru]

cset.uuid		8a5c3a4d-9444-4767-84f1-77d71441776d
[[email protected] bcache]# 

关联前后端

echo "5cb5581e-1372-45dc-8068-4f1bfb9abb5f" > /sys/block/bcache0/bcache/attach 

[[email protected] bcache]# echo "8a5c3a4d-9444-4767-84f1-77d71441776d" > /sys/block/bcache0/bcache/attach 
[[email protected] bcache]# lsblk 
NAME            MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sdb               8:16   0 447.1G  0 disk 
└─sdb1            8:17   0    50G  0 part 
  └─bcache0     252:0    0   500G  0 disk 
sda               8:0    0   9.1T  0 disk 
└─sda1            8:1    0   500G  0 part 
  └─bcache0     252:0    0   500G  0 disk 
nvme0n1         259:0    0 372.6G  0 disk 
├─nvme0n1p1     259:1    0     1G  0 part /boot
└─nvme0n1p2     259:2    0 371.6G  0 part 
  ├─centos-swap 253:1    0  31.4G  0 lvm  [SWAP]
  ├─centos-home 253:2    0 290.2G  0 lvm  /home
  └─centos-root 253:0    0    50G  0 lvm  /

此时前后端盘都有bcache0说明bcache可正常使用

5.bcache参数的设置

bcache的参数配置在/sys/block/bcache0/bcache

[[email protected] bcache]# ls
attach             cache_mode  //写策略  
dev                partial_stripes_expensive  
sequential_cutoff  stats_five_minute  stop //停用bcache时使用
writeback_delay     writeback_rate        
writeback_rate_p_term_inverse
bypass_torture_test    clear_stats  
dirty_data  readahead  state              
stats_hour             stripe_size  
writeback_metadata  
writeback_rate_debug   
writeback_rate_update_seconds
cache         detach       label       
running       stats_day    stats_total        
verify      
writeback_percent        //水位线 最高40%
writeback_rate_d_term  
writeback_running

参数修改使用echo,如

[[email protected] bcache]# cat cache_mode 
[writethrough] writeback writearound none
[[email protected] bcache]# echo writeback > cache_mode  //切换写策略为writeback
[[email protected] bcache]# cat cache_mode 
writethrough [writeback] writearound none

6.清理bcache

解除前段盘与后端盘的关系:
echo “uuid”> /sys/block/bcache0/bcache/detach


删除后端盘bcache:
echo 1 > /sys/block/bcache0/bcache/stop


删除前端盘bcache:
cd /sys/fs/bcache/ 查看bcache uuid
echo 1 >/sys/fs/bcache/uuid/unregister
 

注意:重启设备后lsblk如果后端磁盘还存在bcache0的话,执行echo 1 > /sys/block/bcache0/bcache/stop

清理初始化磁盘:

wipefs -a /dev/sda

mkfs.xfs -f /dev/sda

重新分区:fdisk /dev/sda