summaryrefslogtreecommitdiff
path: root/scripts/testing/debug-vm.sh
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/testing/debug-vm.sh')
-rwxr-xr-xscripts/testing/debug-vm.sh122
1 files changed, 49 insertions, 73 deletions
diff --git a/scripts/testing/debug-vm.sh b/scripts/testing/debug-vm.sh
index a442850..5b2b197 100755
--- a/scripts/testing/debug-vm.sh
+++ b/scripts/testing/debug-vm.sh
@@ -2,6 +2,9 @@
# Launch VM for interactive debugging
# Author: Craig Jennings <craigmartinjennings@gmail.com>
# 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
@@ -27,21 +30,23 @@ 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"
+ 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')
-DEBUG_VM_NAME="archsetup-debug-$TIMESTAMP"
-BASE_DISK="$PROJECT_ROOT/vm-images/archsetup-base.qcow2"
+VM_IMAGES_DIR="$PROJECT_ROOT/vm-images"
+BASE_DISK="$VM_IMAGES_DIR/archsetup-base.qcow2"
ROOT_PASSWORD="archsetup"
+OVERLAY_DISK=""
-# Initialize logging
+# Initialize logging and VM paths
LOGFILE="/tmp/debug-vm-$TIMESTAMP.log"
init_logging "$LOGFILE"
+init_vm_paths "$VM_IMAGES_DIR"
section "Launching Debug VM"
@@ -50,84 +55,55 @@ 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"
+
+ # 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
- info "Using existing disk: $VM_DISK"
+ fatal "Failed to create overlay disk"
fi
+ VM_DISK="$OVERLAY_DISK"
+else
+ info "Using existing disk: $VM_DISK"
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)
+# 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 "VM Name: $DEBUG_VM_NAME"
-if [ -n "$VM_IP" ]; then
- info "IP Address: $VM_IP"
-fi
-info "Disk: $VM_DISK"
+info " Disk: $(basename "$VM_DISK")"
+info " SSH: sshpass -p '$ROOT_PASSWORD' ssh -p $SSH_PORT root@localhost"
+info " Root password: $ROOT_PASSWORD"
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 " The GTK window should be open. Close it to stop the VM."
+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"
+# Wait for QEMU to exit (user closes GTK window)
+step "Waiting for VM to exit..."
+while vm_is_running; do
sleep 2
- virsh console "$DEBUG_VM_NAME"
+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"