Update unifi-os-vm.sh

This commit is contained in:
CanbiZ 2025-11-13 14:34:19 +01:00
parent 58aacbb37c
commit c1aa45e3d5

View File

@ -615,25 +615,84 @@ if ! command -v virt-customize &>/dev/null; then
msg_ok "Installed libguestfs-tools" msg_ok "Installed libguestfs-tools"
fi fi
msg_info "Injecting UniFi OS Installer into Cloud Image" msg_info "Preparing ${OS_DISPLAY} Qcow2 Disk Image"
if [ "$USE_CLOUD_INIT" = "yes" ]; then
# Cloud-Init enabled: No auto-login, standard setup # Set DNS for libguestfs appliance environment
virt-customize -q -a "$FILE" \ export LIBGUESTFS_BACKEND_SETTINGS=dns=8.8.8.8,1.1.1.1
--run-command "echo 'nameserver 1.1.1.1' > /etc/resolv.conf" \
--install qemu-guest-agent,ca-certificates,curl,lsb-release,podman \ # Create first-boot installation script
--run-command "curl -fsSL '${UOS_URL}' -o /root/${UOS_INSTALLER} && chmod +x /root/${UOS_INSTALLER}" \ virt-customize -q -a "${FILE}" --run-command "cat > /root/install-unifi.sh << 'INSTALLEOF'
>/dev/null #!/bin/bash
else # Log output to file
# No Cloud-Init: Keep auto-login for console access exec > /var/log/install-unifi.log 2>&1
virt-customize -q -a "$FILE" \ echo \"[\$(date)] Starting UniFi OS installation on first boot\"
--run-command "echo 'nameserver 1.1.1.1' > /etc/resolv.conf" \
--install qemu-guest-agent,ca-certificates,curl,lsb-release,podman \ # Wait for network to be fully available
--run-command "curl -fsSL '${UOS_URL}' -o /root/${UOS_INSTALLER} && chmod +x /root/${UOS_INSTALLER}" \ for i in {1..30}; do
if ping -c 1 8.8.8.8 >/dev/null 2>&1; then
echo \"[\$(date)] Network is available\"
break
fi
echo \"[\$(date)] Waiting for network... attempt \$i/30\"
sleep 2
done
# Configure DNS
echo \"[\$(date)] Configuring DNS\"
mkdir -p /etc/systemd/resolved.conf.d
cat > /etc/systemd/resolved.conf.d/dns.conf << DNSEOF
[Resolve]
DNS=8.8.8.8 1.1.1.1
FallbackDNS=8.8.4.4 1.0.0.1
DNSEOF
systemctl restart systemd-resolved 2>/dev/null || true
# Update package lists
echo \"[\$(date)] Updating package lists\"
apt-get update
# Install base packages
echo \"[\$(date)] Installing base packages\"
apt-get install -y qemu-guest-agent curl ca-certificates lsb-release podman 2>/dev/null || true
# Download UniFi OS installer
echo \"[\$(date)] Downloading UniFi OS Server ${UOS_VERSION}\"
curl -fsSL '${UOS_URL}' -o /root/${UOS_INSTALLER}
chmod +x /root/${UOS_INSTALLER}
echo \"[\$(date)] UniFi OS installer ready at /root/${UOS_INSTALLER}\"
echo \"[\$(date)] Run the installer manually: /root/${UOS_INSTALLER}\"
# Self-destruct this installation script
rm -f /root/install-unifi.sh
INSTALLEOF
chmod +x /root/install-unifi.sh"
# Set up systemd service for first boot
virt-customize -q -a "${FILE}" --run-command "cat > /etc/systemd/system/unifi-firstboot.service << 'SVCEOF'
[Unit]
Description=UniFi OS First Boot Setup
After=network-online.target
Wants=network-online.target
ConditionPathExists=/root/install-unifi.sh
[Service]
Type=oneshot
ExecStart=/root/install-unifi.sh
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
SVCEOF
ln -s /etc/systemd/system/unifi-firstboot.service /etc/systemd/system/multi-user.target.wants/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 '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'"
>/dev/null
fi fi
msg_ok "UniFi OS Installer integrated"
msg_ok "UniFi OS Installer integrated (will run on first boot)"
msg_info "Creating UniFi OS VM" msg_info "Creating UniFi OS VM"
qm create "$VMID" -agent 1${MACHINE} -tablet 0 -localtime 1 -bios ovmf \ qm create "$VMID" -agent 1${MACHINE} -tablet 0 -localtime 1 -bios ovmf \