ZFS filesystem on Linux

Create ZFS filesystem

apt install -y zfsutils-linux
zpool create tank /dev/system/lxd 
zfs create -o mountpoint=/var/lib/lxd2 tank/lxd

Boot Ubuntu LiveCD

terminal
sudo apt-get install -y ssh
sudo passwd ubuntu
ip a

Mount all datasets

zfs mount -a

ZFS on Ubuntu 23.04
https://github.com/Sithuk/ubuntu-server-zfsbootmenu

SSH login to Ubuntu LiveCD

ssh ubuntu@<IP>
 
sudo su
 
sudo apt-get install -y software-properties-common
sudo apt-add-repository universe
sudo apt update
 
apt install -y debootstrap zfs-initramfs
 
# rmove previous ZFS pool
zpool export rpool
 
DEVICES="
/dev/disk/by-id/ata-TOSHIBA_DT01ACA200_938457298
/dev/disk/by-id/ata-TOSHIBA_DT01ACA200_9D0JN9VR4
"
 
for DEVICE in ${DEVICES}; do
 zpool labelclear -f ${DEVICE}
 parted -s -- ${DEVICE} mklabel gpt
 sgdisk -a1 -n2:40:2047 -t2:EF02 ${DEVICE}
 sgdisk -n9:-8M:0 -t9:BF07 ${DEVICE}
 sgdisk -n1:0:0 -t1:BF01 ${DEVICE}
done
 
partprobe
 
zpool create rpool \
 mirror /dev/disk/by-id/ata-TOSHIBA_DT01ACA200_9384572984-part1 /dev/disk/by-id/ata-TOSHIBA_DT01ACA200_9D0JN9VR43-part1 \
 cache /dev/disk/by-id/ata-M4-CT128M4SSD2_00000000030430HS40443 \
 -o ashift=12 -O atime=off -O canmount=off -O compression=lz4 -O normalization=formD \
 -O mountpoint=/ -R /mnt
 
# statistics
zpool list
zpool status
zpool iostat -v 1
 
zfs create -o canmount=off -o mountpoint=none rpool/ROOT
zfs create -o canmount=noauto -o mountpoint=/ rpool/ROOT/ubuntu
zfs mount rpool/ROOT/ubuntu
zfs create -o setuid=off rpool/home
# zfs create -o mountpoint=/root rpool/home/root
# zfs create -o canmount=off -o setuid=off -o exec=off rpool/var
# zfs create -o com.sun:auto-snapshot=false rpool/var/cache
# zfs create rpool/var/log
# zfs create rpool/var/spool
# zfs create -o com.sun:auto-snapshot=false -o exec=on rpool/var/tmp
 
# install Ubuntu Xenial
debootstrap xenial /mnt
 
zfs set devices=off rpool
 
chmod 1777 /mnt/var/tmp
 
echo zfs > /mnt/etc/hostname
echo "127.0.1.1 zfs.local zfs" > /mnt/etc/hosts
 
cat <<EOF> /mnt/etc/network/interfaces.d/enp3s0
auto enp3s0
iface enp3s0 inet dhcp
EOF
 
cat <<EOF> /mnt/etc/apt/sources.list
deb http://archive.ubuntu.com/ubuntu xenial main universe
deb-src http://archive.ubuntu.com/ubuntu xenial main universe
 
deb http://security.ubuntu.com/ubuntu xenial-security main universe
deb-src http://security.ubuntu.com/ubuntu xenial-security main universe
 
deb http://archive.ubuntu.com/ubuntu xenial-updates main universe
deb-src http://archive.ubuntu.com/ubuntu xenial-updates main universe
EOF
 
mount --rbind /dev /mnt/dev
mount --rbind /proc /mnt/proc
mount --rbind /sys /mnt/sys
 
# chroot to installation
chroot /mnt /bin/bash --login
 
# todo: preseed Configuring console-setup 
apt-get update
apt-get -y dist-upgrade
apt install -y ssh
 
locale-gen en_US.UTF-8
echo 'LANG="en_US.UTF-8"' > /etc/default/locale
 
apt install -y --no-install-recommends linux-image-generic zfs-initramfs grub2
 
echo "root:zfs" | chpasswd
 
# grub hotfix
ln -s /dev/sda /dev/ata-M4-CT128M4SSD2_000000001309092D89F1
ln -s /dev/sdb /dev/ata-TOSHIBA_DT01ACA200_9384572984
ln -s /dev/sdc /dev/ata-TOSHIBA_DT01ACA200_9D0JN9VR43
 
# debug
grub-probe /
 
# install grub
update-grub2
update-initramfs -c -k all
 
grub-install /dev/ata-TOSHIBA_DT01ACA200_9384572984 
grub-install /dev/ata-TOSHIBA_DT01ACA200_9D0JN9VR43
 
zfs snapshot rpool/ROOT/ubuntu@$(date -I)
 
mount | grep -v zfs | tac | awk '/\/mnt/ {print $3}' | xargs -i{} umount -lf {}
zpool export rpool
 
reboot

Create swap

zfs create -V 2G -b $(getconf PAGESIZE) -o compression=zle -o logbias=throughput -o sync=always -o primarycache=metadata -o secondarycache=none -o com.sun:auto-snapshot=false rpool/swap
 
mkswap -f /dev/zvol/rpool/swap
echo /dev/zvol/rpool/swap none swap defaults 0 0 >> /etc/fstab
swapon -a

Snapshot

# create
zfs snapshot rpool/home@$(date -I)
 
# list
zfs list -t snapshot
 
# diff
zfs diff rpool/home@$(date -I) rpool/home
 
diff /home/foo/file.txt /home/.zfs/snapshot/$(date -I)foo/file.txt
 
