The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.
TypeScript 3d-modelingagent-frameworkagent-harnessai-agentscadclaude-codecodexcoding-agentsdeveloper-toolsesp32game-developmentmcp-servermulti-agentopen-hardwareopencodeopenharnesspcbroboticsterminaltmux
os: boot private Apple Silicon installation media (#954)
Dee6e92c2c278b4
diff --git a/.github/workflows/os-asahi-media.yml b/.github/workflows/os-asahi-media.ymlnew file mode 100644index 000000000..d1c53d300--- /dev/null+++ b/.github/workflows/os-asahi-media.yml@@ -0,0 +1,144 @@+name: Harness OS private Apple Silicon installer media++on:+ workflow_dispatch:+ inputs:+ image_run_id:+ description: Successful private Asahi image workflow run+ type: string+ required: true+ image_source:+ description: Exact image producer source commit+ type: string+ required: true+ push:+ branches: ['os-polish/asahi-media', 'os-polish/asahi-media-zstd']+ paths:+ - 'os/platforms/apple-silicon/**'+ - 'os/tools/asahi_media.py'+ - 'os/tests/*asahi_media*.py'+ - '.github/workflows/os-asahi-media.yml'++permissions:+ contents: read+ actions: read++concurrency:+ group: harness-asahi-media-${{ github.ref }}+ cancel-in-progress: false++jobs:+ media:+ runs-on: ubuntu-24.04-arm+ timeout-minutes: 60+ env:+ IMAGE_RUN: ${{ inputs.image_run_id || '37523740636' }}+ IMAGE_SOURCE: ${{ inputs.image_source || 'b3cd5b062e8988ba1b640228a78857f8e7475184' }}+ steps:+ - uses: actions/checkout@v7+ with:+ fetch-depth: 1+ - name: Verify the frozen image producer+ env:+ GH_TOKEN: ${{ github.token }}+ run: |+ [[ "$IMAGE_RUN" =~ ^[0-9]+$ ]]+ [[ "$IMAGE_SOURCE" =~ ^[a-f0-9]{40}$ ]]+ mkdir -p os/work/asahi-media-output+ gh api "repos/$GITHUB_REPOSITORY/actions/runs/$IMAGE_RUN" > os/work/asahi-media-output/image-producer.json+ python3 - <<'PY'+ import json, os+ run = json.load(open('os/work/asahi-media-output/image-producer.json'))+ assert run['head_sha'] == os.environ['IMAGE_SOURCE']+ assert run['path'] == '.github/workflows/os-asahi-image.yml'+ assert run['status'] == 'completed' and run['conclusion'] == 'success'+ assert run['repository']['full_name'] == os.environ['GITHUB_REPOSITORY']+ PY+ - uses: actions/download-artifact@v6+ with:+ name: harness-asahi-private-disk+ run-id: ${{ env.IMAGE_RUN }}+ github-token: ${{ github.token }}+ path: os/work/asahi-image-input+ - uses: actions/download-artifact@v6+ with:+ name: harness-asahi-image-evidence+ run-id: ${{ env.IMAGE_RUN }}+ github-token: ${{ github.token }}+ path: os/work/asahi-image-evidence+ - name: Check media contracts and prepare native builder+ run: |+ python3 -m unittest discover -s os/tests -p 'test_asahi_*.py'+ df -h . "$RUNNER_TEMP"+ sudo apt-get update -qq+ sudo apt-get install -y --no-install-recommends podman zstd+ (cd os/work/asahi-image-input && sha256sum -c SHA256SUMS)+ zstd -d --sparse os/work/asahi-image-input/harness-asahi-private.raw.zst -o os/work/asahi-image-input/payload.raw+ python3 - <<'PY' >> "$GITHUB_ENV"+ import json+ lock = json.load(open('os/platforms/apple-silicon/source.lock.json'))+ print('BUILDER_IMAGE=' + lock['builder_image'])+ print('UPSTREAM_REPO=' + lock['upstream']['repository'])+ print('UPSTREAM_BRANCH=' + lock['upstream']['branch'])+ print('UPSTREAM_COMMIT=' + lock['upstream']['commit'])+ PY+ - name: Fetch the reviewed Fedora Asahi description+ run: |+ git clone --branch "$UPSTREAM_BRANCH" "$UPSTREAM_REPO" os/work/asahi-upstream+ git -C os/work/asahi-upstream checkout --detach "$UPSTREAM_COMMIT"+ sudo podman pull --arch arm64 "$BUILDER_IMAGE"+ sudo podman image inspect "$BUILDER_IMAGE" > os/work/asahi-media-output/builder-image.json+ - name: Build and inspect private offline UEFI media+ run: |+ sudo podman run --rm --privileged --arch arm64 \+ --mount type=bind,source=/dev,target=/dev \+ --mount "type=bind,source=$PWD,target=/source,readonly" \+ --mount "type=bind,source=$PWD/os/work/asahi-media-output,target=/output" \+ -e IMAGE_SOURCE="$IMAGE_SOURCE" "$BUILDER_IMAGE" /bin/bash -euo pipefail -c '+ manager=$(command -v dnf5 || command -v microdnf)+ "$manager" install -y --setopt=install_weak_deps=False --setopt=gpgcheck=True \+ python3 git rpm kiwi kiwi-systemdeps-iso-media distribution-gpg-keys \+ squashfs-tools xorriso util-linux zstd+ python3 /source/os/tools/asahi_media.py \+ --upstream /source/os/work/asahi-upstream \+ --image /source/os/work/asahi-image-input/payload.raw \+ --receipt /source/os/work/asahi-image-evidence/inspection/receipt.json \+ --producer /output/image-producer.json --image-source "$IMAGE_SOURCE" \+ --output /output/recipe+ kiwi-ng --debug --type=iso --profile=HarnessInstall system build \+ --description /output/recipe/description --target-dir /output/media+ python3 /source/os/tests/asahi_media_check.py \+ --iso /output/media/Harness-Asahi-Installer.aarch64-0.0.0.iso \+ --identity /output/recipe/media-inputs.json --output /output/inspection+ cd /output/media+ sha256sum Harness-Asahi-Installer.aarch64-0.0.0.iso > SHA256SUMS+ '+ - name: Retain construction evidence+ if: always()+ uses: actions/upload-artifact@v6+ with:+ name: harness-asahi-media-evidence+ path: |+ os/work/asahi-media-output/builder-image.json+ os/work/asahi-media-output/image-producer.json+ os/work/asahi-media-output/recipe/media-inputs.json+ os/work/asahi-media-output/recipe/description/config.xml+ os/work/asahi-media-output/recipe/description/COPYING+ os/work/asahi-media-output/media/*.packages+ os/work/asahi-media-output/media/kiwi.result.json+ os/work/asahi-media-output/media/build/image-root.log+ os/work/asahi-media-output/inspection/+ if-no-files-found: error+ retention-days: 7+ - name: Retain private media only after inspection+ if: success()+ uses: actions/upload-artifact@v6+ with:+ name: harness-asahi-private-installer+ path: |+ os/work/asahi-media-output/media/Harness-Asahi-Installer.aarch64-0.0.0.iso+ os/work/asahi-media-output/media/SHA256SUMS+ os/work/asahi-media-output/recipe/media-inputs.json+ if-no-files-found: error+ retention-days: 3+ compression-level: 0diff --git a/os/apple-silicon-media.md b/os/apple-silicon-media.mdnew file mode 100644index 000000000..cb97b739c--- /dev/null+++ b/os/apple-silicon-media.md@@ -0,0 +1,77 @@+# Private Apple Silicon installer media++This is development media, not a public download or a claim of physical Mac+support. It boots directly into **Install Harness** and carries the previously+verified Harness disk image for offline installation. It builds no shared client+and changes no release or update feed.++The native ARM builder uses the pinned Fedora Asahi repository description and+signed platform packages. KIWI provides its standard UEFI live ISO and read-only+SquashFS root with a temporary overlay. There is no persistent live session,+account wizard, desktop or trial workspace on this media. The installed payload+retains the tested Harness runtime, OpenCode and optional browser.++## Apple boot requirements++Apple Silicon requires the Asahi installer to establish its machine-specific+boot policy and a minimal UEFI environment, leaving free space for Harness. The+USB contains the ARM UEFI fallback loader; it does not carry or maintain+`m1n1/boot.bin`. Firmware identifies the owning internal EFI partition. Harness+uses only the prepared gap after it and never resizes or rearranges APFS.++See the upstream [distribution guidelines](https://asahilinux.org/docs/alt/policy/)+and [boot process](https://asahilinux.org/docs/alt/boot-process-guide/). This private+work is not endorsed by Asahi. Apple's boot-policy handoff, the installed boot+chain, physical keyboard/display behavior, and device-family acceptance still+need validation before publishing Mac installation instructions.++## Construction and verification++The **Harness OS private Apple Silicon installer media** workflow accepts an+existing successful `os-asahi-image.yml` run and its full source commit. It checks+that producer, downloads its retained disk and inspection evidence, verifies the+compressed file, then verifies the raw image against the inspection's SHA-256.++`os/tools/asahi_media.py` creates the live recipe and records the payload identity+and installer source hashes. The image remains compressed within SquashFS;+startup maps it read-only with 4096-byte sectors. The installer hashes and checks+the source before making any destination changes. A locked root account, masked+login/SSH and installed-system setup services, and enforcing SELinux keep this+media focused on installation.++`os/tests/asahi_media_check.py` mounts the actual produced ISO, its GPT EFI system+partition, and its compressed root read-only. The EFI partition is the firmware+boot surface; the ISO9660 file mirror is only diagnostic evidence. The check+requires ARM64 EFI executables and their boot configuration, then verifies payload bytes, installer files,+platform kernel, automatic installer service, pristine account state and absence+of machine secrets. Only an inspected ISO is retained as the private artifact.+Construction alone does not prove that it boots or installs successfully.++## Native media acceptance++On the Apple Silicon development host:++```sh+python3 os/tests/asahi_live_vm.py \+ --iso /path/to/Harness-Asahi-Installer.aarch64-0.0.0.iso \+ --media-receipt /path/to/media-evidence/inspection/receipt.json \+ --image /path/to/verified/harness-asahi-private.raw \+ --fixture /path/to/verified-arm-fixture \+ --fixture-source FULL_FIXTURE_COMMIT \+ --output os/test-results/asahi-media+```++The observer prepares a fresh regular disk with protected partition sentinels,+then boots the actual read-only ISO through UEFI with no network device. QEMU+provides the firmware EFI identity in its device tree; the installer runs+unchanged. Graphical keyboard input completes the form and invokes **Shut down**.+The observer inspects the resulting installation and protected bytes before+adding only QEMU console/input configuration to that disposable installed disk.+It then verifies wrong-password rejection, encrypted unlock, the frozen Harness+workspace, accounts, SELinux, runtime files and clean shutdown.++Receipts record the exact media, payload, fixture and observer inputs. Real+framebuffer screenshots are retained, including failures. The+test uses a known fixture password: neither its maintenance disk nor installed+target may be published. QEMU acceptance does not establish Apple firmware or+physical hardware support.diff --git a/os/platforms/apple-silicon/00-harness-installer.preset b/os/platforms/apple-silicon/00-harness-installer.presetnew file mode 100644index 000000000..b1714c83b--- /dev/null+++ b/os/platforms/apple-silicon/00-harness-installer.preset@@ -0,0 +1 @@+enable harness-installer.servicediff --git a/os/platforms/apple-silicon/configure-media.sh b/os/platforms/apple-silicon/configure-media.shnew file mode 100644index 000000000..c4f44dac3--- /dev/null+++ b/os/platforms/apple-silicon/configure-media.sh@@ -0,0 +1,40 @@+#!/bin/bash+set -euo pipefail++# Runs only inside KIWI's disposable root; no live-machine configuration.+test -f /.kconfig+test -f /usr/share/harness-installer/media.json+test -f /usr/share/harness-installer/payload.raw++printf 'uninitialized\n' > /etc/machine-id+printf 'harness\n' > /etc/hostname+rm -f /var/lib/systemd/random-seed /etc/ssh/ssh_host_*_key /etc/reconfigSys+usermod -L root++# Removable media must not maintain the internal Asahi ESP or initialize an+# installed OS. Firmware loading in dracut-asahi remains upstream-owned.+rm -rf /boot/efi/m1n1+rm -f /boot/efi/.builder+systemctl mask [email protected] [email protected] sshd.service \+ systemd-firstboot.service first-boot.service initial-setup.service \+ asahi-setup-swap-firstboot.service asahi-extras-firstboot.service+systemctl enable harness-installer.service+systemctl set-default multi-user.target++mkdir -p /etc/dracut.conf.d+printf 'hostonly="no"\nadd_drivers+=" virtio_input "\n' > /etc/dracut.conf.d/20-harness-installer.conf+printf 'KEYMAP=us\nFONT=eurlatgr\n' > /etc/vconsole.conf+sed -i 's/^SELINUX=.*/SELINUX=enforcing/' /etc/selinux/config+sed -i 's/DEFAULTKERNEL=kernel-core/DEFAULTKERNEL=kernel-16k-core/' /etc/sysconfig/kernel+rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-fedora-44-primary++# Use the verified installed image's policy before the live kernel ever reads+# source labels. Unknown types would otherwise be copied as unlabeled_t.+# Move whole trees after RPM transactions so no stale live-only modules remain.+policy=/usr/share/harness-installer/policy+test -f "$policy/etc/selinux/targeted/contexts/files/file_contexts"+test -f "$policy/var/lib/selinux/targeted/active/policy.kern"+rm -rf /etc/selinux/targeted /var/lib/selinux/targeted+mv "$policy/etc/selinux/targeted" /etc/selinux/targeted+mv "$policy/var/lib/selinux/targeted" /var/lib/selinux/targeted+rm -rf "$policy"diff --git a/os/platforms/apple-silicon/harness-installer.service b/os/platforms/apple-silicon/harness-installer.servicenew file mode 100644index 000000000..6567a743c--- /dev/null+++ b/os/platforms/apple-silicon/harness-installer.service@@ -0,0 +1,24 @@+[Unit]+Description=Harness installation+After=systemd-vconsole-setup.service systemd-udev-settle.service+Wants=systemd-udev-settle.service++[Service]+Type=simple+ExecStartPre=/usr/bin/chvt 1+ExecStart=/usr/bin/python3 /usr/lib/harness-installer/media.py+StandardInput=tty+StandardOutput=tty+StandardError=journal+TTYPath=/dev/tty1+TTYReset=yes+TTYVHangup=yes+Environment=TERM=linux LANG=C.UTF-8+UMask=0077+LimitCORE=0+Restart=always+RestartSec=2++[Install]+WantedBy=multi-user.targetdiff --git a/os/platforms/apple-silicon/install.py b/os/platforms/apple-silicon/install.pyindex 2ae7f0181..8dbaf1311 100644--- a/os/platforms/apple-silicon/install.py+++ b/os/platforms/apple-silicon/install.py@@ -90,29 +90,30 @@ def destination():def install(root, plan, payload, password, progress):validate_password(password, password)progress('Checking installation files…')- # An invalid source must not even create destination partitions or a receipt.+ # Verify before any destination change, then keep this read-only source open+ # through copying and startup. Each stage still verifies when used alone.with payload.open():verify_destination(plan)- progress('Preparing storage…')- storage.run('mount', '-o', 'remount,rw', root)- path = root / RECORD- if os.path.lexists(path):- if target.load_plan(path) != plan:- raise target.TargetError('The installation plan changed. Keep it for recovery.')- else:- path.parent.mkdir(mode=0o700, parents=True, exist_ok=True)- target.save_plan(path, plan)- target.apply_plan(path)- storage.install(path, payload, password, progress=progress)- progress('Preparing startup…')- def phase(value):- progress({'boot': 'Setting up your account…', 'account': 'Finishing installation…',- 'complete': 'Harness is installed.'}[value])- state = startup.finish(path, payload, password, progress=phase)- if state['phase'] != 'complete':- raise storage.StorageError('Installation has not finished. Start the installer again to continue.')- storage.run('sync', '-f', root)- return state+ progress('Preparing storage…')+ storage.run('mount', '-o', 'remount,rw', root)+ path = root / RECORD+ if os.path.lexists(path):+ if target.load_plan(path) != plan:+ raise target.TargetError('The installation plan changed. Keep it for recovery.')+ else:+ path.parent.mkdir(mode=0o700, parents=True, exist_ok=True)+ target.save_plan(path, plan)+ target.apply_plan(path)+ storage.install(path, payload, password, progress=progress)+ progress('Preparing startup…')+ def phase(value):+ progress({'boot': 'Setting up your account…', 'account': 'Finishing installation…',+ 'complete': 'Harness is installed.'}[value])+ state = startup.finish(path, payload, password, progress=phase)+ if state['phase'] != 'complete':+ raise storage.StorageError('Installation has not finished. Start the installer again to continue.')+ storage.run('sync', '-f', root)+ return statedef diagnostic(error):diff --git a/os/platforms/apple-silicon/media.py b/os/platforms/apple-silicon/media.pynew file mode 100644index 000000000..0ff511f09--- /dev/null+++ b/os/platforms/apple-silicon/media.py@@ -0,0 +1,98 @@+#!/usr/bin/python3+"""Launch the offline installer from its private, immutable live-media payload."""+from contextlib import contextmanager+import curses+import fcntl+import importlib.util+import json+import os+from pathlib import Path+import platform+import re+import stat+import subprocess++HERE = Path(__file__).resolve().parent+DATA = Path('/usr/share/harness-installer')+spec = importlib.util.spec_from_file_location('asahi_install', HERE / 'install.py')+installer = importlib.util.module_from_spec(spec)+spec.loader.exec_module(installer)+++def identity(folder=DATA, owner_uid=0):+ manifest = folder / 'media.json'+ payload = folder / 'payload.raw'+ for path in (manifest, payload):+ info = path.lstat()+ if not stat.S_ISREG(info.st_mode) or info.st_uid != owner_uid or info.st_mode & 0o022:+ raise ValueError('The installation files have changed. Recreate the installer USB.')+ value = json.loads(manifest.read_text())+ image = value.get('payload', {})+ if (value.get('schema') != 1 or value.get('kind') != 'harness-asahi-installer-media' or+ value.get('published') is not False or value.get('release_ready') is not False or+ not re.fullmatch('[a-f0-9]{40}', str(value.get('source_commit'))) or+ not re.fullmatch('[a-f0-9]{40}', str(image.get('source_commit'))) or+ not re.fullmatch('[a-f0-9]{64}', str(image.get('sha256'))) or+ type(image.get('bytes')) is not int or image['bytes'] != payload.stat().st_size):+ raise ValueError('The installation manifest is incomplete. Recreate the installer USB.')+ return value+++@contextmanager+def payload_device(folder=DATA):+ info = identity(folder)['payload']+ # The raw image stays compressed inside SquashFS and is never expanded into+ # RAM. The existing installer verifies every byte before any target writes.+ run = installer.storage.run+ loop = run('losetup', '--find', '--show', '--read-only', '--partscan',+ '--sector-size', '4096', folder / 'payload.raw').strip()+ if not re.fullmatch('/dev/loop[0-9]+', loop):+ raise ValueError('Could not open the installation files.')+ try:+ run('udevadm', 'settle', '--timeout=15')+ yield installer.storage.Payload(loop, info['sha256'], info['source_commit'])+ finally:+ run('losetup', '--detach', loop)+++def stopped(screen, message):+ view = installer.Page(screen)+ try:+ while True:+ view.render(status=message)+ view.button(11, 'Shut down', 2, compact=True)+ screen.refresh()+ if view.key() in ('\n', '\r', curses.KEY_ENTER):+ installer.storage.run('systemctl', 'poweroff')+ return+ finally:+ view.close()+++def main():+ if platform.system() != 'Linux' or os.geteuid() != 0:+ raise SystemExit('Start from the private Harness installer media.')+ # The service owns this namespace. The install command also defends its own+ # namespace so it remains usable independently in maintenance environments.+ if os.readlink('/proc/self/ns/mnt') == os.readlink('/proc/1/ns/mnt'):+ os.execv('/usr/bin/unshare', ['unshare', '--mount', '--propagation', 'private',+ '/usr/bin/python3', str(HERE / 'media.py')])+ curses.set_escdelay(25)+ lock = os.open('/run/harness-asahi-install.lock', os.O_RDWR | os.O_CREAT | os.O_NOFOLLOW, 0o600)+ installer.target.private_file(lock)+ fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB)+ try:+ with payload_device() as payload:+ installer.target.platform_esp()+ curses.wrapper(lambda screen: installer.application(screen, payload))+ except (OSError, ValueError, subprocess.SubprocessError) as error:+ installer.diagnostic(error)+ curses.wrapper(lambda screen: stopped(screen,+ 'Installation could not start. Prepare this Mac with the Asahi UEFI installer. '+ 'Details: /var/log/harness-asahi-install.log'))+ finally:+ os.close(lock)+++if __name__ == '__main__':+ main()diff --git a/os/platforms/apple-silicon/startup.py b/os/platforms/apple-silicon/startup.pyindex a729d272b..cfefde692 100644--- a/os/platforms/apple-silicon/startup.py+++ b/os/platforms/apple-silicon/startup.py@@ -192,6 +192,13 @@ def offline(root):with ExitStack() as mounts:mounts.enter_context(mount_at('proc', root / 'proc', 'nosuid,nodev,noexec', 'proc'))mounts.enter_context(mount_at('sysfs', root / 'sys', 'ro,nosuid,nodev,noexec', 'sysfs'))+ if Path('/sys/fs/selinux/enforce').exists():+ # Account tools must see the running SELinux policy when replacing+ # passwd/shadow files. A bare sysfs makes them assume it is disabled+ # even though the kernel still enforces labels. Policy queries use+ # writable selinuxfs request files; /sys itself remains read-only.+ mounts.enter_context(mount_at('selinuxfs', root / 'sys/fs/selinux',+ 'nosuid,nodev,noexec', 'selinuxfs'))# --rbind is needed for /dev/pts. The outer private namespace prevents# propagation into installer services; recursive unmount releases it.dev = root / 'dev'@@ -226,15 +233,25 @@ def verify_runtime(root, payload):def label_files(root, *paths):# restorecon can silently do nothing when the installer kernel has SELinux# disabled. setfiles explicitly labels an offline filesystem with the- # installed policy, regardless of the installer's enforcement mode.- run('chroot', root, '/usr/sbin/setfiles', '-F', '-e', '/boot/efi',+ # installed policy, which can contain types absent from the smaller live+ # policy. Validate against its binary rather than the running kernel policy.+ policies = [p for p in checked(root, 'etc/selinux/targeted/policy').iterdir()+ if re.fullmatch(r'policy\.\d+', p.name)]+ if len(policies) != 1:+ raise Error('Use the verified image with one installed SELinux policy.')+ policy = str(policies[0].relative_to(root))+ snapshot(root, policy)+ # These are explicitly mounted target filesystems. A maintenance kernel can+ # omit their seclabel mount flag; automatic mount-table exclusions then+ # silently skip recursive labeling. Keep only our explicit EFI exclusion.+ run('chroot', root, '/usr/sbin/setfiles', '-m', '-F', '-c', '/' + policy, '-e', '/boot/efi','/etc/selinux/targeted/contexts/files/file_contexts', *paths, timeout=90)def build_boot(root, configs):for name, content in configs.items():atomic(checked(root, name), content.encode(), 0o600 if name == 'etc/crypttab' else 0o644)- label_files(root, *('/' + name for name in configs))+ label_files(root, *('/' + name for name in configs), '/boot')run('chroot', root, 'grubby', '--update-kernel=ALL', '--remove-args=root rd.luks.uuid rd.luks.name','--args=' + configs['etc/kernel/cmdline'].strip())run('chroot', root, 'grub2-mkconfig', '-o', '/boot/grub2/grub.cfg')diff --git a/os/platforms/apple-silicon/storage.py b/os/platforms/apple-silicon/storage.pyindex 91f8cb49e..b3415787c 100644--- a/os/platforms/apple-silicon/storage.py+++ b/os/platforms/apple-silicon/storage.py@@ -110,7 +110,7 @@ def save_state(path, state):os.fsync(stream.fileno())os.replace(temporary, path)temporary = None- os.fsync(directory)+ target.sync_directory(directory)finally:if temporary is not None:os.unlink(temporary)@@ -171,9 +171,16 @@ class Payload:self.device = str(device)self.sha256, self.source_commit = image_sha256, source_commitself.root = self.home = self.boot = self.esp = None+ self._opened = False@contextmanagerdef open(self):+ # Installation stages can share one verified, read-only mount lifetime.+ # This is never cached across attempts: the outer exit clears the state+ # and the next independent open verifies the whole source again.+ if self._opened:+ yield self+ returninfo = os.stat(self.device, follow_symlinks=False)if not stat.S_ISBLK(info.st_mode) or run('blockdev', '--getro', self.device) != '1':raise StorageError('The verified source image must be a read-only block device.')@@ -198,8 +205,10 @@ class Payload:self.root, self.home, self.boot = top / 'root', top / 'home', boottry:self.verify_pristine()+ self._opened = Trueyield selffinally:+ self._opened = Falseself.root = self.home = self.boot = self.esp = Nonedef verify_pristine(self):@@ -258,9 +267,15 @@ def subvolumes(top):run('btrfs', 'subvolume', 'show', path)-def copy_tree(source, destination, excludes=()):+def copy_tree(source, destination, excludes=(), *, copy_selinux=True):+ # Some generated boot files are unlabeled. Keep destination labels until+ # startup applies the installed policy, rather than removing those labels+ # under enforcement. Root/home retain their source labels. An explicit+ # xattr rule also requires preserving rsync's system.* exclusion.+ filters = () if copy_selinux else ('--filter=-x system.*', '--filter=-x security.selinux')run('rsync', '-aHAX', '--numeric-ids', '--one-file-system', '--delete', '--checksum',- *('--exclude=' + value for value in excludes), str(source) + '/', str(destination) + '/', timeout=300)+ *filters, *('--exclude=' + value for value in excludes),+ str(source) + '/', str(destination) + '/', timeout=300)def mountpoint(source, destination):@@ -270,6 +285,16 @@ def mountpoint(source, destination):shutil.copystat(source, destination)+def copy_root(source, destination):+ # The kernel supplies runtime filesystems on boot. Copying the image's+ # temporary /dev nodes also tries to remove their SELinux labels, which an+ # enforcing installer correctly refuses. Keep only the mount directories.+ separate = ('boot', 'home', 'dev', 'proc', 'sys', 'run')+ for name in separate:+ mountpoint(source / name, destination / name)+ copy_tree(source, destination, excludes=tuple('/' + name + '/***' for name in separate))++def install(plan_path, payload, password, progress=None):"""Enroll unique encrypted storage, then resume only its unfinished OS copy."""if not isinstance(password, str) or not password or any(c in password for c in '\0\n\r'):@@ -317,13 +342,10 @@ def install(plan_path, payload, password, progress=None):advance(path, state, 'filesystems')advance(path, state, 'copying')report('Copying Harness…')- # Mountpoints remain present even when rsync excludes their contents.- for name in ('boot', 'home'):- mountpoint(payload.root / name, top / 'root' / name)+ copy_root(payload.root, top / 'root')mountpoint(payload.boot / 'efi', boot_path / 'efi')- copy_tree(payload.root, top / 'root', excludes=('/boot/***', '/home/***'))copy_tree(payload.home, top / 'home')- copy_tree(payload.boot, boot_path, excludes=('/efi/***',))+ copy_tree(payload.boot, boot_path, excludes=('/efi/***',), copy_selinux=False)run('sync', '-f', top)run('sync', '-f', boot_path)current = target.read_table(plan['original']['device'])diff --git a/os/platforms/apple-silicon/target.py b/os/platforms/apple-silicon/target.pyindex 826265781..47ca0f739 100644--- a/os/platforms/apple-silicon/target.py+++ b/os/platforms/apple-silicon/target.py@@ -7,6 +7,7 @@ partitions are never removed, moved, resized or formatted by this module.from __future__ import annotationsfrom contextlib import contextmanager+import ctypesimport hashlibimport fcntlimport json@@ -267,6 +268,18 @@ def private_directory(fd):raise TargetError('Save the plan in a private, owned directory.')+def sync_directory(fd):+ os.fsync(fd)+ if platform.system() == 'Linux':+ # On FAT, fsync(file) + rename + fsync(parent) can leave the old entry+ # after a power loss. Flush this filesystem before the next disk stage.+ syncfs = ctypes.CDLL(None, use_errno=True).syncfs+ syncfs.argtypes, syncfs.restype = [ctypes.c_int], ctypes.c_int+ if syncfs(fd) != 0:+ error = ctypes.get_errno()+ raise OSError(error, os.strerror(error))++def save_plan(path, plan):"""Atomically persist before disk writes, including on the FAT boot partition.@@ -294,7 +307,7 @@ def save_plan(path, plan):os.fsync(stream.fileno())os.rename(temporary, path)temporary = None- os.fsync(directory)+ sync_directory(directory)finally:if temporary is not None:os.unlink(temporary)diff --git a/os/tests/arm_session.py b/os/tests/arm_session.pyindex 2ebf3449f..4c97a7324 100644--- a/os/tests/arm_session.py+++ b/os/tests/arm_session.py@@ -103,7 +103,7 @@ class SessionVM(VM):self.monitor('input-send-event', events=events)time.sleep(.12)- def frame(self, name, text, seconds=60, absent=()):+ def frame(self, name, text, seconds=60, absent=(), fatal=()):from PIL import Image, ImageOpsdeadline = time.monotonic() + secondswhile time.monotonic() < deadline:@@ -126,6 +126,9 @@ class SessionVM(VM):['tesseract', str(ocr_frame), 'stdout', '--psm', '6'],text=True, stderr=subprocess.DEVNULL, timeout=15)(self.folder / (name + '.txt')).write_text(output)+ for error in fatal:+ if frame_contains(output, error):+ raise RuntimeError('Visible failure screen: ' + error)if frame_contains(output, text, absent):returntime.sleep(1)diff --git a/os/tests/asahi_install_guest.py b/os/tests/asahi_install_guest.pyindex 8b4459c36..aaa10be9a 100644--- a/os/tests/asahi_install_guest.py+++ b/os/tests/asahi_install_guest.py@@ -1,7 +1,9 @@#!/usr/bin/python3"""VM-only observer for the real private installer. Never run on host disks."""import importlib.util+import errnoimport json+import osfrom pathlib import Pathimport sys@@ -20,6 +22,41 @@ ui.target.platform_esp = lambda: fixture.ESPassert fixture.run('lsblk', '-ndo', 'SERIAL', '/dev/vdc') == 'HARNESS_PAYLOAD'+def verify_root_labels(source, installed):+ # The maintenance observer has SELinux disabled and therefore reads raw+ # stored labels. A live policy missing a source type can silently turn that+ # type into unlabeled_t during copying; in-live comparisons miss the loss.+ assert fixture.run('getenforce') == 'Disabled'+ def label(path):+ try:+ return os.getxattr(path, 'security.selinux', follow_symlinks=False)+ except OSError as error:+ if error.errno == errno.ENODATA:+ return None+ raise+ checked = 0+ policy_unlabeled = []+ for path in source.rglob('*'):+ relative = path.relative_to(source)+ if relative.parts[0] in ('boot', 'home', 'dev', 'proc', 'sys', 'run'):+ continue+ expected, actual = label(path), label(installed / relative)+ if expected is None and actual == b'system_u:object_r:unlabeled_t:s0\0':+ # Enforcing kernels expose an unlabeled inode as this context and+ # rsync serializes it. Accept that representation only when both+ # policies explicitly leave the path unlabeled (e.g. rpc_pipefs).+ # An existing source label becoming unlabeled still fails below.+ for tree in (source, installed):+ assert ui.storage.run('chroot', tree, '/usr/sbin/matchpathcon', '-n',+ '/' + str(relative)) == '<<none>>'+ policy_unlabeled.append(str(relative))+ else:+ assert actual == expected, f'SELinux label differs for {relative}: {actual!r} != {expected!r}'+ checked += 1+ return {'paths': checked, 'observer_selinux': 'Disabled',+ 'explicit_source_labels_preserved': True, 'policy_unlabeled': policy_unlabeled}++def inspect():# The live form already has a read-only view. Observe with the same flags;# asking for a writable FAT superblock here would conflict with that view.@@ -37,6 +74,25 @@ def inspect():result['remaining'] = ui.target.remaining(plan, ui.target.read_table(fixture.DISK))for name in ('storage', 'startup'):result[name] = json.loads(fixture.PLAN.with_name(name + '.json').read_text())+ with ui.storage.encrypted_root('/dev/vdb6', result['storage'], b'firstboot-local-42') as device:+ with ui.storage.work_directory() as work:+ with ui.storage.mounted(device, work / 'root', 'ro,rescue=nologreplay,subvol=root') as root:+ result['runtime_mountpoints'] = {+ name: sorted(p.name for p in (root / name).iterdir())+ for name in ('dev', 'proc', 'sys', 'run')}+ assert all(not entries for entries in result['runtime_mountpoints'].values())+ saved = result['storage']+ payload = ui.storage.Payload('/dev/vdc', saved['image_sha256'], saved['source_commit'])+ with payload.open():+ result['root_labels'] = verify_root_labels(payload.root, root)+ # Check the actual installation before the observer adds+ # QEMU console/input settings or rebuilds any boot files.+ with ui.startup.mount_at('/dev/vdb5', root / 'boot', 'ro,noload'):+ entries = sorted((root / 'boot/loader/entries').glob('*.conf'))+ assert entries+ labels = ['/boot/grub2/grub.cfg', *('/' + str(p.relative_to(root)) for p in entries)]+ ui.storage.run('chroot', root, '/usr/sbin/matchpathcon', '-V', *labels)+ result['boot_labels_verified'] = labelsPath('/var/tmp/harness-install-inspection.json').write_text(json.dumps(result, indent=2) + '\n')finally:fixture.run('umount', fixture.ROOT)diff --git a/os/tests/asahi_live_vm.py b/os/tests/asahi_live_vm.pynew file mode 100644index 000000000..df33ac99f--- /dev/null+++ b/os/tests/asahi_live_vm.py@@ -0,0 +1,224 @@+#!/usr/bin/env python3+"""Boot actual private UEFI media, install offline, and boot the encrypted target.++Only fresh regular test disks are writable. QEMU supplies a firmware device-tree+ESP identity; it does not emulate Apple's boot policy or physical hardware.+"""+import argparse+import json+from pathlib import Path+import platform+import select+import shutil+import subprocess+import threading+import time++from arm_boot import digest+from arm_session import fixture_identity+from asahi_encryption_vm import MaintenanceVM, unlock+from asahi_firstboot_vm import ImageVM, PASSWORD, evidence, type_line+from asahi_install_vm import stage, inspect+from asahi_startup_vm import installed+from asahi_target_vm import guest as target_guest+from session_vm import put++ROOT = Path(__file__).resolve().parents[1]+ESP = 'e51d26b0-4c8f-41fb-9d83-a0fdd62327c0'+++class MediaVM(ImageVM):+ def __init__(self, folder, disk, iso):+ super().__init__(folder, disk, None)+ self.iso = iso++ def start(self):+ firmware = json.loads(Path('/opt/homebrew/share/qemu/firmware/60-edk2-aarch64.json').read_text())['mapping']+ variables = self.folder / 'uefi-vars.fd'+ shutil.copyfile(firmware['nvram-template']['filename'], variables)+ dtb = self.folder / 'firmware.dtb'+ command = ['qemu-system-aarch64', '-machine', 'virt,gic-version=3,acpi=off', '-accel', 'hvf',+ '-cpu', 'host', '-smp', '2', '-m', '3072', '-nodefaults', '-display', 'none', '-no-reboot',+ '-drive', f'if=pflash,format=raw,readonly=on,file={firmware["executable"]["filename"]}',+ '-drive', f'if=pflash,format=raw,file={variables}',+ '-drive', f'file={self.iso},format=raw,if=none,id=media,readonly=on',+ '-device', 'virtio-blk-pci,drive=media,serial=HARNESS_INSTALL_MEDIA,bootindex=1',+ '-drive', f'file={self.disk},format=raw,if=none,id=target',+ '-device', 'virtio-blk-pci,drive=target,serial=HARNESS_ENCRYPT_TEST,logical_block_size=4096,physical_block_size=4096',+ '-device', 'virtio-gpu-pci', '-device', 'virtio-keyboard-pci', '-device', 'virtio-tablet-pci']+ dump = list(command)+ dump[2] += ',dumpdtb=' + str(dtb)+ subprocess.run(dump, stdout=self.stderr, stderr=self.stderr, check=True, timeout=30)+ subprocess.run(['fdtput', '-t', 's', str(dtb), '/chosen', 'asahi,efi-system-partition', ESP],+ check=True, timeout=10)+ command += ['-dtb', str(dtb),+ '-serial', f'unix:{self.control_path / "serial.sock"},server=on,wait=off',+ '-qmp', f'unix:{self.control_path / "qmp.sock"},server=on,wait=off']+ (self.folder / 'command.json').write_text(json.dumps(command, indent=2) + '\n')+ self.started = time.monotonic()+ self.process = subprocess.Popen(command, stdout=self.stderr, stderr=self.stderr)+ self.serial = self.connect('serial.sock')+ self.drain_stop = threading.Event()+ def drain():+ while not self.drain_stop.is_set():+ if select.select([self.serial], [], [], .1)[0]:+ data = self.serial.recv(65536)+ if not data:+ return+ self.log.write(data)+ self.drain_thread = threading.Thread(target=drain, daemon=True)+ self.drain_thread.start()+ self.qmp = self.connect('qmp.sock')+ self.qmp.settimeout(10)+ self.qmp_file = self.qmp.makefile('rb', buffering=0)+ json.loads(self.qmp_file.readline())+ self.monitor('qmp_capabilities')+++def observer_console(vm):+ """Only after validating the actual install, add QEMU's login/input paths."""+ script = '''import importlib.util,json,pathlib+spec=importlib.util.spec_from_file_location('ui','/var/tmp/install.py')+ui=importlib.util.module_from_spec(spec);spec.loader.exec_module(ui)+storage,startup=ui.storage,ui.startup+esp=pathlib.Path('/mnt/harness-observer-esp');esp.mkdir()+with storage.mounted('/dev/vdb2',esp/'mounted','ro,noatime,uid=0,gid=0,fmask=0177,dmask=0077') as boot:+ plan=ui.target.load_plan(boot/'asahi/harness-install/target.json')+ state=json.loads((boot/'asahi/harness-install/storage.json').read_text())+ with storage.encrypted_root('/dev/vdb6',state,b'firstboot-local-42') as mapper:+ with storage.mounted(mapper,esp/'root','rw,subvol=root') as root:+ with startup.mount_at('/dev/vdb5',root/'boot','rw,noatime'):+ for name in ('etc/kernel/cmdline','etc/default/grub'):+ path=root/name;old=path.read_text()+ text=old.rstrip()+' console=ttyAMA0 console=tty0\\n' if name.endswith('cmdline') else old.replace('=harness-root"','=harness-root console=ttyAMA0 console=tty0"')+ path.write_text(text)+ (root/'etc/dracut.conf.d/99-harness-qemu-observer.conf').write_text('force_drivers+=" virtio_input "\\n')+ with startup.offline(root):+ storage.run('chroot',root,'grubby','--update-kernel=ALL','--args=console=ttyAMA0 console=tty0')+ storage.run('chroot',root,'dracut','--regenerate-all','--force','--no-hostonly',timeout=180)+ storage.run('chroot',root,'grub2-mkconfig','-o','/boot/grub2/grub.cfg')+ configs=['/etc/kernel/cmdline','/etc/default/grub','/etc/dracut.conf.d/99-harness-qemu-observer.conf']+ startup.label_files(root,'/boot',*configs)+ boot_files=['/boot/grub2/grub.cfg',*('/'+str(p.relative_to(root)) for p in (root/'boot/loader/entries').glob('*.conf')),+ *('/'+str(p.relative_to(root)) for p in (root/'boot').glob('initramfs-*.img'))]+ # Verify with the installed policy after removing the maintenance kernel's+ # virtual filesystems. With them mounted, its disabled SELinux can+ # canonicalize every expected context to the literal "kernel".+ storage.run('chroot',root,'/usr/sbin/matchpathcon','-V',*configs,*boot_files)+'''+ put(vm, '/var/tmp/harness-media-observer.py', script)+ vm.command('unshare --mount --propagation private python3 /var/tmp/harness-media-observer.py', timeout=240)+++def main():+ parser = argparse.ArgumentParser(description=__doc__)+ for name in ('iso', 'media-receipt', 'image', 'fixture', 'output'):+ parser.add_argument('--' + name, type=Path, required=True)+ parser.add_argument('--fixture-source', required=True)+ args = parser.parse_args()+ if (platform.system(), platform.machine()) != ('Darwin', 'arm64'):+ parser.error('Use the native Apple Silicon observer.')+ media = json.loads(args.media_receipt.read_text())+ if media.get('status') != 'passed' or media.get('media', {}).get('kind') != 'harness-asahi-installer-media':+ parser.error('Use an inspected private installer ISO.')+ for file, expected in ((args.iso, media['artifact']), (args.image, media['media']['payload'])):+ if file.is_symlink() or not file.is_file() or file.stat().st_size != expected['bytes'] or digest(file) != expected['sha256']:+ parser.error('Use exact, verified regular image files.')+ for name, checksum in media['media']['installer'].items():+ if digest(ROOT / 'platforms/apple-silicon' / name) != checksum:+ parser.error('The media installer source differs from this checkout.')+ info = fixture_identity(args.fixture, args.fixture_source)+ output = args.output.resolve()+ output.mkdir(parents=True, exist_ok=False)+ maintenance, disk = output / 'maintenance.raw', output / 'target.raw'+ with disk.open('xb') as file:+ file.truncate(24 * 1024**3)+ subprocess.run(['zstd', '-d', '--sparse', args.fixture / 'guest.raw.zst', '-o', maintenance], check=True, timeout=120)+ assert digest(maintenance) == info['raw_disk']['sha256']+ paths = [ROOT / 'platforms/apple-silicon' / name for name in media['media']['installer']]+ paths += [Path(__file__).with_name(name) for name in ('asahi_live_vm.py', 'asahi_install_vm.py',+ 'asahi_install_guest.py', 'asahi_startup_vm.py', 'asahi_target_vm.py', 'asahi_target_guest.py',+ 'asahi_encryption_vm.py', 'asahi_encryption_guest.py', 'asahi_firstboot_vm.py',+ 'arm_boot.py', 'arm_session.py', 'fedora_session_vm.py', 'session_vm.py', 'vm.py')]+ receipt = {'status': 'running', 'started_at': time.time(), 'publication': False,+ 'media_receipt_sha256': digest(args.media_receipt), 'media': media['artifact'],+ 'media_source': media['media']['source_commit'], 'payload': media['media']['payload'],+ 'fixture_source': args.fixture_source, 'fixture_manifest_sha256': digest(args.fixture / 'manifest.json'),+ 'inputs': {str(path.relative_to(ROOT)): digest(path) for path in paths}, 'shutdowns': [],+ 'limitations': ['QEMU firmware/ESP property, not physical Apple boot policy or m1n1 acceptance.',+ 'Protected partitions contain sentinel bytes, not actual macOS filesystems.',+ 'Known fixture password: never publish maintenance or installed target disks.',+ 'After inspecting the completed install, adds only QEMU console and input configuration.']}+ vm = None+ try:+ vm = MaintenanceVM(output / 'prepare', maintenance, args.fixture / 'Image', disk, source=args.image)+ vm.start()+ vm.command('dnf5 install -y --setopt=install_weak_deps=False --setopt=gpgcheck=True gdisk dosfstools cryptsetup btrfs-progs e2fsprogs rsync', timeout=180)+ target_guest(vm, 'initialize', output)+ stage(vm)+ receipt['before'] = inspect(vm, 'reset-plan')+ receipt['shutdowns'].append(vm.poweroff())+ vm.close()+ vm = MediaVM(output / 'media-boot', disk, args.iso)+ vm.start()+ vm.frame('01-install', ['Disk', 'Encryption', 'Repeat password', 'Install Harness'], seconds=150)+ receipt['media_boot_seconds'] = round(time.monotonic() - vm.started, 3)+ type_line(vm, PASSWORD)+ type_line(vm, PASSWORD)+ vm.frame('02-ready', ['Install Harness'], absent=[PASSWORD])+ started = time.monotonic()+ vm.keys('ret')+ vm.frame('03-progress', 'Checking installation files', seconds=60)+ vm.frame('04-complete', ['Harness is installed', 'Shut down'], seconds=480,+ fatal=['Installation stopped'])+ receipt['install_seconds'] = round(time.monotonic() - started, 3)+ vm.stop_drain()+ vm.keys('ret')+ receipt['shutdowns'].append(vm.poweroff(request=False))+ vm.close()+ print('The actual installer ISO completed an offline encrypted installation.', flush=True)+ vm = MaintenanceVM(output / 'inspect', maintenance, args.fixture / 'Image', disk, source=args.image)+ vm.start(offline=True)+ stage(vm)+ receipt['after'] = inspect(vm)+ assert receipt['after']['protected'] == receipt['before']['protected']+ assert receipt['after']['storage']['phase'] == 'copied'+ assert receipt['after']['startup']['phase'] == 'complete'+ assert not receipt['after']['remaining']+ observer_console(vm)+ receipt['shutdowns'].append(vm.poweroff())+ vm.close()+ vm = ImageVM(output / 'installed', disk, None)+ vm.start()+ unlock(vm, reject_wrong=True)+ vm.frame('04-workspace', ['opencode', 'Ask anything'], seconds=150, absent=['Set your password', 'panic'])+ vm.authenticate()+ receipt['installed'] = installed(vm)+ assert 'Failed to activate with specified passphrase.' in receipt['installed']['journal']+ receipt['account'] = evidence(vm, vm.folder)+ vm.wait_user('pgrep -u 1000 -x opencode >/dev/null && test "$(hn list-panes -F "#{pane_id}" | wc -l)" -eq 3')+ receipt['shutdowns'].append(vm.poweroff())+ receipt['status'] = 'passed'+ print('The media-installed system unlocked into the frozen Harness workspace.', flush=True)+ except BaseException as error:+ receipt.update(status='failed', error=str(error))+ if vm:+ try:+ vm.screenshot('failure')+ except Exception as diagnostic:+ receipt['diagnostic_error'] = str(diagnostic)+ raise+ finally:+ try:+ if vm:+ vm.close()+ finally:+ receipt.update(finished_at=time.time(), original_media_unchanged=digest(args.iso) == media['artifact']['sha256'],+ original_payload_unchanged=digest(args.image) == media['media']['payload']['sha256'])+ if not receipt['original_media_unchanged'] or not receipt['original_payload_unchanged']:+ receipt.update(status='failed', error='An original input image changed.')+ (output / 'receipt.json').write_text(json.dumps(receipt, indent=2) + '\n')+++if __name__ == '__main__':+ main()diff --git a/os/tests/asahi_media_check.py b/os/tests/asahi_media_check.pynew file mode 100644index 000000000..ca5c5273c--- /dev/null+++ b/os/tests/asahi_media_check.py@@ -0,0 +1,175 @@+#!/usr/bin/env python3+"""Inspect the produced private ISO, including its actual compressed live root."""+import argparse+from contextlib import ExitStack+import json+import os+from pathlib import Path+import re+import stat+import struct+import subprocess+import sys+import tempfile+import time++from asahi_image_check import digest+sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tools'))+from asahi_media import policy_inventory+++def efi_partition(document, image_bytes):+ """Use the GPT ESP that USB firmware reads, not the ISO9660 file mirror."""+ table = document.get('partitiontable', {})+ if table.get('label') != 'gpt' or table.get('unit') != 'sectors' or table.get('sectorsize') != 512:+ raise ValueError('Expected a hybrid ISO with a 512-byte GPT.')+ partitions = [p for p in table.get('partitions', [])+ if p.get('type', '').lower() == 'c12a7328-f81f-11d2-ba4b-00a0c93ec93b']+ if len(partitions) != 1:+ raise ValueError('Expected exactly one EFI system partition.')+ part = partitions[0]+ start, size = part.get('start'), part.get('size')+ if (type(start) is not int or type(size) is not int or start < 64 or size < 2048 or+ (start + size) * 512 > image_bytes):+ raise ValueError('The EFI system partition is outside the image.')+ return {'offset': start * 512, 'bytes': size * 512}+++def inspect_efi(esp):+ files = {}+ for relative in ('efi/boot/bootaa64.efi', 'efi/boot/grubaa64.efi', 'efi/boot/grub.cfg'):+ matches = [p for p in esp.rglob('*') if str(p.relative_to(esp)).lower() == relative]+ if len(matches) != 1 or matches[0].is_symlink() or not matches[0].is_file():+ raise ValueError('Missing or ambiguous EFI boot file: ' + relative)+ path = matches[0]+ content = path.read_bytes()+ if relative.endswith('.efi'):+ # PE/COFF Machine=ARM64, PE32+, Subsystem=EFI application. A file+ # called bootaa64.efi is not sufficient evidence of an ARM loader.+ offset = struct.unpack_from('<I', content, 0x3c)[0] if len(content) >= 64 else len(content)+ if (len(content) < 1024 or content[:2] != b'MZ' or offset + 94 > len(content) or+ content[offset:offset + 4] != b'PE\0\0' or+ struct.unpack_from('<H', content, offset + 4)[0] != 0xaa64 or+ struct.unpack_from('<H', content, offset + 24)[0] != 0x20b or+ struct.unpack_from('<H', content, offset + 92)[0] != 10):+ raise ValueError('Not an ARM64 EFI application: ' + relative)+ elif not content.strip():+ raise ValueError('Empty EFI boot configuration.')+ files[str(path.relative_to(esp))] = {'sha256': digest(path), 'bytes': path.stat().st_size}+ if list(esp.rglob('boot.bin')):+ raise ValueError('The USB must not provide m1n1/boot.bin.')+ return files+++def inspect_root(root, identity):+ data = root / 'usr/share/harness-installer'+ if json.loads((data / 'media.json').read_text()) != identity:+ raise ValueError('The live root lost its exact media provenance.')+ raw = data / 'payload.raw'+ if (raw.is_symlink() or not raw.is_file() or raw.stat().st_size != identity['payload']['bytes'] or+ digest(raw) != identity['payload']['sha256']):+ raise ValueError('The media payload differs from the tested image.')+ for name, checksum in identity['installer'].items():+ file = root / 'usr/lib/harness-installer' / name+ if file.is_symlink() or digest(file) != checksum or file.stat().st_uid != 0 or file.stat().st_mode & 0o022:+ raise ValueError('The installer source changed: ' + name)+ for row in (root / 'etc/passwd').read_text().splitlines():+ if 1000 <= int(row.split(':')[2]) < 65534:+ raise ValueError('Installer media has a login account.')+ shadow = next(r.split(':') for r in (root / 'etc/shadow').read_text().splitlines() if r.startswith('root:'))+ if not shadow[1].startswith(('!', '*')):+ raise ValueError('Installer media has an unlocked root account.')+ if (root / 'etc/machine-id').read_text().strip() not in ('', 'uninitialized'):+ raise ValueError('Installer media has a fixed machine identity.')+ if (root / 'var/lib/systemd/random-seed').exists() or list((root / 'etc/ssh').glob('ssh_host_*_key')):+ raise ValueError('Installer media contains machine secrets.')+ if not re.search('^SELINUX=enforcing$', (root / 'etc/selinux/config').read_text(), re.M):+ raise ValueError('Installer SELinux is not enforcing.')+ if policy_inventory(root) != identity.get('selinux'):+ raise ValueError('The live policy differs from the verified installed image policy.')+ if (data / 'policy').exists():+ raise ValueError('The live root still contains temporary policy staging files.')+ if (root / 'boot/efi/m1n1/boot.bin').exists():+ raise ValueError('Removable media must not manage m1n1.')+ enabled = root / 'etc/systemd/system/multi-user.target.wants/harness-installer.service'+ if not enabled.is_symlink() or os.readlink(enabled) != '/usr/lib/systemd/system/harness-installer.service':+ raise ValueError('The installer does not start automatically.')+ preset = root / 'usr/lib/systemd/system-preset/00-harness-installer.preset'+ if preset.read_text().strip() != 'enable harness-installer.service':+ raise ValueError('First-boot presets must keep the installer enabled.')+ for name in ('getty@', 'serial-getty@', 'sshd', 'systemd-firstboot', 'first-boot', 'initial-setup',+ 'asahi-setup-swap-firstboot', 'asahi-extras-firstboot'):+ mask = root / ('etc/systemd/system/' + name + '.service')+ if not mask.is_symlink() or os.readlink(mask) != '/dev/null':+ raise ValueError('A competing live-media service is not masked: ' + name)+ kernels = [p.name for p in (root / 'usr/lib/modules').iterdir() if p.is_dir()]+ if not kernels or any('.asahi.' not in k or '+16k' not in k for k in kernels):+ raise ValueError('The live media must use the Asahi 16 KiB kernel.')+ return kernels+++def main():+ parser = argparse.ArgumentParser(description=__doc__)+ for name in ('iso', 'identity', 'output'):+ parser.add_argument('--' + name, type=Path, required=True)+ args = parser.parse_args()+ if (os.geteuid() != 0 or not (Path('/run/.containerenv').exists() or Path('/.dockerenv').exists()) or+ not stat.S_ISREG(args.iso.lstat().st_mode)):+ parser.error('Inspect a regular ISO in an isolated Linux builder.')+ identity = json.loads(args.identity.read_text())+ if (identity.get('kind') != 'harness-asahi-installer-media' or identity.get('published') is not False or+ identity.get('release_ready') is not False):+ parser.error('Use private media identity.')+ args.output.mkdir(parents=True, exist_ok=False)+ receipt = {'status': 'running', 'started_at': time.time(), 'media': identity,+ 'scope': 'Actual ISO and compressed live root inspection; boot and hardware acceptance separate.'}+ def run(*command, separate_errors=False):+ result = subprocess.run(list(map(str, command)), check=True, text=True,+ stdout=subprocess.PIPE,+ stderr=subprocess.PIPE if separate_errors else subprocess.STDOUT, timeout=180)+ return result.stdout+ try:+ receipt['boot_layout'] = run('xorriso', '-indev', args.iso, '-report_el_torito', 'plain',+ '-report_system_area', 'plain')+ receipt['partition_table'] = json.loads(run('sfdisk', '--json', args.iso, separate_errors=True))+ partition = efi_partition(receipt['partition_table'], args.iso.stat().st_size)+ with tempfile.TemporaryDirectory(prefix='harness-media-inspect-') as tmp, ExitStack() as cleanup:+ iso, root, esp = (Path(tmp) / name for name in ('iso', 'root', 'esp'))+ iso.mkdir(); root.mkdir(); esp.mkdir()+ # Inspect these sequentially: mount refuses overlapping loop+ # mappings, and the kernel cannot mount a whole block device and+ # its partition at the same time even through a single mapping.+ with ExitStack() as efi_cleanup:+ options = f'ro,loop,offset={partition["offset"]},sizelimit={partition["bytes"]}'+ run('mount', '-t', 'vfat', '-o', options, args.iso.resolve(), esp)+ efi_cleanup.callback(run, 'umount', esp)+ receipt['uefi'] = {**partition, 'files': inspect_efi(esp)}+ run('mount', '-o', 'ro,loop', args.iso.resolve(), iso)+ cleanup.callback(run, 'umount', iso)+ receipt['iso_efi_paths'] = [str(p.relative_to(iso)) for p in iso.rglob('*')+ if p.name.lower() == 'bootaa64.efi']+ squash = [p for p in iso.rglob('*') if p.name in ('squashfs.img', 'rootfs.squashfs')]+ if len(squash) != 1:+ raise ValueError('Expected one compressed live root.')+ run('mount', '-t', 'squashfs', '-o', 'ro,loop', squash[0], root)+ cleanup.callback(run, 'umount', root)+ receipt['kernels'] = inspect_root(root, identity)+ if list(iso.rglob('boot.bin')):+ raise ValueError('The USB must not provide m1n1/boot.bin.')+ inventory = run('chroot', root, 'rpm', '-qa', '--qf', '%{NAME}\t%{VERSION}\t%{RELEASE}\t%{ARCH}\n')+ (args.output / 'packages.tsv').write_text('\n'.join(sorted(inventory.splitlines())) + '\n')+ receipt.update(status='passed', artifact={'name': args.iso.name,+ 'bytes': args.iso.stat().st_size, 'sha256': digest(args.iso)})+ except BaseException as error:+ receipt.update(status='failed', error=str(error))+ if isinstance(error, subprocess.CalledProcessError):+ receipt['command_failure'] = {'command': error.cmd, 'returncode': error.returncode,+ 'output': error.stdout, 'stderr': error.stderr}+ raise+ finally:+ receipt['completed_at'] = time.time()+ (args.output / 'receipt.json').write_text(json.dumps(receipt, indent=2) + '\n')+++if __name__ == '__main__':+ main()diff --git a/os/tests/asahi_startup_vm.py b/os/tests/asahi_startup_vm.pyindex e847af7af..21bf78667 100644--- a/os/tests/asahi_startup_vm.py+++ b/os/tests/asahi_startup_vm.py@@ -47,10 +47,12 @@ assert '/dev/vda6' in run('cryptsetup','status','harness-root')assert run('findmnt','-no','SOURCE','/boot')=='/dev/vda5'assert run('findmnt','-no','SOURCE','/boot/efi')=='/dev/vda2'assert run('systemctl','--failed','--no-pager','--no-legend')==''+boot_labels=['/boot/grub2/grub.cfg',*[str(p) for p in pathlib.Path('/boot/loader/entries').glob('*.conf')]]+assert len(boot_labels)>1subprocess.run(['matchpathcon','-V','/etc/passwd','/etc/shadow','/etc/group','/etc/gshadow',- '/etc/greetd/harness.toml','/home','/home/me'],check=True)+ '/etc/greetd/harness.toml','/home','/home/me',*boot_labels],check=True)record={'kernel':run('uname','-r'),'root':run('findmnt','-no','SOURCE','/'),- 'selinux':run('getenforce'),'failed_units':[],+ 'selinux':run('getenforce'),'boot_labels_verified':boot_labels,'failed_units':[],'journal':run('journalctl','-b','--no-pager','-o','cat','-u','systemd-cryptsetup@harness\\\\x2droot.service')}pathlib.Path('/tmp/harness-startup-installed.json').write_text(json.dumps(record,indent=2)+'\\n')'''diff --git a/os/tests/asahi_storage_guest.py b/os/tests/asahi_storage_guest.pyindex 197efd723..9aeceb063 100644--- a/os/tests/asahi_storage_guest.py+++ b/os/tests/asahi_storage_guest.py@@ -33,6 +33,27 @@ def payload():return storage.Payload('/dev/vdc', inputs['image_sha256'], inputs['source_commit'])+def install_copy(password):+ source = payload()+ verifications = 0+ verify = source.verify_pristine+ def observed():+ nonlocal verifications+ verify()+ verifications += 1+ source.verify_pristine = observed+ # Match the installer's outer read-only lifetime across nested stages.+ # Every independent retry constructs and authenticates a fresh source.+ with source.open():+ mounted = (source.root, source.home, source.boot, source.esp)+ state = storage.install(fixture.PLAN, source, password)+ assert mounted == (source.root, source.home, source.boot, source.esp)+ assert all(path.is_dir() for path in mounted)+ assert verifications == 1+ assert all(path is None for path in (source.root, source.home, source.boot, source.esp))+ return state++def guard():fixture.guard()storage.target.platform_esp = lambda: fixture.ESP@@ -71,7 +92,7 @@ def interrupt_encryption():os._exit(76)return resultstorage.run = interrupted- storage.install(fixture.PLAN, payload(), PASSWORD)+ install_copy(PASSWORD)raise AssertionError('The encryption interruption was not exercised')@@ -80,7 +101,7 @@ def interrupt_copy():assert header() == evidence['header']record = STATE.read_bytes()try:- storage.install(fixture.PLAN, payload(), 'incorrect-password')+ install_copy('incorrect-password')raise AssertionError('The retry accepted an incorrect disk password')except storage.StorageError as error:assert 'cryptsetup failed' in str(error)@@ -114,7 +135,7 @@ def interrupt_copy():secret_absent()os._exit(77)storage.run = stop_during_copy- storage.install(fixture.PLAN, payload(), PASSWORD)+ install_copy(PASSWORD)raise AssertionError('The real rsync interruption was not exercised')@@ -128,10 +149,17 @@ def verify_copy(state):with storage.work_directory() as root, ExitStack() as mounts:top = mounts.enter_context(storage.mounted(mapper, root / 'top', 'ro,rescue=nologreplay,subvolid=5'))boot = mounts.enter_context(storage.mounted('/dev/vdb5', root / 'boot', 'ro,noload'))- for src, dst, excludes in [(source.root, top / 'root', ['/boot/***', '/home/***']),- (source.home, top / 'home', []), (source.boot, boot, ['/efi/***'])]:+ separate = ('boot', 'home', 'dev', 'proc', 'sys', 'run')+ for name in separate:+ directory = top / 'root' / name+ assert directory.is_dir() and not directory.is_symlink() and not list(directory.iterdir()), name+ for src, dst, excludes, filters in [+ (source.root, top / 'root', ['/' + name + '/***' for name in separate], []),+ (source.home, top / 'home', [], []),+ (source.boot, boot, ['/efi/***'], ['--filter=-x system.*', '--filter=-x security.selinux'])]:changes = storage.run('rsync', '-aHAXnci', '--numeric-ids', '--one-file-system', '--delete',- *('--exclude=' + value for value in excludes), str(src) + '/', str(dst) + '/', timeout=180)+ *filters, *('--exclude=' + value for value in excludes),+ str(src) + '/', str(dst) + '/', timeout=180)assert not changes, changesimage = json.loads((top / 'root/usr/share/harness-os/image.json').read_text())manifest = image['session_package']['files']@@ -139,7 +167,8 @@ def verify_copy(state):assert fixture.digest(top / 'root' / name) == expected, namefor name, expected in image['first_boot']['files'].items():assert fixture.digest(top / 'root' / name) == expected['sha256'], name- return {'runtime_files_checked': len(manifest), 'source_commit': image['source_commit'],+ return {'runtime_files_checked': len(manifest), 'empty_mountpoints': list(separate),+ 'source_commit': image['source_commit'],'root_uuid': storage.probe(mapper)['UUID'], 'boot_uuid': storage.probe('/dev/vdb5')['UUID']}@@ -158,7 +187,7 @@ def finish():evidence = json.loads(EVIDENCE.read_text())assert json.loads(STATE.read_text())['phase'] == 'copying'assert header() == evidence['header']- state = storage.install(fixture.PLAN, payload(), PASSWORD)+ state = install_copy(PASSWORD)assert state['phase'] == 'copied' and header() == evidence['header']evidence['copy_verification'] = verify_copy(state)assert evidence['copy_verification']['root_uuid'] == state['root_uuid']@@ -172,7 +201,7 @@ def finish():commands.append(args)return command(*args, **kwargs)storage.run = observed- assert storage.install(fixture.PLAN, payload(), PASSWORD) == state+ assert install_copy(PASSWORD) == statestorage.run = commandassert not any(args[0] in ('rsync', 'mkfs.btrfs', 'mkfs.ext4') or args[:2] == ('cryptsetup', 'luksFormat') for args in commands)assert STATE.read_bytes() == record@@ -182,6 +211,7 @@ def finish():fixture.run('umount', fixture.ROOT)assert not Path('/dev/mapper/harness-stage-' + state['luks_uuid'].replace('-', '')).exists()evidence.update(status='passed', state=state, project_sha256=project,+ source_verified_once_per_attempt=True, source_mounts_held_through_copy=True,completed_retry_preserves_work=True, protected_data_unchanged=True,header_unchanged=True, no_secret_in_records=True, mapping_closed=True)RESULT.write_text(json.dumps(evidence, indent=2) + '\n')diff --git a/os/tests/test_asahi_install.py b/os/tests/test_asahi_install.pyindex 68dc4f635..766fd629a 100644--- a/os/tests/test_asahi_install.py+++ b/os/tests/test_asahi_install.py@@ -4,6 +4,7 @@ import cursesimport importlib.utilfrom pathlib import Pathfrom types import SimpleNamespace+import tempfileimport unittestfrom unittest.mock import Mock, patch@@ -92,6 +93,40 @@ class Installer(unittest.TestCase):install.install(Path('/unused'), {}, payload, password, lambda _: None)payload.open.assert_not_called()+ def test_source_stays_open_until_startup_finishes_or_fails(self):+ for failure in (False, True):+ with self.subTest(failure=failure), tempfile.TemporaryDirectory() as tmp:+ active = False+ @contextmanager+ def opened():+ nonlocal active+ active = True+ try:+ yield+ finally:+ active = False+ def require_source(*args, **kwargs):+ self.assertTrue(active)+ def finish(*args, **kwargs):+ require_source()+ if failure:+ raise RuntimeError('startup failed')+ return {'phase': 'complete'}+ payload = SimpleNamespace(open=opened)+ with patch.object(install, 'verify_destination', side_effect=require_source), \+ patch.object(install.storage, 'run', side_effect=require_source), \+ patch.object(install.target, 'save_plan', side_effect=require_source), \+ patch.object(install.target, 'apply_plan', side_effect=require_source), \+ patch.object(install.storage, 'install', side_effect=require_source), \+ patch.object(install.startup, 'finish', side_effect=finish):+ if failure:+ with self.assertRaisesRegex(RuntimeError, 'startup failed'):+ install.install(Path(tmp), {}, payload, 'pw', lambda _: None)+ else:+ result = install.install(Path(tmp), {}, payload, 'pw', lambda _: None)+ self.assertEqual(result, {'phase': 'complete'})+ self.assertFalse(active)+def test_lost_screen_does_not_interrupt_disk_progress(self):view = self.page()view.render = Mock(side_effect=curses.error)diff --git a/os/tests/test_asahi_media.py b/os/tests/test_asahi_media.pynew file mode 100644index 000000000..3a2898f00--- /dev/null+++ b/os/tests/test_asahi_media.py@@ -0,0 +1,260 @@+import copy+import importlib.util+import json+import os+from pathlib import Path+import struct+import sys+import tempfile+import unittest+from unittest.mock import patch+import xml.etree.ElementTree as ET++ROOT = Path(__file__).resolve().parents[2]+sys.path.insert(0, str(ROOT / 'os/tools'))+import asahi_media as build+import asahi_media_check as check++spec = importlib.util.spec_from_file_location('asahi_media_runtime', ROOT / 'os/platforms/apple-silicon/media.py')+media = importlib.util.module_from_spec(spec)+spec.loader.exec_module(media)+++class Media(unittest.TestCase):+ def setUp(self):+ tmp = tempfile.TemporaryDirectory()+ self.addCleanup(tmp.cleanup)+ self.root = Path(tmp.name)+ self.payload = self.root / 'payload.raw'+ self.payload.write_bytes(b'private image fixture')+ self.image_source = 'a' * 40+ self.producer = {'head_sha': self.image_source, 'id': 123, 'path': '.github/workflows/os-asahi-image.yml',+ 'repository': {'full_name': 'autonomous-ai/openharness'},+ 'status': 'completed', 'conclusion': 'success'}+ self.receipt = {'status': 'passed', 'image': {'kind': 'harness-asahi-image-construction',+ 'source_commit': self.image_source, 'profile': 'Harness', 'published': False, 'release_ready': False},+ 'artifact': {'bytes': self.payload.stat().st_size, 'sha256': build.digest(self.payload)}}+ self.identity = {'schema': 1, 'kind': 'harness-asahi-installer-media', 'source_commit': 'b' * 40,+ 'published': False, 'release_ready': False, 'payload': {'source_commit': self.image_source,+ 'bytes': self.payload.stat().st_size, 'sha256': build.digest(self.payload)}}+ self.manifest = self.root / 'media.json'+ self.manifest.write_text(json.dumps(self.identity))++ def test_only_successfully_inspected_same_repository_image_is_packaged(self):+ good = build.payload_identity(self.payload, self.receipt, self.producer, self.image_source)+ self.assertEqual(good['sha256'], build.digest(self.payload))+ for key, value in [('head_sha', 'c' * 40), ('conclusion', 'failure'),+ ('repository', {'full_name': 'other/repo'}), ('path', 'unrelated.yml')]:+ with self.subTest(key=key), self.assertRaises(ValueError):+ build.payload_identity(self.payload, self.receipt, {**self.producer, key: value}, self.image_source)+ bad = copy.deepcopy(self.receipt)+ bad['image']['source_commit'] = 'c' * 40+ with self.assertRaisesRegex(ValueError, 'provenance'):+ build.payload_identity(self.payload, bad, self.producer, self.image_source)++ def test_corrupt_and_symlink_payloads_cannot_be_packaged(self):+ link = self.root / 'link.raw'+ link.symlink_to(self.payload)+ with self.assertRaisesRegex(ValueError, 'differs'):+ build.payload_identity(link, self.receipt, self.producer, self.image_source)+ self.payload.write_bytes(b'X' * self.payload.stat().st_size)+ with self.assertRaisesRegex(ValueError, 'differs'):+ build.payload_identity(self.payload, self.receipt, self.producer, self.image_source)++ def test_live_manifest_rejects_missing_identity_size_and_writable_files(self):+ self.assertEqual(media.identity(self.root, os.geteuid()), self.identity)+ for key, value in [('sha256', 'bad'), ('source_commit', 'main'), ('bytes', 0)]:+ bad = copy.deepcopy(self.identity)+ bad['payload'][key] = value+ self.manifest.write_text(json.dumps(bad))+ with self.subTest(key=key), self.assertRaisesRegex(ValueError, 'manifest'):+ media.identity(self.root, os.geteuid())+ self.manifest.write_text(json.dumps(self.identity))+ self.payload.chmod(0o666)+ with self.assertRaisesRegex(ValueError, 'files have changed'):+ media.identity(self.root, os.geteuid())+ self.payload.chmod(0o644)+ original = self.root / 'original.raw'+ self.payload.rename(original)+ self.payload.symlink_to(original)+ with self.assertRaisesRegex(ValueError, 'files have changed'):+ media.identity(self.root, os.geteuid())++ def test_live_loop_is_readonly_4096_byte_and_detached_after_failed_install(self):+ calls = []+ def run(*args):+ calls.append(args)+ return '/dev/loop9' if '--find' in args else ''+ with patch.object(media, 'identity', return_value=self.identity), \+ patch.object(media.installer.storage, 'run', side_effect=run):+ with self.assertRaisesRegex(RuntimeError, 'install failed'):+ with media.payload_device(self.root) as payload:+ self.assertEqual(payload.sha256, self.identity['payload']['sha256'])+ self.assertEqual(payload.device, '/dev/loop9')+ raise RuntimeError('install failed')+ self.assertEqual(calls[0], ('losetup', '--find', '--show', '--read-only', '--partscan',+ '--sector-size', '4096', self.payload))+ self.assertEqual(calls[-1], ('losetup', '--detach', '/dev/loop9'))++ def test_device_settle_failure_also_releases_owned_loop(self):+ calls = []+ def run(*args):+ calls.append(args)+ if args[0] == 'udevadm':+ raise RuntimeError('settle failed')+ return '/dev/loop9' if '--find' in args else ''+ with patch.object(media, 'identity', return_value=self.identity), \+ patch.object(media.installer.storage, 'run', side_effect=run):+ with self.assertRaisesRegex(RuntimeError, 'settle failed'):+ with media.payload_device(self.root):+ self.fail('Must not start without the source devices')+ self.assertEqual(calls[-1], ('losetup', '--detach', '/dev/loop9'))++ def test_recipe_keeps_signed_asahi_boot_without_installed_or_desktop_profiles(self):+ includes = ('repositories/core.xml', 'repositories/asahi.xml', 'components/boot.xml',+ 'components/base.xml', 'platforms/minimal.xml', 'platforms/workstation.xml')+ (self.root / 'config.xml').write_text('<image name="Fedora-Asahi-Remix"><description><specification/>'+ '</description><preferences><release-version>44</release-version><rpm-check-signatures>true'+ '</rpm-check-signatures></preferences>' + ''.join('<include from="this://./' + p + '"/>'+ for p in includes) + '</image>')+ build.live_recipe(self.root)+ recipe = ET.parse(self.root / 'config.xml')+ self.assertEqual({n.get('from') for n in recipe.findall('include')},+ {'this://./' + p for p in includes[:3]})+ live = recipe.find('preferences/type')+ self.assertEqual(live.get('flags'), 'overlay')+ self.assertEqual(live.get('filesystem'), 'squashfs')+ self.assertEqual(live.get('hybridpersistent'), 'false')+ self.assertNotIn('rd.live.overlay.persistent', live.get('kernelcmdline'))+ self.assertIn('systemd.unit=multi-user.target', live.get('kernelcmdline'))+ self.assertEqual(recipe.findtext('preferences/rpm-check-signatures'), 'true')+ self.assertEqual(recipe.findtext('preferences/locale'), 'C')+ selected = {p.get('name') for p in recipe.findall('packages/package')}+ self.assertTrue({'dracut-kiwi-live', 'cryptsetup', 'rsync', 'grub2-efi-aa64-cdboot'} <= selected)+ self.assertFalse(selected & {'harness-os-session', 'greetd', 'chromium', 'initial-setup'})+++class FirmwareMedia(unittest.TestCase):+ def test_only_one_bounded_gpt_esp_can_be_mounted(self):+ partition = {'type': 'C12A7328-F81F-11D2-BA4B-00A0C93EC93B', 'start': 4096, 'size': 40960}+ table = {'label': 'gpt', 'unit': 'sectors', 'sectorsize': 512, 'partitions': [partition]}+ document = {'partitiontable': table}+ self.assertEqual(check.efi_partition(document, 24 * 1024**2),+ {'offset': 2 * 1024**2, 'bytes': 20 * 1024**2})+ for change in ({'label': 'dos'}, {'sectorsize': 4096}, {'partitions': []},+ {'partitions': [partition, partition]},+ {'partitions': [{**partition, 'start': 0}]},+ {'partitions': [{**partition, 'size': 100000}]}):+ with self.subTest(change=change), self.assertRaises(ValueError):+ check.efi_partition({'partitiontable': {**table, **change}}, 24 * 1024**2)++ def test_firmware_files_require_arm64_executables_and_configuration(self):+ with tempfile.TemporaryDirectory() as tmp:+ esp = Path(tmp)+ boot = esp / 'EFI/BOOT'+ boot.mkdir(parents=True)+ pe = bytearray(2048)+ pe[:2] = b'MZ'+ struct.pack_into('<I', pe, 0x3c, 128)+ pe[128:132] = b'PE\0\0'+ struct.pack_into('<H', pe, 132, 0xaa64)+ struct.pack_into('<H', pe, 152, 0x20b)+ struct.pack_into('<H', pe, 220, 10)+ shim = boot / 'BOOTAA64.EFI'+ shim.write_bytes(pe)+ (boot / 'grubaa64.efi').write_bytes(pe)+ config = boot / 'grub.cfg'+ config.write_text('configfile /boot/grub2/grub.cfg\n')+ self.assertEqual(len(check.inspect_efi(esp)), 3)+ for offset, value in ((132, 0x8664), (152, 0x10b), (220, 3)):+ bad = bytearray(pe)+ struct.pack_into('<H', bad, offset, value)+ shim.write_bytes(bad)+ with self.subTest(offset=offset), self.assertRaisesRegex(ValueError, 'ARM64'):+ check.inspect_efi(esp)+ shim.write_bytes(pe)+ config.unlink()+ with self.assertRaisesRegex(ValueError, 'Missing'):+ check.inspect_efi(esp)+++class PolicyMedia(unittest.TestCase):+ def setUp(self):+ tmp = tempfile.TemporaryDirectory()+ self.addCleanup(tmp.cleanup)+ self.root = Path(tmp.name)+ self.source = self.root / 'source'+ for name in ('etc/selinux/targeted/policy/policy.35',+ 'etc/selinux/targeted/contexts/files/file_contexts',+ 'var/lib/selinux/targeted/active/policy.kern'):+ path = self.source / name+ path.parent.mkdir(parents=True, exist_ok=True)+ path.write_bytes(name.encode())++ def test_installed_policy_and_store_are_copied_exactly(self):+ output = self.root / 'stage'+ expected = build.policy_inventory(self.source, os.geteuid())+ self.assertEqual(build.stage_policy(self.source, output, os.geteuid()), expected)+ self.assertEqual(build.policy_inventory(output, os.geteuid()), expected)+ for name in expected:+ self.assertEqual((self.source / name).read_bytes(), (output / name).read_bytes())++ def test_incomplete_or_ambiguous_compiled_policy_is_refused(self):+ binary = self.source / 'etc/selinux/targeted/policy/policy.35'+ binary.rename(binary.with_name('policy.34'))+ binary.write_bytes(b'Other policy')+ with self.assertRaisesRegex(ValueError, 'complete installed SELinux policy'):+ build.policy_inventory(self.source, os.geteuid())+ binary.unlink()+ (self.source / 'var/lib/selinux/targeted/active/policy.kern').unlink()+ with self.assertRaisesRegex(ValueError, 'complete installed SELinux policy'):+ build.policy_inventory(self.source, os.geteuid())++ def test_redirected_writable_and_special_policy_files_are_refused(self):+ binary = self.source / 'etc/selinux/targeted/policy/policy.35'+ binary.chmod(0o666)+ with self.assertRaisesRegex(ValueError, 'unexpected type, owner or mode'):+ build.policy_inventory(self.source, os.geteuid())+ binary.unlink()+ binary.symlink_to(self.root / 'outside')+ with self.assertRaisesRegex(ValueError, 'unexpected type, owner or mode'):+ build.policy_inventory(self.source, os.geteuid())+ binary.unlink()+ os.mkfifo(binary, 0o600)+ with self.assertRaisesRegex(ValueError, 'unexpected type, owner or mode'):+ build.policy_inventory(self.source, os.geteuid())++ def test_policy_parent_cannot_redirect_the_copy(self):+ parent = self.source / 'etc/selinux'+ original = parent.with_name('saved-selinux')+ parent.rename(original)+ parent.symlink_to(original)+ with self.assertRaisesRegex(ValueError, 'path was redirected'):+ build.stage_policy(self.source, self.root / 'stage', os.geteuid())+ self.assertFalse((self.root / 'stage').exists())++ def test_failed_unmount_never_deletes_the_source_tree(self):+ mount = self.root / 'mount'+ mount.mkdir()+ sentinel = mount / 'keep'+ calls = []+ def run(*args):+ calls.append(args)+ if args[0] == 'losetup':+ return '/dev/loop9'+ if args[0] == 'mount':+ sentinel.write_text('Read-only source data')+ if args[0] == 'umount':+ raise RuntimeError('Unmount failed')+ return ''+ with patch.object(build.tempfile, 'mkdtemp', return_value=str(mount)), \+ patch.object(build, 'run', side_effect=run):+ with self.assertRaisesRegex(RuntimeError, 'Unmount failed'):+ with build.image_root(self.root / 'verified.raw'):+ pass+ self.assertEqual(sentinel.read_text(), 'Read-only source data')+ self.assertFalse(any('--detach' in command for command in calls))+++if __name__ == '__main__':+ unittest.main()diff --git a/os/tests/test_asahi_storage.py b/os/tests/test_asahi_storage.pyindex ef469b496..f95172ba8 100644--- a/os/tests/test_asahi_storage.py+++ b/os/tests/test_asahi_storage.py@@ -1,10 +1,14 @@"""State ownership and non-destructive retry checks for private Asahi storage."""import copy+from contextlib import contextmanager, ExitStack+import hashlibimport importlib.utilimport jsonimport osfrom pathlib import Path+import statimport tempfile+from types import SimpleNamespaceimport unittestfrom unittest.mock import patch@@ -75,6 +79,14 @@ class StorageState(unittest.TestCase):self.assertEqual(self.path.read_bytes(), before)self.assertEqual(self.read()['phase'], 'copied')+ def test_failed_checkpoint_flush_does_not_advance_the_transaction(self):+ storage.save_state(self.path, self.state)+ with patch.object(storage.target, 'sync_directory', side_effect=OSError('flush failed')):+ with self.assertRaises(OSError):+ storage.advance(self.path, self.state, 'copying')+ self.assertEqual(self.state['phase'], 'planned')+ self.assertFalse(list(self.root.glob('.storage-*')))+def test_loose_parent_symlink_and_fifo_are_refused(self):storage.save_state(self.path, self.state)self.root.chmod(0o777)@@ -133,5 +145,106 @@ class StorageState(unittest.TestCase):storage.mountpoint(source, destination)+class PayloadLifetime(unittest.TestCase):+ def setUp(self):+ temporary = tempfile.TemporaryDirectory()+ self.addCleanup(temporary.cleanup)+ self.root = Path(temporary.name)+ self.image = self.root / 'payload.raw'+ self.image.write_bytes(b'verified installation source')+ self.payload = storage.Payload(self.image, hashlib.sha256(self.image.read_bytes()).hexdigest(), 'a' * 40)+ self.mounts = set()++ @contextmanager+ def devices(self):+ original_stat = os.stat+ def source_stat(path, *args, **kwargs):+ if path == str(self.image):+ return SimpleNamespace(st_mode=stat.S_IFBLK)+ return original_stat(path, *args, **kwargs)+ def run(*args):+ self.assertEqual(args[0], 'blockdev')+ return {'--getro': '1', '--getss': '4096',+ '--getsize64': str(self.image.stat().st_size)}[args[1]]+ @contextmanager+ def work():+ yield self.root+ @contextmanager+ def mount(device, path, options):+ self.assertIn('ro', options.split(','))+ self.mounts.add(path)+ try:+ yield path+ finally:+ self.mounts.remove(path)+ parts = [{'node': str(self.image) + str(n)} for n in (1, 2, 3)]+ with ExitStack() as stack:+ for owner, name, options in [+ (storage.os, 'stat', {'side_effect': source_stat}),+ (storage, 'run', {'side_effect': run}),+ (storage.target, 'read_table', {'return_value': {}}),+ (storage.target, 'normalize', {'return_value': {'partitions': parts}}),+ (storage.target, 'verify_gpt', {}),+ (storage, 'probe', {'side_effect': [{'TYPE': kind} for kind in ('vfat', 'ext4', 'btrfs')] * 4}),+ (storage, 'work_directory', {'side_effect': work}),+ (storage, 'mounted', {'side_effect': mount}),+ ]:+ stack.enter_context(patch.object(owner, name, **options))+ verify = stack.enter_context(patch.object(self.payload, 'verify_pristine'))+ digest = stack.enter_context(patch.object(storage.hashlib, 'file_digest', wraps=hashlib.file_digest))+ yield verify, digest++ def test_nested_stages_keep_one_verified_source_mounted(self):+ with self.devices() as (verify, digest):+ with self.payload.open():+ root = self.payload.root+ for _ in range(2):+ with self.payload.open() as current:+ self.assertIs(current, self.payload)+ self.assertEqual(current.root, root)+ self.assertEqual(len(self.mounts), 3)+ self.assertEqual(self.payload.root, root)+ self.assertEqual(digest.call_count, 1)+ verify.assert_called_once()+ self.assertFalse(self.mounts)+ self.assertIsNone(self.payload.root)+ with self.payload.open():+ self.assertEqual(digest.call_count, 2)++ def test_changed_source_is_rejected_on_next_independent_attempt(self):+ with self.devices() as (verify, digest):+ with self.payload.open():+ pass+ self.image.write_bytes(b'corrupted installation source')+ with self.assertRaisesRegex(storage.StorageError, 'checksum'):+ with self.payload.open():+ self.fail('Must not reuse a verification from a previous attempt')+ self.assertEqual(digest.call_count, 2)+ verify.assert_called_once()+ self.assertFalse(self.mounts)++ def test_failed_pristine_check_releases_mounts_and_rechecks_next_time(self):+ with self.devices() as (verify, digest):+ verify.side_effect = [storage.StorageError('Configured source'), None]+ with self.assertRaisesRegex(storage.StorageError, 'Configured'):+ with self.payload.open():+ self.fail('Invalid source must never become reusable')+ self.assertFalse(self.mounts)+ self.assertIsNone(self.payload.root)+ with self.payload.open():+ self.assertEqual(digest.call_count, 2)++ def test_stage_failure_releases_the_outer_source(self):+ with self.devices() as (_, digest):+ with self.assertRaisesRegex(RuntimeError, 'copy failed'):+ with self.payload.open():+ with self.payload.open():+ raise RuntimeError('copy failed')+ self.assertFalse(self.mounts)+ self.assertIsNone(self.payload.root)+ with self.payload.open():+ self.assertEqual(digest.call_count, 2)++if __name__ == '__main__':unittest.main()diff --git a/os/tests/test_asahi_target.py b/os/tests/test_asahi_target.pyindex 15bfac734..65f65846e 100644--- a/os/tests/test_asahi_target.py+++ b/os/tests/test_asahi_target.py@@ -188,6 +188,17 @@ class DurablePlan(unittest.TestCase):target.save_plan(self.path, self.plan)self.assertEqual(target.load_plan(self.path), self.plan)+ def test_filesystem_flush_failure_is_reported(self):+ with patch.object(target.platform, 'system', return_value='Linux'), \+ patch.object(target.ctypes, 'CDLL') as library, \+ patch.object(target.ctypes, 'get_errno', return_value=5):+ library.return_value.syncfs.return_value = -1+ with self.assertRaises(OSError) as failure:+ target.save_plan(self.path, self.plan)+ self.assertEqual(failure.exception.errno, 5)+ # Keep the published identity for a safe retry even if its flush failed.+ self.assertEqual(target.load_plan(self.path), self.plan)+def test_private_directory_and_exclusive_writer_required(self):self.root.chmod(0o755)with self.assertRaises(target.TargetError):diff --git a/os/tools/asahi_media.py b/os/tools/asahi_media.pynew file mode 100644index 000000000..07436825a--- /dev/null+++ b/os/tools/asahi_media.py@@ -0,0 +1,224 @@+#!/usr/bin/env python3+"""Prepare private ARM UEFI media around an already verified Harness image.++Only the installer userspace is built. The installed system and every shared+client remain the exact previously tested image, supplied as an offline payload.+"""+import argparse+from contextlib import contextmanager+import json+from pathlib import Path+import re+import shutil+import stat+import tempfile+import xml.etree.ElementTree as ET++from asahi_image import ROOT, digest, read_lock, run, upstream_identity++INSTALLER_FILES = ('media.py', 'install.py', 'target.py', 'storage.py', 'startup.py')+POLICY_DIRS = ('etc/selinux/targeted', 'var/lib/selinux/targeted')+PACKAGES = (+ 'fedora-release', 'systemd', 'systemd-udev', 'dbus', 'bash', 'python3',+ 'util-linux', 'cryptsetup', 'btrfs-progs', 'e2fsprogs', 'dosfstools', 'gdisk',+ 'rsync', 'policycoreutils', 'selinux-policy-targeted', 'kbd', 'ncurses-base',+ 'glibc-minimal-langpack', 'shadow-utils', 'dracut-kiwi-live', 'grub2-efi-aa64-cdboot',+ 'asahi-platform-metapackage-core', 'asahi-repos', 'tiny-dfr',+)+++def payload_identity(image, receipt, producer, source_commit):+ if (not re.fullmatch('[a-f0-9]{40}', source_commit) or+ producer.get('head_sha') != source_commit or+ producer.get('path') != '.github/workflows/os-asahi-image.yml' or+ producer.get('repository', {}).get('full_name') != 'autonomous-ai/openharness' or+ producer.get('status') != 'completed' or producer.get('conclusion') != 'success' or+ receipt.get('status') != 'passed'):+ raise ValueError('Use a successful, inspected private Asahi image producer.')+ original = receipt.get('image', {})+ if (original.get('kind') != 'harness-asahi-image-construction' or+ original.get('source_commit') != source_commit or original.get('profile') != 'Harness' or+ original.get('published') is not False or original.get('release_ready') is not False):+ raise ValueError('The source image provenance has changed.')+ artifact = receipt.get('artifact', {})+ info = image.lstat()+ if (not stat.S_ISREG(info.st_mode) or info.st_size != artifact.get('bytes') or+ not re.fullmatch('[a-f0-9]{64}', str(artifact.get('sha256'))) or+ digest(image) != artifact['sha256']):+ raise ValueError('The offline payload differs from its verified image.')+ return {'source_commit': source_commit, 'producer_run_id': producer['id'],+ 'sha256': artifact['sha256'], 'bytes': info.st_size}+++@contextmanager+def image_root(image):+ """Read the already verified payload without ever replaying its filesystem."""+ folder = Path(tempfile.mkdtemp(prefix='harness-media-policy-'))+ loop = None+ mounted = False+ try:+ loop = run('losetup', '--find', '--show', '--read-only', '--partscan',+ '--sector-size', '4096', image)+ if not re.fullmatch('/dev/loop[0-9]+', loop):+ raise ValueError('Unexpected policy source device.')+ run('mount', '-t', 'btrfs', '-o', 'ro,rescue=nologreplay,subvol=root', loop + 'p3', folder)+ mounted = True+ yield folder+ finally:+ if mounted:+ run('umount', folder)+ if loop is not None and re.fullmatch('/dev/loop[0-9]+', loop):+ run('losetup', '--detach', loop)+ # A failed unmount must never become a recursive source-image deletion.+ folder.rmdir()+++def policy_inventory(root, owner_uid=0):+ files = {}+ for prefix in POLICY_DIRS:+ base = root / prefix+ for part in [base, *base.parents]:+ if part == root:+ break+ if part.is_symlink():+ raise ValueError('The SELinux policy path was redirected.')+ for path in [base, *sorted(base.rglob('*'))]:+ info = path.lstat()+ if (info.st_uid != owner_uid or info.st_mode & 0o022 or+ not (stat.S_ISREG(info.st_mode) or stat.S_ISDIR(info.st_mode))):+ raise ValueError('The SELinux policy has an unexpected type, owner or mode.')+ if stat.S_ISREG(info.st_mode):+ files[str(path.relative_to(root))] = {'sha256': digest(path), 'bytes': info.st_size,+ 'mode': stat.S_IMODE(info.st_mode)}+ binaries = [name for name in files if re.fullmatch(r'etc/selinux/targeted/policy/policy\.\d+', name)]+ required = {'etc/selinux/targeted/contexts/files/file_contexts',+ 'var/lib/selinux/targeted/active/policy.kern'}+ if len(binaries) != 1 or not required <= files.keys():+ raise ValueError('Use a complete installed SELinux policy and module store.')+ return files+++def stage_policy(root, destination, owner_uid=0):+ # With a smaller live policy, source labels unknown to its kernel appear as+ # unlabeled_t to rsync. Carry the installed policy and its module store so+ # the live kernel can preserve every source type from the start.+ files = policy_inventory(root, owner_uid)+ for prefix in POLICY_DIRS:+ shutil.copytree(root / prefix, destination / prefix, dirs_exist_ok=True)+ if policy_inventory(destination, owner_uid) != files:+ raise ValueError('The staged SELinux policy differs from the verified image.')+ return files+++def live_recipe(description):+ """Retain pinned repositories/BootCore; give the installer its own live root."""+ path = description / 'config.xml'+ tree = ET.parse(path)+ root = tree.getroot()+ if (root.get('name') != 'Fedora-Asahi-Remix' or+ root.findtext('preferences/release-version') != '44' or+ root.findtext('preferences/rpm-check-signatures') != 'true'):+ raise ValueError('Review the changed upstream image contract.')+ root.set('name', 'Harness-Asahi-Installer')+ root.set('displayname', 'Harness')+ root.find('description/specification').text = 'Private Harness offline installation media'+ # C.UTF-8 is supplied by glibc-minimal-langpack and supports the terminal+ # wordmark without pulling every language into the installation environment.+ locale = root.find('preferences/locale')+ if locale is None:+ locale = ET.SubElement(root.find('preferences'), 'locale')+ # KIWI's schema takes "C" and setup_locale appends the UTF-8 suffix.+ locale.text = 'C'+ keep = {'this://./repositories/core.xml', 'this://./repositories/asahi.xml', 'this://./components/boot.xml'}+ for node in list(root.findall('include')):+ if node.get('from') not in keep:+ root.remove(node)+ if {n.get('from') for n in root.findall('include')} != keep:+ raise ValueError('The upstream repository or BootCore includes changed.')+ profiles = ET.SubElement(root, 'profiles')+ selected = ET.SubElement(profiles, 'profile', {'name': 'HarnessInstall', 'description': 'Install Harness'})+ ET.SubElement(selected, 'requires', {'profile': 'BootCore'})+ prefs = ET.SubElement(root, 'preferences', {'profiles': 'HarnessInstall'})+ live = ET.SubElement(prefs, 'type', {'image': 'iso', 'flags': 'overlay', 'filesystem': 'squashfs',+ 'squashfscompression': 'zstd',+ 'firmware': 'uefi', 'hybridpersistent': 'false', 'volid': 'HARNESS_INSTALL',+ 'kernelcmdline': 'console=tty0 quiet systemd.show_status=false rd.udev.log_level=3 systemd.unit=multi-user.target'})+ ET.SubElement(live, 'bootloader', {'name': 'grub2', 'console': 'none', 'timeout': '0'})+ packages = ET.SubElement(root, 'packages', {'type': 'image', 'profiles': 'HarnessInstall'})+ for name in PACKAGES:+ ET.SubElement(packages, 'package', {'name': name})+ ET.SubElement(packages, 'ignore', {'name': 'dracut-config-rescue'})+ ET.indent(tree, space=' ')+ tree.write(path, encoding='utf-8', xml_declaration=True)+++def prepare(upstream, image, receipt_path, producer_path, image_source, output, source=ROOT):+ lock = read_lock(source / 'os/platforms/apple-silicon/source.lock.json')+ entries = upstream_identity(upstream, lock)+ producer, receipt = json.loads(producer_path.read_text()), json.loads(receipt_path.read_text())+ payload = payload_identity(image, receipt, producer, image_source)+ commit = run('git', '-c', f'safe.directory={source}', '-C', source, 'rev-parse', 'HEAD')+ if run('git', '-c', f'safe.directory={source}', '-C', source, 'status', '--porcelain', '--untracked-files=normal'):+ raise ValueError('Commit the media source before building.')+ identity = {'schema': 1, 'kind': 'harness-asahi-installer-media', 'source_commit': commit,+ 'architecture': 'aarch64', 'base': lock, 'published': False, 'release_ready': False,+ 'payload': payload, 'installer': {name: digest(source / 'os/platforms/apple-silicon' / name)+ for name in INSTALLER_FILES}}+ output.mkdir(parents=True, exist_ok=False)+ description = output / 'description'+ description.mkdir()+ for name in filter(None, entries):+ origin, target = upstream / name, description / name+ if Path(name).is_absolute() or '..' in Path(name).parts or not origin.is_file():+ raise ValueError('Unexpected upstream file: ' + name)+ target.parent.mkdir(parents=True, exist_ok=True)+ if origin.is_symlink():+ link = origin.readlink()+ if link.is_absolute() or '..' in link.parts:+ raise ValueError('Unreviewed upstream link: ' + name)+ target.symlink_to(link)+ else:+ shutil.copy2(origin, target)+ live_recipe(description)+ with image_root(image) as mounted:+ identity['selinux'] = stage_policy(mounted, description / 'root/usr/share/harness-installer/policy')+ platform = source / 'os/platforms/apple-silicon'+ # Upstream config.sh configures an installed OS, including boot.bin. Its+ # bootstrap guard stays; this live-specific hook never generates that file.+ shutil.copyfile(platform / 'configure-media.sh', description / 'config.sh')+ (description / 'config.sh').chmod(0o755)+ data = description / 'root/usr/share/harness-installer'+ data.mkdir(parents=True, exist_ok=True)+ (data / 'media.json').write_text(json.dumps(identity, indent=2) + '\n')+ run('cp', '--sparse=always', '--reflink=auto', image, data / 'payload.raw')+ (data / 'payload.raw').chmod(0o444)+ for name in INSTALLER_FILES:+ dest = description / 'root/usr/lib/harness-installer' / name+ dest.parent.mkdir(parents=True, exist_ok=True)+ shutil.copyfile(platform / name, dest)+ dest.chmod(0o644)+ unit = description / 'root/usr/lib/systemd/system/harness-installer.service'+ unit.parent.mkdir(parents=True, exist_ok=True)+ shutil.copyfile(platform / unit.name, unit)+ # PID 1 reapplies vendor presets when the live overlay gets its first+ # machine identity. Explicit enablement alone is undone by Fedora's default.+ preset = description / 'root/usr/lib/systemd/system-preset/00-harness-installer.preset'+ preset.parent.mkdir(parents=True, exist_ok=True)+ shutil.copyfile(platform / preset.name, preset)+ (output / 'media-inputs.json').write_text(json.dumps(identity, indent=2) + '\n')+ return identity+++def main():+ parser = argparse.ArgumentParser(description=__doc__)+ for name in ('upstream', 'image', 'receipt', 'producer', 'output'):+ parser.add_argument('--' + name, type=Path, required=True)+ parser.add_argument('--image-source', required=True)+ args = parser.parse_args()+ identity = prepare(args.upstream.resolve(), args.image, args.receipt, args.producer,+ args.image_source, args.output.resolve())+ print(json.dumps(identity, indent=2))+++if __name__ == '__main__':+ main()