As a change of pace, this missive is actually meant to be useful and informative. If you don’t know anything about grub or grub2 and aren’t interested in learning about bootloaders, then this article is not for you. On the other hand, if you suffer insomnia, then you may wish to keep a copy of this nearby.

Grub2 is the next generation of the Grub bootloader. Grub stands for GRand Unified Bootloader, and offers advantages over LILO and others in that it offers a ‘command line’ mode, enabling one to possibly recover from a non-booting system. Like anything though, there is a learning curve and Grub has it’s own set of commands and quirks. For several years, this article at Linux Journal has been my Grub bible, as it were.

The original Grub (aka legacy grub) is a few years old, and appearantly there were some shortcommings with it, so Grub2 is now out there. It’s available in Debian and Ubuntu repositories, among others. When you install it, legacy Grub is automatically removed. There are warnings that Grub2 is not yet ‘finished’ and all that jazz, but in my case I was having trouble with Grub so I did switch to Grub2, and that seemed to solve my problems. However, it introduced some new ones. Grub2 has a different command set, and more or less works rather differently than legacy Grub. And being so new, there is a lack of good documentation for it. I’m not attempting to write the documentation, only to explain what I was able to figure out myself, in order to rescue a non-booting system.

When you see this upon booting your system, you know you are having some Grub2 problems.

In my case, I had copied the root partition from one drive to another, then installed grub2 on the second drive, then physically moved that second drive to a new system. The problem was that the device.map that was installed with Grub2 was from the original system, and was not valid on the new system. This leaves Grub2 in a just-barely-working state.

Here is one of the main differences between Grub2 and legacy Grub. With legacy grub, at this point there would be an error that it couldn’t find/load it’s Stage2 components and you’d be stuck. With Grub2 it installs just enough into the MBR of the drive that even without finding the rest of itself, there is enough here to work with. Grub2 uses modules (similar to linux kernel modules) instead of one monolithic Stage2. In the rescue mode, there are a minimal set of modules and a minimal set of commands. Not enough to boot the system directly, but enough to (hopefuly) find the rest of the Grub2 modules then boot the system.

In my case, the available commands (you can list them by typing help) are boot, cat, dump, exit, help, insmod, ls, lsmod, rmmod, root, set, unset. Boot and exit aren’t much help at this point, if it could have booted you wouldn’t be sitting at the rescue console, and exit just drops you to whatever your BIOS’s next boot device is. Linux users will recognize the commands cat, insmod, ls, lsmod and rmmod. These do work as you would expect them to. And in this case, the first thing to do is type ls by itself – this will list the root devices (i.e. disks) that Grub2 can see – in my case, the list is:

(hd0) (hd0,1) (hd1) (hd1,1)

This list is showing that there are two physical hard drives (hd0 and hd1) and each hard drive has a single partition on it (hd0,1 and hd1,1). Where legacy Grub counted partitions from #0, Grub2 counts partitions from #1. Another important thing to note is that the order in which Grub2 names the drives does not necessarily correspond with the drive device assignments in Linux. So the first linux drive /dev/sda is not necessarily (hd0). You can figure out which drive is which though, by ‘mounting’ them and checking. The Grub2 root command ‘mounts’ the volume. Note that you want to mount the partition, not the drive. So if I issue the command root (hd0,1) to mount the partition on the first drive, Grub2 responds with:

(hd0,1): Filsystem is ext2

This is good news; the volume is there, it is the right filesystem (in my case I have formatted the volume as ext2; you might have ext3 or ext4 etc.) Still, the other volume (in my case) was also formatted ext2, so I do need to ensure this is the drive I want. To do that, I’m going to use the ls command again, but this time I’m going to give it some parameters – namely, I want to list the files in the /boot/grub folder. If you give ls a directory that doesn’t exist you will get an error message (file not found). In this case, the command ls /boot/grub does return with the correct output. (I won’t type it all out here, suffice to say there’s a few dozen files.) If you further want to verify, you can use the cat command. In this case I can cat /boot/grub/grub.cfg to display the contents of the grub configuration file, and verify fully that I’ve got the right volume.

At this point, we have the correct volume ‘mounted’ so we now need to load the kernel and (optionally) an initram image. Except, our grub2 console doesn’t have the commands to do that… so what do we do? We need to load the correct Grub2 module. Here’s where documentation would help, if there is any, if I could find any. Of all the dozens of modules in /boot/grub, the only one you need is _linux.mod – and you activate it with the insmod command:insmod /boot/grub/_linux.mod

If it works, there will be no feedback, just a new grub rescue> prompt. If it doesn’t work, you’ll get an error message. Now if you use the help command again, you will see there are two more commands available: initrd and linux (in legacy Grub, the kernel was loaded with the kernel command; in Grub2 the command is now linux).

At this point we can go ahead and load the kernel – if you require any kernel options, be sure to include them on the command line. In my case the full command is:linux /boot/my_fmlinuz vga=785 ROOT=/dev/sda1 quiet
The only part of that which is required is the linux kernel itself, the /boot/my_fmlinuz part. Yours will be different of course. The rest of that command is unique to my setup and you may have different requirements. If successful, Grub2 will respond with some feedback. For me, it returned:

[Linux-bzImage, setup=0x2e00, size=0x1e5c30]

