Update unifi-os-vm.sh
This commit is contained in:
parent
189b8569c4
commit
3b5890d796
@ -620,7 +620,7 @@ 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
|
||||||
|
|
||||||
# Create first-boot installation script (suppress all stderr)
|
# Always create first-boot installation script as fallback
|
||||||
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
|
||||||
set -e
|
set -e
|
||||||
@ -628,11 +628,16 @@ set -e
|
|||||||
exec > /var/log/install-unifi.log 2>&1
|
exec > /var/log/install-unifi.log 2>&1
|
||||||
echo \"[\$(date)] Starting UniFi OS installation on first boot\"
|
echo \"[\$(date)] Starting UniFi OS installation on first boot\"
|
||||||
|
|
||||||
# Wait for cloud-init to complete first
|
# Check if already installed
|
||||||
echo \"[\$(date)] Waiting for cloud-init to complete...\"
|
if [ -f /root/.unifi-installed ]; then
|
||||||
|
echo \"[\$(date)] UniFi OS already installed, exiting\"
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Wait for cloud-init to complete if present
|
||||||
if command -v cloud-init >/dev/null 2>&1; then
|
if command -v cloud-init >/dev/null 2>&1; then
|
||||||
|
echo \"[\$(date)] Waiting for cloud-init to complete...\"
|
||||||
cloud-init status --wait 2>/dev/null || true
|
cloud-init status --wait 2>/dev/null || true
|
||||||
echo \"[\$(date)] Cloud-init completed\"
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Wait for network to be fully available
|
# Wait for network to be fully available
|
||||||
@ -646,11 +651,7 @@ for i in {1..60}; do
|
|||||||
sleep 2
|
sleep 2
|
||||||
done
|
done
|
||||||
|
|
||||||
# Wait for systemd-resolved to be ready
|
# Configure DNS
|
||||||
echo \"[\$(date)] Waiting for DNS resolution...\"
|
|
||||||
systemctl is-active systemd-resolved >/dev/null 2>&1 || systemctl start systemd-resolved
|
|
||||||
|
|
||||||
# Configure DNS with multiple fallbacks
|
|
||||||
echo \"[\$(date)] Configuring DNS\"
|
echo \"[\$(date)] Configuring DNS\"
|
||||||
mkdir -p /etc/systemd/resolved.conf.d
|
mkdir -p /etc/systemd/resolved.conf.d
|
||||||
cat > /etc/systemd/resolved.conf.d/dns.conf << DNSEOF
|
cat > /etc/systemd/resolved.conf.d/dns.conf << DNSEOF
|
||||||
@ -658,162 +659,82 @@ cat > /etc/systemd/resolved.conf.d/dns.conf << DNSEOF
|
|||||||
DNS=8.8.8.8 1.1.1.1
|
DNS=8.8.8.8 1.1.1.1
|
||||||
FallbackDNS=8.8.4.4 1.0.0.1
|
FallbackDNS=8.8.4.4 1.0.0.1
|
||||||
DNSEOF
|
DNSEOF
|
||||||
systemctl restart systemd-resolved
|
systemctl restart systemd-resolved 2>/dev/null || true
|
||||||
sleep 3
|
sleep 3
|
||||||
|
|
||||||
# Test DNS resolution
|
# Wait for apt locks to be released
|
||||||
echo \"[\$(date)] Testing DNS resolution...\"
|
|
||||||
for i in {1..10}; do
|
|
||||||
if nslookup archive.ubuntu.com >/dev/null 2>&1 || host archive.ubuntu.com >/dev/null 2>&1; then
|
|
||||||
echo \"[\$(date)] DNS resolution working\"
|
|
||||||
break
|
|
||||||
fi
|
|
||||||
echo \"[\$(date)] DNS not ready, waiting... attempt \$i/10\"
|
|
||||||
sleep 2
|
|
||||||
done
|
|
||||||
|
|
||||||
# Wait for apt locks to be released (cloud-init might still be updating)
|
|
||||||
echo \"[\$(date)] Waiting for package manager to be ready...\"
|
echo \"[\$(date)] Waiting for package manager to be ready...\"
|
||||||
while fuser /var/lib/dpkg/lock-frontend >/dev/null 2>&1 || fuser /var/lib/apt/lists/lock >/dev/null 2>&1; do
|
for i in {1..30}; do
|
||||||
echo \"[\$(date)] Waiting for other package managers to finish...\"
|
if ! fuser /var/lib/dpkg/lock-frontend >/dev/null 2>&1 && ! fuser /var/lib/apt/lists/lock >/dev/null 2>&1; then
|
||||||
sleep 5
|
echo \"[\$(date)] Package manager is ready\"
|
||||||
done
|
|
||||||
|
|
||||||
# Update package lists with retries
|
|
||||||
echo \"[\$(date)] Updating package lists\"
|
|
||||||
for i in {1..5}; do
|
|
||||||
if apt-get update -y; then
|
|
||||||
echo \"[\$(date)] Package lists updated successfully\"
|
|
||||||
break
|
break
|
||||||
fi
|
fi
|
||||||
echo \"[\$(date)] apt-get update failed, retrying in 5s... attempt \$i/5\"
|
echo \"[\$(date)] Waiting for other package managers to finish... attempt \$i/30\"
|
||||||
sleep 5
|
sleep 5
|
||||||
done
|
done
|
||||||
|
|
||||||
# Install base packages with proper error handling
|
# Update package lists
|
||||||
|
echo \"[\$(date)] Updating package lists\"
|
||||||
|
apt-get update
|
||||||
|
|
||||||
|
# Install base packages
|
||||||
echo \"[\$(date)] Installing base packages (this may take several minutes)\"
|
echo \"[\$(date)] Installing base packages (this may take several minutes)\"
|
||||||
DEBIAN_FRONTEND=noninteractive apt-get install -y \
|
DEBIAN_FRONTEND=noninteractive apt-get install -y \
|
||||||
qemu-guest-agent \
|
qemu-guest-agent curl wget ca-certificates podman uidmap slirp4netns iptables 2>/dev/null || true
|
||||||
curl \
|
|
||||||
wget \
|
|
||||||
ca-certificates \
|
|
||||||
gnupg \
|
|
||||||
lsb-release \
|
|
||||||
software-properties-common \
|
|
||||||
apt-transport-https \
|
|
||||||
podman \
|
|
||||||
uidmap \
|
|
||||||
slirp4netns \
|
|
||||||
fuse-overlayfs \
|
|
||||||
iptables \
|
|
||||||
iproute2 \
|
|
||||||
dbus-user-session \
|
|
||||||
systemd-container 2>&1
|
|
||||||
|
|
||||||
if [ \$? -eq 0 ]; then
|
# Start QEMU Guest Agent
|
||||||
echo \"[\$(date)] ✓ Packages installed successfully\"
|
|
||||||
else
|
|
||||||
echo \"[\$(date)] ⚠ Some packages failed, retrying essential packages...\"
|
|
||||||
sleep 5
|
|
||||||
DEBIAN_FRONTEND=noninteractive apt-get install -y \
|
|
||||||
qemu-guest-agent curl wget ca-certificates podman uidmap slirp4netns iptables 2>&1
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Start and enable QEMU Guest Agent
|
|
||||||
echo \"[\$(date)] Starting QEMU Guest Agent\"
|
echo \"[\$(date)] Starting QEMU Guest Agent\"
|
||||||
systemctl enable qemu-guest-agent 2>/dev/null || true
|
systemctl enable qemu-guest-agent 2>/dev/null || true
|
||||||
systemctl start qemu-guest-agent 2>/dev/null || true
|
systemctl start qemu-guest-agent 2>/dev/null || true
|
||||||
systemctl status qemu-guest-agent --no-pager | head -3
|
|
||||||
|
|
||||||
# Configure Podman properly
|
# Configure Podman
|
||||||
echo \"[\$(date)] Configuring Podman\"
|
echo \"[\$(date)] Configuring Podman\"
|
||||||
# Enable lingering for root user (allows rootless podman)
|
|
||||||
loginctl enable-linger root 2>/dev/null || true
|
loginctl enable-linger root 2>/dev/null || true
|
||||||
|
|
||||||
# Start podman socket
|
|
||||||
systemctl enable podman.socket 2>/dev/null || true
|
systemctl enable podman.socket 2>/dev/null || true
|
||||||
systemctl start podman.socket 2>/dev/null || true
|
systemctl start podman.socket 2>/dev/null || true
|
||||||
|
|
||||||
# Verify Podman is working
|
# Verify Podman
|
||||||
echo \"[\$(date)] Verifying Podman installation\"
|
echo \"[\$(date)] Verifying Podman installation\"
|
||||||
if podman --version; then
|
podman --version || echo \"WARNING: Podman not responding\"
|
||||||
echo \"[\$(date)] ✓ Podman is working\"
|
|
||||||
podman info 2>&1 | grep -E '(host|store|runRoot)' || true
|
|
||||||
else
|
|
||||||
echo \"[\$(date)] ✗ WARNING: Podman not responding\"
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Download UniFi OS installer
|
# Download UniFi OS installer
|
||||||
echo \"[\$(date)] Downloading UniFi OS Server ${UOS_VERSION}\"
|
echo \"[\$(date)] Downloading UniFi OS Server ${UOS_VERSION}\"
|
||||||
for i in {1..3}; do
|
curl -fsSL '${UOS_URL}' -o /root/${UOS_INSTALLER}
|
||||||
if curl -fsSL '${UOS_URL}' -o /root/${UOS_INSTALLER}; then
|
|
||||||
echo \"[\$(date)] UniFi OS installer downloaded successfully\"
|
|
||||||
break
|
|
||||||
fi
|
|
||||||
echo \"[\$(date)] Download failed, retrying... attempt \$i/3\"
|
|
||||||
sleep 5
|
|
||||||
done
|
|
||||||
|
|
||||||
chmod +x /root/${UOS_INSTALLER}
|
chmod +x /root/${UOS_INSTALLER}
|
||||||
|
|
||||||
# Run UniFi OS installer
|
# Run UniFi OS installer
|
||||||
echo \"[\$(date)] Running UniFi OS installer (this will take 2-5 minutes)\"
|
echo \"[\$(date)] Running UniFi OS installer\"
|
||||||
echo \"[\$(date)] Installer output:\"
|
/root/${UOS_INSTALLER} install 2>&1 || echo \"Installation returned exit code \$?\"
|
||||||
if /root/${UOS_INSTALLER} install 2>&1; then
|
|
||||||
echo \"[\$(date)] UniFi OS installation completed successfully\"
|
|
||||||
else
|
|
||||||
echo \"[\$(date)] Installation exited with code \$?, checking status...\"
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Wait for installation to settle
|
# Wait and start UniFi OS Server
|
||||||
sleep 10
|
sleep 10
|
||||||
|
|
||||||
# Check if uosserver command exists and user was created
|
|
||||||
if command -v uosserver >/dev/null 2>&1; then
|
if command -v uosserver >/dev/null 2>&1; then
|
||||||
echo \"[\$(date)] UniFi OS Server command found\"
|
echo \"[\$(date)] Starting UniFi OS Server\"
|
||||||
|
|
||||||
if id -u uosserver >/dev/null 2>&1; then
|
if id -u uosserver >/dev/null 2>&1; then
|
||||||
echo \"[\$(date)] Starting UniFi OS Server as uosserver user\"
|
su - uosserver -c 'uosserver start' 2>&1 || true
|
||||||
su - uosserver -c 'uosserver start' 2>&1 || {
|
|
||||||
echo \"[\$(date)] Failed to start as user, trying direct command\"
|
|
||||||
uosserver start 2>&1 || true
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
echo \"[\$(date)] Starting UniFi OS Server as root\"
|
|
||||||
uosserver start 2>&1 || true
|
uosserver start 2>&1 || true
|
||||||
fi
|
fi
|
||||||
|
|
||||||
sleep 5
|
|
||||||
|
|
||||||
# Check if service is running
|
|
||||||
if pgrep -f uosserver >/dev/null 2>&1 || systemctl is-active unifi-os >/dev/null 2>&1; then
|
|
||||||
IP=\$(hostname -I | awk '{print \$1}')
|
IP=\$(hostname -I | awk '{print \$1}')
|
||||||
echo \"[\$(date)] ✓ UniFi OS Server is running\"
|
echo \"[\$(date)] ✓ UniFi OS Server installed - Access at: https://\${IP}:11443\"
|
||||||
echo \"[\$(date)] ✓ Access at: https://\${IP}:11443\"
|
|
||||||
else
|
|
||||||
echo \"[\$(date)] ⚠ UniFi OS Server may not be running, check manually\"
|
|
||||||
fi
|
|
||||||
else
|
else
|
||||||
echo \"[\$(date)] ✗ ERROR: uosserver command not found after installation\"
|
echo \"[\$(date)] ✗ ERROR: uosserver command not found\"
|
||||||
echo \"[\$(date)] Installation log contents:\"
|
|
||||||
ls -la /root/ | grep -i unifi || true
|
|
||||||
echo \"[\$(date)] Checking for error logs:\"
|
|
||||||
find /root /var/log -name '*unifi*' -o -name '*uos*' 2>/dev/null || true
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo \"[\$(date)] First boot installation script completed\"
|
# Create completion flag
|
||||||
# Self-destruct this installation script
|
echo \"[\$(date)] Installation completed\"
|
||||||
rm -f /root/install-unifi.sh
|
touch /root/.unifi-installed
|
||||||
INSTALLEOF 2>/dev/null
|
INSTALLEOF" >/dev/null
|
||||||
chmod +x /root/install-unifi.sh" 2>/dev/null
|
|
||||||
|
|
||||||
# Set up systemd service for first boot (suppress warnings)
|
virt-customize -q -a "${FILE}" --run-command "chmod +x /root/install-unifi.sh" >/dev/null
|
||||||
|
|
||||||
|
# Create systemd service
|
||||||
virt-customize -q -a "${FILE}" --run-command "cat > /etc/systemd/system/unifi-firstboot.service << 'SVCEOF'
|
virt-customize -q -a "${FILE}" --run-command "cat > /etc/systemd/system/unifi-firstboot.service << 'SVCEOF'
|
||||||
[Unit]
|
[Unit]
|
||||||
Description=UniFi OS First Boot Setup
|
Description=UniFi OS First Boot Setup
|
||||||
After=network-online.target
|
After=network-online.target
|
||||||
Wants=network-online.target
|
Wants=network-online.target
|
||||||
ConditionPathExists=/root/install-unifi.sh
|
ConditionPathExists=!/root/.unifi-installed
|
||||||
|
|
||||||
[Service]
|
[Service]
|
||||||
Type=oneshot
|
Type=oneshot
|
||||||
@ -822,8 +743,36 @@ RemainAfterExit=yes
|
|||||||
|
|
||||||
[Install]
|
[Install]
|
||||||
WantedBy=multi-user.target
|
WantedBy=multi-user.target
|
||||||
SVCEOF 2>/dev/null
|
SVCEOF" >/dev/null
|
||||||
systemctl enable unifi-firstboot.service" 2>/dev/null
|
|
||||||
|
virt-customize -q -a "${FILE}" --run-command "systemctl enable unifi-firstboot.service" >/dev/null
|
||||||
|
|
||||||
|
# Try to install base packages during image customization (faster startup if it works)
|
||||||
|
UNIFI_PREINSTALLED="no"
|
||||||
|
|
||||||
|
msg_info "Pre-installing base packages (qemu-guest-agent, podman, curl)"
|
||||||
|
if virt-customize -a "${FILE}" --install qemu-guest-agent,curl,ca-certificates,podman,uidmap,slirp4netns >/dev/null 2>&1; then
|
||||||
|
msg_ok "Pre-installed base packages"
|
||||||
|
|
||||||
|
msg_info "Pre-installing UniFi OS Server ${UOS_VERSION}"
|
||||||
|
if virt-customize -q -a "${FILE}" --run-command "curl -fsSL '${UOS_URL}' -o /root/${UOS_INSTALLER} && chmod +x /root/${UOS_INSTALLER} && /root/${UOS_INSTALLER} install && touch /root/.unifi-installed" >/dev/null 2>&1; then
|
||||||
|
msg_ok "Pre-installed UniFi OS Server (first-boot script will be skipped)"
|
||||||
|
UNIFI_PREINSTALLED="yes"
|
||||||
|
else
|
||||||
|
msg_info "Pre-installation failed, will install on first boot"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
msg_info "Pre-installation not possible, will install on first boot"
|
||||||
|
fi# 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'" 2>/dev/null
|
||||||
|
fi
|
||||||
|
|
||||||
|
msg_ok "UniFi OS Installer integrated (will run on first boot)"
|
||||||
|
|
||||||
|
fi
|
||||||
|
|
||||||
# Add auto-login if Cloud-Init is disabled
|
# Add auto-login if Cloud-Init is disabled
|
||||||
if [ "$USE_CLOUD_INIT" != "yes" ]; then
|
if [ "$USE_CLOUD_INIT" != "yes" ]; then
|
||||||
@ -832,7 +781,11 @@ if [ "$USE_CLOUD_INIT" != "yes" ]; then
|
|||||||
--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'" 2>/dev/null
|
--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'" 2>/dev/null
|
||||||
fi
|
fi
|
||||||
|
|
||||||
msg_ok "UniFi OS Installer integrated (will run on first boot)"
|
if [ "$UNIFI_PREINSTALLED" = "yes" ]; then
|
||||||
|
msg_ok "UniFi OS Server ${UOS_VERSION} pre-installed in image"
|
||||||
|
else
|
||||||
|
msg_ok "UniFi OS Server will be installed on first boot"
|
||||||
|
fi
|
||||||
|
|
||||||
# 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}"
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user