配置zfs
环境
ST2000LM003 是仅有的一款 2T 大小的 CMR 2.5 英寸硬盘,目前非常稀有,淘宝上卖的 ST2000LM003 散装硬盘大概率是清零盘。不过我从咸鱼买到了两块内芯是 ST2000LM003 的移动硬盘,拆解后将其中的 2.5 英寸硬盘放到 deskmini 中组成小型 NAS。
- 系统:Gentoo Linux
- 机器:deskmini A300
- 硬盘:ST2000LM003 * 2
安装 ZFS
Gentoo 系统安装 zfs 还算简单,不过要注意开启一些方便使用的 USE。
echo "sys-fs/zfs dist-kernel" >> /etc/portage/package.use echo "sys-fs/zfs-kmod dist-kernel" >> /etc/portage/package.use emerge -avt sys-fs/zfs
注意,每次更新内核后,都需要重新安装 zfs 模块。Gentoo 可以通过命令 emerge -avt @module-rebuild
完成。
由于这里我并不使用 zfs 作为 root 的文件系统,所以省略了将 zfs 模块添加到 initramfs 中等步骤。
配置 ZFS
设置 ARC 内存大小
zfs 的 ARC 缓存默认最大使用系统的三分之二内存。如果想要内存占用少一点,就需要使用下面的方法加以限制,这里限制了最大使用 4GB 内存。
echo "options zfs zfs_arc_max=4294967296" >> /etc/modprobe.d/zfs.conf
开启 systemd 服务
开启以下服务后,zfs 才能正常使用。注意开启后需要重启,或者手动启动以下服务。
systemctl enable zfs.target systemctl enable zfs-import-cache systemctl enable zfs-mount systemctl enable zfs-import.target
创建 zpool 与 dataset
现在我们就可以正式开始使用 zfs 了,我们先创建 zpool 与 dataset,并设置一些优化参数。这里我们将两块 ST2000LM003 组成 raid0。
首先,使用命令查看硬盘的 id,通过 id 创建 zpool 是最佳的方法,可以避免因盘序或 UUID 变化带来的一些问题。
➜ ls -lh /dev/disk/by-id/ total 0 lrwxrwxrwx 1 root root 9 Jul 18 15:06 ata-ST2000LM003_HN-M201RAD_S362J9DG938918 -> ../../sdb lrwxrwxrwx 1 root root 10 Jul 18 15:06 ata-ST2000LM003_HN-M201RAD_S362J9DG938918-part1 -> ../../sdb1 lrwxrwxrwx 1 root root 10 Jul 18 15:06 ata-ST2000LM003_HN-M201RAD_S362J9DG938918-part9 -> ../../sdb9 lrwxrwxrwx 1 root root 9 Jul 18 15:06 ata-ST2000LM003_HN-M201RAD_S362J9EG922993 -> ../../sda lrwxrwxrwx 1 root root 10 Jul 18 15:06 ata-ST2000LM003_HN-M201RAD_S362J9EG922993-part1 -> ../../sda1 lrwxrwxrwx 1 root root 10 Jul 18 15:06 ata-ST2000LM003_HN-M201RAD_S362J9EG922993-part9 -> ../../sda9
这里,我们得到两块硬盘的 id 分别为 ata-ST2000LM003_HN-M201RAD_S362J9DG938918 和 ata-ST2000LM003_HN-M201RAD_S362J9EG922993。接下来,就通过这两个参数来创建 zpool。
# 创建zpool,注意机械硬盘必须使用参数ashift=12,具有8K扇区的SSD应使用ashift=13。该参数在创建zpool完毕以后,就无法修改了。 zpool create -f -o ashift=12 storage ata-ST2000LM003_HN-M201RAD_S362J9DG938918 ata-ST2000LM003_HN-M201RAD_S362J9EG922993 # 创建dataset并设置挂载点 zfs create -o mountpoint=/srv/data storage/data zfs create -o mountpoint=/srv/data/nas storage/data/nas # 开启压缩并使用压缩算法lz4 zfs set compression=lz4 storage # 因为性能原因,最好设置xattr。详情见https://github.com/zfsonlinux/zfs/issues/170#issuecomment-27348094 zfs set acltype=posixacl storage zfs set xattr=sa storage zfs set aclinherit=passthrough storage # 仅在modified time或changed time 改变,或者acces time24小时内没有更新时,才更新access time zfs set atime=on storage zfs set relatime=on storage # SSD还需要开启autotrim zpool set autotrim=on storage
最后,我们还需要生成 hostid 和更新 cachefile。
# 生成hostid,注意生成完hostid后需要更新initramfs zgenhostid # 更新cachefile zpool set cachefile=/etc/zfs/zpool.cache storage
参考链接
- ZFS archwiki:https://wiki.archlinux.org/title/ZFS
- ZFS Gentoo wiki:https://wiki.gentoo.org/wiki/ZFS
- 在 Gentoo 上优雅的吃 ZFS:https://www.yafa.moe/post/gentoo-on-zfs/
- 在 Linux 上安装和使用 ZFS:https://www.escapelife.site/posts/caf259ea.html
- ZFS dedup 属性:https://docs.oracle.com/cd/E26926_01/html/E25826/gazss.html#gjhav
- Attaching and Detaching Devices in a Storage Pool:https://docs.oracle.com/cd/E19120-01/open.solaris/817-2271/gcfhe/index.html