Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save MysticSnows/999198e95e034cb4f04e765193436ac4 to your computer and use it in GitHub Desktop.

Select an option

Save MysticSnows/999198e95e034cb4f04e765193436ac4 to your computer and use it in GitHub Desktop.
Chroot / rebuild Grub / Fix Damaged EFI Partition / rebuild Kernel (initramfs, vmlinuz) [Fedora 39]

If you simply want to fix grub or chroot, check out this official documented method (Fedora). It looks detailed enough but I came across it after solving my problem. Worth a try.

DISCLAIMER: THIS GUIDE IS SUPER NOVICE. IT REPRODUCES ALL THE STEPS I DID TO GET MY SYSTEM WORKING AFTER CONVERTING FROM EXT4 TO BTRFS.1 I TRIED TO KEEP IT BEGINNER FRIENDLY (PARTLY FOR FUTURE MYSELF). PLEASE UNDERSTAND INTUITION OF ANY COMMAND YOU EXECUTE FROM THIS GIST. READ COMMENTS IN BLOCK OF CODE ALONG THE WAY.

This was tested on Fedora 39 (UEFI).


Table of Contents

  1. Stuck at Grub CMD line
  2. Chroot from Live USB
  3. Reinstall Grub
  4. Booting to System for further steps
  5. Exit out of chroot
  6. Missing Rescue Kernel
  7. References (Do Checkout)

Stuck at Grub Command Line

If you are simply stuck at grub>, consider trying the following:

grub> ls
# it will list as (hdx,gptx) where x=some number
# ls into each (hdx,gptx)/ and TAB to identify the root partition
# E.g.:
grub> ls (hd0,gpt1)/		# TAB for auto completion suggestion
# once root partition identified, try doing cat into root's /boot/loader/entries/...
# E.g.:
grub> cat (hd1,gpt5)/boot/loader/entries/			# Use TAB auto completion
# E.g. Output
title Fedora Linux (6.7.9-200.fc39.x86_64) 39 (KDE Plasma)
version 6.7.9-200.fc39.x86_64
linux /boot/vmlinuz-6.7.9-200.fc39.x86_64
initrd /boot/initramfs-6.7.9-200.fc39.x86_64.img $tuned_initrd
options root=UUID=361c2fe6-65df-4f9e-b4b2-fff62447e3cc ro rootflags=subvol=root resume=UUID=f529420c-56d4-4651-a244-dd86731b4b08 rhgb quiet rd.driver.blacklist=nouveau modprobe.blacklist=nouveau iommu=pt intel_iommu=on 
grub_users $grub_users
grub_arg --unrestricted
grub_class fedora

# If options root=UUID... has any repeating lines, they are populating from /etc/default/grub. Fix it manually after you boot.

# Yours will differ. Do following
grub> load_video
grub> set gfxpayload=keep
grub> insmod gzio
grub> linux (hd1,gpt5)/vmlinuz-... root=UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx ro rootflags=subvol=root quiet
# E.g. For previous output of cat example, it will be `linux (hd1,gpt5)/boot/vmlinuz-6.7.9-200.fc39.x86_64 root=UUID=361c2fe6-65df-4f9e-b4b2-fff62447e3cc ro rootflags=subvol=root quiet`
# You need to type UUID manually so type it carefully
# same for next command. Use auto completions for (hdx,gptx)/boot/v..
grub> initrd (hd1,gpt5)/initramfs-...
grub> set root=(hd1,gpt5)
grub> boot

If you boot successfully, REBUILD GRUB IMMEDIATELY as its temporary.

$ sudo grub2-mkconfig -o /boot/grub2/grub.cfg

Do a reboot to see if everything's working. If yes, congrats! Refrain from doing anything else from this guide now.


Chroot from Live USB

First step is to boot from Fedora Live USB. After booting up, MAKE SURE TO CONNECT TO INTERNET.

Identify Partitions

$ lsblk

# If you have btrfs, also check for subvolumes name
sudo btrfs subvol list -t /

Mount Partitions

Make sure to be in superuser mode. Also be careful with commands since you are doing operations in superuser mode. $ sudo su

# subvol=@ if root is '@' in flat btrfs layout
mount -o subvol=root /dev/nvme0n1p /mnt

# Optional: If you have /boot partition else skip
mount /dev/nvme0n1p /mnt/boot

# mount EFI partition
mount /dev/nvme0n1p /mnt/boot/efi

Chroot for Mounted Root Partition

mount -t proc /proc /mnt/proc
mount --rbind /dev /mnt/dev
mount --make-rslave /mnt/dev
mount --rbind /sys /mnt/sys
mount --make-rslave /mnt/sys

# copy resolv.conf of liveUSB to original system
cp /mnt/etc/resolv.conf /mnt/etc/resolv.conf.chroot
cp -L /etc/resolv.conf /mnt/etc

# if copy fails then edit new resolv.con manually after entering chroot
# make sure to first rename original resolv.conf to resolv.conf.bak (It's a symlink we are renaming)

chroot /mnt /bin/bash

ping www.google.com
# ping should be successful. If not, resolve the /etc/resolv.conf first
# from earlier steps

Time to Reinstall Grub

This assumes you have chroot access to original system discussed in previous steps.

# first check if /mnt/etc/fstab is having correct UUIDs

# DON'T EXIT CHROOT (just a reminder)

# edit this line in /mnt/etc/default/grub to false 
# (otherwise you might only see 'UEFI Firmware' in grub after boot)
GRUB_ENABLE_BLSCFG=false

# if fstab has correct UUIDs
mount /boot/efi

