{"id":1449,"date":"2020-02-21T13:02:12","date_gmt":"2020-02-21T19:02:12","guid":{"rendered":"http:\/\/blog.ls-al.com\/?p=1449"},"modified":"2020-04-12T11:59:57","modified_gmt":"2020-04-12T16:59:57","slug":"ubuntu-server-20-04-zfs-root-and-oci","status":"publish","type":"post","link":"https:\/\/blog.ls-al.com\/ubuntu-server-20-04-zfs-root-and-oci\/","title":{"rendered":"Ubuntu server 20.04 zfs root and OCI"},"content":{"rendered":"
As you probably know newer desktop versions of Ubuntu will offer ZFS for root volume during installation. I am not sure if that is true in Ubuntu 20.04 server installs and when I looked at the 19.10 ZFS installation I did not necessarily want to use the ZFS layout they did. My experiment is my custom case and also tested on Ubuntu 16.04 and 18.04.<\/p>\n
Note this is an experiment and ZFS layout, boot partition type, LUKS, EFI, multiple boot disks(mirrored) and netplan are all debatable configurations. Mine may not be ideal but it works for my use case.<\/p>\n
Also my goal here was to export a bootable\/usable OCI (Oracle Cloud Infrastructure) compute instance.<\/p>\n
Start by booting a recent desktop live CD. Since I am testing 20.04 (focal) I used that. In the live cd environment open a terminal, sudo and apt install ssh. Start the ssh service and set ubuntu user password.<\/p>\n
$ ssh ubuntu@192.168.1.142\n$ sudo -i\n\n##\napt-add-repository universe\napt update\napt install --yes debootstrap gdisk zfs-initramfs\n\n## find correct device name for below\nDISK=\/dev\/disk\/by-id\/ata-VBOX_HARDDISK_VB26c080f2-2bd16227\nUSER=ubuntu\nHOST=server\nPOOL=ubuntu\n\n##\nsgdisk --zap-all $DISK\nsgdisk --zap-all $DISK\nsgdisk -a1 -n1:24K:+1000K -t1:EF02 $DISK\nsgdisk -n2:1M:+512M -t2:EF00 $DISK\nsgdisk -n3:0:+1G -t3:BF01 $DISK\nsgdisk -n4:0:0 -t4:BF01 $DISK\nsgdisk --print $DISK\n\n##\nzpool create -o ashift=12 -d \\\n -o feature@async_destroy=enabled \\\n -o feature@bookmarks=enabled \\\n -o feature@embedded_data=enabled \\\n -o feature@empty_bpobj=enabled \\\n -o feature@enabled_txg=enabled \\\n -o feature@extensible_dataset=enabled \\\n -o feature@filesystem_limits=enabled \\\n -o feature@hole_birth=enabled \\\n -o feature@large_blocks=enabled \\\n -o feature@lz4_compress=enabled \\\n -o feature@spacemap_histogram=enabled \\\n -o feature@userobj_accounting=enabled \\\n -O acltype=posixacl -O canmount=off -O compression=lz4 -O devices=off \\\n -O normalization=formD -O relatime=on -O xattr=sa \\\n -O mountpoint=\/ -R \/mnt bpool ${DISK}-part3\n\nzpool create -o ashift=12 \\\n -O acltype=posixacl -O canmount=off -O compression=lz4 \\\n -O dnodesize=auto -O normalization=formD -O relatime=on -O xattr=sa \\\n -O mountpoint=\/ -R \/mnt rpool ${DISK}-part4\n\nzfs create -o canmount=off -o mountpoint=none rpool\/ROOT\nzfs create -o canmount=off -o mountpoint=none bpool\/BOOT\n\nzfs create -o canmount=noauto -o mountpoint=\/ rpool\/ROOT\/ubuntu\nzfs mount rpool\/ROOT\/ubuntu\n\nzfs create -o canmount=noauto -o mountpoint=\/boot bpool\/BOOT\/ubuntu\nzfs mount bpool\/BOOT\/ubuntu\n\n## Note: I skipped creating datasets for home, root, var\/lib\/ \/var\/log etc etc\n\n##\ndebootstrap focal \/mnt\nzfs set devices=off rpool\n\n## \ncat > \/mnt\/etc\/netplan\/01-netcfg.yaml<< EOF\nnetwork:\n version: 2\n ethernets:\n enp0s3:\n dhcp4: true\nEOF\n\n##\ncat > \/mnt\/etc\/apt\/sources.list<< EOF\ndeb http:\/\/archive.ubuntu.com\/ubuntu focal main universe\nEOF\n\n##\nmount --rbind \/dev \/mnt\/dev\nmount --rbind \/proc \/mnt\/proc\nmount --rbind \/sys \/mnt\/sys\nchroot \/mnt \/usr\/bin\/env DISK=$DISK bash --login\n\n##\nlocale-gen --purge en_US.UTF-8\nupdate-locale LANG=en_US.UTF-8 LANGUAGE=en_US\ndpkg-reconfigure --frontend noninteractive locales\necho US\/Central > \/etc\/timezone \ndpkg-reconfigure -f noninteractive tzdata\n\n##\npasswd\n\n##\napt install --yes --no-install-recommends linux-image-generic\napt install --yes zfs-initramfs\napt install --yes grub-pc\ngrub-probe \/boot\n\n##update-initramfs -u -k all <- this does not work. try below \nKERNEL=`ls \/usr\/lib\/modules\/ | cut -d\/ -f1 | sed 's\/linux-image-\/\/'`\nupdate-initramfs -u -k $KERNEL\n\n# edit \/etc\/default\/grub\nGRUB_DEFAULT=0\n#GRUB_TIMEOUT_STYLE=hidden\nGRUB_TIMEOUT=5\nGRUB_CMDLINE_LINUX_DEFAULT=\nGRUB_CMDLINE_LINUX=root=ZFS=rpool\/ROOT\/ubuntu console=tty1 console=ttyS0,115200\nGRUB_TERMINAL=serial console\nGRUB_SERIAL_COMMAND=serial --unit=0 --speed=115200\n\n##\nupdate-grub\ngrub-install $DISK\n\n##\ncat > \/etc\/systemd\/system\/zfs-import-bpool.service<< EOF\n[Unit]\n DefaultDependencies=no\n Before=zfs-import-scan.service\n Before=zfs-import-cache.service\n\n[Service]\n Type=oneshot\n RemainAfterExit=yes\n ExecStart=\/sbin\/zpool import -N -o cachefile=none bpool\n\n[Install]\n WantedBy=zfs-import.target\nEOF\n\nsystemctl enable zfs-import-bpool.service\n\n##\nzfs set mountpoint=legacy bpool\/BOOT\/ubuntu\necho bpool\/BOOT\/ubuntu \/boot zfs \\\n nodev,relatime,x-systemd.requires=zfs-import-bpool.service 0 0 >> \/etc\/fstab\nzfs snapshot bpool\/BOOT\/ubuntu@install\nzfs snapshot rpool\/ROOT\/ubuntu@install\n\n##\nsystemctl enable serial-getty@ttyS0\napt install ssh\nsystemctl enable ssh\n\n##\nexit\n##\nmount | grep -v zfs | tac | awk '\/\\\/mnt\/ {print $3}' | xargs -i{} umount -lf {}\nzpool export -a<\/code><\/pre>\n<\/h2>\n
** reboot<\/p>\n
** detach live cd<\/p>\n
NOTE: grub had a prompt on reboot but no options. try below<\/p>\n
KERNEL=ls \/usr\/lib\/modules\/ | cut -d\/ -f1 | sed 's\/linux-image-\/\/'\nupdate-initramfs -u -k $KERNEL\nupdate-grub<\/code><\/pre>\n