aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / scripts / build_recovery_media.sh
| 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 | |
| 23 | set -euo pipefail |
| 24 | |
| 25 | umask 077 |
| 26 | |
| 27 | require_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 | |
| 36 | canonical_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. |
| 51 | if [[ "${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 |
| 56 | elif [[ "${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 |
| 61 | fi |
| 62 | |
| 63 | TARGET_DEV="${1:-}" |
| 64 | |
| 65 | if [[ -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 |
| 69 | fi |
| 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. |
| 73 | TARGET_DEV="$(canonical_target "${TARGET_DEV}")" |
| 74 | |
| 75 | if [[ ! -b "${TARGET_DEV}" ]]; then |
| 76 | echo "Error: Target ${TARGET_DEV} is not a valid block device." >&2 |
| 77 | exit 1 |
| 78 | fi |
| 79 | |
| 80 | # Multi-factor identity inspection |
| 81 | TARGET_LABEL=$(lsblk -no LABEL "${TARGET_DEV}" 2>/dev/null || blkid -s LABEL -o value "${TARGET_DEV}" 2>/dev/null || true) |
| 82 | TARGET_UUID=$(lsblk -no UUID "${TARGET_DEV}" 2>/dev/null || blkid -s UUID -o value "${TARGET_DEV}" 2>/dev/null || true) |
| 83 | TARGET_PARTUUID=$(lsblk -no PARTUUID "${TARGET_DEV}" 2>/dev/null || blkid -s PARTUUID -o value "${TARGET_DEV}" 2>/dev/null || true) |
| 84 | TARGET_MOUNTS=$(lsblk -no MOUNTPOINTS "${TARGET_DEV}" 2>/dev/null || true) |
| 85 | |
| 86 | # 1. Reject ATLAS_RECOV by label, filesystem UUID, and partition UUID |
| 87 | KNOWN_ATLAS_RECOV_UUID="669D-4D0E" |
| 88 | KNOWN_ATLAS_RECOV_PARTUUID="335d7260-01" |
| 89 | |
| 90 | if [[ "${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 |
| 94 | fi |
| 95 | |
| 96 | # 2. Reject internal NVMe storage and system partitions |
| 97 | KNOWN_HOST_ROOT_UUID="d27bfd26-ff30-400e-9eca-9cdf73de9406" |
| 98 | KNOWN_HOST_EFI_UUID="9DA2-3597" |
| 99 | |
| 100 | if [[ "${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 |
| 103 | fi |
| 104 | |
| 105 | # 3. Reject active host mounts |
| 106 | if [[ -n "${TARGET_MOUNTS}" ]]; then |
| 107 | echo "FATAL: Target ${TARGET_DEV} has active mountpoints: ${TARGET_MOUNTS}. Unmount before provisioning." >&2 |
| 108 | exit 1 |
| 109 | fi |
| 110 | |
| 111 | # 4. If target is a disk containing partitions, ensure none of its child partitions match ATLAS_RECOV |
| 112 | CHILD_IDS=$(lsblk -no LABEL,UUID,PARTUUID "${TARGET_DEV}" 2>/dev/null || true) |
| 113 | if 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 |
| 116 | fi |
| 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. |
| 120 | require_vfat "${TARGET_DEV}" |
| 121 | if [[ "${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 |
| 124 | fi |
| 125 | |
| 126 | for tool in grub-mkstandalone sbsign sbverify findmnt; do |
| 127 | command -v "${tool}" >/dev/null || { echo "Error: required tool ${tool} not installed." >&2; exit 1; } |
| 128 | done |
| 129 | SIGNING_KEY="${AIENOS_RECOVERY_SIGNING_KEY:-}" |
| 130 | SIGNING_CERT="${AIENOS_RECOVERY_SIGNING_CERT:-}" |
| 131 | if [[ -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 |
| 134 | fi |
| 135 | |
| 136 | WORK_DIR="$(mktemp -d /tmp/aienos-recovery-build.XXXXXX)" |
| 137 | MOUNT_POINT="" |
| 138 | mounted=0 |
| 139 | cleanup() { |
| 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 | } |
| 148 | trap 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. |
| 152 | PROD_VMLINUZ="${AIENOS_RECOVERY_KERNEL:-$(ls -1t /boot/vmlinuz-* | head -n 1)}" |
| 153 | SHIM_EFI="${AIENOS_RECOVERY_SHIM:-/boot/efi/EFI/boot/BOOTAA64.EFI}" |
| 154 | MOK_EFI="${AIENOS_RECOVERY_MOKMANAGER:-/boot/efi/EFI/ubuntu/mmaa64.efi}" |
| 155 | if [[ ! -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 |
| 158 | fi |
| 159 | if [[ -n "${AIENOS_RECOVERY_MOKMANAGER:-}" && ! -r "${MOK_EFI}" ]]; then |
| 160 | echo "FATAL: Explicit MOK manager input is unreadable." >&2 |
| 161 | exit 1 |
| 162 | fi |
| 163 | echo "Building signed recovery image with kernel ${PROD_VMLINUZ}..." |
| 164 | AIENOS_RECOVERY_KVER="${PROD_VMLINUZ##*/vmlinuz-}" \ |
| 165 | bash "$(dirname "$0")/build_standalone_recovery_initrd.sh" "${WORK_DIR}/initrd.img" |
| 166 | cat > "${WORK_DIR}/grub.cfg" <<'GRUB_EOF' |
| 167 | set timeout=5 |
| 168 | set default=0 |
| 169 | menuentry "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 | } |
| 173 | menuentry "Reboot to Incumbent Linux" { |
| 174 | exit |
| 175 | } |
| 176 | GRUB_EOF |
| 177 | cat > "${WORK_DIR}/sbat.csv" <<'SBAT_EOF' |
| 178 | sbat,1,SBAT Version,sbat,1,https://github.com/rhboot/shim/blob/main/SBAT.md |
| 179 | grub,2,AIENOS Recovery GRUB,aien-dev,1,https://github.com/aien-dev/aienux |
| 180 | SBAT_EOF |
| 181 | grub-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" |
| 187 | sbsign --key "${SIGNING_KEY}" --cert "${SIGNING_CERT}" \ |
| 188 | --output "${WORK_DIR}/recovery.efi" "${WORK_DIR}/recovery-unsigned.efi" |
| 189 | sbverify --cert "${SIGNING_CERT}" "${WORK_DIR}/recovery.efi" >/dev/null |
| 190 | |
| 191 | MOUNT_POINT="$(mktemp -d /mnt/aienos-recovery.XXXXXX)" |
| 192 | mount -t vfat -o nodev,nosuid,noexec "${TARGET_DEV}" "${MOUNT_POINT}" |
| 193 | mounted=1 |
| 194 | if [[ "$(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 |
| 197 | fi |
| 198 | |
| 199 | echo "Populating signed EFI boot structure on ${TARGET_DEV}..." |
| 200 | mkdir -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. |
| 204 | rm -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" |
| 207 | cp "${SHIM_EFI}" "${MOUNT_POINT}/EFI/BOOT/BOOTAA64.EFI" |
| 208 | if [[ -r "${MOK_EFI}" ]]; then |
| 209 | cp "${MOK_EFI}" "${MOUNT_POINT}/EFI/BOOT/mmaa64.efi" |
| 210 | fi |
| 211 | # Both possible Ubuntu shim lookup paths receive the same owner-signed PE. |
| 212 | cp "${WORK_DIR}/recovery.efi" "${MOUNT_POINT}/EFI/BOOT/grubaa64.efi" |
| 213 | cp "${WORK_DIR}/recovery.efi" "${MOUNT_POINT}/EFI/ubuntu/grubaa64.efi" |
| 214 | sync |
| 215 | echo "AIENOS signed standalone recovery media build complete on ${TARGET_DEV}." |