port LXC side to Alpine/OpenRC, add Proxmox host-side creator
- Replace systemd units with OpenRC init scripts (supervise-daemon) - Add wirecon-watchdog-loop.sh since OpenRC has no timers - Switch install.sh to apk + rc-update - Update bootstrap.sh to install git/bash via apk - New proxmox/create-lxc.sh: picks next VMID, downloads Alpine template, creates unprivileged LXC with tun passthrough, chains bootstrap - Rewrite README around the Proxmox one-liner flow
This commit is contained in:
1 parent
390e10b06d
commit
5b9abdae08
13 files changed
+290
-128
No files matched your search
@@ -1,35 +1,54 @@
|
||||
# wirecon
|
||||
|
||||
WireGuard tunnel between a **rootless-Docker** internet host and an **unprivileged LXC** on Proxmox, with Caddy reverse-proxying inbound traffic into the LXC's LAN. Tuned for flaky uplinks (Telekom DSL/LTE Hybrid) with a fast-reconnect watchdog and structured journald logging.
|
||||
WireGuard tunnel between a **rootless-Docker** internet host and an **unprivileged Alpine LXC** on Proxmox, with Caddy reverse-proxying inbound traffic into the LXC's LAN. Tuned for flaky uplinks (Telekom DSL/LTE Hybrid) with a fast-reconnect watchdog and structured logging.
|
||||
|
||||
```
|
||||
internet Telekom Hybrid (DSL+LTE)
|
||||
clients ──► Docker host ══════ wg0 (UDP 51820) ══════ LXC ──► 192.168.1.0/24
|
||||
(Caddy + (kernel WG +
|
||||
wireguard-go, watchdog +
|
||||
rootless) monitor)
|
||||
(Caddy + (Alpine + kernel WG
|
||||
wireguard-go, + OpenRC watchdog
|
||||
rootless) & monitor)
|
||||
```
|
||||
|
||||
## Install
|
||||
|
||||
### LXC side — one-liner
|
||||
### Proxmox side — one-liner (creates the LXC from scratch)
|
||||
|
||||
On a fresh unprivileged LXC (after you've added the tun passthrough from `proxmox/lxc.conf.snippet` and restarted the container):
|
||||
Run on the **Proxmox host** as root:
|
||||
|
||||
```
|
||||
curl -fsSL https://git.d4nn13.de/d4nn13/wirecon/raw/branch/main/proxmox/create-lxc.sh | bash
|
||||
```
|
||||
|
||||
Defaults: Alpine template (latest), hostname `concad`, 1 core, 256 MiB RAM, 2 GiB disk, DHCP on `vmbr0`, auto-picked VMID. The script creates the container, applies the `/dev/net/tun` passthrough, starts it, and chains into the in-LXC bootstrap which clones the repo and runs `lxc/install.sh`. Override defaults via env vars:
|
||||
|
||||
```
|
||||
HOSTNAME=tunbox MEMORY=512 STORAGE=fast-nvme \
|
||||
bash -c "$(curl -fsSL https://git.d4nn13.de/d4nn13/wirecon/raw/branch/main/proxmox/create-lxc.sh)"
|
||||
```
|
||||
|
||||
### LXC side only (if the container already exists)
|
||||
|
||||
Inside the unprivileged LXC as root:
|
||||
|
||||
```
|
||||
curl -fsSL https://git.d4nn13.de/d4nn13/wirecon/raw/branch/main/lxc/bootstrap.sh | sudo bash
|
||||
```
|
||||
|
||||
Then edit `/etc/wireguard/wg0.conf` (keys, endpoint, LAN iface in the `PostUp` rules), optionally tune `/etc/wirecon/wirecon.conf`, and:
|
||||
### Finishing up (either path)
|
||||
|
||||
Edit `/etc/wireguard/wg0.conf` (keys, endpoint, LAN iface in `PostUp`), optionally tune `/etc/wirecon/wirecon.conf`, then:
|
||||
|
||||
```
|
||||
systemctl enable --now wg-quick@wg0 wirecon-watchdog.timer wirecon-monitor.service
|
||||
journalctl -f -t wirecon-watchdog -t wirecon-monitor
|
||||
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
|
||||
```
|
||||
|
||||
### Docker side
|
||||
### Docker side (internet host)
|
||||
|
||||
On the internet host (rootless Docker daemon already running):
|
||||
Rootless Docker daemon already running:
|
||||
|
||||
```
|
||||
git clone https://git.d4nn13.de/d4nn13/wirecon.git
|
||||
@@ -43,23 +62,16 @@ Host prereqs:
|
||||
- `/dev/net/tun` readable by the rootless user
|
||||
- `sysctl net.ipv4.ip_unprivileged_port_start=80` if you want 80/443 bound directly
|
||||
|
||||
### Proxmox
|
||||
|
||||
Append `proxmox/lxc.conf.snippet` to `/etc/pve/lxc/<VMID>.conf`, then `pct stop/start <VMID>`.
|
||||
|
||||
## Layout
|
||||
|
||||
```
|
||||
docker/ rootless-Docker stack: wg (wireguard-go) + Caddy sharing netns
|
||||
lxc/ installer, systemd units, watchdog/monitor/MTU-probe scripts
|
||||
proxmox/ tun passthrough snippet for unprivileged LXC
|
||||
lxc/ Alpine installer, OpenRC services, watchdog/monitor/MTU-probe
|
||||
proxmox/ host-side LXC creator + tun-passthrough snippet
|
||||
```
|
||||
|
||||
## Tuning
|
||||
|
||||
- **MTU** — default `1280`. Run `wirecon-mtu-probe.sh` on the LXC to find the real path MTU and update `MTU =` on both peers.
|
||||
- **Watchdog** thresholds (`/etc/wirecon/wirecon.conf`): handshake-age, ping loss, and consecutive-failure streak before a forced `wg-quick down/up`.
|
||||
- **Logs** — everything is structured `key=value` on journald:
|
||||
```
|
||||
journalctl -u wirecon-watchdog.service -u wirecon-monitor.service --since "1 hour ago"
|
||||
```
|
||||
- **MTU** — default `1280`. Run `/usr/local/bin/wirecon-mtu-probe.sh` inside the LXC to find the real path MTU; update `MTU =` on both peers.
|
||||
- **Watchdog** (`/etc/wirecon/wirecon.conf`): `HANDSHAKE_MAX_AGE`, `PING_LOSS_THRESHOLD`, `FAIL_STREAK_THRESHOLD`, `WATCHDOG_INTERVAL`.
|
||||
- **Logs** — structured `key=value` lines in `/var/log/wirecon-*.log`; grep-friendly.
|
||||
+8
-6
@@ -25,15 +25,17 @@ if [[ $EUID -ne 0 ]]; then
|
||||
fi
|
||||
|
||||
# --- prereqs ---------------------------------------------------------------
|
||||
if ! command -v git >/dev/null; then
|
||||
log "installing git"
|
||||
if command -v apt-get >/dev/null; then
|
||||
if ! command -v git >/dev/null || ! command -v bash >/dev/null; then
|
||||
log "installing git + bash"
|
||||
if command -v apk >/dev/null; then
|
||||
apk add --no-cache git bash ca-certificates
|
||||
elif command -v apt-get >/dev/null; then
|
||||
DEBIAN_FRONTEND=noninteractive apt-get update -q
|
||||
DEBIAN_FRONTEND=noninteractive apt-get install -yq git ca-certificates
|
||||
DEBIAN_FRONTEND=noninteractive apt-get install -yq git bash ca-certificates
|
||||
elif command -v dnf >/dev/null; then
|
||||
dnf install -y git ca-certificates
|
||||
dnf install -y git bash ca-certificates
|
||||
else
|
||||
echo "unsupported distro: need apt-get or dnf" >&2
|
||||
echo "unsupported distro: need apk, apt-get, or dnf" >&2
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
# OpenRC config for Alpine's wg-quick init script.
|
||||
# Used when the service is invoked without an instance suffix
|
||||
# (i.e. `rc-service wg-quick ...`). We prefer the per-interface symlink
|
||||
# form (/etc/init.d/wg-quick.wg0) which derives the interface from the
|
||||
# service name, so this file can stay empty.
|
||||
Executable
+17
@@ -0,0 +1,17 @@
|
||||
#!/sbin/openrc-run
|
||||
# wirecon tunnel continuous monitor — supervised long-running loop.
|
||||
|
||||
name="wirecon-monitor"
|
||||
description="wirecon WireGuard tunnel monitor"
|
||||
|
||||
supervisor="supervise-daemon"
|
||||
command="/usr/local/bin/wirecon-monitor.sh"
|
||||
respawn_delay=5
|
||||
respawn_max=0 # unlimited restarts
|
||||
output_log="/var/log/wirecon-monitor.log"
|
||||
error_log="/var/log/wirecon-monitor.log"
|
||||
|
||||
depend() {
|
||||
need net
|
||||
after wg-quick.wg0
|
||||
}
|
||||
Executable
+18
@@ -0,0 +1,18 @@
|
||||
#!/sbin/openrc-run
|
||||
# wirecon watchdog — OpenRC has no timers, so we supervise a loop wrapper
|
||||
# that re-runs the tick script every WATCHDOG_INTERVAL seconds.
|
||||
|
||||
name="wirecon-watchdog"
|
||||
description="wirecon WireGuard tunnel watchdog"
|
||||
|
||||
supervisor="supervise-daemon"
|
||||
command="/usr/local/bin/wirecon-watchdog-loop.sh"
|
||||
respawn_delay=5
|
||||
respawn_max=0
|
||||
output_log="/var/log/wirecon-watchdog.log"
|
||||
error_log="/var/log/wirecon-watchdog.log"
|
||||
|
||||
depend() {
|
||||
need net
|
||||
after wg-quick.wg0
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
[Unit]
|
||||
# Make sure we come up after the LAN is ready.
|
||||
After=network-online.target
|
||||
Wants=network-online.target
|
||||
|
||||
[Service]
|
||||
# wg-quick@ is Type=oneshot with RemainAfterExit=yes by default.
|
||||
# If `wg-quick up` itself fails (e.g. transient DNS blip resolving the
|
||||
# server endpoint at boot), retry a few times before giving up — the
|
||||
# watchdog takes over from there.
|
||||
Restart=on-failure
|
||||
RestartSec=10s
|
||||
# oneshot needs this for Restart to apply:
|
||||
# (systemd accepts Restart on oneshot in practice)
|
||||
@@ -1,17 +0,0 @@
|
||||
[Unit]
|
||||
Description=wirecon tunnel continuous monitor
|
||||
After=wg-quick@wg0.service network-online.target
|
||||
Wants=wg-quick@wg0.service
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
EnvironmentFile=-/etc/wirecon/wirecon.conf
|
||||
ExecStart=/usr/local/bin/wirecon-monitor.sh
|
||||
Restart=always
|
||||
RestartSec=5s
|
||||
SyslogIdentifier=wirecon-monitor
|
||||
# Low priority; observation only.
|
||||
Nice=5
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
@@ -1,11 +0,0 @@
|
||||
[Unit]
|
||||
Description=wirecon tunnel watchdog (one tick)
|
||||
After=wg-quick@wg0.service network-online.target
|
||||
Wants=wg-quick@wg0.service
|
||||
|
||||
[Service]
|
||||
Type=oneshot
|
||||
EnvironmentFile=-/etc/wirecon/wirecon.conf
|
||||
ExecStart=/usr/local/bin/wirecon-watchdog.sh
|
||||
SyslogIdentifier=wirecon-watchdog
|
||||
Nice=-5
|
||||
@@ -1,14 +0,0 @@
|
||||
[Unit]
|
||||
Description=wirecon watchdog — run check every 20s
|
||||
After=wg-quick@wg0.service
|
||||
|
||||
[Timer]
|
||||
# Kick off soon after boot, then every 20s.
|
||||
OnBootSec=30s
|
||||
OnUnitActiveSec=20s
|
||||
AccuracySec=1s
|
||||
# Prevents timer drift if a check takes a while (e.g. during a reconnect).
|
||||
Unit=wirecon-watchdog.service
|
||||
|
||||
[Install]
|
||||
WantedBy=timers.target
|
||||
@@ -27,6 +27,9 @@ MTU_MAX=1420
|
||||
# Monitor cadence (seconds between log emissions)
|
||||
MONITOR_INTERVAL=10
|
||||
|
||||
# Watchdog cadence (seconds between tick invocations by the loop wrapper)
|
||||
WATCHDOG_INTERVAL=20
|
||||
|
||||
# Log tag (shows up as SYSLOG_IDENTIFIER in journald)
|
||||
LOG_TAG=wirecon
|
||||
|
||||
|
||||
+56
-43
@@ -1,10 +1,10 @@
|
||||
#!/bin/bash
|
||||
# Installer for the LXC side. Run as root inside the LXC.
|
||||
#
|
||||
# Installer for the wirecon LXC side on Alpine Linux (OpenRC).
|
||||
# Run as root inside the unprivileged LXC:
|
||||
# sudo ./install.sh
|
||||
#
|
||||
# Idempotent: re-running only updates files that changed. Does NOT touch
|
||||
# wg0.conf if it already exists (so real keys aren't clobbered).
|
||||
# Idempotent. Does NOT overwrite an existing /etc/wireguard/wg0.conf so
|
||||
# real keys survive re-runs.
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
@@ -13,22 +13,27 @@ if [[ $EUID -ne 0 ]]; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if ! command -v apk >/dev/null; then
|
||||
echo "this installer targets Alpine (apk). for Debian/RHEL use an earlier ref." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
SRC="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
|
||||
|
||||
log() { printf '[install] %s\n' "$*"; }
|
||||
|
||||
# --- packages ---------------------------------------------------------------
|
||||
log "installing packages"
|
||||
if command -v apt-get >/dev/null; then
|
||||
DEBIAN_FRONTEND=noninteractive apt-get update -q
|
||||
DEBIAN_FRONTEND=noninteractive apt-get install -yq \
|
||||
wireguard wireguard-tools iptables iproute2 iputils-ping conntrack
|
||||
elif command -v dnf >/dev/null; then
|
||||
dnf install -y wireguard-tools iptables iproute iputils conntrack-tools
|
||||
else
|
||||
echo "unsupported distro: need apt-get or dnf" >&2
|
||||
exit 1
|
||||
fi
|
||||
apk add --no-cache \
|
||||
bash \
|
||||
wireguard-tools \
|
||||
wireguard-tools-openrc \
|
||||
iptables \
|
||||
ip6tables \
|
||||
iproute2 \
|
||||
iputils \
|
||||
conntrack-tools \
|
||||
openrc
|
||||
|
||||
# --- /dev/net/tun sanity ----------------------------------------------------
|
||||
if [[ ! -e /dev/net/tun ]]; then
|
||||
@@ -36,26 +41,30 @@ if [[ ! -e /dev/net/tun ]]; then
|
||||
ERROR: /dev/net/tun is not present in this unprivileged LXC.
|
||||
On the Proxmox host, append proxmox/lxc.conf.snippet to
|
||||
/etc/pve/lxc/<VMID>.conf
|
||||
and restart the container.
|
||||
and restart the container. (proxmox/create-lxc.sh does this automatically.)
|
||||
EOF
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# --- files ------------------------------------------------------------------
|
||||
install -d -m 0755 /etc/wirecon /etc/wireguard /usr/local/bin \
|
||||
/etc/systemd/system/wg-quick@wg0.service.d
|
||||
install -d -m 0755 /etc/wirecon /etc/wireguard /etc/conf.d \
|
||||
/etc/init.d /usr/local/bin /var/log
|
||||
|
||||
log "deploying scripts"
|
||||
install -m 0755 "$SRC/usr/local/bin/wirecon-watchdog.sh" /usr/local/bin/
|
||||
install -m 0755 "$SRC/usr/local/bin/wirecon-monitor.sh" /usr/local/bin/
|
||||
install -m 0755 "$SRC/usr/local/bin/wirecon-mtu-probe.sh" /usr/local/bin/
|
||||
install -m 0755 "$SRC/usr/local/bin/wirecon-watchdog.sh" /usr/local/bin/
|
||||
install -m 0755 "$SRC/usr/local/bin/wirecon-watchdog-loop.sh" /usr/local/bin/
|
||||
install -m 0755 "$SRC/usr/local/bin/wirecon-monitor.sh" /usr/local/bin/
|
||||
install -m 0755 "$SRC/usr/local/bin/wirecon-mtu-probe.sh" /usr/local/bin/
|
||||
|
||||
log "deploying systemd units"
|
||||
install -m 0644 "$SRC/etc/systemd/system/wirecon-watchdog.service" /etc/systemd/system/
|
||||
install -m 0644 "$SRC/etc/systemd/system/wirecon-watchdog.timer" /etc/systemd/system/
|
||||
install -m 0644 "$SRC/etc/systemd/system/wirecon-monitor.service" /etc/systemd/system/
|
||||
install -m 0644 "$SRC/etc/systemd/system/wg-quick@wg0.service.d/override.conf" \
|
||||
/etc/systemd/system/wg-quick@wg0.service.d/override.conf
|
||||
log "deploying OpenRC init scripts"
|
||||
install -m 0755 "$SRC/etc/init.d/wirecon-watchdog" /etc/init.d/
|
||||
install -m 0755 "$SRC/etc/init.d/wirecon-monitor" /etc/init.d/
|
||||
install -m 0644 "$SRC/etc/conf.d/wg-quick" /etc/conf.d/wg-quick
|
||||
|
||||
# Per-interface wg-quick symlink so the init script knows which iface to bring up.
|
||||
if [[ ! -L /etc/init.d/wg-quick.wg0 ]]; then
|
||||
ln -s wg-quick /etc/init.d/wg-quick.wg0
|
||||
fi
|
||||
|
||||
if [[ ! -e /etc/wirecon/wirecon.conf ]]; then
|
||||
log "installing default /etc/wirecon/wirecon.conf"
|
||||
@@ -65,7 +74,7 @@ else
|
||||
fi
|
||||
|
||||
if [[ ! -e /etc/wireguard/wg0.conf ]]; then
|
||||
log "installing wg0.conf.example → /etc/wireguard/wg0.conf (EDIT BEFORE STARTING)"
|
||||
log "seeding /etc/wireguard/wg0.conf from example (EDIT BEFORE STARTING)"
|
||||
install -m 0600 "$SRC/etc/wireguard/wg0.conf.example" /etc/wireguard/wg0.conf
|
||||
EDIT_REQUIRED=1
|
||||
else
|
||||
@@ -73,28 +82,32 @@ else
|
||||
EDIT_REQUIRED=0
|
||||
fi
|
||||
|
||||
# --- systemd wiring ---------------------------------------------------------
|
||||
systemctl daemon-reload
|
||||
|
||||
# --- enable services --------------------------------------------------------
|
||||
if [[ "$EDIT_REQUIRED" == "1" ]]; then
|
||||
cat <<'EOF'
|
||||
|
||||
NEXT STEPS:
|
||||
1. Generate keys, edit /etc/wireguard/wg0.conf (server pubkey, endpoint,
|
||||
private key, LAN iface name in PostUp rules).
|
||||
2. Tune /etc/wirecon/wirecon.conf if defaults don't fit.
|
||||
1. Generate keys and edit /etc/wireguard/wg0.conf:
|
||||
- [Interface] PrivateKey + MTU + PostUp LAN iface name
|
||||
- [Peer] Server PublicKey + Endpoint
|
||||
2. Review /etc/wirecon/wirecon.conf (PEER_IP, thresholds).
|
||||
3. Start everything:
|
||||
systemctl enable --now wg-quick@wg0
|
||||
systemctl enable --now wirecon-watchdog.timer
|
||||
systemctl enable --now wirecon-monitor.service
|
||||
4. Watch it:
|
||||
journalctl -f -t wirecon-watchdog -t wirecon-monitor
|
||||
rc-update add wg-quick.wg0 default
|
||||
rc-update add wirecon-watchdog default
|
||||
rc-update add wirecon-monitor default
|
||||
rc-service wg-quick.wg0 start
|
||||
rc-service wirecon-watchdog start
|
||||
rc-service wirecon-monitor start
|
||||
4. Watch the logs:
|
||||
tail -F /var/log/wirecon-watchdog.log /var/log/wirecon-monitor.log
|
||||
EOF
|
||||
else
|
||||
log "enabling + (re)starting services"
|
||||
systemctl enable --now wg-quick@wg0
|
||||
systemctl enable --now wirecon-watchdog.timer
|
||||
systemctl enable --now wirecon-monitor.service
|
||||
systemctl restart wirecon-monitor.service
|
||||
log "done. tail logs with: journalctl -f -t wirecon-watchdog -t wirecon-monitor"
|
||||
rc-update add wg-quick.wg0 default 2>/dev/null || true
|
||||
rc-update add wirecon-watchdog default 2>/dev/null || true
|
||||
rc-update add wirecon-monitor default 2>/dev/null || true
|
||||
rc-service wg-quick.wg0 restart
|
||||
rc-service wirecon-watchdog restart
|
||||
rc-service wirecon-monitor restart
|
||||
log "done. tail logs: tail -F /var/log/wirecon-watchdog.log /var/log/wirecon-monitor.log"
|
||||
fi
|
||||
Executable
+20
@@ -0,0 +1,20 @@
|
||||
#!/bin/bash
|
||||
# Supervised loop: run the watchdog tick every WATCHDOG_INTERVAL seconds.
|
||||
# Kept separate from wirecon-watchdog.sh so the tick logic remains testable
|
||||
# as a one-shot (`/usr/local/bin/wirecon-watchdog.sh`).
|
||||
|
||||
set -uo pipefail
|
||||
|
||||
CONF="${WIRECON_CONF:-/etc/wirecon/wirecon.conf}"
|
||||
# shellcheck source=/dev/null
|
||||
[[ -r "$CONF" ]] && source "$CONF"
|
||||
|
||||
: "${WATCHDOG_INTERVAL:=20}"
|
||||
|
||||
trap 'exit 0' SIGTERM SIGINT
|
||||
|
||||
while :; do
|
||||
/usr/local/bin/wirecon-watchdog.sh || true
|
||||
sleep "$WATCHDOG_INTERVAL" &
|
||||
wait $!
|
||||
done
|
||||
Executable
+128
@@ -0,0 +1,128 @@
|
||||
#!/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' "$*"; }
|
||||
|
||||
# --- 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"
|
||||
|
||||
cat <<EOF
|
||||
|
||||
[create-lxc] container $VMID ($HOSTNAME) provisioned.
|
||||
[create-lxc] finish configuration inside the LXC:
|
||||
pct enter $VMID
|
||||
vi /etc/wireguard/wg0.conf # keys, endpoint, LAN iface
|
||||
vi /etc/wirecon/wirecon.conf # thresholds (optional)
|
||||
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
|
||||
Reference in new issue
Block a user