summaryrefslogtreecommitdiff
path: root/scripts/testing/debug-vm.sh
blob: a442850bdbcc5cf9e7fb47b1e6ee3331ba81cc9f (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/bin/bash
# Launch VM for interactive debugging
# Author: Craig Jennings <craigmartinjennings@gmail.com>
# 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