Last active
January 26, 2025 06:48
-
-
Save zx0r/df3166fe117f5c39059b4854f336982d to your computer and use it in GitHub Desktop.
[SOLVED] Unable to boot after kernel update
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
#!/usr/bin/env bash | |
# Script: restore_bootloader.sh | |
# Description: Restore of a broken bootloader for Gentoo Linux. | |
# Author: zx0r | |
# Version: 1.0 | |
# Usage: Run as root or with sudo. | |
# Overview: | |
# This script is designed to restore a broken bootloader on a Gentoo Linux system. | |
# It automates the process of mounting the necessary partitions, chrooting into the | |
# system, reinstalling GRUB, and generating a new GRUB configuration file. | |
# Steps performed by the script: | |
# 1. Mounts the root partition to a temporary mount point. | |
# 2. Mounts essential filesystems (proc, sys, dev, run) for chroot. | |
# 3. Chroots into the mounted system. | |
# 4. Mounts the EFI partition (if applicable). | |
# 5. Reinstalls GRUB for UEFI or BIOS systems. | |
# 6. Generates a new GRUB configuration file. | |
# 7. Unmounts filesystems and exits the chroot environment. | |
# 8. Reboots the system to apply changes. | |
# Usage: | |
# 1. Boot into a live CD/USB environment. | |
# 2. Download the script: | |
# curl -sSL https://gist.githubusercontent.com/zx0r/8c8ad48dd795a1cfbd2e2bd4b6b9bdff/raw/restore_bootloader.sh -o restore_bootloader.sh | |
# 3. Make the script executable: | |
# chmod +x restore_bootloader.sh | |
# 4. Run the script as root: | |
# sudo ./restore_bootloader.sh | |
# Notes: | |
# - Replace the default partition variables (CHROOT_PARTITION and EFI_PARTITION) with | |
# the correct partitions for your system. | |
# - Ensure you have a backup of your data before running the script. | |
# - This script is specifically designed for Gentoo Linux but can be adapted for other | |
# distributions with minor modifications. | |
# Example: | |
# CHROOT_PARTITION="/dev/nvme0n1p2" # Replace with your root partition | |
# EFI_PARTITION="/dev/nvme0n1p1" # Replace with your EFI partition (if applicable) | |
set -euo pipefail # Enable strict error handling | |
# Variables | |
CHROOT_PARTITION="/dev/nvme1n1p4" # Replace with your Linux root partition | |
EFI_PARTITION="/dev/nvme1n1p2" # Replace with your EFI partition | |
MOUNT_POINT="/mnt/gentoo" # Temporary mount point | |
# Functions | |
log() { | |
echo "[INFO] $1" | |
} | |
error() { | |
echo "[ERROR] $1" >&2 | |
exit 1 | |
} | |
# Check if the script is run as root | |
if [[ $EUID -ne 0 ]]; then | |
error "This script must be run as root. Use 'sudo' or switch to the root user." | |
fi | |
# Step 1: Mount the root partition | |
log "Mounting root partition $CHROOT_PARTITION to $MOUNT_POINT..." | |
mkdir -p "$MOUNT_POINT" || error "Failed to create mount point $MOUNT_POINT." | |
mount "$CHROOT_PARTITION" "$MOUNT_POINT" || error "Failed to mount $CHROOT_PARTITION." | |
# Step 2: Mount necessary filesystems | |
log "Mounting proc, sys, dev, and run filesystems..." | |
mount --types proc /proc "$MOUNT_POINT/proc" || error "Failed to mount /proc." | |
mount --rbind /sys "$MOUNT_POINT/sys" || error "Failed to mount /sys." | |
mount --rbind /dev "$MOUNT_POINT/dev" || error "Failed to mount /dev." | |
mount --bind /run "$MOUNT_POINT/run" || error "Failed to mount /run." | |
# Make mounts slave for proper chroot environment | |
log "Making sys, dev, and run mounts slave..." | |
mount --make-rslave "$MOUNT_POINT/sys" || error "Failed to make sys slave." | |
mount --make-rslave "$MOUNT_POINT/dev" || error "Failed to make dev slave." | |
mount --make-slave "$MOUNT_POINT/run" || error "Failed to make run slave." | |
# Step 3: Chroot into the environment | |
log "Chrooting into $MOUNT_POINT..." | |
chroot "$MOUNT_POINT" /bin/bash <<EOF | |
set -euo pipefail # Enable strict error handling in chroot | |
# Step 4: Mount the EFI partition | |
log "Mounting EFI partition $EFI_PARTITION to /boot..." | |
mkdir -p /boot || error "Failed to create /boot directory." | |
mount "$EFI_PARTITION" /boot || error "Failed to mount $EFI_PARTITION." | |
# Step 5: Install GRUB | |
log "Installing GRUB for UEFI..." | |
grub-install --target=x86_64-efi --efi-directory=/boot --bootloader-id="GentooUnixPorn" || error "GRUB installation failed." | |
# Step 6: Generate GRUB configuration | |
log "Generating GRUB configuration..." | |
grub-mkconfig -o /boot/grub/grub.cfg || error "Failed to generate GRUB configuration." | |
# Exit chroot | |
log "Exiting chroot environment..." | |
exit | |
EOF | |
# Step 7: Unmount filesystems | |
log "Unmounting filesystems..." | |
umount -l "$MOUNT_POINT"/{dev,sys,proc,run} || error "Failed to unmount dev, sys, proc, or run." | |
umount -R "$MOUNT_POINT" || error "Failed to unmount $MOUNT_POINT." | |
# Step 8: Reboot the system | |
log "Bootloader restoration complete. Rebooting the system..." | |
reboot | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.