Update unifi-os-server-vm.sh

This commit is contained in:
CanbiZ 2025-12-03 17:41:27 +01:00
parent 8693adbeb3
commit 62ac9e9470

View File

@ -591,70 +591,90 @@ msg_ok "Downloaded ${CL}${BL}${FILE}${CL}"
# --- Inject UniFi Installer via Cloud-Init --- # --- Inject UniFi Installer via Cloud-Init ---
msg_info "Preparing ${OS_DISPLAY} Cloud Image for UniFi OS" msg_info "Preparing ${OS_DISPLAY} Cloud Image for UniFi OS"
# Create Cloud-Init user-data for UniFi OS installation # Install virt-customize if not available
cat >user-data.yaml <<EOFUSERDATA if ! command -v virt-customize &>/dev/null; then
#cloud-config apt-get -qq update >/dev/null
apt-get -qq install libguestfs-tools -y >/dev/null
fi
# Write UniFi OS installation script # Create UniFi OS installation script and inject it into the image
write_files: virt-customize -a "${FILE}" --run-command "cat > /root/install-unifi-os.sh << 'INSTALLSCRIPT'
- path: /root/install-unifi-os.sh #!/bin/bash
permissions: '0755' set -x
content: | exec > /var/log/unifi-install.log 2>&1
#!/bin/bash
set -x
exec > /var/log/unifi-install.log 2>&1
echo "=== UniFi OS Installation Started at \$(date) ===" echo \"=== UniFi OS Installation Started at \$(date) ===\"
# Install required packages # Wait for cloud-init to complete
echo "Installing required packages..." if command -v cloud-init >/dev/null 2>&1; then
export DEBIAN_FRONTEND=noninteractive echo \"Waiting for cloud-init to complete...\"
apt-get update cloud-init status --wait 2>/dev/null || true
apt-get install -y curl wget ca-certificates podman uidmap slirp4netns iptables fi
# Configure Podman # Install required packages
echo "Configuring Podman..." echo \"Installing required packages...\"
loginctl enable-linger root export DEBIAN_FRONTEND=noninteractive
apt-get update
apt-get install -y curl wget ca-certificates podman uidmap slirp4netns iptables
# Download UniFi OS Server # Configure Podman
echo "Downloading UniFi OS Server ${UOS_VERSION}..." echo \"Configuring Podman...\"
cd /root loginctl enable-linger root
curl -fsSL '${UOS_URL}' -o unifi-installer.bin
chmod +x unifi-installer.bin
# Install UniFi OS Server # Download UniFi OS Server
echo "Installing UniFi OS Server (this takes 3-5 minutes)..." echo \"Downloading UniFi OS Server ${UOS_VERSION}...\"
./unifi-installer.bin install cd /root
curl -fsSL '${UOS_URL}' -o unifi-installer.bin
chmod +x unifi-installer.bin
echo "Waiting for services to start..." # Install UniFi OS Server
sleep 15 echo \"Installing UniFi OS Server (this takes 3-5 minutes)...\"
./unifi-installer.bin install
# Start UniFi OS Server echo \"Waiting for services to start...\"
if systemctl list-unit-files | grep -q unifi-os-server; then sleep 15
echo "Starting UniFi OS Server service..."
systemctl enable unifi-os-server
systemctl start unifi-os-server
sleep 10
if systemctl is-active --quiet unifi-os-server; then # Start UniFi OS Server
echo "SUCCESS: UniFi OS Server is running" if systemctl list-unit-files | grep -q unifi-os-server; then
else echo \"Starting UniFi OS Server service...\"
echo "WARNING: Checking service status..." systemctl enable unifi-os-server
systemctl status unifi-os-server --no-pager systemctl start unifi-os-server
fi sleep 10
fi
touch /root/.unifi-installed if systemctl is-active --quiet unifi-os-server; then
echo "=== Installation completed at \$(date) ===" echo \"SUCCESS: UniFi OS Server is running\"
else
echo \"WARNING: Checking service status...\"
systemctl status unifi-os-server --no-pager
fi
fi
# Run installation on first boot touch /root/.unifi-installed
runcmd: echo \"=== Installation completed at \$(date) ===\"
- /root/install-unifi-os.sh INSTALLSCRIPT" >/dev/null 2>&1
final_message: "UniFi OS installation complete! Check /var/log/unifi-install.log" virt-customize -a "${FILE}" --chmod 0755:/root/install-unifi-os.sh >/dev/null 2>&1
EOFUSERDATA
msg_ok "Created Cloud-Init configuration for UniFi OS installation" # Create systemd service for first-boot installation
virt-customize -a "${FILE}" --run-command "cat > /etc/systemd/system/unifi-firstboot.service << 'SVCFILE'
[Unit]
Description=UniFi OS First Boot Installation
After=cloud-init.service network-online.target
Wants=network-online.target
ConditionPathExists=!/root/.unifi-installed
[Service]
Type=oneshot
ExecStart=/root/install-unifi-os.sh
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
SVCFILE" >/dev/null 2>&1
virt-customize -a "${FILE}" --run-command "systemctl enable unifi-firstboot.service" >/dev/null 2>&1
msg_ok "Prepared ${OS_DISPLAY} image with UniFi OS installer"
# Expand root partition to use full disk space # Expand root partition to use full disk space
msg_info "Expanding disk image to ${DISK_SIZE}" msg_info "Expanding disk image to ${DISK_SIZE}"
@ -692,19 +712,10 @@ qm set "$VMID" \
qm resize "$VMID" scsi0 "$DISK_SIZE" >/dev/null qm resize "$VMID" scsi0 "$DISK_SIZE" >/dev/null
qm set "$VMID" --agent enabled=1 >/dev/null qm set "$VMID" --agent enabled=1 >/dev/null
# Add Cloud-Init drive and custom user-data # Add Cloud-Init drive (standard Cloud-Init, no custom user-data)
msg_info "Configuring Cloud-Init with UniFi OS installation" msg_info "Configuring Cloud-Init"
setup_cloud_init "$VMID" "$STORAGE" "$HN" "yes" >/dev/null 2>&1 setup_cloud_init "$VMID" "$STORAGE" "$HN" "yes" >/dev/null 2>&1
msg_ok "Cloud-Init configured (UniFi OS installs via systemd service on first boot)"
# Inject custom user-data for UniFi OS installation
# First, ensure snippets directory exists and copy user-data file
mkdir -p /var/lib/vz/snippets
cp user-data.yaml "/var/lib/vz/snippets/unifi-os-${VMID}-user-data.yaml"
# Now configure VM to use custom user-data
qm set "$VMID" --cicustom "user=local:snippets/unifi-os-${VMID}-user-data.yaml" >/dev/null 2>&1
msg_ok "Cloud-Init configured with automated UniFi OS installation"
# Display credentials immediately so user can login # Display credentials immediately so user can login
if [ -n "$CLOUDINIT_CRED_FILE" ] && [ -f "$CLOUDINIT_CRED_FILE" ]; then if [ -n "$CLOUDINIT_CRED_FILE" ] && [ -f "$CLOUDINIT_CRED_FILE" ]; then
@ -712,8 +723,8 @@ if [ -n "$CLOUDINIT_CRED_FILE" ] && [ -f "$CLOUDINIT_CRED_FILE" ]; then
echo -e "${INFO}${BOLD}${GN}Cloud-Init Credentials (save these now!):${CL}" echo -e "${INFO}${BOLD}${GN}Cloud-Init Credentials (save these now!):${CL}"
echo -e "${TAB}${DGN}User: ${BGN}${CLOUDINIT_USER:-root}${CL}" echo -e "${TAB}${DGN}User: ${BGN}${CLOUDINIT_USER:-root}${CL}"
echo -e "${TAB}${DGN}Password: ${BGN}${CLOUDINIT_PASSWORD}${CL}" echo -e "${TAB}${DGN}Password: ${BGN}${CLOUDINIT_PASSWORD}${CL}"
echo -e "${TAB}${RD}⚠️ Installation starts automatically on first boot${CL}" echo -e "${TAB}${RD}⚠️ UniFi OS installation runs automatically on first boot${CL}"
echo -e "${TAB}${INFO}Monitor: ${BL}tail -f /var/log/cloud-init-output.log${CL}" echo -e "${TAB}${INFO}Monitor: ${BL}tail -f /var/log/unifi-install.log${CL}"
echo "" echo ""
fi fi
@ -795,7 +806,7 @@ if [ "$START_VM" == "yes" ]; then
# Update message every 30 seconds # Update message every 30 seconds
if [ $((WAIT_COUNT - LAST_MSG_TIME)) -ge 30 ]; then if [ $((WAIT_COUNT - LAST_MSG_TIME)) -ge 30 ]; then
echo -e "${BFR}${TAB}${YW}${HOLD}Installation in progress... ${WAIT_COUNT}s elapsed${CL}" echo -e "${BFR}${TAB}${YW}${HOLD}Installation in progress... ${WAIT_COUNT}s elapsed${CL}"
echo -e "${TAB}${INFO}${YW}Monitor: ${BL}ssh ${CLOUDINIT_USER:-root}@${VM_IP} 'tail -f /var/log/cloud-init-output.log'${CL}" echo -e "${TAB}${INFO}${YW}Monitor: ${BL}ssh ${CLOUDINIT_USER:-root}@${VM_IP} 'tail -f /var/log/unifi-install.log'${CL}"
LAST_MSG_TIME=$WAIT_COUNT LAST_MSG_TIME=$WAIT_COUNT
fi fi
done done
@ -804,13 +815,13 @@ if [ "$START_VM" == "yes" ]; then
echo -e "\n${TAB}${GATEWAY}${BOLD}${GN}✓ UniFi OS Server is ready!${CL}" echo -e "\n${TAB}${GATEWAY}${BOLD}${GN}✓ UniFi OS Server is ready!${CL}"
echo -e "${TAB}${GATEWAY}${BOLD}${GN}✓ Access at: ${BGN}https://${VM_IP}:11443${CL}\n" echo -e "${TAB}${GATEWAY}${BOLD}${GN}✓ Access at: ${BGN}https://${VM_IP}:11443${CL}\n"
else else
msg_ok "VM is running, installation in progress via Cloud-Init" msg_ok "VM is running, UniFi OS installation in progress"
echo -e "${TAB}${INFO}${YW}Cloud-Init handles the complete installation automatically${CL}" echo -e "${TAB}${INFO}${YW}Installation runs via systemd service on first boot${CL}"
echo -e "${TAB}${INFO}${YW}This takes 5-8 minutes on first boot${CL}" echo -e "${TAB}${INFO}${YW}This takes 5-8 minutes${CL}"
if [ "$USE_CLOUD_INIT" = "yes" ]; then if [ "$USE_CLOUD_INIT" = "yes" ]; then
echo -e "${TAB}${INFO}${YW}SSH: ${BL}ssh ${CLOUDINIT_USER:-root}@${VM_IP}${CL}" echo -e "${TAB}${INFO}${YW}SSH: ${BL}ssh ${CLOUDINIT_USER:-root}@${VM_IP}${CL}"
echo -e "${TAB}${INFO}${YW}Password: ${BGN}${CLOUDINIT_PASSWORD}${CL}" echo -e "${TAB}${INFO}${YW}Password: ${BGN}${CLOUDINIT_PASSWORD}${CL}"
echo -e "${TAB}${INFO}${YW}Monitor: ${BL}tail -f /var/log/cloud-init-output.log${CL}" echo -e "${TAB}${INFO}${YW}Monitor: ${BL}tail -f /var/log/unifi-install.log${CL}"
fi fi
echo -e "${TAB}${INFO}${YW}UniFi OS will be at: ${BGN}https://${VM_IP}:11443${CL}" echo -e "${TAB}${INFO}${YW}UniFi OS will be at: ${BGN}https://${VM_IP}:11443${CL}"
fi fi
@ -819,7 +830,7 @@ if [ "$START_VM" == "yes" ]; then
echo -e "${TAB}${INFO}${YW}Could not auto-detect IP address${CL}" echo -e "${TAB}${INFO}${YW}Could not auto-detect IP address${CL}"
echo -e "${TAB}${INFO}${YW}Use Proxmox Console to login with Cloud-Init credentials${CL}" echo -e "${TAB}${INFO}${YW}Use Proxmox Console to login with Cloud-Init credentials${CL}"
echo -e "${TAB}${INFO}${YW}User: ${BGN}${CLOUDINIT_USER:-root}${CL} / Password: ${BGN}${CLOUDINIT_PASSWORD}${CL}" echo -e "${TAB}${INFO}${YW}User: ${BGN}${CLOUDINIT_USER:-root}${CL} / Password: ${BGN}${CLOUDINIT_PASSWORD}${CL}"
echo -e "${TAB}${INFO}${YW}Monitor installation: ${BL}tail -f /var/log/cloud-init-output.log${CL}" echo -e "${TAB}${INFO}${YW}Monitor installation: ${BL}tail -f /var/log/unifi-install.log${CL}"
fi fi
fi fi