User:Ali3nx/Installing Gentoo Linux EFISTUB On ZFS

Initial notes
This guide is currently an incomplete work in progress. This guide when finished intends to detail installing Gentoo Linux on zfs without using grub bootloader

Install Gentoo Linux on OpenZFS using EFIStub Boot
Author: Michael Crawford (ali3nx) Contact: mcrawford@eliteitminds.com

Preface
This guide will show you how to install Gentoo Linux on AMD64 with:

* UEFI-GPT (EFI System Partition) - This will be on a FAT32 unencrypted partition as per UEFI Spec. * /, /home/username, on segregated ZFS datasets * /home, /usr, /var, /var/lib zfs dataset containers created for pool dataset structure * swap on regular partition * OpenZFS 0.8.3 * efistub boot without Grub * genkernel initramfs * systemd * Gentoo Stable (amd64)

Download the System Rescue CD + ZFS ISO
You will need to download Fearedbliss modified System Rescue CD that includes ZFS from one of the mirrors on his profile page.

Linux
We will be creating a UEFI Bootable USB since this guide will be showing you how to install Gentoo Linux on ZFS with UEFI Enabled.

For the following commands, we will assume that your USB is /dev/sdg.

Create the FAT32 Filesystem on the USB
We will now create the FAT32 filesystem on the USB. This needs to be FAT32 since this is the filesystem used in the UEFI Specification. The label we will use for this partition will be in the following format SYSRCDXYZ, where XYZ is the version number of the System Rescue CD you downloaded.

For example, if you are using System Rescue CD 6.0.7, the label will be SYSRCD607.

Copy files over from ISO to USB
And that's it! You now have a Bootable UEFI USB.

Windows
Balena Etcher is the USB Utility I recommend when on Windows. You can Download Balena Etcher here.


 * 1) Start Balena Etcher
 * 2) Select your USB Device from the Device drop down.
 * 3) Select your ISO by clicking SELECT.
 * 4) Click START.

This should be all that's necessary to have a Bootable UEFI USB.

Assumptions

 * Only installing Gentoo on one drive called /dev/sda (or /dev/nvme0n1, etc)
 * Fearedbliss System Rescue CD + ZFS iso is being used.
 * genkernel is being used as your initramfs.
 * gentoo-sources is being used as your kernel.

You are free to substitute any of the above for whatever you want if you know what you are doing (alternate kernels, initramfs generators, partition layouts (RAIDZ2?), etc). However, support will only be provided from me when the above configuration is used. Also, this guide is the way I install Gentoo, not exactly the way the handbook has it.

Boot your system into the ISO
Since this is highly computer dependent, you will need to figure out how to boot your USB on your system and get to the live environment. You may need to disable Secure Boot if that causes your USB to be rejected. Make sure your system BIOS/UEFI is set up to boot UEFI devices, rather than BIOS devices (Legacy).

Confirm that you booted in UEFI Mode
After you booted into the Live CD, make sure that you booted into UEFI mode by typing the following:

If the above directory is empty or doesn't exist, you are not in UEFI mode. Reboot and boot into UEFI mode.

Partition
We will now partition the drive and aim to create the following layout:

/dev/sda1  | 512 MB        |   EFI System Partition                | /efi /dev/sda2  | 32768 MB      |   swap                                | swap /dev/sda3  | Rest of Disk  |   ZFS                                 | /, /home/username ...

Open up your drive in GNU parted and tell it to use optimal alignment:

Create GPT partition layout
This will delete all partitions and create a new GPT table.

Larger swap will accommodate hibernation should that be desired. 32GB swap is used in the below example to accommodate many different hardware configurations.

Final View
Exit the application

Determine disk/by-id identifier
Using traditional block device identifiers such as /dev/sda or /dev/nvme0n1 with zfs can work but can also be undesirable due to the possibility of a block device name changing. Something as simple as connecting a usb storage device can cause this to occur.

Should this ever happen zfs pools are unaware of the change having occurred which can render a zfs pool inoperable. Use of non generic device specific disk identifiers which are also identified by disk serial number is more desirable for use with zfs as a result of this complication. This also provides added utility advantages for identifying a faulty disk in larger zfs pools.

To determine the non generic ata disk identifier id type the following

Nvme storage devices would resemble this example

Generally using /dev/disk/by-id/ata-disk or /dev/disk/by-id/nvme-disk is more desirable to ensure the disk block device is more specific. There may be /dev/disk/by-id/wmm or /dev/disk/by-id/nvme-eui. Use of these block device identifiers in the example below should be avoided if possible for use with this guide.

