There are times when you inadvertently overwrite your Master Boot Record. The end result being that you are unable to boot into Linux. This is especially true when you are dual booting between windows and Linux OSes. Once when I was working in Windows XP, I accidentally clicked the hibernate button instead of shutdown. And windows somehow overwrote my MBR which housed the GRUB boot loader. At such times, it pays to have this cool tip at hand.
This is what you do to restore the GRUB boot loader when faced with the above problem. First you need a Linux distribution CD. If you are using Fedora (RedHat) then the first CD is sufficient. But you may also use any of the live CDs like Knoppix, Ubuntu Live CD and so on.
With Fedora CD
Boot your computer with the first CD of Fedora in your CD drive (You have to enable your PC to boot from the cdrom, which you can set in the BIOS settings). At the installation boot prompt that you get, enter the following command:
boot: linux rescue ... and press Enter. The installer will ask you a few questions like the language you would like to use, the type of keyboard etc. Then, if you have linux previously installed on your machine, the Fedora installer will automatically detect it and mount it in the /mnt/sysimage directory. Once the linux partition is mounted, you are dropped into the command shell prompt. The next step is to make your newly mounted directory the root (or parent) directory. This you do by running the chroot command as follows:
# chroot /mnt/sysimage
# _
Now you are in the shell with respect to the parent directory which is the linux partition on your harddisk.
From here, the steps needed depends on which bootloader you are using. You have to have a fair idea what is the device node of your harddisk partition housing your MBR. In most cases, it is /dev/hda if you have an IDE harddisk. But if you have a SCSI harddisk, it will be /dev/sda.
Restoring GRUB
Execute the following command :
# grub-install /dev/hda ... to install GRUB boot loader on to your MBR. And then type exit to reboot the machine. Now your GRUB boot loader is fixed.
Restoring LILO
LILO stands for LInux LOader which was the boot loader used before GRUB was developed. It is fairly cryptic and is the default bootloader prior to and in RedHat 7.0 .
Here you just type the command :# /sbin/lilo... to install the bootloader on to the MBR.If you don't have a Fedora CD, then you need not despair. You can also repair the boot loader using one of the numerous live CDs available.
Using Knoppix CD to repair the boot loader
Here you have to boot into knoppix (either GUI mode or text mode). Once you are logged in, fire up a terminal (in GUI mode) and type the following commands:
$ su -
# fdisk -l
This will list your hard disk partition information. From the listing, you can know in which partition you have linux installed. Now you have to mount the partition which contains the linux filesystem. For the sake of this discussion, let us assume it is /dev/hda3 .
# mount -t ext3 -o rw /dev/hda3 /mnt
#_
Now your linux partition is mounted at /mnt . Next you have to use the chroot command as follows:
# chroot /mnt
#_
The rest of the steps are the same as listed above for Restoring GRUB and LILO boot loaders.
Update (August 01 2007) : Many have said when they run the grub-install command after following the above steps, they get an error saying that /dev/hda is missing. The most probable reason is that when you chroot to the mount location, it does not have a copy of the device nodes on your machine. Here is a solution for that. If you have installed Linux with /boot, /usr and / in different partitions then the above method may not get the desired results and running the grub-install /dev/hda command after running chroot might give the error that there is no /dev/hda. This is because you have mounted only one of the partitions which houses '/' . And the error tells you that it cannot find the device nodes. In this scenario you can do the following :
Lets say I have Linux installed with / in /dev/hda2, /boot in /dev/hda1 with an ext3 filesystem. I boot using a Live CD as described above and then mount the partitions as follows :
Also Read :
Dual Booting Windows XP/2000 and Red Hat Fedora Core 2
Update (August 01 2007) : Many have said when they run the grub-install command after following the above steps, they get an error saying that /dev/hda is missing. The most probable reason is that when you chroot to the mount location, it does not have a copy of the device nodes on your machine. Here is a solution for that. If you have installed Linux with /boot, /usr and / in different partitions then the above method may not get the desired results and running the grub-install /dev/hda command after running chroot might give the error that there is no /dev/hda. This is because you have mounted only one of the partitions which houses '/' . And the error tells you that it cannot find the device nodes. In this scenario you can do the following :
Lets say I have Linux installed with / in /dev/hda2, /boot in /dev/hda1 with an ext3 filesystem. I boot using a Live CD as described above and then mount the partitions as follows :
# mount -t ext3 /dev/hda2 /mntRemember that you are mounting the boot partition into the boot directory inside /mnt. Now mount the proc filesystem of your liveCD as follows (This is really an optional step):
# mount -t ext3 /dev/hda1 /mnt/boot
# mount -t proc none /mnt/procFinally bind the device nodes build by your liveCD to the /mnt/dev location as follows :# mount -o bind /dev /mnt/devNow you can chroot to the /mnt location and run the grub-install command as described above.# chroot /mntPlease note: This updated method is applicable only if you have a Linux installation spread across multiple partitions. If you have installed Linux in a single partition, the first and second step works flawlessly. At least it works for me.
# grub-install /dev/hda
Also Read :
Dual Booting Windows XP/2000 and Red Hat Fedora Core 2


