1.編譯安裝
1
| # git clone http:///git/bcache-tools.git
|
安裝前需要兩個依賴包pkg-config和libblkid-dev
1
| # apt-get install pkg-config libblkid-dev
|
然后編譯
1 2 3 4 5 6 7 8 9 10 11 12 13
| # make 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 # 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 -T -m0755 initramfs/hook /usr/share/initramfs-tools/hooks/bcache if [ -d /lib/dracut/modules.d ]; \ then install -D -m0755 dracut/module-setup.sh /lib/dracut/modules.d/90bcache/module-setup.sh; \ fi install -m0644 -- *.8 /usr/share/man/man8/
|
2.使用方式
2.1 創(chuàng)建bcache設(shè)備
1
| 命令:make-bcache -C <cache-device> -B <backing-device>
|
以vde作為緩存盤,vdb和vdc作為后端設(shè)備創(chuàng)建bcache設(shè)備,有幾個后端設(shè)備就會生成幾個bcache設(shè)備。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| # make-bcache -C /dev/vde -B /dev/vdb /dev/vdc UUID: 8941a5d1-074e-4cc6-a6dd-56cb1f65aed3 Set UUID: 7681dbb3-6558-4e60-b062-5fbb648f6665 version: 0 nbuckets: 20480 block_size: 1 bucket_size: 1024 nr_in_set: 1 nr_this_dev: 0 first_bucket: 1 UUID: 9e28d09b-942d-487e-be22-9132063da572 Set UUID: 7681dbb3-6558-4e60-b062-5fbb648f6665 version: 1 block_size: 1 data_offset: 16 UUID: 1bacf83e-2754-4d6a-a964-09e53e6e679f Set UUID: 7681dbb3-6558-4e60-b062-5fbb648f6665 version: 1 block_size: 1 data_offset: 16 root@nbsagent3:~# ls /dev/b bcache0 bcache1 block/ btrfs-control bus/ root@nbsagent3:~# ls /dev/bcache* -la brw-rw---T 1 root disk 250, 0 5月 22 17:23 /dev/bcache0 brw-rw---T 1 root disk 250, 1 5月 22 17:23 /dev/bcache1
|
然后可以使用lsblk查看這些設(shè)備的對應(yīng)關(guān)系
1 2 3 4 5 6 7 8 9 10 11 12 13
| # lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT vda 254:0 0 20G 0 disk └─vda1 254:1 0 20G 0 part / vdb 254:16 0 50G 0 disk └─bcache1 250:1 0 50G 0 disk vdc 254:32 0 50G 0 disk └─bcache0 250:0 0 50G 0 disk vdd 254:48 0 50G 0 disk └─bcache2 250:2 0 50G 0 disk vde 254:64 0 10G 0 disk ├─bcache0 250:0 0 50G 0 disk └─bcache1 250:1 0 50G 0 disk
|
2.2 添加一塊后端設(shè)備(backing device)
1 2 3 4 5 6 7
| 命令:make-bcache -B <backing-device> # make-bcache -B /dev/vdh UUID: bd1bc116-6f39-401e-a328-3bc0af4ac8d1 Set UUID: cd86812e-d24c-46da-b949-683969b59575 version: 1 block_size: 1 data_offset: 16
|
接著看到對應(yīng)的設(shè)備是/dev/bcache3
查看cache set uuid
1 2 3 4 5 6 7
| # ls -la /sys/fs/bcache/ total 0 drwxr-xr-x 3 root root 0 May 22 17:23 . drwxr-xr-x 6 root root 0 May 22 17:21 .. drwxr-xr-x 7 root root 0 May 22 17:26 7681dbb3-6558-4e60-b062-5fbb648f6665 --w------- 1 root root 4096 May 22 17:57 register --w------- 1 root root 4096 May 22 20:44 register_quiet
|
“attach”后端設(shè)備
1
| 命令:echo <cache set uuid> > /sys/block/bcache<N>/bcache/attach
|
attach之后,緩存設(shè)備就能夠?qū)π录拥暮蠖嗽O(shè)備緩存數(shù)據(jù)了。
1
| # echo 7681dbb3-6558-4e60-b062-5fbb648f6665 > /sys/block/bcache3/bcache/attach
|
添加后使用lsblk查看
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| # lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT vda 254:0 0 20G 0 disk └─vda1 254:1 0 20G 0 part / vdb 254:16 0 50G 0 disk └─bcache1 250:1 0 50G 0 disk vdc 254:32 0 50G 0 disk └─bcache0 250:0 0 50G 0 disk vdd 254:48 0 50G 0 disk └─bcache2 250:2 0 50G 0 disk vde 254:64 0 10G 0 disk ├─bcache0 250:0 0 50G 0 disk ├─bcache2 250:2 0 50G 0 disk └─bcache3 250:3 0 30G 0 disk vdf 254:80 0 10G 0 disk vdg 254:96 0 20G 0 disk vdh 254:112 0 30G 0 disk └─bcache3 250:3 0 30G 0 disk
|
2.3 刪除一塊后端設(shè)備
1)detach backing device
1
| 命令:echo <cache set uuid> > /sys/block/bcache<N>/bcache/detach
|
detach后端設(shè)備后,對應(yīng)的bcache設(shè)備還是存在的,只不過這個bcache設(shè)備是無緩存的,查看其狀態(tài)可以看到是no cache
比如刪除bcache3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| # echo 7681dbb3-6558-4e60-b062-5fbb648f6665 > /sys/block/bcache3/bcache/detach # cat /sys/block/bcache3/bcache/state no cache # lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT vda 254:0 0 20G 0 disk └─vda1 254:1 0 20G 0 part / vdb 254:16 0 50G 0 disk └─bcache1 250:1 0 50G 0 disk vdc 254:32 0 50G 0 disk └─bcache0 250:0 0 50G 0 disk vdd 254:48 0 50G 0 disk └─bcache2 250:2 0 50G 0 disk vde 254:64 0 10G 0 disk ├─bcache0 250:0 0 50G 0 disk └─bcache2 250:2 0 50G 0 disk vdf 254:80 0 10G 0 disk vdg 254:96 0 20G 0 disk vdh 254:112 0 30G 0 disk └─bcache3 250:3 0 30G 0 disk
|
2)stop backing device
1
| 命令:echo 1 > /sys/block/bcache<N>/bcache/stop
|
detach后端設(shè)備后,對應(yīng)的bcache設(shè)備還存在,如果要刪除,還需要stop該設(shè)備
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| # echo 1 > /sys/block/bcache3/bcache/stop # ls /dev/bcache* -la brw-rw---T 1 root disk 250, 0 May 22 17:51 /dev/bcache0 brw-rw---T 1 root disk 250, 1 May 22 17:54 /dev/bcache1 brw-rw---T 1 root disk 250, 2 May 22 20:42 /dev/bcache2 # lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT vda 254:0 0 20G 0 disk └─vda1 254:1 0 20G 0 part / vdb 254:16 0 50G 0 disk └─bcache1 250:1 0 50G 0 disk vdc 254:32 0 50G 0 disk └─bcache0 250:0 0 50G 0 disk vdd 254:48 0 50G 0 disk └─bcache2 250:2 0 50G 0 disk vde 254:64 0 10G 0 disk ├─bcache0 250:0 0 50G 0 disk ├─bcache1 250:1 0 50G 0 disk └─bcache2 250:2 0 50G 0 disk vdf 254:80 0 10G 0 disk vdg 254:96 0 20G 0 disk vdh 254:112 0 30G 0 disk
|
2.4 新增一塊緩存設(shè)備(caching device)
1)創(chuàng)建cache設(shè)備
1
| 命令:make-bcache -C <cache device>
|
有可能對應(yīng)的設(shè)備已經(jīng)有一些元數(shù)據(jù),需要使用wipefs清理掉
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| # make-bcache -C /dev/vdf Device /dev/vdf already has a non-bcache superblock, remove it using wipefs and wipefs -a # wipefs -a /dev/vdf 4 bytes were erased at offset 0x27fff0000 (linux_raid_member) they were: fc 4e 2b a9 # make-bcache -C /dev/vdf UUID: 1f8bc71f-0106-4da5-b781-5de7b1517706 Set UUID: 7bfb0d17-b6d0-4fe9-942b-a1c75a0893ab version: 0 nbuckets: 20480 block_size: 1 bucket_size: 1024 nr_in_set: 1 nr_this_dev: 0 first_bucket: 1 # lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT vda 254:0 0 20G 0 disk └─vda1 254:1 0 20G 0 part / vdb 254:16 0 50G 0 disk └─bcache1 250:1 0 50G 0 disk vdc 254:32 0 50G 0 disk └─bcache0 250:0 0 50G 0 disk vdd 254:48 0 50G 0 disk └─bcache2 250:2 0 50G 0 disk vde 254:64 0 10G 0 disk ├─bcache0 250:0 0 50G 0 disk ├─bcache1 250:1 0 50G 0 disk └─bcache2 250:2 0 50G 0 disk vdf 254:80 0 10G 0 disk vdg 254:96 0 20G 0 disk vdh 254:112 0 30G 0 disk └─bcache3 250:3 0 30G 0 disk nbsram_a 251:0 0 16M 0 disk nbsram_b 251:16 0 16M 0 disk nbsram_c 251:32 0 16M 0 disk # ls /sys/fs/bcache/ -la total 0 drwxr-xr-x 4 root root 0 May 22 17:23 . drwxr-xr-x 6 root root 0 May 22 17:21 .. drwxr-xr-x 7 root root 0 May 22 17:26 7681dbb3-6558-4e60-b062-5fbb648f6665 drwxr-xr-x 7 root root 0 May 22 21:05 7bfb0d17-b6d0-4fe9-942b-a1c75a0893ab --w------- 1 root root 4096 May 22 17:57 register --w------- 1 root root 4096 May 22 21:05 register_quiet
|
2)與bcache設(shè)備關(guān)聯(lián)
1
| 命令:echo <cache set uuid> > /sys/block/bcache<N>/bcache/attach
|
緩存設(shè)備需要與bcache設(shè)備關(guān)聯(lián)后,才能作為對應(yīng)bcache設(shè)備緩存。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| # echo 7bfb0d17-b6d0-4fe9-942b-a1c75a0893ab > /sys/block/bcache3/bcache/attach # lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT vda 254:0 0 20G 0 disk └─vda1 254:1 0 20G 0 part / vdb 254:16 0 50G 0 disk └─bcache1 250:1 0 50G 0 disk vdc 254:32 0 50G 0 disk └─bcache0 250:0 0 50G 0 disk vdd 254:48 0 50G 0 disk └─bcache2 250:2 0 50G 0 disk vde 254:64 0 10G 0 disk ├─bcache0 250:0 0 50G 0 disk ├─bcache1 250:1 0 50G 0 disk └─bcache2 250:2 0 50G 0 disk vdf 254:80 0 10G 0 disk └─bcache3 250:3 0 30G 0 disk vdg 254:96 0 20G 0 disk vdh 254:112 0 30G 0 disk └─bcache3 250:3 0 30G 0 disk
|
2.5 刪除cache設(shè)備
首先確保沒有backing device在使用它,上述的“刪除一塊后端設(shè)備”有說明如何設(shè)置取消后端設(shè)備對緩存設(shè)備的使用。
然后可以使用lsblk來查看是否有盤在引用它。
在在/sys/fs/bcache目錄下還有對應(yīng)的cache set uuid,unregister該set uuid后這個cache設(shè)備就被視為刪除了。
1
| 命令:echo 1 > /sys/fs/bcache/<cache set uuid>/unregister
|
比如vde已經(jīng)不作為任何設(shè)備的緩存盤了,在/sys/fs/bcache目錄下有對應(yīng)的cache set uuid
1 2 3 4 5 6 7 8
| # ls /sys/fs/bcache/ -la total 0 drwxr-xr-x 4 root root 0 5月 26 09:07 . drwxr-xr-x 6 root root 0 5月 26 09:06 .. drwxr-xr-x 7 root root 0 5月 26 09:07 7681dbb3-6558-4e60-b062-5fbb648f6665 drwxr-xr-x 7 root root 0 5月 26 09:07 7bfb0d17-b6d0-4fe9-942b-a1c75a0893ab --w------- 1 root root 4096 5月 26 09:07 register --w------- 1 root root 4096 5月 26 09:06 register_quiet
|
然后unregister掉該cache設(shè)備
1
| #echo 1 > /sys/fs/bcache/[SSD bcache UUID]/unregister
|
然后再看/sys/fs/bcache目錄下已經(jīng)沒有這個cache設(shè)備的uuid了
1 2 3 4 5 6 7
| # ls /sys/fs/bcache/ -la total 0 drwxr-xr-x 3 root root 0 5月 26 09:07 . drwxr-xr-x 6 root root 0 5月 26 09:06 .. drwxr-xr-x 7 root root 0 5月 26 09:07 7bfb0d17-b6d0-4fe9-942b-a1c75a0893ab --w------- 1 root root 4096 5月 26 09:07 register --w------- 1 root root 4096 5月 26 09:06 register_quiet
|
2.6 參數(shù)調(diào)節(jié)
1)設(shè)置緩存模式
1
| 命令:echo <cache mode> > /sys/block/bcache<N>/bcache/cache_mode
|
可以為不同的bcache設(shè)備設(shè)置不同的緩存策略
并且設(shè)置了緩存策略后,機器重啟后仍然生效。
另外,機器重啟后,bcache設(shè)備也能自動重建出來。
bcache還有其他可以調(diào)節(jié)的參數(shù),這里不作說明,具體參考官方說明。