# 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 }