Thursday, February 15, 2007

Restoring the GNU Grub

Have you ever found yourself locked out of a Windows installation and had to reinstall them? If so, you have probably noticed that Windows restores the NTLDR (the NT bootloader) to the MBR and Grub disappears. In order to regain access to your GNU/Linux distribution you have to re-install grub and that can be confusing since nearly 1 out of 2 guides out there WILL NOT work (at least not the way they should)…

This happened to me just yesterday and the (manual) steps I describe below worked fine. Enough with the talking, let’s start.

There are two easy ways to reinstall/restore Grub: either automatically or manually.

The automatic way: Super Grub Disk is a LiveCD-based solution for restoring grub via a user-friendly interface. After a couple of minutes you will be asked to reboot and grub will be restored, simply and easily. This solution worked for many people I know but not for me, so I used the manual way which I describe just below.

The manual way:
  1. Get yourself a small LiveCD, I personally used Slax standard edition 5.1.8.1 but any LiveCD should do just fine.

  2. Boot using the LiveCD and mount the partition containing your distribution (if it hasn’t been already mounted by the LiveCD distro)

  3. Open a terminal window, gain root access, and type: "chroot /mnt/hd??" OR "chroot /media/hd??" where the first "?" stands for the hard disk letter and the second "?" stands for the partition number. Note that some distributions use "/media" for mounting drives, so check both "/mnt" and "/media" before attempting to manually mount the partition. If you don’t know which is the distribution partition (e.g. If you have multiple distros installed) then just navigate to the directories you mounted on the previous step until you determine which is the right one.

  4. Type "grub", a message saying "Probing devices to guess BIOS drives" should appear, just wait for a couple of minutes and soon you will see the grub prompt (it looks like "grub> _")

  5. Type "find /boot/grub/stage1". The result will be like "(hd0,5)" or perhaps "(hd0)". Keep this in mind because it is a key-text for the next steps.

  6. Type "root (hd?,?)", replace the question marks with what the output of the command above was.

  7. Now we are ready to install grub, select one of the following options depending on the place you want to install grub:
    • If you want to install grub on the MBR (recommended for most installations) type "setup (hd?)" where "?" is the result BEFORE the comma that command in step 5 returned

    • If you know what you are doing and want to install it on a separate partition, type "setup (hd?,?)" where the first "?" stands for the pre-comma output of the command of step 5 and the second "?" stands for the partition number.

  8. Type "quit" to exit the grub prompt

  9. Reboot the computer and remove the LiveCD, grub should now appear

LINKS:
GNU GRUB | Wikipedia Article - GNU GRUB | Wikipedia Article - NTLDR | Wikipedia Article - Master Boot Record (MBR) | Super Grub Disk | Slax |

EDIT: The command in step 6 had accidentally been copied in step 7 too. The guide has been fixed. The right command is "setup (hd?,?)" and not "root (hd?,?)" which should be used only in step 6. Many thanks to the people who pointed that out in their comments.

96 comments:

Anonymous said...

The procedure posted would do nothing at all! I believe in step 7 the command to use should be "setup (hd?) or setup (hd?,?) to actually do the install.

Anonymous said...

yes, previous anonymous is exectly right!
without "setup" nothing will happen :(
setup (hd?) for putting grub to MBR is necessary!

Anonymous said...

anonymous is indeed correct.
You need to follow the root command with:
setup (hd?)

I love how you arrogantly claim most other guides are wrong, but yours is too!

Anonymous said...

The technique that I use is to boot from a liveCD, chroot to the mounted disk, then use grub-install /dev/hdX or grub-install /dev/hdX#. Hasn't failed yet.

Anonymous said...

There is another way using one command:

cd /mnt && grub-install --root-directory=. /dev/hda

Subtitude "/mnt" with the folder where the root partition is mounted and and "hda" with the hard disk that contains the MBR sector.

Ram

LiGNUxER said...

Thank you all, it seems that during the re-write of the guide (the original version was lost due to a power failure...) I accidentally copied the command of step 6 under step 7 too. The guide has been fixed. :-)

@mike, I had tried many guides to fix GRUB on my own PC but most guides failed to work (even super grub disk failed to restore grub) and so I thought that posting a guide might help somebody out there. This was the reason I mentioned that "nearly 1 out of 2 guides out there WILL NOT work".

Thanks to your contribution the error has been fixed and the guide is now (hopefully :-p) error-free

Anonymous said...

Good design!

Anonymous said...

Interesting!

Anonymous said...

Interesting article, thanks!

Anonymous said...

Great to see this! Thanks!

Anonymous said...

Thanks for interesting article.

Anonymous said...

I like articles like this. Thanks!

Anonymous said...

very good!

Anonymous said...

Write something else. Thanks! Best Blog...

Anonymous said...

