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

* UEFI/GPT (/boot will be on a FAT32 Unencrypted EFI System Partition) * Everything else on ZFS (/, swap - With all feature flags enabled and lz4 compression) * ZFS 0.6.5.8 * GRUB 2 (Bootloader) * systemd (Init) * Gentoo Stable (amd64) * Git-based Portage

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

Assumptions

 * Only installing Gentoo on one drive called /dev/sda.
 * You are using my modified System Rescue CD that includes ZFS
 * Will be using my tools (bliss-kernel, bliss-initramfs).

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  | 500 MB       |   EFI System Partition   | /boot /dev/sda3  | Rest of Disk |   ZFS Pool               | /, 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 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:

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

Configure Portage, get the Portage Tree, and setup bliss-overlay
We will now configure portage, download the Portage Tree, and setup "bliss-overlay" through it. This means that you will be able to receive all bliss-overlay updates through portage.

Since our livecd and chroot don't have git, we will download a github snapshot that contains the .git repository as well. Normally if we download this from github directly, it won't include .git directory, which would require us to re-download all the files again after we compiled git in order to get updates via git. If we do this, we can avoid this second download.

wget http://xyinn.org:8080/gentoo/portage/portage-snapshot.tar.xz tar xf portage-snapshot.tar.xz -C /usr/
 * 1) Bootstrap the portage tree

cp /usr/share/portage/config/repos.conf /etc/portage/repos.conf
 * 1) Copy the example portage config so we can modify it

nano /etc/portage/repos.conf
 * 1) Open repos.conf and switch the following values for the [gentoo] entry to the following:

sync-type = git sync-uri = https://github.com/fearedbliss/gentoo.git
 * 1) Add the following entry to the same file:

[bliss-overlay] location = /var/lib/overlays/bliss-overlay sync-type = git sync-uri = https://github.com/fearedbliss/bliss-overlay.git auto-sync = yes

emerge dev-vcs/git
 * 1) Compile git

emerge --sync
 * 1) Sync once more to get bliss-overlay

Install required applications
Enable zfs support in grub: echo "sys-boot/grub libzfs" >> /etc/portage/package.use

Unmask the latest versions of zfs: echo "sys-kernel/spl ~amd64" >> /etc/portage/package.accept_keywords echo "sys-fs/zfs ~amd64" >> /etc/portage/package.accept_keywords echo "sys-fs/zfs-kmod ~amd64" >> /etc/portage/package.accept_keywords

Now install the apps: emerge -ag bliss-kernel bliss-initramfs grub

'''Consider pinning the kernel version that was installed so that when we do a future kernel upgrade, an emerge --depclean won't delete our kernel files from the /boot directory. If the bliss-kernel ebuild that emerge pulled to install was: bliss-kernel-4.1.8, we would do:'''

emerge --noreplace =bliss-kernel-4.1.8

Update your system
emerge -uDNavg @world

'''The "g" option in the first emerge command will attempt to pull as many compatible binary packages from my server. This means you will have to compile a lot less packages. '''

Installing the bootloader onto your drive
We will need to install the bootloader. Before we do that, let's see if GRUB can detect our /'s filesystem type: grub2-probe /

It should say 'zfs'. 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: grub2-install /dev/sda

The above command should return a "Installation finished. No error reported." message. If it doesn't, then something is wrong and your system will not boot!

'''Do not upgrade your pool above version 0.6.5 if that newer version contains newer feature flags that GRUB 2 doesn't yet support. If you do, your system will become unbootable.'''

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

nano /boot/grub/grub.cfg

set timeout=1 set default=0
 * 1) Place the following inside the grub.cfg file

insmod zfs

menuentry "Gentoo - 4.1.8-FC.01" { linux /gentoo/root/@/boot/kernels/4.1.8-FC.01/vmlinuz root=tank/gentoo/root by=id elevator=noop quiet refresh initrd /gentoo/root/@/boot/kernels/4.1.8-FC.01/initrd }

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 cachefile, and make a new one that is up-to-date. Once we do that, we can remake our initramfs file so that we have the latest correct information.

Generate the initramfs and move the file to its correct location
bliss-initramfs 1 4.1.8-FC.01 mv initrd-4.1.8-FC.01 /boot/kernels/4.1.8-FC.01/initrd

Final steps before reboot
emerge -ag dhcpcd syslog-ng

rc-update add zfs-mount boot rc-update add dhcpcd default rc-update add sshd default rc-update add syslog-ng default

passwd exit reboot

Remove zpool.cache refresh flag from bootloader configuration and remake initramfs
open '/boot/grub/grub.cfg' and remove the 'refresh' flag linux .... by=id elevator=noop quiet

bliss-initramfs 1 4.1.8-FC.01 mv initrd-4.1.8-FC.01 /boot/kernels/4.1.8-FC.01/initrd

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:

zfs snapshot tank/gentoo/root@2016-03-25-0000-01-INSTALL zfs snapshot tank/gentoo/home@2016-03-25-0000-01-INSTALL

You can view the contents of these snapshots by checking their respective and hidden .zfs directories:

ls /.zfs/snapshots ls /home/.zfs/snapshots

NOTE: We are naming the snapshots in this format because it's the format that my 'bliss-zfs-scripts' use, and it is also very readable.

Tools in Portage
emerge -ag sudo htop gentoolkit eix app-misc/screen app-text/tree

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!