From 9d5041e71bc665929e1970e1f8bb1bb385737a9b Mon Sep 17 00:00:00 2001 From: CanbiZ <47820557+MickLesk@users.noreply.github.com> Date: Thu, 13 Nov 2025 15:29:38 +0100 Subject: [PATCH] Improve VM setup and IP detection logic Suppress libguestfs warnings, enable and start QEMU Guest Agent during first boot, and enhance VM IP address detection by waiting for the guest agent to become available and adding a fallback method. These changes improve reliability and provide better feedback during VM initialization. --- vm/unifi-os-vm.sh | 56 +++++++++++++++++++++++++++++++---------------- 1 file changed, 37 insertions(+), 19 deletions(-) diff --git a/vm/unifi-os-vm.sh b/vm/unifi-os-vm.sh index b7fc52cf3..5796c33a9 100644 --- a/vm/unifi-os-vm.sh +++ b/vm/unifi-os-vm.sh @@ -619,11 +619,12 @@ msg_info "Preparing ${OS_DISPLAY} Qcow2 Disk Image" # Set DNS for libguestfs appliance environment export LIBGUESTFS_BACKEND_SETTINGS=dns=8.8.8.8,1.1.1.1 +# Suppress libguestfs warnings about random seed +export LIBGUESTFS_DEBUG=0 +export LIBGUESTFS_TRACE=0 -# Suppress all virt-customize output including warnings -{ - # Create first-boot installation script - virt-customize -q -a "${FILE}" --run-command "cat > /root/install-unifi.sh << 'INSTALLEOF' +# Create first-boot installation script +virt-customize -q -a "${FILE}" --run-command "cat > /root/install-unifi.sh << 'INSTALLEOF' #!/bin/bash # Log output to file exec > /var/log/install-unifi.log 2>&1 @@ -657,6 +658,10 @@ apt-get update echo \"[\$(date)] Installing base packages\" apt-get install -y qemu-guest-agent curl ca-certificates lsb-release podman uidmap slirp4netns iptables 2>/dev/null || true +# Start and enable QEMU Guest Agent +echo \"[\$(date)] Starting QEMU Guest Agent\" +systemctl enable --now qemu-guest-agent 2>/dev/null || true + # Start and enable Podman echo \"[\$(date)] Enabling Podman service\" systemctl enable --now podman 2>/dev/null || true @@ -722,13 +727,12 @@ WantedBy=multi-user.target SVCEOF systemctl enable unifi-firstboot.service" - # Add auto-login if Cloud-Init is disabled - if [ "$USE_CLOUD_INIT" != "yes" ]; then - virt-customize -q -a "${FILE}" \ - --run-command 'mkdir -p /etc/systemd/system/getty@tty1.service.d' \ - --run-command "bash -c 'echo -e \"[Service]\nExecStart=\nExecStart=-/sbin/agetty --autologin root --noclear %I \\\$TERM\" > /etc/systemd/system/getty@tty1.service.d/override.conf'" - fi -} 2>/dev/null +# Add auto-login if Cloud-Init is disabled +if [ "$USE_CLOUD_INIT" != "yes" ]; then + virt-customize -q -a "${FILE}" \ + --run-command 'mkdir -p /etc/systemd/system/getty@tty1.service.d' \ + --run-command "bash -c 'echo -e \"[Service]\nExecStart=\nExecStart=-/sbin/agetty --autologin root --noclear %I \\\$TERM\" > /etc/systemd/system/getty@tty1.service.d/override.conf'" +fi msg_ok "UniFi OS Installer integrated (will run on first boot)" @@ -818,21 +822,35 @@ if [ "$START_VM" == "yes" ]; then msg_info "Waiting for VM to boot and complete first-boot setup (this may take 3-5 minutes)" - # Get VM IP address (wait up to 60 seconds) - VM_IP="" + # Wait for qemu-guest-agent to be ready (up to 120 seconds) + msg_info "Waiting for QEMU Guest Agent to become available..." + AGENT_READY=0 for i in {1..60}; do - VM_IP=$(qm guest cmd $VMID network-get-interfaces 2>/dev/null | jq -r '.[] | select(.name != "lo") | .["ip-addresses"][]? | select(.["ip-address-type"] == "ipv4") | .["ip-address"]' 2>/dev/null | head -1) - if [ -n "$VM_IP" ]; then + if qm agent $VMID ping 2>/dev/null | grep -q "returns OK"; then + AGENT_READY=1 + msg_ok "QEMU Guest Agent is ready" break fi sleep 2 done + # Get VM IP address + VM_IP="" + if [ $AGENT_READY -eq 1 ]; then + for i in {1..30}; do + VM_IP=$(qm guest cmd $VMID network-get-interfaces 2>/dev/null | jq -r '.[] | select(.name != "lo") | .["ip-addresses"][]? | select(.["ip-address-type"] == "ipv4") | .["ip-address"]' 2>/dev/null | head -1 || echo "") + if [ -n "$VM_IP" ] && [ "$VM_IP" != "127.0.0.1" ]; then + break + fi + sleep 2 + done + fi + + # Fallback: Try to get IP from Proxmox network info if [ -z "$VM_IP" ]; then - msg_info "Unable to detect VM IP automatically - checking manually..." - # Fallback: use qm guest agent - sleep 10 - VM_IP=$(qm guest cmd $VMID network-get-interfaces 2>/dev/null | jq -r '.[1]["ip-addresses"][0]["ip-address"]' 2>/dev/null || echo "") + msg_info "Attempting alternative IP detection method..." + sleep 5 + VM_IP=$(qm guest cmd $VMID network-get-interfaces 2>/dev/null | jq -r '.[1]["ip-addresses"][]? | select(.["ip-address-type"] == "ipv4") | .["ip-address"]' 2>/dev/null | grep -v "127.0.0.1" | head -1 || echo "") fi if [ -n "$VM_IP" ]; then