User:Fearedbliss/Installing Gentoo Linux On ZFS

Install Gentoo Linux on ZFS
Author: Jonathan Vasquez (fearedbliss) Contact: jvasquez1011@gmail.com

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

* / on ZFS * Separate /boot and swap * GPT (Partition Layout) * Extlinux (Bootloader) * OpenRC (Init) * Gentoo Stable (amd64) * Portage 2.2.18 (So we can use the new repository management) * Precompiled Binary Packages

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, bliss-boot).

Also,
 * This guide is the way I install Gentoo, not exactly the way the handbook has it
 * I only run Gentoo Stable (with select applications coming from ~amd64).

Optional: Installing over SSH
If you want to be able to copy links easier into your terminal (I use PuTTY on Windows), you could set the password on your laptop, get the ip, and then connect to your laptop over the network and perform most of the install through it. passwd [Enter your new password]

ifconfig [Retrieve the IP for this machine]

screen

Then go to your other machine where you will be installing from, log in, and then re-attach to your screen: screen -rd Using screen is optional but convenient.

Wipe Partition Layout
This will delete all partitions and their respective MBR/GPT data structures. No confirmation or prompt will be given.

sgdisk -Z /dev/sda

Create your partitions
gdisk /dev/sda

Once gdisk is opened, use the following layout:

/dev/sda1 = 250 MB | ext2 /boot Partition (code: 8300) /dev/sda2 = 32 MB | BIOS Boot Partition (code: EF02) /dev/sda3 = 4 GB  | swap Partition (code: 8200) /dev/sda4 = Rest of Disk | ZFS Pool (Will have / and everything else) (code: bf00)

The "->" means to just press enter and accept the default.

/boot Partition

Command: n Partition number: -> First sector: -> Last sector: +250M Hex code or GUID: ->

BIOS Boot Partition (only needed in case you want GRUB 2 in the future)

Command: n Partition number: -> First sector: -> Last sector: +32M Hex code or GUID: EF02

swap Partition

Command: n Partition number: -> First sector: -> Last sector: +4G Hex code or GUID: 8200

/ Partition

Command: n Partition number: -> First sector: -> Last sector: -> Hex code or GUID: bf00

Command: p

Number Start (sector)    End (sector)  Size       Code  Name 1           2048          514047   250.0 MiB   8300  Linux filesystem 2         514048          579583   32.0 MiB    EF02  BIOS boot partition 3         579584         8968191   4.0 GiB     8200  Linux swap 4        8968192       209715166   95.7 GiB    BF00  Solaris root

'''You should have a swap even if it is small. Your system could lock up under high load, especially since ZFS uses your physical RAM for its ARC.'''

Enable Legacy BIOS Bootable Flag
In order for extlinux to boot your /boot partition, you will need to toggle the legacy BIOS bootable flag since we are using the GPT layout.

Command: x Expert Command: a Partition: 1 Toggle which attribute field: 2 Expert Command: w Do you want to proceed? Y

Create /boot
mkfs.ext2 -m1 /dev/sda1

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

zpool create -f -o ashift=12 -o cachefile= -O atime=off -O compression=lz4 -m none -R /mnt/gentoo tank /dev/sda4

Create your zfs datasets
We will keep it simple and just create two datasets, one for your root filesystem, and one for your home. ZFS is extremely flexible and you can easily shrink or extend it in the future.

zfs create tank/gentoo zfs create -o mountpoint=/ tank/gentoo/root zfs create -o mountpoint=/home tank/gentoo/home

You can verify that all of these things worked by running the following:

zpool status zfs list

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:

08 24 1448 2014 (Month, Date, 24 Hour Time, Year). It will be UTC at the moment but we will fix it once we are inside our environment.

Preparing to chroot
cd /mnt/gentoo mkdir boot mount /dev/sda1 boot wget tar xpf

If you don't have a stage 3 for your architecture already downloaded, then click here.

Copy zpool cache
mkdir etc/zfs cp /etc/zfs/zpool.cache etc/zfs

Mount required devices
mount --rbind /dev dev mount --rbind /proc proc mount --rbind /sys sys

Fix your date
I'm in New York so I will do the following:

ln -sf /usr/share/zoneinfo/America/New_York /etc/localtime date 082412542014

Generate locales
nano /etc/locale.gen (I uncommented the en_US ISO/UTF-8 lines) locale-gen

Edit fstab
I removed the root, floppy, and cdrom lines. Our root is on zfs so we don't need it to be here. Our swap is /dev/sda3, and our boot is /dev/sda1. My fstab looks as follows: /dev/sda1              /boot           ext2            noatime         0 0 /dev/sda3              none            swap            sw              0 0

Modify make.conf
Let's modify our /etc/portage/make.conf so we can start installing stuff with a good base:

nano /etc/portage/make.conf

Example, change it to what you need:

ACCEPT_LICENSE="*"

