#!/usr/bin/env bash
# scripts/osd-blank-check.sh
#
# Read-only OSD secondary-disk blank check on the libvirt host (needs sudo for qemu-img).
# Confirms each host VM's OSD disk is wiped blank before redeploy so Ceph re-bootstraps clean.
# Override the image dir with IMGDIR=... if needed.
#
# Pinned host list comes from scripts/lib-hosts.sh (hostname-keyed).
# Exit codes: 0 all blank | 1 a disk looks non-blank | 2 a disk image missing

set -euo pipefail
shopt -s inherit_errexit 2>/dev/null || true
IFS=$'\n\t'

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck source=scripts/lib-net.sh
. "$SCRIPT_DIR/lib-net.sh"
# shellcheck source=scripts/lib-hosts.sh
. "$SCRIPT_DIR/lib-hosts.sh"

IMGDIR="${IMGDIR:-/var/lib/libvirt/images}"
RC=0
for h in "${HOSTS[@]}"; do
  img="$IMGDIR/${h}-1.qcow2"
  echo "== $h ($img) =="
  if [ ! -f "$img" ]; then echo "  FAIL: image not found"; RC=2; continue; fi
  info="$(sudo qemu-img info "$img")"
  echo "$info" | grep -E 'virtual size|disk size' | sed 's/^/  /'
  dsize="$(echo "$info" | awk -F': ' '/disk size/{print $2; exit}')"
  case "$dsize" in
    *KiB*|*bytes*) : ;;
    *) echo "  WARN: disk size '$dsize' looks non-blank (expected KiB)"; if [ "$RC" -eq 0 ]; then RC=1; fi ;;
  esac
done
echo
echo "expect: virtual 512 GiB, disk ~200 KiB (blank). RC=$RC"
exit "$RC"
