Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/scripts/collect_recovery_boot_evidence.sh # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / scripts / collect_recovery_boot_evidence.sh` 203 lines ยท 9382 bytes Raw Blame History 1 #!/bin/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 # collect_recovery_boot_evidence.sh: Record evidence that the AIENOS recovery 18 # USB actually booted Machine 1 into a rescue environment that can mount the 19 # NVMe root, repair /boot/efi, and restore boot entries (issue #17). 20 # 21 # Run this on Machine 1 (the DGX Spark), booted from the recovery USB, under: 22 # aien-proof hold --resource machine-1 --job recovery-boot-evidence -- \ 23 # bash scripts/collect_recovery_boot_evidence.sh /mnt/root 24 # 25 # Optional first argument: where the NVMe root is already mounted (default 26 # /mnt/root). This script is READ-ONLY: it never formats, never deletes, 27 # never writes to the ESP or the NVMe root, and never changes BootOrder. It 28 # only inspects mount state, filesystem presence, and firmware variables 29 # that are already exposed read-only by the kernel. 30 # Gate 1 items 11-12 (recovery unlock) run only when the operator sets 31 # AIENOS_UNLOCK_CIPHER, AIENOS_UNLOCK_SPARE and AIENOS_UNLOCK_IDENTITY for 32 # that run; the store is then opened read-only and the passfile lives only 33 # in RAM (/tmp) and is deleted straight after the mount. 34 set -uo pipefail 35 36 ROOT_MNT="${1:-/mnt/root}" 37 ESP_MNT="${AIENOS_ESP_MNT:-${ROOT_MNT}/boot/efi}" 38 FAILED=0 39 40 check() { # name, pass(0/1), detail 41 if [[ "$2" == 1 ]]; then 42 echo "PASS $1: $3" 43 else 44 echo "FAIL $1: $3" 45 FAILED=1 46 fi 47 } 48 49 echo "== recovery environment identity" 50 echo "captured_utc: $(date -u +%Y-%m-%dT%H:%M:%SZ 2>/dev/null || echo unknown)" 51 echo "kernel: $(uname -r 2>/dev/null || echo unknown)" 52 echo "cmdline: $(cat /proc/cmdline 2>/dev/null || echo unknown)" 53 echo "hostname: $(cat /proc/sys/kernel/hostname 2>/dev/null || echo unknown)" 54 55 echo "== media identity (this boot's root)" 56 root_src="$(findmnt -no SOURCE / 2>/dev/null || echo unknown)" 57 echo "root_source: ${root_src}" 58 check "booted_from_removable_media" \ 59 "$(findmnt -no SOURCE / 2>/dev/null | grep -qE 'sd|usb|mapper|/dev/ram|rootfs|tmpfs' && echo 1 || echo 0)" \ 60 "root is ${root_src} (not the internal NVMe)" 61 62 echo "== secure boot state" 63 sb="unknown" 64 sb_var="/sys/firmware/efi/efivars/SecureBoot-`8be4df61`-93ca-11d2-aa0d-`00e098032b8c`" 65 if [[ -r "${sb_var}" ]]; then 66 sb="$(od -An -t u1 -j 4 -N 1 "${sb_var}" 2>/dev/null | tr -d ' ')" 67 fi 68 echo "secure_boot_byte: ${sb}" 69 check "secure_boot_state_recorded" "$([[ "${sb}" == "0" || "${sb}" == "1" ]] && echo 1 || echo 0)" \ 70 "byte=${sb} (1=enabled)" 71 72 echo "== NVMe root mount" 73 check "nvme_root_mounted" "$(findmnt "${ROOT_MNT}" >/dev/null 2>&1 && echo 1 || echo 0)" \ 74 "findmnt ${ROOT_MNT}" 75 root_dev="$(findmnt -no SOURCE "${ROOT_MNT}" 2>/dev/null || echo unknown)" 76 echo "nvme_root_device: ${root_dev}" 77 check "nvme_root_is_internal" \ 78 "$(echo "${root_dev}" | grep -qE 'nvme|mapper' && echo 1 || echo 0)" \ 79 "source ${root_dev}" 80 root_opts="$(findmnt -no OPTIONS "${ROOT_MNT}" 2>/dev/null || echo unknown)" 81 echo "nvme_root_mount_options: ${root_opts}" 82 check "nvme_root_mounted_readonly" \ 83 "$(case ",${root_opts}," in *,ro,*) echo 1 ;; *) echo 0 ;; esac)" \ 84 "mount options: ${root_opts}" 85 86 echo "== /boot/efi access (read-only inspection; nothing is written here)" 87 esp_ok=0 88 if findmnt "${ESP_MNT}" >/dev/null 2>&1 || mountpoint -q "${ESP_MNT}" 2>/dev/null; then 89 esp_ok=1 90 fi 91 check "esp_mounted" "${esp_ok}" "findmnt ${ESP_MNT}" 92 93 esp_fallback_present=0 94 if [[ "${esp_ok}" == 1 && -f "${ESP_MNT}/EFI/BOOT/BOOTAA64.EFI" ]]; then 95 esp_fallback_present=1 96 fi 97 check "esp_fallback_bootloader_present" "${esp_fallback_present}" \ 98 "${ESP_MNT}/EFI/BOOT/BOOTAA64.EFI" 99 100 esp_opts="$(findmnt -no OPTIONS "${ESP_MNT}" 2>/dev/null || echo unknown)" 101 echo "esp_mount_options: ${esp_opts}" 102 check "esp_mounted_readonly" \ 103 "$(case ",${esp_opts}," in *,ro,*) echo 1 ;; *) echo 0 ;; esac)" \ 104 "mount options: ${esp_opts}" 105 106 echo "== boot entry restore capability" 107 efi_vars=0 108 [[ -d /sys/firmware/efi/efivars ]] && efi_vars=1 109 check "efi_variables_present" "${efi_vars}" "/sys/firmware/efi/efivars" 110 entries="$(efibootmgr 2>/dev/null || true)" 111 echo "${entries}" | sed -n '1,6p' 112 check "efibootmgr_readable" "$(grep -q '^Boot' <<<"${entries}" && echo 1 || echo 0)" \ 113 "efibootmgr lists boot entries" 114 115 echo "== TRUST-1 Gate 1 item 6: disks and encrypted stores (read-only)" 116 lsblk -o NAME,SIZE,TYPE,FSTYPE,LABEL 2>/dev/null || echo "lsblk unavailable" 117 # The Spark's encrypted stores are gocryptfs folders on the NVMe root. 118 # Default: the two known store locations, matched for any home folder on the root (AIENOS_CIPHER_DIRS overrides). 119 if [[ -n "${AIENOS_CIPHER_DIRS:-}" ]]; then 120 CIPHER_DIRS="${AIENOS_CIPHER_DIRS}" 121 else 122 CIPHER_DIRS="" 123 for d in "${ROOT_MNT}"/home/*/atlas-runtime-setup-*/private-cipher "${ROOT_MNT}"/home/*/workspace/atlas-forgejo/cipher; do 124 [[ -d "${d}" ]] && CIPHER_DIRS="${CIPHER_DIRS:+${CIPHER_DIRS} }${d#"${ROOT_MNT}"}" 125 done 126 [[ -n "${CIPHER_DIRS}" ]] || echo "gocryptfs_store: no store at the default locations under ${ROOT_MNT}" 127 fi 128 stores_seen=0 129 for c in ${CIPHER_DIRS}; do 130 conf="${ROOT_MNT}${c}/gocryptfs.conf" 131 if [[ -f "${conf}" ]]; then 132 echo "gocryptfs_store: ${c} conf_sha256=$(sha256sum "${conf}" | cut -d' ' -f1)" 133 stores_seen=$((stores_seen + 1)) 134 else 135 echo "gocryptfs_store: ${c} NOT FOUND under ${ROOT_MNT}" 136 fi 137 done 138 check "encrypted_stores_identified" "$([[ ${stores_seen} -gt 0 ]] && echo 1 || echo 0)" \ 139 "${stores_seen} gocryptfs store(s) found on the NVMe root" 140 141 echo "== TRUST-1 Gate 1 item 8: PCRs and event log (read-only)" 142 export TPM2TOOLS_TCTI="${TPM2TOOLS_TCTI:-device:/dev/tpmrm0}" 143 # FLAG(sovereignty): tpm2_pcrread run inside the recovery image; replace with in-house C PCR reader. 144 pcrs="$(tpm2_pcrread sha256 2>/dev/null || true)" 145 echo "${pcrs}" | sed -n '1,26p' 146 check "pcrs_read" "$(grep -qE '^ +7 *:' <<<"${pcrs}" && echo 1 || echo 0)" "tpm2_pcrread sha256" 147 evlog=/sys/kernel/security/tpm0/binary_bios_measurements 148 if [[ -r "${evlog}" ]]; then 149 echo "event_log_sha256: $(sha256sum "${evlog}" | cut -d' ' -f1)" 150 echo "event_log_bytes: $(wc -c <"${evlog}")" 151 fi 152 check "event_log_recorded" "$([[ -r "${evlog}" ]] && echo 1 || echo 0)" "${evlog}" 153 154 echo "== TRUST-1 Gate 1 items 9-10: AIENOS slots, loader hashes and signatures (read-only)" 155 if [[ "${esp_ok}" == 1 ]]; then 156 ls -la "${ESP_MNT}/EFI" 2>/dev/null 157 ls -laR "${ESP_MNT}"/EFI/AIENOS* 2>/dev/null || echo "aienos_slots: none on this ESP yet (expected before Gate 6)" 158 loaders=0 159 while IFS= read -r -d '' efi; do 160 echo "efi_image: ${efi#"${ESP_MNT}"} sha256=$(sha256sum "${efi}" | cut -d' ' -f1)" 161 sbverify --list "${efi}" 2>/dev/null | sed -n 's/^ *\(subject\|issuer\):/ \1:/p' | head -n 4 162 loaders=$((loaders + 1)) 163 done < <(find "${ESP_MNT}" -type f -iname '*.efi' -print0 2>/dev/null) 164 check "esp_loaders_hashed" "$([[ ${loaders} -gt 0 ]] && echo 1 || echo 0)" "${loaders} EFI image(s) hashed" 165 else 166 check "esp_loaders_hashed" 0 "ESP not mounted" 167 fi 168 169 echo "== TRUST-1 Gate 1 items 11-12: recovery unlock (only when explicitly authorized)" 170 # Runs ONLY when the operator sets all three variables for this run. Opens 171 # ONE store read-only with the offline spare: the passfile is decrypted into 172 # RAM (/tmp), used once and deleted; nothing is written to the NVMe. 173 if [[ -n "${AIENOS_UNLOCK_CIPHER:-}" && -n "${AIENOS_UNLOCK_SPARE:-}" && -n "${AIENOS_UNLOCK_IDENTITY:-}" ]]; then 174 plain="${AIENOS_UNLOCK_MNT:-/mnt/plain}" 175 mkdir -p "${plain}" 176 unlock_ok=0 177 if age -d -i "${AIENOS_UNLOCK_IDENTITY}" -o /tmp/aienos-unlock-pass "${AIENOS_UNLOCK_SPARE}" \ 178 && gocryptfs -q -nosyslog -ro -passfile /tmp/aienos-unlock-pass "${AIENOS_UNLOCK_CIPHER}" "${plain}"; then 179 unlock_ok=1 180 fi 181 rm -f /tmp/aienos-unlock-pass 182 check "recovery_unlock_readonly" "${unlock_ok}" "store ${AIENOS_UNLOCK_CIPHER} opened read-only with the offline spare" 183 if [[ "${unlock_ok}" == 1 && -n "${AIENOS_UNLOCK_EXPECT:-}" ]]; then 184 # AIENOS_UNLOCK_EXPECT="relative/path sha256" of the harmless test file. 185 exp_path="${AIENOS_UNLOCK_EXPECT% *}" 186 exp_sha="${AIENOS_UNLOCK_EXPECT##* }" 187 got_sha="$(sha256sum "${plain}/${exp_path}" 2>/dev/null | cut -d' ' -f1)" 188 echo "test_artifact: ${exp_path} sha256=${got_sha:-unreadable}" 189 check "test_artifact_round_trip" "$([[ "${got_sha}" == "${exp_sha}" ]] && echo 1 || echo 0)" \ 190 "expected ${exp_sha}" 191 fi 192 [[ "${unlock_ok}" == 1 ]] && { umount "${plain}" 2>/dev/null || fusermount3 -u "${plain}"; } 193 else 194 echo "recovery_unlock: not authorized for this run (set AIENOS_UNLOCK_CIPHER, AIENOS_UNLOCK_SPARE, AIENOS_UNLOCK_IDENTITY)" 195 fi 196 197 echo "== verdict" 198 if [[ "${FAILED}" == 0 ]]; then 199 echo "RECOVERY_BOOT_GATE: PASS" 200 else 201 echo "RECOVERY_BOOT_GATE: FAIL" 202 exit 1 203 fi