#!/bin/bash # Football VM Control Script (libvirt/virsh) # Manages QEMU VM for testing Football ISO set -euo pipefail BUILD_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" ISO_FILE="$BUILD_DIR/output/football-installer.iso" DISK_FILE="$BUILD_DIR/output/football-vm-disk.qcow2" VM_NAME="football-test" XML_FILE="$BUILD_DIR/output/${VM_NAME}.xml" # Create directories mkdir -p "$(dirname "$ISO_FILE")" mkdir -p "$(dirname "$DISK_FILE")" mkdir -p "$(dirname "$XML_FILE")" case "$1" in define) echo "Defining VM in libvirt..." # Create disk if it doesn't exist if [ ! -f "$DISK_FILE" ]; then echo "Creating VM disk (8GB)..." qemu-img create -f qcow2 "$DISK_FILE" 8G fi # Create libvirt XML cat > "$XML_FILE" < $VM_NAME 2097152 2097152 2 hvm destroy restart destroy /usr/bin/qemu-system-x86_64
EOF # Define VM in libvirt virsh define "$XML_FILE" echo "VM defined in libvirt" echo "" echo "Manage with:" echo " virsh start $VM_NAME" echo " virsh stop $VM_NAME" echo " virt-manager (view in GUI)" ;; undefine) echo "Undefining VM from libvirt..." virsh shutdown "$VM_NAME" 2>/dev/null || true sleep 2 virsh undefine "$VM_NAME" && echo "VM undefined" ;; start) echo "Starting VM..." virsh start "$VM_NAME" echo "" virsh list echo "" echo "VM is running. View in:" echo " 1. virt-manager" echo " 2. vncviewer localhost:5900" ;; stop) echo "Stopping VM..." virsh shutdown "$VM_NAME" 2>/dev/null || true # Wait for VM to actually stop (up to 30 seconds) for _ in {1..30}; do if ! virsh list --name | grep -q "^${VM_NAME}$"; then echo "VM stopped" break fi sleep 1 done # If still running, force destroy if virsh list --name | grep -q "^${VM_NAME}$"; then virsh destroy "$VM_NAME" && echo "VM destroyed" fi ;; reboot) echo "Rebooting VM..." virsh reboot "$VM_NAME" ;; status) echo "Checking VM status..." virsh list --all | grep -E "Name|$VM_NAME" ;; console) echo "Opening VNC console..." if command -v vncviewer &> /dev/null; then vncviewer localhost:5900 elif command -v remote-viewer &> /dev/null; then remote-viewer vnc://localhost:5900 else echo "Error: No VNC viewer found" echo "Install: sudo apt-get install tigervnc-viewer virt-viewer" fi ;; delete) echo "Deleting VM, disk, and ISO..." # Stop VM virsh destroy "$VM_NAME" 2>/dev/null || true virsh undefine "$VM_NAME" 2>/dev/null || true # Delete files rm -f "$DISK_FILE" rm -f "$ISO_FILE" rm -f "$XML_FILE" echo "VM, disk, and ISO deleted" ;; *) echo "Football VM Control Script (libvirt)" echo "" echo "Usage: $0 {define|undefine|start|stop|reboot|status|console|delete}" echo "" echo "Commands:" echo " define - Create VM definition in libvirt" echo " undefine - Remove VM from libvirt" echo " start - Start VM" echo " stop - Stop VM" echo " reboot - Reboot VM" echo " status - Check VM status" echo " console - Open VNC console viewer" echo " delete - Delete VM, disk, and ISO (CAUTION!)" echo "" echo "VM Details:" echo " Name: $VM_NAME" echo " Disk: $DISK_FILE" echo " ISO: $ISO_FILE" echo " XML: $XML_FILE" ;; esac