switch to rauc based system

This commit is contained in:
Jonas Leder 2026-08-06 21:35:58 +02:00
commit 5b3292f439
12 changed files with 835 additions and 312 deletions

2
.gitignore vendored
View file

@ -1,4 +1,4 @@
.build/
dist/
rauc-key.pem
rauk-cert.pem
rauc-cert.pem

View file

@ -34,8 +34,31 @@ build-image:
- kubernetes
variables:
ARTIFACT_BASE_URL: "${PUBLIC_RELEASE_URL}"
# RAUC_SIGN_CERT/RAUC_SIGN_KEY: set these as protected "File" type
# CI/CD variables (Settings > CI/CD > Variables) holding the PEM
# contents of a cert/key pair generated ONCE, e.g.:
# openssl req -x509 -newkey rsa:4096 -nodes -keyout key.pem \
# -out cert.pem -days 3650 -subj /CN=alamos-alpine-kiosk
# GitLab writes each File variable's content to a runner-chosen
# temp path and exposes that path as the variable's own value --
# rebind those paths to the names build-image.sh actually reads
# (see ensure_rauc_keys()) below. Left unset here on purpose: without
# the two File variables configured, this job falls back to an
# ephemeral dev cert (loud warning, fine for testing this pipeline
# itself, but bundles built that way can't be installed on any
# device whose keyring trusts a different cert).
RAUC_CERT_PATH: "${RAUC_SIGN_CERT}"
RAUC_KEY_PATH: "${RAUC_SIGN_KEY}"
before_script:
- apk add --no-cache bash squashfs-tools cpio coreutils findutils grub grub-efi kmod curl
# e2fsprogs: mke2fs -d builds slot.img directly from a directory (no
# loop-mount needed). meson/ninja/build-base/pkgconf/glib-dev/
# openssl-dev: compiling RAUC from source (Alpine has no rauc package
# at all -- main/community/edge all checked). grub-bios: i386-pc
# modules + grub-mkimage/grub-bios-setup, bundled into the release for
# install-usb.sh's BIOS boot install (replaces extlinux/syslinux).
- apk add --no-cache bash squashfs-tools cpio coreutils findutils \
grub grub-efi grub-bios kmod curl e2fsprogs \
meson ninja build-base pkgconf glib-dev openssl-dev openssl
script:
- export KIOSK_VERSION="${CI_PIPELINE_ID}"
- bash build/build-image.sh
@ -45,6 +68,7 @@ build-image:
name: "${IMAGE_NAME}-${CI_PIPELINE_ID}"
paths:
- dist/${IMAGE_NAME}-*.tar.gz
- dist/${IMAGE_NAME}-*.raucb
- dist/latest.json
expire_in: "30 days"
rules:

184
README.md
View file

@ -4,27 +4,51 @@ Builds a diskless Alpine Linux kiosk image: boots straight into a
fullscreen Chromium window (Wayland, via [cage](https://github.com/cage-kiosk/cage)),
with SSH, VNC, and self-update built in.
Updates are applied via [RAUC](https://github.com/rauc/rauc) into two
interchangeable A/B root slots, so an update is always written to the
*inactive* slot while the device keeps running from the current one, and
a slot that fails to boot cleanly is rolled back automatically by GRUB.
## How it boots
The image runs entirely from RAM. A single ext4 partition on the boot
media (USB stick) holds:
The image runs entirely from RAM, from one of two identical root slots
plus a separate, persistent data partition, all on the same boot media
(USB stick):
```text
boot/vmlinuz-lts kernel
boot/initramfs-lts tiny custom initramfs (build/initramfs-init)
boot/rootfs.squashfs the whole Alpine system, read-only
boot/extlinux.conf bootloader config
kiosk.conf per-device configuration (see below)
.manifest / .version used by the auto-updater
ALPINEESP FAT32, ~100MiB -- a tiny GRUB EFI stub (UEFI only)
that finds ALPINEDATA by label and chainloads the
real grub.cfg from there.
ALPINEKIOSK_A ext4, RAUC slot "rootfs.0":
boot/vmlinuz-lts kernel
boot/initramfs-lts custom initramfs (build/initramfs-init)
boot/rootfs.squashfs the whole Alpine system, read-only
ALPINEKIOSK_B ext4, RAUC slot "rootfs.1" -- identical layout, the
second slot.
ALPINEDATA ext4, rest of the disk -- persistent, never touched
by a RAUC install:
boot/grub/grub.cfg the real GRUB config: A/B
slot selection, 3s manual
override menu
boot/grub/grubenv GRUB's slot-selection state
(ORDER, per-slot _OK/_TRY)
kiosk.conf per-device configuration
rauc.status RAUC's status file
```
At boot, the initramfs finds this partition by filesystem label
(`ALPINEKIOSK`), loop-mounts `rootfs.squashfs` read-only, layers a tmpfs
overlay on top for writability, bind-mounts the boot partition into the
new root at `/media/bootdev` (symlinked as `/kiosk.conf`), and
`switch_root`s into it. Nothing is ever written back into the squashfs;
all runtime state lives in RAM and is discarded on reboot except for
whatever the update script writes to `/media/bootdev`.
At boot, GRUB (BIOS: real `grub-install`-style i386-pc core image; UEFI:
the ESP stub chainloading `grub.cfg`) picks a slot using `grubenv`'s
`ORDER` and each slot's `_OK`/`_TRY` counters, shows a 3 second menu
("Slot A" / "Slot B") so an operator at the console can override that
choice, then boots the chosen slot with `root=LABEL=ALPINEKIOSK_A` (or
`_B`) on the kernel command line. The initramfs reads that label,
mounts the slot read-only, loop-mounts `rootfs.squashfs` from it, layers
a tmpfs overlay on top for writability, bind-mounts `ALPINEDATA` into
the new root at `/media/bootdev` (symlinked as `/kiosk.conf`), and
`switch_root`s into it. Nothing is ever written back into the squashfs
or the booted slot itself; all runtime state lives in RAM and is
discarded on reboot except for whatever's written to `/media/bootdev`
(config, `grubenv`) or, via RAUC, to the *other*, inactive slot.
tty1 autologs in as `kiosk`, whose `.profile` execs
`/usr/local/bin/start-kiosk.sh`, which runs `cage` (a Wayland compositor
@ -32,19 +56,44 @@ built specifically for single-app kiosks) with Chromium in `--kiosk`
fullscreen mode pointed at `KIOSK_URL`. `wayvnc` runs as a system service
and exposes the same Wayland session over VNC on port 5900.
## Updating and rollback
`kiosk-update.sh` (run periodically by crond, see `kiosk.conf` below)
downloads a signed `.raucb` bundle and hands it to `rauc install`, which
verifies its signature and writes it into whichever slot isn't currently
running -- the running slot is never touched. On success the new slot is
marked active and, if `AUTO_REBOOT=true`, the device reboots into it.
If a newly installed slot fails to reach a healthy boot, it's rolled
back automatically, with no involvement from `kiosk-update.sh`: `grub.cfg`
increments that slot's `_TRY` counter on every boot attempt (even ones
that never get past the kernel), and the `rauc-mark-good` OpenRC service
only resets `_TRY` and confirms the slot once Alpine's `default` runlevel
is reached. After `_TRY` exceeds the limit without a confirmation, GRUB
prefers the other, previously-good slot on its own.
## Repository layout
- `build/build-image.sh` — orchestrates the whole build: installs Alpine
packages into a chroot-able rootfs via `apk --root`, copies in the
files under `build/rootfs/`, builds the custom initramfs, squashes the
rootfs, and packages `dist/<name>-<version>.tar.gz` + `dist/latest.json`.
files under `build/rootfs/`, builds the custom initramfs and RAUC
(from source — Alpine ships no `rauc` package), squashes the rootfs,
builds the ext4 slot image and GRUB boot files, and packages
`dist/<name>-<version>.tar.gz` (first-install payload),
`dist/<name>-<version>.raucb` (signed update bundle) and
`dist/latest.json`.
- `build/rauc-hook.sh` — bundled inside the `.raucb`; re-labels whichever
slot RAUC just wrote (`ALPINEKIOSK_A`/`_B`), since the raw image itself
carries a neutral label.
- `build/initramfs-init` — the initramfs's PID 1 (busybox ash script).
- `build/rootfs/` — files overlaid onto the built system: OpenRC
services (`kiosk`, `wayvnc`), the periodic auto-update job, and
services (`kiosk`, `wayvnc`, `rauc-mark-good`), `/etc/rauc/system.conf`,
the periodic auto-update job, and
`/usr/local/bin/{start-kiosk,kiosk-apply-config,kiosk-update}.sh`.
- `config/kiosk.conf.example` — template for the per-device config file.
- `install/install-usb.sh` — formats a USB disk and installs a built
release onto it, bootable via extlinux/BIOS.
- `install/install-usb.sh` — formats a USB disk into the ESP, A/B slot
and data partition layout above, and installs a built release onto it,
bootable via real GRUB on both BIOS and UEFI.
- `.gitlab-ci.yml` — builds the image on a Kubernetes-executor runner.
## Building
@ -54,13 +103,26 @@ sudo ALPINE_VERSION=3.24 KIOSK_VERSION=42 bash build/build-image.sh
```
Requires root (apk needs to populate `--root`), plus `apk`,
`squashfs-tools`, `cpio`, `coreutils`/`findutils` on the host. No
QEMU/virtualization needed — this only works because the build host and
`squashfs-tools`, `cpio`, `coreutils`/`findutils`, `e2fsprogs` (for
`mke2fs -d`, which builds the ext4 slot image directly from a directory
tree, no loop-mount needed), `grub`/`grub-efi`/`grub-bios` (for the
UEFI stub and the bundled BIOS install tools), and `meson`/`ninja`/
`build-base`/`pkgconf`/`glib-dev`/`openssl-dev` to compile RAUC from
source (Alpine ships no `rauc` package) on the host. No QEMU/
virtualization needed — this only works because the build host and
target are both `x86_64`; the one privileged operation is `apk`
internally `chroot()`ing to run package post-install scripts, which only
needs `CAP_SYS_CHROOT` (part of the default container capability set).
Output: `dist/alpine-kiosk-<KIOSK_VERSION>.tar.gz` and `dist/latest.json`.
For real deployments, also set `RAUC_CERT_PATH`/`RAUC_KEY_PATH` to a
cert/key pair generated *once* (see `build-image.sh`'s
`ensure_rauc_keys()`) and reused on every build — without them, builds
fall back to an ephemeral throwaway signing cert (loud warning), whose
bundles no device that trusts a different cert can install.
Output: `dist/alpine-kiosk-<KIOSK_VERSION>.tar.gz` (first-install
payload), `dist/alpine-kiosk-<KIOSK_VERSION>.raucb` (signed update
bundle) and `dist/latest.json`.
## GitLab CI
@ -72,7 +134,9 @@ tagged `kubernetes`, using `CI_PIPELINE_ID` as the version. No
which requires auth for private projects and expires after 30 days)
with wherever you actually publish releases (object storage, a static
site, etc.) so devices' `UPDATE_URL` manifests resolve to a stable,
publicly reachable download.
publicly reachable download, and set `RAUC_CERT_PATH`/`RAUC_KEY_PATH` as
protected CI/CD file variables pointing at a real, persistent signing
cert/key pair.
## Installing to a USB stick
@ -80,19 +144,24 @@ publicly reachable download.
sudo install/install-usb.sh /dev/sdX dist/alpine-kiosk-42.tar.gz [my-kiosk.conf]
```
This **wipes** `/dev/sdX`, creates a single ext4 partition labeled
`ALPINEKIOSK`, extracts the release onto it, and installs extlinux/MBR
boot code using the `extlinux` binary and `mbr.bin` bundled in the
release tarball itself (under `tools/`) — no `syslinux` package needed
on the installer host. Requires `parted`, `mkfs.ext4`, `wipefs`, `dd`.
If no `kiosk.conf` is given and the target doesn't already have one,
the example template is installed — edit it on the device
before first boot (or mount the partition from another machine).
This **wipes** `/dev/sdX`, creates the ESP + `ALPINEKIOSK_A` +
`ALPINEKIOSK_B` + `ALPINEDATA` layout described above (both slots seeded
with identical content so the disk boots either one immediately), and
installs real GRUB for both BIOS and UEFI using the `grub-mkimage`/
`grub-bios-setup`/`grub-editenv` binaries and i386-pc module set bundled
in the release tarball itself (under `tools/`) — no `grub` package
needed on the installer host. Requires `parted`, `mkfs.ext4`,
`mkfs.vfat`, `wipefs`, `dd`, `e2label` (Alpine: `e2fsprogs` +
`e2fsprogs-extra`, the latter is where `e2label` actually lives). If no
`kiosk.conf` is given and
the target doesn't already have one, the example template is installed
— edit it on the device before first boot (or mount the `ALPINEDATA`
partition from another machine).
## `kiosk.conf`
Lives at the root of the boot partition. See
`config/kiosk.conf.example` for the full set of options:
Lives at the root of the `ALPINEDATA` partition (symlinked to `/kiosk.conf`
at runtime). See `config/kiosk.conf.example` for the full set of options:
- `KIOSK_URL` — page Chromium opens fullscreen. Defaults to
`http://localhost/setup`, the bundled AMweb app's own setup page (see
@ -109,10 +178,10 @@ Lives at the root of the boot partition. See
`ETH_IFACE`.
- `AUTO_UPDATE`, `UPDATE_URL`, `UPDATE_INTERVAL_MINUTES` — periodic
check against a `latest.json` manifest (the same format
`build-image.sh` produces); downloads, verifies (sha256), removes
files that existed in the old release but not the new one (via the
`.manifest` diff), and extracts the new release over the boot
partition.
`build-image.sh` produces); if newer, downloads the `.raucb` bundle
and hands it to `rauc install`, which verifies its signature and
writes it into the *inactive* slot (see "Updating and rollback"
above) — the currently running slot is never touched.
- `AUTO_REBOOT` — reboot automatically once an update is applied.
Applied at boot by the `kiosk` OpenRC service
@ -133,8 +202,8 @@ AMweb's setup (entered once at `http://localhost/setup`) is saved by
the browser into Chrome's `localStorage`. Since the whole root
filesystem is a fresh RAM overlay every boot, `start-kiosk.sh` symlinks
just that one directory (`~/.config/chromium/Default/Local Storage`,
prepared and chowned as root by `kiosk-apply-config.sh`) onto the boot
partition, so setup survives a reboot — the rest of the Chrome profile
prepared and chowned as root by `kiosk-apply-config.sh`) onto the
`ALPINEDATA` partition, so setup survives a reboot — the rest of the Chrome profile
(cache, cookies, IndexedDB, ...) stays ephemeral.
## Known limitations / things to verify on real hardware
@ -150,3 +219,38 @@ partition, so setup survives a reboot — the rest of the Chrome profile
- Wi-Fi firmware isn't bundled (`linux-firmware` is large); add the
specific `linux-firmware-<chipset>` package to `PACKAGES` in
`build-image.sh` for your target adapter.
- `ROOTFS_SLOT_SIZE_MIB` (default 2048) fixes each RAUC slot partition's
size at install time, from the release tarball's own `slot.img` size —
if the built system grows past that (e.g. more `chromium-lang` locales,
`EXTRA_FIRMWARE_PKGS`), raise it and reinstall; existing devices
updated in the field can't grow their already-partitioned slots.
- BIOS boot now goes through this project's own bundling of
`grub-mkimage`/`grub-bios-setup` (see `bundle_grub_tools()` in
`build-image.sh`) rather than a `grub-install` most other projects
rely on, since the target device is known and `grub-install`'s own
device-detection isn't needed — verify it boots on real BIOS/CSM
hardware (and a spare disk/VM) before relying on it, the same way you
would any new bootloader integration.
- RAUC's GRUB slot-selection logic (`grub.cfg`'s `_TRY` counters, the
`rauc-mark-good` service) is this project's own implementation of the
documented `ORDER`/`<slot>_OK`/`<slot>_TRY` grubenv contract RAUC's
GRUB backend expects, not a copy of RAUC's own reference script —
test the rollback path deliberately (see the "Simulate a bad update"
step below) rather than assuming it's identical to other RAUC+GRUB
deployments you may have seen.
## Testing an update / rollback
1. Build, then serve `dist/` locally (`python3 -m http.server` from
`dist/`) and point a test device's `UPDATE_URL` at
`http://<host>:8000/latest.json`.
2. Bump `KIOSK_VERSION` and rebuild; `kiosk-update.sh` (or
`/etc/periodic/15min/kiosk-update`) should download the new
`.raucb`, `rauc install` it into the inactive slot, and — with
`AUTO_REBOOT=true` — reboot into it. `rauc status` should show the
new slot active and, once `rauc-mark-good` has run, good.
3. To exercise rollback, corrupt the *inactive* slot after an install
but before rebooting into it (e.g. `dd if=/dev/zero of=/dev/disk/by-label/ALPINEKIOSK_B bs=1M count=8`
if `B` is inactive) and reboot: GRUB's menu should fall back to slot
`A` once `B`'s `_TRY` count is exceeded, rather than hanging on a
broken slot.

