The literal {{ct.host}} in prose was composed into {{ct.ct.host}} when the
notebook is included; describe the capture in words instead.
3.7 KiB
3.7 KiB
title, params
| title | params | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Create an LXC container on Proxmox |
|
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