Create your zpool
Create your zpool which will contain your drives and datasets:

xattrs and posixacl are enabled to provide support for modern filesystem security features. Relative atime updates which are a global default in ext4 are enabled as well.

xattrs is necessary for proper functionality of systemd-journald

Substitute ata-disk1-part3 for nvme-disk1-part3 is you have an nvme ssd disk.

Create your rootfs zfs datasets
Create the dataset container structure and dataset necessary for /.

Create /usr, /var, /var/lib and /home zfs dataset containers
Creation of several unmounted dataset containers is necessary to provide dataset structure for the zfs pool. Creation of these containers after install is complete can be disruptive, involved and best completed before filesystem contents are written to disk to ensure the system will boot. Dataset containers for /usr and /var especially benefit from this having been completed in advance. This structures datasets within the pool for correct dataset segregation. The /var/lib dataset container is created to allow for easy creation of /var/lib/foo datasets for system or network services if desired at a later date.

rpool/home dataset container is created to segregate user home directory dataset contents from the rootfs dataset for improved rootfs dataset incremental snapshot size management to ensure that rootfs snapshots do not fill the available pool storage space.

Create user home directory dataset
Replace username with the desired user name

Verify everything looks good
You can verify that all of these things worked by running the following:

Now we are ready to install Gentoo!

Set your date and time
We use ntpdate to set accurate time and date and hardware clock to mitigate clock skew that can cause software compilation to malfunction

Preparing to chroot
First let's mount our efi boot partition in our chroot directory:

We'll use the Oregon State University Gentoo Linux mirror. If you desire use a different regional mirror from the official Gentoo Linux mirror list

Download the systemd amd64 stage3 system archive and extract it

Edit fstab
Use of disk UUID's to denote block devices entries in fstab has become the more desirable default to ensure an unpredicted block device alteration never renders a filesystem unmountable as a result of fstab becoming inaccurate. Something as simple as connecting a usb storage device to a booted system has been known to cause this to occur. The blkid command reveals these disk identifiers that are available for disk partitions created on gpt disk partition labels. Despite having created disk partition names disk UUID's are more specific.

Everything is on zfs so we don't need anything in here except for the boot and swap entries. fstab should resemble the following example. Substitute the provided UUID's from your blkid command:

Modify make.conf
Let's modify our /etc/portage/make.conf so we can start installing stuff with a good base (Change it to what you need):

Get the portage tree
Copy the default example portage config

Install required applications
Unmask latest ZFS versions

sys-fs/zfs and sys-fs/zfs-kmod must be installed after kernel configuration is complete

Now install the initial apps:

Reviewing the current gentoo-sources Linux kernel version
Gentoo provides eselect to manage many core system environment variables including the active /usr/src/linux symlink.

The command result of eselect should match the active linux kernel symlink

Necessary kernel configuration features
efistub relies on a key Linux kernel configuration feature to function

Installing the 'gentoo-sources' kernel binary
Install the kernel

Installing the bootloader onto your drive
We will need to configure the bootloader entry in uefi firmware to direct boot the linux kerrel and initramfs.

Edit the Linux kernel version to the desired current version used.

The above command will install the uefi bootloader entry in uefi firmware referencing the kernel and initramfs located at /efi/efi/gentoo.

Take a snapshot of your new system
Since we now have a working system, we will snapshot it in case we ever want to go back or recover files:

You can view the status of these snapshots using the zfs command

Limiting the ARC size
If you want to cap the ZFS ARC from growing past a certain point, you can put the number of bytes inside the /etc/modprobe.d/zfs.conf file, and then remake your initramfs. When the system starts up, and the module is loaded, these options will be passed to the zfs kernel module.

ARC cache memory usage will vary depending on zfs pool sizes. I've had a 50TB single vdev raidz2 pool consume 24GB of memory at system idle when unlimted. however zfs wll gneerally default to using 50% of available system memory for the ARC cache

(Temporary) Change the ARC max for the running system to 4 GB

(Permanent) Save the 4 GB ARC cap as a loadable kernel parameter

Once we have the above file created, let's regenerate the initramfs. genkernel will automatically detect that this file exists and copy it into the initramfs. When you reboot your machine, the initramfs will load up the zfs kernel module with the parameters found in the file.

zfs-auto-snapshot

 * Take snapshots of your pool
 * Automatically Clean the old snapshots on your pools.