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:

* /boot, /, and swap on ZFS * ZFS 0.6.5.2 * GPT (Partition Layout) * GRUB 2 (bootloader) * OpenRC (Init) * Gentoo Stable (amd64) * 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:

mkdir /tmp/iso mount -o loop,ro /tmp/iso /tmp/iso/usb_inst.sh

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.

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 = 500 MB      | ZFS Pool Version 28   | /boot Partition     | Code: BE00 /dev/sda2 = 4 MB        | GRUB 2 - GPT          | BIOS Boot Partition | Code: EF02 /dev/sda3 = Rest of Disk | ZFS Pool Version 5000 | /, /home, swap     | Code: BF00

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

/boot Partition

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

BIOS Boot Partition

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

/ 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         1026047   500.0 MiB   BE00  Solaris boot 2        1026048         1034239   4.0 MiB     EF02  BIOS boot partition 3        1034240      1953525134   931.0 GiB   BF00  Solaris root

Create /boot filesystem
Since GRUB 2 only has ZFS support for pool version 28, and we want our /boot to be on ZFS, we will make a separate pool that is version 28. Our / will be on another pool that is on a higher version that includes all of the new ZFS features.

zpool create -f -o version=28 -o ashift=12 -o cachefile= -m none -R /mnt/gentoo boot /dev/sda1

zpool export boot
 * 1) Export the pool since we will import it back in in a bit with /dev/disk/by-id tags

'''Do not upgrade your 'boot' pool above version 28, since this will make GRUB 2 not be able to read it. If you ever do, just backup the existing files in /boot, destroy the 'boot' pool, and recreate it back on version 28. Not a big deal and doesn't take much time, but just something to keep in mind.'''

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

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

Import your pools using /dev/disk/by-id
zpool import -R /mnt/gentoo -o cachefile= -d /dev/disk/by-id boot zpool import -R /mnt/gentoo -o cachefile= -d /dev/disk/by-id tank

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

zfs create tank/gentoo zfs create -o mountpoint=/ tank/gentoo/root zfs create -o mountpoint=/home tank/gentoo/home
 * 1) Create your / and /home datasets

zfs create -o mountpoint=/boot boot/gentoo
 * 1) Create your /boot dataset

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

zfs create -o sync=always -o primarycache=metadata -o secondarycache=none -b 4k -V 4G -o logbias=throughput tank/swap mkswap -f /dev/zvol/tank/swap swapon /dev/zvol/tank/swap


 * Currently, the performance of the swap zvol isn't that great. This should improve over time.
 * 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.

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

octopus ~ # zpool status pool: boot state: ONLINE status: The pool is formatted using a legacy on-disk format. The pool can still be used, but some features are unavailable. action: Upgrade the pool using 'zpool upgrade'. Once this is done, the pool will no longer be accessible on software that does not support feature flags. scan: none requested config:

NAME                                             STATE     READ WRITE CKSUM boot                                             ONLINE       0     0     0 ata-WDC_WD10EZEX-08M2NA0_WD-WCC3FFLXD4AS-part1 ONLINE       0     0     0 errors: No known data errors

pool: tank state: ONLINE scan: none requested config:

NAME                                             STATE     READ WRITE CKSUM tank                                             ONLINE       0     0     0 ata-WDC_WD10EZEX-08M2NA0_WD-WCC3FFLXD4AS-part3 ONLINE       0     0     0 errors: No known data errors

octopus ~ # zfs list NAME              USED  AVAIL  REFER  MOUNTPOINT boot             44.7M   163M   136K  none boot/gentoo      43.2M   163M  43.1M  /boot tank              880G  2.63T   272K  none tank/gentoo       875G  2.63T   272K  none tank/gentoo/home  807G  2.63T   668G  /home tank/gentoo/root 68.0G  2.63T  31.7G  / tank/swap        4.25G  2.63T  20.3M  -

Now we are ready to install Gentoo!

Check and set your time
date

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:

date 082414482014

'''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 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

Copy network settings
cp /etc/resolv.conf etc/

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

Chroot into your environment
env -i HOME=/root TERM=$TERM chroot. bash -l

Create mtab symlink
ln -sf /proc/self/mounts /etc/mtab

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

Set hostname
nano /etc/conf.d/hostname

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

nano /etc/fstab

/dev/zvol/tank/swap    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"

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" FEATURES="buildpkg" LINGUAS="en en_US" VIDEO_CARDS="intel" INPUT_DEVICES="evdev synaptics"

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.

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
Unmask the latest version of GRUB (and enable zfs support): echo "sys-boot/grub ~amd64" >> /etc/portage/package.accept_keywords 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:

grub2-install /dev/sda

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 part_gpt insmod zfs

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

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

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 boot/gentoo@install zfs snapshot tank/gentoo/root@install zfs snapshot tank/gentoo/home@install

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

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

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!