[[restoring-bootloader-using-live-disk]]
= Restoring the bootloader using the Live disk.

Sometimes, especially after a secondary operating systems has been installed, 
the master boot record gets damaged which then prevents the original Linux system
from booting.

If this happens, it is necessary to reinstall *GRUB2* to recreate the original 
settings. The process not only discovers all installed operating systems, but 
usually adds them to the *GRUB2* configuration files, so they will all become 
bootable by *GRUB2*.

.Before you start

* Get the Fedora Live ISO from link:https://download.fedoraproject.org/pub/fedora/linux/releases/[getfedora.org].

* Prepare a bootable device using the downloaded ISO, either a CD or a USB.

.Procedure

. Boot the Fedora live system from the bootable device you have created.

. Open the terminal.

. Examine the partition layout and identify the `/boot` and the `/root` partition.
+
----
# fdisk -l
----

. Follow the <<btrfs-steps, BTRFS steps>> (Fedora 33 or newer) or <<lvm-steps, LVM steps>> (older than Fedora 33) to recover your system.

[[btrfs-steps]]
== BTRFS steps

. If your `/root` partition is encrypted by LUKS, it must be decrypted.

.. Make sure the crypt module is in use.
+
----
# modprobe dm-crypt
----

.. Decrypt the `/root` partition (e.g. `/dev/sda3`).
+
----
# cryptsetup luksOpen /dev/sda3 myvolume
----
+
The decrypted device (i.e. `myvolume`) will be accessible under `/dev/mapper/`.

. Mount the `/root` partition.

* For LUKS.
+
----
# mount /dev/mapper/myvolume /mnt -o subvol=root
----
* For non-LUKS.
+
----
# mount /dev/sda3 /mnt -o subvol=root
----
+

. Mount the `/boot` partition (e.g. `/dev/sda2)`.
+
----
# mount /dev/sda2 /mnt/boot
----
+

. Mount system processes and devices into the `/root` filesystem.
+
----
# mount -o bind /dev /mnt/dev
# mount -o bind /proc /mnt/proc
# mount -o bind /sys /mnt/sys
# mount -o bind /run /mnt/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/sys/firmware/efi/efivars
# mount /dev/sda1 /mnt/boot/efi
----
+
. Change your filesystem to the one mounted under `/mnt/`.
+
----
# chroot /mnt/
----
+
. Re-install *GRUB2*.

* On UEFI systems, several packages are required.
+
----
/]# dnf reinstall shim-* grub2-efi-* grub2-common

----
* On BIOS systems, specify the disk (e.g. `/dev/sda`) where *GRUB2* should be installed.
+
----
/]# grub2-install /dev/sda
----
+
. Re-generate the *GRUB2* configuration file.
+
----
/]# grub2-mkconfig -o /boot/grub2/grub.cfg
----
+
. Sync and exit the chroot.
+
----
/]# sync && exit
----
+
. Reboot the system.

[[lvm-steps]]
== LVM steps

. If your `/root` partition is encrypted by LUKS, it must be decrypted.

.. Make sure the crypt module is in use.
+
----
# modprobe dm-crypt
----

.. Decrypt the `/root` partition (e.g. `/dev/sda3`).
+
----
# cryptsetup luksOpen /dev/sda3 myvolume
----

.. Scan the LVM volumes for the volume group corresponding to the `/root` partition.
+
----
# vgscan
----

.. Activate the volume group (e.g. `fedora_localhost-live`).
+
----
# vgchange -ay fedora_localhost-live
----

.. Find the logical volume corresponding to `/root`.
+
----
# lvs
----
+
The logical volume will be accessible under `/dev/mapper/`.

. Create a `root` directory under `/mnt`.
+
----
# mkdir -p /mnt/root
----
+
. Mount the logical volume (e.g. `/dev/mapper/fedora_localhost--live-root`) corresponding to the `/root` partition.
+
----
# mount /dev/mapper/fedora_localhost--live-root /mnt/root
----
+
. Mount the `/boot` partition (e.g. `/dev/sda2`).
+
----
# mount /dev/sda2 /mnt/root/boot
----
+
. Mount system processes and devices into the `/root` filesystem.
+
----
# 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/root/boot/efi
----
+
. Change your filesystem to the one mounted under `/mnt/root`.
+
----
# chroot /mnt/root/
----
+
. Re-install *GRUB2* and re-generate the *GRUB2* configuration file.

* On UEFI systems, several packages are required.
+
----
/]# dnf reinstall shim-* grub2-efi-* grub2-common
/]# grub2-mkconfig -o /boot/efi/EFI/fedora/grub.cfg
----
* On BIOS systems, specify the disk (e.g. `/dev/sda`) where *GRUB2* should be installed.
+
----
/]# grub2-install /dev/sda
/]# grub2-mkconfig -o /boot/grub2/grub.cfg
----
+
. Sync and exit the chroot.
+
----
/]# sync && exit
----
+
. Reboot the system.