#!/bin/bash # Provision the wirecon Alpine LXC on the Proxmox host. # Run as root on the Proxmox host (NOT inside an LXC). # # Defaults: # hostname=concad 1 core 256 MiB RAM 2 GiB disk DHCP on vmbr0 # VMID is auto-picked via `pvesh get /cluster/nextid`. # Override any default by exporting it, e.g.: # HOSTNAME=tunbox MEMORY=512 STORAGE=fast-nvme ./create-lxc.sh # # What it does: # 1. picks next free VMID # 2. downloads the latest alpine template if missing # 3. creates an unprivileged LXC with nesting+keyctl # 4. appends the /dev/net/tun passthrough to the container config # 5. starts the container, waits for DHCP # 6. chains into lxc/bootstrap.sh inside the LXC (which clones + installs) set -euo pipefail if [[ $EUID -ne 0 ]]; then echo "must run as root on the Proxmox host" >&2 exit 1 fi if ! command -v pct >/dev/null; then echo "pct not found — run this on the Proxmox host" >&2 exit 1 fi : "${HOSTNAME:=concad}" : "${CORES:=1}" : "${MEMORY:=256}" : "${DISK:=2}" : "${BRIDGE:=vmbr0}" : "${STORAGE:=local-lvm}" : "${TEMPLATE_STORAGE:=local}" : "${WIRECON_REPO:=https://git.d4nn13.de/d4nn13/wirecon.git}" : "${WIRECON_REF:=main}" : "${BOOTSTRAP_URL:=https://git.d4nn13.de/d4nn13/wirecon/raw/branch/${WIRECON_REF}/lxc/bootstrap.sh}" log() { printf '[create-lxc] %s\n' "$*"; } # --- interactive prompts (web UI + WG network) ------------------------------ # All settings can be preset via env vars; the prompt is skipped when set. # WIRECON_WEB_AUTOSTART=yes|no, WIRECON_NET=CIDR, WIRECON_PEER_IP=ip, # WIRECON_CLIENT_IP=ip. _ask() { # _ask local var="$1" prompt="$2" default="$3" ans="" if [[ -n "${!var:-}" ]]; then return; fi if [[ -r /dev/tty && -w /dev/tty ]]; then printf '%s [%s]: ' "$prompt" "$default" >/dev/tty IFS= read -r ans /dev/tty IFS= read -r _ans /dev/null tmpl=$(pveam available --section system \ | awk '{print $2}' \ | grep -E '^alpine-[0-9]+\.[0-9]+-default_' \ | sort -V | tail -n1 || true) if [[ -z "$tmpl" ]]; then echo "no alpine-*-default template found in pveam available" >&2 exit 1 fi log "template: $tmpl" if ! pveam list "$TEMPLATE_STORAGE" | awk '{print $1}' | grep -q "/$tmpl$"; then log "downloading $tmpl to $TEMPLATE_STORAGE" pveam download "$TEMPLATE_STORAGE" "$tmpl" else log "template already present in $TEMPLATE_STORAGE" fi # --- create container ------------------------------------------------------- log "pct create $VMID" pct create "$VMID" "$TEMPLATE_STORAGE:vztmpl/$tmpl" \ --ostype alpine \ --hostname "$HOSTNAME" \ --cores "$CORES" \ --memory "$MEMORY" \ --swap 0 \ --rootfs "${STORAGE}:${DISK}" \ --net0 "name=eth0,bridge=${BRIDGE},firewall=1,ip=dhcp" \ --unprivileged 1 \ --features nesting=1,keyctl=1 \ --onboot 1 \ --start 0 # --- tun passthrough -------------------------------------------------------- CONF="/etc/pve/lxc/${VMID}.conf" if ! grep -q 'lxc.mount.entry: /dev/net/tun' "$CONF"; then log "appending /dev/net/tun passthrough to $CONF" cat >> "$CONF" <<'EOF' # wirecon: kernel WireGuard in unprivileged LXC lxc.cgroup2.devices.allow: c 10:200 rwm lxc.mount.entry: /dev/net/tun dev/net/tun none bind,create=file 0 0 EOF fi # --- start + wait for DHCP -------------------------------------------------- log "starting container" pct start "$VMID" log "waiting for eth0 to get an IPv4" for _ in $(seq 1 45); do if pct exec "$VMID" -- sh -c 'ip -4 addr show eth0 | grep -q "inet "' 2>/dev/null; then break fi sleep 1 done ip=$(pct exec "$VMID" -- sh -c "ip -4 -o addr show eth0 | awk '{print \$4}' | cut -d/ -f1" 2>/dev/null || echo "") log "container IPv4: ${ip:-}" # --- chain into bootstrap --------------------------------------------------- log "installing bootstrap prereqs inside LXC" pct exec "$VMID" -- sh -c 'apk add --no-cache bash curl ca-certificates >/dev/null' log "running bootstrap from $BOOTSTRAP_URL" pct exec "$VMID" -- sh -c "curl -fsSL '$BOOTSTRAP_URL' \ | WIRECON_REPO='$WIRECON_REPO' \ WIRECON_REF='$WIRECON_REF' \ WIRECON_NET='$WIRECON_NET' \ WIRECON_PEER_IP='$WIRECON_PEER_IP' \ WIRECON_CLIENT_IP='$WIRECON_CLIENT_IP' \ WIRECON_LAN_CIDR='$WIRECON_LAN_CIDR' \ WIRECON_WEB_AUTOSTART='$WIRECON_WEB_AUTOSTART' \ bash" # --- tty1 auto-login (for Proxmox web Console) ------------------------------ # Drop a /usr/local/sbin/autologin-root wrapper and point getty at it so the # Proxmox "Console" button lands directly in a root shell. Security posture # is unchanged in practice — anyone who can open the web console already has # `pct enter` on the host. log "enabling tty1 auto-login" pct exec "$VMID" -- sh -c 'mkdir -p /usr/local/sbin && cat > /usr/local/sbin/autologin-root && chmod 0755 /usr/local/sbin/autologin-root' <<'AUTOLOGIN' #!/bin/sh exec /bin/login -f root AUTOLOGIN pct exec "$VMID" -- sh -c ' # Drop ANY existing getty line for tty1 (Alpine LXC inittab uses an empty # id field: "::respawn:/sbin/getty 38400 tty1"), then add the auto-login # variant with an explicit id so our line wins on any future re-run. sed -i -E "/getty.*tty1$/d" /etc/inittab echo "tty1::respawn:/sbin/getty -n -l /usr/local/sbin/autologin-root 38400 tty1" >> /etc/inittab # Reload init and kick any running getty so it respawns from the new config. kill -HUP 1 2>/dev/null || true pkill -KILL getty 2>/dev/null || true ' # --- final summary ---------------------------------------------------------- # install.sh (run by bootstrap) has already enabled + started wg-quick, # watchdog, monitor, and (if WIRECON_WEB_AUTOSTART=yes) the web UI. Keys # aren't filled in yet — wg-quick will be failing until you edit wg0.conf # in the web UI (or via the Proxmox Console). if [[ "$WIRECON_WEB_AUTOSTART" == "yes" ]]; then WEB_URL="http://${ip:-}:8080/" else WEB_URL="" fi cat <