Phase 1: scaffold lab.sh, config and preflight checks

Move the archived 2002 site into site-src/ so the lab README can take the
repo root. Add lab.conf, the lab.sh dispatcher, a qemu backend and the
check/init verbs. No VM is created yet.
This commit is contained in:
2026-10-21 18:34:22 -04:00
commit fb06ee5a52
315 changed files with 16171 additions and 0 deletions
+130
View File
@@ -0,0 +1,130 @@
# shellcheck shell=bash
# Shared helpers for lab.sh. Sourced, never executed directly.
LAB_SH_VERSION="0.1.0"
# ------------------------------------------------------------------ output
if [[ -t 1 && -z ${NO_COLOR:-} ]]; then
C_RESET=$'\e[0m'; C_BOLD=$'\e[1m'; C_DIM=$'\e[2m'
C_RED=$'\e[31m'; C_GREEN=$'\e[32m'; C_YELLOW=$'\e[33m'; C_BLUE=$'\e[34m'
else
C_RESET=""; C_BOLD=""; C_DIM=""; C_RED=""; C_GREEN=""; C_YELLOW=""; C_BLUE=""
fi
VERBOSE=${VERBOSE:-0}
STRICT=${STRICT:-0}
info() { printf '%s\n' "$*"; }
raw() { printf '%s\n' "$*"; }
ok() { printf '%s[ ok ]%s %s\n' "$C_GREEN" "$C_RESET" "$*"; }
warn() { printf '%s[warn]%s %s\n' "$C_YELLOW" "$C_RESET" "$*" >&2; }
fail() { printf '%s[fail]%s %s\n' "$C_RED" "$C_RESET" "$*" >&2; }
die() { fail "$*"; exit 1; }
step() { printf '\n%s==> %s%s\n' "$C_BLUE" "$*" "$C_RESET"; }
detail() { if (( VERBOSE )); then printf '%s %s%s\n' "$C_DIM" "$*" "$C_RESET"; fi; return 0; }
# ------------------------------------------------------------------ commands
have_cmd() { command -v "$1" >/dev/null 2>&1; }
require_cmd() {
have_cmd "$1" || die "required command not found: $1${2:+ ($2)}"
}
# ------------------------------------------------------------------ config
load_config() {
LAB_CONF=${LAB_CONF:-$LAB_DIR/lab.conf}
[[ -r $LAB_CONF ]] || die "config not found: $LAB_CONF"
set -a
# shellcheck disable=SC1090
source "$LAB_CONF"
set +a
# A local override wins over the committed config (sourced last on purpose).
if [[ -r $LAB_DIR/lab.conf.local ]]; then
set -a
# shellcheck disable=SC1091
source "$LAB_DIR/lab.conf.local"
set +a
fi
: "${LAB_ROOT:?LAB_ROOT is not set}"
: "${LAB_ISO_CACHE:?LAB_ISO_CACHE is not set}"
: "${BRIDGE:?BRIDGE is not set}"
: "${LAB_PREFIX:?LAB_PREFIX is not set}"
BACKEND=${BACKEND:-qemu}
MIN_FREE_GB=${MIN_FREE_GB:-20}
DISK_FILL_RATIO=${DISK_FILL_RATIO:-60}
ISO_BUDGET_GB=${ISO_BUDGET_GB:-8}
load_vms
}
# VMS is a small whitespace-separated table; load it into parallel arrays.
load_vms() {
VM_NAMES=(); VM_IPS=(); VM_RAM=(); VM_DISK=(); VM_VCPU=(); VM_ROLES=()
local name ip ram disk vcpu role
while read -r name ip ram disk vcpu role; do
[[ -z ${name:-} || ${name:0:1} == '#' ]] && continue
VM_NAMES+=("$name")
VM_IPS+=("$ip")
VM_RAM+=("$ram")
VM_DISK+=("$disk")
VM_VCPU+=("$vcpu")
VM_ROLES+=("$role")
done <<< "${VMS:-}"
(( ${#VM_NAMES[@]} > 0 )) || die "no VMs defined in VMS (lab.conf)"
}
sum_vm_ram_mb() {
local total=0 r
for r in "${VM_RAM[@]}"; do total=$(( total + r )); done
printf '%s\n' "$total"
}
sum_vm_disk_gb() {
local total=0 d
for d in "${VM_DISK[@]}"; do total=$(( total + d )); done
printf '%s\n' "$total"
}
# ------------------------------------------------------------------ filesystem
# Walk up until the path exists; useful before LAB_ROOT is created.
existing_ancestor() {
local p=$1
while [[ ! -e $p ]]; do p=$(dirname -- "$p"); done
printf '%s\n' "$p"
}
fstype_of() {
stat -f -c %T -- "$(existing_ancestor "$1")" 2>/dev/null || printf 'unknown\n'
}
device_of() {
df --output=source -- "$(existing_ancestor "$1")" 2>/dev/null | tail -n1
}
free_gb_of() {
df -BG --output=avail -- "$(existing_ancestor "$1")" 2>/dev/null | tail -n1 | tr -dc '0-9'
}
# ext4 marks a filesystem "emergency_ro" in /proc/mounts after a fatal I/O error.
is_emergency_ro() {
local dev
dev=$(device_of "$1")
[[ -n $dev ]] || return 1
awk -v d="$dev" '$1 == d && $4 ~ /emergency_ro/ { found = 1 } END { exit(found ? 0 : 1) }' /proc/mounts
}
is_mounted() {
local target
target=$(mountpoint_of "$1")
[[ -n $target && $target != "/" ]]
}
mountpoint_of() {
findmnt -rn --target "$(existing_ancestor "$1")" -o TARGET 2>/dev/null
}