# GRUB and EFI Repair Script A comprehensive command-line tool designed to repair GRUB bootloader and EFI boot entries from a live ISO environment. This script is specifically crafted for system administrators and users who need to recover a system that won't boot due to corrupted or missing boot configuration. ## Features - **Live ISO Detection**: Automatically detects if running from a live ISO environment - **System Detection**: Identifies available disks, partitions, and bootable systems - **Safe Mounting**: Properly mounts target systems with all necessary bind mounts - **GRUB Installation**: Installs/reinstalls GRUB bootloader with proper EFI support - **Configuration Updates**: Updates GRUB configuration to detect all installed kernels - **EFI Repair**: Repairs EFI partition and boot entries - **Backup Creation**: Creates backups before making changes - **Comprehensive Logging**: Logs all operations for troubleshooting - **Safe Cleanup**: Properly unmounts and cleans up after operations ## Prerequisites - **Root Access**: Script must be run as root (use `sudo`) - **Live ISO Environment**: Designed to run from a live Linux ISO - **Target System**: An installed Linux system with GRUB bootloader - **Basic Knowledge**: Understanding of disk partitions and Linux boot process ## Installation 1. Download the script to your live ISO environment 2. Make it executable: ```bash chmod +x grub-repair.sh ``` 3. Run with sudo: ```bash sudo ./grub-repair.sh [OPTIONS] COMMAND ``` ## Usage ### Basic Commands ```bash # Show help sudo ./grub-repair.sh --help # Detect available systems sudo ./grub-repair.sh detect # Show current status sudo ./grub-repair.sh status # Clean up and unmount sudo ./grub-repair.sh clean ``` ### System Operations ```bash # Mount a system (e.g., /dev/sda1) sudo ./grub-repair.sh -d /dev/sda -p 1 mount # Unmount current system sudo ./grub-repair.sh unmount # Create backup of current configuration sudo ./grub-repair.sh backup ``` ### GRUB Operations ```bash # Install/reinstall GRUB sudo ./grub-repair.sh install-grub # Update GRUB configuration sudo ./grub-repair.sh update-grub # Check and repair EFI partition sudo ./grub-repair.sh -d /dev/sda check-efi ``` ### Complete Repair ```bash # Complete boot repair with backup sudo ./grub-repair.sh -d /dev/sda -p 1 -b fix-boot # Complete repair without backup sudo ./grub-repair.sh -d /dev/sda -p 1 fix-boot ``` ## Command Line Options | Option | Long Option | Description | |--------|-------------|-------------| | `-h` | `--help` | Show help message | | `-v` | `--verbose` | Enable verbose output | | `-d` | `--device` | Specify target device (e.g., /dev/sda) | | `-p` | `--partition` | Specify partition number (default: 1) | | `-m` | `--mount` | Specify mount point (default: /mnt) | | `-b` | `--backup` | Create backup before operations | | `-f` | `--force` | Force operations without confirmation | | `-l` | `--log` | Specify log file path | ## Commands Reference ### `detect` Scans and displays all available disks, partitions, and bootable systems. ### `mount` Mounts the target system with proper bind mounts for chroot operations. ### `unmount` Safely unmounts all mounted filesystems in the correct order. ### `backup` Creates a timestamped backup of current GRUB configuration and EFI contents. ### `install-grub` Installs or reinstalls GRUB bootloader to the target system. ### `update-grub` Updates GRUB configuration to detect all installed kernels and OS entries. ### `check-efi` Checks and repairs EFI partition and boot entries. ### `fix-boot` Performs a complete boot repair sequence (mount + install + update + repair). ### `status` Shows current status of mounted systems and available configurations. ### `clean` Cleans up temporary files and unmounts all systems. ## Typical Workflow 1. **Boot from Live ISO**: Start your system from a live Linux ISO 2. **Detect Systems**: Identify your target system: ```bash sudo ./grub-repair.sh detect ``` 3. **Complete Repair**: Perform the repair in one command: ```bash sudo ./grub-repair.sh -d /dev/sda -p 1 -b fix-boot ``` 4. **Reboot**: Restart your system and test the boot ## Troubleshooting ### Common Issues - **Permission Denied**: Ensure you're running as root with `sudo` - **Device Not Found**: Verify device names with `lsblk` or `fdisk -l` - **Mount Failures**: Check if partitions are already mounted elsewhere - **GRUB Installation Fails**: Verify EFI partition is properly mounted ### Verbose Mode Enable detailed output for troubleshooting: ```bash sudo ./grub-repair.sh -v -d /dev/sda -p 1 fix-boot ``` ### Log Files The script creates detailed logs in `/tmp/grub-repair-YYYYMMDD-HHMMSS.log` ## Safety Features - **Automatic Backups**: Creates backups before making changes - **Safe Mounting**: Uses proper bind mounts and handles dependencies - **Cleanup Traps**: Automatically cleans up on script exit - **Error Handling**: Comprehensive error checking and reporting - **Live ISO Detection**: Warns if not running from live environment ## Examples ### Scenario 1: Simple GRUB Reinstall ```bash # Mount system sudo ./grub-repair.sh -d /dev/sda -p 1 mount # Install GRUB sudo ./grub-repair.sh install-grub # Update configuration sudo ./grub-repair.sh update-grub # Unmount sudo ./grub-repair.sh unmount ``` ### Scenario 2: Complete EFI Repair ```bash # Complete repair with backup sudo ./grub-repair.sh -d /dev/sda -p 1 -b fix-boot ``` ### Scenario 3: Multiple System Recovery ```bash # Check available systems sudo ./grub-repair.sh detect # Repair first system sudo ./grub-repair.sh -d /dev/sda -p 1 fix-boot # Clean up sudo ./grub-repair.sh clean # Repair second system sudo ./grub-repair.sh -d /dev/sdb -p 1 fix-boot ``` ## Contributing This script is designed to be robust and user-friendly. If you encounter issues or have suggestions for improvements, please: 1. Test thoroughly in your environment 2. Document the issue or enhancement 3. Provide detailed error messages and system information ## License This script is provided as-is for educational and recovery purposes. Use at your own risk and always test in a safe environment first. ## Disclaimer This tool modifies critical boot configuration files. Always create backups and test in a safe environment before using on production systems. The authors are not responsible for data loss or system damage resulting from the use of this script.