#!/usr/bin/env bash # # run-vm.sh - Launch a test VM with Volt # # This script provides sensible defaults for testing Volt. # It checks for required assets and provides helpful error messages. # # Usage: # ./scripts/run-vm.sh # Run with defaults # ./scripts/run-vm.sh --memory 256 # Custom memory # ./scripts/run-vm.sh --kernel # Custom kernel # ./scripts/run-vm.sh --rootfs # Custom rootfs set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_DIR="$(dirname "$SCRIPT_DIR")" # Default paths KERNEL="${KERNEL:-${PROJECT_DIR}/kernels/vmlinux}" ROOTFS="${ROOTFS:-${PROJECT_DIR}/images/alpine-rootfs.ext4}" # VM configuration defaults MEMORY="${MEMORY:-128}" # MB CPUS="${CPUS:-1}" VM_NAME="${VM_NAME:-volt-vmm-test}" API_SOCKET="${API_SOCKET:-/tmp/volt-vmm-${VM_NAME}.sock}" # Logging LOG_LEVEL="${LOG_LEVEL:-info}" # Colors RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' CYAN='\033[0;36m' NC='\033[0m' log() { echo -e "${GREEN}[+]${NC} $*"; } warn() { echo -e "${YELLOW}[!]${NC} $*"; } error() { echo -e "${RED}[✗]${NC} $*"; exit 1; } info() { echo -e "${CYAN}[i]${NC} $*"; } usage() { cat << EOF Usage: $0 [OPTIONS] Launch a test VM with Volt. Options: --kernel PATH Path to kernel (default: kernels/vmlinux) --rootfs PATH Path to rootfs image (default: images/alpine-rootfs.ext4) --memory MB Memory in MB (default: 128) --cpus N Number of vCPUs (default: 1) --name NAME VM name (default: volt-vmm-test) --debug Enable debug logging --dry-run Show command without executing --help Show this help Environment variables: KERNEL, ROOTFS, MEMORY, CPUS, VM_NAME, LOG_LEVEL Examples: $0 # Run with defaults $0 --memory 256 --cpus 2 # Custom resources $0 --debug # Verbose logging EOF exit 0 } # Parse arguments DRY_RUN=false while [[ $# -gt 0 ]]; do case $1 in --kernel) KERNEL="$2" shift 2 ;; --rootfs) ROOTFS="$2" shift 2 ;; --memory) MEMORY="$2" shift 2 ;; --cpus) CPUS="$2" shift 2 ;; --name) VM_NAME="$2" API_SOCKET="/tmp/volt-vmm-${VM_NAME}.sock" shift 2 ;; --debug) LOG_LEVEL="debug" shift ;; --dry-run) DRY_RUN=true shift ;; --help|-h) usage ;; *) error "Unknown option: $1 (use --help for usage)" ;; esac done check_kvm() { if [[ ! -e /dev/kvm ]]; then error "KVM not available (/dev/kvm not found) Make sure: 1. Your CPU supports virtualization (VT-x/AMD-V) 2. Virtualization is enabled in BIOS 3. KVM modules are loaded (modprobe kvm kvm_intel or kvm_amd)" fi if [[ ! -r /dev/kvm ]] || [[ ! -w /dev/kvm ]]; then error "Cannot access /dev/kvm Fix with: sudo usermod -aG kvm \$USER && newgrp kvm" fi log "KVM available" } check_assets() { # Check kernel if [[ ! -f "$KERNEL" ]]; then error "Kernel not found: $KERNEL Build it with: just build-kernel Or specify with: --kernel " fi log "Kernel: $KERNEL" # Check rootfs if [[ ! -f "$ROOTFS" ]]; then # Try squashfs if ext4 not found local alt_rootfs="${ROOTFS%.ext4}.squashfs" if [[ -f "$alt_rootfs" ]]; then ROOTFS="$alt_rootfs" else error "Rootfs not found: $ROOTFS Build it with: just build-rootfs Or specify with: --rootfs " fi fi log "Rootfs: $ROOTFS" } check_binary() { local binary="${PROJECT_DIR}/target/release/volt-vmm" if [[ ! -x "$binary" ]]; then binary="${PROJECT_DIR}/target/debug/volt-vmm" fi if [[ ! -x "$binary" ]]; then error "Volt binary not found Build it with: just build (or just release)" fi echo "$binary" } cleanup() { # Remove stale socket rm -f "$API_SOCKET" } run_vm() { local binary binary=$(check_binary) # Build command local cmd=( "$binary" --kernel "$KERNEL" --rootfs "$ROOTFS" --memory "$MEMORY" --cpus "$CPUS" --api-socket "$API_SOCKET" ) # Add kernel command line for console cmd+=(--cmdline "console=ttyS0 reboot=k panic=1 nomodules") echo "" info "VM Configuration:" echo " Name: $VM_NAME" echo " Memory: ${MEMORY}MB" echo " CPUs: $CPUS" echo " Kernel: $KERNEL" echo " Rootfs: $ROOTFS" echo " Socket: $API_SOCKET" echo "" if $DRY_RUN; then info "Dry run - would execute:" echo " RUST_LOG=$LOG_LEVEL ${cmd[*]}" return fi info "Starting VM (Ctrl+C to exit)..." echo "" # Cleanup on exit trap cleanup EXIT # Run! RUST_LOG="$LOG_LEVEL" exec "${cmd[@]}" } # Main main() { echo "" log "Volt Test VM Launcher" echo "" check_kvm check_assets run_vm } main