25 comments:
Cool...will look around some more.
Hi. I think that you forgot to mention that /usr has to be mounted for grub-install to work. grub-install looks for the data files that it needs (stage1, stage2, etc.) which are located on the /usr partition.
Another way is to simply backup it via dd=/dev/hda bs=512 count=1 and restore it on demand.
polarizers 2cent
http://www.codixx.de/polarizer.html
What a rigmarole!
I recommend Smart Boot Manager (SBM) :
http://btmgr.webframe.org/
This has an installer (runs under Win or Linux) which puts SBM into the MBR. When this is run the first time (by booting) it scans all partitions for bootable OS's and puts them onto its menu (you can edit this afterwards). The menu you get on booting thereafter will have all your OS's and also options such as Floppy, CD, the MBR's of other HDs, and the BIOS menu - nice! While you are at it, also put SBM on a rescue floppy.
SBM chains to the boot sector of the partition you select it does not load Linux : you therefore need GRUB as well, in the boot sector of your Linux /boot partition.
Any time you MBR is nuked, just boot from the SBM rescue floppy for the menu to get back to any of your OS's. Then re-run the SBM installer.
If you're in a fix right now, boot Knoppix from a CD and download and install SBM with it.
I regard GRUB as a Linux Loader (that can also chain to Windows) and is therefore not OS neutral; therefore it's proper place is in the Linux /boot partition. SBM is OS neutral.
Knoppix:
I had the problem described, however chroot
does not work correctly in a GUI, at least not
for me, so the trick is to boot in text mode
Don't forget about Loadlin! If you have one already set up
in a directory in your Windoze system, then all you ever have
to do is run this if you have any sort of boot problem,
in Win, Lin, or whatever.
Also, it is a darn good idea to have a bootable floppy on hand
befor things go awry; it can run LILO, or you can have a MS
bootable item with a batch file that runs loadlin.
Anonymous said: put SBM on a rescue floppy.
what prevents you from making a rescue floppy disk with practically any linux distribution?
I had this problem:
i installed wind XP on a hard disk having Redhat ES4. WinXP overwrited MBR. I tried from linux cd with linux rescue. it couldnt find previous linux installation.
But gave some message: all data on hda will be lost do u want to continue ? ....something like that ..i dont remmember exactly ...
plzzz help me out..........
Manu,
It could be that you unknowingly erased linux while installing windows XP on your machine. See if you have linux partitions by going into start -> settings -> control panel -> administrative tools -> computer management on windows xp.
Re: Chroot not working properly under GUI with Knoppix; the problem is knoppix mounts partitions with the noexec flag, just open a console and mount manually (after su), eg: mount /dev/hda6 /mnt/hda6 . Doesn't get any easier, hey.
I just finished installing a new Red Hat Fedora Core 1 Linux system with the default Grub loader onto a hard disk which previously had an older Red Hat 7.3 with the [then default] lilo.
I was a little too distracted by the football game (etc.) last night to bother paying much attention to the [again default] disk partitioning. When booting to the new system after the install had completed, I was rudely reminded of the old lilo MBR record when the boot choked at the infamous "LI" text in the upper left hand corner of the screen.
I followed the steps above and ran grub-install on /dev/hda (no luck), /dev/hdb still no luck, and finally /dev/hdc seemed to work. Rebooting however, I end up at the grub> prompt in text mode.
What now Kimosabi?
@anonymous 1:55 AM
Perhaps you should go to dos prompt in your windows machine and wipe out the MBR as so :
c:\> fdisk /mbr
Then using the steps I have listed in the article, reinstall grub into your MBR.
Thank you very much for the how-2. I had to install XP, and of course it overwrote the MBR...I rebooted with the Knoppix live CD and followed your tips...and everything is just like before. GREAT!!
Hey guys I tell you how much I was struggling to get my GRUB back after installing XP. Your tips and support is highly appreciable. Keep keeping those solutions pouring. You guys simple rock!!!!!!!!!!!
I posted instructions how to fix grub on any distro using a liveCD such as Knoppix or Gentoo minimal CD right here.
I also put together instructions how to install grub other places instead of MBR - when you're dual booting. Right here
This might be useful if you plan to uninstall your Linux at some point and still be able to use your Windows. I hope this is helpful. If not, can't say I didn't try :) Good luck.
Andrej
After breaking my grub-loaded Ubuntu distro during a failed XP install, I managed to **** GRUB/mbr five ways from tomorrow.
Following the instructions from the post with a Knoppix cd, the fix worked first time and everything was back to normal! Thanks guys!
I installed Mandrake 10 on a computer with Win 98, using Lilo as the boot-loader, installed on hda. After installation I couldn't access either O/S...
After much trouble including reinstalling Windows I still couldn't get straight access to Windows, but had to boot from a CD. It appears the MBR is corrupt or inaccessible..
I then had the bright idea to install Lilo in hdb, and it worked. I have no idea what mess the MBR is in, but now I can access either O/S...
I think the moral of this little story is: don't install a boot-loader in the MBR, but on another disk, or in a logical partition, that way you don't get to mess up the MBR in the first place, and Windows doesn't overwrite the info on re-installation...
Thanks very much for writing this article.
I had Fedora 5 booting with FreeBSD and Solaris, and, Windows, being the stingy beast that it is, hijacked my MBR, so I had to follow your above steps. They worked like a charm off of a Fedora 4 disc.
It would make sense to add a section using the Ubuntu Install/Live CD, no?
The problem is, when you're in X, chroot only affects the current terminal session. Thus, /dev/hd? et al aren't recreated. I will try switching to single-user and post when finished.
Thank you.. I have been fighting a boot issue with both a flash card and a hard drive.. you gave me enough infomation to correct my "learning curve"
Jim Sterling
jasiii [AT] earthlink.net
When you have a working system and it is working just fine you can backup the MBR using A Freedos boot disk. it has a backup your MBR in the Fdisk command. This file winds up being about 180 bytes so you can put it anywhere, like on your Freedos project boot disk which also has a restore MBR command in Fdisk. For those of you that are daring. Download the boot disk and back up the MBR on your dual boot system. Flush the MBR with Fdisk/mbr command. Now restore it and everything is back to normal.
Klutz the clown.
When my mbr took a dump because of an error in wine, I found a gentoo package called "mbr" which installs a command called install-mbr which did the trick when grub installation failed on it's own. After running install-mbr, i was able to successfully run grub-install and i was back up and running.
I fouled myself up by using ghost server 8.0 on a ubuntu/XPsp2 system. This post saved me. :)
I try your solution but as soon as i mount /dev/hda /mnt i dont have the /dev/hda device anymore. Therefore i can't use grub-install /dev/hda since its not a valid block device. In /dev are no hd* nor sd* drivers at all. Any ideas?
@kyron
This could be because your device nodes are not mapped to the chrooted environment. I have posted an update. Please try out the steps in the update and check out.
Cheers
Ravi
Post a Comment