From 85ed8ca6a2459da4ee094bd9492c5a5e06a13cde Mon Sep 17 00:00:00 2001 From: arul Date: Wed, 19 Nov 2025 08:22:55 +0530 Subject: [PATCH] A simple Grub Restore script to Restore Debian based system for UEFI based systems --- README.md | 189 ++++++++++++++++++++++++++++++++++++++++++++++++ restore-grub.sh | 34 +++++++++ 2 files changed, 223 insertions(+) create mode 100644 README.md create mode 100644 restore-grub.sh diff --git a/README.md b/README.md new file mode 100644 index 0000000..c519524 --- /dev/null +++ b/README.md @@ -0,0 +1,189 @@ + +# πŸ› οΈ GRUB EFI Repair Script + +If you run Windows on Dual Boot with Linux, you may have faced issues like your windows partition overwrites the linux boot loader forcing user to log in on windows. + +This script was created to solve that issue in UEFI based Linux systems. + +A simple Bash script to reinstall GRUB on UEFI-based Linux systems and set the correct boot order. + +--- + +## ⚠️ Requirements & Conditions + +Before running this script, ensure your system meets **all** of the following conditions. + +### **1. UEFI System Required** + +This script works **only on UEFI systems**. +Legacy BIOS systems are **not supported**. + +It uses: + +``` +grub-install --target=x86_64-efi +``` + +--- + +### **2. EFI Partition Must Be Mounted at `/boot/efi`** + +Your system must have an EFI System Partition (ESP) mounted at: + +``` +/boot/efi +``` + +The script expects this directory to exist: + +``` +/boot/efi/EFI +``` + +If your distribution uses a different mount point (such as `/efi` or `/boot`), adjust the script accordingly. + +--- + +### **3. GRUB Must Be Installed and Used** + +This script only works with **GRUB**. + +It does **NOT** support: + +* systemd-boot +* rEFInd +* LILO +* vendor-specific boot managers + +Running it on these setups will result in errors. + +--- + +### **4. `update-grub` Must Exist** + +The script uses: + +``` +update-grub +``` + +This command is available only on **Debian-based** systems: + +* Debian +* Ubuntu +* Linux Mint +* Kali +* MX Linux + +It will **not** work on: + +* Fedora / RHEL / CentOS / Rocky / AlmaLinux +* Arch Linux / Manjaro +* OpenSUSE + +Those use `grub2-mkconfig`. + +--- + +### **5. Bootloader ID Defaults to β€œDebian”** + +The GRUB installation uses: + +``` +--bootloader-id=Debian +``` + +Different distros use different EFI bootloader IDs: + +* Ubuntu β†’ `ubuntu` +* Fedora β†’ `fedora` +* Linux Mint β†’ `LinuxMint` +* Arch Linux β†’ `arch` + +Update this value if needed. + +--- + +### **6. Requires `efibootmgr` Support** + +The script modifies UEFI boot order using: + +``` +efibootmgr -o +``` + +This requires: + +* Firmware that allows BootOrder modification +* Root access + +Some cloud/VPS providers restrict this capability. + +--- + +### **7. Must Be Run as Root** + +You must run the script with `sudo` or as root because it executes privileged commands: + +* `grub-install` +* `update-grub` +* `efibootmgr` + +--- + +### **8. Multi-Boot Systems** + +If multiple OSes are installed (e.g., Windows + Linux), this script will set **Debian GRUB as the first boot entry**, overriding others. + +--- + +### **9. Risk Warning** + +Changing GRUB/EFI settings can make a system unbootable if misconfigured. + +Recommended: + +* Keep a bootable USB handy +* Confirm your EFI mount point +* Back up important data + +--- + +## πŸ“œ Usage + +Make the script executable: + +``` +chmod +x grub-fix.sh +``` + +Run the script: + +``` +sudo ./grub-fix.sh +``` + +Reboot when done. + +--- + +## βœ” What the Script Does + +1. Verifies EFI directory presence +2. Lists available EFI boot entries +3. Reinstalls GRUB to the EFI System Partition +4. Updates GRUB configuration +5. Detects the Debian boot entry +6. Sets Debian as the first boot option +7. Completes cleanly + +--- + +## πŸ“Œ Notes + +This script is designed for **Debian-based Linux systems** with a standard EFI layout. +It is **not guaranteed to work on all Linux distributions**. + +--- + + diff --git a/restore-grub.sh b/restore-grub.sh new file mode 100644 index 0000000..d127cbc --- /dev/null +++ b/restore-grub.sh @@ -0,0 +1,34 @@ +#!/bin/bash + +set -e + +echo "=== Checking EFI directories ===" +ls /boot/efi/EFI || { echo "EFI partition not mounted!"; exit 1; } + +echo "EFI entries found:" +ls /boot/efi/EFI/ + +echo "=== Reinstalling GRUB to EFI partition ===" +sudo grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=Debian + +echo "=== Updating GRUB ===" +sudo update-grub + +echo "=== Fixing boot order (setting Debian to first) ===" +# Find Debian boot entry number +DEBIAN_BOOTNUM=$(sudo efibootmgr | grep -i debian | grep -oP 'Boot\K[0-9A-Fa-f]{4}') + +if [ -z "$DEBIAN_BOOTNUM" ]; then + echo "Error: Could not find Debian entry in efibootmgr!" + sudo efibootmgr + exit 1 +fi + +echo "Found Debian entry: Boot$DEBIAN_BOOTNUM" +echo "Setting Debian as first boot option..." + +sudo efibootmgr -o $DEBIAN_BOOTNUM + +echo "=== Done! ===" +echo "Reboot your system and GRUB should load first." +