配置bcache模塊的實例教程

一、前提:內核中需要配置bcache模塊

1.1 檢查

  - 是否存在于內核中:檢查/sys/fs/bcache目錄是否存在,沒有說明內核中沒有bcache

  - 是否以內核模塊方式存在:檢查/lib/modules//kernel/drivers/md/bcache目錄是否存,不存在則說明沒有bcache內核模塊

  - 以上兩步檢查完如果都沒有,則說明內核中沒有配置編譯bcache,需要自己配置編譯新的內核(下載內核時需要注意版本,bcache功能是在3.10及以上版本中才有)

1.2 編譯新內核

$ wget ""                    $ 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設備

$ -bcache -C <cache-device> -B <backing device>--B     -C     -     -b    結果:有幾個backing device就會對應生成幾個/dev/
  
 <br/>

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綁定后端設備

  $  -la /sys/fs/bcache          $  <CSET-UUID> > /sys/block/bcache<n>/bcache/
  
 <br/>

?

3.3 刪除后端設備

  - 1、detach解綁backing device設備

  $  -la /sys/fs/bcache/          $  <CSET-UUID> > /sys/block/bcache<n>/bcache/

?

  - 2、刪除后端設備

  $   > /sys/block/bcache<N>/bcache/

?

3.4 添加緩存設備(caching device)

  -1、 創建緩存設備

  $ make-bcache -C <cache device>  結果:在/sys/fs/bcache目錄下生成對應的CACHE SET UUID    注意:有可能設備本身有殘余數據,需要使用wipefs清理掉    $ wipefs -a /dev/sda

   配置bcache模塊的實例教程? ?  配置bcache模塊的實例教程

?

  ?-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/bcache/ 目錄下

  • cache device 在 /sys/fs/bcache// 目錄下

  對/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)

? 版權聲明
THE END
喜歡就支持一下吧
點贊6 分享