Skip to content

Restoring GRUB

A common question on many Linux forums is something like “I’ve just installed [SomeOtherOS] and now I can’t boot into my OS. How can I get my choice back?” Without getting into all the sticky details, the short answer is that the GRUB boot record has been overwritten and you need to restore it.

GRUB (the GRand Unified Bootloader) is the bit of code that gets loaded when the computer first boots, and gives you the choice of which operating system to boot. It does its magic via a small file written to a special area on your hard drive that gets loaded and run at boot. When you install some OSes who shall remain nameless, they assume you would never consider another OS and just overwrite this special area (the Master Boot Record) with their code. And thus you lose your carefully crafted boot menu.

But it is pretty easy to restore it. The first thing you want to do is to boot a live Linux CD. Pretty much any one will do, but I like the rescue CD from PartedMagic. Once you have booted the live CD, bring up a root terminal window (or a regular one and su as root) and begin using GRUB:

# grub
grub>  find /boot/grub/stage1
(hd0,4)

grub> root (hd0,4)
Filesystem type is ext2fs, partition type 0×83

grub> setup (hd0,4)
Checking if “/boot/grub/stage1″ exists… yes
Checking if “/boot/grub/stage2″ exists… yes
Checking if “/boot/grub/e2fs_stage1_5″ exists… yes
Running “embed /boot/grub/e2fs_stage1_5 (hd0,4)”… failed (this is not fatal)
Running “embed /boot/grub/e2fs_stage1_5 (hd0,4)”… failed (this is not fatal)
Running “install /boot/grub/stage1 (hd0,4) /boot/grub/stage2 p /boot/grub/menu.lst “… succeeded
Done.

grub> quit

The first line (find) shows all the partitions that have GRUB installed on them. The second line (root) sets GRUB’s root to the correct partition. The third line (setup) tells it to install the changes into the correct partition. This example shows how to write the GRUB information into the particular partition. You would do this if you already have another boot manager and just need to boot the OS in this partition and forgot to do it when you installed it.. A more typical command is to use setup (hd0), which would write it directly to the MBR, so it becomes the first menu you see when you boot up.

Now you just reboot, being sure to remove the LiveCD so your computer doesn’t boot from it again. If you did the setup (hd0) command, you should see your GRUB menu again. If you use another boot manager, your partition should once again be bootable.

7 Comments

  1. leslie

    Nice idea, but it did not work for me. Grub could not find the stage1 on my sata which boots ok.

    Ergo, Is the rule for a distribution other than fedora?

    Posted on 04-Sep-07 at 8:47 pm | Permalink
  2. Actually, this tip is distro-neutral, as it pretty much deals with grub (I’ve run this under openSUSE, Fedora and I think PCLinuxO). However, I did just see your problem, having just added my first SATA drive. I’m looking for a solution now.

    Posted on 05-Sep-07 at 5:56 am | Permalink
  3. Kristy

    Just wondering if you managed to find the solution? Am stuck with a messed up MBR on my sata drive. Thanks.

    Posted on 17-Sep-07 at 1:31 pm | Permalink
  4. I have not yet tried some things on my SATA system. I will try it today and report back.

    Posted on 18-Sep-07 at 10:32 am | Permalink
  5. Gabriel

    You have an error on the line

    grub> setup (hd0,4)

    It should be: setup (hd0) // only the disk where grub will be installed, not the partition

    Posted on 22-Sep-07 at 6:57 pm | Permalink
  6. Actually, that’s the crux of this post - whether you need to do setup (hd0,4) or setup (hd0). As mentioned in the post, you do the former if you do *NOT* want to overwrite the main boot record on the hard drive which is the case if you already have something there (another boot manager, some other GRUB install). You do setup (hd0) if you want to always first boot into this.

    Posted on 23-Sep-07 at 8:51 am | Permalink
  7. linux pro

    you need grub with root access so type “sudo grub” instead of just grub ok guys

    Posted on 03-May-08 at 11:50 pm | Permalink

Post a Comment

Your email is never published nor shared.