Skip to content

Instantly share code, notes, and snippets.

@zx0r
Last active January 26, 2025 06:48
Show Gist options
  • Save zx0r/df3166fe117f5c39059b4854f336982d to your computer and use it in GitHub Desktop.
Save zx0r/df3166fe117f5c39059b4854f336982d to your computer and use it in GitHub Desktop.
[SOLVED] Unable to boot after kernel update
#!/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
@zx0r
Copy link
Author

zx0r commented Jan 26, 2025

# Description: Manual restore of a broken bootloader for Gentoo Linux.
# Author: zx0r
# Version: 1.0
# Usage: Run as root or with sudo.

# Prerequisites:
# 1. A live CD/USB with Gentoo or a similar Linux environment.
# [Gentoo LiveGUI USB Image](https://www.gentoo.org/downloads/)
# [Ventoy to create bootable USB drive](https://github.com/ventoy/Ventoy/releases)
# 2. Root access (run the script as root or with sudo).
# 3. Knowledge of the correct partitions for root (/) and EFI (if applicable).

# 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.


livecd /home/gentoo # sudo su
livecd /home/gentoo # fdisk -l | grep 'Linux filesystem'
/dev/nvme1n1p4    5609472 1054185471 1048576000   500G Linux filesystem
livecd /home/gentoo # fdisk -l | grep 'EFI'
/dev/nvme1n1p2      65536    1703935    1638400   800M EFI System
livecd /home/gentoo # CHROOT="/dev/nvme1n1p4"
livecd /home/gentoo # mount -o remount,rw /
livecd /home/gentoo # mount -a
livecd /home/gentoo # mkdir /mnt/gentoo
livecd /home/gentoo # mount ${CHROOT} /mnt/gentoo
livecd /home/gentoo # mount --types proc /proc /mnt/gentoo/proc
livecd /home/gentoo # mount --rbind /sys /mnt/gentoo/sys
livecd /home/gentoo # mount --rbind /dev /mnt/gentoo/dev
livecd /home/gentoo # mount --bind /run /mnt/gentoo/run
livecd /home/gentoo # mount --make-rslave /mnt/gentoo/sys
livecd /home/gentoo # mount --make-rslave /mnt/gentoo/dev
livecd /home/gentoo # mount --make-slave /mnt/gentoo/run
livecd /home/gentoo # chroot /mnt/gentoo /bin/sh
livecd /home/gentoo # env-update && source /etc/profile && export PS1="(chroot) $PS1" && cd
>>> Regenerating /etc/ld.so.cache...
(chroot) livecd ~ # mountpoint /boot
/boot is not a mountpoint
(chroot) livecd ~ # mount -a
(chroot) livecd ~ # mountpoint /boot
/boot is a mountpoint
(chroot) livecd ~ # ls /boot
 EFI                               amd-uc.img                           vmlinuz-6.11.7-gentoo-x86_64
'System Volume Information'        grub
 System.map-6.11.7-gentoo-x86_64   initramfs-6.11.7-gentoo-x86_64.img

# --- # If not entry for /etc/fstab
(chroot) livecd / # mount /dev/nvme1n1p2 /mnt/gentoo/boot
(chroot) livecd / # fdisk -l | grep 'EFI System'
(chroot) livecd / #  mount /dev/nvme1n1p2 /mnt/gentoo/boot
(chroot) livecd / # ls /boot
 EFI                               grub
'System Volume Information'        initramfs-6.11.7-gentoo-x86_64.img
 System.map-6.11.7-gentoo-x86_64   vmlinuz-6.11.7-gentoo-x86_64
 amd-uc.img
(chroot) livecd / # tree -l 2 /boot
# --- #

chroot) livecd /boot/EFI # grub-install --target=x86_64-efi --efi-directory=/boot --bootloader-id="GentooUnixPorn"
Installing for x86_64-efi platform.
Installation finished. No error reported.
# If error installing Grub add this flag --recheck --removable

(chroot) livecd /boot/EFI # grub-mkconfig -o /boot/grub/grub.cfg
Generating grub configuration file ...
Found theme: /usr/share/grub/themes/Cyberpunk/theme.txt
Found linux image: /boot/vmlinuz-6.11.7-gentoo-x86_64
Found initrd image: /boot/amd-uc.img /boot/initramfs-6.11.7-gentoo-x86_64.img
Adding boot menu entry for UEFI Firmware Settings ...
done
(chroot) livecd /boot/EFI # tree /boot/ -L 3
(chroot) livecd /boot/EFI # exit
livecd /home/gentoo # umount -l /mnt/gentoo/{dev,sys,proc,run,}
livecd /home/gentoo # umount -R /mnt/gentoo
livecd /home/gentoo # reboot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment