Files
ShellBook/deploy/proxmox/create-ct.sh
T
Florian Schmidt 399628bf16 feat(deploy): one-step install/update from the Proxmox host (#6)
bootstrap.sh runs on the Proxmox host via
  bash -c "$(curl -fsSL .../deploy/proxmox/bootstrap.sh)"
It unpacks the repository into a temporary directory, runs
create-ct.sh and removes everything again; create-ct.sh also removes
the installer files it pushed into the CT. Nothing stays on the host.

install.sh now installs or updates: an existing checkout is pulled
(fast-forward, SHELLBOOK_REF pins are kept), the service is restarted
and health-checked, and the summary prints the address, the one-time
setup token while setup is pending, the Claude login state and the
SSH public key.

It also installs a root-owned `shellbook-update` that downloads the
installer from the repository and reruns it. Root never executes
scripts from /opt/shellbook, which the service user and its terminal
panel can modify, so deploy/update.sh is removed.

Verified in a Debian 13 container: fresh install, then an update that
pulls a new commit while a tampered checkout script is not executed;
bootstrap.test.sh checks that host and CT keep no installer files.
2026-11-16 10:57:42 +01:00

72 lines
3.6 KiB
Bash
Executable File

#!/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"
# shellcheck source=deploy/proxmox/ct-config.sh
source "$script_dir/ct-config.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 ! has_tun_passthrough <<< "$config"; 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
# Leave no installer files behind in the CT either; later updates use `shellbook-update`.
pct exec "$ctid" -- bash -c 'trap "rm -f /root/shellbook-install.env /root/shellbook-install.sh" EXIT; source /root/shellbook-install.env; bash /root/shellbook-install.sh'