Skip to content

Instantly share code, notes, and snippets.

@Tamal
Last active December 16, 2023 22:59
  • Star 39 You must be signed in to star a gist
  • Fork 11 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save Tamal/73e65bfb0e883e438310c5fe81c5de14 to your computer and use it in GitHub Desktop.
Setting up chroot from a live image in Fedora. Regenerate grub2 for Fedora.
$ # Use Live CD to boot
$ sudo su # Switch to root
$ fdisk -l # Get names of root, boot & EFI partition names. you can also use blkid
$ mount /dev/mapper/fedora_localhost--live-root /mnt # mount root partition
$ cat /mnt/etc/fedora-release
Fedora release 31 (Thirty One)
$ mount /dev/nvme0n1p2 /mnt/boot # mount boot partition
$ mount /dev/nvme0n1p1 /mnt/boot/efi # mount EFI partition
# Note: If you are not able to mount EFI partition ('Input/Output error'),
# You may have to repair ESP file system or format ESP.
# fsck.vfat /dev/nvme0n1p1
# mkfs.vfat /dev/nvme0n1p1
# If formatted then we may have to update UUID at /etc/fstab
$ ls /mnt/boot/efi # should show all OS names under EFI
$ # mount the virtual filesystems that the system uses to communicate with processes and devices
$ for dir in /dev /proc /sys /run; do mount --bind $dir /mnt/$dir ; done
$ # enter chroot
$ chroot /mnt
$ # Now you can do all the work e.g. fix grub
$ dnf reinstall grub2-efi shim -y
$ grub2-mkconfig -o /boot/efi/EFI/fedora/grub.cfg # Regenerate grub2
$ # Check /etc/fstab UUID, update if necessary [Hint: lsblk -f would show partition UUIDs]
$ # All things done; now exit from chroot
$ exit
$ # Check BIOS boot details [ Note: this command won't work if you are inside chroot. ]
$ efibootmgr -v
$ # In case you need to create new entry in BIOS
$ efibootmgr -c -d /dev/nvme0n1p1 -p 1 -L Fedora -l '\EFI\fedora\grubx64.efi' # or, shimx64.efi
$ # To delete entry from efibootmgr use: efibootmgr -b <#entry> -B
$ # Copy grubx64.efi from Live USB, if required
$ cp -p /boot/efi/EFI/grubx64.efi /mnt/boot/efi/EFI/fedora
$ exit
$ # Now you can reboot
@falstaff1288
Copy link

Very useful!

@tobikris
Copy link

Thanks for this writeup - for my usecase I needed to interleave this with some commands to decrypt my luks partition. I documented this in my fork: https://gist.github.com/tobikris/e87e57d0acd11910f1133597a97e73f0.

@janie314
Copy link

Hi! Thanks for a great Bash script. I did find that some directory paths had to be adjusted for one line to work with a current Fedora live USB. I forked and adjusted slightly if you're interested in looking and possibly merging in.

https://gist.github.com/janie314/8cb525aab4adc606c4fc09961d299d64

@hyr2
Copy link

hyr2 commented Sep 2, 2022

Once in chroot /mnt/root

I cant run any command like ls or cd (it says command not found). Similarly I can't run grub2-mkconfig since it gives an error saying command not found. Please help

@elvisisvan
Copy link

i keep getting this "chroot: failed to run command '/bin/bash': no such file or directory" error and yes i did search for solutions on stackoverflow, stackexchange, askubuntu, etc. and i know the bash file (in my case) is at /mnt/fedora-37/root/bin/bash so i tried

chroot /mnt/fedora-37 /bin/bash

and

chroot /mnt/fedora-37 /mnt/fedora-37/root/bin/bash

but none of them work :(
image

image

please help

@Nadahar
Copy link

Nadahar commented Apr 13, 2023

Thanks, your "loop" what was I was missing 👍

With "loop" I mean:

for dir in /dev /proc /sys /run; do mount --bind $dir /mnt/$dir ; done

@bruteforks
Copy link

adding other steps i had to do to get this working correctly:
adding root folder to fedora doc instructions:

# mount -o bind /dev /mnt/root/dev
# mount -o bind /proc /mnt/root/proc
# mount -o bind /sys /mnt/root/sys
# mount -o bind /run /mnt/root/run

On UEFI systems, bind the efivars directory and mount the EFI system partition (e.g. /dev/sda1).

# mount -o bind /sys/firmware/efi/efivars /mnt/root/sys/firmware/efi/efivars
# mount /dev/sda1 /mnt/boot/efi

chroot: failed to run command '/bin/bash': no such file or directory

chroot /mnt/root /usr/bin/bash

then continue official docs

dnf reinstall shim-* grub2-efi-* grub2-common

etc

@borisovdev
Copy link

Thank you so much! You saved my weekend

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