Restore GRUB (Archlinux)

Sometimes I have the need to restore GRUB as bootloader. This especially happens after a Windows installation/upgrade.

Note: this only works for grup version 1, not for grub version 2 and greater.

Anyway, I wanted to document (and share) and easy method for doing just this. Reason: I found it hard to find on the internet. If it turns out to be useful for you... then it would be nice if you leave a short (friendly) comment.

Here we go.

1. Boot linux

Well, of course we will boot Linux from a livecd. Personally I use the archlinux install cd for this, but all this is up to you! With the arch install cd at least I know that the following procedure works.

2. Set up the chroot environment

Now we need to use the originally installed linux system in order to install grub nicely. For this we use a chroot environment. Before we can start, find out what hard disk your linux system is on (use fdisk -l).

Create a directory where you would like to mount the device or partition, then mount it:

mkdir /mnt/arch
mount /dev/<device-or-partition-name> /mnt/arch

Now create the chroot environment:

cd /mnt/arch
mount -o bind /dev dev/
mount -t proc proc proc/
mount -t sysfs sys sys/

Mount other parts of your filesystem (e.g. /boot, /var, /usr...) that reside on separate partitions but which you need access to. For example:

mount /dev/<device-or-partition-name> boot/

Now chroot to your installed device or partition and define your shell:

chroot . /bin/bash

As we will be dealing with GRUB we need to ensure that our mtab file is up-to-date:

grep -v rootfs /proc/mounts > /etc/mtab

 

3. Install GRUB (again)

First we start the GRUB shell:

grub

 Now we are in the GRUB shell and we tell it where to boot from;. Let us ensure that we know where to boot from:

find /boot/grub/stage1

The answer to this will be used in the next command (example: for a /dev/sda5, the answer will be (hd0,4)):

root (hd0,4)
setup (hd0)

If this was succesfull, then exit GRUB:

quit

  and  exit the chroot environment:

  exit

 

 4. Finally

  Now make sure all changes have been saved

  sync

 and reboot to check if everything worked as planned.

 

Comments

Anonymous (not verified)

Thanks for this. I have exactly the same situation and hope this will fix it normally. I'll try  it out later, it looks genuine.
Thanks again!
-Roelof

Anonymous (not verified)

I googled for something like this and your instructions were straightforward and easy to understand.
It fixed my Grub after Windows7 for no apparent reason caused my Grub to bootloop when loading menu.lst..
 
This fixed it! 
Thannks again (will follow your posts closely now)

Alvinator (not verified)

Thank you very much for this. I had a small problem on the find step. Checking out the official GRUB ArchWiki, people who have a separate /boot partition like me have to do this instead: 

> find /grub/stage1

Take note that yo just have to omit the /boot prefix. After that, all will be fine! :)

Dennis Martinez (not verified)

This worked perfectly on my laptop with the latest Arch Linux (fully up-to-date). Thank you so much!
One small hiccup I encountered was that when I was trying to chroot to the mounted partitions, I got the following error: "chroot: cannot run command '/bin/bash': Exec format error". After a quick search I found out it was due to me using the 32-bit (i686) boot disc as the rescue disc, while I installed the 64-bit (x86-64) version of Arch on my laptop. After using the 64-bit version as the rescue disc, everything was flawless. You might want to update the blog post in case someone else encounters this :)

V. T. Eric Layton (not verified)

Patrick,
I know this is a somewhat dated post, but I wanted to add my thanks to the others here. I was playing around with LILO earlier today, but couldn't get it to do what I wanted, so I was going to chroot into my Arch and reinstall GRUB to the MBR.
Ooops! My previous chroot methods weren't working... bad block errors. :( Your tutorial fixed it for me.
Thanks a bunch! :)
~Eric

Aashish (not verified)

I use Arch Linux and I have the same problem, I installed Win 7 and now no linux bootloader. I have been trying to follow the method that you describe here and the same thing is described in the Arch Linux wiki as well. I used both ArchBang Live CD and Arch Linux installation image from USB but I cannot get to the grub shell. When ever I type grub it says cannot find command grub.

Aashish (not verified)

While my question was under review I went ahead and reinstalled everything again. I wanted to do it anyway for a pure systemd installation anyway. I will keep the link in mind if I have the same problem or need to advise others the next time.

Thanks

Rocksteady_Racer (not verified)

My Windows 7 OS crashed (thank god for having 6 different partitions). So I had to reinstall it and windows likes to take over the MBR in the process. This worked perfectly. Also FYI USB Stick Installation media is way faster for both Windows 7 from iso and Arch IMHO. Just use winusb in linux from the command line for win7 usb or dd for archlinux. Works like a cupcake.

© 2008 - 2014 Patrick Hanckmann

View Patrick Hanckmann's profile on LinkedIn