Skip to content

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.

os: enroll Asahi boot and account after encrypted copy (#951)

Dee446f2713d2ac
diff --git a/os/platforms/apple-silicon/README.md b/os/platforms/apple-silicon/README.md
index 256e26197..f117de930 100644
--- a/os/platforms/apple-silicon/README.md
+++ b/os/platforms/apple-silicon/README.md
@@ -5,8 +5,8 @@ KIWI description with a separately verified Harness session RPM. It includes
hn, OpenCode with upstream defaults, terminal panes and the optional Chromium
browser. It selects no GNOME or KDE desktop profile.
-This is image construction work, **not an installable Harness release**. Encryption,
-Fedora base-system updates/recovery and physical Apple hardware acceptance remain
+This is private installation work, **not an installable Harness release**. Installer
+media/UI, Fedora base-system updates/recovery and physical Apple hardware acceptance remain
required. The image contains no pre-created user or known login password. Never
flash this raw disk over a Mac's disk or use the PC whole-disk installer on Apple
Silicon. No public installer metadata or download feed is generated.
@@ -178,7 +178,53 @@ It compares every copied tree, checks the exact frozen runtime hashes and unchan
LUKS header, then verifies that a completed retry preserves a new project. Its
source image is attached read-only and checked again afterwards.
-This remains a private construction stage. Boot configuration, account handoff,
-installer UI/media and physical Apple acceptance are still required before this
-is a bootable Harness installation. The native test uses a public fixture password;
-its resulting disk must never be released.
+This remains a private construction stage. `startup.py` performs the subsequent
+boot configuration and account handoff; storage copying alone does not make the
+target bootable. The native test uses a public fixture password; its resulting
+disk must never be released.
+
+## Private boot and account enrollment
+
+`startup.py` follows a completed `storage.py` copy. It rechecks the verified source,
+firmware-owned target, filesystem identities and frozen runtime before configuring
+the new root, home, boot and EFI mounts. It uses a consistent `harness-root` mapping
+in crypttab and the kernel command line, then regenerates Fedora's boot entries
+and initramfs with encryption and Asahi modules.
+
+The chosen installation password also provisions `me@harness`, using the image's
+existing first-boot helper and Fedora account tools. This runs in an offline chroot
+with private runtime mounts; it cannot contact the installer's systemd or D-Bus.
+Normal account, login-policy and rollback checks still apply. Root remains locked,
+and the installed system does not ask for a second account password at first boot.
+The installed SELinux policy labels new files explicitly with `setfiles`, including
+when the maintenance kernel has SELinux disabled; account and login labels are
+verified before activation. No frozen RPM or first-boot helper file is changed.
+
+A durable EFI receipt separates boot preparation, account enrollment and activation.
+Retry resumes those stages, verifies completed files and never resets the account
+or recopies user work. New ARM EFI files are installed only after the account is
+ready; the fallback loader is written last. Foreign EFI files stop installation,
+and a missing or changed completed loader cannot be reported as success. Existing
+m1n1, vendor firmware and protected partitions remain untouched.
+
+```sh
+python3 os/tests/asahi_startup_vm.py \
+ --image /path/to/harness-asahi-private.raw --sha256 IMAGE_SHA256 \
+ --image-source FULL_IMAGE_COMMIT \
+ --fixture /path/to/verified-arm-fixture --fixture-source FULL_FIXTURE_COMMIT \
+ --output os/test-results/asahi-startup
+```
+
+The native test creates a fresh encrypted installation from the actual image,
+interrupts after boot preparation and account enrollment, reboots offline between
+stages, and retries the completed installation. It then uses UEFI and graphical
+keyboard input to reject a wrong disk password, unlock with the chosen password,
+and reach OpenCode plus two terminals without an account wizard. A second boot
+must preserve the account and a project, with enforcing SELinux and no failed
+services. The observer adds only QEMU console and keyboard configuration.
+
+This does not validate Apple's boot policy, m1n1 handoff, physical hardware or
+recovery from arbitrary power loss. The existing Apple boot chain still needs its
+platform integration and hardware acceptance. These modules are not yet packaged
+into installer media or connected to a user-facing installation screen. Never
+publish the test disk, which contains a known fixture password.
diff --git a/os/platforms/apple-silicon/startup.py b/os/platforms/apple-silicon/startup.py
new file mode 100644
index 000000000..a729d272b
--- /dev/null
+++ b/os/platforms/apple-silicon/startup.py
@@ -0,0 +1,336 @@
+"""Private Asahi boot/account enrollment after a completed encrypted image copy.
+
+This is an installer stage, not a runnable whole-disk installer. It owns only the
+persisted target's Linux filesystems and new EFI files. Apple boot policy, m1n1,
+vendor firmware, other operating systems and the frozen session RPM stay intact.
+"""
+from contextlib import contextmanager, ExitStack
+import hashlib
+import importlib.util
+import json
+import os
+from pathlib import Path
+import re
+import stat
+import tempfile
+
+spec = importlib.util.spec_from_file_location('asahi_storage', Path(__file__).with_name('storage.py'))
+storage = importlib.util.module_from_spec(spec)
+spec.loader.exec_module(storage)
+target = storage.target
+run = storage.run
+Error = storage.StorageError
+PHASES = ('planned', 'boot', 'account', 'complete')
+
+# Run from stdin inside the installed root with its own /run. Reuse the frozen
+# image's provisioning and rollback implementation; do not replace package files
+# or talk to the installer's service manager. All other preflight checks remain.
+ACCOUNT = r'''
+import os, pathlib, sys
+if os.geteuid() != 0 or os.path.samefile('/', '/proc/1/root') or pathlib.Path('/run/systemd/system').exists():
+ raise RuntimeError('Account enrollment requires the isolated offline target.')
+sys.path.insert(0, '/usr/lib/harness-os')
+import firstboot
+class OfflineFirstBoot(firstboot.FirstBoot):
+ def run(self, *command, check=True):
+ if command[:2] == ('/usr/bin/systemctl', 'is-active'):
+ if len(command) != 3 or command[2] not in ('greetd.service', 'display-manager.service'):
+ raise RuntimeError('Unexpected offline activity query.')
+ return 'inactive' # No target services can run in this offline chroot.
+ return super().run(*command, check=check)
+setup = OfflineFirstBoot()
+password = sys.stdin.read() if sys.argv[1] != 'verify' else None
+with setup.locked():
+ if sys.argv[1] == 'verify':
+ state = setup.read_state()
+ if not state or state['phase'] != 'complete':
+ raise RuntimeError('The installation account is incomplete.')
+ setup.identity(state)
+ previous = setup.load()
+ if not previous or previous['phase'] != 'enabled' or previous['user'] != 'me' or previous['uid'] != 1000:
+ raise RuntimeError('Keep the changed login configuration.')
+ setup.verify(previous)
+ setup.account('me')
+ if setup.path(firstboot.DONE).read_text() != 'me@harness\n':
+ raise RuntimeError('The completed account record has changed.')
+ else:
+ state = setup.prepare()
+ if state['phase'] == 'account':
+ setup.provision(state, password)
+ setup.finish(state)
+ password = None
+'''
+
+
+def digest(path):
+ with path.open('rb') as stream:
+ return hashlib.file_digest(stream, 'sha256').hexdigest()
+
+
+def checked(root, name):
+ relative = Path(name)
+ if relative.is_absolute() or not relative.parts or any(p in ('.', '..') for p in relative.parts):
+ raise Error('Invalid installation file path.')
+ path = root / relative
+ for parent in [path, *path.parents]:
+ if parent == root:
+ break
+ if parent.is_symlink():
+ raise Error('An installation path was redirected: ' + name)
+ return path
+
+
+def snapshot(root, name):
+ path = checked(root, name)
+ if not path.exists():
+ return None
+ info = path.stat()
+ if not stat.S_ISREG(info.st_mode) or info.st_uid != 0:
+ raise Error('An installation file has a different type or owner: ' + name)
+ return digest(path)
+
+
+def atomic(path, data, mode=0o644):
+ path.parent.mkdir(parents=True, exist_ok=True)
+ fd, temporary = tempfile.mkstemp(prefix='.harness-boot-', dir=path.parent)
+ try:
+ with os.fdopen(fd, 'wb') as stream:
+ os.fchmod(stream.fileno(), mode)
+ stream.write(data)
+ stream.flush()
+ os.fsync(stream.fileno())
+ os.replace(temporary, path)
+ directory = os.open(path.parent, os.O_RDONLY | os.O_DIRECTORY)
+ try:
+ os.fsync(directory)
+ finally:
+ os.close(directory)
+ finally:
+ if os.path.lexists(temporary):
+ os.unlink(temporary)
+
+
+def configuration(payload, state, esp_uuid):
+ """Replace source disk identities, preserving the image's other kernel options."""
+ for key in ('root_uuid', 'boot_uuid', 'luks_uuid'):
+ target.identifier(state[key])
+ target.identifier(esp_uuid)
+ cmdline = (payload.root / 'etc/kernel/cmdline').read_text().split()
+ if not any(arg.startswith('root=UUID=') for arg in cmdline):
+ raise Error('The image kernel command line has changed.')
+ options = list(dict.fromkeys(arg for arg in cmdline if not arg.startswith(('root=', 'rd.luks.'))))
+ options += ['root=UUID=' + state['root_uuid'], 'rd.luks.name=' + state['luks_uuid'] + '=harness-root']
+ if 'rootflags=subvol=root' not in options:
+ raise Error('The image no longer uses the expected root subvolume.')
+ grub = (payload.root / 'etc/default/grub').read_text()
+ grub, count = re.subn(r'(?m)^GRUB_CMDLINE_LINUX_DEFAULT="[^"]*"$',
+ 'GRUB_CMDLINE_LINUX_DEFAULT="' + ' '.join(options) + '"', grub)
+ if count != 1:
+ raise Error('The image GRUB defaults have changed.')
+ return {
+ 'etc/fstab': (f'UUID={state["root_uuid"]} / btrfs compress=zstd:1,subvol=root 0 0\n'
+ f'UUID={state["root_uuid"]} /home btrfs compress=zstd:1,subvol=home 0 0\n'
+ f'UUID={state["boot_uuid"]} /boot ext4 defaults 0 2\n'
+ f'PARTUUID={esp_uuid} /boot/efi vfat umask=0077,shortname=winnt 0 2\n'),
+ 'etc/crypttab': f'harness-root UUID={state["luks_uuid"]} none luks,x-initrd.attach\n',
+ 'etc/kernel/cmdline': ' '.join(options) + '\n',
+ 'etc/default/grub': grub,
+ 'etc/dracut.conf.d/20-harness-crypt.conf': 'add_dracutmodules+=" crypt "\n',
+ }
+
+
+def efi_files(payload, state):
+ files = {}
+ for prefix in ('EFI/BOOT', 'EFI/fedora'):
+ for path in (payload.esp / prefix).iterdir():
+ if not path.is_file() or path.is_symlink():
+ raise Error('Unexpected source EFI content.')
+ if path.name == 'bootx64.efi':
+ continue
+ name = str(path.relative_to(payload.esp))
+ data = path.read_bytes()
+ if path.name == 'grub.cfg':
+ data = (f'search --fs-uuid --set=root {state["boot_uuid"]}\n'
+ 'set prefix=($root)/grub2\nconfigfile ($root)/grub2/grub.cfg\n').encode()
+ files[name] = data
+ if not {'EFI/BOOT/BOOTAA64.EFI', 'EFI/BOOT/grubaa64.efi', 'EFI/fedora/grub.cfg'} <= files.keys():
+ raise Error('The source image is missing its ARM EFI loader.')
+ return files
+
+
+def record(path, identity):
+ if not os.path.lexists(path):
+ return {**identity, 'phase': 'planned', 'boot_files': {}}
+ fd = os.open(path, os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK)
+ with os.fdopen(fd) as stream:
+ target.private_file(stream.fileno())
+ state = json.loads(stream.read(128 * 1024))
+ if (set(state) != {*identity, 'phase', 'boot_files'} or
+ any(state.get(k) != v for k, v in identity.items()) or state['phase'] not in PHASES or
+ not isinstance(state['boot_files'], dict) or
+ any(not re.fullmatch(r'[a-f0-9]{64}', str(v)) for v in state['boot_files'].values()) or
+ (state['phase'] != 'planned' and not state['boot_files'])):
+ raise Error('The startup receipt belongs to a different installation.')
+ return state
+
+
+@contextmanager
+def mount_at(device, path, options, filesystem=None):
+ if path.is_symlink() or not path.is_dir() or os.path.ismount(path):
+ raise Error('The target mount point has changed.')
+ try:
+ run('mount', *(['-t', filesystem] if filesystem else []), '-o', options, device, path)
+ yield path
+ finally:
+ if os.path.ismount(path):
+ run('umount', '-R', path)
+
+
+@contextmanager
+def offline(root):
+ """Private /run prevents systemctl, D-Bus and password tools reaching the host."""
+ 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'))
+ # --rbind is needed for /dev/pts. The outer private namespace prevents
+ # propagation into installer services; recursive unmount releases it.
+ dev = root / 'dev'
+ if dev.is_symlink() or not dev.is_dir() or os.path.ismount(dev):
+ raise Error('The target device mount point has changed.')
+ run('mount', '--rbind', '/dev', dev)
+ mounts.callback(run, 'umount', '-R', dev)
+ run('mount', '--make-rslave', dev)
+ mounts.enter_context(mount_at('tmpfs', root / 'run', 'mode=0755,nosuid,nodev', 'tmpfs'))
+ yield
+
+
+def check_configs(root, payload, configs):
+ for name, content in configs.items():
+ wanted = hashlib.sha256(content.encode()).hexdigest()
+ if snapshot(root, name) not in (snapshot(payload.root, name), wanted):
+ raise Error('Keep the modified installation configuration: ' + name)
+
+
+def verify_runtime(root, payload):
+ manifest = 'usr/share/harness-os/image.json'
+ image = json.loads(checked(payload.root, manifest).read_text())
+ if snapshot(root, manifest) != snapshot(payload.root, manifest):
+ raise Error('The installed image identity changed.')
+ files = {**image['session_package']['files'],
+ **{k: v['sha256'] for k, v in image['first_boot']['files'].items()}}
+ for name, expected in files.items():
+ if snapshot(root, name) != expected:
+ raise Error('A frozen runtime file changed: ' + name)
+
+
+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',
+ '/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))
+ 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')
+ kernels = [p.name for p in (root / 'usr/lib/modules').iterdir() if p.is_dir() and (p / 'vmlinuz').is_file()]
+ if len(kernels) != 1 or not kernels[0].endswith('+16k'):
+ raise Error('Use the verified image with one Asahi 16 KiB kernel.')
+ initrd = '/boot/initramfs-' + kernels[0] + '.img'
+ run('chroot', root, 'dracut', '--force', '--no-hostonly', initrd, kernels[0], timeout=180)
+ modules = run('chroot', root, 'lsinitrd', '-m', initrd)
+ if 'crypt' not in modules or 'kernel-modules-asahi' not in modules:
+ raise Error('The boot image is missing encryption or Apple drivers.')
+ names = [*configs, 'boot/grub2/grub.cfg', initrd.lstrip('/')]
+ entries = sorted((root / 'boot/loader/entries').glob('*.conf'))
+ if not entries or any(not set(configs['etc/kernel/cmdline'].split()) <= set(
+ next((line.removeprefix('options ') for line in p.read_text().splitlines()
+ if line.startswith('options ')), '').split()) for p in entries):
+ raise Error('The kernel boot entries do not select the installed root.')
+ names += [str(p.relative_to(root)) for p in entries]
+ label_files(root, '/boot')
+ run('sync', '-f', root / 'boot')
+ run('sync', '-f', root)
+ return {name: snapshot(root, name) for name in names}
+
+
+def finish(plan_path, payload, password, *, progress=None):
+ """Complete an owned copy; retry without resetting an account or user work."""
+ if not isinstance(password, str) or not password or any(c in password for c in '\n\r\0'):
+ raise Error('Enter an installation password without line breaks.')
+ path = Path(plan_path)
+ progress = progress or (lambda phase: None)
+ with payload.open(), target.locked_plan(path) as (plan, fd):
+ saved = storage.read_state(path.with_name('storage.json'), plan, payload.sha256, payload.source_commit)
+ if not saved or saved['phase'] != 'copied':
+ raise Error('Complete the verified system copy before preparing startup.')
+ state_path = path.with_name('startup.json')
+ identity = {'schema': 1, 'kind': 'harness-asahi-startup', 'storage_sha256': storage.fingerprint(saved)}
+ state = record(state_path, identity)
+ boot_part, root_part = plan['additions']
+ storage.require_filesystem(storage.probe(root_part['node']), 'crypto_LUKS', saved['luks_uuid'])
+ storage.require_filesystem(storage.probe(boot_part['node']), 'ext4', saved['boot_uuid'])
+ esp = Path(run('findmnt', '-nro', 'TARGET', '--target', path))
+ files = efi_files(payload, saved)
+ for name, data in files.items():
+ allowed = [hashlib.sha256(data).hexdigest()]
+ if state['phase'] != 'complete':
+ allowed.append(None)
+ if snapshot(esp, name) not in allowed:
+ raise Error('Keep the existing EFI loader: ' + name)
+ configs = configuration(payload, saved, plan['esp_uuid'])
+ with storage.encrypted_root(root_part['node'], saved, password.encode()) as device:
+ storage.require_filesystem(storage.probe(device), 'btrfs', saved['root_uuid'])
+ with storage.work_directory() as work, ExitStack() as mounts:
+ root = mounts.enter_context(storage.mounted(device, work / 'root', 'subvol=root'))
+ mounts.enter_context(mount_at(device, checked(root, 'home'), 'subvol=home'))
+ mounts.enter_context(mount_at(boot_part['node'], checked(root, 'boot'), 'defaults'))
+ mounts.enter_context(mount_at(esp, checked(root, 'boot/efi'), 'bind'))
+ verify_runtime(root, payload)
+ if state['phase'] == 'planned':
+ # No existing human account is allowed before this transaction
+ # has durably finished boot configuration.
+ if (root / 'var/lib/harness-os/firstboot.json').exists() or any(
+ 1000 <= int(row.split(':')[2]) < 65534 for row in (root / 'etc/passwd').read_text().splitlines()):
+ raise Error('Keep the existing account; boot preparation was not recorded.')
+ check_configs(root, payload, configs)
+ storage.save_state(state_path, state)
+ with offline(root):
+ state['boot_files'] = build_boot(root, configs)
+ state['phase'] = 'boot'
+ storage.save_state(state_path, state)
+ progress('boot')
+ for name, expected in state['boot_files'].items():
+ if snapshot(root, name) != expected:
+ raise Error('Keep the changed boot configuration: ' + name)
+ if state['phase'] == 'boot':
+ with offline(root):
+ run('chroot', root, '/usr/bin/python3', '-c', ACCOUNT, 'configure', secret=password.encode(), timeout=90)
+ label_files(root, '/etc', '/var/lib/harness-os', '/home')
+ run('chroot', root, '/usr/sbin/matchpathcon', '-V', '/etc/passwd', '/etc/shadow',
+ '/etc/group', '/etc/gshadow', '/etc/greetd/harness.toml', '/home', '/home/me')
+ run('sync', '-f', root)
+ state['phase'] = 'account'
+ storage.save_state(state_path, state)
+ progress('account')
+ with offline(root):
+ run('chroot', root, '/usr/bin/python3', '-c', ACCOUNT, 'verify', timeout=60)
+ if state['phase'] == 'account':
+ # Publish the fallback entry last. Existing m1n1, vendor
+ # firmware and Apple-owned partitions are never copied over.
+ for name in sorted(files, key=lambda n: (n == 'EFI/BOOT/BOOTAA64.EFI', n)):
+ if snapshot(esp, name) != hashlib.sha256(files[name]).hexdigest():
+ atomic(checked(esp, name), files[name], mode=0o600)
+ run('sync', '-f', esp)
+ state['phase'] = 'complete'
+ storage.save_state(state_path, state)
+ progress('complete')
+ verify_runtime(root, payload)
+ target.verify_gpt(fd, int(run('blockdev', '--getsize64', plan['original']['device'])),
+ target.normalize(target.read_table(plan['original']['device'])))
+ return state
diff --git a/os/platforms/apple-silicon/storage.py b/os/platforms/apple-silicon/storage.py
index 203f4c640..91f8cb49e 100644
--- a/os/platforms/apple-silicon/storage.py
+++ b/os/platforms/apple-silicon/storage.py
@@ -170,7 +170,7 @@ class Payload:
validate_identity(image_sha256, source_commit)
self.device = str(device)
self.sha256, self.source_commit = image_sha256, source_commit
- self.root = self.home = self.boot = None
+ self.root = self.home = self.boot = self.esp = None
@contextmanager
def open(self):
@@ -194,12 +194,13 @@ class Payload:
with work_directory() as root, ExitStack() as mounts:
top = mounts.enter_context(mounted(parts[2], root / 'top', 'ro,rescue=nologreplay,subvolid=5'))
boot = mounts.enter_context(mounted(parts[1], root / 'boot', 'ro,noload'))
+ self.esp = mounts.enter_context(mounted(parts[0], root / 'esp', 'ro'))
self.root, self.home, self.boot = top / 'root', top / 'home', boot
try:
self.verify_pristine()
yield self
finally:
- self.root = self.home = self.boot = None
+ self.root = self.home = self.boot = self.esp = None
def verify_pristine(self):
image = json.loads((self.root / 'usr/share/harness-os/image.json').read_text())
diff --git a/os/tests/asahi_startup_guest.py b/os/tests/asahi_startup_guest.py
new file mode 100644
index 000000000..c996aebde
--- /dev/null
+++ b/os/tests/asahi_startup_guest.py
@@ -0,0 +1,96 @@
+#!/usr/bin/env python3
+"""VM-only fresh installed boot fixture with interrupted boot/account enrollment."""
+import importlib.util
+import json
+import os
+from pathlib import Path
+import sys
+
+def module(name, path):
+ spec = importlib.util.spec_from_file_location(name, path)
+ value = importlib.util.module_from_spec(spec)
+ spec.loader.exec_module(value)
+ return value
+
+startup = module('startup', '/var/tmp/startup.py')
+fixture = module('fixture', '/var/tmp/harness-target-guest.py')
+storage = startup.storage
+PASSWORD = 'firstboot-local-42'
+RESULT = Path('/var/tmp/harness-startup-result.json')
+
+
+def main():
+ action = sys.argv[1]
+ fixture.guard()
+ startup.target.platform_esp = lambda: fixture.ESP
+ inputs = json.loads(Path('/var/tmp/harness-startup-input.json').read_text())
+ assert fixture.run('lsblk', '-ndo', 'SERIAL', '/dev/vdc') == 'HARNESS_PAYLOAD'
+ payload = storage.Payload('/dev/vdc', inputs['image_sha256'], inputs['source_commit'])
+ fixture.mount()
+ try:
+ baseline = json.loads(fixture.BASELINE.read_text())
+ expected = {k: v for k, v in baseline.items() if k not in ('table', 'started_at')}
+ if action == 'prepare':
+ startup.target.apply_plan(fixture.PLAN)
+ storage.install(fixture.PLAN, payload, PASSWORD)
+ saved = storage.read_state(fixture.PLAN.with_name('storage.json'), startup.target.load_plan(fixture.PLAN),
+ payload.sha256, payload.source_commit)
+ # QEMU has serial output and virtio input, unlike the physical Apple
+ # firmware console/keyboard. Only these observer hardware additions differ.
+ configuration = startup.configuration
+ def observer_configuration(*args):
+ config = configuration(*args)
+ consoles = ' console=ttyAMA0 console=tty0'
+ config['etc/kernel/cmdline'] = config['etc/kernel/cmdline'].strip() + consoles + '\n'
+ config['etc/default/grub'] = config['etc/default/grub'].replace(
+ '=harness-root"', '=harness-root' + consoles + '"')
+ config['etc/dracut.conf.d/20-harness-crypt.conf'] += 'force_drivers+=" virtio_input "\n'
+ return config
+ startup.configuration = observer_configuration
+ def interrupted(phase):
+ if (action, phase) in (('prepare', 'boot'), ('account', 'account')):
+ os._exit(78 if phase == 'boot' else 79)
+ if action == 'prepare':
+ foreign = fixture.ROOT / 'EFI/BOOT/BOOTAA64.EFI'
+ foreign.parent.mkdir(parents=True)
+ foreign.write_bytes(b'Keep the existing loader')
+ try:
+ startup.finish(fixture.PLAN, payload, PASSWORD)
+ raise AssertionError('Overwrote a foreign EFI boot loader')
+ except startup.Error as error:
+ assert 'existing EFI loader' in str(error)
+ assert foreign.read_bytes() == b'Keep the existing loader'
+ foreign.unlink()
+ state = startup.finish(fixture.PLAN, payload, PASSWORD, progress=interrupted)
+ assert action == 'finish' and state['phase'] == 'complete'
+ assert fixture.protected() == expected
+ plan = startup.target.load_plan(fixture.PLAN)
+ assert not startup.target.remaining(plan, startup.target.read_table(fixture.DISK))
+ # Re-enter the completed installer before first boot. The account's
+ # password and home must stay unchanged, and no recopy may run.
+ receipt_before = state.copy()
+ assert storage.install(fixture.PLAN, payload, PASSWORD)['phase'] == 'copied'
+ assert startup.finish(fixture.PLAN, payload, PASSWORD) == receipt_before
+ assert fixture.protected() == expected
+ loader = fixture.ROOT / 'EFI/BOOT/BOOTAA64.EFI'
+ backup = fixture.PLAN.with_name('loader-test-backup')
+ loader.rename(backup)
+ try:
+ try:
+ startup.finish(fixture.PLAN, payload, PASSWORD)
+ raise AssertionError('Reported a complete install with a missing EFI loader')
+ except startup.Error as error:
+ assert 'existing EFI loader' in str(error)
+ assert not loader.exists()
+ finally:
+ backup.rename(loader)
+ RESULT.write_text(json.dumps({'status': 'passed', 'startup': state, 'storage': saved,
+ 'protected_data_unchanged': True, 'completed_retry': True, 'missing_loader_refused': True,
+ 'observer_arguments': 'console=ttyAMA0 console=tty0', 'observer_driver': 'virtio_input'}, indent=2) + '\n')
+ finally:
+ if fixture.ROOT.is_mount():
+ fixture.run('umount', fixture.ROOT)
+
+
+if __name__ == '__main__':
+ main()
diff --git a/os/tests/asahi_startup_vm.py b/os/tests/asahi_startup_vm.py
new file mode 100644
index 000000000..e847af7af
--- /dev/null
+++ b/os/tests/asahi_startup_vm.py
@@ -0,0 +1,174 @@
+#!/usr/bin/env python3
+"""Fresh encrypted install -> account -> UEFI unlock -> Harness -> second boot."""
+import argparse
+import json
+from pathlib import Path
+import platform
+import re
+import subprocess
+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, evidence
+from asahi_target_vm import guest as target_guest
+from session_vm import put
+
+ROOT = Path(__file__).resolve().parents[1]
+
+
+def enroll(vm, action, image_hash, source):
+ for name in ('startup.py', 'storage.py', 'target.py'):
+ put(vm, '/var/tmp/' + name, (ROOT / 'platforms/apple-silicon' / name).read_text())
+ put(vm, '/var/tmp/harness-target-guest.py', Path(__file__).with_name('asahi_target_guest.py').read_text())
+ put(vm, '/var/tmp/harness-startup-guest.py', Path(__file__).with_name('asahi_startup_guest.py').read_text())
+ put(vm, '/var/tmp/harness-startup-input.json', json.dumps({'image_sha256': image_hash, 'source_commit': source}))
+ output, code = vm.command('unshare --mount --propagation private python3 /var/tmp/harness-startup-guest.py ' + action,
+ timeout=600, check=False)
+ expected = {'prepare': 78, 'account': 79, 'finish': 0}[action]
+ if code != expected:
+ raise RuntimeError(f'{action} exited {code}, expected {expected}: {output[-7000:]}')
+ if code:
+ return {'exit_code': code, 'phase': action}
+ raw = vm.read_file('/var/tmp/harness-startup-result.json')
+ (vm.folder / 'result.json').write_bytes(raw)
+ return json.loads(raw)
+
+
+def installed(vm):
+ script = '''import json,pathlib,subprocess
+def run(*args): return subprocess.check_output(args,text=True).strip()
+assert run('getenforce')=='Enforcing'
+assert run('getconf','PAGESIZE')=='16384'
+assert run('findmnt','-no','SOURCE','/')=='/dev/mapper/harness-root[/root]'
+assert run('findmnt','-no','SOURCE','/home')=='/dev/mapper/harness-root[/home]'
+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')==''
+subprocess.run(['matchpathcon','-V','/etc/passwd','/etc/shadow','/etc/group','/etc/gshadow',
+ '/etc/greetd/harness.toml','/home','/home/me'],check=True)
+record={'kernel':run('uname','-r'),'root':run('findmnt','-no','SOURCE','/'),
+ 'selinux':run('getenforce'),'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')
+'''
+ put(vm, '/tmp/harness-startup-installed.py', script)
+ vm.command('python3 /tmp/harness-startup-installed.py')
+ raw = vm.read_file('/tmp/harness-startup-installed.json')
+ (vm.folder / 'installed.json').write_bytes(raw)
+ return json.loads(raw)
+
+
+def main():
+ parser = argparse.ArgumentParser(description=__doc__)
+ for name in ('fixture', 'image', 'output'):
+ parser.add_argument('--' + name, type=Path, required=True)
+ for name in ('fixture-source', 'image-source', 'sha256'):
+ parser.add_argument('--' + name, required=True)
+ args = parser.parse_args()
+ if (platform.system(), platform.machine()) != ('Darwin', 'arm64'):
+ parser.error('Use the native Apple Silicon observer.')
+ if not re.fullmatch('[a-f0-9]{40}', args.image_source) or not re.fullmatch('[a-f0-9]{64}', args.sha256):
+ parser.error('Use full immutable image and source identities.')
+ source = args.image.resolve()
+ if args.image.is_symlink() or not source.is_file() or digest(source) != args.sha256:
+ parser.error('Use the verified regular source image.')
+ 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 handle:
+ handle.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 ('target.py', 'storage.py', 'startup.py')]
+ paths += [Path(__file__).with_name(name) for name in ('asahi_startup_vm.py', 'asahi_startup_guest.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,
+ 'image_sha256': args.sha256, 'image_source': args.image_source, 'fixture_source': args.fixture_source,
+ 'fixture_sha256': digest(args.fixture / 'manifest.json'),
+ 'inputs': {str(p.relative_to(ROOT)): digest(p) for p in paths}, 'shutdowns': [],
+ 'limitations': ['QEMU UEFI, not Apple firmware/m1n1 handoff or physical hardware acceptance.',
+ 'Known public password; never publish this installed test disk.',
+ 'Observer adds its serial/graphical console and virtio input driver.',
+ 'Existing protected macOS/recovery partitions contain sentinel data.']}
+ vm = None
+ try:
+ vm = MaintenanceVM(output / 'prepare', maintenance, args.fixture / 'Image', disk, source=source)
+ vm.start()
+ vm.command('dnf5 install -y --setopt=install_weak_deps=False --setopt=gpgcheck=True gdisk dosfstools cryptsetup btrfs-progs e2fsprogs rsync', timeout=180)
+ receipt['initialize'] = target_guest(vm, 'initialize', output)
+ receipt['boot_interruption'] = enroll(vm, 'prepare', args.sha256, args.image_source)
+ receipt['shutdowns'].append(vm.poweroff())
+ vm.close()
+ vm = MaintenanceVM(output / 'account', maintenance, args.fixture / 'Image', disk, source=source)
+ vm.start(offline=True)
+ receipt['account_interruption'] = enroll(vm, 'account', args.sha256, args.image_source)
+ receipt['shutdowns'].append(vm.poweroff())
+ vm.close()
+ vm = MaintenanceVM(output / 'finish', maintenance, args.fixture / 'Image', disk, source=source)
+ vm.start(offline=True)
+ receipt['enrollment'] = enroll(vm, 'finish', args.sha256, args.image_source)
+ receipt['shutdowns'].append(vm.poweroff())
+ vm.close()
+ print('Encrypted copy, boot/account enrollment and offline interrupted resumes passed.', flush=True)
+ vm = ImageVM(output / 'first-boot', 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['first_boot'] = installed(vm)
+ assert 'Failed to activate with specified passphrase.' in receipt['first_boot']['journal']
+ first_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')
+ vm.user('mkdir -p ~/projects/startup-check && printf %s preserved > ~/projects/startup-check/result.txt')
+ assert vm.read_file('/home/me/projects/startup-check/result.txt') == b'preserved'
+ receipt['shutdowns'].append(vm.poweroff())
+ vm.close()
+ vm = ImageVM(output / 'second-boot', disk, None)
+ vm.start()
+ unlock(vm)
+ # Observe the real terminal before sending hn commands. The CLI can
+ # start a headless server itself; probing it before hn-screen starts
+ # changes the startup under test instead of observing restored panes.
+ vm.frame('04-restored-workspace', 'me@harness', seconds=90)
+ vm.authenticate()
+ vm.wait_user('systemctl --user is-active --quiet hn-screen')
+ receipt['second_boot'] = installed(vm)
+ assert evidence(vm, vm.folder) == first_account
+ assert vm.read_file('/home/me/projects/startup-check/result.txt') == b'preserved'
+ vm.wait_user('test "$(hn list-panes -F "#{pane_id}" | wc -l)" -eq 3')
+ receipt['same_account_and_project'] = True
+ receipt['shutdowns'].append(vm.poweroff())
+ receipt['status'] = 'passed'
+ print('Fresh installed root unlocks straight into Harness and survives a second boot.', flush=True)
+ except BaseException as error:
+ receipt.update(status='failed', error=str(error))
+ if vm:
+ try:
+ vm.screenshot('failure')
+ if vm.shell_ready:
+ logs, _ = vm.command('journalctl -b --no-pager -n 100 _UID=1000; '
+ 'systemctl --failed --no-pager', check=False, timeout=30)
+ (vm.folder / 'diagnosis.txt').write_text(logs)
+ except Exception as diagnostic:
+ receipt['diagnostic_error'] = str(diagnostic)
+ raise
+ finally:
+ try:
+ if vm:
+ vm.close()
+ finally:
+ receipt.update(finished_at=time.time(), original_source_unchanged=digest(source) == args.sha256)
+ if not receipt['original_source_unchanged']:
+ receipt.update(status='failed', error='The source image changed.')
+ (output / 'receipt.json').write_text(json.dumps(receipt, indent=2) + '\n')
+ if receipt['status'] != 'passed':
+ raise SystemExit(1)
+
+
+if __name__ == '__main__':
+ main()
diff --git a/os/tests/asahi_target_guest.py b/os/tests/asahi_target_guest.py
index 81b954303..e79ae1a03 100644
--- a/os/tests/asahi_target_guest.py
+++ b/os/tests/asahi_target_guest.py
@@ -83,7 +83,7 @@ def initialize():
run('sgdisk', '--zap-all', DISK)
run('sgdisk', '--clear',
f'--new=1:{MiB}:{33 * MiB - 1}', f'--typecode=1:{APFS}', '--change-name=1:macOS fixture',
- f'--new=2:{64 * MiB}:{320 * MiB - 1}', f'--typecode=2:{target.EFI}', f'--partition-guid=2:{ESP}', '--change-name=2:Asahi',
+ f'--new=2:{64 * MiB}:{576 * MiB - 1}', f'--typecode=2:{target.EFI}', f'--partition-guid=2:{ESP}', '--change-name=2:Asahi',
f'--new=3:{20 * GiB}:{20 * GiB + 32 * MiB - 1}', f'--typecode=3:{RECOVERY}', '--change-name=3:Recovery fixture',
f'--new=4:{22 * GiB}:{22 * GiB + 64 * MiB - 1}', f'--typecode=4:{target.EFI}', '--change-name=4:Other OS fixture', DISK)
run('udevadm', 'settle', '--timeout=10')
@@ -94,7 +94,9 @@ def initialize():
os.pwrite(handle.fileno(), bytes([number]) * 65536, 0)
os.pwrite(handle.fileno(), bytes([number + 32]) * 65536, size - 65536)
os.fsync(handle.fileno())
- run('mkfs.vfat', '-F', '32', '-n', 'ASAHI_TEST', DISK + '2')
+ # FAT32 needs at least 65525 clusters. A 256 MiB/4 KiB fixture
+ # mounts in Linux but is undersized for FAT32 and rejected by UEFI.
+ run('mkfs.vfat', '-F', '32', '-s', '1', '-n', 'ASAHI_TEST', DISK + '2')
mount()
for name in FILES:
path = ROOT / name
diff --git a/os/tests/test_asahi_startup.py b/os/tests/test_asahi_startup.py
new file mode 100644
index 000000000..dd2c00f28
--- /dev/null
+++ b/os/tests/test_asahi_startup.py
@@ -0,0 +1,86 @@
+"""Startup identity/configuration boundaries; real boot is tested in the ARM VM."""
+import importlib.util
+import json
+from pathlib import Path
+import tempfile
+from types import SimpleNamespace
+import unittest
+from unittest.mock import patch
+
+spec = importlib.util.spec_from_file_location('asahi_startup', Path(__file__).resolve().parents[1] /
+ 'platforms/apple-silicon/startup.py')
+startup = importlib.util.module_from_spec(spec)
+spec.loader.exec_module(startup)
+
+
+class Startup(unittest.TestCase):
+ def setUp(self):
+ self.temporary = tempfile.TemporaryDirectory()
+ self.addCleanup(self.temporary.cleanup)
+ self.root = Path(self.temporary.name)
+ (self.root / 'etc/kernel').mkdir(parents=True)
+ (self.root / 'etc/default').mkdir()
+ (self.root / 'etc/kernel/cmdline').write_text('root=UUID=old ro rootflags=subvol=root quiet quiet\n')
+ (self.root / 'etc/default/grub').write_text('GRUB_CMDLINE_LINUX_DEFAULT="rootflags=subvol=root"\nGRUB_TIMEOUT=5\n')
+ self.payload = SimpleNamespace(root=self.root, esp=self.root / 'esp')
+ self.state = {k: f'00000000-0000-4000-8000-00000000000{i}'
+ for i, k in enumerate(('root_uuid', 'boot_uuid', 'luks_uuid'), 1)}
+ self.esp_uuid = '00000000-0000-4000-8000-000000000004'
+
+ def test_disk_references_are_replaced_and_other_options_preserved(self):
+ result = startup.configuration(self.payload, self.state, self.esp_uuid)
+ self.assertNotIn('UUID=old', '\n'.join(result.values()))
+ self.assertEqual(result['etc/kernel/cmdline'].split().count('quiet'), 1)
+ self.assertIn('rd.luks.name=' + self.state['luks_uuid'] + '=harness-root', result['etc/kernel/cmdline'])
+ self.assertIn('PARTUUID=' + self.esp_uuid, result['etc/fstab'])
+ self.assertIn('crypt', result['etc/dracut.conf.d/20-harness-crypt.conf'])
+ self.assertIn('GRUB_TIMEOUT=5', result['etc/default/grub'])
+ self.assertNotIn('console=', result['etc/kernel/cmdline'])
+
+ def test_changed_source_layout_stops(self):
+ (self.root / 'etc/kernel/cmdline').write_text('root=/dev/vda3 quiet\n')
+ with self.assertRaises(startup.Error):
+ startup.configuration(self.payload, self.state, self.esp_uuid)
+
+ def test_invalid_identifier_stops_before_config_generation(self):
+ self.state['root_uuid'] = 'bad\nUUID=another'
+ with self.assertRaises(ValueError):
+ startup.configuration(self.payload, self.state, self.esp_uuid)
+
+ def test_no_redirected_or_escaping_paths(self):
+ (self.root / 'redirect').symlink_to(self.root / 'etc', target_is_directory=True)
+ for name in ('redirect/fstab', '../fstab', '/etc/fstab'):
+ with self.subTest(name=name), self.assertRaises(startup.Error):
+ startup.checked(self.root, name)
+
+ def test_receipt_is_bound_to_storage_and_boot_evidence(self):
+ path = self.root / 'startup.json'
+ identity = {'schema': 1, 'kind': 'harness-asahi-startup', 'storage_sha256': 'a' * 64}
+ state = startup.record(path, identity)
+ self.assertEqual(state['phase'], 'planned')
+ for change in ({'storage_sha256': 'b' * 64}, {'phase': 'complete'}, {'extra': True}):
+ path.write_text(json.dumps({**state, **change}))
+ path.chmod(0o600)
+ with patch.object(startup.target, 'private_file'), self.assertRaises(startup.Error):
+ startup.record(path, identity)
+
+ def test_efi_copy_is_arm_only_and_does_not_include_m1n1(self):
+ for name in ('EFI/BOOT/BOOTAA64.EFI', 'EFI/BOOT/grubaa64.efi', 'EFI/BOOT/bootx64.efi',
+ 'EFI/fedora/grub.cfg', 'm1n1/boot.bin'):
+ p = self.payload.esp / name
+ p.parent.mkdir(parents=True, exist_ok=True)
+ p.write_bytes(b'fixture')
+ files = startup.efi_files(self.payload, self.state)
+ self.assertNotIn('m1n1/boot.bin', files)
+ self.assertNotIn('EFI/BOOT/bootx64.efi', files)
+ self.assertIn(self.state['boot_uuid'].encode(), files['EFI/fedora/grub.cfg'])
+ self.assertIn(b'configfile ', files['EFI/fedora/grub.cfg'])
+
+ def test_empty_or_multiline_password_refuses_without_opening_payload(self):
+ for password in ('', 'a\nb', 'a\0b', 'a\rb'):
+ with self.subTest(password=repr(password)), self.assertRaises(startup.Error):
+ startup.finish(Path('/unused'), None, password)
+
+
+if __name__ == '__main__':
+ unittest.main()