interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / scripts / build_standalone_recovery_initrd.sh

308 lines · 12067 bytes Raw Blame History
1#!/usr/bin/env bash
2# SPDX-License-Identifier: Apache-2.0
3# Copyright 2026 Drake Stapleton (AIEN / aien-dev)
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17# build_standalone_recovery_initrd.sh: Creates a self-contained recovery
18# initrd for embedding inside an owner-signed standalone GRUB EFI image.
19# The initrd by itself is NOT authenticated; never load it from USB as an
20# external GRUB initrd. No internal NVMe root or disk secrets are needed.
21
22set -euo pipefail
23
24if [[ $# != 1 || -z "$1" || -L "$1" || -e "$1" ]]; then
25 echo "Usage: $0 <new-output-initrd> (must not already exist or be a symlink)" >&2
26 exit 1
27fi
28set -C # O_EXCL-style refusal if the output appears between the check and open.
29OUT_INITRD="$1"
30WORK_DIR=$(mktemp -d /tmp/recovery-initrd-build.XXXXXX)
31
32cleanup() {
33 rm -rf "$WORK_DIR"
34}
35trap cleanup EXIT
36
37echo "Building standalone recovery root in $WORK_DIR..."
38mkdir -p "$WORK_DIR"/{bin,sbin,usr/bin,usr/sbin,proc,sys,dev,etc,mnt/root,mnt/cipher,mnt/plain,tmp,lib,lib64}
39
40# Ensure base binaries are present
41BINARIES=(
42 /bin/busybox
43 /bin/sh
44 /bin/bash
45 /bin/lsblk
46 /sbin/blkid
47 /bin/mount
48 /bin/umount
49 /bin/mkdir
50 /bin/cat
51 /bin/grep
52 /bin/sed
53 /bin/sha256sum
54 /usr/bin/age
55 # Gate 1 item 10: check Authenticode signatures of the ESP loaders.
56 /usr/bin/sbverify
57 # FUSE mount helper; gocryptfs falls back to it if a direct mount fails.
58 /usr/bin/fusermount3
59 # FLAG(sovereignty): tpm2_pcrread is copied into the recovery image (outside dep); replace with in-house C PCR reader.
60 /usr/bin/tpm2_pcrread
61 /usr/bin/efibootmgr
62 /usr/bin/findmnt
63 /sbin/cryptsetup
64 # Repair tools for issue #17: repair /boot/efi (fsck.vfat, mkfs.vfat),
65 # check the NVMe root filesystem (fsck.ext4), and chroot into it to
66 # restore boot entries from the installed bootloader.
67 /usr/sbin/fsck.vfat
68 /usr/sbin/mkfs.vfat
69 /usr/sbin/fsck.ext4
70 /usr/sbin/chroot
71)
72
73for bin in "${BINARIES[@]}"; do
74 if [[ -f "$bin" ]]; then
75 cp -p "$bin" "$WORK_DIR/bin/"
76 # Copy dynamically linked libraries if binary is dynamic
77 if ldd "$bin" >/dev/null 2>&1; then
78 ldd "$bin" 2>/dev/null | grep -o '/lib[^ ]*' | while read -r lib; do
79 if [[ -f "$lib" ]]; then
80 mkdir -p "$WORK_DIR/$(dirname "$lib")"
81 cp -p -u "$lib" "$WORK_DIR/$lib" 2>/dev/null || true
82 fi
83 done
84 fi
85 else
86 echo "Error: required recovery binary $bin not found on the build host" >&2
87 exit 1
88 fi
89done
90
91# The attended collector must travel on the recovery media itself; it cannot
92# depend on a repository checkout being present in the RAM rescue shell.
93mkdir -p "$WORK_DIR/usr/local/sbin"
94install -m 0755 "$(dirname "$0")/collect_recovery_boot_evidence.sh" \
95 "$WORK_DIR/usr/local/sbin/collect_recovery_boot_evidence"
96
97# gocryptfs is the only way to open the private storage and the Forgejo
98# store from the rescue shell (Gate 1 items 11-12): a stick without it is not
99# a recovery stick, so fail loudly unless the caller explicitly accepts that.
100# FUSE itself is built into the Ubuntu kernel (modprobe reports "builtin").
101GOCRYPTFS_BIN="${AIENOS_GOCRYPTFS:-$(command -v gocryptfs 2>/dev/null || echo "/home/atlas/atlas-forgejo-setup-20260904/runtime/usr/bin/gocryptfs")}"
102if [[ ! -f "$GOCRYPTFS_BIN" ]]; then
103 if [[ "${AIENOS_RECOVERY_ALLOW_NO_GOCRYPTFS:-0}" == 1 ]]; then
104 echo "WARNING: gocryptfs not found; this image cannot unlock gocryptfs storage" >&2
105 else
106 echo "Error: gocryptfs not found at $GOCRYPTFS_BIN (set AIENOS_GOCRYPTFS, or AIENOS_RECOVERY_ALLOW_NO_GOCRYPTFS=1 for a test-only image)" >&2
107 exit 1
108 fi
109fi
110if [[ -f "$GOCRYPTFS_BIN" ]]; then
111 cp -p "$GOCRYPTFS_BIN" "$WORK_DIR/bin/gocryptfs"
112 if ldd "$GOCRYPTFS_BIN" >/dev/null 2>&1; then
113 ldd "$GOCRYPTFS_BIN" 2>/dev/null | grep -o '/lib[^ ]*' | while read -r lib; do
114 if [[ -f "$lib" ]]; then
115 mkdir -p "$WORK_DIR/$(dirname "$lib")"
116 cp -p -u "$lib" "$WORK_DIR/$lib" 2>/dev/null || true
117 fi
118 done
119 fi
120fi
121
122# FLAG(sovereignty): tpm2-tools (outside dep) is packaged into the recovery initrd; replace with in-house C PCR reader.
123# tpm2-tools load their transport library at run time (dlopen), so ldd never
124# lists it. Package the device transport so tpm2_pcrread can talk to
125# /dev/tpmrm0 in the rescue shell (Gate 1 item 8).
126tcti_found=0
127for lib in /lib/*-linux-gnu/libtss2-tcti-device.so.0*; do
128 [[ -e "$lib" ]] || continue
129 mkdir -p "$WORK_DIR/$(dirname "$lib")"
130 cp -a "$lib" "$WORK_DIR/$lib"
131 tcti_found=1
132done
133[[ "$tcti_found" == 1 ]] || { echo "Error: libtss2-tcti-device not found on the build host" >&2; exit 1; }
134
135# Kernel drivers the recovery shell needs on Machine 1 that the Ubuntu kernel
136# builds as modules: the ACPI xHCI controllers (NVDA8000), USB and Logitech
137# keyboards, USB sticks, the internal NVMe, dm-crypt and FAT text encodings.
138# Modules must match the copied kernel. The media builder passes its version;
139# a standalone build defaults to the newest local /boot/vmlinuz-*.
140KVER="${AIENOS_RECOVERY_KVER:-$(basename "$(ls -1t /boot/vmlinuz-* | head -n 1)" | sed 's/^vmlinuz-//')}"
141RECOVERY_MODULES=(
142 xhci-plat-hcd
143 usbhid hid-generic hid-logitech-dj hid-logitech-hidpp
144 usb-storage uas
145 nvme
146 dm-crypt
147 nls_iso8859-1 nls_utf8
148)
149
150mkdir -p "$WORK_DIR/lib/modules/aienos" "$WORK_DIR/etc"
151: > "$WORK_DIR/etc/aienos-modules.order"
152for mod in "${RECOVERY_MODULES[@]}"; do
153 deps=$(modprobe -S "$KVER" --show-depends "$mod") || {
154 echo "Error: module $mod not found for kernel $KVER" >&2
155 exit 1
156 }
157 # Dependencies come first; builtin lines need nothing copied.
158 while read -r kind path _; do
159 [[ "$kind" == "insmod" ]] || continue
160 name=$(basename "$path")
161 name="${name%.zst}"
162 name="${name%.xz}"
163 grep -qxF "$name" "$WORK_DIR/etc/aienos-modules.order" && continue
164 # Decompress so busybox insmod can load it; the signature appended
165 # before compression is kept, so Secure Boot lockdown still accepts it.
166 case "$path" in
167 *.zst) zstd -q -d -c "$path" > "$WORK_DIR/lib/modules/aienos/$name" ;;
168 *.xz) xz -d -c "$path" > "$WORK_DIR/lib/modules/aienos/$name" ;;
169 *) cp -p "$path" "$WORK_DIR/lib/modules/aienos/$name" ;;
170 esac
171 echo "$name" >> "$WORK_DIR/etc/aienos-modules.order"
172 done <<< "$deps"
173done
174echo "Packaged $(wc -l < "$WORK_DIR/etc/aienos-modules.order") kernel modules for $KVER"
175
176# Create standalone init script
177cat << 'INIT_EOF' > "$WORK_DIR/init"
178#!/bin/busybox sh
179# Standalone AIENOS Recovery Environment
180# Runs completely in RAM, independently of internal NVMe storage
181
182/bin/busybox --install -s /bin
183
184mount -t proc proc /proc
185mount -t sysfs sysfs /sys
186mount -t devtmpfs devtmpfs /dev
187mount -t securityfs securityfs /sys/kernel/security 2>/dev/null
188# Read-only: recovery must opt in explicitly before changing firmware variables.
189mount -t efivarfs -o ro efivarfs /sys/firmware/efi/efivars 2>/dev/null
190
191secure_boot="unknown (no EFI variables)"
192sb_var=/sys/firmware/efi/efivars/SecureBoot-8be4df61-93ca-11d2-aa0d-00e098032b8c
193if [ -r "$sb_var" ]; then
194 # 4 attribute bytes, then 1 data byte. busybox od ignores -j with -N,
195 # so read the last byte with tail.
196 case "$(tail -c1 "$sb_var" | od -An -tu1 | tr -d ' ')" in
197 1) secure_boot="enabled" ;;
198 0) secure_boot="disabled" ;;
199 *) secure_boot="unreadable" ;;
200 esac
201fi
202lockdown=$(cat /sys/kernel/security/lockdown 2>/dev/null || echo "unavailable")
203
204loaded=0
205failed=""
206while read -r mod; do
207 if insmod "/lib/modules/aienos/$mod" 2>/dev/null; then
208 loaded=$((loaded + 1))
209 else
210 failed="$failed $mod"
211 fi
212done < /etc/aienos-modules.order
213total=$(wc -l < /etc/aienos-modules.order)
214# Give USB and NVMe time to enumerate before listing disks.
215sleep 3
216
217{
218 echo "============================================================"
219 echo "AIENOS Standalone Hardware Recovery Core"
220 echo "Root filesystem: 100% RAM disk (Self-contained)"
221 echo "Internal NVMe status: UNMOUNTED"
222 echo "============================================================"
223 echo ""
224 echo "Secure Boot: $secure_boot"
225 echo "Kernel lockdown: $lockdown"
226 echo "Kernel modules loaded: $loaded/$total"
227 [ -n "$failed" ] && echo "Kernel modules failed:$failed"
228 echo "Input devices: $(ls /sys/class/input 2>/dev/null | grep -c '^input')"
229 cat /sys/class/input/input*/name 2>/dev/null | sed 's/^/ /'
230 echo "Kernel: $(uname -r) Clock (UTC): $(date -u +%Y-%m-%dT%H:%M:%SZ)"
231 for pcr in 0 7; do
232 echo "TPM PCR $pcr (sha256): $(cat /sys/class/tpm/tpm0/pcr-sha256/$pcr 2>/dev/null || echo unavailable)"
233 done
234 echo ""
235 echo "Block device topology:"
236 lsblk 2>/dev/null || true
237 for part in /dev/sd*[0-9] /dev/nvme*n*p*; do
238 [ -b "$part" ] && echo " $part: $(blkid -p -o export -s LABEL -s TYPE -s UUID "$part" 2>/dev/null | tr "\n" " ")"
239 done
240 echo ""
241 echo "Firmware boot entries:"
242 efibootmgr 2>/dev/null | sed 's/^/ /' || echo " unavailable"
243 echo ""
244} > /tmp/recovery-report 2>&1
245
246# One-shot unattended self-test: Linux leaves aienos-selftest-once on the
247# stick. Only then is the stick mounted read-write, the flag removed and the
248# report saved; an ordinary recovery boot never writes anywhere.
249selftest=0
250# Probe partitions directly: without udev, blkid -L and lsblk see no labels.
251stick=""
252for part in /dev/sd*[0-9] /dev/nvme*n*p*; do
253 [ -b "$part" ] || continue
254 if [ "$(blkid -p -o value -s LABEL "$part" 2>/dev/null)" = AIENOSRECOV ]; then
255 stick="$part"
256 break
257 fi
258done
259if [ -n "$stick" ]; then
260 mkdir -p /mnt/stick
261 if mount -t vfat -o ro "$stick" /mnt/stick 2>/dev/null; then
262 if [ -f /mnt/stick/aienos-selftest-once ] && mount -o remount,rw /mnt/stick 2>/dev/null; then
263 selftest=1
264 rm -f /mnt/stick/aienos-selftest-once
265 mkdir -p /mnt/stick/aienos-evidence
266 cp /tmp/recovery-report "/mnt/stick/aienos-evidence/selftest-$(date -u +%Y%m%dT%H%M%SZ).txt"
267 sync
268 fi
269 umount /mnt/stick
270 fi
271fi
272
273clear
274cat /tmp/recovery-report
275# The shell lives on /dev/console (the screen on Machine 1). Mirror the report
276# to the serial port too, so emulator tests can read it; bounded so a serial
277# port with no carrier can never delay the shell.
278if [ -c /dev/ttyAMA0 ]; then
279 timeout 2 sh -c 'cat /tmp/recovery-report > /dev/ttyAMA0' 2>/dev/null &
280fi
281
282if grep -q "aienos.test=1" /proc/cmdline 2>/dev/null; then
283 echo "TEST MODE DETECTED: automated recovery verification complete."
284 poweroff -f 2>/dev/null || reboot -f 2>/dev/null || exit 0
285fi
286
287if [ "$selftest" = 1 ]; then
288 echo "SELF-TEST: report saved to the recovery stick (aienos-evidence/)."
289 echo "Press Enter within 60 s to stay in the recovery shell; otherwise the machine restarts."
290 if ! read -t 60 _; then
291 echo "SELF-TEST: restarting."
292 sync
293 reboot -f
294 fi
295fi
296
297echo "Dropping into standalone maintenance shell."
298echo "Type 'reboot' or 'poweroff' when recovery verification completes."
299echo ""
300
301exec /bin/sh
302INIT_EOF
303
304chmod +x "$WORK_DIR/init"
305
306echo "Packaging standalone initrd to $OUT_INITRD (not a signed boot artifact)..."
307(cd "$WORK_DIR" && find . | cpio -H newc -o | gzip -9) > "$OUT_INITRD"
308echo "Standalone initrd build complete: $(du -h "$OUT_INITRD")"