$linuxcli.in _    [commands]    [tips & tricks]    [manpage]    [ESXi CLi]    [what is my ip?]    [pingme!]    [BIOS lock code]    [Code repo]


Repair or install GRUB bootloader


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 config file grub.cfg. 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 now it is time to enter into chroot environment.

# chroot /mnt/os

Below command will write grub codes at MBR sector that is in first 448 bytes default it copies the boot files at /boot/grub directory and copy the MBR code at 512 byte starting sector of the target disk at below example the target disk is /dev/sdb

# grub-install /dev/sdb

Or

# 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/grub2/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


Below script can be use to bind require paths for preparing chroot environment.

Get into directory to download script.

# cd /root/

# wget linuxcli.in/s/bm.sh

Give execute permission to the script

# chmod +x bm.sh

Run script with -m switch to bind mount with current system's require directories which will be use for chroot environment

# ./bm.sh -m /target/directory

# chroot /target/directory

Once the work is done in chroot environment exit from the shell and use -u to unmount those bind directories.

# ./bm.sh -u /target/directory

# umount /target/directory

Last updated on 16 January 2024 3:35 pm IST