Validate every writable drive (qcow2, inside PHOEBUS_VM_DISKS, no /dev/*, block/char devices, raw images, escaping symlinks or outside backing chains), give each run its own OVMF variable file, QMP socket and state directory, and use QMP plus argument arrays instead of shell interpolation. Return nonzero on any failed assertion, timeout, EOF or missing result and record PASS/FAIL/BLOCKED/SKIPPED/NOT RUN. Add 16 unit tests, a report generator and a findings register; make test runs the harness suite.
208 lines
7.9 KiB
Bash
208 lines
7.9 KiB
Bash
#!/usr/bin/env python3
|
|
"""Headless serial driver for the Debian-Live Phoebus appliance ISO.
|
|
|
|
The committed `serial-driver.py` drives a *Gentoo* live ISO with an explicit
|
|
kernel/initrd. The appliance (`installer/live/build.sh`) boots its own GRUB on
|
|
UEFI, so it needs its own driver.
|
|
|
|
Two modes:
|
|
|
|
install : boot the appliance, log in as `user`/`live`, fetch a guest script
|
|
over QEMU user networking, install Phoebus non-interactively, then
|
|
power off.
|
|
boot : boot the installed disk alone (no ISO), unlock LUKS, log in and run
|
|
verification commands.
|
|
|
|
Everything is parameterised through environment variables. `DISK`/`DISKS` must
|
|
be qcow2 files inside the disposable directory named by `PHOEBUS_VM_DISKS`
|
|
(default `$PHOEBUS_VM_STATE/disks`); block devices, `/dev/*`, escaping
|
|
symlinks, raw images and outside backing chains are refused. Secrets are served
|
|
from the host as 0600 files and fetched inside the guest, so they never appear
|
|
in the serial transcript.
|
|
|
|
Host setup (see docs/DEVELOPMENT.md):
|
|
|
|
mkdir -p /path/serve && cd /path/serve
|
|
cp /path/to/installer/live/phoebus-installer-amd64.iso .
|
|
cp <stage3>.tar.xz stage3.tar.xz && sha256sum stage3.tar.xz
|
|
cp /path/to/installer/tests/vm/appliance-guest.sh .
|
|
cp /path/to/installer/tests/vm/appliance-test.conf.example test.conf # set STAGE3_SHA256
|
|
printf 'phoebus-luks-test' > luks.pw && chmod 600 luks.pw
|
|
printf 'phoebus-user-test' > user.pw && chmod 600 user.pw
|
|
python3 -m http.server 8000 --bind 127.0.0.1
|
|
"""
|
|
import os
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
import pexpect
|
|
|
|
import vmsafety
|
|
from vmsafety import Results
|
|
|
|
QEMU = os.environ.get("QEMU_BIN", "qemu-system-x86_64")
|
|
OVMF_CODE = os.environ["OVMF_CODE"]
|
|
OVMF_VARS_TEMPLATE = os.environ["OVMF_VARS"]
|
|
DISKS = os.environ.get("DISKS") or os.environ.get("DISK", "")
|
|
ISO = os.environ.get("ISO", "")
|
|
SERVE_URL = os.environ.get("SERVE_URL", "http://10.0.2.2:8000")
|
|
LUKS_PW = os.environ.get("LUKS_PASSPHRASE", "phoebus-luks-test")
|
|
USER_PW = os.environ.get("USER_PASSWORD", "phoebus-user-test")
|
|
USER = os.environ.get("USERNAME", "phoebus")
|
|
LIVE_USER = os.environ.get("LIVE_USER", "user")
|
|
LIVE_PW = os.environ.get("LIVE_PASSWORD", "live")
|
|
MEM = os.environ.get("VM_MEM", "8192")
|
|
SMP = os.environ.get("VM_SMP", "8")
|
|
INSTALL_TIMEOUT = int(os.environ.get("INSTALL_TIMEOUT", "10800"))
|
|
|
|
|
|
def disposable_dir() -> Path:
|
|
base = os.environ.get("PHOEBUS_VM_STATE", "/var/tmp/phoebus-vm")
|
|
return Path(os.environ.get("PHOEBUS_VM_DISKS", base + "/disks"))
|
|
|
|
|
|
def base_args(ovmf_vars, disks):
|
|
args = [
|
|
"-machine", "q35,accel=kvm",
|
|
"-cpu", "host", "-smp", SMP, "-m", MEM,
|
|
"-drive", f"if=pflash,format=raw,readonly=on,file={OVMF_CODE}",
|
|
"-drive", f"if=pflash,format=raw,file={ovmf_vars}",
|
|
]
|
|
for disk in disks:
|
|
args += ["-drive", f"file={disk},if=virtio,format=qcow2"]
|
|
args += [
|
|
"-netdev", "user,id=n0", "-device", "virtio-net-pci,netdev=n0,romfile=",
|
|
"-device", "virtio-rng-pci", "-vga", "std", "-display", "none",
|
|
"-serial", "stdio", "-monitor", "none", "-no-reboot",
|
|
]
|
|
return args
|
|
|
|
|
|
def spawn(args, log):
|
|
child = pexpect.spawn(QEMU, args, timeout=900, encoding="utf-8",
|
|
codec_errors="replace", maxread=65536)
|
|
child.delaybeforesend = 0.4
|
|
child.logfile = log
|
|
return child
|
|
|
|
|
|
def login_live(child):
|
|
idx = child.expect([r"(?:live|debian) login:", r"[$#] $"], timeout=900)
|
|
if idx == 0:
|
|
child.sendline(LIVE_USER)
|
|
child.expect(r"Password:", timeout=60)
|
|
child.sendline(LIVE_PW)
|
|
child.expect(r"[$#] $", timeout=120)
|
|
|
|
|
|
def do_install(results, paths, disks):
|
|
if not ISO:
|
|
results.blocked("iso-required", "ISO is required for install mode")
|
|
return
|
|
args = base_args(str(paths.ovmf_vars), disks)
|
|
args += ["-drive", f"file={ISO},media=cdrom,readonly=on"]
|
|
log = open(paths.serial_log, "w")
|
|
child = spawn(args, log)
|
|
try:
|
|
login_live(child)
|
|
results.pass_("live-login")
|
|
child.sendline(
|
|
f"curl -fsSL {SERVE_URL}/appliance-guest.sh -o /tmp/gi.sh && bash /tmp/gi.sh"
|
|
)
|
|
child.expect([r"===INSTALL-RC=([0-9]+)===", "===FETCH-FAIL===",
|
|
"Kernel panic", pexpect.EOF, pexpect.TIMEOUT],
|
|
timeout=INSTALL_TIMEOUT)
|
|
if "INSTALL-RC" in (child.after or ""):
|
|
rc = child.match.group(1)
|
|
results.assert_true("install-rc-zero", rc == "0", f"INSTALL-RC={rc}")
|
|
else:
|
|
results.fail("install-rc-zero", "install did not report a status")
|
|
serial = paths.serial_log.read_text(errors="replace")
|
|
results.assert_true("no-secret-leak", "SECRET_LEAK_FOUND" not in serial,
|
|
"guest reported a secret leak")
|
|
child.expect(["===POWEROFF===", pexpect.EOF, pexpect.TIMEOUT], timeout=600)
|
|
except pexpect.TIMEOUT as exc:
|
|
results.fail("install", f"timeout: {exc}")
|
|
except pexpect.EOF:
|
|
results.fail("install", "unexpected EOF")
|
|
finally:
|
|
child.close(force=True)
|
|
log.close()
|
|
|
|
|
|
def do_boot(results, paths, disks):
|
|
args = base_args(str(paths.ovmf_vars), disks) + ["-boot", "order=c"]
|
|
checks = os.environ.get(
|
|
"BOOT_CHECKS",
|
|
"echo ===CHECKS-$(printf START)===; "
|
|
"cat /proc/1/comm; systemctl is-system-running; "
|
|
"systemctl --failed --no-pager; ip -brief addr; "
|
|
"lsblk -o NAME,SIZE,TYPE,FSTYPE,MOUNTPOINTS; "
|
|
"/usr/local/bin/sys rollback status 2>&1 || true; "
|
|
"echo ===CHECKS-$(printf END)===",
|
|
)
|
|
log = open(paths.serial_log, "w")
|
|
child = spawn(args, log)
|
|
try:
|
|
child.expect([r"[Pp]assphrase", r"login:"], timeout=600)
|
|
if "assphrase" in (child.after or ""):
|
|
child.sendline(LUKS_PW)
|
|
logged_in = False
|
|
for _ in range(5):
|
|
child.expect(r"phoebus login:", timeout=300)
|
|
child.sendline(USER)
|
|
idx = child.expect([r"[Pp]assword:", r"phoebus login:", r"Login incorrect"],
|
|
timeout=60)
|
|
if idx == 0:
|
|
child.sendline(USER_PW)
|
|
if child.expect([r"[$#] $", r"Login incorrect", r"phoebus login:"],
|
|
timeout=120) == 0:
|
|
logged_in = True
|
|
break
|
|
results.assert_true("detached-boot-login", logged_in, "could not log in")
|
|
child.sendline(checks)
|
|
child.expect("===CHECKS-END===", timeout=int(os.environ.get("CHECKS_TIMEOUT", "300")))
|
|
serial = paths.serial_log.read_text(errors="replace")
|
|
results.assert_true("boot-systemd-running", "running" in serial,
|
|
"systemd did not report running")
|
|
except pexpect.TIMEOUT as exc:
|
|
results.fail("boot", f"timeout: {exc}")
|
|
except pexpect.EOF:
|
|
results.fail("boot", "unexpected EOF")
|
|
finally:
|
|
child.close(force=True)
|
|
log.close()
|
|
|
|
|
|
def main() -> int:
|
|
mode = sys.argv[1] if len(sys.argv) > 1 else ""
|
|
if mode not in ("install", "boot"):
|
|
print(__doc__)
|
|
return 2
|
|
results = Results(f"appliance-{mode}")
|
|
paths = vmsafety.make_run_paths(
|
|
f"appliance-{mode}",
|
|
base=os.environ.get("PHOEBUS_VM_STATE", "/var/tmp/phoebus-vm"),
|
|
ovmf_vars_template=OVMF_VARS_TEMPLATE,
|
|
)
|
|
try:
|
|
disks = vmsafety.validate_writable_drives(DISKS.split(","), disposable_dir())
|
|
except vmsafety.UnsafeDiskError as exc:
|
|
results.blocked("disposable-disk", str(exc))
|
|
results.write(paths.results_json)
|
|
print(f"harness error: {exc}", file=sys.stderr)
|
|
return 2
|
|
results.pass_("disposable-disk", ", ".join(str(d) for d in disks))
|
|
try:
|
|
if mode == "install":
|
|
do_install(results, paths, disks)
|
|
else:
|
|
do_boot(results, paths, disks)
|
|
finally:
|
|
results.write(paths.results_json)
|
|
print(results.summary(), flush=True)
|
|
return results.exit_code()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|