summaryrefslogtreecommitdiff
path: root/scripts/testing/debug-vm.sh
blob: 5b2b197ef3ad7c739445974c630e22cf8b474359 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/bin/bash
# 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

# 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"