diff --git a/tests/test_quickvm.py b/tests/test_quickvm.py index b26411f..8c9605f 100644 --- a/tests/test_quickvm.py +++ b/tests/test_quickvm.py @@ -27,6 +27,7 @@ def test_user_data_has_user_password_and_keys(): text = cloudinit.build_user_data("web", "viktor", "$6$abc", ["ssh-ed25519 AAAAC3 me@laptop"]) assert "hostname: web" in text and "- name: viktor" in text and "passwd: '$6$abc'" in text assert "- ssh-ed25519 AAAAC3 me@laptop" in text and "ssh_pwauth: true" in text and "growpart" in text + assert "packages: [qemu-guest-agent]" in text and "package_update: true" in text assert "ssh_pwauth: false" in cloudinit.build_user_data("web", "viktor", "", ["ssh-ed25519 AAAAC3 me"]) @@ -120,6 +121,7 @@ def test_windows11_gets_uefi_tpm_sata_and_e1000e(logged_in, data_dirs, monkeypat assert cmd[cmd.index("--boot") + 1] == "uefi,cdrom,hd" assert "--tpm" in cmd and "backend.version=2.0" in cmd[cmd.index("--tpm") + 1] assert "bus=sata" in cmd[cmd.index("--disk") + 1] and "model=e1000e" in cmd[cmd.index("--network") + 1] + assert "--channel" not in cmd # Windows has no guest agent without extra drivers def test_linux_installer_detects_os_and_uses_virtio(logged_in, data_dirs, monkeypatch): @@ -129,6 +131,7 @@ def test_linux_installer_detects_os_and_uses_virtio(logged_in, data_dirs, monkey cmd = captured["cmd"] assert cmd[cmd.index("--osinfo") + 1] == "detect=on,require=off" and cmd[cmd.index("--boot") + 1] == "cdrom,hd" assert "bus=virtio" in cmd[cmd.index("--disk") + 1] and "model=virtio" in cmd[cmd.index("--network") + 1] and "--tpm" not in cmd + assert cmd[cmd.index("--channel") + 1] == "unix,target.type=virtio,target.name=org.qemu.guest_agent.0" assert create(logged_in, monkeypatch, iso_form(iso, os_type="bsd"))[0].status_code == 400 diff --git a/web/app.py b/web/app.py index c2bba90..1aa3900 100644 --- a/web/app.py +++ b/web/app.py @@ -406,15 +406,17 @@ def parse_virsh_list() -> list[dict[str, str]]: return [] def ip_from_domifaddr(name: str) -> str: - try: - result = run_cmd(["virsh", "domifaddr", name], timeout=8) - if result.get("ok"): - for ip in re.findall(r"\b(\d{1,3}(?:\.\d{1,3}){3})/\d+", result.get("stdout", "")): - ip = clean_ip(ip) - if ip: - return ip - except Exception: - pass + # DHCP lease first (NAT network), then the guest agent (machines in the home network via br0). + for extra in ([], ["--source", "agent"]): + try: + result = run_cmd(["virsh", "domifaddr", name, *extra], timeout=8) + if result.get("ok"): + for ip in re.findall(r"\b(\d{1,3}(?:\.\d{1,3}){3})/\d+", result.get("stdout", "")): + ip = clean_ip(ip) + if ip: + return ip + except Exception: + pass return "" def ip_from_network_core(name: str) -> str: @@ -1860,6 +1862,9 @@ def vm_create_submit(request: Request, name: str = Form(...), memory: int = Form # Windows 11 refuses to install without a TPM 2.0. cmd += ["--tpm", "model=tpm-crb,backend.type=emulator,backend.version=2.0"] graphics = ["--graphics", "vnc,listen=127.0.0.1", "--noautoconsole"] + if not windows: + # Channel for qemu-guest-agent (installed by cloud-init; on ISO installs — by the user or the distro). + graphics = ["--channel", "unix,target.type=virtio,target.name=org.qemu.guest_agent.0", *graphics] seed_file = "" login_hint = "" diff --git a/web/cloudinit.py b/web/cloudinit.py index 9b55af2..1bb2561 100644 --- a/web/cloudinit.py +++ b/web/cloudinit.py @@ -77,7 +77,11 @@ def build_user_data(hostname: str, user: str, password_hash: str, ssh_keys: list " mode: auto", " devices: ['/']", "resize_rootfs: true", - "package_update: false", + # The guest agent lets the panel see the machine's address in any network and shut it down cleanly. + "package_update: true", + "packages: [qemu-guest-agent]", + "runcmd:", + " - [sh, -c, 'systemctl enable --now qemu-guest-agent 2>/dev/null || { rc-update add qemu-guest-agent default && rc-service qemu-guest-agent start; } || true']", ] return "\n".join(lines) + "\n"