User:LenHuppe/ZFS on Archiso/

From ArchWiki
Warning: Please be sure to see the Arch ISO Wiki before removing the working directory.
Tip: This script is intended as a starting point for anyone needing to create an Arch ISO with OpenZFS and the LTS kernel. It is something that I devised in a pinch with help from the community. If you have never created an Arch ISO it is strongly recommended that you read and understand Arch ISO first.

Create a ZFS Archiso

Note: Creating an Arch ISO with OpenZFS is a straight forward process but there are some important considerations.
  • OpenZFS is hosted by an unofficial user repository which means using a custom repo to create an Arch Linux ISO with OpenZFS. The repo I use is archzfsfollowing the instructions here. Also the machine I build on has the archzfs repo enabled so I have not had to address the repo key in my script.
  • Linux and OpenZFS version at a rapid pace and current releases are sometimes incompatible. This procedure takes advantage of the LTS kernel which is supported in new OpenZFS releses by default, and the DKMS package option. Information about OpenZFS package options is available in the ZFS wiki and in the Git page.
  • Creating a standard Arch Linux ISO is covered in Arch ISO. This procedure requires us to navigate through the standard build directory tree and change a number of configuration files. Also, if you are having to rerun this procedure it is best to remove the old working and build directories first.
  • If you plan to add your own tools to the ISO you will need to add them to file_permissions array in the ISO profile. You can find an example here. Information about the file can be found here. This is how permissions are set on added files. If you forget to include an added file in the file_permissions array you can still set its permissions in the booted ISO. However your changes will not be saved onto the media.
  • Due to an issue with the broadcom-wl package it may not be possible to connect to a wifi network from the booted ISO image. This issue may be resolved in the future. If you plan to use wifi be aware of this limitation.
Note: The following is intended to serve as a starting point. If you are able to improve upon it please share.
custom_archiso.sh
#!/usr/bin/env bash

set -e

# language = English
export LANG=C

# print messages
function print {
	tput setaf 2 ; echo -e "\n\t$1\n" ; tput sgr0
}

# language = English
export LANG=C

# project name
name=archzfsiso

# working directory
build="$HOME"/"$name"

# kernel to use
kernel=linux-lts

# packages to add
pkg_add=(zfs-dkms linux-lts-headers)

# packages to be removed
# broadcom-wl pulls in the linux kernel
pkg_rmv=(broadcom-wl)

# deps
[ ! "$(pacman -Q archiso)" ] && sudo pacman -S --noconfirm archiso

clear
print "This script will create an Arch Linux on OpenZFS installation ISO"
print "Linux LTS version: $(pacman -Qi linux-lts | awk '/Version/ {print $3}')"
print "OpenZFS version: $(pacman -Qi zfs-dkms | awk '/Version/ {print $3}')"

read -r -p "Continue? yes/no : " prompt
until [ "$prompt" == "yes" ] || [ "$prompt" == "no" ] ; do
	read -r -p "Continue? yes/no : " prompt
done
case "$prompt" in
	yes) print "Building the ISO which can take a while ..." ;;
	no) print "Quitting" ; exit ;;
esac

# cleanup
[ -d "$build" ] && sudo find /home/"$SUDO_USER" -type d -name "$name" -exec rm -rf {} +
[ -d /tmp/"$name" ] && sudo find /tmp -type d -name "$name" -exec rm -rf {} +

# working directory
[ ! -d "$build" ] && mkdir "$HOME"/"$name"
cp -r /usr/share/archiso/configs/releng/* "$build"

# change kernel
sed -i "s/^linux$/$kernel/" "$build"/packages.x86_64
mkinit="$build"/airootfs/etc/mkinitcpio.d
mv "$mkinit"/linux.preset "$mkinit"/"$kernel".preset
sed -i "s/vmlinuz-linux/vmlinuz-$kernel/" "$mkinit"/"$kernel".preset
sed -i "s/initramfs-linux.img/initramfs-$kernel.img/" "$mkinit"/"$kernel".preset

# remove packages
for a in "${pkg_rmv[@]}"; do sed -i "/$a/d" "$build"/packages.x86_64 ; done

# syslinux changes
sysconf="$build"/syslinux
sed -i "s/vmlinuz-linux/vmlinuz-$kernel/" "$sysconf"/archiso_sys-linux.cfg
sed -i "s/initramfs-linux.img/initramfs-$kernel.img/" "$sysconf"/archiso_sys-linux.cfg
sed -i "s/vmlinuz-linux/vmlinuz-$kernel/" "$sysconf"/archiso_pxe-linux.cfg
sed -i "s/initramfs-linux.img/initramfs-$kernel.img/" "$sysconf"/archiso_pxe-linux.cfg

# efiboot changes
eficonf="$build"/efiboot/loader/entries
sed -i "s/vmlinuz-linux/vmlinuz-$kernel/" "$eficonf"/01-archiso-x86_64-linux.conf
sed -i "s/initramfs-linux.img/initramfs-$kernel.img/" "$eficonf"/01-archiso-x86_64-linux.conf
sed -i "s/vmlinuz-linux/vmlinuz-$kernel/" "$eficonf"/02-archiso-x86_64-speech-linux.conf
sed -i "s/initramfs-linux.img/initramfs-$kernel.img/" "$eficonf"/02-archiso-x86_64-speech-linux.conf
sed -i "s/vmlinuz-linux/vmlinuz-$kernel/" "$eficonf"/03-archiso-x86_64-ram-linux.conf
sed -i "s/initramfs-linux.img/initramfs-$kernel.img/" "$eficonf"/03-archiso-x86_64-ram-linux.conf

# iso config
echo -e '\n\t\033[1m' 'Welcome to the Arch Linux on OpenZFS Installation ISO' '\033[0m\n' > "$build"/airootfs/etc/motd
sed -i "s/iso_name=\"archlinux\"/iso_name=\"archzfs\"/" "$build"/profiledef.sh
sed -i "s/iso_label=\"ARCH_\$(date +%Y%m)\"/iso_label=\"ARCHZFS_\$(date +%y%m%d)\"/" "$build"/profiledef.sh

# add packages
for a in "${pkg_add[@]}"; do echo "$a" >> "$build"/packages.x86_64 ; done

# enable the archzfs repo
curl -Ls https://git.io/Jsfw2 | grep ^Server > "$build"/airootfs/etc/pacman.d/archzfs-mirrorlist
cat >> "$build"/pacman.conf << EOF
[archzfs]
Include = /etc/pacman.d/archzfs-mirrorlist
EOF

# build the iso
sudo mkarchiso -w /tmp/"${name}" -o "${build}"/out "${build}"

print "Custom ISO build complete"