Eric Radman : a Journal

ZFS Quickstart

ZFS has always provided a wide range of useful features, and over time it's performance has improved. In 2023 the OpenZFS project dramatically improved performance when using NVMe drives.

With some tuning, ZFS can provide good a platform for a database while providing better than 2-to-1 compression.

Rocky/Alma Linux Install

dnf install https://zfsonlinux.org/epel/zfs-release-2-2.el9.noarch.rpm
dnf config-manager --disable zfs
dnf config-manager --enable zfs-kmod
dnf install zfs
echo zfs > /etc/modules-load.d/zfs.conf

See also OpenZFS RHEL install.

Limit ARC memory use if the system is running a memory intensive application such as a database

echo 1073741824 > /sys/module/zfs/parameters/zfs_arc_max

raidz2

ZFS also has the equivalent of RAID 6

zpool create -O canmount=on -O recordsize=16k -O compression=lz4 -o ashift=12 \
  starfishzfs raidz2 /dev/sdb /dev/sdc /dev/sdd /dev/sde /dev/sdf /dev/sdg

FreeBSD

# /etc/rc.conf
zfs_enable="YES"

Limit ARC memory use

# /etc/sysctl.conf
vfs.zfs.arc.max=1073741824

Provision new disk and zpool

gpart create -s gpt nvd1
gpart add -t freebsd-zfs nvd1
gpart show nvd1
zpool create -O compression=lz4 zpool2 /dev/nvd1p1
zfs create -o mountpoint=/scratch-ci zpool2/scratch/ci

PostgreSQL Configuration

Since ZFS provides consistency guarantees that other file systems do not, so PostgreSQL features that hedge against file system errors can be disabled

ALTER SYSTEM SET synchronous_commit=off;
ALTER SYSTEM SET full_page_writes=off;
SELECT pg_reload_conf();

See also: PostgreSQL + ZFS

Automated Snapshot Managment

To automate snapshot retention make a new periodic snapshot and prune anything that is more than N days old

#!/bin/sh -e

today=$(date +"%Y-%m-%d")

case $(hostname -s) in
   bhyve1)
      for fs in zpool1/eradman-backup zpool1/laura-backup; do
         zfs snapshot $fs@$today

         for snap in $(zfs list -t snapshot -H -o name $fs | tail +30); do
            zfs destroy $snap
         done
      done
      ;;
esac

Run daily

15  20  *  *  * /usr/local/bin/zfs-snap.sh

NFS Export

While any local mount can be added to /etc/exports ZFS sharing allows mount points to be automatically exports by sharing the container

$ zfs sharenfs='-ro,-network 192.168.2.0/24' zpool1
$ zfs get sharenfs
NAME               PROPERTY  VALUE                        SOURCE
zpool1             sharenfs  -ro,-network 192.168.2.0/24  local
zpool1/scratch-ci  sharenfs  -ro,-network 192.168.2.0/24  inherited from zpool1