Files
football/scripts/qvm.sh
2026-01-21 08:33:09 -05:00

192 lines
5.6 KiB
Bash
Executable File

#!/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" <<EOF
<domain type='kvm'>
<name>$VM_NAME</name>
<metadata>
<libosinfo:libosinfo xmlns:libosinfo="http://libosinfo.org/qemu/libosinfo/1.0">
<libosinfo:os id="http://debian.org/debian/13"/>
</libosinfo:libosinfo>
</metadata>
<memory unit='KiB'>2097152</memory>
<currentMemory unit='KiB'>2097152</currentMemory>
<vcpu placement='static'>2</vcpu>
<os>
<type arch='x86_64' machine='pc'>hvm</type>
<boot dev='cdrom'/>
</os>
<features>
<acpi/>
<apic/>
</features>
<cpu mode='host-passthrough' check='none' migratable='on'/>
<clock offset='utc'/>
<on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
<on_crash>destroy</on_crash>
<devices>
<emulator>/usr/bin/qemu-system-x86_64</emulator>
<disk type='file' device='cdrom'>
<driver name='qemu' type='raw'/>
<source file='$ISO_FILE'/>
<target dev='sda' bus='sata'/>
<readonly/>
</disk>
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2'/>
<source file='$DISK_FILE'/>
<target dev='sdb' bus='sata'/>
</disk>
<controller type='usb' index='0' model='qemu-xhci' ports='15'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
</controller>
<interface type='user'>
<mac address='52:54:00:00:00:01'/>
<model type='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
</interface>
<graphics type='vnc' port='-1' autoport='yes' listen='127.0.0.1'>
<listen type='address' address='127.0.0.1'/>
</graphics>
<video>
<model type='cirrus' vram='16384' heads='1' primary='yes'/>
</video>
<memballoon model='virtio'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
</memballoon>
</devices>
</domain>
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