This repository has no description
0

Configure Feed

Select the types of activity you want to include in your feed.

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