#!/bin/bash # ============================================================ # setup_vnc.sh – Configure Xvnc and XFCE xstartup # VNC state is stored in /data/vnc for persistence # ============================================================ set -euo pipefail log() { echo "[VNC] $*"; } mkdir -p /data/vnc mkdir -p /data/home/.vnc # ── Symlink ~/.vnc → /data/vnc ──────────────────────────────── VNC_LINK=/data/home/.vnc if [ -d "$VNC_LINK" ] && [ ! -L "$VNC_LINK" ]; then cp -rn "$VNC_LINK/." /data/vnc/ 2>/dev/null || true rm -rf "$VNC_LINK" fi if [ ! -L "$VNC_LINK" ]; then ln -sf /data/vnc "$VNC_LINK" log "Symlinked ~/.vnc → /data/vnc" fi # ── VNC password ────────────────────────────────────────────── if [ -n "${VNC_PASSWORD:-}" ]; then echo "$VNC_PASSWORD" | vncpasswd -f > /data/vnc/passwd chmod 600 /data/vnc/passwd log "VNC password set" else # No-auth mode (safe because Xvnc binds localhost only) rm -f /data/vnc/passwd log "VNC running in no-password mode (localhost-only, proxied via nginx)" fi # ── xstartup – launches XFCE session ───────────────────────── cat > /data/vnc/xstartup << 'XSTARTUP' #!/bin/bash # VNC xstartup – launched by Xvnc when a client connects unset SESSION_MANAGER unset DBUS_SESSION_BUS_ADDRESS export DISPLAY=:1 export HOME=/data/home export USER=ubuntu export XDG_CONFIG_HOME=/data/home/.config export XDG_DATA_HOME=/data/home/.local/share export XDG_CACHE_HOME=/data/cache export XDG_RUNTIME_DIR=/tmp/runtime-ubuntu export LANG=en_US.UTF-8 # Ensure runtime dir exists mkdir -p /tmp/runtime-ubuntu chmod 700 /tmp/runtime-ubuntu # Wallpaper / theme baseline (non-fatal) xsetroot -solid "#1a1a2e" 2>/dev/null || true # Start XFCE desktop session exec dbus-launch --exit-with-session startxfce4 XSTARTUP chmod +x /data/vnc/xstartup # ── Xvnc log file ───────────────────────────────────────────── touch /data/logs/xvnc.log touch /data/logs/xfce4.log # ── Ownership ───────────────────────────────────────────────── chown -R ubuntu:ubuntu /data/vnc /data/home/.vnc 2>/dev/null || true log "VNC configuration ready."