#!/usr/bin/env bash set -euo pipefail [[ $EUID == 0 ]] || { echo 'Run on the Proxmox host as root' >&2; exit 1; } script_dir=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd) # shellcheck source=deploy/proxmox/template-picker.sh source "$script_dir/template-picker.sh" ctid=${CTID:-501} [[ $ctid =~ ^[0-9]+$ ]] || { echo 'Invalid CTID' >&2; exit 1; } arch=$(dpkg --print-architecture) if pct config "$ctid" >/dev/null 2>&1; then config=$(pct config "$ctid") [[ $config == *'ostype: debian'* && $config == *"arch: $arch"* && $config == *'unprivileged: 1'* ]] || { echo 'Refusing to touch a non-Debian, wrong-architecture, or privileged CT' >&2; exit 1; } pct start "$ctid" 2>/dev/null || [[ $(pct status "$ctid") == *running* ]] # These variables must expand inside the CT, not on the host. # shellcheck disable=SC2016 pct exec "$ctid" -- sh -c '. /etc/os-release; test "$ID" = debian && test "$VERSION_ID" = 13' || { echo 'Refusing to touch a CT that is not Debian 13' >&2; exit 1; } container_arch=$(pct exec "$ctid" -- dpkg --print-architecture) [[ $container_arch == "$arch" ]] || { echo 'Container architecture mismatch' >&2; exit 1; } else pveam update available=$(pveam available --section system) template=$(pick_template "$arch" <<< "$available") [[ -n $template ]] || { echo "No Debian 13 template for $arch" >&2; exit 1; } pveam download "${TEMPLATE_STORAGE:-local}" "$template" pct create "$ctid" "${TEMPLATE_STORAGE:-local}:vztmpl/$template" \ --hostname "${CT_HOSTNAME:-shellbook}" --ostype debian --arch "$arch" \ --unprivileged 1 --features nesting=1 --onboot 1 \ --cores "${CT_CORES:-2}" --memory "${CT_MEMORY:-2048}" --swap 512 \ --rootfs "${CT_STORAGE:-local-lvm}:${CT_DISK_GB:-16}" \ --net0 "name=eth0,bridge=${CT_BRIDGE:-vmbr0},ip=${CT_IP:-dhcp}" fi # pct manages the active config correctly even when snapshot sections exist. config=$(pct config "$ctid") needs_restart=0 features=$(awk -F': ' '$1 == "features" {print $2}' <<< "$config") if [[ $features != *nesting=1* ]]; then features=${features//nesting=0/nesting=1} [[ $features == *nesting=1* ]] || features="${features:+$features,}nesting=1" pct set "$ctid" --features "$features" needs_restart=1 fi pct set "$ctid" --onboot 1 if [[ $config != *'path=/dev/net/tun'* && $config != *'dev0: /dev/net/tun'* ]]; then [[ $config != *'dev0:'* ]] || { echo 'dev0 already occupied; configure /dev/net/tun passthrough manually' >&2; exit 1; } pct set "$ctid" --dev0 /dev/net/tun needs_restart=1 fi if [[ $needs_restart == 1 && $(pct status "$ctid") == *running* ]]; then pct shutdown "$ctid" --timeout 60; fi if [[ $(pct status "$ctid") != *running* ]]; then pct start "$ctid"; fi # Wait for init/network after creation or reboot. for ((attempt=0; attempt<60; attempt++)); do if pct exec "$ctid" -- test -d /run/systemd/system; then break; fi sleep 1 done pct push "$ctid" "$script_dir/../install.sh" /root/shellbook-install.sh --perms 0700 # Transfer optional settings without exposing TS_AUTHKEY in process arguments. settings=$(mktemp) trap 'rm -f "$settings"' EXIT chmod 600 "$settings" for key in SHELLBOOK_REPO SHELLBOOK_REF SHELLBOOK_HOST SHELLBOOK_PORT SHELLBOOK_TRUSTED_PROXIES TS_AUTHKEY; do if [[ -v $key ]]; then printf 'export %s=%q\n' "$key" "${!key}" >> "$settings"; fi done pct push "$ctid" "$settings" /root/shellbook-install.env --perms 0600 pct exec "$ctid" -- bash -c 'trap "rm -f /root/shellbook-install.env" EXIT; source /root/shellbook-install.env; bash /root/shellbook-install.sh'