Install or Re-Install Grub using Live-Cd

On occasion I have had to install grub from a Live-Cd when something has gone wrong or I have forgotten to install it during an OS install, below is the method I use to get it back.

I start by running a Live-Cd on the machine that need grub installing, and open a terminal, I use Q4OS for this but these instructions should work with almost any Live Distribution.

First we need to find out the root partition, this can be done by using the blkid command, you will need to be root to run this or if you have sudo installed you can use that.

sudo blkid

This should give you a listing of the partitions available on your machine something similar to the below example but probably with less partitions as I have 5 different OS’s installed here and 3 DATA partitions (I’m a complex guy 🙂 )

/dev/sda1: LABEL="Scorpion" UUID="0ad7f291-160b-44e9-b3e2-8df28cfdb18c" TYPE="ext4" PARTUUID="00123d72-01"
/dev/sda2: LABEL="DATA" UUID="1a5bdd45-51fc-4488-88ed-885e2c500a5c" TYPE="ext4" PARTUUID="00123d72-02"
/dev/sda3: UUID="871905ef-0abf-4ad7-a031-6ee627d4c761" TYPE="swap" PARTUUID="00123d72-03"
/dev/sda5: LABEL="DATA01" UUID="4c787233-2d71-4dbf-a37e-4634838335e6" TYPE="ext4" PARTUUID="00123d72-05"
/dev/sda6: LABEL="Centaurus" UUID="ad4fe825-8785-45c6-8ebb-1c6503d1a6cd" TYPE="ext4" PARTUUID="00123d72-06"
/dev/sda7: LABEL="DATA02" UUID="eaddd9e0-2685-49e3-9426-131032c96842" TYPE="ext4" PARTUUID="00123d72-07"
/dev/sda8: LABEL="Stretch" UUID="2475303f-90c8-471f-b6fa-2a32cdee1af9" TYPE="ext4" PARTUUID="00123d72-08"
/dev/sda9: LABEL="Buster" UUID="bf42da01-3412-4093-9d1d-b84823b3fc4b" TYPE="ext4" PARTUUID="00123d72-09"
/dev/sda10: LABEL="OS3" UUID="76478f76-091c-4917-913a-ae243927259e" TYPE="ext4" PARTUUID="00123d72-0a"

from this I can see that I want to use the partition /dev/sda1 which is my Q4OS Scorpion installation which is the OS I place in control of grub.

With this information I can now prepare the system using the following commands

TARGET=/mnt/sda1
sudo mkdir -p $TARGET
sudo mount /dev/sda1 $TARGET
sudo mount --bind /dev     $TARGET/dev
sudo mount --bind /dev/pts $TARGET/dev/pts
sudo mount --bind /proc    $TARGET/proc
sudo mount --bind /sys     $TARGET/sys
sudo chroot $TARGET /bin/bash 

This now places me in the environment of the system located at /dev/sda1, now I can re-install grub with the following code

grub install /dev/sda
update-grub

After these two command have completed I have grub installed back into the Master Boot Record of the hard drive. Now to clean up I unmount everything I previously mounted.

sudo umount -l $TARGET/dev/pts
sudo umount -l $TARGET/dev
sudo umount -l $TARGET/proc
sudo umount -l $TARGET/sys
sudo umount -l $TARGET
sudo rmdir $TARGET

I am now ready to reboot the machine and can use it as normal.

If you are using a UEFI system you would need to adjust these instructions a little to allow for correct installation, start by getting the results of blkid again, this time you will need to look for the EFI boot partition as well as the OS partition, these are the results from my UEFI laptop

/dev/sda1: LABEL="Recovery" UUID="72A4A907A4A8CEC3" TYPE="ntfs" PARTLABEL="Basic data partition" PARTUUID="2d5ad23c-d15e-4c7d-8806-ebcd1b89ac99"
/dev/sda2: UUID="DAAC-7F84" TYPE="vfat" PARTLABEL="EFI system partition" PARTUUID="56586012-bbe4-4c51-82e3-b5f03d68c9ab"
/dev/sda3: PARTLABEL="Microsoft reserved partition" PARTUUID="8bb882e0-7637-4399-a490-ad563b107eef"
/dev/sda4: UUID="32E0B6AFE0B678A5" TYPE="ntfs" PARTLABEL="Basic data partition" PARTUUID="b9639f62-7b24-4ba5-ae72-cec821706089"
/dev/sda5: LABEL="swap" UUID="27a5826d-62ac-43cd-936b-46e26fbece33" TYPE="swap" PARTLABEL="swap" PARTUUID="b02857ea-baba-4c1c-8cd0-6af677fd8b6d"
/dev/sda6: LABEL="Scorpion" UUID="b2463b0e-2fa0-48f3-9a91-b4ab424fc72e" TYPE="ext4" PARTLABEL="Scorpion" PARTUUID="6c426d7f-f190-47e9-9fa5-66fc4287d14d"
/dev/sda7: LABEL="Centaurus" UUID="dac8f1cf-d800-4daa-8607-f4e10e913b5b" TYPE="ext4" PARTLABEL="Centaurus" PARTUUID="a65886e2-464f-419a-b80e-11418ebb2ab1"
/dev/sda8: LABEL="Stretch" UUID="14900f95-eec2-47d1-8e6a-5e396198998e" TYPE="ext4" PARTLABEL="Stretch" PARTUUID="6b846ea3-83e8-4459-a454-7f4ce74f5896"
/dev/sda9: LABEL="Buster" UUID="06af0f4b-7302-4934-8bc1-75ab732f095e" TYPE="ext4" PARTLABEL="Buster" PARTUUID="e839b40e-0e07-4fa3-ba5d-2ba914cf575b"
/dev/sda10: UUID="c48861f8-21d2-4350-9f48-41824b88231e" TYPE="ext4" PARTUUID="0b016f4f-9517-407a-83aa-e69195963170"
/dev/sda11: LABEL="DATA" UUID="756ab33a-969c-490c-b1f8-f0dd9b72e238" TYPE="ext4" PARTLABEL="DATA" PARTUUID="4ccf7884-884b-4aa3-aac6-7128ad511903"

The partitions of interest here are the EFI partition (/dev/sda2) and the OS partition (/dev/sda6) which is again Q4OS Scorpion, the following are the commands that lead to the chroot environment

TARGET=/mnt/sda6
sudo mkdir -p $TARGET 
sudo mount /dev/sda6 $TARGET 
sudo mount /dev/sda2 $TARGET/boot/efi
sudo mount --bind /dev $TARGET/dev
sudo mount --bind /dev/pts $TARGET/dev/pts
sudo mount --bind /proc $TARGET/proc
sudo mount --bind /sys $TARGET/sys
sudo chroot $TARGET /bin/bash

And the installation is a little different as there is no need to tell grub where to install

grub-install
update-grub

Again we must clean up afterwards with these commands

sudo umount -l $TARGET/dev/pts
sudo umount -l $TARGET/dev
sudo umount -l $TARGET/proc
sudo umount -l $TARGET/sys
sudo umount -l $TARGET/boot/efi
sudo umount -l $TARGET
sudo rmdir $TARGET

We can now reboot into our system normally.

When installing grub you should observe the output to verify it is using the the efi version of grub, it might simply fail if you only have the standard version of grub installed to the OS, but in that case you would not have been able to use the OS in the first place.