Skip to content

Instantly share code, notes, and snippets.

@CodeSigils
Last active August 19, 2022 01:45
Show Gist options
  • Save CodeSigils/b3079626a5b32cd463dd09d853a820e1 to your computer and use it in GitHub Desktop.
Save CodeSigils/b3079626a5b32cd463dd09d853a820e1 to your computer and use it in GitHub Desktop.
Artix - ZFS - OpenRc and Refind bootloader
#!/usr/bin/env bash
## TITLE: Install and setup basic Artix - ZFS - OpenRc - rEFInd
## TODO: Make proper install scripts from this gist
## EFI SETUP: https://wiki.archlinux.org/index.php/EFI_system_partition
## ARTIX: https://wiki.artixlinux.org/Main/InstallationOnZFS
## REFIND: https://www.rodsbooks.com/refind/installing.html#linux
## INSTALL: https://wiki.artixlinux.org/Main/Installation
## ARCHZFS: https://www.youtube.com/watch?v=kPNcRSSaYQo
## TODO
echo '=============================================='
echo ' 1.0 Setup partitions '
echo '=============================================='
## Setup Partition schema must be as following
## In this example
## /dev/sda1 /boot (must be vfat for refind)
## /dev/sda2 /
## Check for an existing partition
fdisk -l /dev/sda
## Format EFI partition as Fat
## If you get the message WARNING: Not enough clusters for a 32 bit FAT!,
## reduce cluster size with mkfs.fat -s2 -F32 ... or -s1;
## otherwise the partition may be unreadable by UEFI.
mkfs.fat -F 32 /dev/sda1 &&
fatlabel /dev/sda1 BOOT
## confirm with
fdisk -l
## Define type of root as "Solaris root" type (55 or 66 in fdisk)
## A schema that works well with ZFS and grub is the following:
## Device Start End Sectors Size Type
## /dev/sda1 2048 1048576 1046529 2M BIOS boot
## /dev/sda2 1050624 2099199 1048576 512M Linux filesystem
## /dev/sda3 2099200 58720222 56621023 27G Solaris root
echo '=============================================='
echo ' 1.1 Install and load ZFS module in live env '
echo ' This will also need to be done inside chroot '
echo '=============================================='
## Add zfs arch repo to /etc/pacman.conf
tee -a /etc/pacman.conf <<EOF
## ZFS repo
[archzfs-testing]
Server = https://archzfs.com/\$repo/\$arch
Server = Optional TrustAll
EOF
## Get the gpg keys and finish installation
curl https://archzfs.com/archzfs.gpg > archzfs.gpg &&
pacman-key -a archzfs.gpg &&
pacman-key --init &&
pacman-key --populate artix &&
pacman-key -r F75D9D76 &&
pacman-key --lsign-key F75D9D76
## Install and load the zfs module
## Dependencies: linux-headers || linux-lts-headers
##|| linux-zen-headers || linux-hardened-headers
pacman -Sy --noconfirm zfs-dkms && modprobe zfs
## Check result
echo $?
echo '============================================='
echo ' 1.2 Install helpers '
echo '============================================='
pacman -Sy --noconfirm neovim parted
echo '============================================='
echo ' 1.3 ZFS Pool and Datasets creation '
echo '============================================='
## 1.3a Create the ZFS pool, named zroot.
## This is for a standard single pool, no mirror or RAID
## Name your pool zroot or rpool
## Example 1: from https://wiki.archlinux.org/index.php/Install_Arch_Linux_on_ZFS
## with compression and native encryption
#zpool create -f -o ashift=12 \
# -O acltype=posixacl \
# -O relatime=on \
# -O atime=off \
# -O xattr=sa \
# -O dnodesize=legacy \
# -O normalization=formD \
# -O mountpoint=none \
# -O canmount=off \
# -O devices=off \
# -O compression=lz4 \
# -O encryption=aes-256-gcm \
# -R /mnt \
# rpool /dev/disk/by-partuuid/some_disk_UUid
## Example 2: From Artix wiki. This is not as robust as the above.
## When reboot ZSF may not find disks if not defined with UUIDs.
#zpool create -o ashift=12 rpool /dev/sda2
#zfs set atime=off rpool
#zfs set relatime=on rpool
## Verify zpool creation. Must result in something like the following:
## NAME SIZE ALLOC FREE CKPOINT EXPANDSZ FRAG CAP DEDUP HEALTH ALTROOT
## rpool 26.5G 396K 26.5G - - 0% 0% 1.00x ONLINE /mnt
zpool list
## 1.3b Create a Dataset layout under the pool.
## Note that ZFS datasets can be customized later on.
## Example 1: From Artix wiki
#zfs create -o compression=lz4 -o mountpoint=none rpool/ROOT
#zfs create -o compression=lz4 -o mountpoint=/ rpool/ROOT/default
#zfs create -o compression=lz4 -o mountpoint=none rpool/HOME
#zfs create -o compression=lz4 -o mountpoint=/home/{USERNAME} rpool/HOME/{USERNAME}
## Example 2: From arch wiki
## Data that don't belong to the OS snapshot
zfs create -o mountpoint=none rpool/data
## The primary OS
zfs create -o compression=lz4 -o mountpoint=none rpool/ROOT
## The actual boot point that can we snapshot to rool back
zfs create -o mountpoint=/ rpool/ROOT/default
zfs create -o mountpoint=/home rpool/data/home
zfs create -o mountpoint=/root rpool/data/home/root
## Confirm folder creation by ZFS. Those folders are mounted:
ls /mnt
## home root
## Unmount them and remove them:
zfs umount -a && rm -rf /mnt/*
## List zfs file systems
zfs list
# NAME USED AVAIL REFER MOUNTPOINT
# rpool 1.31M 25.7G 96K none
# rpool/ROOT 192K 25.7G 96K none
# rpool/ROOT/default 96K 25.7G 96K /mnt
# rpool/data 288K 25.7G 96K none
# rpool/data/home 192K 25.7G 96K /mnt/home
# rpool/data/home/root 96K 25.7G 96K /mnt/root
echo '=================================================='
echo ' 1.4 zpool import cache file and bootloader setup '
echo '=================================================='
## Tell the bootloader which pool to import/use
zpool set bootfs=rpool/ROOT/default rpool &&
## Create a cache file
zpool set cachefile=/etc/zfs/zpool.cache rpool &&
## export (detach) the pool so that we can import it later.
## Everytime we reboot the pool will be imported so we need
## to check if it works without issue:
zpool export rpool &&
## From Artix wiki: Import the pool, prior to installation.
## This mounts the pool to the Artix installation location.
#zpool import -d /dev/sda2 -R /mnt rpool
## OR import the pool without mounting (-N) any of the filesystems yet
## in order to mount them in a selected order
zpool import -d /dev/disk/by-partuuid -R /mnt rpool -N &&
## First mount default then everything else
zfs mount rpool/ROOT/default
zfs mount -a
## Create the boot directory and mount partition
mkdir /mnt/boot &&
mount /dev/whatever_boot_partition /mnt/boot
echo '============================================='
echo ' 2.0 Install base and select init system '
echo '============================================='
## OpenRc init
basestrap /mnt base base-devel openrc
## Runit init
# basestrap /mnt base base-devel runit elogind-runit
## S6 init
# basestrap /mnt base base-devel s6 elogind-s6
## If you encounter errors, you can use the -i flag
## of basestrap ('interactive'). Example:
# basestrap -i /mnt base
echo '================================================'
echo ' 2.1 Install linux Kernel, firmware and headers '
echo ' Install and load ZFS module into chroot '
echo '================================================'
## Now our main fs is created in /mnt
## Add zfs arch repo to /mnt/etc/pacman.conf
tee -a /mnt/etc/pacman.conf <<EOF
## ZFS repo
[archzfs-testing]
Server = https://archzfs.com/\$repo/\$arch
Server = Optional TrustAll
EOF
## Get the gpg keys and finish installation
curl https://archzfs.com/archzfs.gpg > archzfs.gpg &&
pacman-key -a archzfs.gpg &&
pacman-key --init &&
pacman-key --populate artix &&
pacman-key -r F75D9D76 &&
pacman-key --lsign-key F75D9D76
## Install a Kernel then install and load the zfs module
## Note: Artix provides two kernels linux-lts and linux, but you
## can use any other kernel you like ('-ck, -pf, -zen' etc).
## While optional, it is recommended to install linux-firmware.
## You can try not installing it, but some devices such as network
## cards may not work.
basestrap /mnt linux linux-headers linux-firmware zfs-dkms &&
modprobe zfs
## Generate fstab
## Then edit and verify, also set root, swap, home and etc..
## Note: -U stands for UUID and -L for Label
fstabgen -U /mnt >> /mnt/etc/fstab
## Check the resulting fstab for errors before rebooting.
## Now, you can chroot into your new Artix system with:
artools-chroot /mnt
echo '==============================================='
echo ' 2.2 CHROOT - Configure base system '
echo '==============================================='
## NOTE: The following method for creating and activating
## a swap file does NOT work on ZFS. Use a dedicated ext4
## partition or use Zswap
## https://askubuntu.com/questions/1198903/can-not-use-swap-file-on-zfs-files-with-holes
#fallocate -l 2GB /swapfile &&
#zfs create rpool/swap -o compression=off -o mountpoint=/swap/ &&
#chmod 600 /swapfile &&
#mkswap /swapfile &&
#swapon /swapfile &&
#echo "/swapfile none swap deafults 0 0" | tee -a /etc/fstab
## Lang, clock and network setup
ln -sf /usr/share/zoneinfo/Europe/Athens /etc/localtime &&
hwclock --systohc &&
echo "en_US.UTF-8 UTF-8" >> /etc/locale.gen &&
locale-gen &&
echo "export LANG=en_US.UTF-8" >> /etc/locale.conf &&
echo 'export LC_COLLATE="C"' >> /etc/locale.conf &&
. /etc/locale.conf
echo '========================================================='
echo ' 2.3 ZFS OpenRc init activation - Rebuild Initramfs image'
echo '========================================================='
## Set host id and cache file. If skip this system will fail to boot
zgenhostid $(hostid)
zpool set cachefile=/etc/zfs/zpool.cache rpool
## With OpenRc
tee -a /etc/init.d/zfs << EOF
#!/sbin/openrc-run
command="zfs mount -a"
EOF
chmod +x /etc/init.d/zfs && rc-update add zfs default
## Edit /etc/mkinitcpio.conf and change the hook to look like this:
HOOKS=(base udev autodetect modconf block keyboard zfs filesystems)
## Then rebuild image
mkinitcpio -p linux-lts ## (or linux, zen ... whatever kernel flavor)
echo '=============================================='
echo ' 2.4 Refind boot manager installation & setup '
echo ' https://www.youtube.com/watch?v=Onw4Q0ejKvw '
echo '=============================================='
## Install refind bootloader and Fat fs tools
pacman -S --noconfirm refind-efi \
efibootmgr \
dialog \
mtools \
dosfstools \
openssl \
openssh
## Install refind to doot partition, in this case /dev/sda1
## Then generate config file
refind-install --usedefault /dev/sda1 --alldrivers && mkrlconf
## Configure /boot/refind_linux.conf
mv /boot/refind_linux.conf{,.bk} &&
echo '"Boot Artix Linux" "root=ZFS=bootfs rw"' | tee -a refind_linux.conf &&
## Search for arch linux and replace "root=PARTUUID=5028fa.... rw add_efi_memmap"
## with our EFI partition: "root=/dev/sda1 rw add_efi_memmap"
cd /boot/EFI/BOOT/ &&
vim refind.conf
echo '============================================='
echo ' 2.5 Users and Network configuration '
echo '============================================='
## 2.51 Set root password
passwd &&
## Create a wheel user (i.e: sand) and set password
useradd -mG wheel sand &&
passwd sand &&
sed -i '/wheel ALL=(ALL) ALL/s/^#//g' /etc/sudoers
## 2.52 Basic network configuration
echo "sandbox" >> /etc/hostname &&
tee -a /etc/hosts << EOF
127.0.0.1 localhost
::1 localhost
127.0.1.1 sandbox.localdomain sandbox
## Blocked domains example
0.0.0.0 incoming.telemetry.mozilla.org
0.0.0.0 toolkit.telemetry.server.org
EOF
## TODO
## Get the exact name of your interface
ip -s link
## Add config_<interface>="dhcp"
## $EDITOR /etc/conf.d/net
sed -i '/config_eth0="dhcp"/s/^#//g' /etc/conf.d/net
## should be symlinked to create additional
## scripts for each network interface and
## then loaded into an openrc runlevel.
##ln -s /etc/init.d/net.lo /etc/init.d/net.<interface> &&
##rc-update add net.<interface> default
ln -s /etc/init.d/net.lo /etc/init.d/net.eth0 &&
rc-update add net.eht0 default
## 2.53 Alternatively install Network tools
## Install connman-gtk or cmst for Qt-based DEs
#pacman -S connman-openrc \
#connman-gtk \
## Start connmand
#rc-update add connmand
## 2.54 Exit chroot, cleanup and reboot
exit
umount -R /mnt
reboot
echo '============================================='
echo ' 3.0 Post installation '
echo '============================================='
## 3.1 Setup Video drivers and install xorg
sudo pacman -S xf86-video-qxl ## For Virt
#sudo pacman -S xf86-video-intel ## For Intel
#sudo pacman -S xf86-video-amdgpu ## For AMD
#suod apcman -S nvidia nvidia-utils ## For NVidia
## Xorg server
sudo pacman -S xorg
# TODO
## Display Manager
@CodeSigils
Copy link
Author

CodeSigils commented Dec 17, 2020

Install and setup basic Artix Linux with ZFS - OpenRc - rEFInd. This file can help with manual installation.
Sources:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment