Files
pxe_boot_alma/tools/probe_capture_baseline.sh
andersenandClaude Fable 5 6d398b826a refactor: move tree under homelab/, add alma10 hardening and kickstart docs
- restructure boot/kickstarts/scripts/docs into homelab/
- docs: alma9->10 breaking changes, CIS hardening, kickstart changes
- add find_cron_entries.sh helper and codex running memory notes

Co-Authored-By: Claude Fable 5 <[email protected]>
2026-07-06 22:35:07 -04:00

72 lines
2.2 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
ROOT=/usr/local/valherun/pxe_boot_alma
VM=probe
DISK=/var/lib/libvirt/images/probe.qcow2
ARTIFACT_ROOT=/tmp/probe-stage-watch
TEMPLATE_DIR=/var/lib/libvirt/images/probe-stage
META="$ARTIFACT_ROOT/baseline-template.env"
CURRENT_IP_FILE="$ARTIFACT_ROOT/baseline-source-ip.txt"
SUDO="${SUDO:-sudo}"
LIBVIRT_URI="${LIBVIRT_URI:-qemu:///system}"
VIRSH=( "$SUDO" virsh --connect "$LIBVIRT_URI" )
template_label="${1:-baseline}"
wait_secs="${2:-120}"
safe_label="$(printf '%s' "$template_label" | tr -cs 'A-Za-z0-9._-' '-')"
TEMPLATE="$TEMPLATE_DIR/probe-${safe_label}-template.qcow2"
mkdir -p "$ARTIFACT_ROOT"
"$SUDO" mkdir -p "$TEMPLATE_DIR"
current_ip() {
local ip=""
ip=$("${VIRSH[@]}" domifaddr "$VM" --source arp 2>/dev/null | awk '/ipv4/ {sub(/\/.*$/, "", $4); print $4; exit}')
if [[ -z "$ip" ]]; then
ip=$(ip neigh show dev virbr1 2>/dev/null | awk '$0 ~ /52:54:00:aa:bb:01/ {print $1; exit}')
fi
echo "$ip"
}
state=$("${VIRSH[@]}" domstate "$VM" 2>/dev/null || echo missing)
ip="$(current_ip || true)"
if [[ -n "${ip:-}" ]]; then
printf '%s\n' "$ip" > "$CURRENT_IP_FILE"
fi
if [[ "$state" != "shut off" ]]; then
if [[ -n "${ip:-}" ]]; then
ssh -o BatchMode=yes -o StrictHostKeyChecking=no root@"$ip" 'systemctl poweroff' >/dev/null 2>&1 || true
fi
"${VIRSH[@]}" shutdown "$VM" >/dev/null 2>&1 || true
for _ in $(seq 1 "$wait_secs"); do
state="$("${VIRSH[@]}" domstate "$VM" 2>/dev/null || echo missing)"
[[ "$state" == "shut off" ]] && break
sleep 1
done
fi
state="$("${VIRSH[@]}" domstate "$VM" 2>/dev/null || echo missing)"
if [[ "$state" != "shut off" ]]; then
echo "baseline source VM did not shut off cleanly" >&2
exit 1
fi
tmp_template="${TEMPLATE}.tmp"
"$SUDO" rm -f "$tmp_template"
"$SUDO" qemu-img convert -O qcow2 "$DISK" "$tmp_template"
"$SUDO" mv -f "$tmp_template" "$TEMPLATE"
{
echo "BASELINE_TEMPLATE='$TEMPLATE'"
echo "BASELINE_LABEL='$template_label'"
echo "BASELINE_CREATED='$(date '+%F %T')'"
echo "BASELINE_SOURCE_DISK='$DISK'"
echo "BASELINE_SOURCE_IP='${ip:-}'"
echo "BASELINE_VM='$VM'"
echo "BASELINE_SIZE_BYTES='$("$SUDO" stat -c %s "$TEMPLATE")'"
} > "$META"
printf '%s\n' "$TEMPLATE"