Files
pentest.intro/content/lib/check.sh
T
quoije 890dd58700 Split into qemu and proxmox platforms over a shared content tree
platforms/qemu and platforms/proxmox each have their own lab.sh and
lab.conf; content/ holds everything shared (roles, seed, site, helpers).
The dispatcher, preflight and command implementations are common and take
platform differences from the backend.

Proxmox backend: qm create with q35/SATA/e1000, ISOs and seed ISOs in an
ISO storage, provisioning over qm guest exec (no Python packages needed on
the host), template + clone for reset, and peers/ to emit WireGuard configs.
The role scripts now ride on the seed ISO, so provisioning transfers nothing.

build-bundle.sh produces a self-contained tarball per platform, for hosts
that cannot reach the git server.

Both configs are generic: no absolute path from this machine remains.
2026-10-22 19:23:16 -04:00

214 lines
6.0 KiB
Bash

# 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() {
local arg
for arg in "$@"; do
[[ $arg == --strict ]] && STRICT=1
done
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) · ${PLATFORM:-qemu} platform"
_check_commands
[[ ${PLATFORM:-qemu} == qemu ]] && _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
local needed
if [[ ${PLATFORM:-qemu} == proxmox ]]; then
needed=(qm pvesm python3 curl)
else
needed=(qemu-system-x86_64 qemu-img socat python3 curl)
fi
for cmd in "${needed[@]}"; 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 [[ ${PLATFORM:-qemu} == qemu ]]; then
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
fi
if [[ ${PLATFORM:-qemu} == qemu && $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 "$LAB_BRIDGE" >/dev/null 2>&1; then
_c_ok "bridge ${LAB_BRIDGE} exists"
else
if [[ ${PLATFORM:-qemu} == proxmox ]]; then
_c_warn "bridge ${LAB_BRIDGE} missing: create it in the Proxmox GUI"
else
_c_warn "bridge ${LAB_BRIDGE} missing: run sudo ./scripts/setup-network.sh"
fi
fi
if [[ ${PLATFORM:-qemu} == qemu ]]; then
if [[ -r /etc/qemu/bridge.conf ]] && grep -qx "allow ${LAB_BRIDGE}" /etc/qemu/bridge.conf; then
_c_ok "qemu-bridge-helper allows ${LAB_BRIDGE}"
else
_c_warn "/etc/qemu/bridge.conf does not allow ${LAB_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
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|proxmox) backend_check ;;
*) _c_fail "unknown backend: ${BACKEND}" ;;
esac
}