| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- #!/usr/bin/env bash
- # Boot a real Debian cloud image under QEMU TCG with PID 1 = systemd and run
- # the host safety install matrix. Exit 0 pass, 1 fail, 2 NOT RUN.
- set -euo pipefail
- ROOT="$(cd "$(dirname "$0")/.." && pwd)"
- if ! command -v qemu-system-x86_64 >/dev/null 2>&1; then
- echo "qemu-system-x86_64: NOT RUN"
- exit 2
- fi
- if ! command -v genisoimage >/dev/null 2>&1; then
- echo "genisoimage: NOT RUN"
- exit 2
- fi
- export PATH="/data/prod2u/opt/go1.25.13/bin:/root/go/bin:/usr/bin:/bin:${PATH}"
- export GOTOOLCHAIN="${GOTOOLCHAIN:-local}"
- export GOROOT="${GOROOT:-/data/prod2u/opt/go1.25.13}"
- WORKDIR="${HTH_QEMU_VM_DIR:-/var/tmp/hth-qemu-vm}"
- IMGDIR="${HTH_CLOUD_IMG_DIR:-/data/prod2u/hth-nspawn}"
- mkdir -p "${WORKDIR}" "${IMGDIR}"
- BASE="${IMGDIR}/debian-13-genericcloud-amd64.qcow2"
- URL="${HTH_CLOUD_IMG_URL:-https://cloud.debian.org/images/cloud/trixie/latest/debian-13-genericcloud-amd64.qcow2}"
- if [[ ! -s "${BASE}" ]]; then
- echo "downloading Debian cloud image to ${BASE}"
- if command -v wget >/dev/null 2>&1; then
- if ! wget -O "${BASE}.part" "${URL}"; then
- echo "cloud image download failed: NOT RUN"
- rm -f "${BASE}.part"
- exit 2
- fi
- elif command -v curl >/dev/null 2>&1; then
- if ! curl -fsSL --retry 3 -o "${BASE}.part" "${URL}"; then
- echo "cloud image download failed: NOT RUN"
- rm -f "${BASE}.part"
- exit 2
- fi
- else
- echo "wget/curl missing: NOT RUN"
- exit 2
- fi
- mv "${BASE}.part" "${BASE}"
- fi
- echo "host: building amd64 binaries and share tarball"
- mkdir -p "${ROOT}/bin"
- (cd "${ROOT}" && CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -trimpath -o "${ROOT}/bin/hthd" ./cmd/hthd)
- (cd "${ROOT}" && CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -trimpath -o "${ROOT}/bin/hthctl" ./cmd/hthctl)
- (cd "${ROOT}" && CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -trimpath -o "${ROOT}/bin/hth-capture" ./cmd/hth-capture)
- chmod +x "${ROOT}/deploy/install.sh" "${ROOT}/deploy/uninstall.sh" "${ROOT}/tools/qemu_guest_hthd.sh"
- SHARE="${WORKDIR}/hth-share.tar"
- tar -C "${ROOT}" --exclude='.venv-sdk' --exclude='dist' --exclude='__pycache__' \
- --exclude='.pytest_cache' --exclude='reviews/artifacts' \
- -cf "${SHARE}" .
- dd if=/dev/zero bs=1M count=1 status=none >> "${SHARE}"
- DISK="${WORKDIR}/hth-vm.qcow2"
- SEED="${WORKDIR}/seed.iso"
- SERIAL="${WORKDIR}/serial.log"
- rm -f "${DISK}" "${SEED}" "${SERIAL}"
- qemu-img create -f qcow2 -F qcow2 -b "${BASE}" "${DISK}" 8G
- META="${WORKDIR}/meta-data"
- USER="${WORKDIR}/user-data"
- cat > "${META}" <<'EOF'
- instance-id: hth-systemd-vm
- local-hostname: hth-vm
- EOF
- cat > "${USER}" <<'EOF'
- #cloud-config
- hostname: hth-vm
- manage_etc_hosts: true
- package_update: false
- package_upgrade: false
- ssh_pwauth: false
- runcmd:
- - [bash, -lc, "set -x; echo HTH_VM_BOOT; mkdir -p /mnt/hth; tar -xf /dev/vdc -C /mnt/hth || tar -xf /dev/vdb -C /mnt/hth; ls -la /mnt/hth | head; test -x /mnt/hth/tools/qemu_guest_hthd.sh; /mnt/hth/tools/qemu_guest_hthd.sh; echo HTH_GUEST_RC=$?; poweroff -f"]
- EOF
- genisoimage -quiet -output "${SEED}" -volid cidata -joliet -rock "${USER}" "${META}"
- echo "host: booting QEMU TCG (PID1=systemd in guest)"
- set +e
- timeout 720 qemu-system-x86_64 \
- -name hth-systemd-vm \
- -machine q35,accel=tcg \
- -cpu max \
- -smp 2 \
- -m 1536 \
- -nographic \
- -no-reboot \
- -drive file="${DISK}",if=virtio,format=qcow2 \
- -drive file="${SEED}",if=virtio,media=cdrom,format=raw \
- -drive file="${SHARE}",if=virtio,format=raw,readonly=on \
- -serial file:"${SERIAL}" \
- -monitor none \
- -nic user,model=virtio
- rc=$?
- set -e
- echo "qemu exit=${rc}"
- if [[ ! -f "${SERIAL}" ]]; then
- echo "systemd-vm: FAIL no serial log"
- exit 1
- fi
- if [[ -n "${HTH_RUN_DIR:-}" ]]; then
- mkdir -p "${HTH_RUN_DIR}"
- cp -a "${SERIAL}" "${HTH_RUN_DIR}/guest-serial.log"
- python3 - "${SERIAL}" "${HTH_RUN_DIR}" <<'PY'
- import sys
- from pathlib import Path
- serial = Path(sys.argv[1]).read_text(encoding="utf-8", errors="replace")
- out = Path(sys.argv[2])
- name = None
- buf = []
- for line in serial.splitlines(keepends=True):
- if line.startswith("HTH_GUEST_FILE_BEGIN "):
- name = line.split(None, 1)[1].strip()
- buf = []
- continue
- if line.startswith("HTH_GUEST_FILE_END "):
- end = line.split(None, 1)[1].strip()
- if name and end == name:
- (out / name).write_text("".join(buf), encoding="utf-8")
- name = None
- buf = []
- continue
- if name:
- buf.append(line)
- print("extracted_guest_files", sorted(p.name for p in out.glob("*.log")))
- PY
- fi
- if grep -q "HTH_SYSTEMD_VM_PASS" "${SERIAL}"; then
- echo "SYSTEMD_QEMU_VM_PASS"
- exit 0
- fi
- echo "----- serial tail -----"
- tail -n 80 "${SERIAL}"
- if grep -qi "NOT RUN" "${SERIAL}"; then
- exit 2
- fi
- echo "systemd-vm: FAIL (marker missing)"
- exit 1
|