#!/bin/bash # Launch VM for interactive debugging # Author: Craig Jennings # License: GNU GPLv3 set -e # Get script directory SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)" # Source utilities source "$SCRIPT_DIR/lib/logging.sh" source "$SCRIPT_DIR/lib/vm-utils.sh" # Parse arguments VM_DISK="" USE_BASE=false if [ $# -eq 0 ]; then USE_BASE=true elif [ "$1" = "--base" ]; then USE_BASE=true elif [ -f "$1" ]; then VM_DISK="$1" else echo "Usage: $0 [disk-image.qcow2 | --base]" echo "" echo "Options:" echo " --base Use base VM (read-only, safe for testing)" echo " disk-image.qcow2 Use existing test disk image" echo " (no args) Clone base VM for debugging" exit 1 fi # Configuration TIMESTAMP=$(date +'%Y%m%d-%H%M%S') DEBUG_VM_NAME="archsetup-debug-$TIMESTAMP" BASE_DISK="$PROJECT_ROOT/vm-images/archsetup-base.qcow2" ROOT_PASSWORD="archsetup" # Initialize logging LOGFILE="/tmp/debug-vm-$TIMESTAMP.log" init_logging "$LOGFILE" section "Launching Debug VM" # Determine which disk to use if $USE_BASE; then if [ ! -f "$BASE_DISK" ]; then fatal "Base disk not found: $BASE_DISK" fi VM_DISK="$BASE_DISK" info "Using base VM (read-only snapshot mode)" else if [ -z "$VM_DISK" ]; then # Clone base VM VM_DISK="$PROJECT_ROOT/vm-images/$DEBUG_VM_NAME.qcow2" step "Cloning base VM for debugging" clone_disk "$BASE_DISK" "$VM_DISK" || fatal "Failed to clone base VM" success "Debug disk created: $VM_DISK" else info "Using existing disk: $VM_DISK" fi fi # Create debug VM step "Creating debug VM: $DEBUG_VM_NAME" virt-install \ --connect qemu:///system \ --name "$DEBUG_VM_NAME" \ --memory 4096 \ --vcpus 2 \ --disk path="$VM_DISK",format=qcow2,bus=virtio \ --os-variant archlinux \ --network network=default,model=virtio \ --graphics vnc,listen=127.0.0.1 \ --console pty,target.type=serial \ --boot uefi \ --import \ --noautoconsole \ >> "$LOGFILE" 2>&1 success "Debug VM created" # Wait for boot step "Waiting for VM to boot..." sleep 20 # Get VM IP VM_IP=$(get_vm_ip "$DEBUG_VM_NAME" 2>/dev/null || true) # Display connection information section "Debug VM Ready" info "" info "VM Name: $DEBUG_VM_NAME" if [ -n "$VM_IP" ]; then info "IP Address: $VM_IP" fi info "Disk: $VM_DISK" info "" info "Connect via:" info " Console: virsh console $DEBUG_VM_NAME" if [ -n "$VM_IP" ]; then info " SSH: ssh root@$VM_IP" fi info " VNC: virt-viewer $DEBUG_VM_NAME" info "" info "Root password: $ROOT_PASSWORD" info "" info "When done debugging:" info " virsh destroy $DEBUG_VM_NAME" info " virsh undefine $DEBUG_VM_NAME" if [ ! "$VM_DISK" = "$BASE_DISK" ] && [ -z "$1" ]; then info " rm $VM_DISK" fi info "" info "Log file: $LOGFILE" info "" # Offer to connect to console read -p "Connect to console now? [Y/n] " -n 1 -r echo "" if [[ $REPLY =~ ^[Nn]$ ]]; then info "VM is running in background" info "Connect later with: virsh console $DEBUG_VM_NAME" else info "Connecting to console..." info "Press Ctrl+] to disconnect from console" sleep 2 virsh console "$DEBUG_VM_NAME" fi