How to recover from 'grub-install /dev/hda1'

written by joehall@pobox.com, (acknowledgements, (cc))

(aka, "I've written GRUB stage1 to the boot sector of my Windows partition on accident... how do I get things back to the way they were?")



Who is this page written for?

  • If you don't understand the text in the next three bullets, than this page is not written for you!
  • If you've ever written over your master boot record (MBR) with another bootloader (or while installing Windows) and attempted to reinstall GRUB (the GRand Unified Bootloader; click here for the manual), you've had to issue the command, 'grub-install /dev/hda' to write GRUB to your MBR.
  • Furthermore, if you are not a Linux expert, you could mistakenly install GRUB onto another partition on your hard drive instead of the MBR (for example, the command 'grub-install /dev/hda1' writes the GRUB stage1 to the first partition on the hard drive hda... in my case this was my Windows partition).
  • If you have done this and don't have a clue how to get your system back to the way it was before... this page is for you!.
  • This page serves as documentation of how I recovered from having made this mistake. This worked for me, it might not work for you. I and my employer make absolutely no claims as to the validity of the information on this page. Do any of what is described here at your own risk.





Symptoms of my problem:

  1. When attempting to boot into Windows, GRUB prints to the screen 'GRUB loading stage2...' and hangs. (This is happening because the small bit of code that is the stage1 of GRUB has been written to the boot sector of the Windows partition. The system files that normally boot Windows have been corrupted.)
  2. If you are lucky (and I am not), you might be able to use a Windows boot disk to copy the system files to your hard drive. In my case, every time I typed 'sys a: c:' or 'sys c:' I got back
    Invalid media type reading drive C,
    Abort, Retry, Fail?
    
    Read on about how I recovered.


How did I fix this?

(Note: if you have two hard drives, read this explanation by Don Hardaway (hardawayd@slu.edu).)

This is all relatively simple if you know what you're doing (which I usually don't... worst linux user ever). What needs to be done is basically this:

(NOTE: Jason Thomas pointed out that it might only be necessary to zero out the boot sector of the Windows partition and recopy the system files (assuming that you have GRUB installed correctly on the MBR)... this means only do numbers 2 and 4 from the following list. This avoids zeroing out the MBR (number 1) which is the most delicate part of this whole scheme.)
  1. Zero out MBR. (but not all of it!) You need to use 'dd' to erase the MBR. For my machine the command was:
    dd if=/dev/zero of=/dev/hda bs=446 count=1
    Why the 'bs=446'? Because the rest of your MBR holds the partition table for the drive... which you do not want to destroy. (note how 'hda' in this context refers to the MBR where, in general, 'hda' is the first hard drive in your system)
  2. Zero out the boot sector of the Windows partition. Once again, for my system, the commad was:
    dd if=/dev/zero of=/dev/hda1 bs=512 count=1
    This erases the boot sector of the first partition on hda.
  3. Re-write a Windows MBR. You'll need to boot into DOS with your trusty Windows 98SE OEM boot disk and do:
    fdisk /mbr
    (NOTE: I have received reports that you need to use specifically a Windows 98SE OEM ("Original Equipment Manufacturer") boot disk (boot98se.exe) as neither of the two commands sys a: c: or format C: /s seem to correctly copy the system files to the boot sector of Windows ME (and, I assume, anything later than ME) partitions. "Apparently the Windows Me disk requires Windows to be installed and detectable before it will do squat, the 98 disk ignores this and just does what we want (install the boot sector)."--reader Erich Hoover for the tip)
  4. Copy Windows system files to boot sector of Windows partition. Do:
    sys a: c:
    Which tells DOS to copy the system files from a: to the boot sector of c:.
  5. At this point your computer should be able to reboot into Windows as if you just had Windows installed. Try it by rebooting.
  6. If all has gone well, you can reinstall GRUB.
  7. Now is a good time to make copies of the boot sectors of all your partitions and your MBR. Then you can use 'dd' to replace them if anything should go wrong in the future.
  8. Pat yourself on the back... you deserve it.

Acknowledgments:

I would like to thank George Fisher, James Lloyd, and Jason Thomas for their help in resolving my problem and the eventual solution. Please email me at joehall@pobox.com for any corrections or additions to this document.

Creative Commons License
This work is licensed under a Creative Commons License.








Notes:



What's all this talk of 'stage1' and 'stage2'?



