依据此链接做的实验:http://blog.csdn.net/liangchen0322/article/details/50322635
centos7的内核3.10安装后没有/sys/fs/bcache/register,升级后4.4自动出现这个目录
下载
[root@localhost mnt]# git clone https://github.com/g2p/bcache-tools.git
Cloning into 'bcache-tools'...
remote: Counting objects: 498, done.
remote: Total 498 (delta 0), reused 0 (delta 0), pack-reused 498
Receiving objects: 100% (498/498), 106.14 KiB | 89.00 KiB/s, done.
Resolving deltas: 100% (257/257), done.
[root@localhost mnt]# ls
bcache-tools
[root@localhost mnt]# cd bcache-tools/
[root@localhost bcache-tools]# ls
69-bcache.rules bcache.h bcache-super-show.8 bcache-test.c dracut initramfs make-bcache.c probe-bcache.8 README
bcache.c bcache-register.c bcache-super-show.c COPYING initcpio make-bcache.8 Makefile probe-bcache.c
[root@localhost bcache-tools]#
----------------------------------------------------------------------------------------------
编译
[root@localhost bcache-tools]# make
cc -O2 -Wall -g `pkg-config --cflags uuid blkid` -c -o bcache.o bcache.c
cc -O2 -Wall -g `pkg-config --cflags uuid blkid` make-bcache.c bcache.o `pkg-config --libs uuid blkid` -o make-bcache
cc -O2 -Wall -g `pkg-config --cflags uuid blkid` probe-bcache.c `pkg-config --libs uuid blkid` -o probe-bcache
cc -O2 -Wall -g -std=gnu99 bcache-super-show.c bcache.o `pkg-config --libs uuid` -o bcache-super-show
cc -O2 -Wall -g -c -o bcache-register.o bcache-register.c
cc bcache-register.o -o bcache-register
[root@localhost bcache-tools]# make install
install -m0755 make-bcache bcache-super-show /usr/sbin/
install -m0755 probe-bcache bcache-register /lib/udev/
install -m0644 69-bcache.rules /lib/udev/rules.d/
install -m0644 -- *.8 /usr/share/man/man8/
install -D -m0755 initramfs/hook /usr/share/initramfs-tools/hooks/bcache
install -D -m0755 initcpio/install /usr/lib/initcpio/install/bcache
install -D -m0755 dracut/module-setup.sh /lib/dracut/modules.d/90bcache/module-setup.sh
注:make时候会报blkid.h: No such file
解决 yum install libblkid-devel
因为我的sdb有数据所以要做一下这一步
[root@localhost bcache-tools]# wipefs -a /dev/sdb /dev/sdc
/dev/sdb: 8 bytes were erased at offset 0x00000200 (gpt): 45 46 49 20 50 41 52 54
/dev/sdb: 8 bytes were erased at offset 0x4fffffe00 (gpt): 45 46 49 20 50 41 52 54
/dev/sdb: 2 bytes were erased at offset 0x000001fe (PMBR): 55 aa
/dev/sdb: calling ioclt to re-read partition table: Success
将sdb作为backing device,也就是HDD的盘符。
[root@localhost bcache-tools]# make-bcache -B /dev/sdb
UUID: 1e957d89-9d85-4011-a582-09a21a320784
Set UUID: 2959a329-b79c-4488-9006-ea3b2143477a
version: 1
block_size: 1
data_offset: 16
将sdc作为cache device,也就是SSD的盘符。后面三个参数可以不要
[root@localhost bcache-tools]# make-bcache -C /dev/sdc -w4k -b1M --writeback
UUID: cf262569-b799-4e8f-9d06-7a27813005c1
Set UUID: 5049fd85-0353-4177-b8b5-139446e14a49
version: 0
nbuckets: 20480
block_size: 8
bucket_size: 2048
nr_in_set: 1
nr_this_dev: 0
first_bucket: 1
注:
参数意义:
-B: 设置backing device
-C: 设置cache device
-w: block size (hard sector size of SSD),默认是2K,可使用--block=4K代替
-b: bucket size,可以使用--bucket=1M代替
设备注册
格式化完成后需要完成设备的注册,使内核获取设备,可能由于内核版本的问题,在实际配置中发现,会报错,提示无法向register节点写入信息,但是并不影响使用。
[root@localhost bcache]# echo /dev/sdb > /sys/fs/bcache/register
-bash: echo: write error: Invalid argument
[root@localhost bcache]# echo /dev/sdc > /sys/fs/bcache/register
-bash: echo: write error: Invalid argument
设备绑定
完成注册的backing device需要在使用之前绑定到bchache,否则功能无法启用。首先找到完成注册的backing device设备的
[root@localhost bcache]# ls /sys/fs/bcache/
5049fd85-0353-4177-b8b5-139446e14a49 register register_quiet
那么UUID= 5049fd85-0353-4177-b8b5-139446e14a49
[root@localhost bcache]# echo 5049fd85-0353-4177-b8b5-139446e14a49 > /sys/block/bcache0/bcache/attach
其中使用sdb作为backing device也就是HDD盘,sdc是SSD硬盘
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sdb 8:16 0 20G 0 disk
└─bcache0 252:0 0 20G 0 disk
sr0 11:0 1 680M 0 rom
sdc 8:32 0 20G 0 disk
sda 8:0 0 20G 0 disk
├─sda2 8:2 0 19G 0 part
│ ├─cl-swap 253:1 0 2G 0 lvm [SWAP]
│ └─cl-root 253:0 0 17G 0 lvm /
└─sda1 8:1 0 1G 0 part /boot
[root@localhost ~]#
格式化
[root@localhost ~]# mkfs.ext4 /dev/bcache0
挂载
[root@localhost ~]# mount /dev/bcache0 /mnt
[root@localhost ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
devtmpfs 1002136 0 1002136 0% /dev
tmpfs 1013960 0 1013960 0% /dev/shm
tmpfs 1013960 8744 1005216 1% /run
tmpfs 1013960 0 1013960 0% /sys/fs/cgroup
/dev/mapper/cl-root 17811456 5406640 12404816 31% /
/dev/sda1 1038336 266176 772160 26% /boot
tmpfs 202796 0 202796 0% /run/user/0
/dev/bcache0 20511304 45080 19401268 1% /mnt
完成
-------------------------------------------------------------------------
其中使用sdb作为backing device,sdc是SSD硬盘
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sdb 8:16 0 20G 0 disk
└─bcache0 252:0 0 20G 0 disk /mnt
sr0 11:0 1 680M 0 rom
sdc 8:32 0 20G 0 disk
sda 8:0 0 20G 0 disk
├─sda2 8:2 0 19G 0 part
│ ├─cl-swap 253:1 0 2G 0 lvm [SWAP]
│ └─cl-root 253:0 0 17G 0 lvm /
└─sda1 8:1 0 1G 0 part /boot
bcache device状态查看
[root@localhost ~]# bcache-super-show -f /dev/sdc
sb.magic ok
sb.first_sector 8 [match]
sb.csum C4ECE29EDDF45C24 [match]
sb.version 3 [cache device]
dev.label (empty)
dev.uuid cf262569-b799-4e8f-9d06-7a27813005c1
dev.sectors_per_block 8
dev.sectors_per_bucket 2048
dev.cache.first_sector 2048
dev.cache.cache_sectors 41940992
dev.cache.total_sectors 41943040
dev.cache.ordered yes
dev.cache.discard no
dev.cache.pos 0
dev.cache.replacement 0 [lru]
cset.uuid 5049fd85-0353-4177-b8b5-139446e14a49
[root@localhost ~]# bcache-super-show -f /dev/sdb
sb.magic ok
sb.first_sector 8 [match]
sb.csum ADE4CA9B43970BA5 [match]
sb.version 1 [backing device]
dev.label (empty)
dev.uuid 1e957d89-9d85-4011-a582-09a21a320784
dev.sectors_per_block 1
dev.sectors_per_bucket 1024
dev.data.first_sector 16
dev.data.cache_mode 0 [writethrough]
dev.data.cache_state 0 [detached]
cset.uuid 2959a329-b79c-4488-9006-ea3b2143477a
[root@localhost ~]#
----------------------------------------------------------------------
停用设备
注销设备:
echo 1 >/sys/fs/bcache/
/unregister
停用bcache:
echo 1 >/sys/block/bcache0/bcache/stop
擦除磁盘残留数据:
wipefs -a /dev/sdc
wipefs -a /dev/sdb
---------------------------------------------------------------------
工作模式
bcache有四种不同工作模式:
writethrough、writeback、writearouond、none
工作模式切换:
echo writeback > /sys/block/sdr/bcache/cache_mode