summaryrefslogtreecommitdiff
path: root/scripts/testing/create-base-vm.sh
blob: 7979bd2e8f12f401e059d6c1a9240ceae7066481 (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/bin/bash
# Create base VM for archsetup testing - Automated via Archangel ISO
# Author: Craig Jennings <craigmartinjennings@gmail.com>
# License: GNU GPLv3
#
# This script boots an archangel ISO in QEMU, copies a config file into the
# live environment via SSH, and runs a fully unattended Arch Linux installation.
# The result is a base VM disk with a "clean-install" snapshot ready for testing.

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"

# Configuration
VM_IMAGES_DIR="$PROJECT_ROOT/vm-images"
CONFIG_FILE="$SCRIPT_DIR/archsetup-test.conf"
LIVE_ISO_PASSWORD="archzfs"
SNAPSHOT_NAME="clean-install"

# Initialize logging
mkdir -p "$PROJECT_ROOT/test-results"
LOGFILE="$PROJECT_ROOT/test-results/create-base-vm-$(date +'%Y%m%d-%H%M%S').log"
init_logging "$LOGFILE"
init_vm_paths "$VM_IMAGES_DIR"

section "Creating Base VM for ArchSetup Testing"

# ─── Prerequisites ────────────────────────────────────────────────────

step "Checking prerequisites"
check_prerequisites || fatal "Missing prerequisites"
success "Prerequisites satisfied"

# Verify config file exists
if [ ! -f "$CONFIG_FILE" ]; then
    fatal "Config file not found: $CONFIG_FILE"
fi

# Find archangel ISO in vm-images/
ISO_PATH=$(find "$VM_IMAGES_DIR" -maxdepth 1 -name "archzfs-*.iso" -type f 2>/dev/null | sort -V | tail -1)
if [ -z "$ISO_PATH" ]; then
    fatal "No archangel ISO found in $VM_IMAGES_DIR/"
    info "Copy an archzfs-*.iso file to: $VM_IMAGES_DIR/"
fi
info "Using ISO: $(basename "$ISO_PATH")"

# ─── Prepare Disk ─────────────────────────────────────────────────────

section "Preparing VM Disk"

# Remove old disk and OVMF vars if they exist
if [ -f "$DISK_PATH" ]; then
    warn "Removing existing disk: $DISK_PATH"
    rm -f "$DISK_PATH"
fi
rm -f "$OVMF_VARS"

# Create fresh disk
step "Creating ${VM_DISK_SIZE}G qcow2 disk"
if qemu-img create -f qcow2 "$DISK_PATH" "${VM_DISK_SIZE}G" >> "$LOGFILE" 2>&1; then
    success "Disk created: $DISK_PATH"
else
    fatal "Failed to create disk image"
fi

# ─── Phase 1: Install from ISO ───────────────────────────────────────

section "Phase 1: Archangel Installation"

start_timer "install"

# Boot from ISO
start_qemu "$DISK_PATH" "iso" "$ISO_PATH" "none" || fatal "Failed to start QEMU"

# Wait for live ISO SSH
wait_for_ssh "$LIVE_ISO_PASSWORD" 120 || fatal "Live ISO SSH not available"

# Copy config file into VM
copy_to_vm "$CONFIG_FILE" "/root/archsetup-test.conf" "$LIVE_ISO_PASSWORD" || \
    fatal "Failed to copy config to VM"

# Run archangel installer (synchronous - typically 5-10 minutes)
step "Running archangel installer (unattended)..."
info "This will partition, install, and configure the base system"

if vm_exec "$LIVE_ISO_PASSWORD" "archangel --config-file /root/archsetup-test.conf"; then
    success "Archangel installation completed"
else
    error "Archangel installation failed"
    step "Capturing serial log for debugging"
    info "Serial log: $SERIAL_LOG"
    fatal "Base VM installation failed - check logs"
fi

# Power off
stop_qemu
stop_timer "install"

# ─── Phase 2: Verify Installation ────────────────────────────────────

section "Phase 2: Verifying Installation"

start_timer "verify"

# Boot from installed disk
start_qemu "$DISK_PATH" "disk" "" "none" || fatal "Failed to boot installed system"

# Wait for SSH on installed system
wait_for_ssh "$ROOT_PASSWORD" 120 || fatal "Installed system SSH not available"

# Basic verification
step "Verifying base system"

KERNEL=$(vm_exec "$ROOT_PASSWORD" "uname -r" 2>/dev/null)
success "Kernel: $KERNEL"

if vm_exec "$ROOT_PASSWORD" "systemctl is-active sshd" &>/dev/null; then
    success "SSH service active"
else
    warn "SSH service not active"
fi

if vm_exec "$ROOT_PASSWORD" "systemctl is-active NetworkManager" &>/dev/null; then
    success "NetworkManager active"
else
    warn "NetworkManager not active"
fi

# Power off for snapshot
stop_qemu
stop_timer "verify"

# ─── Phase 3: Create Snapshot ────────────────────────────────────────

section "Phase 3: Creating Clean-Install Snapshot"

create_snapshot "$DISK_PATH" "$SNAPSHOT_NAME" || fatal "Failed to create snapshot"

# ─── Done ─────────────────────────────────────────────────────────────

section "Base VM Created Successfully"

info ""
info "  Disk:     $DISK_PATH"
info "  Snapshot: $SNAPSHOT_NAME"
info "  Config:   $(basename "$CONFIG_FILE")"
info "  Log:      $LOGFILE"
info ""
info "Next step: Run ./scripts/testing/run-test.sh"
info ""