#!/bin/bash # Launch VM for interactive debugging # Author: Craig Jennings # License: GNU GPLv3 # # Launches a QEMU VM with a graphical display for interactive debugging. # Uses a copy-on-write overlay when using --base to protect the base image. 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 via copy-on-write overlay (safe)" echo " disk-image.qcow2 Use existing test disk image directly" echo " (no args) Same as --base" exit 1 fi # Configuration TIMESTAMP=$(date +'%Y%m%d-%H%M%S') VM_IMAGES_DIR="$PROJECT_ROOT/vm-images" BASE_DISK="$VM_IMAGES_DIR/archsetup-base.qcow2" ROOT_PASSWORD="archsetup" OVERLAY_DISK="" # Initialize logging and VM paths LOGFILE="/tmp/debug-vm-$TIMESTAMP.log" init_logging "$LOGFILE" init_vm_paths "$VM_IMAGES_DIR" 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 # Create a copy-on-write overlay (instant, protects base image) OVERLAY_DISK="$VM_IMAGES_DIR/debug-overlay-$TIMESTAMP.qcow2" step "Creating copy-on-write overlay" if qemu-img create -f qcow2 -b "$BASE_DISK" -F qcow2 "$OVERLAY_DISK" >> "$LOGFILE" 2>&1; then success "Overlay created: $(basename "$OVERLAY_DISK")" else fatal "Failed to create overlay disk" fi VM_DISK="$OVERLAY_DISK" else info "Using existing disk: $VM_DISK" fi # If snapshot exists, restore it first (only for non-overlay disks) if [ -z "$OVERLAY_DISK" ] && snapshot_exists "$VM_DISK" "clean-install"; then step "Restoring clean-install snapshot" restore_snapshot "$VM_DISK" "clean-install" fi # Launch QEMU with graphical display step "Starting QEMU with graphical display" start_qemu "$VM_DISK" "disk" "" "gtk" || fatal "Failed to start QEMU" # Display connection information section "Debug VM Ready" info "" info " Disk: $(basename "$VM_DISK")" info " SSH: sshpass -p '$ROOT_PASSWORD' ssh -p $SSH_PORT root@localhost" info " Root password: $ROOT_PASSWORD" info "" info " The GTK window should be open. Close it to stop the VM." info " Log file: $LOGFILE" info "" # Wait for QEMU to exit (user closes GTK window) step "Waiting for VM to exit..." while vm_is_running; do sleep 2 done success "VM has stopped" # Clean up overlay disk if [ -n "$OVERLAY_DISK" ] && [ -f "$OVERLAY_DISK" ]; then step "Removing overlay disk" rm -f "$OVERLAY_DISK" success "Overlay cleaned up" fi _cleanup_qemu_files info "Debug session complete"