User:Fearedbliss/Installing Gentoo Linux On ZFS

Install Gentoo Linux on ZFS
Author: Jonathan Vasquez (fearedbliss) Contact: fearedbliss@gentoo.org

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

* UEFI/GPT (EFI System Partition (ESP) will be on a FAT32 unencrypted partition - as per UEFI specifications) * Everything on ZFS (/, /home, /boot, swap) * ZFS 0.6.5.9 * GRUB 2 (Bootloader) * OpenRC * Gentoo Stable (amd64)

Notes on other stuff you can do in the ZFS install can be found here.

Required Tools
Download my modified System Rescue CD that includes ZFS from one of the mirrors on the front page.

Linux
If you have a Linux system, you can create a bootable usb from it by doing the following:

That should open up a screen where you can select your plugged in flash drive.

Windows
You can download "Rufus" and select the ISO by clicking the little image next to the "Create a bootable disk using <>". After that, you can use the default settings and press "Start".

Download Rufus

macOS
If you have a macOS system, you can create a bootable usb by doing the following:


 * 1) Insert the USB device.
 * 2) Using Disk Utility, format the USB device. Make sure to set the file system to FAT.
 * 3) Mount the ISO. Both the freshly formatted USB and the ISO should appear mounted under /Volumes
 * 4) From a terminal, run: cp -R /Volumes/sysresccd_zfs /Volumes/ (Chances are your USB drive name is UNTITLED.)
 * 5) Unmount the USB using Finder or the command line diskutil application.
 * 6) Reboot, holding down Left-Option when you hear the chime. Release after a few seconds.
 * 7) You should see a removable device you can now boot from. After selecting, it should load GRUB.

Assumptions

 * Only installing Gentoo on one drive called /dev/sda.
 * You are using my modified System Rescue CD that includes ZFS
 * bliss-initramfs and bliss-kernel are used. You can also use genkernel and your own custom compiled kernel.

Also, this guide is the way I install Gentoo, not exactly the way the handbook has it.

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

/dev/sda1  | 2 MB         |   BIOS Boot Partition                   | So GRUB 2 can work in GPT /dev/sda2  | 100 MB       |   EFI System Partition                  | /boot/efi /dev/sda3  | 500 MB       |   Boot Partition (ZFS Pool Version 28)  | /boot /dev/sda4  | Rest of Disk |   ZFS (Pool Version 5000)               | /, swap, etc

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.

Final View
Exit the application

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

Create your zfs datasets
We will keep it simple and just create a few datasets for /, and /home. ZFS is extremely flexible and you can easily add or remove datasets in the future.

Create your boot pool
It is safer for us to create a separate zpool that has all feature flags disabled. This is because even though grub currently supports the latest feature flags for the 0.6.5 release of ZFS, new feature flags added to ZFS without proper bootloader support can make your system unbootable. Since GRUB 2 has solid support for a zpool with no feature flags, we will create a separate featureless boot pool. However, our main system will still have all features enabled.

Create your swap zvol
This creates the zfs block device that we will use for swap.

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

Now we are ready to install Gentoo!

Check and set your time
If it's wrong then fix it. Let's say it's August 24, 2014 @ 2:48 PM (will be 14:48 in 24 hour time), we would do the following:

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

and now download the amd64 image here and extract it:

Generate locales
I'm using US English w/ UTF-8, so I will enable that locale on my system:

Fix your date
I'm in New York and the time currently is August 24, 2014 @ 2:48 PM (will be 14:48 in 24 hour time)

Set hostname
Open up /etc/conf.d/hostname with your text editor and add your desired hostname in there.

For example, my /etc/conf.d/hostname file could be:

hostname="bliss"

Edit fstab
Everything is on zfs so we don't need anything in here except for the boot and swap zvol entries. My fstab looks as follows:

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

Using the 'bliss-kernel' configuration as a base
If you don't want to use the pre-compiled "bliss-kernel" in "bliss-overlay" below, I've made available the 'bliss-kernel' configs so that you can compile the kernel yourself using that configuration as a base. The configuration is ready to go for general desktop, laptop, and server use, and it is also configured to work with ZFS.

You can download the 'bliss-kernel' configuration here.

Using your own configuration
If you are not going to use the above configuration, then you will need to make sure your kernel has the following options enabled or ZFS will not work:

Disable anything marked [ ], Compile in [*], Options marked [M] may either be separate modules or compiled into the kernel:

(Optional) Add bliss-overlay and install a pre-compiled/configured ZFS-ready kernel
bliss-kernel is a vanilla kernel (directly from kernel.org, no patches) that fearedbliss maintains. It uses a slightly tweaked fedora config. The kernel is pre-configured and tested for ZFS. Use this if you don't want to spend time tweaking/debugging the Linux Kernel.

Open repos.conf and add bliss-overlay

Compile git

Sync once more to get bliss-overlay

Install the kernel

Install required applications
Enable the AMD64 EFI platform for grub

Unmask the latest versions of zfs

Enable ZFS support in GRUB

Now install the apps:

Installing the bootloader onto your drive
We will need to install the bootloader onto the drive. Before we do that however, let's see if GRUB can detect our /boot and /boot/efi filesystem types:

This should say 'zfs'. If it doesn't, then something is wrong and your system will not boot!

This should say 'fat'. If it doesn't, then something is wrong and your system will not boot!

Now run the following to install the bootloader to the drive:

The above command will install the grub bootloader files into /boot and the efi files into /boot/efi. It should return a "Installation finished. No error reported." message. If it doesn't, then something is wrong and your system will not boot!

Make the GRUB 2 configuration file
You can use the following configuration file as a basis for your system:

Generating new zpool.cache file before/after reboot
ZFS is very sensitive about the data that is contained in the zpool.cache file and at this point, when we reboot, the information in it might not be completely accurate. To ensure we have a good cache file, we have instructed bliss-initramfs in the bootloader config above, to ignore the current cachefile on the system, and make a new one that is up-to-date. We only need to do this once.

Remove zpool.cache refresh flag from bootloader configuration
Open up your grub.cfg and remove the 'refresh' flag from the kernel line.

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 contents of these snapshots by checking their respective and hidden .zfs directories:

Bliss ZFS Scripts
The following scripts allow you to automatically:


 * Take snapshots of your pool
 * Replicate the pool to another pool (Full and Incremental Backups)
 * Clean the old snapshots on your pools.

You can download, customize, and install the scripts into your /usr/local/sbin directory. Github.

And that's it. Enjoy!