How to repair or install GRUB?

GRUB or GRand Unified Bootloader is a multiboot bootloader.
When configuration file of grub is update it first scan the devices
to find available operating system at the current machine by a
script os-prober, which provide boot entries and location of each
operating system.

To repair GRUB is like freshly installing it in a drive with boot code.
In MBR partition table first 446 bytes of hard drive is use for boot code
which point towards location of bootloader.

GRUB can be install in a two way with Live CD image.
(1). In a Live CD image environment while updating the boot file grub.cfg
it will also add boot entry of Live CD environment.
(2). Chroot environment which is a isolate space and at there only available
boot entries can be found while updating grub.cfg file.

For best practice chroot option is use. In case if grub files or binary is
corrupted then it can be fix by re-installing grub package but before that
some files need to be present in that environment for that we can mount
require directories to avail those file into chroot environment.
For example we have mount the OS drive on directory /mnt/os
 #  mount -t ext4 /dev/sdb2 /mnt/os/ 


After mounting the OS drive following directories is require while
installing grub so we will point those directories inside the OS drive.
 # mount --bind /proc  /mnt/os/proc 
 # mount --bind /sys   /mnt/os/sys  
 # mount --bind /dev/pts  /mnt/os/dev/pts 
 # mount --bind /dev  /mnt/os/dev 
 # mount --bind /run  /mnt/os/run 


After mounting require partitions now it is time to enter into chroot
environment.
 # chroot /mnt/os 

 # grub-install /dev/sdb 
 # grub2-install /dev/sdb 

In case if the grub package is partial missing or installation is corrupted
then use below command but make sure Internet is available to download grub
package.
 # apt update && apt install --reinstall grub-pc -y 


Below command will create file at path /boot/grub/grub.cfg

 # update-grub 
Or
 # grub2-mkconfig /boot/grub/grub.cfg 
In some case manually path is need to mention so that the grub.cfg file can
be update.

Once the grub.cfg file is update then proceed to reboot the system to verify
the changes.
 # reboot 


 bind mount script 




Designed by SanjuD(@ngineerbabu)