qemu_systemd_vm.sh 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #!/usr/bin/env bash
  2. # Boot a real Debian cloud image under QEMU TCG with PID 1 = systemd and run
  3. # the host safety install matrix. Exit 0 pass, 1 fail, 2 NOT RUN.
  4. set -euo pipefail
  5. ROOT="$(cd "$(dirname "$0")/.." && pwd)"
  6. if ! command -v qemu-system-x86_64 >/dev/null 2>&1; then
  7. echo "qemu-system-x86_64: NOT RUN"
  8. exit 2
  9. fi
  10. if ! command -v genisoimage >/dev/null 2>&1; then
  11. echo "genisoimage: NOT RUN"
  12. exit 2
  13. fi
  14. export PATH="/data/prod2u/opt/go1.25.13/bin:/root/go/bin:/usr/bin:/bin:${PATH}"
  15. export GOTOOLCHAIN="${GOTOOLCHAIN:-local}"
  16. export GOROOT="${GOROOT:-/data/prod2u/opt/go1.25.13}"
  17. WORKDIR="${HTH_QEMU_VM_DIR:-/var/tmp/hth-qemu-vm}"
  18. IMGDIR="${HTH_CLOUD_IMG_DIR:-/data/prod2u/hth-nspawn}"
  19. mkdir -p "${WORKDIR}" "${IMGDIR}"
  20. BASE="${IMGDIR}/debian-13-genericcloud-amd64.qcow2"
  21. URL="${HTH_CLOUD_IMG_URL:-https://cloud.debian.org/images/cloud/trixie/latest/debian-13-genericcloud-amd64.qcow2}"
  22. if [[ ! -s "${BASE}" ]]; then
  23. echo "downloading Debian cloud image to ${BASE}"
  24. if command -v wget >/dev/null 2>&1; then
  25. if ! wget -O "${BASE}.part" "${URL}"; then
  26. echo "cloud image download failed: NOT RUN"
  27. rm -f "${BASE}.part"
  28. exit 2
  29. fi
  30. elif command -v curl >/dev/null 2>&1; then
  31. if ! curl -fsSL --retry 3 -o "${BASE}.part" "${URL}"; then
  32. echo "cloud image download failed: NOT RUN"
  33. rm -f "${BASE}.part"
  34. exit 2
  35. fi
  36. else
  37. echo "wget/curl missing: NOT RUN"
  38. exit 2
  39. fi
  40. mv "${BASE}.part" "${BASE}"
  41. fi
  42. echo "host: building amd64 binaries and share tarball"
  43. mkdir -p "${ROOT}/bin"
  44. (cd "${ROOT}" && CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -trimpath -o "${ROOT}/bin/hthd" ./cmd/hthd)
  45. (cd "${ROOT}" && CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -trimpath -o "${ROOT}/bin/hthctl" ./cmd/hthctl)
  46. (cd "${ROOT}" && CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -trimpath -o "${ROOT}/bin/hth-capture" ./cmd/hth-capture)
  47. chmod +x "${ROOT}/deploy/install.sh" "${ROOT}/deploy/uninstall.sh" "${ROOT}/tools/qemu_guest_hthd.sh"
  48. SHARE="${WORKDIR}/hth-share.tar"
  49. tar -C "${ROOT}" --exclude='.venv-sdk' --exclude='dist' --exclude='__pycache__' \
  50. --exclude='.pytest_cache' --exclude='reviews/artifacts' \
  51. -cf "${SHARE}" .
  52. dd if=/dev/zero bs=1M count=1 status=none >> "${SHARE}"
  53. DISK="${WORKDIR}/hth-vm.qcow2"
  54. SEED="${WORKDIR}/seed.iso"
  55. SERIAL="${WORKDIR}/serial.log"
  56. rm -f "${DISK}" "${SEED}" "${SERIAL}"
  57. qemu-img create -f qcow2 -F qcow2 -b "${BASE}" "${DISK}" 8G
  58. META="${WORKDIR}/meta-data"
  59. USER="${WORKDIR}/user-data"
  60. cat > "${META}" <<'EOF'
  61. instance-id: hth-systemd-vm
  62. local-hostname: hth-vm
  63. EOF
  64. cat > "${USER}" <<'EOF'
  65. #cloud-config
  66. hostname: hth-vm
  67. manage_etc_hosts: true
  68. package_update: false
  69. package_upgrade: false
  70. ssh_pwauth: false
  71. runcmd:
  72. - [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"]
  73. EOF
  74. genisoimage -quiet -output "${SEED}" -volid cidata -joliet -rock "${USER}" "${META}"
  75. echo "host: booting QEMU TCG (PID1=systemd in guest)"
  76. set +e
  77. timeout 720 qemu-system-x86_64 \
  78. -name hth-systemd-vm \
  79. -machine q35,accel=tcg \
  80. -cpu max \
  81. -smp 2 \
  82. -m 1536 \
  83. -nographic \
  84. -no-reboot \
  85. -drive file="${DISK}",if=virtio,format=qcow2 \
  86. -drive file="${SEED}",if=virtio,media=cdrom,format=raw \
  87. -drive file="${SHARE}",if=virtio,format=raw,readonly=on \
  88. -serial file:"${SERIAL}" \
  89. -monitor none \
  90. -nic user,model=virtio
  91. rc=$?
  92. set -e
  93. echo "qemu exit=${rc}"
  94. if [[ ! -f "${SERIAL}" ]]; then
  95. echo "systemd-vm: FAIL no serial log"
  96. exit 1
  97. fi
  98. if [[ -n "${HTH_RUN_DIR:-}" ]]; then
  99. mkdir -p "${HTH_RUN_DIR}"
  100. cp -a "${SERIAL}" "${HTH_RUN_DIR}/guest-serial.log"
  101. python3 - "${SERIAL}" "${HTH_RUN_DIR}" <<'PY'
  102. import sys
  103. from pathlib import Path
  104. serial = Path(sys.argv[1]).read_text(encoding="utf-8", errors="replace")
  105. out = Path(sys.argv[2])
  106. name = None
  107. buf = []
  108. for line in serial.splitlines(keepends=True):
  109. if line.startswith("HTH_GUEST_FILE_BEGIN "):
  110. name = line.split(None, 1)[1].strip()
  111. buf = []
  112. continue
  113. if line.startswith("HTH_GUEST_FILE_END "):
  114. end = line.split(None, 1)[1].strip()
  115. if name and end == name:
  116. (out / name).write_text("".join(buf), encoding="utf-8")
  117. name = None
  118. buf = []
  119. continue
  120. if name:
  121. buf.append(line)
  122. print("extracted_guest_files", sorted(p.name for p in out.glob("*.log")))
  123. PY
  124. fi
  125. if grep -q "HTH_SYSTEMD_VM_PASS" "${SERIAL}"; then
  126. echo "SYSTEMD_QEMU_VM_PASS"
  127. exit 0
  128. fi
  129. echo "----- serial tail -----"
  130. tail -n 80 "${SERIAL}"
  131. if grep -qi "NOT RUN" "${SERIAL}"; then
  132. exit 2
  133. fi
  134. echo "systemd-vm: FAIL (marker missing)"
  135. exit 1