Files
wirecon/proxmox/create-lxc.sh
T
d4nn13 1ab70a6f9a create-lxc: replace password flow with passwordless SSH key injection
Always authorize the Proxmox host's root pubkey (generate an ed25519
pair if missing) so `ssh root@<lxc>` from the host works immediately.
Extra keys via SSH_PUBKEY (literal or path). Relies on Alpine's default
PermitRootLogin=prohibit-password — no sshd_config edits, no password
ever set; pct enter stays as fallback.
2026-04-19 19:15:46 +02:00

196 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' "$*"; }
# --- SSH key injection ------------------------------------------------------
# Passwordless access. Always authorize the Proxmox host's root key (create
# an ed25519 pair if none exists) so `ssh root@<lxc>` from *this* host works
# out of the box. Additional keys via SSH_PUBKEY (one key per line, or a
# path to a file containing keys).
PUBKEYS=""
HOST_KEY="${HOST_KEY:-/root/.ssh/id_ed25519}"
if [[ ! -f "$HOST_KEY" ]]; then
log "generating Proxmox root SSH key at $HOST_KEY (none existed)"
install -d -m 0700 /root/.ssh
ssh-keygen -t ed25519 -N "" -f "$HOST_KEY" -C "root@$(hostname)" >/dev/null
fi
PUBKEYS="$(cat "${HOST_KEY}.pub")"
if [[ -n "${SSH_PUBKEY:-}" ]]; then
# treat as path if it looks like one, otherwise inline value
if [[ -f "$SSH_PUBKEY" ]]; then
PUBKEYS+=$'\n'"$(cat "$SSH_PUBKEY")"
else
PUBKEYS+=$'\n'"$SSH_PUBKEY"
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"
# --- install sshd + authorize keys (passwordless) ---------------------------
log "installing sshd + authorizing $(printf '%s\n' "$PUBKEYS" | grep -c '^ssh-') key(s)"
# Alpine's default sshd_config is `PermitRootLogin prohibit-password` which
# allows root login with keys and rejects passwords — exactly what we want.
# No sshd_config edits needed.
printf '%s\n' "$PUBKEYS" | pct exec "$VMID" -- sh -c '
apk add --no-cache openssh >/dev/null
install -d -m 0700 /root/.ssh
cat >> /root/.ssh/authorized_keys
sort -u /root/.ssh/authorized_keys -o /root/.ssh/authorized_keys
chmod 0600 /root/.ssh/authorized_keys
rc-update add sshd default >/dev/null
rc-service sshd restart >/dev/null
'
# --- 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.
[create-lxc] shell: ssh root@${ip:-<lxc-ip>} (passwordless, key-only)
${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 over ssh / pct enter)
- 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