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/
  •   https://yq.aliyun.com/articles/60734
  •   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

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM