Skip to content

Instantly share code, notes, and snippets.

@janie314
Forked from Tamal/set-chroot.sh
Last active September 9, 2022 11:01
Show Gist options
  • Save janie314/8cb525aab4adc606c4fc09961d299d64 to your computer and use it in GitHub Desktop.
Save janie314/8cb525aab4adc606c4fc09961d299d64 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
@J-Dunn
Copy link

J-Dunn commented Sep 9, 2022

Thanks to you and Tamal for this script.
It seems you lost a slash here on dev proc sys run compared to orginal .
$ for dir in dev proc sys run; do mount --bind $dir /mnt/$dir ; done

I'm trying to adapt this to prepare a HDD on a system running Fed35. I'm trying to clone the Fed34 installation on existing dual drive laptop ( which is broken) onto a new dual drive laptop ( HP Pavilion g7 ) .

I have edited the partitions to keep the original HP partitions to retain a win7 boot option and wish to clone the Fed34 to avoid a complete re-installation and reconfiguration.

I have copied over /root and /boot done all the mounts and chroot as indicated above. The chroot shows the expected Fed34.

cat  /etc/fedora-release
Fedora release 34 (Thirty Four)

However when I run grub2-mkconfig if finds the Fed35 on the running system; the Win7 and Win7 recovery partitions but not the Fed34 partition on /dev/sdc8 which is now the fs inside chroot.

grub2-mkconfig -o /boot/efi/EFI/fedora/grub.cfg
Generating grub configuration file ...
Found Fedora Linux 35 (Thirty Five) on /dev/sda3
Found Windows 7 on /dev/sdc1
Found Windows 7 on /dev/sdc2
Found Windows Recovery Environment on /dev/sdc3
Adding boot menu entry for UEFI Firmware Settings ...
done

Can you tell my why that is ?
Thanks.

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