#!/bin/bash # Bootloader management module for bootc image creation # This module handles GRUB installation and configuration # Common functions are sourced by the main script # Install GRUB bootloader install_grub_bootloader() { log_info "Installing bootloader..." # Check if kernel exists if [ -f "$WORK_DIR/mnt/boot/vmlinuz-6.12.41+deb13-amd64" ]; then log_info "Kernel found in /boot location" KERNEL_PATH="boot/vmlinuz-6.12.41+deb13-amd64" INITRD_PATH="boot/initrd.img-6.12.41+deb13-amd64" elif [ -f "$WORK_DIR/mnt/usr/lib/ostree-boot/vmlinuz-6.12.41+deb13-amd64" ]; then log_info "Kernel found in OSTree location" KERNEL_PATH="usr/lib/ostree-boot/vmlinuz-6.12.41+deb13-amd64" INITRD_PATH="usr/lib/ostree-boot/initrd.img-6.12.41+deb13-amd64" else log_error "Kernel not found in expected locations" log_info "Searching for kernel files..." find "$WORK_DIR/mnt" -name "*vmlinuz*" -o -name "*kernel*" 2>/dev/null | head -10 exit 1 fi # Install GRUB bootloader log_info "Installing GRUB bootloader..." # Create GRUB directory sudo mkdir -p "$WORK_DIR/mnt/boot/grub" # Temporarily unmount and remount for safer GRUB installation log_info "Temporarily unmounting filesystem for GRUB installation..." sudo umount "$WORK_DIR/mnt" # Get the loop device from the disk module if [ -z "$loop_dev" ]; then log_error "Loop device not set. Cannot install bootloader." return 1 fi # Ensure the loop device path is correct local device_path="${loop_dev}" if [[ "$device_path" == /dev/* ]]; then device_path="${device_path#/dev/}" fi log_info "Loop device: $loop_dev, device path: $device_path" log_info "Mounting partition /dev/${device_path}p2" sudo mount "/dev/${device_path}p2" "$WORK_DIR/mnt" # Install GRUB to the disk log_info "Installing GRUB to /dev/$device_path..." if sudo grub-install --target=i386-pc --boot-directory="$WORK_DIR/mnt/boot" "/dev/$device_path"; then log_info "GRUB installed successfully" else log_warn "GRUB installation failed, trying alternative method..." # Try installing to the partition instead if sudo grub-install --target=i386-pc --boot-directory="$WORK_DIR/mnt/boot" "/dev/${device_path}p2"; then log_info "GRUB installed to partition successfully" else log_error "GRUB installation failed completely" return 1 fi fi # Create GRUB configuration log_info "Creating GRUB configuration..." sudo tee "$WORK_DIR/mnt/boot/grub/grub.cfg" > /dev/null << EOF set timeout=5 set default=0 menuentry "Debian Bootc" { linux /$KERNEL_PATH root=/dev/sda2 rw console=tty0 console=ttyS0,115200n8 initrd /$INITRD_PATH } EOF log_info "Bootloader installation completed" } # Install systemd-boot (for UEFI systems) install_systemd_boot() { log_info "Installing systemd-boot..." # Create bootloader entries directory sudo mkdir -p "$WORK_DIR/mnt/boot/loader/entries" # Create bootloader entry sudo tee "$WORK_DIR/mnt/boot/loader/entries/debian-bootc.conf" > /dev/null << EOF title Debian Bootc version 1 linux /ostree/debian-bootc/vmlinuz-6.12.41+deb13-amd64 initrd /ostree/debian-bootc/initramfs-6.12.41+deb13-amd64.img options ostree=/ostree/boot.0/debian-bootc/0 rw console=tty0 console=ttyS0,115200n8 EOF # Create loader configuration sudo tee "$WORK_DIR/mnt/boot/loader/loader.conf" > /dev/null << EOF default debian-bootc timeout 5 editor no EOF log_info "systemd-boot installation completed" } # Verify bootloader installation verify_bootloader() { log_info "Verifying bootloader installation..." # Check GRUB configuration if [ -f "$WORK_DIR/mnt/boot/grub/grub.cfg" ]; then log_info "GRUB configuration found" else log_error "GRUB configuration not found" return 1 fi # Check GRUB core image if [ -f "$WORK_DIR/mnt/boot/grub/i386-pc/core.img" ]; then log_info "GRUB core image found" else log_warn "GRUB core image not found - bootloader may not work" fi # Check kernel and initrd if [ -f "$WORK_DIR/mnt/boot/vmlinuz-6.12.41+deb13-amd64" ]; then log_info "Kernel found: vmlinuz-6.12.41+deb13-amd64" else log_error "Kernel not found" return 1 fi if [ -f "$WORK_DIR/mnt/boot/initrd.img-6.12.41+deb13-amd64" ]; then log_info "Initrd found: initrd.img-6.12.41+deb13-amd64" else log_error "Initrd not found" return 1 fi log_info "Bootloader verification completed successfully" return 0 } # Show bootloader information show_bootloader_info() { log_info "Bootloader information:" echo "GRUB configuration:" if [ -f "$WORK_DIR/mnt/boot/grub/grub.cfg" ]; then cat "$WORK_DIR/mnt/boot/grub/grub.cfg" else echo "GRUB configuration not found" fi echo "" echo "Boot directory contents:" ls -la "$WORK_DIR/mnt/boot/" echo "" echo "GRUB directory contents:" if [ -d "$WORK_DIR/mnt/boot/grub" ]; then ls -la "$WORK_DIR/mnt/boot/grub/" else echo "GRUB directory not found" fi } # Customize GRUB configuration customize_grub_config() { local custom_config="$1" if [ -n "$custom_config" ] && [ -f "$custom_config" ]; then log_info "Applying custom GRUB configuration..." sudo cp "$custom_config" "$WORK_DIR/mnt/boot/grub/grub.cfg" log_info "Custom GRUB configuration applied" else log_warn "No custom GRUB configuration provided or file not found" fi } # Set GRUB timeout set_grub_timeout() { local timeout="${1:-5}" log_info "Setting GRUB timeout to ${timeout} seconds..." # Update the existing grub.cfg with new timeout sudo sed -i "s/set timeout=.*/set timeout=$timeout/" "$WORK_DIR/mnt/boot/grub/grub.cfg" log_info "GRUB timeout set to ${timeout} seconds" } # Add custom GRUB menu entries add_grub_menu_entry() { local title="$1" local kernel_path="$2" local initrd_path="$3" local root_device="${4:-/dev/sda2}" local extra_params="${5:-}" log_info "Adding GRUB menu entry: $title" # Append to grub.cfg sudo tee -a "$WORK_DIR/mnt/boot/grub/grub.cfg" > /dev/null << EOF menuentry "$title" { linux /$kernel_path root=$root_device rw console=tty0 console=ttyS0,115200n8 $extra_params initrd /$initrd_path } EOF log_info "GRUB menu entry added: $title" } # Print module usage print_module_usage() { echo "Usage: source modules/bootloader.sh" echo "This module handles bootloader installation and configuration." echo "" echo "Available functions:" echo " install_bootloader - Install GRUB bootloader" echo " install_systemd_boot - Install systemd-boot (UEFI)" echo " verify_bootloader - Verify bootloader installation" echo " show_bootloader_info - Display bootloader information" echo " customize_grub_config - Apply custom GRUB configuration" echo " set_grub_timeout - Set GRUB timeout value" echo " add_grub_menu_entry - Add custom GRUB menu entries" echo "" echo "Required variables:" echo " WORK_DIR - Working directory for operations" echo " loop_dev - Loop device for disk operations" }