Skip to content

Instantly share code, notes, and snippets.

@enbeec
Created November 24, 2021 07:41
Show Gist options
  • Save enbeec/db2f2cd070fdd62530832d764eaec4cc to your computer and use it in GitHub Desktop.
Save enbeec/db2f2cd070fdd62530832d764eaec4cc to your computer and use it in GitHub Desktop.
How to Install Alpine on ZFS in 2021

Alpine on ZFS

First up....

Sources

This Alpine Wiki article and the pastebin document found under "Discussions" got me to a working state but I wanted to put my own slightly clearer version out there.

Also the Arch wiki entry on ZFS root was super helpful for the ZFS system datasets.

Disk Setup

Don't type comments. Comments are anything between # and the end of the line.

  • Install required packages: apk add zfs parted e2fsprogs syslinux
  • Alpine setup: setup-alpine # and Ctrl-C when you get to disk setup
  • Partition:
    parted /dev/sdX
    (parted) mklabel msdos # answer yes
    (parted) mkpart primary ext4 1M 200M # I like 200M but you can use 100M just fine
    (parted) set 1 boot on # pretty self explanatory
    (parted) mkpart primary 201M 100%
    
  • Create Filesystems:
    mdev -s # create device nodes
    mkfs.ext4 /dev/sdX1
    modprobe zfs
    zpool create -f -o ashift=12 \
      -O acltype=posixacl -O canmount=off -O compression=lz4 \
      -O dnodesize=auto -O normalization=formD -O relatime=on -O xattr=sa \
      -O encryption=aes-256-gcm -O keylocation=prompt -O keyformat=passphrase \
      -O mountpoint=/ -R /mnt \
      rpool /dev/sdX2
    
  • Get ready to run the setup:
    zfs create -o mountpoint=none -o canmount=off rpool/ROOT
    zfs create -o mountpoint=legacy rpool/ROOT/alpine # will get mounted to /
    mount -t zfs rpool/ROOT/rpool /mnt/
    
    zfs create -o mountpoint=/var rpool/var
    zfs create -o mountpoint=/var/log rpool/var/log 
    zfs create -o mountpoint=/var/lib rpool/var/lib
    # ... and so on
    
    zfs create -o mountpoint=none rpool/data
    zfs create -o mountpoint=/home rpool/data/home
    zfs create -o mountpoint=/root rpool/data/home/root
    
    mkdir /mnt/boot/
    mount -t ext4 /dev/sdX1 /mnt/boot/
    rc-update add zfs-import sysinit
    rc-update add zfs-mount sysinit
    
  • Alpine setup (2/2): setup-disk /mnt

Mount and Chroot (a.k.a. Rescue)

  • boot from image
  • install and activate zfs
    apk add zfs
    modprobe zfs
    
  • import the pool to /mnt without mounting: zpool import -NR /mnt rpool
  • unlock the datasets: zfs load-key rpool
  • mount the root dataset: mount -t zfs rpool/ROOT/alpine /mnt
  • mount the rest: zfs mount -a
  • don't forget boot: `mount -t ext4 /dev/sda1 /mnt/boot
  • set up the chroot:
    • as far as I can tell, the following special filesystems are required for rebuilding syslinux
      • mount -o bind /dev /mnt/dev
      • mount -t proc none /mnt/proc
      • mount -o bind /sys /mnt/sys
  • chroot: chroot /mnt
  • rebuild the bootloader: extlinux --install /boot
  • replace the MBR: dd if=/usr/share/syslinux/mbr.bin of=/dev/sda
  • you may exit and poweroff

accessed on Nov 28 2021

Alpine on ZFS root issues in wiki procedure

Generic

enable networking after booting in the alpine extended iso: $ mkdir -p /etc/networking --> /etc/network/interfaces """ auto eth0 iface eth0 inet dhcp """ $ /etc/init.d/networking restart

Notes in the install guide:

Part 1

Guide: https://wiki.alpinelinux.org/wiki/Setting_up_ZFS_with_native_encryption

The installation is from the current alppine extended iso. I add the part where I need to install ext4 and zfs (apk add e2fsprogs zfs). I load the zfs module (modprobe zfs). and than start with following the setps described in:

