A simple Grub Restore script to Restore Debian based system for UEFI based systems

This commit is contained in:
arul 2025-11-19 08:22:55 +05:30
commit 85ed8ca6a2
2 changed files with 223 additions and 0 deletions

189
README.md Normal file
View File

@ -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**.
---

34
restore-grub.sh Normal file
View File

@ -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."