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.
This commit is contained in:
CanbiZ 2025-11-13 15:29:38 +01:00
parent 0c8fb8e79c
commit 9d5041e71b

View File

@ -619,9 +619,10 @@ msg_info "Preparing ${OS_DISPLAY} Qcow2 Disk Image"
# Set DNS for libguestfs appliance environment # Set DNS for libguestfs appliance environment
export LIBGUESTFS_BACKEND_SETTINGS=dns=8.8.8.8,1.1.1.1 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 # Create first-boot installation script
virt-customize -q -a "${FILE}" --run-command "cat > /root/install-unifi.sh << 'INSTALLEOF' virt-customize -q -a "${FILE}" --run-command "cat > /root/install-unifi.sh << 'INSTALLEOF'
#!/bin/bash #!/bin/bash
@ -657,6 +658,10 @@ apt-get update
echo \"[\$(date)] Installing base packages\" 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 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 # Start and enable Podman
echo \"[\$(date)] Enabling Podman service\" echo \"[\$(date)] Enabling Podman service\"
systemctl enable --now podman 2>/dev/null || true systemctl enable --now podman 2>/dev/null || true
@ -728,7 +733,6 @@ systemctl enable unifi-firstboot.service"
--run-command 'mkdir -p /etc/systemd/system/getty@tty1.service.d' \ --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'" --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 fi
} 2>/dev/null
msg_ok "UniFi OS Installer integrated (will run on first boot)" 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)" 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) # Wait for qemu-guest-agent to be ready (up to 120 seconds)
VM_IP="" msg_info "Waiting for QEMU Guest Agent to become available..."
AGENT_READY=0
for i in {1..60}; do 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 qm agent $VMID ping 2>/dev/null | grep -q "returns OK"; then
if [ -n "$VM_IP" ]; then AGENT_READY=1
msg_ok "QEMU Guest Agent is ready"
break break
fi fi
sleep 2 sleep 2
done 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 if [ -z "$VM_IP" ]; then
msg_info "Unable to detect VM IP automatically - checking manually..." msg_info "Attempting alternative IP detection method..."
# Fallback: use qm guest agent sleep 5
sleep 10 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 "")
VM_IP=$(qm guest cmd $VMID network-get-interfaces 2>/dev/null | jq -r '.[1]["ip-addresses"][0]["ip-address"]' 2>/dev/null || echo "")
fi fi
if [ -n "$VM_IP" ]; then if [ -n "$VM_IP" ]; then