2 Setting up Alpine Linux Using ZFS with native encryption 2.1 Creating the Partition Layout 2.2 Setting up the root pool 2.2.1 Creating the required datasets 2.3 Creating the /boot filesystem 2.4 Mounting the /boot filesystem

The only change I made, is that I name my zpool 'tank'instead of 'rpool'.

Part 2

Guide: https://wiki.alpinelinux.org/wiki/Alpine_Linux_in_a_chroot

Issues:

Entering your chroot $ chroot /mnt /bin/bash -l --> chroot: can't execute '/bin/bash': No such file or directory --> resolved using /bin/sh $ chroot /mnt /bin/sh -l

Part 3

Guide: https://wiki.alpinelinux.org/wiki/Setting_up_ZFS_with_native_encryption

I following the setps described in:

2.5 Installing Alpine Linux 2.6 Unmounting the filesystems 2.7 Booting the system

I start where the previous part ends, so I am still in the chroot environment.

Issues:

Install the ZoL and linux-vanilla package: apk add linux-vanilla zfs $ apk add linux-vanilla zfs --> ERROR: unsatisfiable constraints linux-vanilla (missing) required by: world[linux-vanilla] --> resolved by installing linux-lts $ apk add linux-lts zfs

Edit the /etc/mkinitfs/mkinitfs.conf file and append zfs module to the features parameter: $ vi /etc/mkinitfs/mkinitfs.conf --> The folder /etc/mkinitfs does not exist --> Resolved by installing mkinitfs $ apk add mkinitfs --> Than the folder and file exist and can be edited

Rebuild the initial RAM disk: # mkinitfs $(ls /lib/modules/) --> ls: /lib/modules/: No such file or directory /lib/modules/5.4.5-0-lts does not exist or is not a directory --> resolved by installing linux-lts $ apk add linux-lts --> Than the folder exist and the command finishes

Edit the /etc/update-extlinux.conf file, set the root ZFS dataset and append the following kernel options to the default_kernel_opts parameter: $ vi /etc/update-extlinux.conf --> the folder and file do not exist --> resolved by installing syslinux

apk add syslinux

--> Than the folder and file exist and can be edited

Edit the /etc/update-extlinux.conf file, set the root ZFS dataset and append the following kernel options to the default_kernel_opts parameter: --> The wiki instructs to add: root=rpool/ROOT/alpine --> This should instead be: root=ZFS=rpool/ROOT/alpine

Update extlinux's config (if you're not using a different bootloader) $ update-extlinux --> /boot is device /dev/sda1 extlinux: no previous syslinux boot sector found --> Is this command output expected? Or does it point to an error?

Reboot the system: --> After reboot the following message appears: --> Missing operating system. FATAL: INT18: BOOT FAILURE

To go an fix this I did:

  • Reboot with iso
  • $ apk add e2fsprogs zfs
  • $ modprobe zfs
  • $ zpool import -a
  • $ mount -t zfs tank/ROOT/alpine /mnt/
  • $ mount -t ext4 /dev/sda1 /mnt/boot/
  • Redo steps from: Update extlinux's config...

Update extlinux's config (if you're not using a different bootloader) $ update-extlinux --> /boot is device /dev/sda1 extlinux: no previous syslinux boot sector found --> resolved by installing extlinux from the chroot environment $ extlinux --install /boot --> /boot is device /dev/sda1 --> than exit, umount, and reboot

Result after reboot: --> The ZFS modules are not loaded. Try running '/sbin/modprobe zfs' as root to load them. mount: mounting tank/ROOT/alpine on /sysroot failed: No such file or directory Mounting root failed. initramfs emergency recovery shell launched. Type 'exit' to continue boot etc. etc. etc.

Part 4

I think it is useful go give some more hints on how to get it running again in the installation environment after a reboot. Therefore these steps could be added/clarified in the troubleshooting section of the wiki:

  • Reboot with iso
  • $ apk add e2fsprogs zfs
  • $ modprobe zfs
  • $ zpool import -a
  • $ mount -t zfs tank/ROOT/alpine /mnt/
  • $ mount -t ext4 /dev/sda1 /mnt/boot/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment