#!/usr/bin/env bash # ============================================================================= # tools/boot-image.sh — Create a bootable disk image from the ESP directory # # Produces a raw disk image (bootable via UEFI) for dd-ing to a USB stick # or attaching to QEMU as a hard disk. # # Usage: # ./tools/boot-image.sh [output.img] # # Default output: raisernics.img # ============================================================================= set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "$0")/.." && pwd)" cd "$SCRIPT_DIR" OUTPUT="${1:-raisernics.img}" ESP_DIR="${ESP_DIR:-esp}" MBR_SZ=1 # 1 MB reserved at start for GPT/pmbr SECTOR_SZ=512 # Rebuild ESP if stale make all if [ ! -d "$ESP_DIR" ]; then echo "ERROR: ESP directory '$ESP_DIR' not found. Run 'make all' first." exit 1 fi # Calculate required size: ESP contents + MBR gap + some slack ESP_SIZE_MB=$(du -sm "$ESP_DIR" 2>/dev/null | cut -f1 || echo 1) TOTAL_MB=$((ESP_SIZE_MB + MBR_SZ + 1)) # +1 MB slack echo "Creating ${TOTAL_MB} MB disk image: ${OUTPUT}" # Create empty image with MBR gap dd if=/dev/zero of="$OUTPUT" bs=1M count="$TOTAL_MB" status=none 2>/dev/null # Partition table: single GPT partition, type EFI System (C12A7328-F81F-11D2-BA4B-00A0C93EC93B) sgdisk --clear \ --new=1:${MBR_SZ}M:0 \ --typecode=1:C12A7328-F81F-11D2-BA4B-00A0C93EC93B \ --change-name=1:"raisernics" \ "$OUTPUT" >/dev/null # Find partition offset PART_START=$(sgdisk --info=1 "$OUTPUT" 2>/dev/null | grep "Partition start sector" | awk '{print $NF}') if [ -z "$PART_START" ]; then PART_START=$((MBR_SZ * 1024 * 1024 / SECTOR_SZ)) fi # Format as FAT32 mformat -i "$OUTPUT"@$((PART_START * SECTOR_SZ)) -h 32 -s 32 -t "$((TOTAL_MB * 2))" >/dev/null 2>&1 || true # Copy the ESP contents onto the partition mcopy -i "$OUTPUT"@$((PART_START * SECTOR_SZ)) -s "$ESP_DIR"/* ::/ 2>/dev/null echo " -> ${OUTPUT} ($(ls -lh "$OUTPUT" | awk '{print $5}'))" echo "" echo "To write to USB: sudo dd if=${OUTPUT} of=/dev/sdX bs=4M status=progress" echo "To test with QEMU: qemu-system-x86_64 -bios -drive file=${OUTPUT},format=raw"