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 / qemu_verify_recovery_media.sh

173 lines · 7067 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# qemu_verify_recovery_media.sh: Verify standalone recovery environment in QEMU AArch64
18# Invariant: BOOTS WITH ZERO HARD DISKS ATTACHED. Internal NVMe must not be present.
19# Proves 100% RAM disk self-containment under rdinit=/init.
20# Zero Disk Secrets and Unslop compliant.
21
22set -euo pipefail
23
24REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
25cd "${REPO_ROOT}"
26
27command -v qemu-system-aarch64 >/dev/null || { echo "qemu-system-aarch64 not installed"; exit 2; }
28
29if [[ "$(uname -m)" != "aarch64" ]]; then
30 echo "SKIPPED: Host architecture is $(uname -m). AArch64 kernel recovery boot requires an AArch64 host or cross-built AArch64 kernel."
31 [[ "${AIENOS_STRICT:-0}" != "1" ]] || { echo "STRICT FAIL: recovery boot skipped under AIENOS_STRICT=1" >&2; exit 1; }
32 exit 0
33fi
34
35WORK_DIR=$(mktemp -d)
36trap 'rm -rf "${WORK_DIR}"' EXIT
37LOG="${WORK_DIR}/recovery_qemu.log"
38
39PROD_VMLINUZ=$(sudo -n ls -1t /boot/vmlinuz-* 2>/dev/null | head -n 1 || ls -1t /boot/vmlinuz-* 2>/dev/null | head -n 1 || true)
40if [[ -z "${PROD_VMLINUZ}" ]]; then
41 echo "Error: Signed production kernel not found in /boot" >&2
42 exit 1
43fi
44
45KERNEL_IMAGE="${WORK_DIR}/vmlinuz"
46# /boot/vmlinuz-* is root 0600 on Ubuntu. The Spark judge runs gates as aien-run with no_new_privs, so sudo
47# cannot copy it there; the operator keeps a read-only copy of the same kernel at $HOME/boot/vmlinuz (root 0644,
48# HOME=/var/lib/aien-run), the way it provides $HOME/models. Use that copy only when the kernel cannot be read.
49JUDGE_VMLINUZ="${HOME:-/nonexistent}/boot/vmlinuz"
50if [[ -r "${PROD_VMLINUZ}" ]]; then
51 cp "${PROD_VMLINUZ}" "${KERNEL_IMAGE}"
52elif sudo -n cp "${PROD_VMLINUZ}" "${KERNEL_IMAGE}" 2>/dev/null; then
53 sudo -n chown "$(id -u):$(id -g)" "${KERNEL_IMAGE}"
54elif [[ -r "${JUDGE_VMLINUZ}" ]]; then
55 echo "NOTE ${PROD_VMLINUZ} is not readable and sudo is unavailable; using the read-only copy ${JUDGE_VMLINUZ}"
56 cp "${JUDGE_VMLINUZ}" "${KERNEL_IMAGE}"
57else
58 echo "Error: cannot read ${PROD_VMLINUZ} (no sudo, no ${JUDGE_VMLINUZ})" >&2
59 exit 1
60fi
61
62INITRD_TMP="${WORK_DIR}/aienos-recovery-standalone-initrd.img"
63# Repo checks run on hosts without gocryptfs (CI, laptops). Only there, and
64# visibly, allow a test image without it. The real stick build
65# (build_recovery_media.sh) never sets this.
66if ! command -v gocryptfs >/dev/null 2>&1 && [[ ! -f /home/atlas/atlas-forgejo-setup-20260904/runtime/usr/bin/gocryptfs && -z "${AIENOS_GOCRYPTFS:-}" ]]; then
67 echo "SKIP gocryptfs not on this host; building a TEST-ONLY image without it"
68 export AIENOS_RECOVERY_ALLOW_NO_GOCRYPTFS=1
69fi
70
71echo "Building standalone recovery initrd..."
72bash "${REPO_ROOT}/scripts/build_standalone_recovery_initrd.sh" "${INITRD_TMP}"
73
74echo "Booting standalone recovery kernel in QEMU with ZERO hard disks attached..."
75started=$(date +%s)
76set +e
77timeout 30 qemu-system-aarch64 \
78 -M virt -cpu max -smp 2 -m 1024 \
79 -kernel "${KERNEL_IMAGE}" \
80 -initrd "${INITRD_TMP}" \
81 -append "rdinit=/init aienos.test=1 console=ttyAMA0 panic=1 quiet" \
82 -display none -nic none \
83 -serial file:"${LOG}" -no-reboot
84QEMU_STATUS=$?
85set -e
86elapsed=$(( $(date +%s) - started ))
87
88tr -d '\r' <"${LOG}" >"${WORK_DIR}/recovery_qemu.txt"
89[[ -z "${AIENOS_LOG_DIR:-}" ]] || cp "${WORK_DIR}/recovery_qemu.txt" "${AIENOS_LOG_DIR}/recovery_zero_disk_serial.log"
90
91failed=0
92check() {
93 if grep -q -- "$2" "${WORK_DIR}/recovery_qemu.txt"; then
94 echo "PASS $1"
95 else
96 echo "FAIL $1"
97 failed=1
98 fi
99}
100
101echo "qemu exit ${QEMU_STATUS} after ${elapsed} s"
102check "Recovery banner displayed" "AIENOS Standalone Hardware Recovery Core"
103check "100% RAM disk verified" "Root filesystem: 100% RAM disk"
104check "Internal NVMe confirmed unmounted" "Internal NVMe status: UNMOUNTED"
105check "Automated recovery test passed" "TEST MODE DETECTED: automated recovery verification complete"
106
107if [[ "${failed}" != 0 ]]; then
108 echo "---- recovery console log ----"
109 cat "${WORK_DIR}/recovery_qemu.txt"
110 echo "RECOVERY_ZERO_DISK: FAIL"
111 exit 1
112fi
113
114echo "RECOVERY_ZERO_DISK: PASS (verified standalone RAM recovery with zero attached storage)"
115
116# Second boot: the packaged drivers must find a USB stick, an NVMe drive and a
117# USB keyboard. The disks are blank scratch images, never host storage.
118DEV_LOG="${WORK_DIR}/recovery_devices.log"
119truncate -s 64M "${WORK_DIR}/usb.img" "${WORK_DIR}/nvme.img"
120ACCEL=(-cpu max)
121DEV_TIMEOUT=120
122if [[ -w /dev/kvm ]]; then
123 ACCEL=(-accel kvm -cpu host)
124 DEV_TIMEOUT=60
125fi
126
127echo "Booting recovery kernel with a scratch USB stick, NVMe drive and USB keyboard..."
128set +e
129timeout "${DEV_TIMEOUT}" qemu-system-aarch64 \
130 -M virt "${ACCEL[@]}" -smp 2 -m 1024 \
131 -kernel "${KERNEL_IMAGE}" \
132 -initrd "${INITRD_TMP}" \
133 -append "rdinit=/init aienos.test=1 console=ttyAMA0 panic=1" \
134 -device qemu-xhci -device usb-kbd \
135 -drive if=none,id=usbstick,format=raw,file="${WORK_DIR}/usb.img" -device usb-storage,drive=usbstick \
136 -drive if=none,id=nvme0,format=raw,file="${WORK_DIR}/nvme.img" -device nvme,drive=nvme0,serial=aienos-test \
137 -display none -nic none \
138 -serial file:"${DEV_LOG}" -no-reboot
139DEV_STATUS=$?
140set -e
141
142tr -d '\r' <"${DEV_LOG}" >"${WORK_DIR}/recovery_devices.txt"
143[[ -z "${AIENOS_LOG_DIR:-}" ]] || cp "${WORK_DIR}/recovery_devices.txt" "${AIENOS_LOG_DIR}/recovery_devices_serial.log"
144echo "qemu exit ${DEV_STATUS}"
145
146failed=0
147check_devices() {
148 if grep -qE -- "$2" "${WORK_DIR}/recovery_devices.txt"; then
149 echo "PASS $1"
150 else
151 echo "FAIL $1"
152 failed=1
153 fi
154}
155modules_line=$(grep -m1 'Kernel modules loaded:' "${WORK_DIR}/recovery_devices.txt" || true)
156if [[ "${modules_line}" =~ loaded:\ ([0-9]+)/([0-9]+) ]] && [[ "${BASH_REMATCH[1]}" == "${BASH_REMATCH[2]}" ]] && [[ "${BASH_REMATCH[2]}" -gt 0 ]]; then
157 echo "PASS All packaged kernel modules loaded (${BASH_REMATCH[1]}/${BASH_REMATCH[2]})"
158else
159 echo "FAIL All packaged kernel modules loaded (${modules_line:-no report})"
160 failed=1
161fi
162check_devices "USB keyboard detected" "Input devices: [1-9]"
163check_devices "USB stick visible" "^sda"
164check_devices "NVMe drive visible" "^nvme0n1"
165
166if [[ "${failed}" != 0 ]]; then
167 echo "---- recovery device console log ----"
168 cat "${WORK_DIR}/recovery_devices.txt"
169 echo "RECOVERY_DEVICES: FAIL"
170 exit 1
171fi
172
173echo "RECOVERY_DEVICES: PASS (packaged drivers found USB keyboard, USB stick and NVMe)"