# delete
zfs destroy rpool/ROOT/ubuntu@2016-12-17
 
# remove snapshots
for POOL in ROOT USERDATA; do
    sudo zfs list -t snapshot -o name -S creation -d2 rpool/${POOL} | tail -n +30 #| sudo xargs -n 1 zfs destroy
done
 
zfs destroy zpool1/dataset@20171112%20171212
 
# mount snapshot
mount -t zfs tank/u1804@2018-08-13 /mnt

rescue ZFS with Ubuntu LiveCD

apt update && apt install -y zfsutils-linux
zpool export -a
zpool import -N -R /mnt rpool
zfs mount rpool/ROOT/ubuntu
zfs mount -a
mount --rbind /dev  /mnt/dev
mount --rbind /proc /mnt/proc
mount --rbind /sys  /mnt/sys
chroot /mnt /bin/bash --login

ZFS module configuration

# Limit ARC to 4 GB
echo "options zfs zfs_arc_max=4294967296" >> /etc/modprobe.d/zfs.conf

Cache

parted -s -- /dev/disk/by-id/ata-SanDisk_SDSSDHP128G_1234 mklabel gpt
sgdisk -n1:0:2G -t1:BF07 /dev/disk/by-id/ata-SanDisk_SDSSDHP128G_1234
sgdisk -n2:0:96G -t2:BF07 /dev/disk/by-id/ata-SanDisk_SDSSDHP128G_1234
 
zpool add tank log mirror /dev/sdc1 /dev/sdd1
zpool add tank cache /dev/sdc2 /dev/sdd2

Commands

zfs set mountpoint=legacy tank/foo
mount -t zfs tank/foo /mnt
 
# show compressratio ratio
zfs get compressratio rpool/ROOT/ubuntu
sudo zfs get all | grep compressratio
 
# get all
zfs get all tank/home

remove / delete volume

zpool destroy tank
zfs destroy rpool/ROOT/ubuntu_16.10
zfs destroy media/sys2 -r

Quota

zfs get refquota,quota tank/foo
zfs set quota=450G tank/foo
zfs set refquota=150G tank/foo

reservation

zfs set reservation=100G tank/foo
sudo zfs get all | grep reservation | grep -v none

Cronjobs

# cat /etc/cron.d/zfs-snapshot 
# create zfs snapshoot
0 12 * * * root for POOL in sys2 home; do /sbin/zfs snapshot media/${POOL}@$(date -I); done
 
# remove old snapshoots
1 12 * * * root for POOL in sys2 home; do /sbin/zfs list -t snapshot -o name -S creation -d1  media/${POOL} | tail -n +31 | sudo xargs -n 1 zfs destroy; done
 
service cron restart

Mount

mount -t zfs tank/sys2@2017-12-31 /mnt

Manage mounts

zfs mount tank/foo
zfs umount tank/foo

Change mountpoint

zfs set mountpoint=/mnt tank/backup
 
# test
sed -i 's| ro | rw |g' /etc/grub.d/10_linux
 
zpool history
 
# replace device
parted -s -- /dev/sdb mklabel gpt
sgdisk -a1 -n2:40:2047 -t2:EF02 /dev/sdb
sgdisk -n9:-8M:0 -t9:BF07 /dev/sdb
sgdisk -n1:0:0 -t1:BF01 /dev/sdb
zpool replace tank ata-ST31000528AS_axxxxxxxx-part1 ata-WDC_WD1002FBYS-18A6B0_WD-axxxxxxxx-part1
# ln -s /dev/sdb /dev/ata-WDC_WD1002FBYS-18A6B0_WD-axxxxxxxx
#grub-install /dev/ata-WDC_WD1002FBYS-18A6B0_WD-axxxxxxxx
 
# manual scrub
zpool scrub tank
zpool status -v tank

Parameter

# show all parameter
zfs get all tank/media
 
zfs set snapdir=visible tank/test
 
# greate raid10
sudo zpool create tank \
  mirror /dev/sda /dev/sdb \
  mirror /dev/sdc /dev/sdd
 
# add device to zpool
sudo zpool add [existing pool name] mirror /dev/sdd /dev/sde

zfs-auto-snapshot

sudo apt-add-repository multiverse
sudo apt install -y zfs-auto-snapshot

ZFS import pool

zpool import tank tank2 -m

scrub

sudo zpool scrub tank
sudo zpool status -v

Copy dataset

zfs send rpool/filesystem@snapshot | zfs receive destinationpool/filesystem

ZFS SYStem integration control zsys daemon

zsysctl save

Encryption
https://arstechnica.com/gadgets/2021/06/a-quick-start-guide-to-openzfs-native-encryption/

zfs change-key rpool -o keyformat=passphrase -o keylocation=prompt 

Trim

zpool get autotrim
zpool set autotrim=on zpool1
sudo zpool trim rpool

Links
https://openzfs.github.io/openzfs-docs/Getting%20Started/Ubuntu/Ubuntu%2022.04%20Root%20on%20ZFS.html
https://wiki.ubuntu.com/Kernel/Reference/ZFS
https://wiki.ubuntu.com/ZFS
https://github.com/zfsonlinux/zfs/wiki/Ubuntu-16.04-Root-on-ZFS
https://www.freebsd.org/cgi/man.cgi?query=zfs&sektion=8&apropos=0&manpath=FreeBSD+11.0-RELEASE+and+Ports
https://docs.oracle.com/cd/E53394_01/html/E54801/gkkih.html
http://blog.mellenthin.de/archives/2012/08/11/ubuntu-auf-einem-rootserver-mit-zfs/ - ZFS on Hetzner root server