diff --git a/Makefile b/Makefile index 5a25290..14799d3 100644 --- a/Makefile +++ b/Makefile @@ -12,6 +12,8 @@ export DESKTOP ?= console export ENABLE_DESKTOP ?= 0 export ROOT_LABEL ?= FORGE_ROOT export EFI_LABEL ?= FORGE_EFI +export APP_LABEL ?= FORGE_APPS +export ATOMIC_BASE ?= 1 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 @@ -40,6 +42,7 @@ help: ' ENABLE_DESKTOP=1 make rootfs - add Openbox/tint2/PCManFM desktop' \ ' 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' \ ' 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' \ @@ -101,7 +104,7 @@ run-image: @./scripts/run-qemu.sh image run-desktop: ENABLE_DESKTOP = 1 -run-desktop: IMAGE_SIZE_MIB = 4096 +run-desktop: IMAGE_SIZE_MIB = 6144 run-desktop: MEMORY_MB = 3072 run-desktop: image @./scripts/run-qemu.sh image-gui diff --git a/README.md b/README.md index ae03c79..f0b656a 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,8 @@ 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 - - an ext4 root partition + - a read-only ext4 base root partition + - a writable ext4 app/state partition - a faster QEMU smoke-test path using `kernel + initramfs` ## Layout @@ -45,11 +46,17 @@ That dependency script is for Debian/Ubuntu hosts and installs only the packages The default console accounts are `forge` and `root`; both use the password `forge`. Console logins go through BusyBox `login`, Linux-PAM, and `pam_systemd.so`, so `systemd-logind` tracks real per-user sessions. -Nix is staged into the normal ForgeOS rootfs as the core package manager. On first boot, `forgeos-nix-bootstrap.service` registers the initial `/nix/store` closure, creates the default Nix profile, and starts `nix-daemon`. Log in as `forge` and install packages with the modern profile command: +Nix is staged into the writable ForgeOS app layer as the core package manager. Disk images boot a read-only base OS and bind the app layer over `/nix`, `/home`, `/root`, `/var`, `/opt`, and `/usr/local`. On first boot, `forgeos-nix-bootstrap.service` registers the initial `/nix/store` closure, creates the default Nix profile, and starts `nix-daemon`. Log in as `forge` and install apps with the friendly wrapper: + +```bash +forgeos-app install hello +hello +``` + +The wrapper uses Nix profiles underneath, so direct Nix commands still work: ```bash nix profile install nixpkgs#hello -hello ``` To build a UEFI disk image: @@ -101,9 +108,9 @@ sudo make install DISK=/dev/nvme0n1 - 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. - For default direct QEMU testing, `scripts/run-qemu.sh direct` uses the generated `rootfs.cpio.gz`. -- The host-side installer expands the root partition to fill larger target disks. +- 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. - 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. Override `NIX_VERSION` or `NIX_SYSTEM` if you intentionally want a different upstream tarball. +- 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`. - The console starts PAM-backed login prompts on `tty1` and `ttyS0` through native systemd units instead of BusyBox `inittab`. - The system bus is provided by source-built `dbus-daemon` and socket-activated at `/run/dbus/system_bus_socket`. @@ -146,7 +153,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 normal ForgeOS rootfs, but ForgeOS does not yet have a declarative NixOS-style system rebuild/update flow. +- 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. - 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/busybox.fragment b/config/busybox.fragment index e80c645..9fa347a 100644 --- a/config/busybox.fragment +++ b/config/busybox.fragment @@ -9,6 +9,7 @@ CONFIG_ASH_OPTIMIZE_FOR_SIZE=y CONFIG_ASH_BASH_COMPAT=y CONFIG_ASH_JOB_CONTROL=y CONFIG_ASH_ALIAS=y +CONFIG_AWK=y CONFIG_CAT=y CONFIG_CHMOD=y CONFIG_CHOWN=y diff --git a/config/kernel.fragment b/config/kernel.fragment index 1380266..aa98d43 100644 --- a/config/kernel.fragment +++ b/config/kernel.fragment @@ -88,5 +88,5 @@ CONFIG_IGB=y CONFIG_IGC=y CONFIG_TG3=y CONFIG_CMDLINE_BOOL=y -CONFIG_CMDLINE="root=PARTLABEL=root rootfstype=ext4 rootwait rw console=tty0 console=ttyS0,115200n8 loglevel=7 ignore_loglevel i915.enable_psr=0 i915.fastboot=0" +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 diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index c0debe7..7e7bc6e 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -22,15 +22,17 @@ 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. By default, `out/BOOTX64.EFI` is a standalone GRUB UEFI fallback loader with a small debug boot menu. -4. The built kernel EFI stub is copied to `EFI/BOOT/FORGEOS.EFI`. -5. UEFI firmware executes GRUB from `EFI/BOOT/BOOTX64.EFI`, and GRUB starts the kernel. -6. The built-in kernel command line mounts `PARTLABEL=root` as the real root filesystem. -7. systemd starts as `/sbin/init`, 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 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`. ## Root Filesystem Model -The root filesystem is assembled in `staging/rootfs` from: +The canonical root filesystem is assembled in `staging/rootfs` from: - systemd install output from `staging/systemd` - D-Bus install output from `staging/dbus` @@ -41,11 +43,18 @@ The root filesystem is assembled in `staging/rootfs` from: - kernel modules from the kernel build - the repository overlay in `overlay/rootfs` +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 + +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. + 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 rootfs. `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 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. +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. -The default console build also generates `out/rootfs.cpio.gz` for quick QEMU smoke tests. Desktop-enabled rootfs builds skip the initramfs artifact and boot through the ext4 root partition image. +The default console build also generates `out/rootfs.cpio.gz` for quick QEMU smoke tests. That direct initramfs path is intentionally monolithic and writable because it has no persistent app partition. Desktop-enabled rootfs builds skip the initramfs artifact and boot through the ext4 disk image path. For `DESKTOP=gnome`, `scripts/build-gnome-rootfs.sh` uses `mmdebstrap` or root-run `debootstrap` to assemble a Debian GNOME root filesystem in `staging/rootfs`, adds ForgeOS identity defaults, creates the initial desktop user, enables GDM and NetworkManager, and copies ForgeOS kernel modules. The GNOME image path deliberately does not copy `rootfs.cpio.gz` into the EFI System Partition because a desktop rootfs is too large for the initramfs-based smoke-test flow. @@ -96,6 +105,7 @@ This is enough for a first CLI OS image and a development boot path, but not yet - 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 10e50a4..97d7fb8 100644 --- a/docs/INSTALLER.md +++ b/docs/INSTALLER.md @@ -13,8 +13,8 @@ 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 partition `2` and the ext4 root filesystem to fill the device -7. randomizes the ext4 root filesystem UUID by default +6. expands the writable app/state partition to fill the device +7. randomizes ext4 filesystem UUIDs by default ## Basic Usage @@ -39,11 +39,11 @@ cd /home/joe/forgeos sudo make install DISK=/dev/sdc INSTALL_ARGS="--yes --unmount" ``` -Install without expanding the root filesystem: +Install without expanding the writable app/state filesystem: ```bash cd /home/joe/forgeos -sudo make install DISK=/dev/sdc INSTALL_ARGS="--no-expand-root" +sudo make install DISK=/dev/sdc INSTALL_ARGS="--no-expand-apps" ``` ## Direct Script Usage @@ -57,8 +57,10 @@ Optional arguments: - `--image /path/to/forgeos.img` - `--yes` - `--unmount` -- `--no-expand-root` -- `--keep-root-uuid` +- `--no-expand-apps` +- `--no-expand-root` as a legacy alias for `--no-expand-apps` +- `--keep-uuids` +- `--keep-root-uuid` as a legacy alias for `--keep-uuids` ## Secure Boot Images @@ -81,6 +83,7 @@ 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. - 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/etc/systemd/system/forgeos-home-ownership.service b/overlay/rootfs/etc/systemd/system/forgeos-home-ownership.service index 1838bb7..2cb1d63 100644 --- a/overlay/rootfs/etc/systemd/system/forgeos-home-ownership.service +++ b/overlay/rootfs/etc/systemd/system/forgeos-home-ownership.service @@ -6,7 +6,7 @@ Before=systemd-user-sessions.service getty.target forgeos-login@tty1.service for [Service] Type=oneshot -ExecStart=/bin/sh -c 'if [ -d /home/forge ]; then chown -R 1000:1000 /home/forge && chmod 700 /home/forge; fi' +ExecStart=/bin/sh -c 'if [ -d /home/forge ]; then chown -R 1000:1000 /home/forge 2>/dev/null || true; chmod 700 /home/forge 2>/dev/null || true; fi' [Install] WantedBy=multi-user.target diff --git a/overlay/rootfs/sbin/forgeos-switch-root b/overlay/rootfs/sbin/forgeos-switch-root index de7502a..9ab2e51 100755 --- a/overlay/rootfs/sbin/forgeos-switch-root +++ b/overlay/rootfs/sbin/forgeos-switch-root @@ -106,8 +106,7 @@ console_note "ForgeOS initramfs: locating real root filesystem..." ROOTDEV=$(wait_for_root_device) || drop_to_rescue "root partition not found" || exit 1 mkdir -p "$NEWROOT" -mount -t ext4 -o rw "$ROOTDEV" "$NEWROOT" 2>/dev/null || \ - mount -t ext4 -o ro "$ROOTDEV" "$NEWROOT" 2>/dev/null || \ +mount -t ext4 -o ro "$ROOTDEV" "$NEWROOT" 2>/dev/null || \ drop_to_rescue "could not mount $ROOTDEV" || exit 1 if ! is_mounted_newroot; then diff --git a/overlay/rootfs/usr/bin/forgeos-app b/overlay/rootfs/usr/bin/forgeos-app new file mode 100644 index 0000000..87e6200 --- /dev/null +++ b/overlay/rootfs/usr/bin/forgeos-app @@ -0,0 +1,143 @@ +#!/bin/sh + +set -eu + +PATH=/usr/bin:/usr/sbin:/bin:/sbin +export PATH + +usage() { + cat <<'EOF' +Usage: + forgeos-app status + forgeos-app install NAME|FLAKE... + forgeos-app remove PACKAGE... + forgeos-app list + forgeos-app search TERMS... + forgeos-app upgrade [PACKAGE...] + forgeos-app rollback + forgeos-app shell NAME|FLAKE... + forgeos-app gc + +Short names are expanded to nixpkgs#NAME for install and shell. +EOF +} + +need_nix() { + command -v nix >/dev/null 2>&1 || { + echo "nix is unavailable; is the ForgeOS app layer mounted?" >&2 + exit 1 + } +} + +as_installable() { + case "$1" in + *#*|/*|git+*|http://*|https://*) + printf '%s\n' "$1" + ;; + *) + printf 'nixpkgs#%s\n' "$1" + ;; + esac +} + +show_mount() { + awk -v target="$1" ' + $2 == target { + printf "%-12s <- %s (%s, %s)\n", target, $1, $3, $4 + found = 1 + } + END { + if (!found) { + printf "%-12s <- not mounted\n", target + } + } + ' /proc/mounts +} + +cmd=${1:-} +if [ -z "$cmd" ]; then + usage + exit 2 +fi +shift || true + +case "$cmd" in + status) + show_mount / + show_mount /forge + show_mount /nix + show_mount /home + show_mount /root + show_mount /var + show_mount /opt + show_mount /usr/local + df -h /forge /nix /home 2>/dev/null || true + ;; + install) + [ "$#" -gt 0 ] || { + usage >&2 + exit 2 + } + need_nix + for item in "$@"; do + nix profile install "$(as_installable "$item")" + done + ;; + remove) + [ "$#" -gt 0 ] || { + usage >&2 + exit 2 + } + need_nix + nix profile remove "$@" + ;; + list) + need_nix + nix profile list + ;; + search) + [ "$#" -gt 0 ] || { + usage >&2 + exit 2 + } + need_nix + nix search nixpkgs "$@" + ;; + upgrade) + need_nix + if [ "$#" -eq 0 ]; then + nix profile upgrade '.*' + else + nix profile upgrade "$@" + fi + ;; + rollback) + need_nix + nix profile rollback + ;; + shell) + [ "$#" -gt 0 ] || { + usage >&2 + exit 2 + } + need_nix + set -- "$@" + expanded= + for item in "$@"; do + expanded="${expanded}${expanded:+ }$(as_installable "$item")" + done + # shellcheck disable=SC2086 + exec nix shell $expanded + ;; + gc) + need_nix + nix store gc + ;; + help|--help|-h) + usage + ;; + *) + usage >&2 + exit 2 + ;; +esac diff --git a/overlay/rootfs/usr/lib/tmpfiles.d/forgeos.conf b/overlay/rootfs/usr/lib/tmpfiles.d/forgeos.conf index c98959d..b5ce7be 100644 --- a/overlay/rootfs/usr/lib/tmpfiles.d/forgeos.conf +++ b/overlay/rootfs/usr/lib/tmpfiles.d/forgeos.conf @@ -1,5 +1,3 @@ d /home/forge 0700 forge forge - z /home/forge 0700 forge forge - z /root 0700 root root - -z /etc/shadow 0600 root root - -z /etc/gshadow 0600 root root - diff --git a/scripts/build-image.sh b/scripts/build-image.sh index 219f001..b563cb8 100755 --- a/scripts/build-image.sh +++ b/scripts/build-image.sh @@ -4,12 +4,15 @@ set -euo pipefail source "$(dirname "$0")/common.sh" -require_cmd qemu-img parted mkfs.vfat mkfs.ext4 mcopy mmd dd awk truncate +require_cmd qemu-img parted mkfs.vfat mkfs.ext4 mcopy mmd dd awk sed truncate rsync du find fakeroot ensure_dirs 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 + [[ -f "$ROOTFS_STAGING_DIR/etc/machine-id" ]] || return 1 + [[ ! -f "$ROOTFS_STAGING_DIR/etc/fstab" ]] || return 1 if truthy "$ENABLE_DESKTOP"; then [[ -f "$ROOTFS_STAGING_DIR/etc/forgeos-desktop" ]] || return 1 @@ -39,6 +42,9 @@ GRUB_CFG="$OUT_DIR/grub.cfg" ESP_END_MIB=$((1 + ESP_SIZE_MIB)) ESP_IMAGE="$OUT_DIR/forgeos-esp.img" ROOT_IMAGE="$OUT_DIR/forgeos-root.img" +APP_IMAGE="$OUT_DIR/forgeos-apps.img" +IMAGE_ROOTFS_DIR="$ROOTFS_STAGING_DIR" +APP_IMAGE_NEEDED=0 partition_field() { local partition=$1 @@ -51,7 +57,139 @@ partition_field() { ' } +reset_staging_tree() { + local path=$1 + + if [[ -d "$path" ]]; then + chmod -R u+w "$path" 2>/dev/null || true + fi + rm -rf "$path" + mkdir -p "$path" +} + +copy_app_path() { + local rel=$1 + local app_rel=$2 + local src="$ROOTFS_STAGING_DIR/$rel" + local dest="$APPFS_STAGING_DIR/$app_rel" + + mkdir -p "$dest" + if [[ -d "$src" ]]; then + rsync -a "$src"/ "$dest"/ + fi +} + +empty_base_mountpoint() { + local rel=$1 + local path="$BASE_ROOTFS_STAGING_DIR/$rel" + + if [[ -e "$path" || -L "$path" ]]; then + chmod -R u+w "$path" 2>/dev/null || true + fi + rm -rf "$path" + mkdir -p "$path" +} + +dir_size_mib() { + du -sm "$1" | awk '{ print $1 }' +} + +build_ext4_image() { + local label=$1 + local tree=$2 + local image=$3 + local size=$4 + + truncate -s "$size" "$image" + fakeroot -- bash -euo pipefail -c ' + tree=$1 + label=$2 + image=$3 + + find "$tree" -xdev -exec chown -h 0:0 {} + 2>/dev/null || true + + if [[ -d "$tree/home/forge" ]]; then + find "$tree/home/forge" -xdev -exec chown -h 1000:1000 {} + 2>/dev/null || true + chmod 0700 "$tree/home/forge" + fi + + [[ ! -d "$tree/root" ]] || chmod 0700 "$tree/root" + + if [[ -d "$tree/nix/store" ]]; then + find "$tree/nix/store" -xdev -exec chown -h 0:30000 {} + 2>/dev/null || true + chmod 1775 "$tree/nix/store" + chmod -R ugo-w "$tree/nix/store"/* 2>/dev/null || true + fi + + mkfs.ext4 -F -L "$label" -d "$tree" "$image" >/dev/null + ' bash "$tree" "$label" "$image" +} + +write_atomic_fstab() { + cat > "$BASE_ROOTFS_STAGING_DIR/etc/fstab" < "$OUT_DIR/base-rootfs.manifest" + find "$APPFS_STAGING_DIR" -printf '%P\n' | LC_ALL=C sort > "$OUT_DIR/appfs.manifest" + + IMAGE_ROOTFS_DIR="$BASE_ROOTFS_STAGING_DIR" + APP_IMAGE_NEEDED=1 +} + write_grub_config() { + local root_mode=rw + + if atomic_base_enabled; then + root_mode=ro + fi + cat > "$GRUB_CFG" <<'EOF' set timeout=10 set default=0 @@ -63,7 +201,7 @@ set gfxpayload=keep insmod all_video insmod gfxterm terminal_output gfxterm console -set linux_common="root=PARTLABEL=root rootfstype=ext4 rootwait rw console=tty0 loglevel=7 ignore_loglevel earlyprintk=efi,keep vt.global_cursor_default=1" +set linux_common="__LINUX_COMMON__" set initrd_common="rdinit=/sbin/init" set handoff_common="rdinit=/init forgeos.switch_root=1" @@ -88,6 +226,8 @@ 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" + if [[ "$DESKTOP" != "gnome" ]] && ! truthy "$ENABLE_DESKTOP"; then cat >> "$GRUB_CFG" <<'EOF' @@ -188,20 +328,54 @@ prepare_efi_loader() { msg "preparing EFI loader" prepare_efi_loader +if atomic_base_enabled; then + prepare_atomic_image_trees + + BASE_USED_MIB=$(dir_size_mib "$BASE_ROOTFS_STAGING_DIR") + APP_USED_MIB=$(dir_size_mib "$APPFS_STAGING_DIR") + BASE_HEADROOM_MIB=${BASE_HEADROOM_MIB:-256} + APP_HEADROOM_MIB=${APP_HEADROOM_MIB:-1024} + 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)) + 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" +fi + msg "creating raw image $IMAGE_PATH" -rm -f "$IMAGE_PATH" "$ESP_IMAGE" "$ROOT_IMAGE" +rm -f "$IMAGE_PATH" "$ESP_IMAGE" "$ROOT_IMAGE" "$APP_IMAGE" qemu-img create -f raw "$IMAGE_PATH" "${IMAGE_SIZE_MIB}M" >/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 -parted -s "$IMAGE_PATH" mkpart root ext4 "${ESP_END_MIB}MiB" 100% +if [[ "$APP_IMAGE_NEEDED" -eq 1 ]]; then + parted -s "$IMAGE_PATH" mkpart root ext4 "${ESP_END_MIB}MiB" "${BASE_END_MIB}MiB" + parted -s "$IMAGE_PATH" mkpart apps ext4 "${BASE_END_MIB}MiB" 100% +else + parted -s "$IMAGE_PATH" mkpart root ext4 "${ESP_END_MIB}MiB" 100% +fi ESP_OFFSET=$(partition_field 1 2) ESP_SIZE=$(partition_field 1 4) ROOT_OFFSET=$(partition_field 2 2) ROOT_SIZE=$(partition_field 2 4) +if [[ "$APP_IMAGE_NEEDED" -eq 1 ]]; then + APP_OFFSET=$(partition_field 3 2) + APP_SIZE=$(partition_field 3 4) +else + APP_OFFSET= + APP_SIZE= +fi [[ -n "$ESP_OFFSET" && -n "$ESP_SIZE" && -n "$ROOT_OFFSET" && -n "$ROOT_SIZE" ]] || die "failed to parse partition layout" +if [[ "$APP_IMAGE_NEEDED" -eq 1 ]]; then + [[ -n "$APP_OFFSET" && -n "$APP_SIZE" ]] || die "failed to parse app partition layout" +fi msg "building EFI partition image" truncate -s "$ESP_SIZE" "$ESP_IMAGE" @@ -215,12 +389,19 @@ if [[ "$DESKTOP" != "gnome" ]] && ! truthy "$ENABLE_DESKTOP"; then fi msg "building ext4 root partition image" -truncate -s "$ROOT_SIZE" "$ROOT_IMAGE" -mkfs.ext4 -F -L "$ROOT_LABEL" -d "$ROOTFS_STAGING_DIR" "$ROOT_IMAGE" >/dev/null +build_ext4_image "$ROOT_LABEL" "$IMAGE_ROOTFS_DIR" "$ROOT_IMAGE" "$ROOT_SIZE" + +if [[ "$APP_IMAGE_NEEDED" -eq 1 ]]; then + msg "building ext4 app/state partition image" + build_ext4_image "$APP_LABEL" "$APPFS_STAGING_DIR" "$APP_IMAGE" "$APP_SIZE" +fi msg "embedding partitions into raw disk image" dd if="$ESP_IMAGE" of="$IMAGE_PATH" bs=4M seek="$ESP_OFFSET" oflag=seek_bytes conv=notrunc status=none dd if="$ROOT_IMAGE" of="$IMAGE_PATH" bs=4M seek="$ROOT_OFFSET" oflag=seek_bytes conv=notrunc status=none +if [[ "$APP_IMAGE_NEEDED" -eq 1 ]]; then + dd if="$APP_IMAGE" of="$IMAGE_PATH" bs=4M seek="$APP_OFFSET" oflag=seek_bytes conv=notrunc status=none +fi sync msg "disk image ready: $IMAGE_PATH" diff --git a/scripts/build-kernel.sh b/scripts/build-kernel.sh index 8bc98ea..4291c75 100755 --- a/scripts/build-kernel.sh +++ b/scripts/build-kernel.sh @@ -16,7 +16,7 @@ mkdir -p "$KERNEL_BUILD_DIR" "$MODULES_STAGING_DIR" make -C "$KERNEL_SRC_DIR" O="$KERNEL_BUILD_DIR" ARCH="$ARCH" x86_64_defconfig apply_kconfig_fragment "$KERNEL_BUILD_DIR/.config" "$ROOT_DIR/config/kernel.fragment" cmdline_fragment=$(mktemp) -printf 'CONFIG_CMDLINE="root=PARTLABEL=root rootfstype=ext4 rootwait rw console=tty0 console=ttyS0,115200n8 loglevel=7 ignore_loglevel i915.enable_psr=0 i915.fastboot=0"\n' > "$cmdline_fragment" +printf '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"\n' > "$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 diff --git a/scripts/build-rootfs.sh b/scripts/build-rootfs.sh index 1b4b5c9..d01607e 100755 --- a/scripts/build-rootfs.sh +++ b/scripts/build-rootfs.sh @@ -63,6 +63,7 @@ ensure_dirs [[ -x "$BUSYBOX_BUILD_DIR/busybox" && -f "$OUT_DIR/busybox.config" ]] || "$ROOT_DIR/scripts/build-busybox.sh" grep -qx 'CONFIG_PAM=y' "$OUT_DIR/busybox.config" || "$ROOT_DIR/scripts/build-busybox.sh" grep -qx 'CONFIG_LOGIN_SESSION_AS_CHILD=y' "$OUT_DIR/busybox.config" || "$ROOT_DIR/scripts/build-busybox.sh" +grep -qx 'CONFIG_AWK=y' "$OUT_DIR/busybox.config" || "$ROOT_DIR/scripts/build-busybox.sh" [[ -x "$SYSTEMD_STAGING_DIR/usr/lib/systemd/systemd" && -x "$SYSTEMD_STAGING_DIR/usr/lib/systemd/systemd-logind" && -f "$SYSTEMD_STAGING_DIR/usr/lib/security/pam_systemd.so" && -f "$SYSTEMD_STAGING_DIR/.forgeos-systemd-complete" ]] || "$ROOT_DIR/scripts/build-systemd.sh" [[ -x "$DBUS_STAGING_DIR/usr/bin/dbus-daemon" && -f "$DBUS_STAGING_DIR/.forgeos-dbus-complete" ]] || "$ROOT_DIR/scripts/build-dbus.sh" [[ -f "$OUT_DIR/bzImage" ]] || "$ROOT_DIR/scripts/build-kernel.sh" @@ -84,7 +85,7 @@ rsync -a "$SYSTEMD_STAGING_DIR"/ "$ROOTFS_STAGING_DIR"/ rsync -a "$DBUS_STAGING_DIR"/ "$ROOTFS_STAGING_DIR"/ copy_busybox_deps -mkdir -p "$ROOTFS_STAGING_DIR"/{proc,sys,dev,run/dbus,tmp,var/lib/dbus,var/log,root,home/forge,boot,mnt,etc/systemd/system} +mkdir -p "$ROOTFS_STAGING_DIR"/{proc,sys,dev,run/dbus,tmp,var/lib/dbus,var/log,root,home/forge,boot,mnt,forge,opt,usr/local,etc/systemd/system} rsync -a "$OVERLAY_DIR"/ "$ROOTFS_STAGING_DIR"/ "$ROOT_DIR/scripts/stage-nix.sh" @@ -93,6 +94,8 @@ ln -s ../usr/lib/systemd/systemd "$ROOTFS_STAGING_DIR/sbin/init" ln -sfn ../../sbin/modprobe "$ROOTFS_STAGING_DIR/usr/bin/modprobe" ln -sfn ../run/systemd/resolve/resolv.conf "$ROOTFS_STAGING_DIR/etc/resolv.conf" ln -sfn ../run "$ROOTFS_STAGING_DIR/var/run" +: > "$ROOTFS_STAGING_DIR/etc/machine-id" +chmod 0444 "$ROOTFS_STAGING_DIR/etc/machine-id" mkdir -p \ "$ROOTFS_STAGING_DIR/etc/systemd/system/getty.target.wants" \ @@ -164,6 +167,7 @@ chmod 755 \ "$ROOTFS_STAGING_DIR/sbin/forgeos-console-banner" \ "$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/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 0d5d5e7..6023873 100755 --- a/scripts/common.sh +++ b/scripts/common.sh @@ -22,6 +22,8 @@ DESKTOP=${DESKTOP:-console} ENABLE_DESKTOP=${ENABLE_DESKTOP:-0} ROOT_LABEL=${ROOT_LABEL:-FORGE_ROOT} EFI_LABEL=${EFI_LABEL:-FORGE_EFI} +APP_LABEL=${APP_LABEL:-FORGE_APPS} +ATOMIC_BASE=${ATOMIC_BASE:-1} case "$DESKTOP" in console|gnome) ;; *) @@ -83,6 +85,8 @@ SYSTEMD_STAGING_DIR="$STAGING_DIR/systemd" DBUS_STAGING_DIR="$STAGING_DIR/dbus" PAM_STAGING_DIR="$STAGING_DIR/pam" ROOTFS_STAGING_DIR="$STAGING_DIR/rootfs" +BASE_ROOTFS_STAGING_DIR="$STAGING_DIR/rootfs-base" +APPFS_STAGING_DIR="$STAGING_DIR/appfs" OPENBOX_DESKTOP_DEB_DIR=${OPENBOX_DESKTOP_DEB_DIR:-"$DOWNLOAD_DIR/openbox-desktop-debs"} OPENBOX_DESKTOP_APT_STATE_DIR=${OPENBOX_DESKTOP_APT_STATE_DIR:-"$BUILD_DIR/openbox-desktop-apt-state"} @@ -116,6 +120,10 @@ truthy() { esac } +atomic_base_enabled() { + [[ "$DESKTOP" != "gnome" ]] && truthy "$ATOMIC_BASE" +} + ensure_dirs() { mkdir -p "$DOWNLOAD_DIR" "$SOURCE_DIR" "$BUILD_DIR" "$STAGING_DIR" "$OUT_DIR" } diff --git a/scripts/install-image-deps.sh b/scripts/install-image-deps.sh index 5c147d4..0ba4a7c 100755 --- a/scripts/install-image-deps.sh +++ b/scripts/install-image-deps.sh @@ -17,6 +17,7 @@ image_packages=( grub-common grub-efi-amd64-bin ovmf + fakeroot ) required_commands=( @@ -31,6 +32,7 @@ required_commands=( awk truncate qemu-system-x86_64 + fakeroot ) required_paths=( diff --git a/scripts/install-to-disk.sh b/scripts/install-to-disk.sh index 57bd03c..58db3c1 100755 --- a/scripts/install-to-disk.sh +++ b/scripts/install-to-disk.sh @@ -7,9 +7,9 @@ source "$(dirname "$0")/common.sh" IMAGE_PATH="$OUT_DIR/forgeos-${KERNEL_VERSION}.img" DEVICE="" AUTO_CONFIRM=0 -EXPAND_ROOT=1 +EXPAND_WRITABLE=1 LIST_ONLY=0 -RANDOMIZE_ROOT_UUID=1 +RANDOMIZE_FS_UUIDS=1 UNMOUNT_TARGET=0 CUSTOM_IMAGE=0 @@ -25,8 +25,10 @@ Options: --list Show candidate disk devices and exit. --yes Skip the interactive destruction prompt. --unmount Unmount non-critical mounted target partitions first. - --no-expand-root Keep the root partition at the image's built size. - --keep-root-uuid Preserve the ext4 root filesystem UUID. + --no-expand-apps Keep the writable app/state partition at the image's built size. + --no-expand-root Legacy alias for --no-expand-apps. + --keep-uuids Preserve ext4 filesystem UUIDs. + --keep-root-uuid Legacy alias for --keep-uuids. --help Show this help text. Examples: @@ -128,7 +130,7 @@ ensure_target_is_safe() { ;; esac - for critical_mount in / /boot /boot/efi /home; do + for critical_mount in / /boot /boot/efi /home /nix /var /forge /opt /usr/local; do source=$(findmnt -nr -o SOURCE "$critical_mount" 2>/dev/null || true) if critical_source_on_target "$source"; then die "refusing to overwrite active system storage mounted at $critical_mount" @@ -160,7 +162,7 @@ unmount_target_children() { part=${entry%%|*} mountpoint=${entry#*|} case "$mountpoint" in - /|/boot|/boot/efi|/home) + /|/boot|/boot/efi|/home|/nix|/var|/forge|/opt|/usr/local) die "refusing to unmount critical mountpoint $mountpoint on $part" ;; esac @@ -225,45 +227,74 @@ repair_and_rescan_partitions() { udevadm settle >/dev/null 2>&1 || true } -expand_root_partition() { +partition_fstype() { + lsblk -dnro FSTYPE "$1" 2>/dev/null | head -n1 +} + +writable_partition_number() { + local apps_part + + apps_part=$(partition_path "$DEVICE" 3) + if [[ -b "$apps_part" ]]; then + printf '3\n' + else + printf '2\n' + fi +} + +expand_writable_partition() { local image_size local device_size - local root_part + local part_num + local target_part + local target_name image_size=$(stat -c %s "$IMAGE_PATH") device_size=$(device_size_bytes "$DEVICE") - root_part=$(partition_path "$DEVICE" 2) + 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 - wait_for_block "$root_part" + wait_for_block "$target_part" - if (( device_size <= image_size )) || [[ "$EXPAND_ROOT" -ne 1 ]]; then - msg "leaving root partition at image size" + if (( device_size <= image_size )) || [[ "$EXPAND_WRITABLE" -ne 1 ]]; then + msg "leaving $target_name partition at image size" return fi - msg "expanding root partition to fill the remaining disk" - parted -s -f "$DEVICE" resizepart 2 100% + msg "expanding $target_name partition to fill the remaining disk" + parted -s -f "$DEVICE" resizepart "$part_num" 100% partprobe "$DEVICE" >/dev/null 2>&1 || true udevadm settle >/dev/null 2>&1 || true - wait_for_block "$root_part" + wait_for_block "$target_part" - msg "checking ext4 root filesystem" - e2fsck -fy "$root_part" >/dev/null + msg "checking ext4 $target_name filesystem" + e2fsck -fy "$target_part" >/dev/null - msg "resizing ext4 root filesystem" - resize2fs "$root_part" >/dev/null + msg "resizing ext4 $target_name filesystem" + resize2fs "$target_part" >/dev/null } -randomize_root_uuid() { - local root_part +randomize_ext4_uuids() { + local part_num + local part + local fs_type - [[ "$RANDOMIZE_ROOT_UUID" -eq 1 ]] || return 0 + [[ "$RANDOMIZE_FS_UUIDS" -eq 1 ]] || return 0 - root_part=$(partition_path "$DEVICE" 2) - wait_for_block "$root_part" + for part_num in 2 3; do + part=$(partition_path "$DEVICE" "$part_num") + [[ -b "$part" ]] || continue + fs_type=$(partition_fstype "$part") + [[ "$fs_type" == "ext4" ]] || continue - msg "randomizing ext4 root filesystem UUID" - tune2fs -U random "$root_part" >/dev/null + msg "randomizing ext4 filesystem UUID on $part" + tune2fs -U random "$part" >/dev/null + done } print_summary() { @@ -296,12 +327,12 @@ while [[ $# -gt 0 ]]; do UNMOUNT_TARGET=1 shift ;; - --no-expand-root) - EXPAND_ROOT=0 + --no-expand-apps|--no-expand-root) + EXPAND_WRITABLE=0 shift ;; - --keep-root-uuid) - RANDOMIZE_ROOT_UUID=0 + --keep-uuids|--keep-root-uuid) + RANDOMIZE_FS_UUIDS=0 shift ;; --help|-h) @@ -339,8 +370,8 @@ fi confirm_install write_image repair_and_rescan_partitions -expand_root_partition -randomize_root_uuid +expand_writable_partition +randomize_ext4_uuids repair_and_rescan_partitions print_summary diff --git a/scripts/stage-nix.sh b/scripts/stage-nix.sh index 73607f8..f05bafc 100755 --- a/scripts/stage-nix.sh +++ b/scripts/stage-nix.sh @@ -119,11 +119,41 @@ mkdir -p \\ "\$NIX_STATE/daemon-socket" \\ /root -chown root:root /nix /nix/var /nix/var/nix /nix/var/log /nix/var/log/nix /nix/var/log/nix/drvs 2>/dev/null || true +chown root:root \\ + /nix \\ + /nix/var \\ + /nix/var/log \\ + /nix/var/log/nix \\ + /nix/var/log/nix/drvs \\ + "\$NIX_STATE" \\ + "\$NIX_STATE/bootstrap" \\ + "\$NIX_STATE/db" \\ + "\$NIX_STATE/gcroots" \\ + "\$NIX_STATE/gcroots/per-user" \\ + "\$NIX_STATE/profiles" \\ + "\$NIX_STATE/profiles/per-user" \\ + "\$NIX_STATE/temproots" \\ + "\$NIX_STATE/userpool" \\ + "\$NIX_STATE/daemon-socket" 2>/dev/null || true chown -R root:nixbld /nix/store 2>/dev/null || true chmod 1775 /nix/store chmod -R ugo-w /nix/store/* 2>/dev/null || true -chmod 0755 "\$NIX_STATE" "\$NIX_STATE/bootstrap" "\$NIX_STATE/daemon-socket" +chmod 0755 \\ + /nix \\ + /nix/var \\ + /nix/var/log \\ + /nix/var/log/nix \\ + /nix/var/log/nix/drvs \\ + "\$NIX_STATE" \\ + "\$NIX_STATE/bootstrap" \\ + "\$NIX_STATE/db" \\ + "\$NIX_STATE/gcroots" \\ + "\$NIX_STATE/gcroots/per-user" \\ + "\$NIX_STATE/profiles" \\ + "\$NIX_STATE/profiles/per-user" \\ + "\$NIX_STATE/temproots" \\ + "\$NIX_STATE/userpool" \\ + "\$NIX_STATE/daemon-socket" if [ -e "\$MARKER" ]; then exit 0 @@ -151,9 +181,11 @@ cat > "$ROOTFS_STAGING_DIR/etc/systemd/system/forgeos-nix-bootstrap.service" <<' Description=Bootstrap ForgeOS Nix store DefaultDependencies=no After=local-fs.target -Before=sockets.target multi-user.target +Before=nix-daemon.socket nix-daemon.service sockets.target multi-user.target +RequiresMountsFor=/nix/store +RequiresMountsFor=/nix/var/nix/bootstrap +RequiresMountsFor=/nix/var/nix/db ConditionPathExists=/nix/var/nix/bootstrap/reginfo -ConditionPathExists=!/nix/var/nix/forgeos-bootstrap-complete [Service] Type=oneshot @@ -169,6 +201,7 @@ cat > "$ROOTFS_STAGING_DIR/etc/systemd/system/nix-daemon.service" < "$ROOTFS_STAGING_DIR/etc/systemd/system/nix-daemon.socket" <<'EOF' [Unit] Description=Nix package manager daemon socket +After=forgeos-nix-bootstrap.service Before=multi-user.target +Requires=forgeos-nix-bootstrap.service RequiresMountsFor=/nix/store ConditionPathIsReadWrite=/nix/var/nix/daemon-socket