ZFS filesystem on Linux

boot Ubuntu 16.04 LiveCD
terminal
sudo apt-get install -y ssh
sudo passwd ubuntu
ip a

SSH login to Ubuntu LiveCD
ssh ubuntu@

sudo su

apt-add-repository universe
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

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 < /mnt/etc/network/interfaces.d/enp3s0
auto enp3s0
iface enp3s0 inet dhcp
EOF

cat < /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
zfs snapshot rpool/home@$(date -I)
zfs list -t snapshot
zfs diff rpool/home@$(date -I) rpool/home

diff /home/foo/file.txt /home/.zfs/snapshot/$(date -I)foo/file.txt

zfs destroy rpool/ROOT/ubuntu@2016-12-17

# remove snapshots
zfs list -t snapshot -o name -S creation -d1 rpool/home | tail -n +30 #| xargs -n 1 zfs destroy
zfs destroy zpool1/dataset@20171112%20171212

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

Commands
zfs set mountpoint=legacy tank/home/foo
mount -F zfs tank/home/foo /mnt

# show compressratio ratio
zfs get compressratio rpool/ROOT/ubuntu

# remove volume
zfs destroy rpool/ROOT/ubuntu_16.10

Quota
zfs set quota=100G tank/foo
zfs set reservation=100G tank/foo
zfs get quota tank/foo

Cronjobs
echo "0 10 * * * root /sbin/zfs snapshot rpool/home@$(date -I)" > /etc/cron.d/zfs-snapshot
service cron restart

Manage mounts
zfs mount tank/foo
zfs umount tank/foo

Links
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