Files
werewulf/scripts/B20-build-head-template.sh
T
ilker c38c2b1887 hpcod: fix per-tenant addressing, and cut provisioning to ~3.6 min
The important fix here is not a speed one. group_vars/all.yml carried
tenant 1's subnet, head address and DHCP pool as "defaults the
inventory overrides". Ansible's precedence is the other way round:
playbook-adjacent group_vars outrank an inventory's own group vars, so
those values silently won over the generated inventory. With one
tenant the two agreed and nothing showed. The second tenant was built
with the first tenant's network, the playbook reported success, and
dhcpd then refused to start because no interface was on the subnet it
had been told to serve. Nothing tenant-specific lives in that file now,
and site.yml asserts the inventory supplies it.

Two smaller correctness fixes on the same path. The generated inventory
never named an SSH key, so whether Ansible could connect at all
depended on $HOME being set for a service account that has no real home
directory. And nothing waited for the head node: Terraform returns as
soon as Proxmox has started the VM, before cloud-init has written the
authorised keys, so the first run failed with a bare "Permission
denied" that reads like a wrong key rather than a race. site.yml now
opens with wait_for_connection, and the inventory is generated by
B30-tenant-inventory.py instead of by hand - Warewulf matches nodes by
MAC, and a hand-copied MAC means a node PXE boots and is never
recognised, with nothing in the logs to say why.

Then the speed work, each part measured before and after.

Slim the node image. Measured on the unslimmed 1.8 GB rootfs, device
firmware alone was 657 MB, and a guest using virtio needs none of it;
the base image also left a second, orphaned kernel module tree behind.
Dropping those, the locales, vim and the docs takes the unpacked rootfs
to 937 MB, which is paid three times over - pulling the image, sending
it to each node over PXE, and again as RAM, since the root filesystem
is a tmpfs. Node boot to Slurm "idle" went from 139s to 81s. Firmware
removal is behind KEEP_FIRMWARE for sites whose nodes are physical.

Build with --squash-all rather than --squash. --squash keeps the base
image layers, so files deleted in our own layers still ship underneath:
the first attempt slimmed the filesystem but moved the registry image
only from 1.94 GB to 1.91 GB. Squashing everything took it to 950 MB.

Stop rebuilding the node image on every tenant. The golden cleanup was
removing all of /var/lib/warewulf/provision, but that directory holds
two unlike things - provision/overlays/<node>/ carries the tenant's
MUNGE key and SSH keys and must not survive into a template, while
provision/images/ is the compiled image, 1.2 GB that is byte for byte
identical for every tenant and holds no secret. Deleting both made
every tenant spend ~28s recompressing an image it could have
inherited; profiling showed it was the slowest task in the playbook by
an order of magnitude. The cleanup now takes only the overlays, and
the build is gated on the compiled image actually being stale.

Measured, cold, from destroy to a cluster running jobs on both nodes:
terraform 32s, head boot 2s, ansible 100s, node PXE and Slurm 81s.
2026-09-02 06:01:41 +02:00

95 lines
3.4 KiB
Bash
Executable File

#!/bin/bash
# Build the golden head node and convert it to a Proxmox template.
#
# Run on the control plane host. This is a build-time job, like the container
# image: everything it installs is paid for once here instead of by every
# tenant while a user waits.
#
# ./B20-build-head-template.sh
# ./B20-build-head-template.sh -v 9101 -t hpcod-head-v2
#
# The template deliberately carries no MUNGE key, SSH host keys or machine id.
# A shared MUNGE key would let any tenant authenticate to any other tenant's
# Slurm; the tenant playbook regenerates all three on first boot.
set -euo pipefail
SSH_KEY=${SSH_KEY:-/var/lib/hpcod/.ssh/id_ed25519}
PVE_HOST=${PVE_HOST:-10.0.0.10}
SSH_OPTS="-o BatchMode=yes -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i ${SSH_KEY}"
PVE_SSH="ssh ${SSH_OPTS} root@${PVE_HOST}"
VMID=9100
NAME=hpcod-head
BUILD_IP=${BUILD_IP:-10.0.0.61}
GATEWAY=${GATEWAY:-10.0.0.1}
IMAGE=${IMAGE:-local:import/Rocky-9-GenericCloud-Base.latest.x86_64.qcow2}
STORAGE=${STORAGE:-zfspool}
ANSIBLE_DIR=${ANSIBLE_DIR:-/var/lib/hpcod/ansible}
while getopts "v:t:i:" o; do
case $o in
v) VMID=$OPTARG ;;
t) NAME=$OPTARG ;;
i) BUILD_IP=$OPTARG ;;
*) echo "usage: $0 [-v vmid] [-t name] [-i build-ip]"; exit 1 ;;
esac
done
PUBKEY=$(cat "${SSH_KEY}.pub")
echo "==> removing any previous build VM"
$PVE_SSH "qm stop ${VMID} >/dev/null 2>&1 || true; qm destroy ${VMID} --purge >/dev/null 2>&1 || true"
echo "==> creating build VM ${VMID} at ${BUILD_IP}"
$PVE_SSH "set -e
printf '%s\n' '${PUBKEY}' > /tmp/hpcod-golden.pub
qm create ${VMID} --name ${NAME}-build --memory 4096 --cores 2 --cpu host \
--net0 virtio,bridge=vmbr0 --scsihw virtio-scsi-single --ostype l26 \
--agent 1 --serial0 socket
qm set ${VMID} --scsi0 ${STORAGE}:0,import-from=${IMAGE},discard=on,ssd=1 >/dev/null
qm resize ${VMID} scsi0 40G >/dev/null
qm set ${VMID} --ide2 ${STORAGE}:cloudinit --boot order=scsi0 >/dev/null
qm set ${VMID} --ciuser ilker --sshkeys /tmp/hpcod-golden.pub \
--ipconfig0 ip=${BUILD_IP}/24,gw=${GATEWAY} --nameserver '1.1.1.1 8.8.8.8' --ciupgrade 0 >/dev/null
rm -f /tmp/hpcod-golden.pub
qm start ${VMID}"
echo "==> waiting for SSH"
for _ in $(seq 1 60); do
ssh ${SSH_OPTS} -o ConnectTimeout=5 "ilker@${BUILD_IP}" true 2>/dev/null && break
sleep 5
done
echo "==> installing the stack"
# A private mktemp file, not a fixed path in the shared /tmp: on a host with
# other users a predictable name is a symlink target, and fs.protected_regular
# blocks the write outright once someone else owns the leftover file.
INV_DIR=$(mktemp -d "${STATE_DIR:-/var/lib/hpcod}/.golden-inv.XXXXXX")
trap 'rm -rf "${INV_DIR}"' EXIT
INV="${INV_DIR}/inventory.yml"
cat > "${INV}" <<EOF
all:
children:
golden:
hosts:
${NAME}-build:
ansible_host: ${BUILD_IP}
ansible_user: ilker
ansible_become: true
ansible_ssh_private_key_file: ${SSH_KEY}
EOF
# --tags install selects only what belongs in a template; the cleanup play
# is tagged "always" so it still runs.
( cd "${ANSIBLE_DIR}" && ansible-playbook -i "${INV}" golden.yml --tags install )
echo "==> shutting down and converting to a template"
$PVE_SSH "set -e
qm shutdown ${VMID} --timeout 120 || qm stop ${VMID}
sleep 3
qm set ${VMID} --name ${NAME} >/dev/null
qm template ${VMID}"
echo
echo "TEMPLATE READY: ${VMID} (${NAME})"
echo "Point the Terraform module at it: head_template_vmid = ${VMID}"