Files
makronix/tools/vmctl.py
T
lotherk 980c4113a3 vmbuild: CDE built on a NetBSD host this tree produces, and in the set.
wm/cde cannot be cross-built from Linux (mk/tools/replace.mk:142 refuses
awk:run when OPSYS != NATIVE_OPSYS), so it is built on a live image from src/
under qemu: tools/vm-boot.cfg puts the console on com0, tools/vmctl.py drives
it, and tools/vmbuild/ holds the stages that bootstrap pkgsrc there with
--prefer-native=yes and build the recipe unchanged.

Two settings, both configuration, both scoped to wm/cde in the build host's
mk.conf: FORCE_C_STD=gnu11 (autoconf 2.72 picks C23, where CDE's 'int f();'
means 'int f(void)') and the C wrapper's append list for the five GCC 14
implicit-declaration errors, the only lever that reaches the ksh93 subtree.

stage6 walks the runtime closure over the @pkgdep lines in +CONTENTS, not over
+DEPENDS (that file is in the binary package, not in the registration
database), and the closure is unpacked into the sysroot rather than pkg_add'ed
so CDE's /etc/dt paths do not land on the build host.  10837 set entries,
checkflist green.
2026-11-03 13:52:27 +00:00

127 lines
4.0 KiB
Python

#!/usr/bin/env python3
# vmctl.py -- drive a NetBSD virtual machine's serial console.
#
# Why this exists: the CDE build needs a NetBSD build host (pkgsrc's
# mk/tools/replace.mk refuses "USE_TOOLS+=awk:run" whenever OPSYS !=
# NATIVE_OPSYS, which is what a cross-OS build from Linux is). The host is a
# NetBSD amd64 image built by this tree's own build.sh, run under qemu with
# KVM, and the only way in is the serial console.
#
# Usage:
# vmctl.py --sock PATH --log PATH [--do ACTION ...]
#
# Actions, in order:
# wait:SECONDS:REGEX read the console until REGEX matches (SECONDS=0: no limit)
# send:TEXT send TEXT; \n sends Enter, \\n a literal backslash-n
# sleep:SECONDS wait
# mark:TEXT write TEXT to the log as a step marker
#
# The console transcript is appended to --log, so the evidence stays in the
# tree the way AGENTS.md asks for it.
import argparse
import os
import re
import select
import socket
import sys
import time
CHUNK = 4096
class Console:
def __init__(self, sock_path, log_path, quiet=False):
self.log = open(log_path, "ab", buffering=0)
self.quiet = quiet
deadline = time.time() + 60
while True:
try:
self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
self.sock.connect(sock_path)
break
except OSError:
self.sock.close()
if time.time() > deadline:
raise SystemExit("vmctl: cannot connect to %s" % sock_path)
time.sleep(0.5)
self.buf = b""
def _pump(self, deadline):
wait = None
if deadline:
wait = max(0.0, deadline - time.time())
r, _, _ = select.select([self.sock], [], [], wait if wait is not None else 1.0)
if not r:
if deadline and time.time() >= deadline:
return False
return True
try:
data = self.sock.recv(CHUNK)
except OSError:
raise SystemExit("vmctl: console closed")
if not data:
raise SystemExit("vmctl: console EOF")
self.log.write(data)
self.buf += data
if not self.quiet:
sys.stdout.write(data.decode("utf-8", "replace"))
sys.stdout.flush()
return True
def wait_for(self, pattern, timeout):
rx = re.compile(pattern.encode())
deadline = time.time() + timeout if timeout else None
while True:
m = rx.search(self.buf)
if m:
self.buf = self.buf[m.end():]
return True
if not self._pump(deadline):
return False
def send(self, text):
self.sock.sendall(text.encode())
time.sleep(0.05)
def close(self):
try:
self.sock.close()
finally:
self.log.close()
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--sock", required=True)
ap.add_argument("--log", required=True)
ap.add_argument("--do", action="append", default=[])
ap.add_argument("--quiet", action="store_true")
args = ap.parse_args()
con = Console(args.sock, args.log, args.quiet)
for action in args.do:
kind, _, rest = action.partition(":")
if kind == "wait":
timeout, _, pattern = rest.partition(":")
if not pattern:
raise SystemExit("vmctl: wait needs SECONDS:REGEX")
ok = con.wait_for(pattern, float(timeout))
if not ok:
raise SystemExit("vmctl: timeout waiting for %r" % pattern)
elif kind == "send":
con.send(rest.replace("\\n", "\n"))
elif kind == "sleep":
time.sleep(float(rest))
elif kind == "mark":
con.log.write(("\n=== vmctl: %s ===\n" % rest).encode())
if not args.quiet:
print("=== vmctl: %s ===" % rest)
else:
raise SystemExit("vmctl: unknown action %r" % kind)
con.close()
if __name__ == "__main__":
main()