Skip to content
Vlad edited this page Feb 6, 2022 · 6 revisions

ZFS

Pools

# Use disk ids (ex. /dev/disk/by-id/scsi-SATA_ST4000VN008-2DR1_ZM40Y3K9)
ll /dev/disk/by-id/
# Create a mirrored pool
sudo zpool create new-pool mirror /dev/disk/by-id/scsi-SATA_ST3000VN000-111111 /dev/disk/by-id/scsi-SATA_ST3000VN000-222222
# Create a RAIDZ-1 pool
sudo zpool create new-pool raidz /dev/disk/by-id/scsi-SATA_ST4000VN008-111111 /dev/disk/by-id/scsi-SATA_ST4000VN008-222222 /dev/disk/by-id/scsi-SATA_ST4000VN008-333333
# Check the status of ZFS pools
sudo zpool status
# Remove the pool! Beware that this will also remove any files that you have created within the pool!
sudo zpool destroy new-pool

Warning: Do not set dnodesize on rpool (the zfs boot drive) because GRUB is not able to handle a different size

## Warning: Do not set dnodesize on root rpool
zfs set compression=lz4 atime=off relatime=off xattr=sa dnodesize=auto vpool
## Limit Memory (min 4GB - max 8GB; 8GB * 1024 * 1024 * 1024)
echo 'options zfs zfs_arc_max=8589934592' | tee -a /etc/modprobe.d/zfs.conf
echo 'options zfs zfs_arc_min=4294967296' | tee -a /etc/modprobe.d/zfs.conf
## Update initramfs
update-initramfs -u
## Reboot
reboot
## Check ARC size and limits
arcstat

Swappines

echo 'vm.swappiness = 0' | sudo tee /etc/sysctl.d/swappiness.conf
sysctl --system

Properties of the filesystem

zfs get all vpool/store
zfs get compression
zfs get atime
zfs get relatime
zfs get xattr
zfs get dnodesize

Cache

## Use flash for caching/logs. If you have only one SSD, use parted of gdisk to create a small partition for the ZIL (ZFS intent log) and a larger one for the L2ARC (ZFS read cache on disk). Make sure that the ZIL is on the first partition.
gdisk /dev/nvme1n1
zpool add vpool log /dev/disk/by-id/scsi-0QEMU_QEMU_HARDDISK_drive-scsi4-part1
zpool add vpool cache /dev/disk/by-id/scsi-0QEMU_QEMU_HARDDISK_drive-scsi4-part2

Import even if disks labels have changed

zpool import -ad /dev/disk/by-id/

Pools and datasets

zfs list
zpool list
zpool list -v
zpool iostat
zpool iostat -v
zfs create vpool/backup
zfs create vpool/store
zfs create vpool/vms

Snapshots

# Create snapshots for all descendent file systems by using the -r option
zfs snapshot -r tank@now
zfs snapshot -r vpool@"snapshot-$(date '+%Y%m%d-%H%M')"
zfs list -t snapshot
zfs destroy tank/home/home@now
# List space used by snapshots
zfs list -o space
# Move an entire pool:
zfs snapshot -r sourcepool@moving
zfs send -R sourcepool@moving | zfs receive -F destpool
# send snapshot to gzip file
time zfs send wpool/vm-101-disk-0@autosnap_2021-11-22_18:38:55_frequently | gzip > /mnt/pve/vds/testfile.gz
# list last snapshot
zfs list -t snapshot -o name -s creation -r wpool/vm-101-disk-0 | tail -1
# send last snasphost to gzip file
time zfs send $(zfs list -t snapshot -o name -s creation -r wpool/vm-101-disk-0 | tail -1) | gzip > /mnt/pve/vds/testfile.gz
# send snapshot compressed and encrypted
zfs send mybook/testzone@20100719-1600 | gzip | openssl enc -aes-256-cbc -a -salt > /storage/temp/testzone.gz.ssl
# decrypt and decompress snapshot
openssl enc -d -aes-256-cbc -a -in /storage/temp/testzone.gz.ssl | gunzip | zfs receive mybook/testzone_new
# send with ssh
zfs send mybook/testzone@20100719-1600 | ssh testbox zfs receive sandbox/testzone@20100719-1600

Share

# Share ZFS datasets (with ZFS is recommended because NFS service died right before the ZFS datasets were mounted)
apt-get update && apt-get install nfs-kernel-server
zfs set [email protected]/32 vpool/backup
zfs set [email protected]/32 vpool/store
# Multiple hosts: zfs set sharenfs='[email protected]/32,[email protected]/32' vpool/store
# Multiple CIDRs: zfs set sharenfs="rw=192.168.0.0/24,ro=10.0.0.0/8" vpool/store
# Stop share: zfs sharenfs=off vpool/store
# Check
showmount -e 127.0.0.1
cat /etc/dfs/sharetab

Misc

# Should we allow overlay mounts?
#  overlay=off|on
#    Allow mounting on a busy directory or a directory which already contains files or directories. This is the default
#    mount behavior for Linux file systems.  For consistency with OpenZFS on other platforms overlay mounts are off by
#    default.
#    Set to on to enable overlay mounts. Datasets will inherit.
zfs get overlay
zfs set overlay=on vpool

# Find ZFS version
cat /sys/module/zfs/version

References