Files
wirecon/proxmox/create-lxc.sh
T
d4nn13andClaude Opus 4.7 78c3a790be auto-start services, prompt for WG + LAN nets, fix busybox httpd applet
- create-lxc.sh prompts for WIRECON_NET / PEER_IP / CLIENT_IP / LAN_CIDR
  (defaults 10.13.13.0/24, .1, .2, 192.168.1.0/24) and passes them through
  to the in-container bootstrap
- install.sh generates a placeholder /etc/wireguard/wg0.conf from those
  env vars instead of copying the static example, and updates PEER_IP in
  wirecon.conf on first install
- install.sh always enables + starts wg-quick, watchdog, monitor and
  (if WIRECON_WEB_AUTOSTART=yes, default yes) the web UI, even before
  keys are filled in — wg-quick will fail until then, and the web UI
  is the way to finish configuration
- apk installs busybox-extras and wirecon-web now runs /usr/sbin/httpd
  (fixes "httpd: applet not found" on base busybox)
- create-lxc.sh drops its own web-UI start block (install.sh owns it)

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-19 19:41:44 +02:00

205 lines
7.6 KiB
Bash
Executable File

#!/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 <varname> <prompt> <default>
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 || ans=""
printf -v "$var" '%s' "${ans:-$default}"
else
printf -v "$var" '%s' "$default"
fi
}
if [[ -z "${WIRECON_WEB_AUTOSTART:-}" ]]; then
if [[ -r /dev/tty && -w /dev/tty ]]; then
printf 'start web UI automatically on boot? [Y/n] ' >/dev/tty
IFS= read -r _ans </dev/tty || _ans=""
case "$_ans" in n|N|no|NO) WIRECON_WEB_AUTOSTART=no ;; *) WIRECON_WEB_AUTOSTART=yes ;; esac
else
WIRECON_WEB_AUTOSTART=yes
fi
fi
_ask WIRECON_NET "wireguard tunnel network (CIDR)" "10.13.13.0/24"
_ask WIRECON_PEER_IP "wireguard server (peer) IP" "10.13.13.1"
_ask WIRECON_CLIENT_IP "wireguard client (this LXC) IP" "10.13.13.2"
_ask WIRECON_LAN_CIDR "local LAN subnet (CIDR)" "192.168.1.0/24"
log "wg net=$WIRECON_NET peer=$WIRECON_PEER_IP client=$WIRECON_CLIENT_IP lan=$WIRECON_LAN_CIDR web_autostart=$WIRECON_WEB_AUTOSTART"
# --- pick VMID --------------------------------------------------------------
VMID=$(pvesh get /cluster/nextid)
log "VMID=$VMID hostname=$HOSTNAME cores=$CORES mem=${MEMORY}MiB disk=${DISK}GiB"
# --- find + download template ----------------------------------------------
log "refreshing template index"
pveam update >/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:-<not ready>}"
# --- 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 '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:-<lxc-ip>}:8080/"
else
WEB_URL=""
fi
cat <<EOF
[create-lxc] container $VMID ($HOSTNAME) provisioned.
[create-lxc] shell: open Console in the Proxmox UI → root shell, no prompt
[create-lxc] (or from the host: pct enter $VMID)
${WEB_URL:+[create-lxc] web UI: $WEB_URL}
[create-lxc] all services are already enabled + running; wg-quick will
[create-lxc] fail until keys are configured. Finish setup by editing
[create-lxc] /etc/wireguard/wg0.conf (PrivateKey, Peer PublicKey, Endpoint)
[create-lxc] /etc/wirecon/wirecon.conf (thresholds — optional)
[create-lxc] via the web UI above, then:
[create-lxc] rc-service wg-quick.wg0 restart
[create-lxc] tail -F /var/log/wirecon-watchdog.log /var/log/wirecon-monitor.log
EOF