-
-
Save stevensdotb/1509861d5895472f1ced85866cb04c24 to your computer and use it in GitHub Desktop.
grub_nvme_repair
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # GRUB Repair Script for NVMe Disk (After BIOS Update) | |
| # This script repairs GRUB bootloader after BIOS updates that break Linux boot | |
| # Usage: Run this from a Kali Linux Live USB | |
| # Color codes | |
| RED='\033[0;31m' | |
| GREEN='\033[0;32m' | |
| YELLOW='\033[1;33m' | |
| BLUE='\033[0;34m' | |
| CYAN='\033[0;36m' | |
| NC='\033[0m' # No Color | |
| echo -e "${CYAN}==========================================${NC}" | |
| echo -e "${CYAN}GRUB Repair Script for NVMe Disk${NC}" | |
| echo -e "${CYAN}==========================================${NC}" | |
| echo "" | |
| # Check if running as root | |
| if [ "$EUID" -ne 0 ]; then | |
| echo -e "${RED}ERROR: Please run as root (use sudo)${NC}" | |
| exit 1 | |
| fi | |
| # Step 1: Identify partitions | |
| echo -e "${BLUE}[Step 1] Identifying disk partitions...${NC}" | |
| echo "" | |
| lsblk -f | |
| echo "" | |
| echo -e "${YELLOW}Look for your Kali Linux partition (usually ext4 on nvme0n1)${NC}" | |
| echo "" | |
| # Get user input for partitions | |
| read -p "Enter your Kali ROOT partition (e.g., nvme0n1p3): " ROOT_PARTITION | |
| read -p "Enter your EFI partition (usually nvme0n1p1): " EFI_PARTITION | |
| # Validate input | |
| if [ -z "$ROOT_PARTITION" ] || [ -z "$EFI_PARTITION" ]; then | |
| echo -e "${RED}ERROR: Partitions cannot be empty${NC}" | |
| exit 1 | |
| fi | |
| # Extract disk name (e.g., nvme0n1 from nvme0n1p3) | |
| DISK_NAME=$(echo $ROOT_PARTITION | sed 's/p[0-9]*$//') | |
| echo "" | |
| echo -e "${CYAN}Configuration:${NC}" | |
| echo -e " Root partition: ${GREEN}/dev/$ROOT_PARTITION${NC}" | |
| echo -e " EFI partition: ${GREEN}/dev/$EFI_PARTITION${NC}" | |
| echo -e " Disk: ${GREEN}/dev/$DISK_NAME${NC}" | |
| echo "" | |
| read -p "Is this correct? (y/n): " CONFIRM | |
| if [ "$CONFIRM" != "y" ] && [ "$CONFIRM" != "Y" ]; then | |
| echo -e "${YELLOW}Aborted by user${NC}" | |
| exit 1 | |
| fi | |
| # Step 2: Mount partitions | |
| echo "" | |
| echo -e "${BLUE}[Step 2] Mounting partitions...${NC}" | |
| umount -R /mnt 2>/dev/null # Unmount if already mounted | |
| mount /dev/$ROOT_PARTITION /mnt | |
| if [ $? -ne 0 ]; then | |
| echo -e "${RED}ERROR: Failed to mount root partition${NC}" | |
| exit 1 | |
| fi | |
| echo -e "${GREEN}✓ Mounted root partition${NC}" | |
| # Check if /boot/efi exists in the root partition | |
| if [ ! -d /mnt/boot/efi ]; then | |
| mkdir -p /mnt/boot/efi | |
| echo -e "${GREEN}✓ Created /boot/efi directory${NC}" | |
| fi | |
| mount /dev/$EFI_PARTITION /mnt/boot/efi | |
| if [ $? -ne 0 ]; then | |
| echo -e "${RED}ERROR: Failed to mount EFI partition${NC}" | |
| umount /mnt | |
| exit 1 | |
| fi | |
| echo -e "${GREEN}✓ Mounted EFI partition${NC}" | |
| # Mount system directories | |
| mount --bind /dev /mnt/dev | |
| mount --bind /proc /mnt/proc | |
| mount --bind /sys /mnt/sys | |
| echo -e "${GREEN}✓ Mounted system directories${NC}" | |
| # Step 3: Chroot and repair GRUB | |
| echo "" | |
| echo -e "${BLUE}[Step 3] Entering chroot environment and repairing GRUB...${NC}" | |
| echo "" | |
| # Create a script to run inside chroot | |
| cat > /mnt/tmp/repair_grub.sh << 'CHROOT_SCRIPT' | |
| #!/bin/bash | |
| # Color codes for chroot | |
| RED='\033[0;31m' | |
| GREEN='\033[0;32m' | |
| YELLOW='\033[1;33m' | |
| BLUE='\033[0;34m' | |
| NC='\033[0m' | |
| echo -e "${CYAN}Inside chroot environment...${NC}" | |
| echo "" | |
| # Check if grub directory exists | |
| if [ ! -d /boot/grub ]; then | |
| echo -e "${YELLOW}Creating /boot/grub directory...${NC}" | |
| mkdir -p /boot/grub | |
| fi | |
| # Check if grub binaries exist | |
| if [ ! -f /usr/sbin/grub-install ]; then | |
| echo -e "${YELLOW}WARNING: GRUB packages not found!${NC}" | |
| echo "Attempting to reinstall (requires internet)..." | |
| apt update | |
| apt install -y --reinstall grub-efi-amd64 grub-common grub2-common | |
| fi | |
| # Install GRUB | |
| echo -e "${BLUE}Installing GRUB to disk...${NC}" | |
| DISK_NAME_CHROOT=$(echo $ROOT_PARTITION | sed 's/p[0-9]*$//') | |
| grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=kali --force /dev/$DISK_NAME_CHROOT | |
| if [ $? -ne 0 ]; then | |
| echo -e "${YELLOW}WARNING: grub-install reported issues (this might be normal in Live USB)${NC}" | |
| fi | |
| # Generate GRUB configuration | |
| echo "" | |
| echo -e "${BLUE}Generating GRUB configuration...${NC}" | |
| grub-mkconfig -o /boot/grub/grub.cfg | |
| if [ $? -eq 0 ] && [ -f /boot/grub/grub.cfg ]; then | |
| echo -e "${GREEN}✓ GRUB configuration created successfully${NC}" | |
| echo "" | |
| echo -e "Config file size: ${GREEN}$(du -h /boot/grub/grub.cfg | cut -f1)${NC}" | |
| else | |
| echo -e "${RED}ERROR: Failed to create GRUB configuration${NC}" | |
| exit 1 | |
| fi | |
| # Update initramfs | |
| echo "" | |
| echo -e "${BLUE}Updating initramfs...${NC}" | |
| update-initramfs -u -k all | |
| echo "" | |
| echo -e "${GREEN}✓ GRUB repair completed inside chroot${NC}" | |
| CHROOT_SCRIPT | |
| chmod +x /mnt/tmp/repair_grub.sh | |
| # Export the ROOT_PARTITION variable so it's available in chroot | |
| export ROOT_PARTITION | |
| # Execute the script inside chroot | |
| chroot /mnt /bin/bash -c "ROOT_PARTITION=$ROOT_PARTITION /tmp/repair_grub.sh" | |
| CHROOT_EXIT_CODE=$? | |
| # Step 4: Create fallback boot entries | |
| echo "" | |
| echo -e "${BLUE}[Step 4] Creating fallback EFI boot entries...${NC}" | |
| # Ensure Boot folder exists with fallback bootloader | |
| mkdir -p /mnt/boot/efi/EFI/Boot | |
| if [ -f /mnt/boot/efi/EFI/kali/grubx64.efi ]; then | |
| cp /mnt/boot/efi/EFI/kali/grubx64.efi /mnt/boot/efi/EFI/Boot/bootx64.efi | |
| echo -e "${GREEN}✓ Created fallback bootloader${NC}" | |
| elif [ -f /mnt/boot/efi/EFI/debian/grubx64.efi ]; then | |
| cp /mnt/boot/efi/EFI/debian/grubx64.efi /mnt/boot/efi/EFI/Boot/bootx64.efi | |
| echo -e "${GREEN}✓ Created fallback bootloader from debian${NC}" | |
| fi | |
| # Step 5: Install and use efibootmgr | |
| echo "" | |
| echo -e "${BLUE}[Step 5] Installing efibootmgr and registering EFI boot entry...${NC}" | |
| # Check if efibootmgr is installed | |
| if ! command -v efibootmgr &> /dev/null; then | |
| echo -e "${YELLOW}Installing efibootmgr...${NC}" | |
| # Check if we have internet connection | |
| if ping -c 1 8.8.8.8 &> /dev/null; then | |
| apt update | |
| apt install -y efibootmgr | |
| if [ $? -eq 0 ]; then | |
| echo -e "${GREEN}✓ efibootmgr installed successfully${NC}" | |
| else | |
| echo -e "${YELLOW}⚠ Failed to install efibootmgr (no internet or repository issue)${NC}" | |
| echo " You may need to manually set boot order in BIOS" | |
| fi | |
| else | |
| echo -e "${YELLOW}⚠ No internet connection - cannot install efibootmgr${NC}" | |
| echo -e " Please connect to internet ${CYAN}(nmtui for WiFi)${NC} and run script again" | |
| echo " Or manually set boot order in BIOS" | |
| fi | |
| fi | |
| # Use efibootmgr if available | |
| if command -v efibootmgr &> /dev/null; then | |
| echo "" | |
| echo -e "${CYAN}Current boot entries:${NC}" | |
| efibootmgr -v | |
| echo "" | |
| # Extract partition number from EFI_PARTITION | |
| PART_NUM=$(echo $EFI_PARTITION | grep -o '[0-9]*$') | |
| # Add Kali boot entry | |
| echo -e "${BLUE}Adding Kali Linux boot entry...${NC}" | |
| efibootmgr -c -d /dev/$DISK_NAME -p $PART_NUM -L "Kali Linux" -l "\\EFI\\kali\\grubx64.efi" | |
| if [ $? -eq 0 ]; then | |
| echo -e "${GREEN}✓ EFI boot entry registered${NC}" | |
| echo "" | |
| echo -e "${CYAN}Updated boot entries:${NC}" | |
| efibootmgr -v | |
| else | |
| echo -e "${YELLOW}⚠ Failed to register EFI boot entry${NC}" | |
| echo " This is normal in some Live USB environments" | |
| fi | |
| else | |
| echo -e "${YELLOW}⚠ efibootmgr not available${NC}" | |
| echo " You may need to manually set boot order in BIOS" | |
| fi | |
| # Step 6: Cleanup | |
| echo "" | |
| echo -e "${BLUE}[Step 6] Cleaning up...${NC}" | |
| rm -f /mnt/tmp/repair_grub.sh | |
| umount /mnt/boot/efi | |
| umount /mnt/dev | |
| umount /mnt/proc | |
| umount /mnt/sys | |
| umount /mnt | |
| echo -e "${GREEN}✓ All partitions unmounted${NC}" | |
| # Final instructions | |
| echo "" | |
| echo -e "${CYAN}==========================================${NC}" | |
| echo -e "${GREEN}GRUB Repair Complete!${NC}" | |
| echo -e "${CYAN}==========================================${NC}" | |
| echo "" | |
| echo -e "${YELLOW}Next steps:${NC}" | |
| echo -e "1. Reboot your system: ${CYAN}sudo reboot${NC}" | |
| echo -e "2. Enter BIOS ${CYAN}(press F2 during boot on Acer Nitro)${NC}" | |
| echo "3. Check these settings:" | |
| echo -e " - Boot Mode: ${GREEN}UEFI${NC} (not Legacy/CSM)" | |
| echo -e " - Secure Boot: ${GREEN}Disabled${NC}" | |
| echo -e " - Fast Boot: ${GREEN}Disabled${NC}" | |
| echo -e " - Boot Order: Set ${GREEN}nvme0n1${NC} as first boot device" | |
| echo -e "4. Look for ${GREEN}'Kali Linux'${NC} in boot options and select it" | |
| echo "" | |
| echo "If GRUB still doesn't load, you may need to manually" | |
| echo "add a boot entry in BIOS pointing to:" | |
| echo -e " ${CYAN}\\EFI\\kali\\grubx64.efi${NC}" | |
| echo " or" | |
| echo -e " ${CYAN}\\EFI\\Boot\\bootx64.efi${NC}" | |
| echo "" | |
| echo -e "${CYAN}==========================================${NC}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment