Access is now via the Proxmox web Console (or pct enter) — no SSH, no keys, no passwords. /etc/inittab's tty1 line is rewritten to `getty -n -l /usr/local/sbin/autologin-root` which execs `login -f root`, dropping straight into a root shell when the Console button is clicked.
179 lines
6.4 KiB
Bash
Executable File
179 lines
6.4 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' "$*"; }
|
|
|
|
# --- 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"
|
|
|
|
# --- 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 '
|
|
# Replace the tty1 getty line; if none exists, append one.
|
|
if grep -qE "^tty1::" /etc/inittab; then
|
|
sed -i "s|^tty1::respawn:.*|tty1::respawn:/sbin/getty -n -l /usr/local/sbin/autologin-root 38400 tty1|" /etc/inittab
|
|
else
|
|
echo "tty1::respawn:/sbin/getty -n -l /usr/local/sbin/autologin-root 38400 tty1" >> /etc/inittab
|
|
fi
|
|
kill -HUP 1 2>/dev/null || true
|
|
'
|
|
|
|
# --- 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: 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] finish configuration:
|
|
- edit /etc/wireguard/wg0.conf and /etc/wirecon/wirecon.conf
|
|
(via the web UI above, or the Proxmox Console)
|
|
- 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
|