Skip to content

Instantly share code, notes, and snippets.

@hww
Last active September 6, 2023 21:40
Show Gist options
  • Save hww/7692087b7f7eb4d5beaefc869b0e8ea7 to your computer and use it in GitHub Desktop.
Save hww/7692087b7f7eb4d5beaefc869b0e8ea7 to your computer and use it in GitHub Desktop.
Install gentoo ZFS root
#
# Boot Linux Live CD with ZFS support
# fearedbliss maintains a variant of System Rescue CD that is highly recommended:
# https://wiki.gentoo.org/wiki/User:Fearedbliss
# Instruction for a USB key version
# http://www.sysresccd.org/Sysresccd-manual-en_How_to_install_SystemRescueCd_on_an_USB-stick
# To use ssh
# ssh -fN -R localhost:10099:localhost:22 name@server
# Create pool
zpool create -f -o ashift=12 -o cachefile=/tmp/zpool.cache -O normalization=formD -m none -R /mnt/gentoo -d -o feature@async_destroy=enabled -o feature@empty_bpobj=enabled -o feature@lz4_compress=enabled -o feature@spacemap_histogram=enabled -o feature@enabled_txg=enabled -o feature@extensible_dataset=enabled -o feature@bookmarks=enabled -f zroot mirror /dev/disk/by-id/ata-ST1000DM003-9YN162_S1D3SDFW-part1 /dev/disk/by-id/ata-WDC_WD10EAVS-00D7B0_WD-WCAU40316702-part1
# Workaround 0.6.4 regression
zfs umount "/mnt/gentoo/zroot"
rmdir "/mnt/gentoo/zroot"
# Create rootfs
zfs create -o mountpoint=none zroot/ROOT
zfs create -o mountpoint=/ zroot/ROOT/gentoo
# Create home directories
zfs create -o mountpoint=/home zroot/HOME
zfs create -o mountpoint=/root zroot/HOME/root
# Create portage directories
zfs create -o mountpoint=none -o setuid=off zroot/GENTOO
zfs create -o mountpoint=/usr/portage -o atime=off zroot/GENTOO/portage
zfs create -o mountpoint=/usr/portage/distfiles zroot/GENTOO/distfiles
# Create portage build directory
zfs create -o mountpoint=/var/tmp/portage -o compression=lz4 -o sync=disabled zroot/GENTOO/build-dir
# Create optional packages directory
zfs create -o mountpoint=/usr/portage/packages zroot/GENTOO/packages
# Create optional ccache directory
zfs create -o mountpoint=/var/tmp/ccache -o compression=lz4 zroot/GENTOO/ccache
# Set bootfs
zpool set bootfs=zroot/ROOT/gentoo zroot
# Download stage3
wget 'ftp://gentoo.osuosl.org/pub/gentoo/releases/amd64/autobuilds/current-stage3-amd64/stage3-amd64-[0-9]*.tar.bz2'
# Extract stage3
tar -xvjpf stage3-amd64-*.tar.bz2 -C /mnt/gentoo
# Copy zpool.cache into chroot
mkdir -p /mnt/gentoo/etc/zfs
cp /tmp/zpool.cache /mnt/gentoo/etc/zfs/zpool.cache
# Copy resolv.conf into chroot
cp /etc/resolv.conf /mnt/gentoo/etc/resolv.conf
# Mount filesystems
mount -t proc none /mnt/gentoo/proc
mount --rbind /dev /mnt/gentoo/dev
mount --rbind /sys /mnt/gentoo/sys
# chroot
chroot /mnt/gentoo /bin/bash
env-update; source /etc/profile; export PS1="(chroot) $PS1"; cd
# Get portage snapshot (use OSUOSL mirror because it is usually fast)
env GENTOO_MIRRORS="http://gentoo.osuosl.org" emerge-webrsync
# Install genkernel
emerge sys-kernel/genkernel
# Install sources
emerge sys-kernel/gentoo-sources
# Build initial kernel (required for checks in sys-kernel/spl and sys-fs/zfs)
# FIXME: Make genkernel support modules_prepare
genkernel kernel --no-clean --no-mountboot
# FIXME: Here was error
* ERROR: Failed to compile the "firmware_install" target...
# To fix it:
# echo sys-kernel/genkernel >> /etc/portage/package.accept_keywords
# emerge genkernel
# emerge sys-kernel/gentoo-sources
# genkernel kernel --no-clean --no-mountboot
# Install ZFS
# FXME: Because here is gentoo specific bug instead of this:
#echo "sys-kernel/spl ~amd64" >> /etc/portage/package.accept_keywords
#echo "sys-fs/zfs-kmod ~amd64" >> /etc/portage/package.accept_keywords
#echo "sys-fs/zfs ~amd64" >> /etc/portage/package.accept_keywords
# use this
echo "sys-kernel/spl **" >> /etc/portage/package.accept_keywords
echo "sys-fs/zfs-kmod **" >> /etc/portage/package.accept_keywords
echo "sys-fs/zfs **" >> /etc/portage/package.accept_keywords
# then
emerge sys-fs/zfs
# Add zfs to boot runlevel
rc-update add zfs-import boot
rc-update add zfs-mount boot
rc-update add zfs-share default
rc-update add zfs-zed default
# Install gptfdisk
emerge sys-apps/gptfdisk
# Make BIOS Boot Partition (make certain /dev/sda is your actual block device!)
sgdisk --new=2:48:2047 --typecode=2:EF02 --change-name=2:"BIOS boot partition" /dev/sda
# Flush the page cache to ensure GRUB2 sees the latest disk content
echo 1 > /proc/sys/vm/drop_caches
# Install GRUB2
echo "sys-boot/grub:2 libzfs" >> /etc/portage/package.use/zfs
echo "sys-boot/grub:2 ~amd64" >> /etc/portage/package.accept_keywords
emerge sys-boot/grub:2
touch /etc/mtab
# sys-fs/zfs-kmod and sys-fs/zfs switched to a proposed stable /dev/zfs API in
# 0.6.5.3. This breaks GRUB2 if the userland expects the new ABI and the kernel
# modules in the live environment do not support it.
#
# Provided you have the latest ebuilds (0.6.5.3-r1), your userland should be
# using the stable ABI. You can see if the new stable ABI is supported in your
# live environment by running grub2-probe:
grub2-probe /
# Idealy, grub2-probe would print "zfs" (without the quotes). If grub2-probe
# reported an error, it is likely that your kernel modules from your live
# environment do not support the new ABI. The kernel modules from 0.6.5.3-r1
# support both, so install the older userland tools as a workaround. This will
# avoid a failure in grub2-install and/or genkernel's invocation of grub2-mkconfig.
emerge -1v --nodeps =sys-fs/zfs-0.6.5.3
# Install GRUB2 to disk (make certain /dev/sda is your actual block device!)
grub2-install /dev/sda
# We must create an empty configuration file so genkernel fines the right one.
touch /boot/grub/grub.cfg
# Build kernel and initramfs
genkernel all --no-clean --no-mountboot --zfs --bootloader=grub2 --callback="emerge @module-rebuild"
# If you needed to install the old tools, you should run the following to
# install the new tools again and update the initramfs archive.
emerge -1v --nodeps sys-fs/zfs
genkernel initramfs --no-clean --no-mountboot --zfs
# Comment the BOOT, ROOT and SWAP lines in /etc/fstab
sed -i -e "s/\(.*\)\/\(BOOT\|ROOT\|SWAP\)\(.*\)/\#\1\/\2\3/g" /etc/fstab
#
# Follow chapters 8 through 12 of Gentoo Handboook: http://www.gentoo.org/doc/en/handbook/
#
# Make recovery snapshot after booting into new install
zfs snapshot zroot/ROOT/gentoo@install
# Additional tips
# ARC tuning - You can configure the RAM that ARC uses in bytes
echo options zfs zfs_arc_max=536870912 >> /etc/modprobe.d/zfs.conf
# Set portage niceness to minimize potential for updates to cause lag
echo PORTAGE_NICENESS=19 >> /etc/make.conf
# Set portage to compile in a CGROUP to minimize lag
cat << END > /usr/local/sbin/portage-cgroup
#!/bin/sh
# $1 must be the portage PID
cgroup=$(mktemp -d --tmpdir=/sys/fs/cgroup/cpu portage-XXXX) &&
echo $1 > "${cgroup}/tasks" &&
echo 1 > "${cgroup}/notify_on_release"
echo 256 > "${cgroup}/cpu.shares"
END
chmod u+x /usr/local/sbin/portage-cgroup
echo 'PORTAGE_IONICE_COMMAND="/usr/local/sbin/portage-cgroup \${PID}"' >> /etc/make.conf
# Swap support
zfs create -o sync=always -o primarycache=metadata -o secondarycache=none -b 4K -V 8G zroot/swap
mkswap -f /dev/zvol/zroot/swap
zfs snapshot zroot/swap@install
# Edit fstab to use swap here
#
# Note: Integrate `zfs create -o mountpoint=/home/user zroot/HOME/user` into procedure
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment