ZFS filesystem on Linux

boot Ubuntu 16.04 LiveCD
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


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}


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 " zfs.local zfs" > /mnt/etc/hosts

cat < /mnt/etc/network/interfaces.d/enp3s0
auto enp3s0
iface enp3s0 inet dhcp

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

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-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


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

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

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

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 / reservation
zfs set quota=100G tank/foo
zfs set reservation=100G tank/foo
zfs get quota tank/foo

#echo "0 10 * * * root /sbin/zfs snapshot rpool/home@\$(date -I)" | sudo tee -a /etc/cron.d/zfs-snapshot
echo "59 23 * * * root for POOL in lxc-sys1 home; do /sbin/zfs snapshot tank/\${POOL}@\$(date -I); done" | sudo tee /etc/cron.d/zfs-snapshot

service cron restart

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

http://blog.mellenthin.de/archives/2012/08/11/ubuntu-auf-einem-rootserver-mit-zfs/ - ZFS on Hetzner root server