Your results will probably not be identical, but as long as you don’t get an error, you have the kernel loaded. Next, if you use an initrd image, load that with the initrd command. In my case: initrd /boot/my_ramfs.img. Again your command will differ, you need to use the correct initrd file name for your system. If the initrd command works, there is no feedback. Otherwise there is an error.

Incidentally, you can list the various files in your boot directory with the command ls /boot. Generally speaking, initrd files will usually have a .img suffix, and the kernel may contain the word linuz (not linux). On Ubuntu installations for example, they make it even easier – the kernel is symlinked to /vmlinuz and the initrd file is symlinked to /initrd.img so you don’t even have to go into the boot directory. You could just use the commands linux /vmlinuz and initrd /initrd.img to load them.

Now, we have the kernel loaded, and we have the initrd loaded, finally we can just issue the boot command, and it should work!

Assuming everything does work correctly and you get into linux, it would be a good idea to repair whatever went wrong with Grub2 in the first place. You can do this by verifying that the device.map is correct then issuing the grub-install command. For more information on this, read the manual: man grub-install

I hope this document has been of some use to someone — I sure could have used it yesterday!

21 Comments

Your article was way more helpful than the official GRUB 2 manual. The knowledge that I needed to load more modules in order to get more commands was essential – they should really make “help” print this if minicmd.mod isn’t loaded…

I guess with more users installing ubuntu 9.10 you get more traffic on this article.
In my case, ubuntu 9.10’s grub2 didn’t worked well after several tryouts. Now I try your tips. Curious about if it will help.

The info is very convincing, but it did not work in my present situation, which the grub rescue shell recognized no commands. Not even the HELP command in Kubuntu 9.10, I landed in this condition after inserting a USB thumb drive and then installed Kubuntu inside the thumb drive treating it as a USB harddisk. The USB thumb drive became /dev/sda my SATA HDD became /dev/sdb. After removing USB thumb drive, I found that GRUB 2.0 ruined my Kubuntu 9.10 which was originally inside /dev/sdb SATA HDD.
And I landed in GRUB RESCUE shell when I boot without the USB Thumb drive.

So you have Grub 2 on a thumb drive, but your hard drive has Grub 1 on it?

Unfortunately my advice here is dependant on Grub 2’s support files being present on the hard drive you’re booting from, and in your case it sounds like the Grub 2 support files are on the USB drive.

That’s a bad situation – in this case I’d suggest you probably just want to re-install Grub 1 on the hard drive… or put the USB drive back in, boot from that if you can, then copy the Grub2 support files onto your hard drive.

Both HDD & USB thumb drives are Kubuntu 9.10 and should be both GRUB 2.0 I had reinstalled the ruined HDD. I had not seen rescue prompt in GRUB1 there was only a normal GRUB prompt which was obtained by pressing ‘c’. I am familiar with that one.
I saw a rescue prompt which I think was from GRUB 2, and tried your information unsuccessfully. The shell did not even support HELP command. :-Z

I installed Ubuntu long time ago then my USB ports malfunctioned. When I needed reinstallation I used the kernel and initrd to perform network installation and it worked perfectly till today. Something wipped my grub or MBR (i’m not that experienced) and when i start the pc it enters “grub rescue” mode. I still have the kernel and initrd on the drive, but without the .mod file I cannot use the commands to load the needed files and perform the installation as I usually do. I guess the only thing remaining is to mount my HDD on another pc with working USB or CD ROM, as yuo suggested.

I think if the MBR was wiped, then you wouldn’t even get the grub rescue prompt. You should be able to use the rescue commands to at least mount the hard drive and list what’s there; if it was just the /boot/grub directory that was lost then you can rebuild it by mounting the drive on another computer.

Another option, if the computer has an empty PCI slot would be to install a USB i/o card, that should give you working USB ports. Either that or put a CD or DVD ROM drive in it if there’s room; either option would cost less than $20.

I already have such card, however this device is not active during boot. Ant there is no such device to enable in the BIOS Boot order. I will get IDE to USB converter and attach the HDD to another computer, from which I will recover the GRUB.

My problem is that i follow a
stupid tutorial on removing ubuntu using window partition program… they didnt mention nothink about any bootloaders or nothink lik that….. so i followed this and removed the ubunt and upon my next boot up i got grub rescue.

You mention you were trying to remove Ubuntu – that suggests that either you want to replace Ubuntu with something else, or you have something else on the same hard drive that you want to use.

If you are replacing Ubuntu with something else, can’t you just go ahead and install the new thing? It should write its own bootloader when it is installed.

On the other hand, if you have something else on that hard drive, that you want to get back to, then you just need to get a bootloader to point to that other thing. If this is the case, then you’d have to refer to the documentation for whatever that other thing is. Eg. if it’s Windows, you can use a Windows rescue disk, and IIRC there is an fdisk command line function fdisk /mbr which will put a normal dos/windows fdisk master boot record in place. Still – consult the appropriate documentation first.

Thanks, this is about the best guide I’ve read so far.
However! It doesn’t really help me… I have Ubuntu natty installed on a partition on an external hard drive, which I use successfully on an old laptop. However, I used to be able to use it to boot Ubuntu on my wife’s new netbook (which is W7 with super hardware and 3d card etc) when I needed.
After a super crash I had to start from scratch this morning with a fresh install, works fine on my laptop but errors out to
grub rescue>
on the netbook… which seems to make sense as the device maps must be different, but how did I have it working before? It definitely worked before, on Maverick, and on a fresh install of Natty.