bcache的使用,bcache使用


一、前提:内核中需要配置bcache模块

1.1 检查

  - 是否存在于内核中:检查/sys/fs/bcache目录是否存在,没有说明内核中没有bcache

  - 是否以内核模块方式存在:检查/lib/modules/<$version>/kernel/drivers/md/bcache目录是否存,不存在则说明没有bcache内核模块

  - 以上两步检查完如果都没有,则说明内核中没有配置编译bcache,需要自己配置编译新的内核(下载内核时需要注意版本,bcache功能是在3.10及以上版本中才有)

1.2 编译新内核

$ wget "http://vault.centos.org/7.3.1611/os/Source/SPackages/kernel-3.10.0-514.el7.src.rpm"                    
$ rpm2cpio ./kernel-3.10.0-514.el7.src.rpm | cpio -idmv    //提取rpm包内容,获取内核:linux-3.10.0-514.el7.tar.xz
$ make menuconfig        //内核配置
     Device Drivers 
         ->Multiple devices driver support (RAID and LVM)
             -><*> Block device as cache
$ make bzImage (V=1)     //编译内核
$ make modules        //编译内核模块
$ make modules_install      //拷贝内核模块的.ko文件到/lib/modules下
$ make install              //拷贝initrd和bzImage到boot目录下,并修改开机启动配置文件
$ reboot                    //重启,根据菜单选择对应内核版本进入

 


二、编译安装

//获取bcache-tools工具(以下两个网址任选其一)
$ git clone http://evilpiepirate.org/git/bcache-tools.git
$ git clone https://github.com/g2p/bcache-tools.git
//安装前需要两个依赖包pkg-config和libblkid-dev
$ yum -y install pkg-config libblkid-dev
//编译安装bcache-tools
$ make
$ make install 

 三、部署方式

3.1 创建bcache设备

$ make-bcache -C <cache-device> -B <backing device>
解释:make-bcache提供了同时初始化多个设备的功能,并自动绑定缓存设备和后端设备
参数:    -B    // 格式化backing device
         -C    // 格式化caching device
         -w    // block size(hard sector size of SSD),默认2K,也可使用 --block=4K设置。注意一般需要block size = (backing device的)扇区大小。
         -b    // bucket size,也可使用--bucket=1M设置。注意一般需要bucket size =(caching device的)erase block size大小。
结果:有几个backing device就会对应生成几个/dev/bcache设备
查看:用lsblk命令查看设备的对应关系
  
 

example: the default block and bucket sizes of 512B and 128kB are used. The block size should match the backing devices sector size which will usually be either 512 or 4k. The bucket size should match the erase block size of the caching device with the intent of reducing write amplification. For example, using a HDD with 4k sectors and an SSD with an erase block size of 2MB this command would look like

# make-bcache --block 4k --bucket 2m -C /dev/sdy

 

3.2 添加后端设备(backing device)

  - 1、创建后端设备

  $ make-bcache -B <backing-device>
  结果:生成对应的设备/dev/bcache<n>

  - 2、attach绑定后端设备

  $ ls -la /sys/fs/bcache        //查看cache set uuid
  $ echo <CSET-UUID> > /sys/block/bcache<n>/bcache/attach
  结果:attach之后,缓存设备就能够对新加的后端设备缓存数据了
  查看:用lsblk命令查看对应关系
  
 

 

3.3 删除后端设备

  - 1、detach解绑backing device设备

  $ ls -la /sys/fs/bcache/        //查看cache set uuid
  $ echo <CSET-UUID> > /sys/block/bcache<n>/bcache/detach
  结果:detach后端设备后,对应的bcache设备还是存在的,只不过这个bcache设备是无缓存的,查看其状态可以看到是no cache
  查看:用lsblk查看对应关系
  

 

  - 2、删除后端设备

  $ echo 1 > /sys/block/bcache<N>/bcache/stop
  解释:detach后端设备后,对应的bcache设备还存在,如果要删除,还需要stop该设备
  查看:用lsblk查看设备对应关系
  

 

3.4 添加缓存设备(caching device)

  -1、 创建缓存设备

  $ make-bcache -C <cache device>
  结果:在/sys/fs/bcache目录下生成对应的CACHE SET UUID
  注意:有可能设备本身有残余数据,需要使用wipefs清理掉
  $ wipefs -a /dev/sda
        

 

   -2、attach,与bcache设备关联

  $ echo <CSET-UUID> > /sys/block/bcache<n>/bcache/attach
  解释:通过后端设备attach缓存设备,cache device才能开始缓存,backing   device才能被缓存

 

3.5 删除缓存设备

前提:确保没有backing device在使用它(可以通过lsblk查看)
解释: - cache设备的存在,可以通过/sys/fs/bcache目录对应的cache set uuid了解
    - unregister该uuid后这个cache设备就被视为删除了

$ echo 1 > /sys/fs/bcache/<cache set uuid>/unregister
结果:再看/sys/fs/bcache目录下就没有这个cache设备的uuid了

 

3.6 格式化bcache并挂载使用

$ mkfs.xfs /dev/bcache<n>            //格式化设备为xfs文件系统
$ mount /dev/bcache<n>  /mnt       //挂载设备到/mnt目录进行访问

 


 四、使用操作

4.1 查看运行状态

$ cat /sys/block/bcache<n>/bcache/state
  • no cache:该backing device没有attach任何caching device(这意味着所有I / O都直接传递到backing device[pass-through mode])。
  • clean:everything is ok,缓存是干净的。
  • dirty:这意味着一切都设置正常,并且您已启用回写,缓存是脏的。
  • inconsistent:您遇到问题,因为后台设备与缓存设备不同步。

4.2 查看缓存的数据量

$ cat /sys/block/bcache<n>/bcache/dirty_data

4.3 查看/设置缓存模式

// 设置缓存模式(默认writethrough)
$ echo <cache mode> > /sys/block/bcache<N>/bcache/cache_mode
// 查看缓存模式
$ cat /sys/block/bcache<N>/bcache/cache_mode
 [writethrough]    writeback        writearound    none

4.4 打印device信息

$ bcache-super-show /dev/sd<n>

4.5 配置信息

  • backing device 在 /sys/block/bcache<n>/bcache/ 目录下
  • cache device 在 /sys/fs/bcache/<CSET-UUID>/ 目录下

  对/sys中配置信息的改变是暂时的,重启会失效。如果想要在启动时设置当前的配置,需要创建一个conf配置文件在/etc/tmpfile.d/中,例如 /etc/tmpfile.d/my-bcache.conf:

w /sys/block/bcache0/bcache/sequential_cutoff - - - - 1
w /sys/block/bcache0/bcache/cache_mode - - - - writeback
(To set, in a persistent fashion, the sequential cutoff for bcache0 to 1 MB and write back)


 五、参考资料

  http://bcache.evilpiepirate.org/
  http://lwn.net/Articles/550207/
  http://blog.csdn.net/liumangxiong/article/details/18090043
  http://unix.stackexchange.com/questions/115764/how-to-remove-cache-device-from-bcache
  http://pommi.nethuis.nl/ssd-caching-using-linux-and-bcache/
  https://www.kernel.org/doc/Documentation/bcache.txt
  https://evilpiepirate.org/git/linux-bcache.git/tree/Documentation/bcache.txt#n126

 

相关内容