CPU_FLAGS_X86="mmx sse sse2" USE="$CPU_FLAGS_X86 -branding"

SYNC="rsync://xyinn.org/portage" GENTOO_SOURCES="http://xyinn.org/gentoo/ http://mirrors.rit.edu/gentoo/" PORTAGE_BINHOST="http://xyinn.org/gentoo/packages"

MAKEOPTS="-j5" EMERGE_DEFAULT_OPTS="--jobs=2 --load-average=2 --with-bdeps=y --quiet-build=y" LINGUAS="en en_US" VIDEO_CARDS="intel" INPUT_DEVICES="evdev synaptics"

Get your portage tree
We will first download the portage tree using the current version of portage that is on the stage3, afterwards, we will migrate to the new portage so that we can use its new repository management features. This allows us to pull the bliss-overlay git repository and integrate it directly into portage so that when we do an 'emerge --sync', our overlay also gets updated along the portage tree.

emerge --sync

Compile git, migrate to new portage, and setup bliss-overlay
echo "=sys-apps/portage-2.2.18 ~amd64" >> /etc/portage/package.accept_keywords emerge -1au portage emerge -a git

mkdir /etc/portage/repos.conf/ cp /usr/share/portage/config/repos.conf /etc/portage/repos.conf/gentoo.conf
 * 1) Now set up the new portage layout
 * 2) (Need to remove SYNC from make.conf and set up the repos.conf directory and our overlay):

nano /etc/portage/make.conf
 * 1) Open make.conf and remove the SYNC line (Keep note of the value of this variable)

nano /etc/portage/repos.conf/gentoo.conf sync-uri = rsync://xyinn.org/portage nano /etc/portage/repos.conf/bliss-overlay.conf
 * 1) Open gentoo.conf and switch the sync-uri to what we had before in our SYNC line
 * 1) Make a config for bliss-overlay. The following is what I'm using:

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

echo 'PORTDIR_OVERLAY="/var/lib/overlays/bliss-overlay ${PORTDIR_OVERLAY}"' > /var/lib/overlays/base.conf
 * 1) Finally connect this directory to portage so portage can see it. We will simulate something similar to layman.

nano /etc/portage/make.conf
 * 1) Open up make.conf and add 'source' to the end of the file:

source /var/lib/overlays/base.conf

emerge --sync
 * 1) Finally, test out that rsync and git are both working.
 * 2) You should see bliss-overlay being cloned into your system.

Install required applications
Let's use ~amd64 syslinux:

Now install the apps:

''' 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-3.14.26, we would do:

emerge --noreplace =bliss-kernel-3.14.26 '''

Update your system
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 from source a lot less packages.

Installing the bootloader onto your drive
We will need to install the bootloader, copy some files so we can get a menu, and also flash the firmware that will allow us to boot from gpt:

mkdir /boot/extlinux cd /boot/extlinux extlinux --install. cp /usr/share/syslinux/{libutil.c32,menu.c32}. dd if=/usr/share/syslinux/gptmbr.bin of=/dev/sda

Configure bliss-boot
Navigate to the "Kernels & Options" section and modify the default entry that's there to look like the following (or similar depending your situation):

And switch the bootloader to extlinux so that bliss-boot generates that config type:

Generate the bootloader config
''' Check your extlinux.conf to make sure that it has the correct information! '''

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

If all went well, extlinux should now be installed and configured.

Final steps before reboot
emerge -ag dhcpcd syslog-ng

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

passwd exit reboot

Ensuring we have our /boot directory files inside our zfs snapshots
Since our /boot directory is not on zfs, any zfs snapshots we take will not contain the files in this directory (kernel, initramfs, bootloader config, etc). This is pretty important considering one of the reasons we've put our rootfs on zfs is so that in the case of a system failure, we could easily rollback to a good state.

What we can do is use the program 'lsyncd' so that it monitors our /boot directory and whenever a file gets added, removed, changed, it will update another location (/boot2). This /boot2 directory will reside on the zfs pool itself, which means our snapshots will contain this folder.

I've added the openrc script and an example config file (Which happens to be a /boot -> /boot2 example) to my overlay (ebuild was forked from Gentoo's portage tree).

emerge -a lsyncd::bliss-overlay rc-config start lsyncd rc-update add lsyncd default

If you check your / directory, you should see that you now have a /boot and /boot2 directory, with /boot2 just mirroring the files that are on /boot.

octopus bliss-overlay # ls /boot* /boot: extlinux kernels  lost+found

/boot2: extlinux kernels  lost+found

Take a snapshot of your new system
We will take a snapshot with zfs of the system in this state in case we ever want to rollback to it:

zfs snapshot tank/gentoo/root@initialInstall zfs snapshot tank/gentoo/home@initialInstall

Bliss ZFS Scripts
The following scripts allow you to automatically take snapshots of your pool, automatically replicate the pool to another pool (Full and Incremental Backups), and 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!