aboutsummaryrefslogtreecommitdiff
path: root/scripts/test-vm.sh
blob: 581fa6cb32bcd736ffbce05d0f2254c1004c3e3e (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/bin/bash
# test-vm.sh - Test the archzfs ISO in a QEMU virtual machine
#
# Usage:
#   ./test-vm.sh              # Create new VM and boot ISO
#   ./test-vm.sh --boot-disk  # Boot from existing virtual disk (after install)
#   ./test-vm.sh --clean      # Remove VM disk and start fresh

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_DIR="$(dirname "$SCRIPT_DIR")"

# VM Configuration
VM_NAME="archzfs-test"
VM_DIR="$PROJECT_DIR/vm"
VM_DISK="$VM_DIR/$VM_NAME.qcow2"
VM_DISK_SIZE="50G"
VM_RAM="4096"
VM_CPUS="4"

# UEFI firmware (adjust path for your system)
OVMF_CODE="/usr/share/edk2/x64/OVMF_CODE.4m.fd"
OVMF_VARS_ORIG="/usr/share/edk2/x64/OVMF_VARS.4m.fd"
OVMF_VARS="$VM_DIR/OVMF_VARS.fd"

# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
CYAN='\033[0;36m'
NC='\033[0m'

info()  { echo -e "${GREEN}[INFO]${NC} $1"; }
warn()  { echo -e "${YELLOW}[WARN]${NC} $1"; }
error() { echo -e "${RED}[ERROR]${NC} $1"; exit 1; }

# Find the ISO
find_iso() {
    ISO_FILE=$(ls -t "$PROJECT_DIR/out/"*.iso 2>/dev/null | head -1)
    if [[ -z "$ISO_FILE" ]]; then
        error "No ISO found in $PROJECT_DIR/out/"
        echo "Build the ISO first with: sudo ./build.sh"
        exit 1
    fi
    info "Using ISO: $ISO_FILE"
}

# Check dependencies
check_deps() {
    local missing=()

    command -v qemu-system-x86_64 >/dev/null 2>&1 || missing+=("qemu")

    if [[ ! -f "$OVMF_CODE" ]]; then
        missing+=("edk2-ovmf")
    fi

    if [[ ${#missing[@]} -gt 0 ]]; then
        error "Missing dependencies: ${missing[*]}"
        echo "Install with: sudo pacman -S ${missing[*]}"
        exit 1
    fi
}

# Create VM directory and disk
setup_vm() {
    mkdir -p "$VM_DIR"

    if [[ ! -f "$VM_DISK" ]]; then
        info "Creating virtual disk: $VM_DISK ($VM_DISK_SIZE)"
        qemu-img create -f qcow2 "$VM_DISK" "$VM_DISK_SIZE"
    else
        info "Using existing disk: $VM_DISK"
    fi

    # Copy OVMF vars if needed
    if [[ ! -f "$OVMF_VARS" ]]; then
        info "Setting up UEFI variables"
        cp "$OVMF_VARS_ORIG" "$OVMF_VARS"
    fi
}

# Clean up VM files
clean_vm() {
    warn "Removing VM files..."
    rm -f "$VM_DISK"
    rm -f "$OVMF_VARS"
    info "VM files removed. Ready for fresh install."
}

# Boot VM from ISO
boot_iso() {
    find_iso
    setup_vm

    info "Starting VM with ISO..."
    echo ""
    echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
    echo "  VM: $VM_NAME"
    echo "  RAM: ${VM_RAM}MB | CPUs: $VM_CPUS"
    echo "  Disk: $VM_DISK_SIZE"
    echo "  ISO: $(basename "$ISO_FILE")"
    echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
    echo ""
    echo "Tips:"
    echo "  - Press Ctrl+Alt+G to release mouse grab"
    echo "  - Press Ctrl+Alt+F to toggle fullscreen"
    echo "  - Run 'install-archzfs' to start installation"
    echo ""

    qemu-system-x86_64 \
        -name "$VM_NAME" \
        -machine q35,accel=kvm \
        -cpu host \
        -smp "$VM_CPUS" \
        -m "$VM_RAM" \
        -drive if=pflash,format=raw,readonly=on,file="$OVMF_CODE" \
        -drive if=pflash,format=raw,file="$OVMF_VARS" \
        -drive file="$VM_DISK",format=qcow2,if=virtio \
        -cdrom "$ISO_FILE" \
        -boot d \
        -netdev user,id=net0,hostfwd=tcp::2222-:22 \
        -device virtio-net-pci,netdev=net0 \
        -device virtio-vga-gl \
        -display gtk,gl=on \
        -audiodev pipewire,id=audio0 \
        -device ich9-intel-hda \
        -device hda-duplex,audiodev=audio0 \
        -usb \
        -device usb-tablet
}

# Boot VM from disk (after installation)
boot_disk() {
    setup_vm

    if [[ ! -f "$VM_DISK" ]]; then
        error "No disk found. Run without --boot-disk first to install."
    fi

    info "Booting from installed disk..."
    echo ""
    echo "SSH access: ssh -p 2222 localhost"
    echo ""

    qemu-system-x86_64 \
        -name "$VM_NAME" \
        -machine q35,accel=kvm \
        -cpu host \
        -smp "$VM_CPUS" \
        -m "$VM_RAM" \
        -drive if=pflash,format=raw,readonly=on,file="$OVMF_CODE" \
        -drive if=pflash,format=raw,file="$OVMF_VARS" \
        -drive file="$VM_DISK",format=qcow2,if=virtio \
        -boot c \
        -netdev user,id=net0,hostfwd=tcp::2222-:22 \
        -device virtio-net-pci,netdev=net0 \
        -device virtio-vga-gl \
        -display gtk,gl=on \
        -audiodev pipewire,id=audio0 \
        -device ich9-intel-hda \
        -device hda-duplex,audiodev=audio0 \
        -usb \
        -device usb-tablet
}

# Show help
show_help() {
    echo "Usage: $0 [OPTIONS]"
    echo ""
    echo "Options:"
    echo "  (none)        Create VM and boot from ISO for installation"
    echo "  --boot-disk   Boot from existing virtual disk (after install)"
    echo "  --clean       Remove VM disk and start fresh"
    echo "  --help        Show this help message"
    echo ""
    echo "VM Configuration (edit this script to change):"
    echo "  Disk size: $VM_DISK_SIZE"
    echo "  RAM: ${VM_RAM}MB"
    echo "  CPUs: $VM_CPUS"
    echo ""
    echo "SSH into running VM:"
    echo "  ssh -p 2222 localhost"
}

# Main
check_deps

case "${1:-}" in
    --boot-disk)
        boot_disk
        ;;
    --clean)
        clean_vm
        ;;
    --help|-h)
        show_help
        ;;
    "")
        boot_iso
        ;;
    *)
        error "Unknown option: $1"
        show_help
        exit 1
        ;;
esac