Restructure the repository per session
session1/ now holds the whole live lab (content, platforms, docs, answer key, README); session2/ keeps the design for the second workshop. The root README indexes both and states that session2 modifies session1 rather than duplicating it. build-bundle.sh builds from session1 and still produces a flat archive, so the documented paths inside a deployment (platforms/<p>/lab.sh) are unchanged. .gitignore follows the move.
This commit is contained in:
@@ -0,0 +1,401 @@
|
||||
# shellcheck shell=bash
|
||||
# Proxmox VE backend.
|
||||
#
|
||||
# Hardware model mirrors the QEMU platform: q35 + SATA + e1000, so the same
|
||||
# unattended file and the same role scripts work unchanged. Provisioning talks
|
||||
# to the QEMU guest agent (`qm guest exec`), so the Proxmox host needs no
|
||||
# Python packages beyond its own.
|
||||
|
||||
# ------------------------------------------------------------------ helpers
|
||||
pve_vmid() {
|
||||
local i
|
||||
i=$(vm_index "$1") || return 1
|
||||
printf '%s' $(( PVE_VMID_BASE + i ))
|
||||
}
|
||||
|
||||
pve_template_vmid() {
|
||||
local i
|
||||
i=$(vm_index "$1") || return 1
|
||||
printf '%s' $(( PVE_VMID_BASE + 100 + i ))
|
||||
}
|
||||
|
||||
pve_exists() { qm status "$1" >/dev/null 2>&1; }
|
||||
pve_is_template() { qm config "$1" 2>/dev/null | grep -qx 'template: 1'; }
|
||||
pve_running() { qm status "$1" 2>/dev/null | grep -q 'status: running'; }
|
||||
pve_ip() { local i; i=$(vm_index "$1"); printf '%s' "${VM_IPS[$i]}"; }
|
||||
|
||||
# ------------------------------------------------------------------ preflight
|
||||
# Which storages advertise iso content. `pvesm status --content` is not
|
||||
# available on every version, so fall back to parsing storage.cfg.
|
||||
pve_iso_storages() {
|
||||
local out
|
||||
out=$(pvesm status --content iso 2>/dev/null | awk 'NR>1{print $1}' | tr '\n' ' ')
|
||||
if [[ -z ${out// /} && -r /etc/pve/storage.cfg ]]; then
|
||||
out=$(awk '/^[a-z]+: /{name=$2} /^[[:space:]]*content[[:space:]]/ && /iso/ {print name}' \
|
||||
/etc/pve/storage.cfg | tr '\n' ' ')
|
||||
fi
|
||||
printf '%s' "$out"
|
||||
}
|
||||
|
||||
backend_check() {
|
||||
if ! command -v qm >/dev/null 2>&1; then
|
||||
_c_fail "qm not found — this script must run on a Proxmox VE host"
|
||||
return
|
||||
fi
|
||||
_c_ok "qm present ($(pveversion 2>/dev/null | cut -d/ -f1-2))"
|
||||
|
||||
command -v pvesm >/dev/null 2>&1 && _c_ok "pvesm present" || _c_fail "pvesm missing"
|
||||
|
||||
# storages
|
||||
local iso_content
|
||||
if pvesm status 2>/dev/null | awk 'NR>1{print $1}' | grep -qx "$PVE_STORAGE_DISKS"; then
|
||||
_c_ok "disk storage ${PVE_STORAGE_DISKS} present"
|
||||
else
|
||||
_c_fail "disk storage ${PVE_STORAGE_DISKS} not found (run pvesm status)"
|
||||
fi
|
||||
|
||||
iso_content=$(pve_iso_storages)
|
||||
if [[ " $iso_content " == *" ${PVE_STORAGE_ISO} "* ]]; then
|
||||
_c_ok "iso storage ${PVE_STORAGE_ISO} present"
|
||||
else
|
||||
_c_fail "storage ${PVE_STORAGE_ISO} has no iso content (available: ${iso_content:-none})"
|
||||
fi
|
||||
|
||||
if [[ -d $PVE_ISO_DIR && -w $PVE_ISO_DIR ]]; then
|
||||
_c_ok "iso directory ${PVE_ISO_DIR} writable"
|
||||
else
|
||||
_c_fail "iso directory ${PVE_ISO_DIR} missing or not writable"
|
||||
fi
|
||||
|
||||
# isolated bridge
|
||||
if [[ -d /sys/class/net/$PVE_BRIDGE ]]; then
|
||||
if ip -4 -o addr show dev "$PVE_BRIDGE" 2>/dev/null | grep -q "inet ${LAB_HOST_IP}/"; then
|
||||
_c_ok "bridge ${PVE_BRIDGE} has ${LAB_HOST_IP}"
|
||||
else
|
||||
_c_fail "bridge ${PVE_BRIDGE} exists but does not carry ${LAB_HOST_IP}"
|
||||
fi
|
||||
else
|
||||
_c_fail "bridge ${PVE_BRIDGE} missing — create it in the GUI (see lab.conf)"
|
||||
fi
|
||||
|
||||
# guest agent is what provisioning talks to; it cannot be probed before boot
|
||||
_c_ok "provisioning over qm guest exec (agent installed by bootstrap)"
|
||||
|
||||
[[ -f $WINDOWS_ISO ]] && _c_ok "Windows ISO present" \
|
||||
|| _c_warn "Windows ISO missing: run ./lab.sh isos"
|
||||
|
||||
if command -v genisoimage >/dev/null 2>&1 || command -v xorriso >/dev/null 2>&1; then
|
||||
_c_ok "ISO tooling present"
|
||||
else
|
||||
_c_fail "genisoimage or xorriso required to build seed ISOs"
|
||||
fi
|
||||
}
|
||||
|
||||
# ------------------------------------------------------------------ lifecycle
|
||||
backend_running() {
|
||||
local vmid
|
||||
vmid=$(pve_vmid "$1") || return 1
|
||||
pve_running "$vmid"
|
||||
}
|
||||
|
||||
backend_create_disk() {
|
||||
local name=$1 dry=${2:-0}
|
||||
local i vmid ram disk vcpu seed win mac
|
||||
|
||||
i=$(vm_index "$name") || die "unknown vm: $name"
|
||||
vmid=$(pve_vmid "$name")
|
||||
ram=${VM_RAM[$i]}; disk=${VM_DISK[$i]}; vcpu=${VM_VCPU[$i]}
|
||||
seed="$SEED_DIR/${name}-seed.iso"
|
||||
win=$WINDOWS_ISO
|
||||
mac=$(qemu_mac_for "${VM_IPS[$i]}")
|
||||
|
||||
if pve_exists "$vmid"; then
|
||||
ok "$(printf '%-6s vm %s already exists' "$name" "$vmid")"
|
||||
return 0
|
||||
fi
|
||||
[[ -f $seed ]] || (( dry )) || die "missing seed ISO for $name (run ./lab.sh seeds)"
|
||||
[[ -f $win ]] || (( dry )) || die "missing Windows ISO: $win (run ./lab.sh isos)"
|
||||
|
||||
local args=(
|
||||
"$vmid"
|
||||
--name "norbourg-$name"
|
||||
--memory "$ram" --cores "$vcpu" --sockets 1
|
||||
--machine "$PVE_MACHINE" --bios "$PVE_BIOS" --ostype "$PVE_OSTYPE" --cpu "$PVE_CPU"
|
||||
--net0 "${PVE_NET_MODEL},bridge=${PVE_BRIDGE},macaddr=${mac}"
|
||||
--sata0 "${PVE_STORAGE_DISKS}:${disk}"
|
||||
--ide2 "${PVE_STORAGE_ISO}:iso/$(basename "$win"),media=cdrom"
|
||||
--ide0 "${PVE_STORAGE_ISO}:iso/$(basename "$seed"),media=cdrom"
|
||||
--boot "order=ide2;ide0"
|
||||
--onboot 0
|
||||
)
|
||||
(( PVE_AGENT )) && args+=( --agent enabled=1 )
|
||||
|
||||
# virtio-win carries the QEMU guest agent that provisioning talks to.
|
||||
if [[ -f $VIRTIO_ISO ]]; then
|
||||
args+=( --ide3 "${PVE_STORAGE_ISO}:iso/$(basename "$VIRTIO_ISO"),media=cdrom" )
|
||||
else
|
||||
warn "virtio-win.iso missing: the guest agent cannot be installed (run ./lab.sh isos)"
|
||||
fi
|
||||
|
||||
if (( dry )); then
|
||||
info " + qm create ${args[*]}"
|
||||
return 0
|
||||
fi
|
||||
|
||||
qm create "${args[@]}" || { fail "qm create failed for $name"; return 1; }
|
||||
ok "$(printf '%-6s vm %s created (%sG on %s)' "$name" "$vmid" "$disk" "$PVE_STORAGE_DISKS")"
|
||||
}
|
||||
|
||||
backend_launch() {
|
||||
local name=$1 mode=${2:-run} dry=${3:-0}
|
||||
local vmid
|
||||
vmid=$(pve_vmid "$name")
|
||||
|
||||
if (( dry )); then
|
||||
info " + qm start $vmid"
|
||||
return 0
|
||||
fi
|
||||
pve_running "$vmid" && { info " $name already running"; return 0; }
|
||||
|
||||
qm start "$vmid" >/dev/null || { fail "qm start failed for $name"; return 1; }
|
||||
ok "$(printf '%-6s started (vm %s)' "$name" "$vmid")"
|
||||
}
|
||||
|
||||
backend_stop() {
|
||||
local name=$1 vmid
|
||||
vmid=$(pve_vmid "$name")
|
||||
|
||||
pve_running "$vmid" || { info " $name already stopped"; return 0; }
|
||||
|
||||
qm shutdown "$vmid" --timeout 90 >/dev/null 2>&1 || true
|
||||
if pve_running "$vmid"; then
|
||||
warn "$name did not shut down cleanly, forcing stop"
|
||||
qm stop "$vmid" >/dev/null 2>&1 || true
|
||||
sleep 2
|
||||
fi
|
||||
ok "$name stopped"
|
||||
}
|
||||
|
||||
backend_snapshot() {
|
||||
local name=$1 vmid tpl
|
||||
vmid=$(pve_vmid "$name")
|
||||
tpl=$(pve_template_vmid "$name")
|
||||
|
||||
pve_running "$vmid" && { fail "$name is still running"; return 1; }
|
||||
|
||||
if pve_exists "$tpl"; then
|
||||
ok "$(printf '%-6s template %s already exists' "$name" "$tpl")"
|
||||
return 0
|
||||
fi
|
||||
pve_exists "$vmid" || { fail "$name has no VM to freeze"; return 1; }
|
||||
|
||||
qm template "$vmid" >/dev/null || { fail "qm template failed for $name"; return 1; }
|
||||
ok "$(printf '%-6s frozen as template %s' "$name" "$tpl")"
|
||||
}
|
||||
|
||||
backend_has_baseline() { pve_exists "$(pve_template_vmid "$1")"; }
|
||||
|
||||
backend_reset() {
|
||||
local name=$1 vmid tpl
|
||||
vmid=$(pve_vmid "$name")
|
||||
tpl=$(pve_template_vmid "$name")
|
||||
|
||||
pve_running "$vmid" && backend_stop "$name" >/dev/null 2>&1 || true
|
||||
pve_exists "$tpl" || { fail "$name has no template — run ./lab.sh snapshot first"; return 1; }
|
||||
|
||||
pve_exists "$vmid" && qm destroy "$vmid" --purge 1 >/dev/null 2>&1
|
||||
qm clone "$tpl" "$vmid" --name "norbourg-$name" >/dev/null \
|
||||
|| { fail "qm clone failed for $name"; return 1; }
|
||||
ok "$(printf '%-6s reverted from template %s' "$name" "$tpl")"
|
||||
}
|
||||
|
||||
backend_destroy() {
|
||||
local name=$1 vmid tpl
|
||||
vmid=$(pve_vmid "$name")
|
||||
tpl=$(pve_template_vmid "$name")
|
||||
|
||||
pve_running "$vmid" && backend_stop "$name" >/dev/null 2>&1 || true
|
||||
if pve_exists "$vmid"; then
|
||||
qm destroy "$vmid" --purge 1 >/dev/null 2>&1 || true
|
||||
fi
|
||||
if pve_exists "$tpl"; then
|
||||
qm destroy "$tpl" --purge 1 >/dev/null 2>&1 || true
|
||||
fi
|
||||
ok "$name removed"
|
||||
}
|
||||
|
||||
# ------------------------------------------------------------------ transport
|
||||
# qm guest exec returns JSON; unwrap it without adding Python dependencies.
|
||||
_qga() {
|
||||
local name=$1; shift
|
||||
local vmid
|
||||
vmid=$(pve_vmid "$name")
|
||||
qm guest exec "$vmid" --timeout "${QGA_TIMEOUT:-180}" -- "$@"
|
||||
}
|
||||
|
||||
_qga_unwrap() {
|
||||
python3 - "$1" <<'PY'
|
||||
import json, sys
|
||||
try:
|
||||
data = json.loads(sys.argv[1])
|
||||
except Exception:
|
||||
print("could not parse guest agent output", file=sys.stderr)
|
||||
sys.exit(2)
|
||||
sys.stdout.write(data.get("out", ""))
|
||||
if data.get("err", "").strip():
|
||||
sys.stderr.write(data["err"])
|
||||
sys.exit(int(data.get("exitcode", 0)))
|
||||
PY
|
||||
}
|
||||
|
||||
backend_ps() {
|
||||
local name=$1 script=$2 json
|
||||
json=$(_qga "$name" powershell.exe -NoProfile -NonInteractive -ExecutionPolicy Bypass -Command "$script" 2>/dev/null) \
|
||||
|| return 2
|
||||
_qga_unwrap "$json"
|
||||
}
|
||||
|
||||
backend_ready() {
|
||||
local name=$1 timeout=${2:-300}
|
||||
local deadline=$(( SECONDS + timeout )) vmid
|
||||
vmid=$(pve_vmid "$name")
|
||||
|
||||
while (( SECONDS < deadline )); do
|
||||
pve_running "$vmid" || { sleep 10; continue; }
|
||||
if _qga "$name" cmd.exe /c exit >/dev/null 2>&1; then
|
||||
return 0
|
||||
fi
|
||||
sleep 10
|
||||
done
|
||||
fail "$name: guest agent not answering (is qemu-ga installed? see C:\\norbourg\\bootstrap.log)"
|
||||
return 1
|
||||
}
|
||||
|
||||
backend_start() {
|
||||
local name=$1 remote=$2
|
||||
backend_ps "$name" "
|
||||
Remove-Item -Force -ErrorAction SilentlyContinue '${remote}.done'
|
||||
schtasks.exe /End /TN NorbourgStage 2>\$null | Out-Null
|
||||
schtasks.exe /Delete /TN NorbourgStage /F 2>\$null | Out-Null
|
||||
\$tr = 'powershell.exe -NoProfile -ExecutionPolicy Bypass -File \"${remote}\"'
|
||||
schtasks.exe /Create /TN NorbourgStage /F /SC ONCE /ST 00:00 /RU SYSTEM /RL HIGHEST /TR \$tr | Out-Null
|
||||
schtasks.exe /Run /TN NorbourgStage | Out-Null
|
||||
"
|
||||
}
|
||||
|
||||
backend_wait() {
|
||||
local name=$1 remote=$2 timeout=${3:-1800}
|
||||
local deadline=$(( SECONDS + timeout )) out
|
||||
|
||||
while (( SECONDS < deadline )); do
|
||||
out=$(backend_ps "$name" "Test-Path '${remote}.done'" 2>/dev/null | tr -d '\r\n ')
|
||||
if [[ $out == True ]]; then
|
||||
backend_ps "$name" "Get-Content -Raw '${remote}.done'" 2>/dev/null | sed '/^$/d'
|
||||
return 0
|
||||
fi
|
||||
sleep 10
|
||||
done
|
||||
return 3
|
||||
}
|
||||
|
||||
# ------------------------------------------------------------------ peers
|
||||
# Generate one WireGuard config per participant, plus the router's wg0.conf.
|
||||
cmd_peers() {
|
||||
local names=("$@")
|
||||
(( ${#names[@]} )) || die "usage: ./lab.sh peers <name> [name...]"
|
||||
|
||||
have_cmd wg || die "wireguard-tools is required (apt install wireguard-tools)"
|
||||
|
||||
mkdir -p "$WG_DIR"
|
||||
chmod 700 "$WG_DIR"
|
||||
|
||||
if [[ ! -f $WG_DIR/server.key ]]; then
|
||||
( umask 077; wg genkey > "$WG_DIR/server.key" )
|
||||
wg pubkey < "$WG_DIR/server.key" > "$WG_DIR/server.pub"
|
||||
ok "server key generated"
|
||||
fi
|
||||
|
||||
local endpoint=${WG_ENDPOINT:-}
|
||||
if [[ -z $endpoint ]]; then
|
||||
local iface ip
|
||||
iface=$(ip route show default 2>/dev/null | awk '/default/{print $5; exit}')
|
||||
ip=$(ip -4 -o addr show dev "${iface:-vmbr0}" 2>/dev/null | awk '{print $4}' | cut -d/ -f1 | head -1)
|
||||
endpoint="${ip:-<LAN-IP>}:${WG_PORT}"
|
||||
else
|
||||
endpoint="${endpoint}:${WG_PORT}"
|
||||
fi
|
||||
|
||||
local i name addr priv pub psk router_peers=""
|
||||
local LAB_IFACE=${WG_LAB_IFACE:-eth1}
|
||||
for i in "${!names[@]}"; do
|
||||
name=${names[$i]}
|
||||
addr="10.10.10.$(( 101 + i ))/32"
|
||||
|
||||
( umask 077; wg genkey > "$WG_DIR/${name}.key" )
|
||||
wg pubkey < "$WG_DIR/${name}.key" > "$WG_DIR/${name}.pub"
|
||||
( umask 077; wg genpsk > "$WG_DIR/${name}.psk" )
|
||||
|
||||
priv=$(<"$WG_DIR/${name}.key")
|
||||
pub=$(<"$WG_DIR/${name}.pub")
|
||||
psk=$(<"$WG_DIR/${name}.psk")
|
||||
|
||||
cat > "$WG_DIR/${name}.conf" <<CONF
|
||||
[Interface]
|
||||
PrivateKey = ${priv}
|
||||
Address = ${addr}
|
||||
DNS = ${LAB_DC_IP}
|
||||
|
||||
[Peer]
|
||||
PublicKey = $(<"$WG_DIR/server.pub")
|
||||
PresharedKey = ${psk}
|
||||
Endpoint = ${endpoint}
|
||||
AllowedIPs = ${LAB_SUBNET}
|
||||
PersistentKeepalive = 25
|
||||
CONF
|
||||
chmod 600 "$WG_DIR/${name}.conf"
|
||||
|
||||
router_peers+="
|
||||
[Peer]
|
||||
# ${name}
|
||||
PublicKey = ${pub}
|
||||
PresharedKey = ${psk}
|
||||
AllowedIPs = ${addr}"
|
||||
ok "$(printf '%-10s %-16s %s' "$name" "${addr%/32}" "$WG_DIR/${name}.conf")"
|
||||
done
|
||||
|
||||
cat > "$WG_DIR/wg0.conf" <<CONF
|
||||
# Router VM — /etc/wireguard/wg0.conf, then: systemctl enable --now wg-quick@wg0
|
||||
#
|
||||
# The VM needs TWO interfaces:
|
||||
# eth0 on vmbr0 (your LAN) — optional, for management
|
||||
# eth1 on vmbr10 (the lab bridge) — give it 10.10.10.251/24
|
||||
# wg0 is a /32 inside the lab subnet so it does not collide with those /24s:
|
||||
# traffic to .250 goes through the tunnel, everything else through eth1.
|
||||
# Adjust LAB_IFACE below if your lab NIC is not eth1.
|
||||
[Interface]
|
||||
Address = 10.10.10.250/32
|
||||
ListenPort = ${WG_PORT}
|
||||
PrivateKey = $(<"$WG_DIR/server.key")
|
||||
PostUp = sysctl -w net.ipv4.ip_forward=1
|
||||
PostUp = nft add rule inet filter forward iifname "wg0" oifname "${LAB_IFACE}" accept
|
||||
PostUp = nft add rule inet filter forward iifname "${LAB_IFACE}" oifname "wg0" ct state established,related accept
|
||||
PostDown = nft delete rule inet filter forward iifname "wg0" oifname "${LAB_IFACE}" accept
|
||||
PostDown = nft delete rule inet filter forward iifname "${LAB_IFACE}" oifname "wg0" ct state established,related accept
|
||||
${router_peers}
|
||||
CONF
|
||||
chmod 600 "$WG_DIR/wg0.conf"
|
||||
|
||||
printf '\n'
|
||||
ok "peer configs in ${WG_DIR}"
|
||||
info " router config: ${WG_DIR}/wg0.conf (address 10.10.10.250 on the lab bridge)"
|
||||
}
|
||||
|
||||
|
||||
# After a successful install, stop booting from the installer CD: otherwise
|
||||
# every boot waits five seconds and a stray keypress re-enters Setup.
|
||||
backend_finalize() {
|
||||
local name=$1 vmid
|
||||
vmid=$(pve_vmid "$name")
|
||||
qm set "$vmid" --boot 'order=sata0' --delete ide2 >/dev/null 2>&1 || true
|
||||
ok "$(printf '%-6s boots from disk (installer ISO detached)' "$name")"
|
||||
}
|
||||
Reference in New Issue
Block a user