diff --git a/Makefile b/Makefile index 14799d3..5f6cb95 100644 --- a/Makefile +++ b/Makefile @@ -14,6 +14,7 @@ export ROOT_LABEL ?= FORGE_ROOT export EFI_LABEL ?= FORGE_EFI export APP_LABEL ?= FORGE_APPS export ATOMIC_BASE ?= 1 +export ROLLBACK_SLOTS ?= 2 desktop_enabled := $(filter 1 true TRUE yes YES on ON,$(ENABLE_DESKTOP)) export IMAGE_SIZE_MIB ?= $(if $(filter gnome,$(DESKTOP)),12288,$(if $(desktop_enabled),6144,4096)) export ESP_SIZE_MIB ?= 256 @@ -43,6 +44,7 @@ help: ' make gnome-rootfs - assemble a Debian GNOME desktop rootfs' \ ' make image - build a bootable GPT/UEFI disk image' \ ' make image builds an atomic read-only base plus writable app/state layer' \ + ' ROLLBACK_SLOTS=1 make image - disable extra base rollback slots' \ ' make gnome-image - build a bootable GPT/UEFI GNOME disk image' \ ' make secure-boot-keys - generate a local Secure Boot signing keypair' \ ' make run - boot kernel + initramfs in QEMU' \ diff --git a/README.md b/README.md index 73fe317..be11ab8 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ As of `2026-06-17`, the project defaults to: - a minimal root filesystem with systemd as `/sbin/init` - a bootable GPT disk image with: - an EFI system partition - - a read-only ext4 base root partition + - read-only ext4 base root partitions for current and rollback boots - a writable ext4 app/state partition - a faster QEMU smoke-test path using `kernel + initramfs` @@ -74,6 +74,21 @@ mount -a systemctl restart forgeos-nix-bootstrap.service nix-daemon.socket nix-daemon.service ``` +Base OS rollback slots are managed in the installed OS with `forgeos-rollback`. As root, create a rollback snapshot before changing the base OS: + +```bash +forgeos-rollback status +forgeos-rollback create +``` + +If the current base fails later, reboot and choose `ForgeOS rollback selector` from GRUB. After booting the rollback slot and confirming it works, make it the current base again: + +```bash +forgeos-rollback promote +``` + +If `forgeos-rollback create` says the source and destination are the same slot, check `forgeos-rollback status`. You are probably already booted from `root-rollback`; use `forgeos-rollback promote` from that boot to copy it back to `root`. + To build a UEFI disk image: ```bash @@ -121,9 +136,11 @@ sudo make install DISK=/dev/nvme0n1 - The root filesystem is not copied from a distro. It is assembled from upstream systemd, upstream D-Bus, upstream Linux-PAM, upstream BusyBox rescue tools, copied runtime library dependencies for systemd, D-Bus, PAM, and BusyBox, and the files in `overlay/rootfs/`. - The default removable-media boot path uses GRUB at `EFI/BOOT/BOOTX64.EFI` and keeps the kernel EFI stub at `EFI/BOOT/FORGEOS.EFI`. -- The built-in kernel command line targets `PARTLABEL=root` and keeps Intel graphics modesetting enabled, with a couple of laptop display workarounds for newer firmware. +- The default GRUB menu boots `PARTLABEL=root` and includes a rollback selector for any additional base slots, starting with `PARTLABEL=root-rollback`. The primary selector entries use an initramfs-assisted fixed-framebuffer handoff so recovery boots stay visible. - For default direct QEMU testing, `scripts/run-qemu.sh direct` uses the generated `rootfs.cpio.gz`. -- The host-side installer expands the writable app/state partition to fill larger target disks. The base root partition stays fixed-size and read-only at runtime. +- The host-side installer expands the writable app/state partition to fill larger target disks. Base root partitions stay fixed-size and read-only at runtime. +- Normal atomic images build with `ROLLBACK_SLOTS=2` by default: one current base slot plus one rollback base slot. Set `ROLLBACK_SLOTS=1` to use the old single-base-slot layout. +- `forgeos-rollback` can copy the currently booted read-only base into an inactive rollback slot and promote a booted rollback slot back to `root`. - systemd is installed as `/sbin/init`. BusyBox remains available for `/bin/sh`, PAM-backed `/bin/login`, early switch-root support, and emergency command-line tools. - Nix `2.34.0` is staged from the official `x86_64-linux` binary tarball and configured in multi-user daemon mode with `/nix`, `nixbld` build users, `cache.nixos.org`, and `nix-command`/`flakes` enabled. The `/nix` tree lives in the mutable app layer on disk images. Override `NIX_VERSION` or `NIX_SYSTEM` if you intentionally want a different upstream tarball. - A ForgeOS-native `neofetch` command is included in the overlay with a custom ForgeOS ASCII logo at `/usr/share/neofetch/ascii/distro/forgeos`. @@ -134,7 +151,8 @@ sudo make install DISK=/dev/nvme0n1 - `ENABLE_DESKTOP=1` adds an Openbox/tint2/PCManFM desktop layer to the normal ForgeOS rootfs by extracting a minimal Debian package payload while preserving the source-built systemd and D-Bus daemons. - `DESKTOP=gnome` switches rootfs assembly to a Debian package bootstrap because GNOME depends on a large desktop stack that ForgeOS does not source-build yet. -Set `BOOTLOADER=stub` when building an image to use the direct kernel EFI-stub fallback path instead of GRUB. +Set `BOOTLOADER=stub` only when building a direct kernel EFI-stub image with its own firmware-provided load options or a kernel built with an embedded command line. +The rollback selector is generated by the default GRUB bootloader path. ## Secure Boot Signing @@ -168,7 +186,7 @@ Linux-PAM, systemd, and D-Bus are built with Meson/Ninja and staged dynamically. - UEFI only for now; BIOS boot is not implemented. - The kernel config includes common desktop, laptop, and QEMU storage/input paths, but not every vendor driver. - Wireless firmware, GPU acceleration, stock Microsoft-trusted Secure Boot/shim integration, audio, and power-management polish are not bundled yet. -- Nix is the package manager for the mutable app layer, but ForgeOS does not yet have a declarative NixOS-style base OS rebuild/update flow or rollback selector. +- Nix is the package manager for the mutable app layer, but ForgeOS does not yet have a declarative NixOS-style base OS rebuild/update flow. - Account management is limited to the built-in `forge` and `root` accounts. - The Openbox desktop layer is a staged runtime payload, not an in-OS package manager. - GNOME support is currently a package-bootstrapped desktop flavor, not a source-built ForgeOS desktop stack. diff --git a/config/kernel.fragment b/config/kernel.fragment index aa98d43..b9693a0 100644 --- a/config/kernel.fragment +++ b/config/kernel.fragment @@ -87,6 +87,4 @@ CONFIG_R8169=y CONFIG_IGB=y CONFIG_IGC=y CONFIG_TG3=y -CONFIG_CMDLINE_BOOL=y -CONFIG_CMDLINE="root=PARTLABEL=root rootfstype=ext4 rootwait ro console=tty0 console=ttyS0,115200n8 loglevel=7 ignore_loglevel i915.enable_psr=0 i915.fastboot=0" -# CONFIG_CMDLINE_OVERRIDE is not set +# CONFIG_CMDLINE_BOOL is not set diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 7e7bc6e..8563ff3 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -22,13 +22,15 @@ ForgeOS currently targets UEFI systems. 1. The disk image contains a GPT. 2. Partition 1 is a FAT32 EFI System Partition labeled `FORGE_EFI`. -3. Partition 2 is an ext4 base OS partition labeled `FORGE_ROOT` with GPT partition label `root`. -4. Partition 3 is an ext4 writable app/state partition labeled `FORGE_APPS` with GPT partition label `apps`. -5. By default, `out/BOOTX64.EFI` is a standalone GRUB UEFI fallback loader with a small debug boot menu. -6. The built kernel EFI stub is copied to `EFI/BOOT/FORGEOS.EFI`. -7. UEFI firmware executes GRUB from `EFI/BOOT/BOOTX64.EFI`, and GRUB starts the kernel. -8. The kernel mounts `PARTLABEL=root` as the real root filesystem with `ro` so the base OS stays atomic. -9. systemd starts as `/sbin/init`, mounts the app layer at `/forge`, bind-mounts mutable paths from it, activates the D-Bus system bus, starts `systemd-logind`, `systemd-resolved`, and `systemd-networkd`, reaches `multi-user.target`, and launches PAM-backed login prompts on `tty1` and `ttyS0`. +3. Partition 2 is the current ext4 base OS slot labeled `FORGE_ROOT` with GPT partition label `root`. +4. Normal atomic images reserve additional read-only base OS slots for rollback selection. With the default `ROLLBACK_SLOTS=2`, partition 3 is labeled `FORGE_ROOT_1` with GPT partition label `root-rollback`. +5. The ext4 writable app/state partition is labeled `FORGE_APPS` with GPT partition label `apps`. With the default rollback layout it is partition 4; with `ROLLBACK_SLOTS=1` it is partition 3. +6. By default, `out/BOOTX64.EFI` is a standalone GRUB UEFI fallback loader with a small debug boot menu. +7. The built kernel EFI stub is copied to `EFI/BOOT/FORGEOS.EFI`. +8. UEFI firmware executes GRUB from `EFI/BOOT/BOOTX64.EFI`, and GRUB starts the kernel. +9. The default menu mounts `PARTLABEL=root` as the real root filesystem with `ro` so the current base OS stays atomic. +10. The GRUB rollback selector can instead boot another base slot such as `PARTLABEL=root-rollback`. +11. systemd starts as `/sbin/init`, mounts the app layer at `/forge`, bind-mounts mutable paths from it, activates the D-Bus system bus, starts `systemd-logind`, `systemd-resolved`, and `systemd-networkd`, reaches `multi-user.target`, and launches PAM-backed login prompts on `tty1` and `ttyS0`. ## Root Filesystem Model @@ -45,11 +47,25 @@ The canonical root filesystem is assembled in `staging/rootfs` from: For normal ForgeOS disk images, `scripts/build-image.sh` derives two trees from `staging/rootfs`: -- `staging/rootfs-base`: the read-only base OS used for partition 2 -- `staging/appfs`: the writable app/state layer used for partition 3 +- `staging/rootfs-base`: the read-only base OS copied into each base slot +- `staging/appfs`: the writable app/state layer used for the `apps` partition The base tree keeps the source-built OS, boot-critical tools, systemd units, configuration, and symlinks into `/nix/store`. The app/state tree owns `/nix`, `/home`, `/root`, `/var`, `/opt`, and `/usr/local`. The image builder writes an `/etc/fstab` into `staging/rootfs-base` that mounts `LABEL=FORGE_APPS` at `/forge` and bind-mounts those paths back into the normal filesystem layout. `/tmp` is a tmpfs. The canonical `staging/rootfs` used for `rootfs.cpio.gz` does not include those app-layer fstab entries, so the direct initramfs smoke-test path stays monolithic and does not wait for a disk app partition. +## Rollback Selection + +Atomic ForgeOS images build `ROLLBACK_SLOTS=2` by default. Slot 0 is the normal current base at `PARTLABEL=root`; slot 1 is a rollback base at `PARTLABEL=root-rollback`. Additional slots can be requested with higher `ROLLBACK_SLOTS` values and receive GPT labels such as `root-rollback-2`. + +The GRUB fallback loader emits a `ForgeOS rollback selector` submenu with entries for each base slot. On console images, the main selector entry for each slot uses the initramfs-assisted handoff path with fixed framebuffer settings so a recovery boot stays visible even when the direct kernel path cannot mount the requested slot or loses display output. Each slot also has normal-display and direct-kernel entries for comparison. The early `forgeos-switch-root` helper parses the selected `root=` or `forgeos.root=` argument so it switches into the requested `PARTLABEL`, `LABEL`, `UUID`, `PARTUUID`, or explicit `/dev/...` root device instead of assuming the current slot. + +`BOOTLOADER=stub` signs or copies the kernel EFI stub directly and therefore has no interactive rollback menu. The default kernel build does not bake in a root slot, so the stub path requires a kernel built with `CONFIG_CMDLINE_BOOL=y` or firmware-provided EFI load options. + +The `forgeos-rollback` command provides the first in-OS management layer for these slots. It can list slots, copy the currently booted base into an inactive rollback slot with `forgeos-rollback create`, and promote a booted rollback slot back to `root` with `forgeos-rollback promote`. Slot copying is block-level, so the source root should be mounted read-only and the destination slot must be unmounted. + +If `tune2fs` is available, `forgeos-rollback` randomizes the destination filesystem UUID and restores the expected filesystem label after copying. Without `tune2fs`, the copy still works for GRUB selection because boot entries use GPT partition labels, but the command warns that the ext4 UUID and label may still match the source. + +ForgeOS still needs a declarative NixOS-style base OS rebuild/update flow that prepares a new base deployment and calls this slot machinery automatically. + The default network path uses DHCP through `systemd-networkd`. DNS servers learned from DHCP are exposed through `systemd-resolved`, and `/etc/resolv.conf` is a symlink to the resolved-managed compatibility file at `/run/systemd/resolve/resolv.conf`. Tools that query systemd over D-Bus use the system bus at `/run/dbus/system_bus_socket`. The source-built systemd layer also includes `systemd-logind`, `loginctl`, `pam_systemd.so`, the `org.freedesktop.login1` system-bus policy, and the logind varlink socket. Console login uses BusyBox `getty` and `login`; `/etc/pam.d/login` authenticates against `/etc/shadow` with `pam_unix.so` and registers sessions with `pam_systemd.so`. Nix is integrated as a multi-user daemon package manager in the normal ForgeOS app layer. `scripts/stage-nix.sh` copies the official Nix store closure into `/nix/store`, stages `nix.conf`, adds `nixbld` build accounts, enables `forgeos-nix-bootstrap.service`, and exposes `nix-daemon.service` plus `nix-daemon.socket`. On disk images, the image builder moves that `/nix` tree into `staging/appfs` so package installs and Nix state mutate the app partition, not the base OS. On first boot, the bootstrap service fixes `/nix` ownership, loads the bundled `.reginfo` into the Nix database, and creates the default profile with Nix, Nix manual pages, and the bundled CA certificate package. ForgeOS enables `nix-command` and `flakes`, uses `cache.nixos.org`, and leaves Nix build sandboxing disabled until the OS has a verified sandbox policy. @@ -99,13 +115,12 @@ This is enough for a first CLI OS image and a development boot path, but not yet - Microsoft/shim-signed Secure Boot chain - measured boot and full verified boot - full laptop Wi-Fi and firmware coverage -- declarative NixOS-style system rebuilds and rollback management +- declarative NixOS-style system rebuilds and automated rollback-slot population - source-built GNOME and graphical desktop dependency closure - source-built Openbox/Xorg/GTK graphical dependency closure - source-built dependency closure for systemd and D-Bus runtime libraries - account management beyond the built-in `forge` and `root` users - in-OS guided installer and update system -- base OS rollback selection across multiple base deployments - hardening review of the kernel and userspace defaults ## Suggested Next Steps diff --git a/docs/INSTALLER.md b/docs/INSTALLER.md index 2dfac7a..0cd849e 100644 --- a/docs/INSTALLER.md +++ b/docs/INSTALLER.md @@ -13,7 +13,7 @@ It installs the built raw ForgeOS image to a real block device from an existing 3. optionally unmounts non-critical target partitions 4. writes the ForgeOS raw image to the target disk 5. repairs the GPT backup header on larger disks -6. expands the writable app/state partition to fill the device +6. finds the writable app/state partition by label and expands it to fill the device 7. randomizes ext4 filesystem UUIDs by default ## Basic Usage @@ -82,8 +82,9 @@ Enroll `out/secure-boot/ForgeOS.cer` in the target firmware Secure Boot database - The installer is destructive. It erases the target disk. - Run it from a Linux host, not from the disk you are installing over. -- The current boot model uses a built-in kernel command line with `root=PARTLABEL=root`. -- Normal ForgeOS images use partition `2` for the read-only base OS and partition `3` for mutable apps and state. +- The default GRUB boot path uses `root=PARTLABEL=root` for the current base slot and offers a rollback selector for additional base slots. The primary selector entries use an initramfs-assisted fixed-framebuffer handoff for recovery visibility. +- Normal ForgeOS images use partition `2` for the current read-only base OS. With the default `ROLLBACK_SLOTS=2`, partition `3` is the rollback base slot and partition `4` is mutable apps and state. With `ROLLBACK_SLOTS=1`, partition `3` is mutable apps and state. +- After installation, use `forgeos-rollback create` from the running OS to refresh the rollback base slot before making base OS changes. - Avoid leaving multiple ForgeOS installations attached during early testing, because multiple disks with the same GPT partition label can confuse root selection. - Secure Boot trust is tied to the enrolled signing certificate. Protect the generated private key. diff --git a/overlay/rootfs/sbin/forgeos-switch-root b/overlay/rootfs/sbin/forgeos-switch-root index 9ab2e51..ded4808 100755 --- a/overlay/rootfs/sbin/forgeos-switch-root +++ b/overlay/rootfs/sbin/forgeos-switch-root @@ -30,16 +30,81 @@ console_note() { done } -find_root_device() { +cmdline_value() { + local key=$1 + local arg + local value= + + for arg in $(cat /proc/cmdline 2>/dev/null); do + case "$arg" in + "$key="*) + value=${arg#*=} + ;; + esac + done + + [ -n "$value" ] || return 1 + printf '%s\n' "$value" +} + +requested_root_spec() { + local spec + + spec=$(cmdline_value forgeos.root 2>/dev/null || true) + if [ -z "$spec" ]; then + spec=$(cmdline_value root 2>/dev/null || true) + fi + + case "$spec" in + ''|rootfs|tmpfs) + return 1 + ;; + esac + + printf '%s\n' "$spec" +} + +lookup_root_symlink() { + local kind=$1 + local value=$2 + local path + + case "$kind" in + PARTLABEL) + path=/dev/disk/by-partlabel/$value + ;; + LABEL) + path=/dev/disk/by-label/$value + ;; + UUID) + path=/dev/disk/by-uuid/$value + ;; + PARTUUID) + path=/dev/disk/by-partuuid/$value + ;; + *) + return 1 + ;; + esac + + [ -b "$path" ] || return 1 + printf '%s\n' "$path" +} + +blkid_matches() { + local info=$1 + local kind=$2 + local value=$3 + + printf '%s\n' "$info" | grep -F " $kind=\"$value\"" >/dev/null 2>&1 +} + +scan_matching_root_device() { + local kind=$1 + local value=$2 local dev local info - for dev in /dev/disk/by-partlabel/root /dev/disk/by-label/FORGE_ROOT; do - [ -b "$dev" ] || continue - printf '%s\n' "$dev" - return 0 - done - for dev in \ /dev/nvme[0-9]n[0-9]p[0-9] \ /dev/sd[a-z][0-9] \ @@ -47,12 +112,63 @@ find_root_device() { /dev/vd[a-z][0-9]; do [ -b "$dev" ] || continue info=$(blkid "$dev" 2>/dev/null || true) - case "$info" in - *' PARTLABEL="root"'*|*' LABEL="FORGE_ROOT"'*) - printf '%s\n' "$dev" - return 0 - ;; - esac + blkid_matches "$info" "$kind" "$value" || continue + printf '%s\n' "$dev" + return 0 + done + + return 1 +} + +find_requested_root_device() { + local spec=$1 + local kind + local value + + case "$spec" in + PARTLABEL=*) + kind=PARTLABEL + value=${spec#PARTLABEL=} + ;; + LABEL=*) + kind=LABEL + value=${spec#LABEL=} + ;; + UUID=*) + kind=UUID + value=${spec#UUID=} + ;; + PARTUUID=*) + kind=PARTUUID + value=${spec#PARTUUID=} + ;; + /dev/*) + [ -b "$spec" ] || return 1 + printf '%s\n' "$spec" + return 0 + ;; + *) + return 1 + ;; + esac + + lookup_root_symlink "$kind" "$value" || scan_matching_root_device "$kind" "$value" +} + +find_root_device() { + local dev + local spec + + spec=$(requested_root_spec 2>/dev/null || true) + if [ -n "$spec" ]; then + find_requested_root_device "$spec" + return $? + fi + + for dev in /dev/disk/by-partlabel/root /dev/disk/by-label/FORGE_ROOT; do + [ -b "$dev" ] || continue + printf '%s\n' "$dev" + return 0 done return 1 diff --git a/overlay/rootfs/usr/sbin/forgeos-rollback b/overlay/rootfs/usr/sbin/forgeos-rollback new file mode 100755 index 0000000..e7b308b --- /dev/null +++ b/overlay/rootfs/usr/sbin/forgeos-rollback @@ -0,0 +1,981 @@ +#!/bin/sh + +set -eu + +PATH=/usr/sbin:/usr/bin:/sbin:/bin +export PATH + +ROOT_LABEL_PREFIX=${FORGEOS_ROOT_LABEL:-FORGE_ROOT} +APP_LABEL=${FORGEOS_APP_LABEL:-FORGE_APPS} +DD_BS=${FORGEOS_ROLLBACK_BS:-16M} +ASSUME_YES=0 +FORCE_RW_SOURCE=0 +VERSION=2026.06.18.9 + +usage() { + cat <<'EOF' +Usage: + forgeos-rollback status + forgeos-rollback create [ROLLBACK_SLOT] + forgeos-rollback promote [CURRENT_SLOT] + forgeos-rollback relabel [SLOT] + forgeos-rollback version + +Options: + --yes Do not prompt before copying a base slot. + --force-rw-source Allow copying from a read-write mounted root. + +Slots: + root Current base slot. + root-rollback First rollback slot. + root-rollback-N Additional rollback slot. + 0, 1, 2... Numeric aliases for the same slots. + +Examples: + forgeos-rollback status + forgeos-rollback create + forgeos-rollback promote +EOF +} + +die() { + printf 'error: %s\n' "$*" >&2 + exit 1 +} + +warn() { + printf 'warning: %s\n' "$*" >&2 +} + +show_device_diagnostics() { + local current + local current_slot + local selected_arg + local selected_slot + local path + local dev + + current=$(current_root_device 2>/dev/null || true) + current_slot=unknown + if [ -n "$current" ]; then + current_slot=$(slot_for_device "$current" 2>/dev/null || true) + [ -n "$current_slot" ] || current_slot=unknown + fi + + selected_arg=$(selected_root_spec 2>/dev/null || true) + selected_slot=unknown + if [ -n "$selected_arg" ]; then + selected_slot=$(slot_from_root_spec "$selected_arg" 2>/dev/null || true) + [ -n "$selected_slot" ] || selected_slot=unknown + fi + + printf 'Current boot slot: %s\n' "$current_slot" >&2 + printf 'Selected boot slot: %s\n' "$selected_slot" >&2 + printf 'Selected root arg: %s\n' "${selected_arg:-unknown}" >&2 + printf 'Current root device: %s\n' "${current:-unknown}" >&2 + printf 'Slot symlinks:\n' >&2 + for path in \ + /dev/disk/by-partlabel/root \ + /dev/disk/by-partlabel/root-rollback \ + /dev/disk/by-partlabel/root-rollback-*; do + [ -e "$path" ] || continue + printf ' %s -> %s\n' "$path" "$(real_device "$path")" >&2 + done + printf 'Detected ForgeOS base partitions:\n' >&2 + device_candidates | while IFS= read -r dev; do + case "$(partition_partlabel "$dev")" in + root|root-rollback|root-rollback-*) + printf ' %s: %s\n' "$dev" "$(blkid "$dev" 2>/dev/null || true)" >&2 + ;; + esac + done +} + +die_same_slot() { + local src_slot=$1 + local dst_slot=$2 + local src=$3 + local dst=$4 + + printf 'error: source and destination resolve to the same block device\n' >&2 + printf ' source slot: %s -> %s\n' "$src_slot" "$(real_device "$src")" >&2 + printf ' destination slot: %s -> %s\n' "$dst_slot" "$(real_device "$dst")" >&2 + show_device_diagnostics + + if [ "$src_slot" = "$dst_slot" ] && [ "$src_slot" != "root" ]; then + printf "hint: you are already booted from %s; run 'forgeos-rollback promote' to copy it back to root.\n" "$src_slot" >&2 + else + printf "hint: check for duplicate or stale GPT partition labels with 'ls -l /dev/disk/by-partlabel' and 'blkid'.\n" >&2 + fi + + exit 1 +} + +need_root() { + [ "$(id -u 2>/dev/null || echo 1)" = "0" ] || die "must run as root" +} + +cmdline_value() { + local key=$1 + local arg + local value= + + for arg in $(cat /proc/cmdline 2>/dev/null); do + case "$arg" in + "$key="*) + value=${arg#*=} + ;; + esac + done + + [ -n "$value" ] || return 1 + printf '%s\n' "$value" +} + +real_device() { + readlink -f "$1" 2>/dev/null || printf '%s\n' "$1" +} + +same_device() { + local a=$1 + local b=$2 + local aid + local bid + + [ -b "$a" ] || return 1 + [ -b "$b" ] || return 1 + + aid=$(block_device_id "$a" 2>/dev/null || true) + bid=$(block_device_id "$b" 2>/dev/null || true) + [ -n "$aid" ] && [ -n "$bid" ] && [ "$aid" = "$bid" ] +} + +block_device_id() { + local dev=$1 + local name + local sys_dev + local stat_dev + + name=$(basename "$(real_device "$dev")") + sys_dev=/sys/class/block/$name/dev + if [ -r "$sys_dev" ]; then + cat "$sys_dev" + return 0 + fi + + stat_dev=$(stat -c '%t:%T' "$dev" 2>/dev/null || true) + case "$stat_dev" in + ''|0:0) + return 1 + ;; + esac + printf '%s\n' "$stat_dev" +} + +device_candidates() { + local dev + + for dev in \ + /dev/nvme[0-9]n[0-9]p[0-9] \ + /dev/sd[a-z][0-9] \ + /dev/mmcblk[0-9]p[0-9] \ + /dev/vd[a-z][0-9]; do + [ -b "$dev" ] || continue + printf '%s\n' "$dev" + done +} + +blkid_value() { + local dev=$1 + local key=$2 + + blkid "$dev" 2>/dev/null | sed -n "s/.* ${key}=\"\\([^\"]*\\)\".*/\\1/p" | head -n 1 +} + +partition_partlabel() { + blkid_value "$1" PARTLABEL +} + +partition_label() { + blkid_value "$1" LABEL +} + +is_forgeos_system() { + grep -qx 'ID=forgeos' /etc/os-release 2>/dev/null || [ -f /etc/forgeos-nix ] +} + +current_root_device_from_mountinfo() { + local devnum + local sys_path + local block_name + local dev + + devnum=$(awk '$5 == "/" { print $3; exit }' /proc/self/mountinfo 2>/dev/null || true) + [ -n "$devnum" ] || return 1 + + sys_path=/sys/dev/block/$devnum + [ -e "$sys_path" ] || return 1 + + block_name=$(basename "$(readlink -f "$sys_path" 2>/dev/null || true)") + [ -n "$block_name" ] || return 1 + + dev=/dev/$block_name + [ -b "$dev" ] || return 1 + printf '%s\n' "$dev" +} + +block_name_for_device() { + basename "$(real_device "$1")" +} + +partition_number_for_device() { + local name + local path + + name=$(block_name_for_device "$1") + path=/sys/class/block/$name/partition + [ -r "$path" ] || return 1 + cat "$path" +} + +parent_block_name_for_device() { + local name + local real_sys + + name=$(block_name_for_device "$1") + real_sys=$(readlink -f "/sys/class/block/$name" 2>/dev/null || true) + [ -n "$real_sys" ] || return 1 + [ -r "/sys/class/block/$name/partition" ] || return 1 + basename "$(dirname "$real_sys")" +} + +partition_path_for_number() { + local parent=$1 + local number=$2 + local path + + case "$parent" in + *[0-9]) + path="/dev/${parent}p${number}" + ;; + *) + path="/dev/${parent}${number}" + ;; + esac + + [ -b "$path" ] || return 1 + printf '%s\n' "$path" +} + +slot_from_partition_number() { + local number=$1 + + case "$number" in + ''|*[!0-9]*) + return 1 + ;; + esac + + [ "$number" -ge 2 ] || return 1 + if [ "$number" = "2" ]; then + printf 'root\n' + elif [ "$number" = "3" ]; then + printf 'root-rollback\n' + else + printf 'root-rollback-%s\n' "$((number - 2))" + fi +} + +slot_device_by_layout() { + local slot=$1 + local current + local parent + local slot_num + local part_num + local dev + + is_forgeos_system || return 1 + + current=$(current_root_device_from_mountinfo 2>/dev/null || true) + [ -n "$current" ] || return 1 + + parent=$(parent_block_name_for_device "$current" 2>/dev/null || true) + [ -n "$parent" ] || return 1 + + slot_num=$(slot_number "$slot") + part_num=$((slot_num + 2)) + dev=$(partition_path_for_number "$parent" "$part_num" 2>/dev/null || true) + [ -n "$dev" ] || return 1 + + [ "$(partition_label "$dev")" != "$APP_LABEL" ] || return 1 + printf '%s\n' "$dev" +} + +normalize_slot() { + local slot=${1:-root-rollback} + local n + + case "$slot" in + current|primary|root|0) + printf 'root\n' + ;; + rollback|root-rollback|1) + printf 'root-rollback\n' + ;; + root-rollback-*) + n=${slot#root-rollback-} + case "$n" in + ''|*[!0-9]*) + die "invalid rollback slot: $slot" + ;; + esac + printf '%s\n' "$slot" + ;; + *[!0-9]*) + die "invalid slot: $slot" + ;; + *) + [ "$slot" -gt 1 ] || die "invalid slot: $slot" + printf 'root-rollback-%s\n' "$slot" + ;; + esac +} + +slot_number() { + local slot=$1 + local n + + case "$slot" in + root) + printf '0\n' + ;; + root-rollback) + printf '1\n' + ;; + root-rollback-*) + n=${slot#root-rollback-} + case "$n" in + ''|*[!0-9]*) + die "invalid rollback slot: $slot" + ;; + esac + printf '%s\n' "$n" + ;; + *) + die "invalid slot: $slot" + ;; + esac +} + +slot_fs_label() { + local slot=$1 + local n + + n=$(slot_number "$slot") + if [ "$n" = "0" ]; then + printf '%s\n' "$ROOT_LABEL_PREFIX" + else + printf '%s_%s\n' "$ROOT_LABEL_PREFIX" "$n" + fi +} + +slot_device() { + local slot + local path + local dev + local actual + + slot=$(normalize_slot "${1:-root-rollback}") + path="/dev/disk/by-partlabel/$slot" + if [ -b "$path" ] && [ "$(partition_partlabel "$path")" = "$slot" ]; then + printf '%s\n' "$path" + return 0 + fi + + while IFS= read -r dev; do + [ "$(partition_partlabel "$dev")" = "$slot" ] || continue + printf '%s\n' "$dev" + return 0 + done </dev/null || true) + if [ -n "$dev" ]; then + printf '%s\n' "$dev" + return 0 + fi + + if [ -b "$path" ]; then + actual=$(partition_partlabel "$path") + if [ -n "$actual" ] && [ "$actual" != "$slot" ]; then + die "slot $slot exists at $path but blkid reports PARTLABEL='$actual'; run 'partprobe' or reboot to refresh partition labels" + fi + printf '%s\n' "$path" + return 0 + fi + + die "missing rollback slot $slot" +} + +resolve_root_spec() { + local spec=$1 + local kind + local value + local path + + case "$spec" in + PARTLABEL=*) + kind=partlabel + value=${spec#PARTLABEL=} + case "$value" in + root|root-rollback|root-rollback-*) + slot_device "$value" + return $? + ;; + esac + ;; + LABEL=*) + kind=label + value=${spec#LABEL=} + ;; + UUID=*) + kind=uuid + value=${spec#UUID=} + ;; + PARTUUID=*) + kind=partuuid + value=${spec#PARTUUID=} + ;; + /dev/*) + [ -b "$spec" ] || return 1 + printf '%s\n' "$spec" + return 0 + ;; + *) + return 1 + ;; + esac + + path="/dev/disk/by-$kind/$value" + [ -b "$path" ] || return 1 + printf '%s\n' "$path" +} + +mount_source() { + awk '$2 == "/" { print $1; exit }' /proc/mounts 2>/dev/null +} + +mount_options() { + awk '$2 == "/" { print $4; exit }' /proc/mounts 2>/dev/null +} + +current_root_device() { + local spec + local dev + local src + + dev=$(current_root_device_from_mountinfo 2>/dev/null || true) + if [ -n "$dev" ]; then + printf '%s\n' "$dev" + return 0 + fi + + spec=$(cmdline_value forgeos.root 2>/dev/null || true) + if [ -z "$spec" ]; then + spec=$(cmdline_value root 2>/dev/null || true) + fi + if [ -n "$spec" ]; then + dev=$(resolve_root_spec "$spec" 2>/dev/null || true) + if [ -n "$dev" ]; then + printf '%s\n' "$dev" + return 0 + fi + fi + + src=$(mount_source) + [ -n "$src" ] || return 1 + printf '%s\n' "$src" +} + +selected_root_spec() { + local spec + + spec=$(cmdline_value forgeos.root 2>/dev/null || true) + if [ -z "$spec" ]; then + spec=$(cmdline_value root 2>/dev/null || true) + fi + + case "$spec" in + ''|rootfs|tmpfs) + return 1 + ;; + esac + + printf '%s\n' "$spec" +} + +slot_from_root_spec() { + local spec=$1 + local value + local dev + + case "$spec" in + PARTLABEL=*) + value=${spec#PARTLABEL=} + case "$value" in + root|root-rollback|root-rollback-*) + normalize_slot "$value" + return 0 + ;; + esac + ;; + esac + + dev=$(resolve_root_spec "$spec" 2>/dev/null || true) + [ -n "$dev" ] || return 1 + slot_for_device "$dev" +} + +selected_boot_slot() { + local spec + + spec=$(selected_root_spec 2>/dev/null || true) + [ -n "$spec" ] || return 1 + slot_from_root_spec "$spec" +} + +slot_for_current_root() { + local current + local slot + local selected + + current=$(current_root_device 2>/dev/null || true) + if [ -n "$current" ]; then + slot=$(slot_for_device "$current" 2>/dev/null || true) + if [ -n "$slot" ]; then + printf '%s\n' "$slot" + return 0 + fi + fi + + selected=$(selected_boot_slot 2>/dev/null || true) + if [ -n "$selected" ]; then + warn "mounted root device could not be classified; using selected boot slot '$selected' from kernel command line" + printf '%s\n' "$selected" + return 0 + fi + + return 1 +} + +is_initramfs_root() { + case "$(mount_source 2>/dev/null || true)" in + rootfs|tmpfs) + return 0 + ;; + esac + + return 1 +} + +recovery_promote_slot() { + local dst_slot=$1 + + [ "$dst_slot" = "root" ] || return 1 + is_initramfs_root || return 1 + [ -b /dev/disk/by-partlabel/root ] || return 1 + [ -b /dev/disk/by-partlabel/root-rollback ] || return 1 + + warn "running from initramfs/rootfs; using root-rollback as promotion source" + printf 'root-rollback\n' +} + +die_unknown_current_slot() { + printf 'error: current root is not a known ForgeOS base slot\n' >&2 + show_device_diagnostics + exit 1 +} + +slot_for_device() { + local dev=$1 + local path + local slot + local part_num + + slot=$(partition_partlabel "$dev") + case "$slot" in + root|root-rollback|root-rollback-*) + printf '%s\n' "$slot" + return 0 + ;; + esac + + part_num=$(partition_number_for_device "$dev" 2>/dev/null || true) + if is_forgeos_system && [ -n "$part_num" ] && [ "$(partition_label "$dev")" != "$APP_LABEL" ]; then + slot=$(slot_from_partition_number "$part_num" 2>/dev/null || true) + if [ -n "$slot" ]; then + printf '%s\n' "$slot" + return 0 + fi + fi + + for path in \ + /dev/disk/by-partlabel/root \ + /dev/disk/by-partlabel/root-rollback \ + /dev/disk/by-partlabel/root-rollback-*; do + [ -b "$path" ] || continue + same_device "$dev" "$path" || continue + slot=${path##*/} + printf '%s\n' "$slot" + return 0 + done + + return 1 +} + +is_root_read_only() { + local opts + + opts=$(mount_options) + case ",$opts," in + *,ro,*) + return 0 + ;; + esac + + return 1 +} + +is_device_mounted() { + local dev=$1 + local src + local target + local _fstype + local _opts + local _rest + + while read -r src target _fstype _opts _rest; do + case "$src" in + /dev/*) + [ -b "$src" ] || continue + same_device "$src" "$dev" && return 0 + ;; + esac + done < /proc/mounts + + return 1 +} + +mountpoints_for_device() { + local dev=$1 + local src + local target + local _fstype + local _opts + local _rest + local sep= + local found=0 + + while read -r src target _fstype _opts _rest; do + case "$src" in + /dev/*) + [ -b "$src" ] || continue + same_device "$src" "$dev" || continue + printf '%s%s' "$sep" "$target" + sep=, + found=1 + ;; + esac + done < /proc/mounts + + [ "$found" -eq 1 ] || printf '-' +} + +bytes_to_mib() { + local bytes=$1 + + case "$bytes" in + ''|*[!0-9]*) + printf '-' + ;; + *) + printf '%sMiB' "$((bytes / 1024 / 1024))" + ;; + esac +} + +slot_paths() { + local dev + local path + local found=0 + local current + local parent + local number + local label + + while IFS= read -r dev; do + case "$(partition_partlabel "$dev")" in + root|root-rollback|root-rollback-*) + printf '%s\n' "$dev" + found=1 + ;; + esac + done </dev/null || true) + [ -n "$current" ] || return 0 + parent=$(parent_block_name_for_device "$current" 2>/dev/null || true) + [ -n "$parent" ] || return 0 + + number=2 + while :; do + dev=$(partition_path_for_number "$parent" "$number" 2>/dev/null || true) + [ -n "$dev" ] || break + label=$(partition_label "$dev") + [ "$label" = "$APP_LABEL" ] && break + slot_from_partition_number "$number" >/dev/null 2>&1 && printf '%s\n' "$dev" + number=$((number + 1)) + done +} + +show_status() { + local current + local current_slot + local root_arg + local forgeos_arg + local selected_arg + local selected_dev + local selected_slot + local path + local slot + local dev + local marker + local size + local mounts + local info + + current=$(current_root_device 2>/dev/null || true) + current_slot=unknown + if [ -n "$current" ]; then + current_slot=$(slot_for_device "$current" 2>/dev/null || true) + [ -n "$current_slot" ] || current_slot=unknown + fi + + root_arg=$(cmdline_value root 2>/dev/null || true) + forgeos_arg=$(cmdline_value forgeos.root 2>/dev/null || true) + selected_arg=$(selected_root_spec 2>/dev/null || true) + selected_slot=unknown + if [ -n "$selected_arg" ]; then + selected_slot=$(slot_from_root_spec "$selected_arg" 2>/dev/null || true) + [ -n "$selected_slot" ] || selected_slot=unknown + fi + + printf 'ForgeOS rollback slots\n' + printf 'Command version: %s\n' "$VERSION" + printf 'Current boot slot: %s\n' "$current_slot" + printf 'Selected boot slot: %s\n' "$selected_slot" + printf 'Selected root arg: %s\n' "${selected_arg:-unknown}" + printf 'Mounted root device: %s\n' "${current:-unknown}" + printf '%-2s %-18s %-16s %-12s %-12s %s\n' '' SLOT DEVICE SIZE MOUNTED BLKID + + slot_paths | while IFS= read -r path; do + slot=$(slot_for_device "$path" 2>/dev/null || true) + [ -n "$slot" ] || slot=$(partition_partlabel "$path") + [ -n "$slot" ] || slot=${path##*/} + dev=$(real_device "$path") + marker=' ' + if [ -n "$current" ] && same_device "$current" "$path"; then + marker='*' + fi + size=$(blockdev --getsize64 "$path" 2>/dev/null || true) + mounts=$(mountpoints_for_device "$path") + info=$(blkid "$path" 2>/dev/null || true) + printf '%-2s %-18s %-16s %-12s %-12s %s\n' \ + "$marker" "$slot" "$dev" "$(bytes_to_mib "$size")" "$mounts" "$info" + done + + printf '\n* marks the slot backing the currently booted root filesystem.\n' +} + +confirm_copy() { + local src_slot=$1 + local dst_slot=$2 + local src=$3 + local dst=$4 + local reply + + [ "$ASSUME_YES" -eq 1 ] && return 0 + + cat >&2 </dev/null 2>&1; then + warn "tune2fs is unavailable; $slot may retain the source filesystem UUID and label" + warn "boot selection uses GPT PARTLABEL, but install e2fsprogs and run: forgeos-rollback relabel $slot" + return 0 + fi + + if is_device_mounted "$dev"; then + die "refusing to relabel mounted slot $slot" + fi + + tune2fs -U random -L "$label" "$dev" >/dev/null + blkid "$dev" 2>/dev/null || true +} + +copy_slot() { + local src_slot=$1 + local dst_slot=$2 + local src + local dst + local src_size + local dst_size + local current + local current_slot + + need_root + + src_slot=$(normalize_slot "$src_slot") + dst_slot=$(normalize_slot "$dst_slot") + src=$(slot_device "$src_slot") + dst=$(slot_device "$dst_slot") + + same_device "$src" "$dst" && die_same_slot "$src_slot" "$dst_slot" "$src" "$dst" + is_device_mounted "$dst" && die "refusing to overwrite mounted slot $dst_slot" + + src_size=$(blockdev --getsize64 "$src") + dst_size=$(blockdev --getsize64 "$dst") + [ "$dst_size" -ge "$src_size" ] || die "$dst_slot is smaller than $src_slot" + + current=$(current_root_device 2>/dev/null || true) + current_slot= + if [ -n "$current" ]; then + current_slot=$(slot_for_device "$current" 2>/dev/null || true) + fi + + if [ "$FORCE_RW_SOURCE" -ne 1 ] && [ "$src_slot" = "$current_slot" ] && ! is_root_read_only; then + die "root is mounted read-write; remount read-only or rerun with --force-rw-source" + fi + + confirm_copy "$src_slot" "$dst_slot" "$src" "$dst" + + sync + dd if="$src" of="$dst" bs="$DD_BS" iflag=fullblock conv=fsync + sync + relabel_slot "$dst_slot" + sync +} + +create_rollback() { + local dst_slot=${1:-root-rollback} + local src_slot + + dst_slot=$(normalize_slot "$dst_slot") + src_slot=$(slot_for_current_root || true) + [ -n "$src_slot" ] || die_unknown_current_slot + + if [ "$src_slot" = "$dst_slot" ]; then + if [ "$src_slot" = "root" ]; then + die "cannot create a rollback by copying root to itself; choose an inactive rollback slot" + fi + die "currently booted from $src_slot; run 'forgeos-rollback promote' to make it the current root, or choose a different inactive rollback slot" + fi + + copy_slot "$src_slot" "$dst_slot" +} + +promote_current() { + local dst_slot=${1:-root} + local src_slot + + dst_slot=$(normalize_slot "$dst_slot") + src_slot=$(slot_for_current_root || true) + if [ -z "$src_slot" ]; then + src_slot=$(recovery_promote_slot "$dst_slot" || true) + fi + [ -n "$src_slot" ] || die_unknown_current_slot + [ "$src_slot" != "root" ] || die "boot a rollback slot before promoting it to root" + + copy_slot "$src_slot" "$dst_slot" +} + +while [ "$#" -gt 0 ]; do + case "$1" in + --yes|-y) + ASSUME_YES=1 + shift + ;; + --force-rw-source) + FORCE_RW_SOURCE=1 + shift + ;; + --help|-h) + usage + exit 0 + ;; + *) + break + ;; + esac +done + +cmd=${1:-status} +[ "$#" -eq 0 ] || shift + +case "$cmd" in + status|list) + show_status + ;; + create) + create_rollback "${1:-root-rollback}" + ;; + promote) + promote_current "${1:-root}" + ;; + relabel) + need_root + relabel_slot "${1:-root-rollback}" + ;; + version) + printf '%s\n' "$VERSION" + ;; + help) + usage + ;; + *) + usage >&2 + exit 2 + ;; +esac diff --git a/scripts/build-image.sh b/scripts/build-image.sh index 61eea77..b6ebd6b 100755 --- a/scripts/build-image.sh +++ b/scripts/build-image.sh @@ -11,6 +11,7 @@ normal_rootfs_matches() { [[ -d "$ROOTFS_STAGING_DIR" ]] || return 1 [[ -f "$ROOTFS_STAGING_DIR/etc/forgeos-nix" ]] || return 1 [[ -x "$ROOTFS_STAGING_DIR/usr/bin/forgeos-app" ]] || return 1 + [[ -x "$ROOTFS_STAGING_DIR/usr/sbin/forgeos-rollback" ]] || return 1 [[ -f "$ROOTFS_STAGING_DIR/etc/machine-id" ]] || return 1 [[ ! -f "$ROOTFS_STAGING_DIR/etc/fstab" ]] || return 1 @@ -45,6 +46,11 @@ ROOT_IMAGE="$OUT_DIR/forgeos-root.img" APP_IMAGE="$OUT_DIR/forgeos-apps.img" IMAGE_ROOTFS_DIR="$ROOTFS_STAGING_DIR" APP_IMAGE_NEEDED=0 +ROOT_SLOT_COUNT=1 +ROOT_PART_START=2 +APP_PART_NUM= +declare -a ROOT_OFFSETS=() +declare -a ROOT_SIZES=() partition_field() { local partition=$1 @@ -57,6 +63,77 @@ partition_field() { ' } +validate_rollback_slots() { + [[ "$ROLLBACK_SLOTS" =~ ^[0-9]+$ ]] || die "ROLLBACK_SLOTS must be a positive integer" + (( ROLLBACK_SLOTS >= 1 )) || die "ROLLBACK_SLOTS must be at least 1" +} + +root_slot_partlabel() { + local slot=$1 + + if (( slot == 0 )); then + printf 'root\n' + elif (( slot == 1 )); then + printf 'root-rollback\n' + else + printf 'root-rollback-%s\n' "$slot" + fi +} + +root_slot_fs_label() { + local slot=$1 + + if (( slot == 0 )); then + printf '%s\n' "$ROOT_LABEL" + else + printf '%s_%s\n' "$ROOT_LABEL" "$slot" + fi +} + +root_slot_image() { + local slot=$1 + + if (( slot == 0 )); then + printf '%s\n' "$ROOT_IMAGE" + else + printf '%s/forgeos-root-%s.img\n' "$OUT_DIR" "$slot" + fi +} + +root_slot_title() { + local slot=$1 + + if (( slot == 0 )); then + printf 'current base\n' + elif (( slot == 1 )); then + printf 'rollback base\n' + else + printf 'rollback base %s\n' "$slot" + fi +} + +configure_root_slots() { + local slot + local fs_label + local partlabel + + validate_rollback_slots + + if atomic_base_enabled; then + ROOT_SLOT_COUNT=$ROLLBACK_SLOTS + else + ROOT_SLOT_COUNT=1 + fi + + for ((slot=0; slot> "$GRUB_CFG" + + for ((slot=0; slot> "$GRUB_CFG" + done + + printf '\n' >> "$GRUB_CFG" +} + +write_grub_rollback_menu() { + local slot + local title + local partlabel + + (( ROOT_SLOT_COUNT > 1 )) || return 0 + + cat >> "$GRUB_CFG" <<'EOF' + +submenu "ForgeOS rollback selector" { +EOF + + for ((slot=0; slot> "$GRUB_CFG" + printf ' search --no-floppy --file --set=esp /EFI/BOOT/FORGEOS.EFI\n' >> "$GRUB_CFG" + printf ' set gfxmode=1024x768\n' >> "$GRUB_CFG" + printf ' set gfxpayload=keep\n' >> "$GRUB_CFG" + printf ' linux ($esp)/EFI/BOOT/FORGEOS.EFI $linux_slot_%s $handoff_common nomodeset video=efifb:1024x768 fbcon=map:0 fbcon=font:VGA8x16\n' "$slot" >> "$GRUB_CFG" + printf ' initrd ($esp)/EFI/BOOT/rootfs.cpio.gz\n' >> "$GRUB_CFG" + printf ' }\n' >> "$GRUB_CFG" + + printf '\n' >> "$GRUB_CFG" + printf ' menuentry "ForgeOS - %s normal display (%s)" {\n' "$title" "$partlabel" >> "$GRUB_CFG" + printf ' search --no-floppy --file --set=esp /EFI/BOOT/FORGEOS.EFI\n' >> "$GRUB_CFG" + printf ' linux ($esp)/EFI/BOOT/FORGEOS.EFI $linux_slot_%s $handoff_common i915.enable_psr=0 i915.fastboot=0\n' "$slot" >> "$GRUB_CFG" + printf ' initrd ($esp)/EFI/BOOT/rootfs.cpio.gz\n' >> "$GRUB_CFG" + printf ' }\n' >> "$GRUB_CFG" + else + printf ' menuentry "ForgeOS - %s (%s)" {\n' "$title" "$partlabel" >> "$GRUB_CFG" + printf ' search --no-floppy --file --set=esp /EFI/BOOT/FORGEOS.EFI\n' >> "$GRUB_CFG" + printf ' set gfxmode=1024x768\n' >> "$GRUB_CFG" + printf ' set gfxpayload=keep\n' >> "$GRUB_CFG" + printf ' linux ($esp)/EFI/BOOT/FORGEOS.EFI $linux_slot_%s nomodeset video=efifb:1024x768 fbcon=map:0 fbcon=font:VGA8x16\n' "$slot" >> "$GRUB_CFG" + printf ' }\n' >> "$GRUB_CFG" + fi + + printf '\n' >> "$GRUB_CFG" + printf ' menuentry "ForgeOS - %s direct kernel (%s)" {\n' "$title" "$partlabel" >> "$GRUB_CFG" + printf ' search --no-floppy --file --set=esp /EFI/BOOT/FORGEOS.EFI\n' >> "$GRUB_CFG" + printf ' linux ($esp)/EFI/BOOT/FORGEOS.EFI $linux_slot_%s i915.enable_psr=0 i915.fastboot=0\n' "$slot" >> "$GRUB_CFG" + printf ' }\n' >> "$GRUB_CFG" + + if (( slot + 1 < ROOT_SLOT_COUNT )); then + printf '\n' >> "$GRUB_CFG" + fi + done + + cat >> "$GRUB_CFG" <<'EOF' +} +EOF +} + write_grub_config() { local root_mode=rw + local linux_args if atomic_base_enabled; then root_mode=ro fi + linux_args="rootfstype=ext4 rootwait $root_mode console=tty0 loglevel=7 ignore_loglevel earlyprintk=efi,keep vt.global_cursor_default=1" + cat > "$GRUB_CFG" <<'EOF' set timeout=10 set default=0 @@ -201,10 +353,13 @@ set gfxpayload=keep insmod all_video insmod gfxterm terminal_output gfxterm console -set linux_common="__LINUX_COMMON__" set initrd_common="rdinit=/sbin/init" set handoff_common="rdinit=/init forgeos.switch_root=1" +EOF + write_grub_root_slots "$linux_args" + + cat >> "$GRUB_CFG" <<'EOF' menuentry "ForgeOS - Dell Precision 7720 default" { search --no-floppy --file --set=esp /EFI/BOOT/FORGEOS.EFI linux ($esp)/EFI/BOOT/FORGEOS.EFI $linux_common i915.enable_psr=0 i915.fastboot=0 @@ -226,7 +381,7 @@ menuentry "ForgeOS - firmware framebuffer safe mode" { } EOF - sed -i "s|__LINUX_COMMON__|root=PARTLABEL=root rootfstype=ext4 rootwait $root_mode console=tty0 loglevel=7 ignore_loglevel earlyprintk=efi,keep vt.global_cursor_default=1|" "$GRUB_CFG" + write_grub_rollback_menu if [[ "$DESKTOP" != "gnome" ]] && ! truthy "$ENABLE_DESKTOP"; then cat >> "$GRUB_CFG" <<'EOF' @@ -302,6 +457,9 @@ EOF prepare_efi_loader() { case "$BOOTLOADER" in stub) + if ! grep -qx 'CONFIG_CMDLINE_BOOL=y' "$OUT_DIR/kernel.config" 2>/dev/null; then + die "BOOTLOADER=stub requires a kernel with CONFIG_CMDLINE_BOOL=y or firmware-provided EFI load options; use BOOTLOADER=grub for the default rollback-capable image" + fi "$ROOT_DIR/scripts/sign-efi-loader.sh" "$OUT_DIR/bzImage" "$EFI_LOADER" install -m 0644 "$OUT_DIR/bzImage" "$EFI_KERNEL" ;; @@ -325,6 +483,8 @@ prepare_efi_loader() { esac } +configure_root_slots + msg "preparing EFI loader" prepare_efi_loader @@ -338,23 +498,32 @@ if atomic_base_enabled; then BASE_SIZE_MIB=${BASE_SIZE_MIB:-$((BASE_USED_MIB + BASE_HEADROOM_MIB))} BASE_MIN_MIB=$((BASE_USED_MIB + 64)) APP_MIN_MIB=$((APP_USED_MIB + APP_HEADROOM_MIB)) - BASE_END_MIB=$((ESP_END_MIB + BASE_SIZE_MIB)) + BASE_END_MIB=$((ESP_END_MIB + (BASE_SIZE_MIB * ROOT_SLOT_COUNT))) APP_SIZE_MIB=$((IMAGE_SIZE_MIB - BASE_END_MIB)) (( BASE_SIZE_MIB >= BASE_MIN_MIB )) || die "BASE_SIZE_MIB=$BASE_SIZE_MIB is too small; need at least ${BASE_MIN_MIB}MiB" (( APP_SIZE_MIB >= APP_MIN_MIB )) || die "IMAGE_SIZE_MIB=$IMAGE_SIZE_MIB leaves ${APP_SIZE_MIB}MiB for apps; need at least ${APP_MIN_MIB}MiB" - msg "atomic layout: ${BASE_SIZE_MIB}MiB base root, ${APP_SIZE_MIB}MiB writable app/state layer" + msg "atomic layout: ${ROOT_SLOT_COUNT} base slot(s) at ${BASE_SIZE_MIB}MiB each, ${APP_SIZE_MIB}MiB writable app/state layer" fi msg "creating raw image $IMAGE_PATH" -rm -f "$IMAGE_PATH" "$ESP_IMAGE" "$ROOT_IMAGE" "$APP_IMAGE" +rm -f "$IMAGE_PATH" "$ESP_IMAGE" "$APP_IMAGE" +rm -f "$OUT_DIR"/forgeos-root-[0-9]*.img +for ((slot=0; slot/dev/null parted -s "$IMAGE_PATH" mklabel gpt parted -s "$IMAGE_PATH" mkpart ESP fat32 1MiB "${ESP_END_MIB}MiB" parted -s "$IMAGE_PATH" set 1 esp on if [[ "$APP_IMAGE_NEEDED" -eq 1 ]]; then - parted -s "$IMAGE_PATH" mkpart root ext4 "${ESP_END_MIB}MiB" "${BASE_END_MIB}MiB" + for ((slot=0; slot "$cmdline_fragment" -apply_kconfig_fragment "$KERNEL_BUILD_DIR/.config" "$cmdline_fragment" -rm -f "$cmdline_fragment" make -C "$KERNEL_SRC_DIR" O="$KERNEL_BUILD_DIR" ARCH="$ARCH" olddefconfig msg "building Linux ${KERNEL_VERSION}" diff --git a/scripts/build-rootfs.sh b/scripts/build-rootfs.sh index d01607e..9a8a58c 100755 --- a/scripts/build-rootfs.sh +++ b/scripts/build-rootfs.sh @@ -168,6 +168,7 @@ chmod 755 \ "$ROOTFS_STAGING_DIR/sbin/forgeos-switch-root" \ "$ROOTFS_STAGING_DIR/usr/lib/forgeos/nix-bootstrap" \ "$ROOTFS_STAGING_DIR/usr/bin/forgeos-app" \ + "$ROOTFS_STAGING_DIR/usr/sbin/forgeos-rollback" \ "$ROOTFS_STAGING_DIR/usr/bin/neofetch" \ "$ROOTFS_STAGING_DIR/usr/share/udhcpc/default.script" chmod 700 "$ROOTFS_STAGING_DIR/root" "$ROOTFS_STAGING_DIR/home/forge" diff --git a/scripts/common.sh b/scripts/common.sh index 6023873..d18df0e 100755 --- a/scripts/common.sh +++ b/scripts/common.sh @@ -24,6 +24,7 @@ ROOT_LABEL=${ROOT_LABEL:-FORGE_ROOT} EFI_LABEL=${EFI_LABEL:-FORGE_EFI} APP_LABEL=${APP_LABEL:-FORGE_APPS} ATOMIC_BASE=${ATOMIC_BASE:-1} +ROLLBACK_SLOTS=${ROLLBACK_SLOTS:-2} case "$DESKTOP" in console|gnome) ;; *) diff --git a/scripts/install-to-disk.sh b/scripts/install-to-disk.sh index 58db3c1..428601b 100755 --- a/scripts/install-to-disk.sh +++ b/scripts/install-to-disk.sh @@ -231,14 +231,57 @@ partition_fstype() { lsblk -dnro FSTYPE "$1" 2>/dev/null | head -n1 } -writable_partition_number() { - local apps_part +partition_partlabel() { + lsblk -dnro PARTLABEL "$1" 2>/dev/null | head -n1 +} - apps_part=$(partition_path "$DEVICE" 3) - if [[ -b "$apps_part" ]]; then - printf '3\n' +partition_label() { + lsblk -dnro LABEL "$1" 2>/dev/null | head -n1 +} + +partition_number() { + lsblk -dnro PARTN "$1" 2>/dev/null | head -n1 +} + +device_partitions() { + lsblk -lnpo NAME "$DEVICE" | awk 'NR > 1 { print $1 }' +} + +writable_partition_path() { + local part + local partlabel + local fs_label + local last_ext4= + + while IFS= read -r part; do + [[ -b "$part" ]] || continue + partlabel=$(partition_partlabel "$part") + fs_label=$(partition_label "$part") + if [[ "$partlabel" == "apps" || "$fs_label" == "$APP_LABEL" ]]; then + printf '%s\n' "$part" + return 0 + fi + + if [[ "$(partition_fstype "$part")" == "ext4" ]]; then + last_ext4=$part + fi + done < <(device_partitions) + + [[ -n "$last_ext4" ]] || return 1 + printf '%s\n' "$last_ext4" +} + +writable_partition_name() { + local part=$1 + local partlabel + local fs_label + + partlabel=$(partition_partlabel "$part") + fs_label=$(partition_label "$part") + if [[ "$partlabel" == "apps" || "$fs_label" == "$APP_LABEL" ]]; then + printf 'app/state\n' else - printf '2\n' + printf 'root\n' fi } @@ -251,13 +294,10 @@ expand_writable_partition() { image_size=$(stat -c %s "$IMAGE_PATH") device_size=$(device_size_bytes "$DEVICE") - part_num=$(writable_partition_number) - target_part=$(partition_path "$DEVICE" "$part_num") - if [[ "$part_num" == "3" ]]; then - target_name="app/state" - else - target_name="root" - fi + target_part=$(writable_partition_path) || die "could not locate writable ForgeOS partition on $DEVICE" + part_num=$(partition_number "$target_part") + [[ -n "$part_num" ]] || die "could not determine partition number for $target_part" + target_name=$(writable_partition_name "$target_part") wait_for_block "$target_part" @@ -280,21 +320,19 @@ expand_writable_partition() { } randomize_ext4_uuids() { - local part_num local part local fs_type [[ "$RANDOMIZE_FS_UUIDS" -eq 1 ]] || return 0 - for part_num in 2 3; do - part=$(partition_path "$DEVICE" "$part_num") + while IFS= read -r part; do [[ -b "$part" ]] || continue fs_type=$(partition_fstype "$part") [[ "$fs_type" == "ext4" ]] || continue msg "randomizing ext4 filesystem UUID on $part" tune2fs -U random "$part" >/dev/null - done + done < <(device_partitions) } print_summary() { @@ -352,7 +390,7 @@ fi [[ -n "$DEVICE" ]] || die "missing required --device argument" -require_cmd lsblk findmnt parted partprobe dd blockdev e2fsck resize2fs tune2fs udevadm flock +require_cmd lsblk findmnt parted partprobe dd blockdev e2fsck resize2fs tune2fs udevadm flock awk require_root ensure_dirs