#!/bin/bash # grub-install — Install GRUB on a device (Red Bear OS wrapper) # # Compatibility: Matches GNU GRUB grub-install CLI conventions. # Maps standard grub-install switches to Red Bear OS cookbook/ESP workflow. # # Usage: # grub-install [OPTIONS] INSTALL_DEVICE # grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=REDBEAR /dev/sda # grub-install --target=x86_64-efi --removable /dev/sda # # For disk images (Red Bear OS build workflow): # grub-install --target=x86_64-efi --disk-image=build/x86_64/harddrive.img # # Differences from Linux GRUB: # --disk-image=FILE Install into a disk image file instead of a block device # Modules are pre-selected for Red Bear OS (chainload to Redox bootloader) # No NVRAM/efibootmgr integration (Red Bear OS uses removable boot path) set -euo pipefail PROG="$(basename "$0")" # Defaults TARGET="" EFI_DIRECTORY="" BOOTLOADER_ID="REDBEAR" BOOT_DIRECTORY="" DISK_IMAGE="" MODULES="" REMOVABLE=0 VERBOSE=0 INSTALL_DEVICE="" usage() { cat <&2 echo "Try '$PROG --help' for more information." >&2 exit 1 ;; *) INSTALL_DEVICE="$1" shift ;; esac done [ "$VERBOSE" -eq 1 ] && echo "$PROG: target=$TARGET efi_directory=$EFI_DIRECTORY bootloader_id=$BOOTLOADER_ID removable=$REMOVABLE disk_image=$DISK_IMAGE device=$INSTALL_DEVICE" # Validate target if [ -n "$TARGET" ] && [ "$TARGET" != "x86_64-efi" ]; then echo "$PROG: unsupported target '$TARGET' (only x86_64-efi is supported on Red Bear OS)" >&2 exit 1 fi SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)" FAT_TOOL="$SCRIPT_DIR/fat_tool.py" # Mode 1: Disk image installation (Red Bear OS build workflow) if [ -n "$DISK_IMAGE" ]; then if [ ! -f "$DISK_IMAGE" ]; then echo "$PROG: disk image not found: $DISK_IMAGE" >&2 exit 1 fi if [ ! -f "$FAT_TOOL" ]; then echo "$PROG: fat_tool.py not found at $FAT_TOOL" >&2 exit 1 fi # Find GRUB EFI binary GRUB_EFI="" for f in "$REPO_ROOT/local/recipes/core/grub/target/x86_64-unknown-redox/stage/usr/lib/boot/grub.efi" \ "$REPO_ROOT/repo/x86_64-unknown-redox/grub/root/usr/lib/boot/grub.efi"; do if [ -f "$f" ]; then GRUB_EFI="$f" break fi done if [ -z "$GRUB_EFI" ]; then echo "$PROG: grub.efi not found. Build the GRUB recipe first: make r.grub" >&2 exit 1 fi [ "$VERBOSE" -eq 1 ] && echo "$PROG: using grub.efi: $GRUB_EFI" # Find Redox bootloader REDBEAR_EFI="" for f in "$REPO_ROOT/local/recipes/core/bootloader/target/x86_64-unknown-redox/stage/usr/lib/boot/bootloader.efi" \ "$REPO_ROOT/repo/x86_64-unknown-redox/bootloader/root/usr/lib/boot/bootloader.efi"; do if [ -f "$f" ]; then REDBEAR_EFI="$f" break fi done if [ -z "$REDBEAR_EFI" ]; then echo "$PROG: bootloader.efi not found. Build the bootloader recipe first." >&2 exit 1 fi [ "$VERBOSE" -eq 1 ] && echo "$PROG: using bootloader.efi: $REDBEAR_EFI" # Find grub.cfg GRUB_CFG="$REPO_ROOT/local/recipes/core/grub/grub.cfg" if [ ! -f "$GRUB_CFG" ]; then echo "$PROG: grub.cfg not found at $GRUB_CFG" >&2 exit 1 fi # ESP at LBA 2048 (standard Redox GPT layout) ESP_OFFSET=$((2048 * 512)) # Write GRUB to ESP if [ "$REMOVABLE" -eq 1 ]; then BOOT_PATH="EFI/BOOT" else BOOT_PATH="EFI/${BOOTLOADER_ID}" fi "$FAT_TOOL" mkdir "$DISK_IMAGE" "$ESP_OFFSET" "EFI" 2>/dev/null || true "$FAT_TOOL" mkdir "$DISK_IMAGE" "$ESP_OFFSET" "$BOOT_PATH" 2>/dev/null || true "$FAT_TOOL" mkdir "$DISK_IMAGE" "$ESP_OFFSET" "EFI/REDBEAR" 2>/dev/null || true "$FAT_TOOL" cp-in "$DISK_IMAGE" "$ESP_OFFSET" "$GRUB_EFI" "$BOOT_PATH/BOOTX64.EFI" [ "$VERBOSE" -eq 1 ] && echo "$PROG: installed GRUB to $BOOT_PATH/BOOTX64.EFI" "$FAT_TOOL" cp-in "$DISK_IMAGE" "$ESP_OFFSET" "$GRUB_CFG" "$BOOT_PATH/grub.cfg" [ "$VERBOSE" -eq 1 ] && echo "$PROG: installed grub.cfg to $BOOT_PATH/grub.cfg" "$FAT_TOOL" cp-in "$DISK_IMAGE" "$ESP_OFFSET" "$REDBEAR_EFI" "EFI/REDBEAR/redbear.efi" [ "$VERBOSE" -eq 1 ] && echo "$PROG: installed Redox bootloader to EFI/REDBEAR/redbear.efi" echo "Installation finished. No error reported." exit 0 fi # Mode 2: Full image build via Red Bear installer (INSTALL_DEVICE given) if [ -n "$INSTALL_DEVICE" ]; then echo "$PROG: block device installation is not supported by this wrapper." >&2 echo "$PROG: For Red Bear OS, use one of:" >&2 echo "$PROG: make all CONFIG_NAME=redbear-full-grub" >&2 echo "$PROG: redox_installer -c redbear-full-grub.toml $INSTALL_DEVICE" >&2 exit 1 fi # No device or disk image given echo "$PROG: no installation device specified." >&2 echo "Try '$PROG --help' for more information." >&2 exit 1