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:
+195
@@ -0,0 +1,195 @@
|
||||
# shellcheck shell=bash
|
||||
# cmd_check — preflight for the selected backend.
|
||||
# Fatal problems set CHECK_FAILS, advisory ones set CHECK_WARNS.
|
||||
# `--strict` turns warnings into a non-zero exit.
|
||||
|
||||
CHECK_FAILS=0
|
||||
CHECK_WARNS=0
|
||||
|
||||
_c_ok() { ok "$*"; }
|
||||
_c_warn() { warn "$*"; CHECK_WARNS=$(( CHECK_WARNS + 1 )); }
|
||||
_c_fail() { fail "$*"; CHECK_FAILS=$(( CHECK_FAILS + 1 )); }
|
||||
|
||||
cmd_check() {
|
||||
CHECK_FAILS=0
|
||||
CHECK_WARNS=0
|
||||
|
||||
local os_pretty="unknown release"
|
||||
if [[ -r /etc/os-release ]]; then
|
||||
# shellcheck disable=SC1091
|
||||
os_pretty=$(. /etc/os-release; printf '%s' "${PRETTY_NAME:-unknown release}")
|
||||
fi
|
||||
|
||||
step "host"
|
||||
info " ${os_pretty} · $(nproc) threads · kernel $(uname -r)"
|
||||
_check_commands
|
||||
_check_kvm
|
||||
_check_ram
|
||||
|
||||
step "storage"
|
||||
_check_storage
|
||||
|
||||
step "network"
|
||||
_check_network
|
||||
|
||||
step "config"
|
||||
_check_config
|
||||
_check_backend
|
||||
|
||||
printf '\n'
|
||||
if (( CHECK_FAILS > 0 )); then
|
||||
fail "${CHECK_FAILS} blocking issue(s), ${CHECK_WARNS} warning(s)"
|
||||
return 1
|
||||
fi
|
||||
if (( CHECK_WARNS > 0 && STRICT )); then
|
||||
warn "${CHECK_WARNS} warning(s) with --strict"
|
||||
return 1
|
||||
fi
|
||||
ok "preflight passed · ${CHECK_WARNS} warning(s)"
|
||||
}
|
||||
|
||||
_check_commands() {
|
||||
local missing=() cmd
|
||||
for cmd in qemu-system-x86_64 qemu-img socat python3 curl; do
|
||||
have_cmd "$cmd" || missing+=("$cmd")
|
||||
done
|
||||
if (( ${#missing[@]} > 0 )); then
|
||||
_c_fail "missing commands: ${missing[*]}"
|
||||
else
|
||||
_c_ok "required commands present"
|
||||
fi
|
||||
have_cmd genisoimage || have_cmd xorriso \
|
||||
|| _c_warn "no genisoimage/xorriso: seed ISOs cannot be built"
|
||||
}
|
||||
|
||||
_check_kvm() {
|
||||
if [[ ! -e /dev/kvm ]]; then
|
||||
_c_fail "/dev/kvm absent: software emulation would be unusably slow"
|
||||
return
|
||||
fi
|
||||
if [[ -r /dev/kvm && -w /dev/kvm ]]; then
|
||||
_c_ok "KVM device accessible to $(id -un)"
|
||||
else
|
||||
_c_fail "/dev/kvm not readable/writable by $(id -un)"
|
||||
fi
|
||||
if grep -qm1 -E '(vmx|svm)' /proc/cpuinfo; then
|
||||
_c_ok "CPU virtualization flag present"
|
||||
else
|
||||
_c_fail "CPU reports no vmx/svm flag"
|
||||
fi
|
||||
}
|
||||
|
||||
_check_ram() {
|
||||
local total avail need
|
||||
total=$(awk '/^MemTotal:/{printf "%d", $2/1024}' /proc/meminfo)
|
||||
avail=$(awk '/^MemAvailable:/{printf "%d", $2/1024}' /proc/meminfo)
|
||||
need=$(( $(sum_vm_ram_mb) + 2048 ))
|
||||
detail "total ${total} MB · available ${avail} MB · VMs ${need} MB"
|
||||
if (( avail < need )); then
|
||||
_c_warn "only ${avail} MB available, ${need} MB planned (close heavy apps)"
|
||||
else
|
||||
_c_ok "RAM: ${avail} MB available, ${need} MB planned"
|
||||
fi
|
||||
}
|
||||
|
||||
_check_storage() {
|
||||
local ft free need_gb nominal dev
|
||||
|
||||
ft=$(fstype_of "$LAB_ROOT")
|
||||
case $ft in
|
||||
fuseblk|vfat|exfat)
|
||||
_c_fail "LAB_ROOT is on ${ft}: qcow2 needs a POSIX filesystem"
|
||||
;;
|
||||
*)
|
||||
_c_ok "LAB_ROOT filesystem: ${ft}"
|
||||
;;
|
||||
esac
|
||||
|
||||
dev=$(device_of "$LAB_ROOT")
|
||||
if is_emergency_ro "$LAB_ROOT"; then
|
||||
_c_fail "${dev} is mounted read-only after I/O errors"
|
||||
fi
|
||||
if [[ $dev == /dev/sda1 ]]; then
|
||||
_c_fail "LAB_ROOT resolves to ${dev}, the known-bad disk"
|
||||
fi
|
||||
if [[ $LAB_ROOT == *oof* || $LAB_ISO_CACHE == *oof* ]]; then
|
||||
_c_fail "a configured path points at /media/oof (excluded on purpose)"
|
||||
fi
|
||||
|
||||
free=$(free_gb_of "$LAB_ROOT")
|
||||
nominal=$(sum_vm_disk_gb)
|
||||
need_gb=$(( ISO_BUDGET_GB + nominal * DISK_FILL_RATIO / 100 + MIN_FREE_GB ))
|
||||
detail "free ${free} GB · projected need ${need_gb} GB"
|
||||
if (( free < need_gb )); then
|
||||
_c_fail "not enough space on ${LAB_ROOT}: ${free} GB free, ${need_gb} GB projected"
|
||||
else
|
||||
_c_ok "disk: ${free} GB free, ${need_gb} GB projected"
|
||||
fi
|
||||
|
||||
if is_mounted "$LAB_ISO_CACHE"; then
|
||||
_c_ok "ISO cache mounted: $(mountpoint_of "$LAB_ISO_CACHE")"
|
||||
else
|
||||
_c_warn "ISO cache parent $(dirname "$LAB_ISO_CACHE") is not a mount point"
|
||||
fi
|
||||
|
||||
if [[ $ft == btrfs ]]; then
|
||||
if [[ -d $LAB_ROOT/images ]] && lsattr -d "$LAB_ROOT/images" 2>/dev/null | awk '{print $1}' | grep -q 'C'; then
|
||||
_c_ok "images/ has +C (CoW disabled)"
|
||||
else
|
||||
_c_warn "images/ missing or without +C: run ./lab.sh init"
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
_check_network() {
|
||||
local addrs
|
||||
|
||||
if ip link show "$BRIDGE" >/dev/null 2>&1; then
|
||||
_c_ok "bridge ${BRIDGE} exists"
|
||||
else
|
||||
_c_warn "bridge ${BRIDGE} missing: run sudo ./scripts/setup-network.sh"
|
||||
fi
|
||||
|
||||
if [[ -r /etc/qemu/bridge.conf ]] && grep -qx "allow ${BRIDGE}" /etc/qemu/bridge.conf; then
|
||||
_c_ok "qemu-bridge-helper allows ${BRIDGE}"
|
||||
else
|
||||
_c_warn "/etc/qemu/bridge.conf does not allow ${BRIDGE}"
|
||||
fi
|
||||
|
||||
if [[ $(sysctl -n net.ipv4.ip_forward 2>/dev/null) == 1 ]]; then
|
||||
_c_ok "ip_forward enabled"
|
||||
else
|
||||
_c_warn "ip_forward disabled"
|
||||
fi
|
||||
|
||||
addrs=$(ip -4 -o addr show scope global 2>/dev/null | awk '{ printf "%s=%s ", $2, $4 }')
|
||||
detail "host addresses: ${addrs}"
|
||||
}
|
||||
|
||||
_check_config() {
|
||||
local i name dup bad=0
|
||||
|
||||
for i in "${!VM_NAMES[@]}"; do
|
||||
name=${VM_NAMES[$i]}
|
||||
if [[ ${VM_IPS[$i]} != "${LAB_PREFIX}."* ]]; then
|
||||
_c_fail "vm ${name}: ${VM_IPS[$i]} is outside ${LAB_SUBNET}"
|
||||
bad=1
|
||||
fi
|
||||
[[ ${VM_RAM[$i]} =~ ^[0-9]+$ ]] || { _c_fail "vm ${name}: ram_mb not numeric"; bad=1; }
|
||||
[[ ${VM_DISK[$i]} =~ ^[0-9]+$ ]] || { _c_fail "vm ${name}: disk_gb not numeric"; bad=1; }
|
||||
done
|
||||
|
||||
dup=$(printf '%s\n' "${VM_IPS[@]}" | sort | uniq -d)
|
||||
[[ -z $dup ]] || { _c_fail "duplicate IPs: ${dup}"; bad=1; }
|
||||
|
||||
(( bad )) || _c_ok "config: ${#VM_NAMES[@]} VMs on ${LAB_SUBNET}"
|
||||
detail "domain ${DOMAIN} · netbios ${NETBIOS} · backend ${BACKEND}"
|
||||
}
|
||||
|
||||
_check_backend() {
|
||||
case $BACKEND in
|
||||
qemu) backend_check ;;
|
||||
proxmox) _c_warn "proxmox backend is still a stub (phase 11)" ;;
|
||||
*) _c_fail "unknown backend: ${BACKEND}" ;;
|
||||
esac
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
# shellcheck shell=bash
|
||||
# cmd_init — create the storage layout. No ISOs downloaded yet (phase 3).
|
||||
|
||||
cmd_init() {
|
||||
step "storage layout"
|
||||
mkdir -p "$LAB_ROOT"/images "$LAB_ROOT"/seeds "$LAB_ROOT"/run "$LAB_ROOT"/logs
|
||||
ok "LAB_ROOT ${LAB_ROOT}"
|
||||
|
||||
# chattr +C must be set on the directory *before* any file is created in it,
|
||||
# otherwise btrfs keeps CoW for the existing tree.
|
||||
if [[ $(fstype_of "$LAB_ROOT") == btrfs ]]; then
|
||||
mkdir -p "$LAB_ROOT/images/base" "$LAB_ROOT/images/overlays"
|
||||
if chattr +C "$LAB_ROOT/images" 2>/dev/null; then
|
||||
if lsattr -d "$LAB_ROOT/images" 2>/dev/null | awk '{print $1}' | grep -q 'C'; then
|
||||
ok "images/ : +C set (CoW disabled)"
|
||||
else
|
||||
warn "images/ : chattr +C did not stick"
|
||||
fi
|
||||
else
|
||||
warn "images/ : chattr +C failed (btrfs?)"
|
||||
fi
|
||||
else
|
||||
mkdir -p "$LAB_ROOT/images/base" "$LAB_ROOT/images/overlays"
|
||||
info " images/ : $(fstype_of "$LAB_ROOT") — no +C needed"
|
||||
fi
|
||||
|
||||
step "iso cache"
|
||||
if is_mounted "$LAB_ISO_CACHE" || is_mounted "$(dirname "$LAB_ISO_CACHE")"; then
|
||||
mkdir -p "$LAB_ISO_CACHE"
|
||||
ok "ISO cache ${LAB_ISO_CACHE}"
|
||||
else
|
||||
warn "ISO cache parent $(dirname "$LAB_ISO_CACHE") is not mounted — skipped"
|
||||
fi
|
||||
|
||||
step "directories"
|
||||
for d in images/base images/overlays seeds run logs; do
|
||||
printf ' %-24s %s\n' "$d" "$([[ -d $LAB_ROOT/$d ]] && echo present || echo missing)"
|
||||
done
|
||||
|
||||
printf '\n'
|
||||
ok "layout ready — next: ./lab.sh check"
|
||||
}
|
||||
+130
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user