Repairing a Broken GRUB 2 Boot-Loader on Ubuntu

Posted on July 6, 2010 7:30 AM by Rob Williams

When Ubuntu’s Lucid Lynx (10.04) was set out to pasture this past April, it brought a couple of interesting features to the table. The most obvious might be the desktop theme overhaul, but other important changes included improved NVIDIA driver support, the removal of a default GIMP installation, a built-in music store, integrated social networking tools and much more. Oh, and who could ignore the introduction of GRUB 2, an update to the most popular Linux boot-loader around.

There’s no question that GRUB 2 brought on numerous improvements over earlier versions, but it also introduced a small downside… a relative overhaul to how things are done. There are no more menu.1st (or similar) files that keep a simple configuration, nor are all of the commands the exact same at the command-line. You might have been an expert with GRUB 0.x, but you’ll still face a small learning-curve with GRUB 2.

One of the biggest additions to GRUB 2 is its “Rescue Mode”, which I found myself having to learn over the weekend. You see, in writing my review for OCZ’s Enyo, I used my netbook to install an OS to it, and as I didn’t first remove the netbook’s hard drive (there are like SIX screws, come on!), Ubuntu ended up writing over my MBR, rendering the next boot rather humorous. I took the chance of this happening because I’ve dealt with GRUB a lot in the past. Of course, it wasn’t until that reboot when I remembered I had to deal with GRUB 2, not the GRUB 0.x that I’ve become used to.

Once I realized I wasn’t dealing with a familiar GRUB, I decided to hit up Google for some answers. Surely, there would be a good how-to on fixing something as common as a GRUB error, right? Well, I was kind of shocked to find the opposite. I looked in countless forum threads, but most dealt with GRUB 0.x, and the ones that did deal with GRUB 2 weren’t quite detailed enough – or the solutions just didn’t work for me.

I did end up finding a solution, and in the least-suspecting place… Ubuntu’s own support website. There’s an entire page dedicated to all things GRUB 2, and it’s fantastic, with lots of  information you might need, along with step-by-step instructions on how to fix an issue… including the one I found myself faced with. I’ll reiterate the steps here, and hopefully if you find yourself in a similar situation, you’ll be back to an active desktop fast.

set prefix=(hdX,Y)/boot/grub
set root=(hdX,Y)
ls /boot
insmod /boot/grub/linux.mod
linux /vmlinuz root=/dev/sdXY ro
initrd /initrd.img

Repairing GRUB 2 on Ubuntu 10.04

The first step is to run the ls command, which lists the available hard drives and partitions. For those who haven’t dealt with hard drives in this manner before, the 0 in (hd0,1) refers to the physical hard drive, which in this case is the absolute primary in the PC, while the 1 refers to the first partition. While partition numbers begin at 1, the physical hard drive numbers begin at 0. So, for the third hard drive and fourth partition, the proper choice would be (hd2,4).

It would take a strange configuration for your MBR to not be originally written to the first disk, so you’re likely to be fine with sticking with the example of (hd0,1) that I give. You’re more familiar with your configuration than I am, though, so if you want the MBR on another disk for some reason, you’ll need to adjust the number accordingly, and likewise, the boot order in the BIOS would have to compliment it.

In a normal installation, Ubuntu places its /boot folder in the same partition as the OS itself, which in most cases would result in (hd0,1). The first important commend to run is set prefix=(hdX,Y)/boot/grub (no space before /boot) which tells GRUB which hard drive and partition its folder can be found on. Second, the set root=(hdX,Y) needs to be run, and like before, the (hdX,Y) equaled (hd0,1) for me and possibly also for you.

Once both of these values are set, the simple set command mounts the folders appropriately. The how-to above next mentions to run an ls /boot command, which I can only imagine is recommended so that you can immediately see if you’ve mounted the proper drive. If so, you can continue onto the insmod /boot/grub/linux.mod command to load the proper Linux module, and then linux /vmlinuz root=/dev/sdXY ro to activate the Linux kernel executable and mount the primary partition. X refers to the drive letter (0=a, 1=b, 2=c, et cetera), while Y refers to the partition number. Generally, the first partition of the first hard drive is root, so the result would be /dev/sda1.

In the example found on the Ubuntu page, the drive is mounted with the ro switch, meaning read-only. If you’re /boot is found on the same partition as your actual OS install, like mine is, you should mount with something else, such as defaults, else the desktop might not boot properly (given it wouldn’t be able to write temporary files back to the drive).

Finally, you will need to initialize the boot-loader’s RAM disk by typing in initrd /initrd.img and finish things off with boot. At this point, Ubuntu should be booting right into the desktop. If you halt on an error, read it to make sure that there wasn’t a typo somewhere, and if there was, you’ll have to reboot and try again. If you’re certain you’re typing everything correctly, you might need more than this fix, because in most cases, this should work.

Once you’re at the desktop, don’t relax, because you’re not quite done. Up to this point, you’ve merely created a temporary boot-loader in order to get back into the OS, so you will need to make it permanent. To make it so, you need to load up a terminal, and run these two commands, sudo update-grub and sudo grub-install /dev/sdX, where X refers to the hard drive letter. In my case, that letter was “a”.

Whew, that’s a lot of work just to replace the boot-loader, isn’t it? You’d almost imagine that by now, Ubuntu’s Live CD would be able to detect a broken MBR and fix it, but hopefully that’s something to come in the future. As complicated as these steps are, at least the MBR can be fixed within minutes, and you won’t have to go through with an even more tedious OS re-install. Ugh.

I should note that while I wrote this guide with Ubuntu in mind, the steps might be exactly the same for any distro that uses GRUB 2, and in all likeliness, they probably are. I can’t say for sure since Ubuntu is the only distro I’ve used so far that has had it. And if you still find yourself truly stuck, our forums are always there for you to pose your question, so feel free to hop on over and rant away.

Source: Ubuntu's GRUB 2 Support Page

  • Cristopher Araya

    AWESOME !!!!!!!!!!!!!!!!!!!
    Thank you

Recent Tech News
Recent Site Content