Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Generic User Avatar
- - - - -

How to Repair or Re-install GRUB using the chroot command


  • Please log in to reply
3 replies to this topic

#1 Naught McNoone

Naught McNoone

  •  Avatar image
  • Members
  • 806 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:The Great White North
  • Local time:01:39 AM

Posted 05 January 2021 - 11:56 AM

CAUTION!
The these instructions are for older systems using IDE mode only.

 



 

 

How to Repair or Re-install GRUB using the chroot command

 
Guide Overview

The purpose of this guide is to teach you how to Repair or Re-install GRUB using the chroot command.
A typical example of the need for this is when you install or re-install Windows in dual boot system. Windows will over write your boot sector, causing you to loose GRUB.

Tools Needed


  • Live USB or Bootable CD Installation Media

  • Computer with failed or broken GRUB boot menu

Instructions


  • Step1
    Boot the computer using the Live Media and mount the Linux system partition.

Using your USB or CD media, boot the computer. DO NOT use the install option. Select the “Try Without Installing” or similar option, depending on your flavour.
 
Determine which device is your computer’s root system. It is most likely an ext4 partition. Your Windows will be on an ntfs partition. A simple method is to start gparted from the Live menu. It is pre-installed on almost all installation media.
 
If it is an SATA or IDE drive, it will appear as something similar to this /dev/sdxn where x is the drive letter, and n is the partition number. On my old computer, for example, it is /dev/sda2. Newer SSD drives may appear as something like this /dev/nvme0n1p2.
Now determine your mount point. Open a terminal on your live desktop. Most live systems will have a directory called /mnt. You can confirm this with the command ls /mnt. If /mnt does not exist, the create it with the command sudo mkdir /mnt.
You can now mount your system partition using the following command:
 
sudo mount -t ext4 /dev/sdxn /mnt
 
Substitute the correct drive letter and partition number for your installation. The -t switch tells the mount command that it is an ext4 partition. If you are typing it by hand, don’t forget the spaces!


  • Step2
    Tell grub where to find the information it needs to repair your system, using the --bind switch with the mount command. Then change your root directory.

You are going to bind the directories that grub needs to the /mnt directory. Enter the following series of commands:
 
sudo mount --bind /dev /mnt/dev &&
sudo mount --bind /dev/pts /mnt/dev/pts &&
sudo mount --bind /proc /mnt/proc &&
sudo mount --bind /sys /mnt/sys
 
You can copy and paste them as a block. The && at the end of the line tells your shell to execute the next command in line only if that command is successful.
 
Now, use the chroot command to make the /mnt directory our new root.
 
sudo chroot /mnt
 
Any commands you enter, from this point, will act directly on your computers Linux system root.


  • Step3
    Install, check, and update grub.

Enter each of the following commands, one at a time, to re-install and update grub.
 

NOTE: You are installing grub in the boot sector of your hard drive. NOT in a partition. Only use the drive letter. Do not add the partition number.

 
Normally it would be the first drive in the computer, that is /dev/sda. If you had grub installed on another drive, then you must use that drive letter.
 
grub-install /dev/sda
grub-install --recheck /dev/sda
update-grub
 
You have now reset your grub menu.


  • Step4
    Exit the chroot shell, unmount our binds and system partition, and restart the computer.

To return to your original root, simply type the exit command.
 
Then unbind the directories and unmount the system partition using the following sequence:
 
sudo umount /mnt/sys &&
sudo umount /mnt/proc &&
sudo umount /mnt/dev/pts &&
sudo umount /mnt/dev &&
sudo umount /mnt
 
NOTE: Everything is done in reverse order. We want to make sure that everything is unmounted cleanly.
 
Now shut down the computer using the power off menu from the install media. Remove the USB or CD and restart the computer.
 
Your grub menu should appear, as normal.
 
Cheers!
 
Naught


Edited by NickAu, 04 March 2021 - 10:54 PM.


BC AdBot (Login to Remove)

 


#2 Naught McNoone

Naught McNoone
  • Topic Starter

  •  Avatar image
  • Members
  • 806 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:The Great White North
  • Local time:01:39 AM

Posted 15 February 2021 - 10:26 AM

CAUTION!

 

The above instructions are for older systems using IDE mode only.

 

DO NOT USE if your system using UEFI.

 

Use the Ubuntu Boot Repair instead.

 

https://help.ubuntu.com/community/Boot-Repair

 

Ubuntu Boot Repair automates the above method, and adds support for NVME SSDs.

 

Cheers!

 

Naught



#3 sidzen

sidzen

  •  Avatar image
  • Members
  • 92 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Lat33N
  • Local time:11:39 PM

Posted 15 February 2021 - 11:28 AM

A much needed How2 for dual-booters, nowadays.

Well done!



#4 Naught McNoone

Naught McNoone
  • Topic Starter

  •  Avatar image
  • Members
  • 806 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:The Great White North
  • Local time:01:39 AM

Posted 25 February 2021 - 11:19 AM

CAUTION!

 

The above instructions are for older systems using IDE mode only.

 

DO NOT USE if your system using UEFI.

 

 

 

UPDATE TO PROCEDURE

CAN BE USED IN UEFI SYSTEMS

 

After much testing, and re-testing, on a UEFI system, I can now say that the "chroot" method IS SAFE to use with a UEFI system.

 

The caution came about after an attempt to talk someone through a system repair, via long distance phone call!

 

The issue was resolved, and was mostly due to PEBKAC errors.

 

For more information, and non-standard installations, such as RAID, refer to the Ubuntu Community Help Wiki.

 

 

The Following Changes Are Recommended

 

:step1: The preferred method of repair is to install the Ubuntu boot-repair on your live system. 

 

sudo add-apt-repository ppa:yannubuntu/boot-repair
sudo apt-get update
sudo apt-get install -y boot-repair && boot-repair


NOTE:  The install will not be permanent.  Next time you boot the live media, it will not be there.

 

If there is no available internet access, then you must use the "chroot" method.

 

If you are using a separate "/boot" partition, you must mount that partition, instead of your main "/" partition.

 

 

 

:step2:  Add this line to the list of virtual filesystem binds:

 

sudo mount --bind /run /mnt/run

 

 

Your bind list should look like this:

 

sudo mount --bind /dev /mnt/dev &&

sudo mount --bind /dev/pts /mnt/dev/pts &&

sudo mount --bind /proc /mnt/proc &&

sudo mount --bind /sys /mnt/sys &&

sudo mount --bind /run /mnt/run

 

 

OR

 

:step3:  You can also use the follow in lieu.

 

As an alternative to the bind list, mount the critical virtual filesystems. Run the following as a single command:

 

for i in /dev /dev/pts /proc /sys /run; do sudo mount -B $i /mnt$i; done

 

Ubuntu documentation shows that you can exit the chroot shell and reboot without unmounting the virtual filesystems.

 

The rest of the procedure remains the same, to install, update, or repair Grub2.

 

Cheers!

 

Naught


Edited by Naught McNoone, 25 February 2021 - 11:24 AM.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users