Files
Florian Schmidt 16cfcd36ea fix(templates): avoid template syntax in LXC prose (#12)
The literal {{ct.host}} in prose was composed into {{ct.ct.host}} when the
notebook is included; describe the capture in words instead.
2026-11-16 12:31:54 +01:00

3.7 KiB

title, params
title params
Create an LXC container on Proxmox
proxmox hostname ctid os cores memory swap disk storage template_storage bridge net
type required label
host true Proxmox host
type required label
name true Container hostname
type default label
name {{nextid}} Container ID (empty = next free)
type options default label
choice
debian-12-standard
debian-13-standard
ubuntu-24.04-standard
debian-12-standard Template
type default label
name 2 CPU cores
type default label
name 2048 Memory (MiB)
type default label
name 512 Swap (MiB)
type default label
name 8 Disk (GiB)
type default label
name local-lvm Disk storage
type default label
name local Template storage
type default label
name vmbr0 Network bridge
type default label
string ip=dhcp IP config (ip=dhcp or ip=10.0.0.5/24,gw=10.0.0.1)

Create LXC container {{hostname}}

Creates an unprivileged container on {{proxmox}} (connects as a user that may run pct, usually root), starts it with ShellBook's SSH key for root, and adds it to Hosts. Other notebooks can include this one with ::use{notebook="infra/lxc-container.md" as=ct} and target the new machine through its host capture (ct.host with that alias).

command -v pct
command -v pveam
command -v pvesh
pvesh get /cluster/nextid
if pct status {{ctid}} > /dev/null 2>&1; then
  echo "Container {{ctid}} already exists" >&2
  exit 1
fi

The template step refreshes the template index and downloads the newest {{os}} template if it is not on {{template_storage}} yet.

pveam update > /dev/null
name=$(pveam available --section system | awk -v prefix={{os}} 'index($2, prefix) == 1 { print $2 }' | sort -V | tail -n 1)
if [ -z "$name" ]; then
  echo "No template matching {{os}} is available" >&2
  exit 1
fi
if ! pveam list {{template_storage}} | grep -qF "$name"; then
  pveam download {{template_storage}} "$name" >&2
fi
printf '%s:vztmpl/%s\n' {{template_storage}} "$name"
keys=$(mktemp)
trap 'rm -f "$keys"' EXIT
printf '%s\n' {{shellbook.public_key}} > "$keys"
pct create {{ctid}} {{template}} \
  --hostname {{hostname}} \
  --cores {{cores}} --memory {{memory}} --swap {{swap}} \
  --rootfs {{storage}}:{{disk}} \
  --net0 name=eth0,bridge={{bridge}},{{net}} \
  --unprivileged 1 --features nesting=1 --onboot 1 \
  --ssh-public-keys "$keys"
pct start {{ctid}}

The next check waits for the container's address and prints its SSH host key fingerprints (stderr). Compare them with the fingerprint ShellBook shows on the first connection.

ip=""
for _ in $(seq 1 45); do
  ip=$(pct exec {{ctid}} -- hostname -I 2> /dev/null | awk '{ print $1 }' || true)
  [ -n "$ip" ] && break
  sleep 2
done
if [ -z "$ip" ]; then
  echo "Container {{ctid}} has no IP address yet" >&2
  exit 1
fi
pct exec {{ctid}} -- sh -c 'for key in /etc/ssh/ssh_host_*_key.pub; do ssh-keygen -lf "$key"; done' >&2 || true
printf '%s\n' "$ip"

::host{name="{{hostname}}" address="{{address}}" user=root port=22 capture=host}

uname -a
head -n 2 /etc/os-release