View file

@ -1,45 +1,41 @@
#!/bin/bash
# Builds a diskless Alpine Linux kiosk image.
# Builds a diskless Alpine Linux kiosk image, updated via RAUC A/B slots.
#
# Produces dist/alpine-kiosk-<VERSION>.tar.gz containing:
# boot/vmlinuz-lts kernel
# boot/initramfs-lts custom initramfs (squashfs+overlay boot, see
# initramfs-init)
# boot/rootfs.squashfs the full Alpine system (packages + overlay
# files below), read-only
# boot/extlinux.conf BIOS bootloader config (copied to boot/ on the
# USB's ext4 data partition)
# esp/EFI/BOOT/BOOTX64.EFI standalone UEFI GRUB binary with an embedded
# config (finds the ext4 data partition by its
# ALPINEKIOSK label, boots the same
# vmlinuz-lts/initramfs-lts) -- goes on a
# separate small FAT32 ESP partition,
# install/install-usb.sh sets up both
# tools/extlinux statically-pathed extlinux binary + its musl
# tools/lib/ld-musl-*.so.1 loader + mbr.bin, bundled so install-usb.sh
# tools/mbr.bin needs nothing but the release tarball itself
# (no syslinux package on the installer host)
# kiosk.conf.example kiosk config template (only written if the
# target doesn't already have one, see
# install/install-usb.sh)
# .manifest list of all paths in this release, used by
# kiosk-update.sh to remove stale files
# .version the KIOSK_VERSION of this release
# Produces:
# dist/alpine-kiosk-<VERSION>.tar.gz first-install payload, used by
# install/install-usb.sh:
# slot.img ext4 image (boot/{vmlinuz-lts,
# initramfs-lts,rootfs.squashfs}), dd'd onto
# BOTH RAUC slot partitions at install time
# so the disk boots either one immediately
# esp/EFI/BOOT/BOOTX64.EFI standalone UEFI GRUB stub: finds the
# ALPINEDATA partition by label and
# chainloads the real grub.cfg from there
# data/boot/grub/grub.cfg the real, slot-selecting GRUB config
# (menu with a 3s timeout, reads grubenv's
# ORDER/_OK/_TRY to pick a default) --
# installed onto the ALPINEDATA partition
# data/kiosk.conf.example kiosk config template
# tools/ grub-mkimage, grub-bios-setup,
# grub-editenv + the i386-pc module dir +
# their musl loader/shared libs, so
# install-usb.sh needs no `grub` package
# -- see bundle_grub_tools()
# dist/alpine-kiosk-<VERSION>.raucb signed RAUC bundle for
# kiosk-update.sh: same slot.img content plus a post-install hook
# (build/rauc-hook.sh) that relabels whichever slot RAUC just wrote.
# dist/latest.json {version, file, url} the update-checker polls.
#
# Must run as root (apk needs to write uid/gid-preserving files, device
# nodes, etc into --root). Designed to run unprivileged beyond that: no
# qemu, no loop-mounts, no bind-mounts, no manual chroot mounting of
# /proc /sys /dev. apk handles chroot-execution of package scripts
# internally using only CAP_SYS_CHROOT, which is part of the default
# container capability set. Building the EFI bootloader additionally
# needs the `grub` and `grub-efi` packages on the *build* host (see
# .gitlab-ci.yml's before_script) -- unlike extlinux/syslinux, grub's
# output is a data file (a PE binary for firmware to execute), not
# something this script itself needs to run, so it isn't bundled the
# same way. Also needs `kmod` on the build host, purely for its
# `depmod` (used to compute module dependencies for the initramfs --
# depmod only parses ELF metadata, so the build host's native depmod
# works fine against modules built for a different kernel version).
# qemu, no manual chroot mounting of /proc /sys /dev. apk handles
# chroot-execution of package scripts internally using only
# CAP_SYS_CHROOT, part of the default container capability set. ext4
# slot images are built directly from a directory tree via `mke2fs -d`
# (no loop-mount needed either). Building RAUC and the GRUB tooling
# additionally needs build-host packages not installed into --root: see
# build_rauc()'s and bundle_grub_tools()'s own command -v checks below,
# and .gitlab-ci.yml's before_script for the full list.
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
@ -51,6 +47,13 @@ ALPINE_MIRROR="${ALPINE_MIRROR:-https://dl-cdn.alpinelinux.org/alpine}"
KIOSK_VERSION="${KIOSK_VERSION:-${CI_PIPELINE_ID:-0}}"
IMAGE_NAME="${IMAGE_NAME:-alpine-kiosk}"
# Fixed size for each RAUC rootfs slot partition -- both install-usb.sh
# (partitioning) and this script (sizing slot.img) need to agree on it,
# but only this script's default actually matters: install-usb.sh reads
# the real size of the built slot.img out of the release tarball itself,
# it never has to be told separately.
ROOTFS_SLOT_SIZE_MIB="${ROOTFS_SLOT_SIZE_MIB:-2048}"
WORKDIR="${WORKDIR:-$PROJECT_DIR/.build}"
ROOTFS="$WORKDIR/rootfs"
INITRAMFS_DIR="$WORKDIR/initramfs"
@ -156,11 +159,21 @@ prune_chromium_locales() {
# pulling the ~750MiB full linux-firmware set. If your target hardware
# needs specific firmware (wifi chipset, GPU, ...), pass e.g.
# EXTRA_FIRMWARE_PKGS="linux-firmware-brcm linux-firmware-intel"
#
# e2fsprogs-extra: needed on-device for `e2label` (NOT in the base
# e2fsprogs package on Alpine, which only has mke2fs/e2fsck -- e2label
# lives in -extra), which build/rauc-hook.sh runs (as part of a RAUC
# bundle install) to re-label whichever slot RAUC just wrote -- see
# build_rauc_bundle() below. glib/openssl: runtime libs the RAUC binary
# built by build_rauc() is dynamically linked against
# (glib-2.0/gio-2.0/gio-unix-2.0, libssl/libcrypto); chromium already
# needs a modern openssl-compatible libcrypto too, but glib isn't
# otherwise pulled in by anything else here.
PACKAGES="alpine-base busybox-mdev-openrc openrc util-linux linux-lts linux-firmware-none \
chromium cage seatd wayvnc mesa-dri-gallium mesa-gl mesa-egl mesa-gbm \
ttf-dejavu \
openssh dhcpcd wpa_supplicant iproute2 iw wireless-tools \
curl jq ca-certificates tzdata \
curl jq ca-certificates tzdata e2fsprogs-extra glib openssl \
htop nano \
nginx \
kbd \
@ -208,7 +221,96 @@ bundle_amweb() {
rm -rf "$zip" "$headers" "$extract_dir"
}
# --- 2. Overlay files, users, services ------------------------------------
# --- 2. RAUC signing keys -------------------------------------------------
# A fixed cert/key pair must persist across builds -- regenerating one
# per build would make every new bundle unverifiable by devices that
# already trust a previous keyring. For real deployments, generate a
# cert/key pair ONCE and keep passing the same one via RAUC_CERT_PATH/
# RAUC_KEY_PATH (e.g. as protected CI/CD variables/files), the same
# pattern as ARTIFACT_BASE_URL for the release URL. Only falls back to
# an ephemeral throwaway cert -- loudly -- when neither is set, for
# local/dev iteration.
RAUC_CERT_PATH="${RAUC_CERT_PATH:-}"
RAUC_KEY_PATH="${RAUC_KEY_PATH:-}"
ensure_rauc_keys() {
if [ -n "$RAUC_CERT_PATH" ] && [ -n "$RAUC_KEY_PATH" ]; then
[ -f "$RAUC_CERT_PATH" ] || { echo "RAUC_CERT_PATH=$RAUC_CERT_PATH not found" >&2; exit 1; }
[ -f "$RAUC_KEY_PATH" ] || { echo "RAUC_KEY_PATH=$RAUC_KEY_PATH not found" >&2; exit 1; }
return
fi
log "WARNING: RAUC_CERT_PATH/RAUC_KEY_PATH not set -- generating a THROWAWAY dev signing cert/key under $WORKDIR. Bundles signed with this cannot be installed by any device whose keyring already trusts a different (e.g. previous dev, or real) cert. For real deployments generate one cert/key pair ONCE (e.g. openssl req -x509 -newkey rsa:4096 -nodes -keyout key.pem -out cert.pem -days 3650 -subj /CN=alpine-kiosk) and pass it via RAUC_CERT_PATH/RAUC_KEY_PATH on every build."
mkdir -p "$WORKDIR/dev-keys"
RAUC_CERT_PATH="$WORKDIR/dev-keys/dev.cert.pem"
RAUC_KEY_PATH="$WORKDIR/dev-keys/dev.key.pem"
if [ ! -f "$RAUC_CERT_PATH" ] || [ ! -f "$RAUC_KEY_PATH" ]; then
openssl req -x509 -newkey rsa:4096 -nodes \
-keyout "$RAUC_KEY_PATH" -out "$RAUC_CERT_PATH" \
-days 3650 -subj "/CN=alpine-kiosk-dev" 2>/dev/null
fi
}
# --- 3. Build RAUC from source --------------------------------------------
# Alpine ships no RAUC package (checked main/community/edge -- none
# exist), so it's compiled here. Build host and target are both
# x86_64 (see the top comment's "no QEMU" note), so the exact same
# binary this function produces is used directly by build_rauc_bundle()
# below (`rauc bundle`, on the build host) AND copied into $ROOTFS for
# the target device's own `rauc install`/`rauc status`.
RAUC_VERSION="${RAUC_VERSION:-1.15.2}"
RAUC_SRC_URL="${RAUC_SRC_URL:-https://github.com/rauc/rauc/releases/download/v${RAUC_VERSION}/rauc-${RAUC_VERSION}.tar.xz}"
RAUC_BIN=""
build_rauc() {
log "building RAUC $RAUC_VERSION from source"
for bin in meson ninja pkg-config openssl; do
command -v "$bin" >/dev/null 2>&1 || {
echo "$bin not found -- install 'meson ninja build-base pkgconf glib-dev openssl-dev' on the build host" >&2
exit 1
}
done
local src="$WORKDIR/rauc-$RAUC_VERSION"
local builddir="$WORKDIR/rauc-build"
local destdir="$WORKDIR/rauc-install"
rm -rf "$src" "$builddir" "$destdir"
curl -fsSL "$RAUC_SRC_URL" -o "$WORKDIR/rauc.tar.xz"
tar -C "$WORKDIR" -xf "$WORKDIR/rauc.tar.xz"
# No D-Bus/systemd here (OpenRC, no service manager RAUC's background
# service mode expects) -- only the CLI (`rauc install`, `rauc
# status`, `rauc bundle`) is used, so the background service, network
# and streaming install modes (network also pulls in libcurl --
# kiosk-update.sh downloads bundles itself and hands rauc a local
# path instead), JSON output and GPT support are all disabled to keep
# RAUC's dependency footprint (and the target rootfs's) minimal.
meson setup "$builddir" "$src" \
--prefix=/usr \
--buildtype=release \
-Dservice=false \
-Dnetwork=false \
-Dstreaming=false \
-Djson=disabled \
-Dgpt=disabled \
-Dcomposefs=disabled \
-Dtests=false \
-Dmanpages=false \
-Dhtmldocs=false
ninja -C "$builddir"
DESTDIR="$destdir" ninja -C "$builddir" install
RAUC_BIN="$destdir/usr/bin/rauc"
[ -x "$RAUC_BIN" ] || { echo "RAUC build did not produce $RAUC_BIN" >&2; exit 1; }
mkdir -p "$ROOTFS/usr/bin"
cp -a "$RAUC_BIN" "$ROOTFS/usr/bin/rauc"
mkdir -p "$ROOTFS/etc/rauc"
cp -a "$RAUC_CERT_PATH" "$ROOTFS/etc/rauc/ca.cert.pem"
}
# --- 4. Overlay files, users, services ------------------------------------
configure_rootfs() {
log "copying overlay files"
cp -a "$SCRIPT_DIR/rootfs/." "$ROOTFS/"
@ -217,7 +319,8 @@ configure_rootfs() {
# functions.sh -> /usr/libexec/rc/sh/functions.sh) would make chmod
# follow that absolute symlink against the *host* root, not $ROOTFS.
chmod +x "$ROOTFS"/usr/local/bin/*.sh
chmod +x "$ROOTFS/etc/init.d/kiosk" "$ROOTFS/etc/init.d/wayvnc" "$ROOTFS/etc/init.d/amweb"
chmod +x "$ROOTFS/etc/init.d/kiosk" "$ROOTFS/etc/init.d/wayvnc" \
"$ROOTFS/etc/init.d/amweb" "$ROOTFS/etc/init.d/rauc-mark-good"
chmod +x "$ROOTFS"/etc/periodic/15min/*
bundle_amweb
@ -272,11 +375,15 @@ EOF
# "default", parsing a nonexistent/stale /etc/network/interfaces
# every boot. Within a single runlevel OpenRC's topological sort does
# respect the dependency, so both go in "boot" here.
#
# rauc-mark-good goes in "default" too, after sshd/wayvnc/crond: it
# only needs to run once OpenRC has gotten this far without falling
# over, not before anything else starts.
for rl_svc in "sysinit:devfs" "sysinit:mdev" "sysinit:hwdrivers" \
"boot:hostname" "boot:modules" "boot:sysctl" "boot:hwclock" \
"boot:seatd" "boot:kiosk" "boot:networking" "boot:amweb" \
"default:sshd" "default:wayvnc" "default:crond" \
"default:local"; do
"default:rauc-mark-good" "default:local"; do
rl="${rl_svc%%:*}"; svc="${rl_svc##*:}"
mkdir -p "$ROOTFS/etc/runlevels/$rl"
[ -e "$ROOTFS/etc/init.d/$svc" ] && \
@ -290,7 +397,7 @@ EOF
rm -rf "$ROOTFS/var/cache/apk"/*
}
# --- 3. Build custom initramfs (busybox + squashfs/overlay init) --------
# --- 5. Build custom initramfs (busybox + squashfs/overlay init) --------
build_initramfs() {
log "building initramfs"
rm -rf "$INITRAMFS_DIR"
@ -374,11 +481,12 @@ build_initramfs() {
>"$WORKDIR/initramfs-lts"
}
# --- 4. squashfs + assemble the boot-partition image tree ----------------
assemble_image() {
# --- 6. squashfs + ext4 slot image -----------------------------------------
build_slot_image() {
log "building squashfs (this can take a while for chromium)"
rm -rf "$IMAGE_DIR"
mkdir -p "$IMAGE_DIR/boot"
local slot_dir="$IMAGE_DIR/slot"
rm -rf "$slot_dir"
mkdir -p "$slot_dir/boot"
# boot/vmlinuz-lts is shipped alongside the squashfs for the
# bootloader to load directly, and boot/initramfs-lts is the stock
# mkinitfs-generated initramfs (a linux-lts dependency trigger we
@ -387,45 +495,125 @@ assemble_image() {
# running system still needs the full module set (GPU/DRM drivers,
# wifi, etc.), the initramfs only carries the small subset needed to
# find and mount this squashfs in the first place.
mksquashfs "$ROOTFS" "$IMAGE_DIR/boot/rootfs.squashfs" \
mksquashfs "$ROOTFS" "$slot_dir/boot/rootfs.squashfs" \
-comp zstd -noappend -no-progress \
-e boot/vmlinuz-lts -e boot/initramfs-lts
cp -a "$ROOTFS/boot/vmlinuz-lts" "$IMAGE_DIR/boot/vmlinuz-lts"
cp -a "$WORKDIR/initramfs-lts" "$IMAGE_DIR/boot/initramfs-lts"
cp -a "$ROOTFS/boot/vmlinuz-lts" "$slot_dir/boot/vmlinuz-lts"
cp -a "$WORKDIR/initramfs-lts" "$slot_dir/boot/initramfs-lts"
cat >"$IMAGE_DIR/boot/extlinux.conf" <<EOF
DEFAULT kiosk
PROMPT 0
TIMEOUT 10
log "building ext4 slot image (${ROOTFS_SLOT_SIZE_MIB}MiB) from $slot_dir"
command -v mke2fs >/dev/null 2>&1 || {
echo "mke2fs not found -- install the 'e2fsprogs' package on the build host" >&2
exit 1
}
local img="$IMAGE_DIR/slot.img"
rm -f "$img"
# -d populates the filesystem straight from a directory tree in one
# step -- no loop-mount needed (see the top comment's "no loop-mounts"
# constraint). Labeled ALPINEKIOSK (neutral, not _A/_B): this same
# image seeds both slot partitions at install time and is also what
# ships inside the .raucb bundle for later updates -- either way,
# whichever slot device it lands on gets relabeled to the correct
# _A/_B afterwards (install-usb.sh directly, or build/rauc-hook.sh via
# RAUC's post-install hook for a field update).
truncate -s "${ROOTFS_SLOT_SIZE_MIB}M" "$img"
mke2fs -q -F -t ext4 -L ALPINEKIOSK -d "$slot_dir" "$img"
LABEL kiosk
LINUX vmlinuz-lts
INITRD initramfs-lts
APPEND root=LABEL=ALPINEKIOSK console=tty1
# Only slot.img itself ships in the release (both the tarball and the
# .raucb bundle) -- this staging directory's content is now fully
# duplicated inside it, so drop it rather than packaging both copies.
rm -rf "$slot_dir"
}
# --- 7. GRUB: boot-selection config + EFI stub -----------------------------
# The *real*, slot-selecting grub.cfg lives on the persistent ALPINEDATA
# partition (installed by install-usb.sh from $IMAGE_DIR/data), not
# baked into any bootloader binary -- so it (and grubenv beside it) can
# keep being read/written by GRUB and RAUC without ever re-flashing the
# ESP or MBR. Both the EFI stub (built here) and the BIOS core.img
# (built by install-usb.sh at install time, from tools/embed.cfg bundled
# below) share one tiny bootstrap script whose only job is finding
# ALPINEDATA and chainloading that real config -- see embed_cfg().
GRUB_MODULES="part_msdos fat ext2 search search_fs_uuid search_fs_file search_label configfile normal linux loadenv echo test"
embed_cfg() {
cat <<'EOF'
search --no-floppy --set=root --label ALPINEDATA
set prefix=($root)/boot/grub
configfile ($root)/boot/grub/grub.cfg
EOF
}
# Config template: install-usb.sh only writes this if the user didn't
# supply their own kiosk.conf, so re-installs never clobber an existing
# on-device config.
cp "$PROJECT_DIR/config/kiosk.conf.example" "$IMAGE_DIR/kiosk.conf.example"
build_grub_cfg() {
log "generating grub.cfg (A/B slot selection, 3s manual-override menu)"
mkdir -p "$IMAGE_DIR/data/boot/grub"
cat >"$IMAGE_DIR/data/boot/grub/grub.cfg" <<'EOF'
# RAUC A/B slot selection. grubenv (next to this file, both on
# ALPINEDATA) holds ORDER (space-separated slot preference, e.g. "A B")
# and, per slot, <slot>_OK (1 once rauc-mark-good has confirmed a boot)
# and <slot>_TRY (incremented here, on every attempt to boot that slot --
# NOT by the OS -- so a slot that panics before OpenRC even starts still
# counts as a failed attempt). `rauc status mark-good`/`mark-bad` (see
# the rauc-mark-good OpenRC service) reset a slot's _TRY to 0 and set
# _OK accordingly; this script never writes _OK itself.
#
# Per your request: a 3 second menu delay before the computed default
# boots, so an operator at the console can override it manually.
set timeout=3
set timeout_style=menu
bundle_installer_tools
bundle_efi_boot
insmod part_msdos
insmod ext2
insmod fat
echo "$KIOSK_VERSION" >"$IMAGE_DIR/.version"
load_env
( cd "$IMAGE_DIR" && find . -type f | sed 's#^\./##' | sort ) >"$IMAGE_DIR/.manifest"
set MAX_TRY=3
if [ -z "$ORDER" ]; then set ORDER="A B"; fi
if [ -z "$A_OK" ]; then set A_OK=1; fi
if [ -z "$B_OK" ]; then set B_OK=1; fi
if [ -z "$A_TRY" ]; then set A_TRY=0; fi
if [ -z "$B_TRY" ]; then set B_TRY=0; fi
set default="slotA"
for slot in $ORDER; do
if [ "$slot" = "A" -a "$A_OK" = "1" -a "$A_TRY" -lt "$MAX_TRY" ]; then
set default="slotA"
break
fi
if [ "$slot" = "B" -a "$B_OK" = "1" -a "$B_TRY" -lt "$MAX_TRY" ]; then
set default="slotB"
break
fi
done
menuentry "Slot A" --id slotA {
set A_TRY=$((A_TRY + 1))
save_env A_TRY
search --no-floppy --set=slotroot --label ALPINEKIOSK_A
linux ($slotroot)/boot/vmlinuz-lts root=LABEL=ALPINEKIOSK_A console=tty1
initrd ($slotroot)/boot/initramfs-lts
}
menuentry "Slot B" --id slotB {
set B_TRY=$((B_TRY + 1))
save_env B_TRY
search --no-floppy --set=slotroot --label ALPINEKIOSK_B
linux ($slotroot)/boot/vmlinuz-lts root=LABEL=ALPINEKIOSK_B console=tty1
initrd ($slotroot)/boot/initramfs-lts
}
EOF
}
# Builds a standalone UEFI GRUB binary (EFI/BOOT/BOOTX64.EFI, the
# well-known "removable media" path firmware falls back to without
# needing an NVRAM boot entry) with its grub.cfg baked in via
# grub-mkstandalone's memdisk, so the ESP needs no separate config file.
# The config finds the ext4 data partition by filesystem label (not by
# partition number), so it keeps working across reinstalls/updates
# regardless of partition layout.
bundle_efi_boot() {
# needing an NVRAM boot entry) with embed_cfg()'s tiny bootstrap script
# baked in via grub-mkstandalone's memdisk -- it just finds ALPINEDATA
# and hands off to the real grub.cfg there, so the ESP itself never
# needs rewriting after install (unlike the pre-RAUC version of this
# script, which baked the entire boot config in here).
bundle_esp() {
log "building UEFI GRUB standalone binary"
command -v grub-mkstandalone >/dev/null 2>&1 || {
echo "grub-mkstandalone not found -- install the 'grub' and 'grub-efi' packages on the build host" >&2
@ -438,18 +626,8 @@ bundle_efi_boot() {
exit 1
}
local grub_cfg="$WORKDIR/grub.cfg"
cat >"$grub_cfg" <<'EOF'
set timeout=0
insmod part_msdos
insmod part_gpt
insmod fat
insmod ext2
search --no-floppy --set=root --label ALPINEKIOSK
linux ($root)/boot/vmlinuz-lts root=LABEL=ALPINEKIOSK console=tty1
initrd ($root)/boot/initramfs-lts
boot
EOF
local cfg="$WORKDIR/efi-embed.cfg"
embed_cfg >"$cfg"
mkdir -p "$IMAGE_DIR/esp/EFI/BOOT"
grub-mkstandalone \
@ -458,32 +636,112 @@ EOF
--output="$IMAGE_DIR/esp/EFI/BOOT/BOOTX64.EFI" \
--locales= \
--fonts= \
--modules="part_msdos part_gpt fat ext2 search search_fs_uuid search_fs_file search_label configfile normal linux" \
"boot/grub/grub.cfg=$grub_cfg"
--modules="$GRUB_MODULES" \
"boot/grub/grub.cfg=$cfg"
}
# Extracts a self-contained extlinux + mbr.bin into the release, so
# install/install-usb.sh needs no `syslinux` package on the installer
# host -- just the tarball itself (plus generic tools: parted, mkfs.ext4,
# wipefs, dd). This installs syslinux into its own throwaway root (kept
# out of the kiosk's own rootfs/squashfs, which never runs a bootloader
# installer) purely to pull the two files out of the package.
bundle_installer_tools() {
log "bundling installer tools (extlinux, mbr.bin)"
local sysroot="$WORKDIR/syslinux-root"
rm -rf "$sysroot"
mkdir -p "$sysroot/etc/apk/keys"
cp -a /etc/apk/keys/. "$sysroot/etc/apk/keys/"
cat >"$sysroot/etc/apk/repositories" <<EOF
$ALPINE_MIRROR/v$ALPINE_VERSION/main
$ALPINE_MIRROR/v$ALPINE_VERSION/community
EOF
apk add --root "$sysroot" --arch "$ALPINE_ARCH" --initdb --no-cache syslinux
# Bundles grub-mkimage, grub-bios-setup, grub-editenv and the i386-pc
# module directory (+ their musl loader/shared library dependencies)
# into the release, the same reasoning the pre-RAUC version of this
# script used for extlinux: install-usb.sh needs no `grub` package on
# the installer host, just the tarball itself. It's install-usb.sh, not
# this script, that actually runs grub-mkimage/grub-bios-setup (it knows
# the real target device; this script doesn't) -- see its own comments
# for why BIOS boot moves from extlinux to real GRUB here (RAUC's GRUB
# bootloader backend needs the environment-block support extlinux has
# no equivalent of).
bundle_grub_tools() {
log "bundling GRUB BIOS installer tools (grub-mkimage, grub-bios-setup, grub-editenv, i386-pc modules)"
local bin
for bin in grub-mkimage grub-bios-setup grub-editenv; do
command -v "$bin" >/dev/null 2>&1 || {
echo "$bin not found -- install the 'grub' and 'grub-bios' packages on the build host" >&2
exit 1
}
done
local moddir="/usr/lib/grub/i386-pc"
[ -d "$moddir" ] || {
echo "$moddir not found -- install the 'grub-bios' package on the build host" >&2
exit 1
}
mkdir -p "$IMAGE_DIR/tools/lib"
cp -a "$sysroot/sbin/extlinux" "$IMAGE_DIR/tools/extlinux"
cp -a "$sysroot/usr/share/syslinux/mbr.bin" "$IMAGE_DIR/tools/mbr.bin"
cp -a "$sysroot/lib/ld-musl-${ALPINE_ARCH}.so.1" "$IMAGE_DIR/tools/lib/"
mkdir -p "$IMAGE_DIR/tools/grub/i386-pc" "$IMAGE_DIR/tools/lib"
cp -a "$moddir/." "$IMAGE_DIR/tools/grub/i386-pc/"
local loader="/lib/ld-musl-${ALPINE_ARCH}.so.1"
[ -f "$loader" ] || loader="/usr/lib/ld-musl-${ALPINE_ARCH}.so.1"
cp -a "$loader" "$IMAGE_DIR/tools/lib/"
local src dep
for bin in grub-mkimage grub-bios-setup grub-editenv; do
src=$(command -v "$bin")
cp -a "$src" "$IMAGE_DIR/tools/$bin"
# Unlike extlinux (which only ever needed the bare musl loader),
# these link against a handful more shared libs (e.g. libdevmapper)
# -- resolve and bundle them the same way build_initramfs() does
# for busybox, flattened into tools/lib/ (install-usb.sh points the
# bundled loader's --library-path at that one directory).
if [ -x "$loader" ]; then
"$loader" --list "$src" 2>/dev/null | grep -o '/[^ ]*' | sort -u | while read -r dep; do
[ -f "$dep" ] || continue
cp -a "$dep" "$IMAGE_DIR/tools/lib/$(basename "$dep")" 2>/dev/null || true
done
fi
done
local cfg="$IMAGE_DIR/tools/embed.cfg"
embed_cfg >"$cfg"
echo "$GRUB_MODULES" >"$IMAGE_DIR/tools/grub-modules.txt"
}
# --- 8. Assemble the release tree ------------------------------------------
assemble_image() {
build_slot_image
build_grub_cfg
# Config template: install-usb.sh only writes this if the user didn't
# supply their own kiosk.conf, so re-installs never clobber an existing
# on-device config.
cp "$PROJECT_DIR/config/kiosk.conf.example" "$IMAGE_DIR/data/kiosk.conf.example"
bundle_grub_tools
bundle_esp
echo "$KIOSK_VERSION" >"$IMAGE_DIR/.version"
( cd "$IMAGE_DIR" && find . -type f | sed 's#^\./##' | sort ) >"$IMAGE_DIR/.manifest"
}
# --- 9. RAUC bundle ---------------------------------------------------------
build_rauc_bundle() {
log "building RAUC bundle"
local bundle_dir="$WORKDIR/rauc-bundle"
rm -rf "$bundle_dir"
mkdir -p "$bundle_dir"
cp -a "$IMAGE_DIR/slot.img" "$bundle_dir/rootfs.ext4"
install -m 0755 "$SCRIPT_DIR/rauc-hook.sh" "$bundle_dir/hook.sh"
cat >"$bundle_dir/manifest.raucm" <<EOF
[update]
compatible=alpine-kiosk
version=$KIOSK_VERSION
[hooks]
filename=hook.sh
[image.rootfs]
filename=rootfs.ext4
hooks=post-install
EOF
mkdir -p "$DIST_DIR"
local bundle="$DIST_DIR/${IMAGE_NAME}-${KIOSK_VERSION}.raucb"
rm -f "$bundle"
"$RAUC_BIN" bundle \
--cert="$RAUC_CERT_PATH" --key="$RAUC_KEY_PATH" \
"$bundle_dir" "$bundle"
log "built $bundle"
}
package_release() {
@ -492,23 +750,25 @@ package_release() {
log "packaging $tarball"
tar -C "$IMAGE_DIR" -czf "$tarball" .
local sha256
sha256=$(sha256sum "$tarball" | awk '{print $1}')
local raucb_url=""
[ -n "${ARTIFACT_BASE_URL:-}" ] && raucb_url="${ARTIFACT_BASE_URL}${IMAGE_NAME}-${KIOSK_VERSION}.raucb"
cat >"$DIST_DIR/latest.json" <<EOF
{
"version": "$KIOSK_VERSION",
"file": "$(basename "$tarball")",
"url": "${ARTIFACT_BASE_URL:-}$(basename "$tarball")",
"sha256": "$sha256"
"file": "$(basename "$DIST_DIR/${IMAGE_NAME}-${KIOSK_VERSION}.raucb")",
"url": "$raucb_url"
}
EOF
log "done: $tarball"
log "done: $tarball and ${IMAGE_NAME}-${KIOSK_VERSION}.raucb"
}
require_root
ensure_rauc_keys
prepare_rootfs
build_rauc
configure_rootfs
build_initramfs
assemble_image
build_rauc_bundle
package_release

View file

@ -1,11 +1,19 @@
#!/bin/busybox sh
# Custom initramfs init for the diskless Alpine kiosk image.
#
# Boots entirely into RAM: finds the boot/data partition (by label),
# loop-mounts rootfs.squashfs from it read-only, layers a tmpfs overlay
# on top for writability, bind-mounts the boot partition into the new
# root at /media/bootdev (this is where kiosk.conf, the squashfs image
# and the kernel/initramfs used by auto-update live) and switches root.
# Boots entirely into RAM from one of two interchangeable RAUC A/B root
# slots (partitions labeled ALPINEKIOSK_A / ALPINEKIOSK_B, each holding
# boot/{vmlinuz-lts,initramfs-lts,rootfs.squashfs}) plus a separate,
# persistent ALPINEDATA partition (kiosk.conf, grubenv, RAUC's status
# file, etc -- never touched by a RAUC install, unlike the slots). GRUB
# picks which slot to boot (see build-image.sh's grub.cfg generation)
# and passes it on as the kernel's root=LABEL=... parameter; this script
# mounts that slot read-only, loop-mounts its rootfs.squashfs, layers a
# tmpfs overlay on top for writability, bind-mounts ALPINEDATA into the
# new root at /media/bootdev (symlinked as /kiosk.conf) and switches
# root. Nothing is ever written back into the squashfs or the slot
# partition itself at runtime -- RAUC only ever writes to the *other*,
# currently-inactive slot, from the full running system, never from here.
# Alpine's busybox is NOT built with standalone-shell support, so bare
# command names (cat, find, grep, ls, ...) do NOT fall back to busybox
@ -22,10 +30,10 @@ mount -t proc proc /proc
mount -t sysfs sysfs /sys
mount -t devtmpfs devtmpfs /dev 2>/dev/null || mdev -s
BOOT_LABEL="ALPINEKIOSK"
DATA_LABEL="ALPINEDATA"
NEWROOT=/mnt/newroot
mkdir -p /mnt/bootdev "$NEWROOT"
mkdir -p /mnt/bootdev /mnt/slot "$NEWROOT"
# Filesystem modules aren't tied to any bus device, so nothing ever
# auto-triggers loading them -- request explicitly. modprobe (not raw
@ -97,12 +105,26 @@ dump_diagnostics() {
dmesg | grep -iE 'usb|xhci|ehci|ohci|uhci|scsi|ata|sd[a-z] |block|pci' | tail -n 60
}
echo "[initramfs] waiting for boot device (LABEL=$BOOT_LABEL)..."
# GRUB sets root=LABEL=ALPINEKIOSK_A or root=LABEL=ALPINEKIOSK_B on the
# kernel cmdline depending on which slot it chose to boot (see grub.cfg,
# generated by build-image.sh) -- that's the single source of truth for
# which slot this boot is using, so parse it rather than hardcoding one.
SLOT_LABEL=""
for arg in $(cat /proc/cmdline); do
case "$arg" in
root=LABEL=*) SLOT_LABEL="${arg#root=LABEL=}" ;;
esac
done
[ -n "$SLOT_LABEL" ] || SLOT_LABEL="ALPINEKIOSK_A"
echo "[initramfs] waiting for boot devices (slot LABEL=$SLOT_LABEL, data LABEL=$DATA_LABEL)..."
i=0
DEV=""
SLOT_DEV=""
DATA_DEV=""
while [ $i -lt 60 ]; do
DEV=$(findfs "LABEL=$BOOT_LABEL" 2>/dev/null)
[ -n "$DEV" ] && [ -b "$DEV" ] && break
[ -z "$SLOT_DEV" ] && SLOT_DEV=$(findfs "LABEL=$SLOT_LABEL" 2>/dev/null)
[ -z "$DATA_DEV" ] && DATA_DEV=$(findfs "LABEL=$DATA_LABEL" 2>/dev/null)
{ [ -n "$SLOT_DEV" ] && [ -b "$SLOT_DEV" ] && [ -n "$DATA_DEV" ] && [ -b "$DATA_DEV" ]; } && break
if [ $((i % 10)) -eq 0 ] && [ $i -gt 0 ]; then
echo "[initramfs] still waiting (${i}s)..."
dump_diagnostics
@ -111,20 +133,25 @@ while [ $i -lt 60 ]; do
i=$((i + 1))
done
if [ -z "$DEV" ]; then
echo "[initramfs] FATAL: boot device with LABEL=$BOOT_LABEL not found after ${i}s"
if [ -z "$SLOT_DEV" ] || [ -z "$DATA_DEV" ]; then
echo "[initramfs] FATAL: slot device (LABEL=$SLOT_LABEL: ${SLOT_DEV:-not found}) or data device (LABEL=$DATA_LABEL: ${DATA_DEV:-not found}) missing after ${i}s"
dump_diagnostics
echo "[initramfs] dropping to a shell for manual inspection"
exec sh
fi
mount -t ext4 -o rw "$DEV" /mnt/bootdev || {
echo "[initramfs] FATAL: failed to mount $DEV"
mount -t ext4 -o ro "$SLOT_DEV" /mnt/slot || {
echo "[initramfs] FATAL: failed to mount $SLOT_DEV"
exec sh
}
mount -t ext4 -o rw "$DATA_DEV" /mnt/bootdev || {
echo "[initramfs] FATAL: failed to mount $DATA_DEV"
exec sh
}
mkdir -p /mnt/squashfs /mnt/overlay/upper /mnt/overlay/work
mount -t squashfs -o loop,ro /mnt/bootdev/boot/rootfs.squashfs /mnt/squashfs || {
mount -t squashfs -o loop,ro /mnt/slot/boot/rootfs.squashfs /mnt/squashfs || {
echo "[initramfs] FATAL: failed to mount rootfs.squashfs"
exec sh
}
@ -138,9 +165,13 @@ mount -t overlay overlay \
mkdir -p "$NEWROOT/media/bootdev"
# Move the already-mounted boot device into the new root so the running
# system (and the update script) can read/write kiosk.conf, the squashfs
# image and boot files.
# Move the already-mounted, persistent data partition into the new root
# so the running system (and kiosk-update.sh) can read/write kiosk.conf,
# grubenv, RAUC's status file, etc. The slot partition mounted at
# /mnt/slot and its squashfs loop mount at /mnt/squashfs are left as-is
# (not moved) -- they just need to stay mounted, as the overlay's
# lowerdir, for the life of this boot; nothing reads them by path after
# this point.
mount --move /mnt/bootdev "$NEWROOT/media/bootdev"
# kiosk.conf lives on the boot partition; expose it at the filesystem

23
build/rauc-hook.sh Normal file
View file

@ -0,0 +1,23 @@
#!/bin/sh
# Bundled inside the .raucb (see build-image.sh's build_rauc_bundle) and
# run by RAUC *on the target device* after it raw-copies rootfs.ext4
# onto the inactive slot's block device. That raw copy overwrites the
# target partition's superblock -- including its filesystem label --
# with whatever the bundled image itself carries (a neutral label, since
# the same image seeds both slots at first install, see
# install-usb.sh), which would otherwise leave both slots indistinguishable.
# Re-label the just-written slot back to ALPINEKIOSK_A/_B here so
# /etc/rauc/system.conf's by-label device paths and grub.cfg's
# `search --label` keep working.
set -eu
case "${RAUC_HOOK:-}" in
post-install)
[ -n "${RAUC_SLOT_DEVICE:-}" ] || exit 0
[ -n "${RAUC_SLOT_BOOTNAME:-}" ] || exit 0
# Full path, not just `e2label`: hooks run with whatever (possibly
# minimal) PATH RAUC itself has, and e2label lives in /usr/sbin (the
# e2fsprogs-extra package, separate from e2fsprogs' own mke2fs/e2fsck).
/usr/sbin/e2label "$RAUC_SLOT_DEVICE" "ALPINEKIOSK_${RAUC_SLOT_BOOTNAME}"
;;
esac

View file

@ -0,0 +1,21 @@
#!/sbin/openrc-run
# Marks the currently booted RAUC slot as good once the default runlevel
# is reached (resets grub.cfg's boot-try counter, see build-image.sh's
# grub.cfg generation, so GRUB keeps booting this slot). If the system
# never gets this far -- kernel panic, broken rootfs, any failure before
# OpenRC reaches "default" -- the try counter keeps climbing until GRUB
# falls back to the other, previously-good slot on its own. This does
# NOT wait for Chromium to actually render anything, only that OpenRC's
# default runlevel was reached; see README's known limitations.
description="Mark the current RAUC slot as good"
depend() {
after kiosk wayvnc amweb
}
start() {
ebegin "Marking current RAUC slot as good"
rauc status mark-good booted
eend $?
}

View file

@ -0,0 +1,27 @@
# RAUC system configuration for the alpine-kiosk A/B rootfs slots.
#
# grubenv/statusfile live on the persistent ALPINEDATA partition
# (mounted at /media/bootdev), which is never a RAUC slot itself and is
# never touched by an install -- only the two rootfs slots below are.
[system]
compatible=alpine-kiosk
bootloader=grub
grubenv=/media/bootdev/boot/grub/grubenv
statusfile=/media/bootdev/rauc.status
mountprefix=/mnt/rauc
[keyring]
path=/etc/rauc/ca.cert.pem
# device= paths are /dev/disk/by-label/* symlinks created at boot by
# kiosk-apply-config.sh (via busybox findfs) -- this image runs mdev,
# not udev/eudev, so nothing else maintains /dev/disk/by-label.
[slot.rootfs.0]
device=/dev/disk/by-label/ALPINEKIOSK_A
type=ext4
bootname=A
[slot.rootfs.1]
device=/dev/disk/by-label/ALPINEKIOSK_B
type=ext4
bootname=B

View file

@ -180,4 +180,15 @@ fi
rc-service networking restart >/dev/null 2>&1 || true
mkdir -p /run/user/1000
chown 1000:1000 /run/user/1000
chown 1000:1000 /run/user/1000
# --- RAUC slot device symlinks -------------------------------------------
# This image runs mdev, not udev/eudev, so /dev/disk/by-label/* isn't
# maintained automatically -- create the handful of symlinks
# /etc/rauc/system.conf's slot device= entries need, the same way
# build/initramfs-init finds partitions (busybox findfs).
mkdir -p /dev/disk/by-label
for label in ALPINEKIOSK_A ALPINEKIOSK_B ALPINEDATA; do
dev=$(findfs "LABEL=$label" 2>/dev/null) || true
[ -n "$dev" ] && [ -b "$dev" ] && ln -sf "$dev" "/dev/disk/by-label/$label"
done

View file

@ -1,9 +1,18 @@
#!/bin/sh
# Auto-update: compares the running version (CI_PIPELINE_ID baked into
# the image at build time) against UPDATE_URL's manifest. If newer,
# downloads the release tar.gz, removes files that existed in the old
# release but not the new one, extracts the new release over the boot
# partition, then reboots if AUTO_REBOOT=true.
# Auto-update via RAUC: compares the running version (baked into
# /etc/kiosk-version at build time, see build-image.sh) against
# UPDATE_URL's manifest. If newer, downloads the .raucb bundle and hands
# it to `rauc install`, which verifies its signature against
# /etc/rauc/ca.cert.pem and writes it into the *inactive* slot -- the
# slot this script is currently running from is never touched. On
# success the new slot is marked active for next boot, then this reboots
# if AUTO_REBOOT=true.
#
# This script has no rollback logic of its own: if a newly installed
# slot fails to boot cleanly, GRUB's own try-counter (see grub.cfg,
# generated by build-image.sh) falls back to the previous slot
# automatically, and rauc-mark-good (the OpenRC service) only confirms a
# slot once the default runlevel is reached on it.
set -u
LOG_TAG="kiosk-update"
@ -15,23 +24,15 @@ log() { logger -t "$LOG_TAG" "$*"; echo "$LOG_TAG: $*"; }
: "${UPDATE_URL:=}"
BOOTDEV=/media/bootdev
CURRENT_VERSION_FILE="$BOOTDEV/.version"
MANIFEST_FILE="$BOOTDEV/.manifest"
CURRENT_VERSION_FILE=/etc/kiosk-version
# Checked by start-kiosk.sh's restart loop, which pauses (doesn't
# relaunch cage/chromium) while this exists. Removing it is handled by
# the cleanup trap below so the kiosk always resumes on any exit path
# (success, failure, or an update that turned out not to be needed) --
# never left permanently paused just because a download failed partway.
# the cleanup trap below so the kiosk always resumes on any exit path.
PAUSE_MARKER=/tmp/.kiosk-update-in-progress
# On the boot partition (real storage), NOT /tmp (tmpfs, i.e. RAM): a
# ~500MB release staged in tmpfs was the single largest RAM consumer
# during an update, on top of everything else already resident (kernel,
# Chromium, the rest of the ephemeral overlay) -- the reason this image
# needed 2GB RAM as a floor. The boot partition already has to hold one
# full release, so it comfortably has room for a second copy during
# staging; RAM does not. Any leftover staging dir from a previous
# run that got interrupted (power loss, crash) is wiped first so it
# can't silently accumulate across runs.
# On the boot partition (real storage), NOT /tmp (tmpfs, i.e. RAM) --
# same reasoning as before RAUC: a multi-hundred-MB bundle download
# shouldn't compete with everything else resident for RAM. Any leftover
# staging dir from a previous interrupted run is wiped first.
WORKDIR="$BOOTDEV/.update-staging"
rm -rf "$WORKDIR"
mkdir -p "$WORKDIR"
@ -61,13 +62,16 @@ if ! curl -fsSL -o "$WORKDIR/latest.json" "$UPDATE_URL"; then
fi
REMOTE_VERSION=$(jq -r '.version' "$WORKDIR/latest.json")
REMOTE_FILE_URL=$(jq -r '.url' "$WORKDIR/latest.json")
REMOTE_SHA256=$(jq -r '.sha256 // empty' "$WORKDIR/latest.json")
BUNDLE_URL=$(jq -r '.url' "$WORKDIR/latest.json")
if [ -z "$REMOTE_VERSION" ] || [ "$REMOTE_VERSION" = "null" ]; then
log "manifest missing version field"
exit 1
fi
if [ -z "$BUNDLE_URL" ] || [ "$BUNDLE_URL" = "null" ]; then
log "manifest missing url field"
exit 1
fi
if [ "$REMOTE_VERSION" -le "$CURRENT_VERSION" ] 2>/dev/null; then
log "already up to date"
@ -76,84 +80,38 @@ fi
log "new version available: $REMOTE_VERSION (have $CURRENT_VERSION)"
# Stop the kiosk session before downloading: frees up RAM/CPU on
# resource-constrained hardware for the download+extract, and clears
# out Chromium's disk cache (--disk-cache-dir=/tmp, see start-kiosk.sh)
# before it can compete with the update for space in /tmp (which is
# tmpfs-backed, i.e. RAM, same as everything else in this ephemeral
# overlay). touch the pause marker first so start-kiosk.sh's restart
# loop doesn't immediately relaunch chromium once cage exits (which it
# does on its own once its one client/chromium is gone).
# Stop the kiosk session before downloading, same reasoning as before
# RAUC: frees RAM/CPU on resource-constrained hardware, and clears
# Chromium's disk cache (--disk-cache-dir=/tmp) before it competes with
# the download for tmpfs space.
log "stopping kiosk session and clearing /tmp"
touch "$PAUSE_MARKER"
pkill -f chromium 2>/dev/null || true
pkill cage 2>/dev/null || true
find /tmp -mindepth 1 -not -name "$(basename "$PAUSE_MARKER")" -exec rm -rf {} + 2>/dev/null || true
rm -Rf ./kiosk/.config/chromium/BrowserMetrics
# WORKDIR lives on the boot partition (see above), not /tmp, so
# clearing /tmp above doesn't touch it -- latest.json and the REMOTE_*
# values already parsed from it are both still intact here.
log "downloading and extracting $REMOTE_FILE_URL"
# Streamed straight from curl into tar -- the release tarball is now
# too large to hold both the compressed download and its extracted
# copy in tmpfs (RAM) at once. sha256sum reads the same stream via a
# named pipe alongside tar (tee duplicates it to both); /bin/sh here is
# busybox ash, which has neither bash's pipefail nor >() process
# substitution, so curl's and tar's exit codes are captured explicitly
# instead of relying on either.
mkdir -p "$WORKDIR/extracted"
mkfifo "$WORKDIR/pipe"
sha256sum "$WORKDIR/pipe" >"$WORKDIR/actual.sha256" &
SHA_PID=$!
{ curl -fsSL "$REMOTE_FILE_URL"; echo $? >"$WORKDIR/curl.exit"; } \
| tee "$WORKDIR/pipe" \
| tar -xz -C "$WORKDIR/extracted"
TAR_EXIT=$?
wait "$SHA_PID"
CURL_EXIT=$(cat "$WORKDIR/curl.exit" 2>/dev/null || echo 1)
if [ "$CURL_EXIT" != "0" ]; then
log "download failed (curl exit $CURL_EXIT)"
exit 1
fi
if [ "$TAR_EXIT" != "0" ]; then
log "extraction failed (tar exit $TAR_EXIT)"
log "downloading $BUNDLE_URL"
BUNDLE="$WORKDIR/update.raucb"
if ! curl -fsSL -o "$BUNDLE" "$BUNDLE_URL"; then
log "download failed"
exit 1
fi
if [ -n "$REMOTE_SHA256" ]; then
ACTUAL_SHA256=$(awk '{print $1}' "$WORKDIR/actual.sha256")
if [ "$ACTUAL_SHA256" != "$REMOTE_SHA256" ]; then
log "checksum mismatch, aborting (expected $REMOTE_SHA256, got $ACTUAL_SHA256)"
exit 1
fi
fi
if [ ! -f "$WORKDIR/extracted/.manifest" ]; then
log "release is missing .manifest, aborting"
log "installing bundle via rauc (verifies signature, writes inactive slot)"
if ! rauc install "$BUNDLE"; then
log "rauc install failed"
exit 1
fi
# Remove files that existed in the currently installed release but are
# absent from the new one (old kernels, stale squashfs, etc).
if [ -f "$MANIFEST_FILE" ]; then
while IFS= read -r rel; do
[ -z "$rel" ] && continue
if ! grep -qxF "$rel" "$WORKDIR/extracted/.manifest"; then
log "removing stale file: $rel"
rm -f "$BOOTDEV/$rel"
fi
done <"$MANIFEST_FILE"
log "activating newly installed slot for next boot"
if ! rauc status mark-active other; then
log "rauc status mark-active failed"
exit 1
fi
log "installing new release over $BOOTDEV"
cp -a "$WORKDIR/extracted/." "$BOOTDEV/"
sync
if [ "$AUTO_REBOOT" = "true" ]; then
log "update applied, rebooting"
sync
reboot
else
log "update applied, reboot required manually"

View file

@ -66,7 +66,11 @@ ETH_IFACE=""
# --- Auto update ---------------------------------------------------------
# When enabled, the kiosk periodically checks UPDATE_URL for a JSON
# manifest describing the latest build (see build/build-image.sh) and,
# if the version is newer than the running one, downloads and applies it.
# if the version is newer than the running one, downloads the signed
# RAUC bundle (.raucb) it points at and installs it into the *inactive*
# A/B slot via `rauc install` -- the slot currently running is never
# touched, and a slot that fails to boot cleanly is rolled back
# automatically by GRUB (see README's "Updating and rollback").
AUTO_UPDATE="true"
UPDATE_URL="https://s3.jonasled.de/alamos-alpine-kiosk/latest.json"
# How often to check, in minutes.

View file

@ -1,27 +1,37 @@
#!/bin/bash
# Formats a USB disk and installs a pre-built alpine-kiosk-<version>.tar.gz
# release onto it, making it bootable on both BIOS (legacy, via extlinux)
# and UEFI (via a standalone GRUB binary) firmware.
# release onto it as two interchangeable RAUC A/B root slots, bootable on
# both BIOS (legacy, real GRUB i386-pc) and UEFI (a standalone GRUB stub)
# firmware.
#
# Usage:
# sudo install/install-usb.sh /dev/sdX dist/alpine-kiosk-123.tar.gz [kiosk.conf]
#
# Creates two partitions:
# 1. ESP FAT32, ~100MiB, holds EFI/BOOT/BOOTX64.EFI (found by firmware
# without any NVRAM boot entry -- the standard "removable
# media" fallback path). Its config is baked in at build time
# and finds partition 2 by filesystem label, so it doesn't
# care which partition number it ends up as.
# 2. DATA ext4, labeled ALPINEKIOSK, rest of the disk -- the squashfs
# system, kernel/initramfs, kiosk.conf, etc. Marked as the
# BIOS-active partition; extlinux is installed onto it and the
# MBR boot code chainloads there on legacy boot.
# Creates four partitions:
# 1. ESP FAT32, ~100MiB, holds EFI/BOOT/BOOTX64.EFI -- a
# tiny GRUB stub (baked in at build time) that just
# finds partition 4 by label and chainloads the
# real, editable grub.cfg from there.
# 2. ALPINEKIOSK_A ext4, RAUC slot "rootfs.0" -- boot/{vmlinuz-lts,
# initramfs-lts,rootfs.squashfs}.
# 3. ALPINEKIOSK_B ext4, RAUC slot "rootfs.1" -- identical layout,
# the second slot. Both are seeded with the exact
# same content here so the disk boots either one
# immediately; kiosk-update.sh's `rauc install`
# only ever writes to whichever of the two isn't
# currently running.
# 4. ALPINEDATA ext4, rest of the disk -- kiosk.conf, grub.cfg +
# grubenv (GRUB's slot-selection state, seeded
# here), RAUC's status file. Persistent; never
# touched by a RAUC install.
#
# The release tarball bundles its own extlinux binary + mbr.bin (under
# tools/, see build/build-image.sh's bundle_installer_tools) and its own
# prebuilt BOOTX64.EFI (under esp/, see bundle_efi_boot), so this script
# needs no `syslinux` or `grub` packages installed on the installer host
# -- only generic partitioning/filesystem tools.
# Real GRUB replaces extlinux entirely, BIOS included: RAUC's GRUB
# bootloader backend needs the environment-block (grubenv) support
# extlinux has no equivalent of. The release tarball bundles its own
# grub-mkimage/grub-bios-setup/grub-editenv + i386-pc module set (see
# build/build-image.sh's bundle_grub_tools) and its own prebuilt
# BOOTX64.EFI, so this script needs no `grub` package on the installer
# host -- only generic partitioning/filesystem tools.
#
# WARNING: this WIPES the target device entirely.
set -euo pipefail
@ -34,8 +44,10 @@ die() { echo "[install-usb] ERROR: $*" >&2; exit 1; }
DEVICE="${1:-}"
RELEASE_TARBALL="${2:-}"
KIOSK_CONF="${3:-}"
LABEL="ALPINEKIOSK"
ESP_LABEL="ALPINEESP"
LABEL_A="ALPINEKIOSK_A"
LABEL_B="ALPINEKIOSK_B"
DATA_LABEL="ALPINEDATA"
[ -n "$DEVICE" ] && [ -n "$RELEASE_TARBALL" ] || {
echo "Usage: $0 <device> <release-tar.gz> [kiosk.conf]" >&2
@ -44,7 +56,7 @@ ESP_LABEL="ALPINEESP"
[ -b "$DEVICE" ] || die "$DEVICE is not a block device"
[ -f "$RELEASE_TARBALL" ] || die "$RELEASE_TARBALL not found"
for bin in parted mkfs.ext4 mkfs.vfat dd lsblk; do
for bin in parted mkfs.ext4 mkfs.vfat dd lsblk e2label stat wipefs partprobe findmnt; do
command -v "$bin" >/dev/null 2>&1 || die "required tool '$bin' not found in PATH"
done
@ -60,6 +72,27 @@ echo
read -r -p "Type the device path again to confirm ($DEVICE): " CONFIRM
[ "$CONFIRM" = "$DEVICE" ] || die "confirmation did not match, aborting"
STAGE=$(mktemp -d)
ESP_MNT=$(mktemp -d)
DATA_MNT=$(mktemp -d)
cleanup() {
umount "$ESP_MNT" 2>/dev/null || true
umount "$DATA_MNT" 2>/dev/null || true
rm -rf "$STAGE" "$ESP_MNT" "$DATA_MNT"
}
trap cleanup EXIT
log "extracting $RELEASE_TARBALL"
tar -xzf "$RELEASE_TARBALL" -C "$STAGE"
[ -f "$STAGE/slot.img" ] || die "release tarball has no slot.img (rebuild with a current build-image.sh)"
[ -d "$STAGE/esp/EFI" ] || die "release tarball has no esp/EFI (rebuild with a current build-image.sh)"
[ -d "$STAGE/data" ] || die "release tarball has no data/ (rebuild with a current build-image.sh)"
[ -d "$STAGE/tools" ] || die "release tarball has no tools/ (rebuild with a current build-image.sh)"
SLOT_SIZE_BYTES=$(stat -c%s "$STAGE/slot.img")
SLOT_SIZE_MIB=$(( (SLOT_SIZE_BYTES + 1024 * 1024 - 1) / (1024 * 1024) ))
log "slot image size: ${SLOT_SIZE_MIB}MiB"
part_suffix() {
if [[ "$DEVICE" =~ [0-9]$ ]]; then
echo "${DEVICE}p$1"
@ -68,17 +101,24 @@ part_suffix() {
fi
}
ESP_PART=$(part_suffix 1)
DATA_PART=$(part_suffix 2)
A_PART=$(part_suffix 2)
B_PART=$(part_suffix 3)
DATA_PART=$(part_suffix 4)
log "wiping existing partition table on $DEVICE"
wipefs -a "$DEVICE"
log "creating partition table (1: ESP fat32, 2: ext4 data)"
ESP_END_MIB=101
A_END_MIB=$((ESP_END_MIB + SLOT_SIZE_MIB))
B_END_MIB=$((A_END_MIB + SLOT_SIZE_MIB))
log "creating partition table (1: ESP fat32, 2+3: ${SLOT_SIZE_MIB}MiB ext4 A/B slots, 4: ext4 data)"
parted -s "$DEVICE" mklabel msdos
parted -s "$DEVICE" mkpart primary fat32 1MiB 101MiB
parted -s "$DEVICE" mkpart primary fat32 1MiB "${ESP_END_MIB}MiB"
parted -s "$DEVICE" set 1 esp on
parted -s "$DEVICE" mkpart primary ext4 101MiB 100%
parted -s "$DEVICE" set 2 boot on
parted -s "$DEVICE" mkpart primary ext4 "${ESP_END_MIB}MiB" "${A_END_MIB}MiB"
parted -s "$DEVICE" mkpart primary ext4 "${A_END_MIB}MiB" "${B_END_MIB}MiB"
parted -s "$DEVICE" mkpart primary ext4 "${B_END_MIB}MiB" 100%
partprobe "$DEVICE" || true
udevadm settle 2>/dev/null || true
@ -105,37 +145,30 @@ wait_for_part() {
mknod "$part" b $majmin
}
wait_for_part "$ESP_PART"
wait_for_part "$A_PART"
wait_for_part "$B_PART"
wait_for_part "$DATA_PART"
log "formatting $ESP_PART as FAT32 (label=$ESP_LABEL)"
mkfs.vfat -F 32 -n "$ESP_LABEL" "$ESP_PART"
log "formatting $DATA_PART as ext4 (label=$LABEL)"
mkfs.ext4 -F -L "$LABEL" "$DATA_PART"
log "seeding $A_PART and $B_PART from slot.img (identical content, distinct labels)"
dd if="$STAGE/slot.img" of="$A_PART" bs=4M conv=notrunc status=none
dd if="$STAGE/slot.img" of="$B_PART" bs=4M conv=notrunc status=none
e2label "$A_PART" "$LABEL_A"
e2label "$B_PART" "$LABEL_B"
STAGE=$(mktemp -d)
ESP_MNT=$(mktemp -d)
DATA_MNT=$(mktemp -d)
cleanup() {
umount "$ESP_MNT" 2>/dev/null || true
umount "$DATA_MNT" 2>/dev/null || true
rm -rf "$STAGE" "$ESP_MNT" "$DATA_MNT"
}
trap cleanup EXIT
log "formatting $DATA_PART as ext4 (label=$DATA_LABEL)"
mkfs.ext4 -F -L "$DATA_LABEL" "$DATA_PART"
mount "$ESP_PART" "$ESP_MNT"
mount "$DATA_PART" "$DATA_MNT"
log "extracting $RELEASE_TARBALL"
tar -xzf "$RELEASE_TARBALL" -C "$STAGE"
log "installing EFI boot files to the ESP"
[ -d "$STAGE/esp/EFI" ] || die "release tarball has no esp/EFI (rebuild with a current build-image.sh)"
cp -a "$STAGE/esp/." "$ESP_MNT/"
log "installing system files to the data partition"
cp -a "$STAGE"/boot "$STAGE"/tools "$STAGE"/.manifest "$STAGE"/.version "$STAGE"/kiosk.conf.example "$DATA_MNT/"
[ -f "$STAGE/kiosk.conf" ] && cp -a "$STAGE/kiosk.conf" "$DATA_MNT/"
log "installing data partition files (kiosk.conf template, grub.cfg)"
cp -a "$STAGE/data/." "$DATA_MNT/"
if [ -n "$KIOSK_CONF" ]; then
[ -f "$KIOSK_CONF" ] || die "kiosk.conf $KIOSK_CONF not found"
@ -146,21 +179,48 @@ elif [ ! -f "$DATA_MNT/kiosk.conf" ]; then
cp "$DATA_MNT/kiosk.conf.example" "$DATA_MNT/kiosk.conf"
fi
EXTLINUX_BIN="$DATA_MNT/tools/extlinux"
MUSL_LOADER="$DATA_MNT/tools/lib/ld-musl-x86_64.so.1"
MBR_BIN="$DATA_MNT/tools/mbr.bin"
for f in "$EXTLINUX_BIN" "$MUSL_LOADER" "$MBR_BIN"; do
TOOLS_LIB="$STAGE/tools/lib"
MUSL_LOADER="$TOOLS_LIB/ld-musl-x86_64.so.1"
GRUB_MKIMAGE="$STAGE/tools/grub-mkimage"
GRUB_BIOS_SETUP="$STAGE/tools/grub-bios-setup"
GRUB_EDITENV="$STAGE/tools/grub-editenv"
GRUB_MODDIR="$STAGE/tools/grub/i386-pc"
GRUB_EMBED_CFG="$STAGE/tools/embed.cfg"
GRUB_MODULES_FILE="$STAGE/tools/grub-modules.txt"
for f in "$MUSL_LOADER" "$GRUB_MKIMAGE" "$GRUB_BIOS_SETUP" "$GRUB_EDITENV" "$GRUB_EMBED_CFG" "$GRUB_MODULES_FILE"; do
[ -f "$f" ] || die "bundled installer tool $f missing from release tarball (rebuild with a current build-image.sh)"
done
chmod +x "$EXTLINUX_BIN" "$MUSL_LOADER"
[ -d "$GRUB_MODDIR" ] || die "bundled GRUB i386-pc module dir missing from release tarball"
chmod +x "$MUSL_LOADER" "$GRUB_MKIMAGE" "$GRUB_BIOS_SETUP" "$GRUB_EDITENV"
log "installing extlinux bootloader (BIOS)"
"$MUSL_LOADER" "$EXTLINUX_BIN" --install "$DATA_MNT/boot"
run_tool() {
"$MUSL_LOADER" --library-path "$TOOLS_LIB" "$@"
}
# MBR boot code so BIOS firmware hands off to extlinux on the data
# partition (marked active/boot above). UEFI firmware ignores this and
# boots esp/EFI/BOOT/BOOTX64.EFI instead.
dd if="$MBR_BIN" of="$DEVICE" bs=4M count=1 conv=notrunc status=none
log "building GRUB BIOS core image (prefix on the data partition's /boot/grub)"
CORE_IMG="$STAGE/core.img"
GRUB_MODULES=$(cat "$GRUB_MODULES_FILE")
# shellcheck disable=SC2086
run_tool "$GRUB_MKIMAGE" \
--directory="$GRUB_MODDIR" \
--format=i386-pc \
--output="$CORE_IMG" \
--prefix=/boot/grub \
--config="$GRUB_EMBED_CFG" \
biosdisk $GRUB_MODULES
log "installing GRUB to the MBR + post-MBR gap (BIOS boot)"
run_tool "$GRUB_BIOS_SETUP" \
--directory="$GRUB_MODDIR" \
--core-image="$CORE_IMG" \
--skip-fs-probe \
"$DEVICE"
log "seeding grubenv (both slots good, ORDER=\"A B\")"
mkdir -p "$DATA_MNT/boot/grub"
run_tool "$GRUB_EDITENV" "$DATA_MNT/boot/grub/grubenv" create
run_tool "$GRUB_EDITENV" "$DATA_MNT/boot/grub/grubenv" set \
ORDER="A B" A_OK=1 A_TRY=0 B_OK=1 B_TRY=0
sync
log "done. $DEVICE is ready to boot as the kiosk (BIOS and UEFI)."
log "done. $DEVICE is ready to boot as the kiosk (BIOS and UEFI), slots A and B both seeded."