+ Equipo $n
+
+ $url
+ Primer comando: less LEEME
+
+EOF
+ done < <(sort -n "$TOKENS")
+ echo ''
+ echo ''
+}
+
+grep -qs '^template: 1' "/etc/pve/lxc/$TEMPLATE_ID.conf" ||
+ die "no existe la plantilla $TEMPLATE_ID: ejecuta antes ./template.sh"
+command -v qrencode >/dev/null || die "falta qrencode: apt install qrencode"
+mkdir -p state/qr
+touch "$TOKENS"
+
+for ((n = 1; n <= count; n++)); do
+ id=$((CT_ID_BASE + n))
+ conf=/etc/pve/lxc/$id.conf
+ if [[ ! -e $conf ]]; then
+ echo "eq$n: creando el CT $id"
+ create_team "$n"
+ continue
+ fi
+ echo "eq$n: el CT $id ya existe, se salta"
+ grep -q '^\[clean\]' "$conf" ||
+ echo " aviso: el CT $id no tiene snapshot 'clean', quedó a medias: pct destroy $id --purge y vuelve a ejecutar" >&2
+ [[ -n $(token_of "$id") ]] ||
+ echo " aviso: el CT $id no tiene token en $TOKENS, se queda sin ruta" >&2
+done
+
+write_routes > state/taller-routes.conf
+pct push "$PROXY_ID" state/taller-routes.conf /etc/nginx/taller-routes.conf
+pct exec "$PROXY_ID" -- nginx -tq
+pct exec "$PROXY_ID" -- systemctl reload nginx
+# Un CT recreado conserva su IP pero no su MAC: borra del proxy las entradas ARP obsoletas
+pct exec "$PROXY_ID" -- ip neigh flush to "$SUBNET.0/24"
+
+write_slips > state/papelitos.html
+echo "rutas publicadas; imprime state/papelitos.html"
diff --git a/template.sh b/template.sh
new file mode 100755
index 0000000..497e108
--- /dev/null
+++ b/template.sh
@@ -0,0 +1,74 @@
+#!/usr/bin/env bash
+# Se ejecuta en: host Proxmox.
+# Uso: ./template.sh
+# Construye la plantilla (TEMPLATE_ID) de la que se clonan los CT de equipo:
+# paquetes, usuario alumno, servicio ttyd, los ficheros de files/ y los retos.
+# Si la plantilla ya existe no hace nada; para rehacerla, antes pct destroy.
+set -euo pipefail
+cd "$(dirname "$0")"
+# shellcheck source=SCRIPTDIR/common.sh
+source ./common.sh
+
+GEN_LOG=retos/1-logs/gen-access-log.sh
+
+in_ct() {
+ pct exec "$TEMPLATE_ID" -- "$@"
+}
+
+conf=/etc/pve/lxc/$TEMPLATE_ID.conf
+if [[ -e $conf ]]; then
+ grep -q '^template: 1' "$conf" ||
+ die "el CT $TEMPLATE_ID existe pero no es plantilla (¿quedó a medias?): pct destroy $TEMPLATE_ID"
+ echo "la plantilla $TEMPLATE_ID ya existe, no hay nada que hacer"
+ exit 0
+fi
+[[ -f $GEN_LOG ]] || die "no existe $GEN_LOG"
+
+ttyd_bin=$(mktemp)
+trap 'rm -f "$ttyd_bin"' EXIT
+curl -fsSL -o "$ttyd_bin" "https://github.com/tsl0922/ttyd/releases/download/$TTYD_VERSION/ttyd.x86_64"
+sha256sum --check --quiet <<< "$TTYD_SHA256 $ttyd_bin"
+
+pveam update
+os_image=$(pveam available --section system |
+ awk '$2 ~ /^debian-13-standard_.*_amd64\./ { print $2 }' | sort -V | tail -n 1)
+[[ -n $os_image ]] || die "no hay imagen de Debian 13 en 'pveam available'"
+# Sin grep -q: con pipefail, si grep sale antes de tiempo, pveam puede fallar por SIGPIPE
+pveam list "$IMAGE_STORAGE" | grep -F "$os_image" >/dev/null ||
+ pveam download "$IMAGE_STORAGE" "$os_image"
+
+# Sin privilegios; nesting es lo que recomienda Proxmox para systemd >= 254 en el CT
+pct create "$TEMPLATE_ID" "$IMAGE_STORAGE:vztmpl/$os_image" \
+ --hostname taller-template \
+ --unprivileged 1 \
+ --features nesting=1 \
+ --rootfs "$CT_STORAGE:$CT_DISK_GB" \
+ --net0 "name=eth0,bridge=$EXT_BRIDGE,ip=dhcp"
+pct start "$TEMPLATE_ID"
+wait_for 60 in_ct getent hosts deb.debian.org >/dev/null ||
+ die "el CT $TEMPLATE_ID no tiene red"
+
+in_ct apt-get update
+in_ct env DEBIAN_FRONTEND=noninteractive apt-get install -y bash-completion shellcheck tmux \
+ vim nano less man-db gawk jq inotify-tools procps cron tree
+in_ct apt-get clean
+in_ct useradd --create-home --shell /bin/bash alumno
+
+pct push "$TEMPLATE_ID" "$ttyd_bin" /usr/local/bin/ttyd --perms 755
+pct push "$TEMPLATE_ID" files/ttyd.service /etc/systemd/system/ttyd.service
+in_ct systemctl enable ttyd
+
+pct push "$TEMPLATE_ID" files/LEEME /home/alumno/LEEME --user alumno --group alumno
+# Readline ignora /etc/inputrc si existe ~/.inputrc; este lo incluye
+pct push "$TEMPLATE_ID" files/inputrc /home/alumno/.inputrc --user alumno --group alumno
+
+# El generador no entra en el CT: leyéndolo se sabría la respuesta
+in_ct mkdir /home/alumno/retos
+tar -C retos --exclude=gen-access-log.sh -cf - . | in_ct tar -C /home/alumno/retos -xf -
+in_ct bash -c 'bash -s > /home/alumno/retos/1-logs/access.log' < "$GEN_LOG"
+in_ct chown -R alumno:alumno /home/alumno/retos
+
+pct shutdown "$TEMPLATE_ID"
+pct set "$TEMPLATE_ID" --delete net0
+pct template "$TEMPLATE_ID"
+echo "plantilla $TEMPLATE_ID lista"