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_recovery_media.sh

215 lines · 9343 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_recovery_media.sh: Build a signed, self-contained UEFI recovery image
18# on a dedicated FAT USB partition. No separately loaded kernel, menu or
19# initrd may be present on the writable medium.
20# Guard: Multi-factor validation rejects ATLAS_RECOV and the internal NVMe.
21# Invariant: NO PLAINTEXT SECRETS IN REPOSITORY OR BUILD ARTIFACTS.
22
23set -euo pipefail
24
25umask 077
26
27require_vfat() {
28 local target_type
29 target_type=$(blkid -p -o value -s TYPE "$1" 2>/dev/null || true)
30 if [[ "${target_type}" != vfat ]]; then
31 echo "FATAL: Recovery target must be an existing FAT (vfat) partition; found ${target_type:-unknown}." >&2
32 return 1
33 fi
34}
35
36canonical_target() {
37 local resolved
38 resolved=$(readlink -e -- "$1") || {
39 echo "FATAL: Recovery target path does not resolve: $1" >&2
40 return 1
41 }
42 if [[ "${resolved##*/}" == nvme* ]]; then
43 echo "FATAL: Recovery target resolves to internal NVMe: ${resolved}" >&2
44 return 1
45 fi
46 printf '%s\n' "${resolved}"
47}
48
49# Read-only probes for offline tests. Neither validates a block device nor
50# authorizes an image build.
51if [[ "${1:-}" == --check-fstype ]]; then
52 [[ $# == 2 ]] || { echo "Usage: $0 --check-fstype <scratch-image>" >&2; exit 1; }
53 require_vfat "$2"
54 echo "TYPE_ONLY: vfat (no device authorization or media build)"
55 exit 0
56elif [[ "${1:-}" == --check-target-path ]]; then
57 [[ $# == 2 ]] || { echo "Usage: $0 --check-target-path <scratch-path>" >&2; exit 1; }
58 canonical_target "$2"
59 echo "PATH_ONLY: no device authorization or media build"
60 exit 0
61fi
62
63TARGET_DEV="${1:-}"
64
65if [[ -z "${TARGET_DEV}" ]]; then
66 echo "Usage: $0 <target-partition> (e.g. /dev/sdb1)" >&2
67 echo "Error: Target block device must be explicitly specified." >&2
68 exit 1
69fi
70
71# Resolve aliases before the block check, all identity probes and the mount.
72# A /dev/disk/by-id/ alias must not hide an internal /dev/nvme* partition.
73TARGET_DEV="$(canonical_target "${TARGET_DEV}")"
74
75if [[ ! -b "${TARGET_DEV}" ]]; then
76 echo "Error: Target ${TARGET_DEV} is not a valid block device." >&2
77 exit 1
78fi
79
80# Multi-factor identity inspection
81TARGET_LABEL=$(lsblk -no LABEL "${TARGET_DEV}" 2>/dev/null || blkid -s LABEL -o value "${TARGET_DEV}" 2>/dev/null || true)
82TARGET_UUID=$(lsblk -no UUID "${TARGET_DEV}" 2>/dev/null || blkid -s UUID -o value "${TARGET_DEV}" 2>/dev/null || true)
83TARGET_PARTUUID=$(lsblk -no PARTUUID "${TARGET_DEV}" 2>/dev/null || blkid -s PARTUUID -o value "${TARGET_DEV}" 2>/dev/null || true)
84TARGET_MOUNTS=$(lsblk -no MOUNTPOINTS "${TARGET_DEV}" 2>/dev/null || true)
85
86# 1. Reject ATLAS_RECOV by label, filesystem UUID, and partition UUID
87KNOWN_ATLAS_RECOV_UUID="669D-4D0E"
88KNOWN_ATLAS_RECOV_PARTUUID="335d7260-01"
89
90if [[ "${TARGET_LABEL}" == "ATLAS_RECOV" || "${TARGET_UUID}" == "${KNOWN_ATLAS_RECOV_UUID}" || "${TARGET_PARTUUID}" == "${KNOWN_ATLAS_RECOV_PARTUUID}" ]]; then
91 echo "FATAL: Target matches ATLAS_RECOV identity (Label: , UUID: , PARTUUID: )." >&2
92 echo "ATLAS_RECOV contains encrypted offline recovery keys and must NEVER be overwritten." >&2
93 exit 1
94fi
95
96# 2. Reject internal NVMe storage and system partitions
97KNOWN_HOST_ROOT_UUID="d27bfd26-ff30-400e-9eca-9cdf73de9406"
98KNOWN_HOST_EFI_UUID="9DA2-3597"
99
100if [[ "${TARGET_UUID}" == "${KNOWN_HOST_ROOT_UUID}" || "${TARGET_UUID}" == "${KNOWN_HOST_EFI_UUID}" ]]; then
101 echo "FATAL: Target ${TARGET_DEV} is an internal host system disk or EFI system partition." >&2
102 exit 1
103fi
104
105# 3. Reject active host mounts
106if [[ -n "${TARGET_MOUNTS}" ]]; then
107 echo "FATAL: Target ${TARGET_DEV} has active mountpoints: ${TARGET_MOUNTS}. Unmount before provisioning." >&2
108 exit 1
109fi
110
111# 4. If target is a disk containing partitions, ensure none of its child partitions match ATLAS_RECOV
112CHILD_IDS=$(lsblk -no LABEL,UUID,PARTUUID "${TARGET_DEV}" 2>/dev/null || true)
113if echo "${CHILD_IDS}" | grep -qE "ATLAS_RECOV|${KNOWN_ATLAS_RECOV_UUID}|${KNOWN_ATLAS_RECOV_PARTUUID}"; then
114 echo "FATAL: Device ${TARGET_DEV} contains partition matching ATLAS_RECOV identity." >&2
115 exit 1
116fi
117
118# FAT cannot store symlinks. Reject a Unix filesystem before any privileged
119# mount or write, and check the actual mounted type after mounting.
120require_vfat "${TARGET_DEV}"
121if [[ "${AIENOS_RECOVERY_ALLOW_NO_GOCRYPTFS:-0}" == 1 ]]; then
122 echo "FATAL: Test-only no-gocryptfs image is not eligible for recovery media." >&2
123 exit 1
124fi
125
126for tool in grub-mkstandalone sbsign sbverify findmnt; do
127 command -v "${tool}" >/dev/null || { echo "Error: required tool ${tool} not installed." >&2; exit 1; }
128done
129SIGNING_KEY="${AIENOS_RECOVERY_SIGNING_KEY:-}"
130SIGNING_CERT="${AIENOS_RECOVERY_SIGNING_CERT:-}"
131if [[ -z "${SIGNING_KEY}" || -z "${SIGNING_CERT}" || ! -r "${SIGNING_KEY}" || ! -r "${SIGNING_CERT}" ]]; then
132 echo "FATAL: Supply readable AIENOS_RECOVERY_SIGNING_KEY and AIENOS_RECOVERY_SIGNING_CERT (owner-enrolled shim trust). No unsigned recovery image is installed." >&2
133 exit 1
134fi
135
136WORK_DIR="$(mktemp -d /tmp/aienos-recovery-build.XXXXXX)"
137MOUNT_POINT=""
138mounted=0
139cleanup() {
140 if [[ "${mounted}" == 1 ]]; then
141 umount "${MOUNT_POINT}" || echo "WARNING: Could not unmount ${MOUNT_POINT}; inspect before removing media." >&2
142 fi
143 if [[ -n "${MOUNT_POINT}" ]]; then
144 rmdir "${MOUNT_POINT}" 2>/dev/null || true
145 fi
146 rm -rf "${WORK_DIR}"
147}
148trap cleanup EXIT
149
150# The signed PE contains GRUB's own menu, the production kernel and the
151# complete initrd. GRUB reads only its embedded memdisk, never USB paths.
152PROD_VMLINUZ="${AIENOS_RECOVERY_KERNEL:-$(ls -1t /boot/vmlinuz-* | head -n 1)}"
153SHIM_EFI="${AIENOS_RECOVERY_SHIM:-/boot/efi/EFI/boot/BOOTAA64.EFI}"
154MOK_EFI="${AIENOS_RECOVERY_MOKMANAGER:-/boot/efi/EFI/ubuntu/mmaa64.efi}"
155if [[ ! -r "${PROD_VMLINUZ}" || ! -r "${SHIM_EFI}" || "${PROD_VMLINUZ##*/}" != vmlinuz-* ]]; then
156 echo "FATAL: Require a readable vmlinuz-* kernel and signed shim input." >&2
157 exit 1
158fi
159if [[ -n "${AIENOS_RECOVERY_MOKMANAGER:-}" && ! -r "${MOK_EFI}" ]]; then
160 echo "FATAL: Explicit MOK manager input is unreadable." >&2
161 exit 1
162fi
163echo "Building signed recovery image with kernel ${PROD_VMLINUZ}..."
164AIENOS_RECOVERY_KVER="${PROD_VMLINUZ##*/vmlinuz-}" \
165 bash "$(dirname "$0")/build_standalone_recovery_initrd.sh" "${WORK_DIR}/initrd.img"
166cat > "${WORK_DIR}/grub.cfg" <<'GRUB_EOF'
167set timeout=5
168set default=0
169menuentry "AIENOS Standalone Recovery System (RAM Maintenance Core)" {
170 linux (memdisk)/boot/vmlinuz rdinit=/init console=ttyAMA0,115200n8 console=tty0 quiet
171 initrd (memdisk)/boot/initrd.img
172}
173menuentry "Reboot to Incumbent Linux" {
174 exit
175}
176GRUB_EOF
177cat > "${WORK_DIR}/sbat.csv" <<'SBAT_EOF'
178sbat,1,SBAT Version,sbat,1,https://github.com/rhboot/shim/blob/main/SBAT.md
179grub,2,AIENOS Recovery GRUB,aien-dev,1,https://github.com/aien-dev/aienux
180SBAT_EOF
181grub-mkstandalone -O arm64-efi --fonts='' --locales='' --themes='' \
182 --modules='linux normal' --compress=no --sbat="${WORK_DIR}/sbat.csv" \
183 -o "${WORK_DIR}/recovery-unsigned.efi" \
184 "/boot/grub/grub.cfg=${WORK_DIR}/grub.cfg" \
185 "/boot/vmlinuz=${PROD_VMLINUZ}" \
186 "/boot/initrd.img=${WORK_DIR}/initrd.img"
187sbsign --key "${SIGNING_KEY}" --cert "${SIGNING_CERT}" \
188 --output "${WORK_DIR}/recovery.efi" "${WORK_DIR}/recovery-unsigned.efi"
189sbverify --cert "${SIGNING_CERT}" "${WORK_DIR}/recovery.efi" >/dev/null
190
191MOUNT_POINT="$(mktemp -d /mnt/aienos-recovery.XXXXXX)"
192mount -t vfat -o nodev,nosuid,noexec "${TARGET_DEV}" "${MOUNT_POINT}"
193mounted=1
194if [[ "$(findmnt -n -o FSTYPE --target "${MOUNT_POINT}")" != vfat ]]; then
195 echo "FATAL: Mounted recovery target is not FAT; refusing to write." >&2
196 exit 1
197fi
198
199echo "Populating signed EFI boot structure on ${TARGET_DEV}..."
200mkdir -p "${MOUNT_POINT}/EFI/BOOT" "${MOUNT_POINT}/EFI/ubuntu"
201# Remove unsigned leftovers from older recovery builds. The standalone EFI
202# never reads these; leaving them risks confusing an operator or a future
203# fallback loader into treating unauthenticated USB bytes as recovery code.
204rm -f "${MOUNT_POINT}/EFI/BOOT/grub.cfg" "${MOUNT_POINT}/EFI/ubuntu/grub.cfg" \
205 "${MOUNT_POINT}/EFI/BOOT/mmaa64.efi" \
206 "${MOUNT_POINT}/aienos-recovery/vmlinuz" "${MOUNT_POINT}/aienos-recovery/initrd.img"
207cp "${SHIM_EFI}" "${MOUNT_POINT}/EFI/BOOT/BOOTAA64.EFI"
208if [[ -r "${MOK_EFI}" ]]; then
209 cp "${MOK_EFI}" "${MOUNT_POINT}/EFI/BOOT/mmaa64.efi"
210fi
211# Both possible Ubuntu shim lookup paths receive the same owner-signed PE.
212cp "${WORK_DIR}/recovery.efi" "${MOUNT_POINT}/EFI/BOOT/grubaa64.efi"
213cp "${WORK_DIR}/recovery.efi" "${MOUNT_POINT}/EFI/ubuntu/grubaa64.efi"
214sync
215echo "AIENOS signed standalone recovery media build complete on ${TARGET_DEV}."