Well done. Keep up the great work. Best regards!

Anonymous said...

I like it a lot! Nice site, I will bookmark!

Anonymous said...

I like it a lot! Nice site, I will bookmark!

Anonymous said...

Thanks to author! I like articles like this, very interesting.

Anonymous said...

Thanks for the nice post!

Anonymous said...

fQGTZ3 You have a talant! Write more!

Anonymous said...

Dp6U8L Thanks to author.

Anonymous said...

9mtKdF The best blog you have!

Anonymous said...

972N7w Nice Article.

Anonymous said...

Wonderful blog.

Anonymous said...

Thanks to author.

Anonymous said...

Please write anything else!

Anonymous said...

Hello all!

Anonymous said...

Please write anything else!

Anonymous said...

Good job!

Anonymous said...

Good job!

Anonymous said...

tpsdJS write more, thanks.

Anonymous said...

Thanks to author.

Anonymous said...

Please write anything else!

Anonymous said...

Wonderful blog.

Anonymous said...

Hello all!

Anonymous said...

Hello all!

Anonymous said...

Wonderful blog.

Anonymous said...

Please write anything else!

Anonymous said...

Please write anything else!

Anonymous said...

Magnific!

Anonymous said...

Magnific!

Anonymous said...

actually, that's brilliant. Thank you. I'm going to pass that on to a couple of people.

Anonymous said...

Hello all!

Anonymous said...

Friends help you move. Real friends help you move bodies.

Anonymous said...

A lot of people mistake a short memory for a clear conscience.

Anonymous said...

Beam me aboard, Scotty..... Sure. Will a 2x10 do?

Anonymous said...

Save the whales, collect the whole set

Anonymous said...

Build a watch in 179 easy steps - by C. Forsberg.

Anonymous said...

Good job!

Anonymous said...

Oops. My brain just hit a bad sector.

Anonymous said...

When there's a will, I want to be in it.

Anonymous said...

actually, that's brilliant. Thank you. I'm going to pass that on to a couple of people.

Anonymous said...

Calvin, we will not have an anatomically correct snowman!

Anonymous said...

The gene pool could use a little chlorine.

Anonymous said...

Nice Article.

Anonymous said...

Lottery: A tax on people who are bad at math.

Anonymous said...

When there's a will, I want to be in it.

Anonymous said...

Friends help you move. Real friends help you move bodies.

Anonymous said...

Thanks to author.

Anonymous said...

Build a watch in 179 easy steps - by C. Forsberg.

Anonymous said...

Good job!

Anonymous said...

What is a free gift ? Aren't all gifts free?

Anonymous said...

Ever notice how fast Windows runs? Neither did I.

Anonymous said...

I don't suffer from insanity. I enjoy every minute of it.

Anonymous said...

What is a free gift ? Aren't all gifts free?

Anonymous said...

Oops. My brain just hit a bad sector.

Anonymous said...

Friends help you move. Real friends help you move bodies.

Anonymous said...

If ignorance is bliss, you must be orgasmic.

Anonymous said...

Wonderful blog.

Anonymous said...

Good job!

Anonymous said...

I'm not a complete idiot, some parts are missing!

Anonymous said...

Clap on! , Clap off! clap@#&$NO CARRIER

Anonymous said...

Change is inevitable, except from a vending machine.

Anonymous said...

Nice Article.

Anonymous said...

Lottery: A tax on people who are bad at math.

Anonymous said...

The gene pool could use a little chlorine.

Anonymous said...

actually, that's brilliant. Thank you. I'm going to pass that on to a couple of people.

Anonymous said...

Hello all!

Anonymous said...

What is a free gift ? Aren't all gifts free?

Anonymous said...

Build a watch in 179 easy steps - by C. Forsberg.

Anonymous said...

Build a watch in 179 easy steps - by C. Forsberg.

Anonymous said...

Give me ambiguity or give me something else.

Anonymous said...

If ignorance is bliss, you must be orgasmic.

Anonymous said...

640K ought to be enough for anybody. - Bill Gates 81

Anonymous said...

actually, that's brilliant. Thank you. I'm going to pass that on to a couple of people.

Anonymous said...

If ignorance is bliss, you must be orgasmic.

Anonymous said...

Hello all!

Anonymous said...

All generalizations are false, including this one.

Anonymous said...

Clap on! , Clap off! clap@#&$NO CARRIER

Anonymous said...

Please write anything else!

Anonymous said...

Give me ambiguity or give me something else.

Anonymous said...

Ever notice how fast Windows runs? Neither did I.

Anonymous said...

What is a free gift ? Aren't all gifts free?

Anonymous said...

Build a watch in 179 easy steps - by C. Forsberg.

Anonymous said...

When there's a will, I want to be in it.

Anonymous said...

C++ should have been called B