NAS 使用机械硬盘做 RAID 时对小文件的读写性能不佳, 使用一块固态硬盘做缓存可以改善此问题. Linux 自带的磁盘缓存机制大多需要向已有设备写入元数据. 但是我的 RAID 阵列中已经存了不少数据了, 写入元数据会破坏已有的数据, 所以我选择安装 OpenCAS 解决缓存问题.
安装 OpenCAS
系统要求
建议使用 DEB 或 RPM 系 Linux. OpenCAS 支持导出为 deb, rpm 等安装包安装. 比 make install 更方便.
不建议使用 Ubuntu 26.04, 截至目前 OpenCAS 不支持 Linux 7.x, 并且无法通过 gcc 15 编译.
编译
服务器使用 Ubuntu Server 24.04.
克隆仓库并切换到稳定版本:
git clone https://github.com/Open-CAS/open-cas-linuxcd open-cas-linuxgit checkout v26.03.2 # 替换为最新稳定版初始化 submodule:
git submodule update --init生成配置并编译:
sudo apt install make gcc
./configuremake -j$(nproc)配置 MOK 证书(可选)
在 UEFI 引导并且开启了安全启动的系统上必须生成并信任 MOK 证书才能正常使用 DKMS 编译的系统模块.
验证方式:
ls /sys/firmware/efi# 若成功则为 UEFI 引导sudo apt install mokutilmokutil --sb-state# 若输出 “SecureBoot enabled” 则开启安全启动若经过验证你的系统确实使用 UEFI 引导且开启了安全启动请展开并进行下面的操作
点击展开折叠内容
OpenCAS 的内核模块需要通过 DKMS 编译安装, 所以需要配置 MOK 证书.
sudo update-secureboot-policy --new-key执行后 /var/lib/shim-signed/mok/ 会生成对应的 MOK 密钥.
导入密钥:
sudo apt install mokutilsudo mokutil --import /var/lib/shim-signed/mok/MOK.der执行时需要设置密码, 随便写一个方便记忆的密码, 之后导入时要用.
完成后重启系统:
reboot你会进入一个蓝色的界面

选择 Enroll MOK

选择 Continue.
如果需要确认可以选择 View key 0 查看详细信息.

选择 Yes

输入刚才的密码.

选择 Reboot 重启设备.
导出并安装
先导出为 deb包
sudo apt install devscripts debhelper fakeroot dkms dh-dkms libelf-devmake deb完成后 /packages 目录下会出现几个 .deb 文件,使用:
sudo apt install ./packages/*.deb安装这些 deb 包并重启系统.
重启后执行
lsmod | grep cas若输出信息则安装成功.
配置 OpenCAS
获取磁盘设备 id
OpenCAS 只支持使用 by-id 的形式配置磁盘. 首先获取缓存设备与后端设备的 id.
使用:
ls -l /dev/disk/by-id/ | grep -- 'sda$' # “sda” 替换为你的设备名获取设备的 id.
输入示例如下:
lrwxrwxrwx 1 root root 9 Jun 5 22:38 scsi-30000000000000000 -> ../../sdalrwxrwxrwx 1 root root 9 Jun 5 22:38 scsi-SATA_WDC_XXXXXXXXXXX-_XXXXXXXXXXXXXXXXXXXX -> ../../sdalrwxrwxrwx 1 root root 9 Jun 5 22:38 wwn-0x0000000000000000 -> ../../sdaid 建议使用 scsi-SATA_WDC_XXXXXXXXXXX-_XXXXXXXXXXXXXXXXXXXX, 该 id 只与磁盘本身关联, 与接口无关.
配置缓存
编辑 /etc/opencas/opencas.conf
sudo vim /etc/opencas/opencas.confversion=19.3.0# Version tag has to be first line in this file## Open CAS configuration file - for reference on syntax# of this file please refer to appropriate documentation
# NOTES:# 1) It is required to specify cache/core device using links in# /dev/disk/by-id/, preferably those using device WWN if available:# /dev/disk/by-id/wwn-0x123456789abcdef0# Referencing devices via /dev/sd* is prohibited because# may result in cache misconfiguration after system reboot# due to change(s) in drive order. It is allowed to use /dev/cas*-*# as a device path.
## Caches configuration section[caches]## Cache ID Cache device Cache mode Extra fields (optional)## Uncomment and edit the below line for cache configuration#1 /dev/disk/by-id/nvme-INTEL_SSDP.. WT
1 /dev/disk/by-id/<刚才获取的缓存设备的id> <缓存模式>
## Core devices configuration[cores]## Cache ID Core ID Core device Extra fields (optional)## Uncomment and edit the below line for core configuration#1 1 /dev/disk/by-id/wwn-0x123456789abcdef0
1 1 /dev/disk/by-id/<刚才获取的后端设备的id>
## To specify use of the IO Classification file, place content of the following line in the## Caches configuration section under Extra fields (optional)## ioclass_file=/etc/opencas/ioclass-config.csv
## If given cache/core pair is especially slow to start up, often doesn't come back## up after reboot or you simply don't care if it does and don't want it to have## an effect on your boot you can mark cores as lazy to prevent Open CAS from## dropping boot to emergency shell because of this core failure. To do this## put following line under in Extra fields (optional) section of core config:## lazy_startup=true## NOTE: This will cause open-cas.service to not wait for marked core while## starting up - this option should be used with care to prevent races with## other services for devices (e.g. mounts based on FS labels)缓存模式:
wt(Write-Through) OpenCAS 默认模式, 数据同时写入缓存与后端.wb(Write-Back) 数据直接写入缓存, 之后后台异步写入后端. (此模式突然断电可能丢失数据, 建议搭配 UPS 使用.)wa(Write-Around) 如果数据已经在缓存中则同时更新缓存与后端, 如果不在则直接写入后端.pt(Pass-Through) 完全透传数据. 相当于关闭缓存, 主要用于调试.wo(Write-Only) 只缓存写入, 不缓存读取.
配置完成后保存并重启.
重启后运行:
sudo casadm -L显示类型以下内容则配置成功.
type id disk status write policy devicecache 1 /dev/sdb Running wb -└core 1 /dev/md0 Active - /dev/cas1-1/dev/cas1-1 即为使用 /dev/sdb 做缓存加速后的 /dev/md0, /dev/md0 已有数据原封不动.