# reinstall grub
dnf reinstall grub2-efi shim -y

# create grub 
grub2-mkconfig -o /boot/efi/EFI/fedora/grub.cfg

# OPTIONAL STEP : follow if you don't have a Kernel installed
# reinstall kernel-core (for initramfs and vmlinuz)
dnf reinstall kernel-core -y
# To detect installed kernel and create entry
grub2-mkconfig -o /boot/efi/EFI/fedora/grub.cfg

WE ARE NOT DONE. FOLLOW NEXT STEPS BASED ON ISSUES ENCOUNTERED.


Booting to Original System

After reboot, you will be in your original system.

1. Failed Boot with Service Errors

I encountered this (maybe since I converted system to BTRFS from EXT4) but you may not. Lets see what to do if you encounter it as well.2

To resolve, we need to set correct permissions on root partition At the GRUB menu, edit the first boot entry (Of Fedora). Remove rhgb quiet and add rd.shell rd.timeout=30 systemd.debug-shell=1 , switch to tty9 (Ctrl + Alt + F9) and issue # rpm --setperms filesystem. You might need to issue it multiple times (3 times should do).

2. Setting BLSCFG back to true and fixing grub.cfg location

Setting GRUB_ENABLE_BLSCFG=true in /etc/default/grub.

We made it false so in Live USB, running grub2-mkconfig detects the install of linux in /boot (basically the initramfs & vmlinuz).

This is IMPORTANT otherwise kernel updates will not boot your system as grub was not updated. (You need to regenerate grub manually after kernel updates if you keep it false)

We need to change GRUB_ENABLE_BLSCFG=false to GRUB_ENABLE_BLSCFG=true .

Easy Way

Easy way is to do sudo grub2-switch-to-blscfg with GRUB_ENABLE_BLSCFG=false then making it true ; but that did not work for me (since I had missing /boot/loader/entries. More on that later.).

Hard Way

I have a roundabout way that worked, not claiming its the efficient way. Follow this if your /boot/loader/entries is empty somehow. Check using sudo ls -l /boot/loader/entries If its empty, we will create those entries after fixing grub.cfg location.

Fixing grub.cfg location

This involves deleting grub.cfg from /boot/efi/efi/fedora.3

MAKE SURE YOUR COMPUTER WON'T LOSE ELECTRICITY WHILE DOING THIS OR BACK TO SQUARE ONE

# modify this line in /etc/default/grub
GRUB_ENABLE_BLSCFG=true

# remove grub.cfg located here. Alternatively, you can delete entire grub related files (if you know the way)
sudo rm /boot/efi/efi/fedora/grub.cfg /boot/grub2/grub.cfg

# reinstall grub. 
# Here grub2-common is IMPORTANT as it will create grub.cfg inside /boot/efi/efi/fedora automatically
sudo dnf reinstall grub2-efi shim-x64 grub2-common -y

# create grub.cfg
sudo grub2-mkconfig -o /boot/grub2/grub.cfg

# regenerate your initramfs 
# Optionally, you can reinstall kernel-core before this step
# Make sure you don't have more than one kernel installed (just in case to avoid dracut: Can't write to /boot/efi... error)
rpm -q kernel

sudo dracut --regenerate-all --force

DON'T REBOOT YET!

For some reason, /boot/loader/entries was empty for me even after this. If its for you as well, this is how I fixed it.4

sudo ls -l /boot/loader/entries again. If its still empty, execute the following code.

sudo /bin/kernel-install add 6.7.9-200.fc39.x86_64 /lib/modules/6.7.9-200.fc39.x86_64/vmlinuz
# tab auto completions work after 'add'.
# select the current running kernel with which the system is going to boot

Voila! Now if you check /boot/loader/entries the entry is created. You can run dracut again (not necessary).


Exit out of chroot

umount --recursive /mnt/sys
umount --recursive /mnt/dev
umount /mnt/proc
umount /mnt

Missing Rescue Kernel

There is a good followup here5 and in References. I would suggest you give it a read and follow it instead.

sudo dnf install dracut-config-rescue

# ensure if dracut_rescue_image="yes"
$ cat /usr/lib/dracut/dracut.conf.d/02-rescue.conf
dracut_rescue_image="yes"

# ensure installonly_limit=2 or 0 else modify it accordingly
sudo cat /etc/dnf/dnf.conf
# (If its 0 it means it will keep all kernels. More info in citation.)

sudo dnf reinstall kernel kernel-core

That should bring the Rescue Kernel back.


PS: Feedback about any of these steps which are redundant, incorrect or obsolete (in future) will be appreciated, making the experience better for everyone! References to discussions or docs link will also make it so much more better. Share what different steps you followed aside from this guide (alongside commands ;). Let me know if this helped you in any way.

Thanks.


References

(Citations at bottom)

Mount Options

More on GRUB

Interesting BTRFS discussion

Using Timeshift on Fedora BTRFS

Update Rescue Kernel Fedora

Footnotes

  1. Converting EXT4 to BTRFS on Fedora

  2. Fedora boot process endless with services failing and no login

  3. Grub boots from /boot/efi/efi/fedora/grub.cfg instead of /boot/grub2/grub.cfg

  4. Fix empty /boot/loader/entries

  5. Reddit: Add Rescue Entry in Fedora

@MysticSnows
Copy link
Copy Markdown
Author

If anyone is having an encrypted BTRFS partition, do checkout the process followed by another reddit user (on Nobara 42, a modified version of Fedora42) for guidance as its more specific to that situation. Always review each step carefully as these processes reflect our individual experiences and your system config/environment may differ.

Link to Reddit Post

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