Files
wirecon/proxmox/create-lxc.sh
T

198 lines
7.0 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' "$*"; }
# --- root password ----------------------------------------------------------
# Accept ROOT_PASSWORD from env; otherwise prompt on the controlling TTY
# (works even when invoked via `curl … | bash` because we read from /dev/tty).
# If neither is available, fall back to the no-password default — `pct enter`
# from the Proxmox host is still possible.
if [[ -z "${ROOT_PASSWORD:-}" ]]; then
if [[ -r /dev/tty && -w /dev/tty ]]; then
exec 3</dev/tty
printf 'root password for container (empty to skip): ' >/dev/tty
IFS= read -rs ROOT_PASSWORD <&3
printf '\n' >/dev/tty
if [[ -n "$ROOT_PASSWORD" ]]; then
printf 'confirm: ' >/dev/tty
IFS= read -rs ROOT_PASSWORD_CONFIRM <&3
printf '\n' >/dev/tty
if [[ "$ROOT_PASSWORD" != "$ROOT_PASSWORD_CONFIRM" ]]; then
echo "passwords do not match" >&2
exit 1
fi
fi
exec 3<&-
else
log "no ROOT_PASSWORD set and no TTY — root will have no password (pct enter only)"
fi
fi
# --- web UI autostart -------------------------------------------------------
# WIRECON_WEB_AUTOSTART=yes|no overrides the prompt. Default: yes (the UI is
# how you'll edit wg0.conf / wirecon.conf without drop-to-shell).
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
# --- 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' bash"
# --- optional: set root password + enable sshd -----------------------------
if [[ -n "${ROOT_PASSWORD:-}" ]]; then
log "setting root password"
# Pipe via stdin so the secret never appears in argv / ps.
printf 'root:%s\n' "$ROOT_PASSWORD" | pct exec "$VMID" -- chpasswd
log "installing + enabling sshd (PermitRootLogin yes)"
pct exec "$VMID" -- sh -c '
apk add --no-cache openssh >/dev/null
sed -i "s/^#\?PermitRootLogin.*/PermitRootLogin yes/" /etc/ssh/sshd_config
grep -q "^PermitRootLogin yes" /etc/ssh/sshd_config || echo "PermitRootLogin yes" >> /etc/ssh/sshd_config
rc-update add sshd default
rc-service sshd restart
'
fi
# --- optional: start web UI -------------------------------------------------
if [[ "$WIRECON_WEB_AUTOSTART" == "yes" ]]; then
log "enabling + starting wirecon-web"
pct exec "$VMID" -- sh -c 'rc-update add wirecon-web default && rc-service wirecon-web start'
WEB_URL="http://${ip:-<lxc-ip>}:8080/"
else
log "wirecon-web left disabled (enable later: rc-update add wirecon-web default && rc-service wirecon-web start)"
WEB_URL=""
fi
cat <<EOF
[create-lxc] container $VMID ($HOSTNAME) provisioned.
${WEB_URL:+[create-lxc] web UI: $WEB_URL}
[create-lxc] finish configuration:
- edit /etc/wireguard/wg0.conf and /etc/wirecon/wirecon.conf
(via the web UI above, or: pct enter $VMID && vi …)
- then start the tunnel services:
rc-update add wg-quick.wg0 default && rc-service wg-quick.wg0 start
rc-update add wirecon-watchdog default && rc-service wirecon-watchdog start
rc-update add wirecon-monitor default && rc-service wirecon-monitor start
- tail -F /var/log/wirecon-watchdog.log /var/log/wirecon-monitor.log
EOF