"""Prepare the audited initramfs and X11 sidecar consumed by deploy-iso.sh.""" import argparse import hashlib import os from pathlib import Path import re import subprocess import sys import tempfile def assemble(args): output = args.output if output.exists() or output.is_symlink(): raise FileExistsError(output) arch = "amd64" if args.arch == "x86_64" else "arm64" if not re.fullmatch(r"[0-9a-f]{40}", args.commit): raise ValueError("expected the full Vinix source commit") if args.release and not re.fullmatch(r"[A-Za-z0-9][A-Za-z0-9._-]*", args.release): raise ValueError("invalid Vinix release tag") programs = ["usr/bin/vinix-desktop", "usr/sbin/vinix-account", "usr/sbin/vinix-authd", "usr/sbin/vinix-session", "usr/sbin/vinix-system"] machine = 62 if args.arch == "x86_64" else 183 for program in programs: path = args.native / program if path.is_symlink() or not path.is_file() or not os.access(path, os.X_OK): raise ValueError(f"native layer is missing executable /{program}") with path.open("rb") as stream: header = stream.read(20) if header[:6] != b"\x7fELF\x02\x01" or int.from_bytes(header[18:20], "little") != machine: raise ValueError(f"native layer executable has the wrong ELF architecture: /{program}") tools = Path(__file__).resolve().parent with tempfile.TemporaryDirectory(prefix=".ports-inputs-", dir=output.parent) as temporary: work = Path(temporary) bundle = work / "bundle" stage = work / "image" bundle_command = [sys.executable, tools / "bundle-x11.py", args.arch, args.desktop, args.graphics, args.auditor, bundle] if args.prepared_x11: bundle_command.extend(["--prepared", args.prepared_x11]) subprocess.run(bundle_command, check=True) command = [sys.executable, tools / "stage-image.py", args.arch, args.desktop, args.repo, stage, args.auditor, args.fingerprint, "--native-layer", args.native, "--initramfs", f"--release={args.release}", "--seed-client", args.graphics] if args.archive_coverage: command.extend(["--archive-coverage", args.archive_coverage]) subprocess.run(command, check=True) (stage / "initramfs.tar").rename(bundle / "initramfs.tar") info = bundle / "build-info" info.write_text(f"commit={args.commit}\nrelease={args.release}\narch={arch}\n") assets = sorted(bundle.iterdir()) with (bundle / "SHA256SUMS").open("x") as manifest: for path in assets: with path.open("rb") as stream: digest = hashlib.file_digest(stream, "sha256").hexdigest() manifest.write(f"{digest} {path.name}\n") bundle.rename(output) if __name__ == "__main__": parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("arch", choices=("x86_64", "aarch64")) for name in ("repo", "desktop", "graphics", "native", "auditor", "output"): parser.add_argument(name, type=lambda path: Path(path).absolute()) for name in ("fingerprint", "commit", "release"): parser.add_argument(name) parser.add_argument("--archive-coverage", type=Path, help="verified archive inventory for the signed seed") parser.add_argument("--prepared-x11", type=Path, help="directory of the previously assembled X11 sidecar") assemble(parser.parse_args())