|
| 1 | +#!/bin/bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 5 | +BUILD_DIR="${SCRIPT_DIR}/build" |
| 6 | +DEPLOY_DIR="${BUILD_DIR}/tmp/deploy/images/emulator" |
| 7 | +OE_INIT="${SCRIPT_DIR}/src/oe-core/oe-init-build-env" |
| 8 | + |
| 9 | +RED='\033[0;31m' |
| 10 | +GREEN='\033[0;32m' |
| 11 | +YELLOW='\033[1;33m' |
| 12 | +NC='\033[0m' |
| 13 | + |
| 14 | +info() { echo -e "${GREEN}[emulator]${NC} $*"; } |
| 15 | +warn() { echo -e "${YELLOW}[emulator]${NC} $*"; } |
| 16 | +error() { echo -e "${RED}[emulator]${NC} $*" >&2; } |
| 17 | + |
| 18 | +usage() { |
| 19 | + echo "Usage: $0 [--build|--launch|--help]" |
| 20 | + echo "" |
| 21 | + echo " (no args) Build image if not found, then launch emulator" |
| 22 | + echo " --build Force rebuild the image, then launch" |
| 23 | + echo " --launch Launch only (skip build, image must exist)" |
| 24 | + echo " --help Show this help" |
| 25 | + echo "" |
| 26 | + echo "SSH access: ssh -p 2222 root@127.0.0.1" |
| 27 | + exit 0 |
| 28 | +} |
| 29 | + |
| 30 | +find_image() { |
| 31 | + if [ ! -d "${DEPLOY_DIR}" ]; then |
| 32 | + return 1 |
| 33 | + fi |
| 34 | + ROOTFS=$(ls -t "${DEPLOY_DIR}"/asteroid-image-emulator.rootfs-*.ext4 2>/dev/null | head -1) |
| 35 | + KERNEL=$(ls -t "${DEPLOY_DIR}"/bzImage 2>/dev/null | head -1) |
| 36 | + if [ -z "${ROOTFS:-}" ] || [ -z "${KERNEL:-}" ]; then |
| 37 | + return 1 |
| 38 | + fi |
| 39 | + return 0 |
| 40 | +} |
| 41 | + |
| 42 | +build_image() { |
| 43 | + info "Building AsteroidOS emulator image..." |
| 44 | + info "This may take a while on first build." |
| 45 | + |
| 46 | + if [ ! -f "${OE_INIT}" ]; then |
| 47 | + error "OE init script not found at ${OE_INIT}" |
| 48 | + error "Run prepare-build.sh emulator first to fetch sources." |
| 49 | + exit 1 |
| 50 | + fi |
| 51 | + |
| 52 | + cd "${SCRIPT_DIR}" |
| 53 | + bash -c " |
| 54 | + source '${OE_INIT}' '${BUILD_DIR}' > /dev/null |
| 55 | + bitbake asteroid-image |
| 56 | + " |
| 57 | + |
| 58 | + if ! find_image; then |
| 59 | + error "Build completed but image artifacts not found in ${DEPLOY_DIR}" |
| 60 | + exit 1 |
| 61 | + fi |
| 62 | + |
| 63 | + info "Build complete!" |
| 64 | + info " Root FS: ${ROOTFS}" |
| 65 | + info " Kernel: ${KERNEL}" |
| 66 | +} |
| 67 | + |
| 68 | +find_qemu() { |
| 69 | + if command -v qemu-system-i386 &>/dev/null; then |
| 70 | + QEMU_BIN="qemu-system-i386" |
| 71 | + elif [ -x "${BUILD_DIR}/tmp/work/x86_64-linux/qemu-helper-native/1.0/recipe-sysroot-native/usr/bin/qemu-system-i386" ]; then |
| 72 | + QEMU_BIN="${BUILD_DIR}/tmp/work/x86_64-linux/qemu-helper-native/1.0/recipe-sysroot-native/usr/bin/qemu-system-i386" |
| 73 | + else |
| 74 | + error "qemu-system-i386 not found. Install QEMU:" |
| 75 | + error " sudo apt install qemu-system-x86 # Debian/Ubuntu" |
| 76 | + error " sudo pacman -S qemu-full # Arch" |
| 77 | + exit 1 |
| 78 | + fi |
| 79 | + |
| 80 | + DISPLAY_BACKENDS=$("${QEMU_BIN}" -display help 2>&1 | grep -oP '^\w+' || true) |
| 81 | + DISPLAY_OPT="" |
| 82 | + GPU_DEVICE="" |
| 83 | + |
| 84 | + if echo "${DISPLAY_BACKENDS}" | grep -q '^gtk$'; then |
| 85 | + DISPLAY_OPT="-display gtk,gl=on,show-cursor=on" |
| 86 | + GPU_DEVICE="-vga none -device virtio-vga-gl" |
| 87 | + info "Using GTK display with virgl 3D acceleration" |
| 88 | + elif echo "${DISPLAY_BACKENDS}" | grep -q '^sdl$'; then |
| 89 | + DISPLAY_OPT="-display sdl,gl=on" |
| 90 | + GPU_DEVICE="-vga none -device virtio-vga-gl" |
| 91 | + info "Using SDL display with virgl 3D acceleration" |
| 92 | + else |
| 93 | + warn "No GTK/SDL display backend found, trying fallback..." |
| 94 | + if "${QEMU_BIN}" -device help 2>&1 | grep -q 'virtio-vga-gl'; then |
| 95 | + GPU_DEVICE="-vga none -device virtio-vga-gl" |
| 96 | + elif "${QEMU_BIN}" -device help 2>&1 | grep -q 'virtio-vga'; then |
| 97 | + GPU_DEVICE="-vga none -device virtio-vga" |
| 98 | + else |
| 99 | + GPU_DEVICE="-vga none -device virtio-gpu-pci" |
| 100 | + fi |
| 101 | + for backend in gtk sdl spice-app; do |
| 102 | + if echo "${DISPLAY_BACKENDS}" | grep -q "^${backend}$"; then |
| 103 | + DISPLAY_OPT="-display ${backend}" |
| 104 | + break |
| 105 | + fi |
| 106 | + done |
| 107 | + if [ -z "${DISPLAY_OPT}" ]; then |
| 108 | + error "No graphical display backend available in ${QEMU_BIN}" |
| 109 | + error "Available backends: ${DISPLAY_BACKENDS}" |
| 110 | + error "Install QEMU with GTK or SDL support." |
| 111 | + exit 1 |
| 112 | + fi |
| 113 | + fi |
| 114 | + |
| 115 | + KVM_OPT="" |
| 116 | + if [ -w /dev/kvm ]; then |
| 117 | + KVM_OPT="-enable-kvm" |
| 118 | + info "KVM hardware acceleration enabled" |
| 119 | + else |
| 120 | + warn "KVM not available (no /dev/kvm or no permission). Emulation will be slower." |
| 121 | + warn " To enable: sudo usermod -aG kvm \$USER && newgrp kvm" |
| 122 | + fi |
| 123 | +} |
| 124 | + |
| 125 | +launch_emulator() { |
| 126 | + info "Launching AsteroidOS emulator..." |
| 127 | + info " Root FS: $(basename "${ROOTFS}")" |
| 128 | + info " Kernel: $(basename "${KERNEL}")" |
| 129 | + info "" |
| 130 | + info " SSH: ssh -p 2222 root@127.0.0.1" |
| 131 | + info " Telnet: telnet 127.0.0.1 2323" |
| 132 | + info "" |
| 133 | + info "Close the QEMU window or press Ctrl+C to stop." |
| 134 | + echo "" |
| 135 | + |
| 136 | + exec "${QEMU_BIN}" \ |
| 137 | + -device virtio-net-pci,netdev=net0,mac=52:54:00:12:35:02 \ |
| 138 | + -netdev user,id=net0,hostfwd=tcp:127.0.0.1:2222-:22,hostfwd=tcp:127.0.0.1:2323-:23 \ |
| 139 | + -object rng-random,filename=/dev/urandom,id=rng0 -device virtio-rng-pci,rng=rng0 \ |
| 140 | + -drive file="${ROOTFS}",if=virtio,format=raw,snapshot=on \ |
| 141 | + -usb -device usb-tablet -usb -device usb-kbd \ |
| 142 | + -cpu IvyBridge -machine q35,i8042=off ${KVM_OPT} -smp 4 -m 512 \ |
| 143 | + ${GPU_DEVICE} \ |
| 144 | + ${DISPLAY_OPT} \ |
| 145 | + -kernel "${KERNEL}" \ |
| 146 | + -append "root=/dev/vda rw mem=512M ip=dhcp oprofile.timer=1 tsc=reliable no_timer_check rcupdate.rcu_expedited=1 swiotlb=0" |
| 147 | +} |
| 148 | + |
| 149 | +FORCE_BUILD=false |
| 150 | +LAUNCH_ONLY=false |
| 151 | + |
| 152 | +case "${1:-}" in |
| 153 | + --build) FORCE_BUILD=true ;; |
| 154 | + --launch) LAUNCH_ONLY=true ;; |
| 155 | + --help|-h) usage ;; |
| 156 | + "") ;; |
| 157 | + *) error "Unknown option: $1"; usage ;; |
| 158 | +esac |
| 159 | + |
| 160 | +if [ "${LAUNCH_ONLY}" = true ]; then |
| 161 | + if ! find_image; then |
| 162 | + error "No emulator image found. Run without --launch to build first." |
| 163 | + exit 1 |
| 164 | + fi |
| 165 | +elif [ "${FORCE_BUILD}" = true ] || ! find_image; then |
| 166 | + if ! find_image; then |
| 167 | + info "No existing image found, building..." |
| 168 | + fi |
| 169 | + build_image |
| 170 | +else |
| 171 | + info "Using existing image (use --build to force rebuild)" |
| 172 | +fi |
| 173 | + |
| 174 | +find_qemu |
| 175 | +launch_emulator |
0 commit comments