| 1 | """Prepare the audited initramfs and X11 sidecar consumed by deploy-iso.sh.""" |
| 2 | |
| 3 | import argparse |
| 4 | import hashlib |
| 5 | import os |
| 6 | from pathlib import Path |
| 7 | import re |
| 8 | import subprocess |
| 9 | import sys |
| 10 | import tempfile |
| 11 | |
| 12 | |
| 13 | def assemble(args): |
| 14 | output = args.output |
| 15 | if output.exists() or output.is_symlink(): |
| 16 | raise FileExistsError(output) |
| 17 | arch = "amd64" if args.arch == "x86_64" else "arm64" |
| 18 | if not re.fullmatch(r"[0-9a-f]{40}", args.commit): |
| 19 | raise ValueError("expected the full Vinix source commit") |
| 20 | if args.release and not re.fullmatch(r"[A-Za-z0-9][A-Za-z0-9._-]*", args.release): |
| 21 | raise ValueError("invalid Vinix release tag") |
| 22 | programs = ["usr/bin/vinix-desktop", "usr/sbin/vinix-account", "usr/sbin/vinix-authd", |
| 23 | "usr/sbin/vinix-session", "usr/sbin/vinix-system"] |
| 24 | machine = 62 if args.arch == "x86_64" else 183 |
| 25 | for program in programs: |
| 26 | path = args.native / program |
| 27 | if path.is_symlink() or not path.is_file() or not os.access(path, os.X_OK): |
| 28 | raise ValueError(f"native layer is missing executable /{program}") |
| 29 | with path.open("rb") as stream: |
| 30 | header = stream.read(20) |
| 31 | if header[:6] != b"\x7fELF\x02\x01" or int.from_bytes(header[18:20], "little") != machine: |
| 32 | raise ValueError(f"native layer executable has the wrong ELF architecture: /{program}") |
| 33 | |
| 34 | tools = Path(__file__).resolve().parent |
| 35 | with tempfile.TemporaryDirectory(prefix=".ports-inputs-", dir=output.parent) as temporary: |
| 36 | work = Path(temporary) |
| 37 | bundle = work / "bundle" |
| 38 | stage = work / "image" |
| 39 | subprocess.run([sys.executable, tools / "bundle-x11.py", args.arch, args.desktop, |
| 40 | args.graphics, args.auditor, bundle], check=True) |
| 41 | command = [sys.executable, tools / "stage-image.py", args.arch, args.desktop, |
| 42 | args.repo, stage, args.auditor, args.fingerprint, "--native-layer", |
| 43 | args.native, "--initramfs", f"--release={args.release}", |
| 44 | "--seed-client", args.graphics] |
| 45 | if args.archive_coverage: |
| 46 | command.extend(["--archive-coverage", args.archive_coverage]) |
| 47 | subprocess.run(command, check=True) |
| 48 | (stage / "initramfs.tar").rename(bundle / "initramfs.tar") |
| 49 | info = bundle / "build-info" |
| 50 | info.write_text(f"commit={args.commit}\nrelease={args.release}\narch={arch}\n") |
| 51 | assets = sorted(bundle.iterdir()) |
| 52 | with (bundle / "SHA256SUMS").open("x") as manifest: |
| 53 | for path in assets: |
| 54 | with path.open("rb") as stream: |
| 55 | digest = hashlib.file_digest(stream, "sha256").hexdigest() |
| 56 | manifest.write(f"{digest} {path.name}\n") |
| 57 | bundle.rename(output) |
| 58 | |
| 59 | |
| 60 | if __name__ == "__main__": |
| 61 | parser = argparse.ArgumentParser(description=__doc__) |
| 62 | parser.add_argument("arch", choices=("x86_64", "aarch64")) |
| 63 | for name in ("repo", "desktop", "graphics", "native", "auditor", "output"): |
| 64 | parser.add_argument(name, type=lambda path: Path(path).absolute()) |
| 65 | for name in ("fingerprint", "commit", "release"): |
| 66 | parser.add_argument(name) |
| 67 | parser.add_argument("--archive-coverage", type=Path, help="verified archive inventory for the signed seed") |
| 68 | assemble(parser.parse_args()) |
| 69 | |