This repository has no description
4.5 kB
189 lines
1#!/usr/bin/env bash
2set -euo pipefail
3set -x
4
5# Default values
6ISO_FILE=""
7MEMORY="2048"
8CPUS="2"
9ARCH=""
10NOGRAPHIC=false
11
12usage() {
13 cat <<EOF
14Usage: $0 [OPTIONS] ISO_FILE
15
16Boot an ISO file in a VM with automatic architecture detection.
17
18OPTIONS:
19 -m, --memory SIZE Memory in MB (default: 2048)
20 -c, --cpus COUNT Number of CPU cores (default: 2)
21 -a, --arch ARCH Force architecture (x86_64 or aarch64)
22 -n, --nographic Run in text mode without graphics
23 -h, --help Show this help message
24
25EXAMPLE:
26 $0 installer.iso
27 $0 -m 4096 -c 4 installer.iso
28 $0 -a x86_64 installer.iso
29EOF
30}
31
32# Parse arguments
33while [[ $# -gt 0 ]]; do
34 case $1 in
35 -m|--memory)
36 MEMORY="$2"
37 shift 2
38 ;;
39 -c|--cpus)
40 CPUS="$2"
41 shift 2
42 ;;
43 -a|--arch)
44 ARCH="$2"
45 shift 2
46 ;;
47 -n|--nographic)
48 NOGRAPHIC=true
49 shift
50 ;;
51 -h|--help)
52 usage
53 exit 0
54 ;;
55 -*)
56 echo "Unknown option: $1"
57 usage
58 exit 1
59 ;;
60 *)
61 if [ -z "$ISO_FILE" ]; then
62 ISO_FILE="$1"
63 else
64 echo "Multiple ISO files specified"
65 usage
66 exit 1
67 fi
68 shift
69 ;;
70 esac
71done
72
73# Check if ISO file is provided
74if [ -z "$ISO_FILE" ]; then
75 echo "Error: ISO file not specified"
76 usage
77 exit 1
78fi
79
80# Check if ISO file exists
81if [ ! -f "$ISO_FILE" ]; then
82 echo "Error: ISO file '$ISO_FILE' not found"
83 exit 1
84fi
85
86# Detect architecture if not specified
87if [ -z "$ARCH" ]; then
88 echo "Detecting ISO architecture..."
89 ISO_INFO=$(file "$ISO_FILE")
90 if echo "$ISO_INFO" | grep -q "aarch64"; then
91 ARCH="aarch64"
92 elif echo "$ISO_INFO" | grep -q "x86-64\|x86_64"; then
93 ARCH="x86_64"
94 else
95 # Try to extract more info from the ISO
96 ISO_LABEL=$(blkid -o value -s LABEL "$ISO_FILE" 2>/dev/null || echo "")
97 if echo "$ISO_LABEL" | grep -q "aarch64"; then
98 ARCH="aarch64"
99 elif echo "$ISO_LABEL" | grep -q "x86"; then
100 ARCH="x86_64"
101 else
102 echo "Could not detect architecture from ISO. Please specify with -a option."
103 echo "Supported architectures: x86_64, aarch64"
104 exit 1
105 fi
106 fi
107fi
108
109echo "Booting $ISO_FILE with $ARCH architecture..."
110echo "Memory: $MEMORY MB, CPUs: $CPUS"
111
112# Build QEMU command based on architecture
113if [ "$ARCH" = "x86_64" ]; then
114 echo "Starting x86_64 VM with EFI..."
115 # Get the x86_64-linux UEFI firmware
116 echo "Building x86_64-linux UEFI firmware..."
117 UEFI_STORE_PATH=$(nix build --no-link --print-out-paths --system x86_64-linux nixpkgs#OVMF.fd)
118 UEFI_FW="$UEFI_STORE_PATH/FV/OVMF.fd"
119 if [ ! -f "$UEFI_FW" ]; then
120 echo "Error: Could not find x86_64 UEFI firmware at $UEFI_FW"
121 exit 1
122 fi
123 echo "Using UEFI firmware: $UEFI_FW"
124 if [ "$NOGRAPHIC" = true ]; then
125 exec qemu-system-x86_64 \
126 -machine q35 \
127 -cpu qemu64 \
128 -m "$MEMORY" \
129 -smp "$CPUS" \
130 -accel tcg \
131 -bios "$UEFI_FW" \
132 -cdrom "$ISO_FILE" \
133 -boot d \
134 -nographic \
135 -serial mon:stdio
136 else
137 exec qemu-system-x86_64 \
138 -machine q35 \
139 -cpu qemu64 \
140 -m "$MEMORY" \
141 -smp "$CPUS" \
142 -accel tcg \
143 -bios "$UEFI_FW" \
144 -cdrom "$ISO_FILE" \
145 -boot d
146 fi
147elif [ "$ARCH" = "aarch64" ]; then
148 echo "Starting aarch64 VM..."
149 # Get the aarch64-linux UEFI firmware
150 echo "Building aarch64 UEFI firmware..."
151 # Use the pre-built firmware path we know works
152 UEFI_FW="/nix/store/2ciakp99mrcb3fx98j6ji0nxkxcyq5gy-OVMF-202505-fd/FV/QEMU_EFI.fd"
153 if [ ! -f "$UEFI_FW" ]; then
154 echo "Firmware not found in store, building..."
155 UEFI_STORE_PATH=$(nix build --no-link --print-out-paths nixpkgs#OVMF-fd.aarch64-linux)
156 UEFI_FW="$UEFI_STORE_PATH/FV/QEMU_EFI.fd"
157 fi
158 if [ ! -f "$UEFI_FW" ]; then
159 echo "Error: Could not find aarch64 UEFI firmware at $UEFI_FW"
160 exit 1
161 fi
162 echo "Using UEFI firmware: $UEFI_FW"
163 if [ "$NOGRAPHIC" = true ]; then
164 exec qemu-system-aarch64 \
165 -machine virt \
166 -cpu cortex-a57 \
167 -m "$MEMORY" \
168 -smp "$CPUS" \
169 -accel tcg \
170 -bios "$UEFI_FW" \
171 -cdrom "$ISO_FILE" \
172 -boot d \
173 -nographic
174 else
175 exec qemu-system-aarch64 \
176 -machine virt \
177 -cpu cortex-a57 \
178 -m "$MEMORY" \
179 -smp "$CPUS" \
180 -accel tcg \
181 -bios "$UEFI_FW" \
182 -cdrom "$ISO_FILE" \
183 -boot d
184 fi
185else
186 echo "Error: Unsupported architecture '$ARCH'"
187 echo "Supported architectures: x86_64, aarch64"
188 exit 1
189fi