The MBR (master boot record) is a small piece of space on the beginning of the hard drive that tells your computer what to boot from (a floopy, cdrom, hard drive) and where on that media to boot from. The amount of memory in the MBR is so small that it doesn't allow for much in the way of fancy graphical environments that allow you to choose what you want to boot (remember LILO? not much in the way of graphics or features). GRUB gets around this by having a stage1 and stage2 part of the bootloading process. Stage1 is written to your MBR when you install GRUB and stage2 is somewhere on your hard drive where there is much more space to put a fancy bootloader like GRUB. Stage1 simply runs stage2... what happens then depends on how you have GRUB configured. If you did a default install while installing RedHat Linux like myself, stage1 launches a menu that you can choose which partition on your hard drive to boot from.

What if you have two drives?


This is an exact quote from Don Hardaway who was able to recover from a similar grub problem with two drives:

"I am not sure I have all of the answers but here is what I understand and what I did.

The first 512 bytes of any disk is the MBR [Master Boot Record]. My understanding is that you can write to the MBR of any disk. In my case I have hda (my first drive) and hdc (my second drive).

[Here's what happened:]

  • I installed windows to hda which promptly overwrote the MBR of hda with a windows primary bootloader making Linux on hdc nonbootable for the moment.
  • Next, I rebooted with my redhat cd and went into rescue mode.
  • It searches both disks on my laptop and found my linux partitions and mounted them under /mnt/sysimage.
  • Then I was able to chroot to /mnt/sysimage to anchor the the root partition on hdc.
  • Next I ran /sbin/grub-install /dev/hda. This overwrote the windows primary boot loader with grub.
  • I took out the cd and rebooted and the grub menu displayed with only linux as a choice.
  • I booted into linux and then edited /boot/grub/grub.conf and added another section for windows as follows:
    title window 2000
    rootnoverify (hd0,1)
    chainloader +1
    
That was it. I rebooted and now can select windows to launch on hda or linux to launch on hdc." (Thanks to Don Hardaway (hardawayd@slu.edu).) for this).

Copying system files...


  1. First you will need a Windows boot disk with the system files on it. Bootdisk.com has all the flavors of Windows and DOS.
  2. Boot into Windows with this disk and try 'sys a: c:' which tells DOS to copy the system files that reside on the floppy in the a: drive to the boot sector on drive c:.

    NOTE: if this doesn't work and you get 'invalid media type while reading drive c, abort, retry, fail?' then you can try one more thing that may or may not work (it didn't for me). You can try to issue 'fdisk /mbr' and then a 'sys a: c:'. BE CAREFUL!!! 'fdisk /mbr' will write over your MBR and anything (GRUB) on it so you better be prepared to reinstall GRUB.

How do I reinstall GRUB?



Hopefully you know how to do this but if you don't:
  1. You need to install GRUB stage1 on your MBR:
    • Boot into linux.
    • Find where the 'grub' and 'grub-install' executibles are. Change directories to where these executibles reside.
    • Do
      ./grub-install /dev/hda
      which will install GRUB on your MBR. (NOTE: if you type './grub-install /dev/hda1' you are back where you started from... this is bad)
  2. Next, you need to tell GRUB where it's pieces reside:
    • Use 'su' to become root.
    • Go to where your 'grub' executible resides.
    • type
      ./grub
    • you are now in the command-prompt version of GRUB's stage2. you need to use 'install' to tell GRUB where to look for stage1, stage2 and the menu.lst file (Here is a good article that covers menu.lst files). In my case, I have a boot partition (which is the second partition on drive c:... or (hd0,1) in GRUB lingo), so I did the following install command:
      grub> install (hd0,1)/grub/stage1 d (hd0) (hd0,1)/grub/stage2 p (hd0,1)/grub/menu.lst
      
      If the GRUB executibles (and the stage1 and stage2 executibles) are in your Linux root directory (as in '/boot/grub/') and your Linux partition is the second partition (assuming you have something like Windows or NT as the first partition) you would want to do:
      grub> install (hd0,1)/boot/grub/stage1 d (hd0) (hd0,1)/boot/grub/stage2 p (hd0,1)/boot/grub/menu.lst
      
      If your situation is different than the above (if the above install fails) then you can use the 'find' or 'cat' commands available from the GRUB prompt to look for the menu.lst file and then just change the naming conventions above to reflect where stage1, stage2 and menu.lst files are (more than likely all these files will be in the same directory).
  3. Hopefully when you reboot, the GRUB menu will come up (although Windows may not boot, hopefully again Linux will).

How do I find a file?



Use the find command... for example if you are looking for your GRUB executibles you can type
find . -name "grub-install" -print
and that will return any matches.