--- title: Create an LXC container on Proxmox params: proxmox: { type: host, required: true, label: Proxmox host } hostname: { type: name, required: true, label: Container hostname } ctid: { type: name, default: "{{nextid}}", label: Container ID (empty = next free) } os: { type: choice, options: [debian-12-standard, debian-13-standard, ubuntu-24.04-standard], default: debian-12-standard, label: Template } cores: { type: name, default: "2", label: CPU cores } memory: { type: name, default: "2048", label: Memory (MiB) } swap: { type: name, default: "512", label: Swap (MiB) } disk: { type: name, default: "8", label: Disk (GiB) } storage: { type: name, default: local-lvm, label: Disk storage } template_storage: { type: name, default: local, label: Template storage } bridge: { type: name, default: vmbr0, label: Network bridge } net: { type: string, default: "ip=dhcp", label: "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 as `{{ct.host}}`. ```sh {check host="{{proxmox}}" id=pve title="Proxmox tools are available"} command -v pct command -v pveam command -v pvesh ``` ```sh {check host="{{proxmox}}" id=next-id title="Next free container ID" capture=nextid} pvesh get /cluster/nextid ``` ```sh {check host="{{proxmox}}" id=id-free title="Container ID is free"} 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. ```sh {run host="{{proxmox}}" id=template title="Download template" capture=template timeout=900} 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" ``` ```sh {run host="{{proxmox}}" id=create title="Create and start container" confirm timeout=600} 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. ```sh {check host="{{proxmox}}" id=address title="Wait for the container address" capture=address timeout=120} 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} ```sh {check host="{{host}}" id=login title="ShellBook can log in"} uname -a head -n 2 /etc/os-release ```