#!/usr/bin/env bash # Prepare the host: start libvirtd, verify KVM + nested virt, ensure the # libvirt working directory exists, and generate the automation SSH key. set -euo pipefail source "$(dirname "$0")/lab.env" sudo -v # prompt for the sudo password once, up front echo "==> Verifying KVM + nested virtualization" [[ -e /dev/kvm ]] || { echo "ERROR: /dev/kvm missing"; exit 1; } nested=$(cat /sys/module/kvm_intel/parameters/nested 2>/dev/null \ || cat /sys/module/kvm_amd/parameters/nested 2>/dev/null || echo N) [[ "$nested" == "Y" || "$nested" == "1" ]] || { echo "ERROR: nested virtualization is not enabled (got '$nested')"; exit 1; } echo " /dev/kvm present, nested=$nested" echo "==> Enabling libvirt (modular daemons — Fedora default)" # Fedora uses split per-driver daemons; the libvirt client connects to a socket # per driver (virtqemud for VMs, virtstoraged for pools, virtnetworkd for nets). # The deprecated monolithic libvirtd is stood down so the two don't fight over state. sudo systemctl disable --now libvirtd.service libvirtd.socket \ libvirtd-ro.socket libvirtd-admin.socket 2>/dev/null || true for d in virtqemud virtstoraged virtnetworkd virtnodedevd virtsecretd virtnwfilterd virtinterfaced; do sudo systemctl enable --now "${d}.socket" 2>/dev/null || true sudo systemctl enable --now "${d}-ro.socket" 2>/dev/null || true done sudo virsh -c qemu:///system version >/dev/null echo " libvirt (modular) active" echo "==> Ensuring default storage pool is running" if ! sudo virsh pool-info default >/dev/null 2>&1; then sudo virsh pool-define-as default dir --target /var/lib/libvirt/images fi sudo virsh pool-start default 2>/dev/null || true sudo virsh pool-autostart default 2>/dev/null || true # Pre-create a persistent pool for our image dir so virt-install reuses it # instead of auto-creating a transient one (parallel installs would otherwise # race to create the same-named pool and one would fail). echo "==> Ensuring 'pve-lab' storage pool for $LIB_DIR" if ! sudo virsh pool-info pve-lab >/dev/null 2>&1; then sudo virsh pool-define-as pve-lab dir --target "$LIB_DIR" fi sudo virsh pool-start pve-lab 2>/dev/null || true sudo virsh pool-autostart pve-lab 2>/dev/null || true echo "==> Creating libvirt working directory: $LIB_DIR" sudo install -d -m 0711 "$LIB_DIR" # Make sure new files land with the right SELinux label for qemu. command -v restorecon >/dev/null && sudo restorecon -R "$LIB_DIR" || true echo "==> Creating build directory: $BUILD_DIR" mkdir -p "$BUILD_DIR" echo "==> Generating automation SSH key: $SSH_KEY" mkdir -p "$(dirname "$SSH_KEY")" chmod 700 "$(dirname "$SSH_KEY")" if [[ ! -f "$SSH_KEY" ]]; then ssh-keygen -t ed25519 -N "" -C "pve-lab-automation" -f "$SSH_KEY" >/dev/null echo " created" else echo " already exists, reusing" fi echo echo "Prereqs OK. Next: ./01-network.sh"