Proxmox nodes: install in an LXC container instead of Docker on the node

Run on a Proxmox VE node (pct and /etc/pve present), the installers now
default to INSTALL_MODE=lxc: create or reuse (by tag netmon-hub /
netmon-probe) an unprivileged Debian LXC on vmbr0 with nesting and onboot,
push the installer into it (rebuilt from its own functions) and run it there
in native mode. LXC_* variables override ID, storage, template storage,
bridge, IP and size. Docker on a node now needs an explicit
INSTALL_MODE=docker and prints a warning; an earlier Docker install is
pointed out with removal steps.

Ping setup inside an unprivileged container stays within the mapped group
range, which the kernel requires.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
neil 2026-11-04 18:28:21 -08:00
parent edeb77f833
commit a17f2b502c
7 changed files with 382 additions and 38 deletions

View file

@ -10,10 +10,13 @@
# Private repo over https (the token is asked for, not typed into history):
# read -rsp 'Git token: ' GIT_TOKEN && export GIT_TOKEN && curl -fsSL -H "Authorization: token $GIT_TOKEN" https://git.neilbantoc.dev/services/network-monitor/raw/branch/main/install.sh | bash
#
# Two ways to run (INSTALL_MODE):
# docker The app in a container (compose). Default on Linux hosts.
# Where it installs (INSTALL_MODE):
# lxc On a Proxmox VE node: creates (or reuses) a small Debian LXC container, tagged
# netmon-hub, and installs natively inside it. Default on Proxmox nodes, so the
# node itself gets no Docker and no packages. LXC_* settings below.
# docker The app in a container (compose). Default on other Linux hosts.
# native A Python virtualenv and a boot service: systemd on Linux, launchd on macOS.
# Default on macOS and inside LXC containers (e.g. Proxmox LXC).
# Default on macOS and inside LXC containers.
# The mode is saved in .env, so re-runs keep it. Re-running is safe: it keeps .env (values
# you pass replace those keys), updates the checkout and restarts. Ordinary deploys:
# docker: docker exec network-monitor update
@ -27,6 +30,9 @@
# GIT_TOKEN, GIT_USERNAME private repo over https
# GIT_SSH_KEY private repo over SSH (base64-encoded deploy key)
# HOST_PORT default 8000 (bound to HOST_BIND from .env: 0.0.0.0 so probes can report)
# Proxmox (lxc mode): LXC_ID (default: next free), LXC_STORAGE (default: local-lvm, local-zfs
# or the first active), LXC_TEMPLATE_STORAGE (local), LXC_BRIDGE (vmbr0), LXC_IP (dhcp,
# or 192.168.1.50/24 with LXC_GW), LXC_CORES (1), LXC_MEMORY (512), LXC_DISK (4 GB)
#
# install-probe.sh is generated from this file by scripts/build-installers.sh: edit this one.
# Everything is inside main(), so a partial download runs nothing. bash 3.2 compatible (macOS).
@ -58,12 +64,14 @@ main() {
SUDO=sudo
fi
CALLER_MODE=${INSTALL_MODE:-}
step "source"
load_existing_env
REPO_URL=${REPO_URL:-$DEFAULT_REPO_URL}
REPO_REF=${REPO_REF:-$DEFAULT_REF}
printf -v "$PORT_KEY" '%s' "${!PORT_KEY:-$DEFAULT_PORT}"
choose_mode
if [ "$MODE" = lxc ]; then lxc_install; return; fi
step "prerequisites"
install_packages
@ -106,14 +114,34 @@ is_lxc() {
grep -qa 'container=lxc' /proc/1/environ 2>/dev/null
}
is_pve_host() {
[ "$OS" = Linux ] && command -v pct >/dev/null 2>&1 && [ -d "${NETMON_PVE_DIR:-/etc/pve}" ]
}
choose_mode() {
MODE=${INSTALL_MODE:-auto}
if is_pve_host; then
# On a Proxmox node only an explicit INSTALL_MODE counts; a mode saved in .env does not.
MODE=${CALLER_MODE:-auto}
[ "$MODE" != auto ] || MODE=lxc
warn_existing_docker_install
fi
if [ "$MODE" = auto ]; then
if [ "$OS" = Darwin ]; then MODE=native
elif is_lxc; then MODE=native; log "LXC container detected: native mode (set INSTALL_MODE=docker for Docker)"
else MODE=docker; fi
fi
case $MODE in docker|native) ;; *) die "INSTALL_MODE must be docker or native, not '$MODE'" ;; esac
case $MODE in docker|native|lxc) ;; *) die "INSTALL_MODE must be docker, native or lxc, not '$MODE'" ;; esac
if [ "$MODE" = lxc ]; then
is_pve_host || die "INSTALL_MODE=lxc only works on a Proxmox VE node (needs pct)"
[ "$(id -u)" = 0 ] || die "on a Proxmox node, run the installer as root"
log "mode: lxc (Proxmox node: the $ROLE goes in its own container, installed natively inside)"
return
fi
if is_pve_host && [ "$MODE" = docker ]; then
log "warning: installing Docker on a Proxmox node changes its firewall and can break guest networking."
log " Leave INSTALL_MODE unset to install in an LXC container instead."
fi
if [ "$OS" = Darwin ] && [ "$MODE" = docker ]; then
die "on macOS use INSTALL_MODE=native: Docker Desktop runs containers in a VM, so a probe would measure the VM's network, and it only starts at login"
fi
@ -128,6 +156,103 @@ choose_mode() {
log "mode: $MODE"
}
# An earlier run on this Proxmox node may have installed with Docker. Say how to remove it.
warn_existing_docker_install() {
[ "${CALLER_MODE:-}" != docker ] || return 0
local found=
[ -f "$INSTALL_DIR/compose.yml" ] && found=1
command -v docker >/dev/null 2>&1 && docker inspect "$NAME" >/dev/null 2>&1 && found=1
[ -n "$found" ] || return 0
log "note: this node has a Docker install of $NAME from an earlier run. To remove it:"
log " cd $INSTALL_DIR && docker compose -f $COMPOSE_FILE down -v --rmi all && cd / && rm -rf $INSTALL_DIR"
log " The README (Proxmox section) shows how to remove Docker from the node."
}
# ---------- Proxmox: create (or reuse) an LXC container and install inside it ----------
lxc_install() {
local tag="netmon-$ROLE" ctname script ip k port=${!PORT_KEY}
if [ "$ROLE" = hub ]; then ctname=netmon-hub; else ctname="netmon-probe-$(hostname -s | tr -c 'a-zA-Z0-9-\n' '-')"; fi
step "proxmox container"
CTID=$(find_ct "$tag")
if [ -n "$CTID" ]; then log "reusing container $CTID (tagged $tag)"; else create_ct "$ctname" "$tag"; fi
pct status "$CTID" | grep -q running || pct start "$CTID"
wait_ct_network
step "install inside container $CTID"
# This installer re-assembled from its own functions, so nothing is downloaded again.
script=$(mktemp)
{ printf '#!/usr/bin/env bash\nNETMON_ROLE=%s\n' "$ROLE"; declare -f; printf 'main "$@"\n'; } > "$script"
pct push "$CTID" "$script" /root/netmon-install.sh --perms 0700
rm -f "$script"
set -- env INSTALL_MODE=native
for k in $KEYS; do
if [ "$k" != INSTALL_MODE ] && [ -n "${!k:-}" ]; then set -- "$@" "$k=${!k}"; fi
done
pct exec "$CTID" -- "$@" bash /root/netmon-install.sh
pct exec "$CTID" -- rm -f /root/netmon-install.sh
ip=$(pct exec "$CTID" -- hostname -I 2>/dev/null | cut -d' ' -f1)
step "done"
echo "$NAME runs in Proxmox container $CTID ($(pct config "$CTID" | sed -n 's/^hostname: //p')), IP ${ip:-unknown}."
if [ "$ROLE" = hub ]; then echo " open the web UI: http://${ip:-<container-ip>}:$port"; fi
echo " give it a fixed IP: a DHCP reservation, or pct set $CTID --net0 name=eth0,bridge=...,ip=<ip>/<cidr>,gw=<gw>"
echo " deploy a new commit: pct exec $CTID -- $NAME-update"
echo " logs: pct exec $CTID -- journalctl -u $NAME -f"
echo " settings: pct exec $CTID -- nano /opt/$NAME/.env (then: pct exec $CTID -- systemctl restart $NAME)"
echo " shell inside: pct enter $CTID"
echo " upgrade: run this installer on the node again (it reuses container $CTID)"
echo " uninstall: pct stop $CTID && pct destroy $CTID"
}
# The container this installer made before, found by its tag.
find_ct() {
local id
for id in $(pct list 2>/dev/null | awk 'NR > 1 { print $1 }'); do
if pct config "$id" 2>/dev/null | grep -Eq "^tags: (.*[;, ])?$1([;, ]|\$)"; then echo "$id"; return; fi
done
}
create_ct() {
local ctname=$1 tag=$2 tstore=${LXC_TEMPLATE_STORAGE:-local} storage=${LXC_STORAGE:-} tmpl name
CTID=${LXC_ID:-$(pvesh get /cluster/nextid)}
tmpl=$(pveam list "$tstore" 2>/dev/null | awk '{ print $1 }' | grep -E 'vztmpl/debian-1[23]-standard' | sort -V | tail -1 || true)
if [ -z "$tmpl" ]; then
log "downloading a Debian template to $tstore"
pveam update >/dev/null 2>&1 || true
name=$(pveam available --section system 2>/dev/null | awk '{ print $2 }' | grep -E '^debian-1[23]-standard' | sort -V | tail -1 || true)
[ -n "$name" ] || die "no Debian 12/13 template available from pveam"
pveam download "$tstore" "$name" >/dev/null || die "could not download $name to $tstore (set LXC_TEMPLATE_STORAGE)"
tmpl="$tstore:vztmpl/$name"
fi
if [ -z "$storage" ]; then
local avail
avail=$(pvesm status --content rootdir --enabled 1 2>/dev/null | awk 'NR > 1 && $3 == "active" { print $1 }')
for name in local-lvm local-zfs $avail; do
if echo "$avail" | grep -qx "$name"; then storage=$name; break; fi
done
[ -n "$storage" ] || die "no active storage for containers found; set LXC_STORAGE"
fi
log "creating container $CTID ($ctname) from $tmpl on $storage, bridge ${LXC_BRIDGE:-vmbr0}"
pct create "$CTID" "$tmpl" --hostname "$ctname" --cores "${LXC_CORES:-1}" --memory "${LXC_MEMORY:-512}" --swap 256 \
--rootfs "$storage:${LXC_DISK:-4}" \
--net0 "name=eth0,bridge=${LXC_BRIDGE:-vmbr0},ip=${LXC_IP:-dhcp}${LXC_GW:+,gw=$LXC_GW}" \
--unprivileged 1 --features nesting=1 --onboot 1 --tags "$tag" \
--description "Network Monitor $ROLE, created by the $NAME installer" >/dev/null || die "pct create failed"
}
wait_ct_network() {
log "waiting for container $CTID to get network"
local i=0
while [ "$i" -lt 90 ]; do
if pct exec "$CTID" -- sh -c 'ip -4 route show default | grep -q . && getent hosts deb.debian.org >/dev/null' 2>/dev/null; then
log "container $CTID is online"; return
fi
sleep 1; i=$((i + 1))
done
die "container $CTID has no network after 90 s (bridge ${LXC_BRIDGE:-vmbr0}, ip ${LXC_IP:-dhcp}); check: pct exec $CTID -- ip addr"
}
install_packages() {
if [ "$OS" = Darwin ]; then
xcode-select -p >/dev/null 2>&1 || die "git needs the Xcode Command Line Tools: run xcode-select --install, then this installer again"
@ -175,9 +300,13 @@ install_docker() {
# The probe runs without root or capabilities, so ping needs unprivileged ICMP sockets.
# Most distros allow them already (systemd's default); some ship "1 0", which blocks them.
enable_ping() {
local want="0 2147483647" cur tmp
local want="0 2147483647" cur tmp top
# In an unprivileged container (user namespace), the range must stay within the mapped
# groups (usually 0-65535) or the kernel rejects it.
top=$(awk 'NR == 1 { print $1 + $3 - 1 }' /proc/self/gid_map 2>/dev/null || true)
if [ -n "$top" ] && [ "$top" -lt 2147483647 ] 2>/dev/null; then want="0 $top"; fi
cur=$(sysctl -n net.ipv4.ping_group_range 2>/dev/null | tr -s '[:space:]' ' ' | sed 's/ $//') || cur=
if [ "$cur" = "$want" ]; then log "ping: unprivileged ICMP allowed"; return; fi
if [ "$cur" = "$want" ] || [ "$cur" = "0 2147483647" ]; then log "ping: unprivileged ICMP allowed"; return; fi
log "ping: allowing unprivileged ICMP (net.ipv4.ping_group_range was '${cur:-unknown}')"
tmp=$(mktemp)
printf 'net.ipv4.ping_group_range = %s\n' "$want" > "$tmp"