← Back to Archives

Oh crap, I just typed 'grub-install /dev/hda1'

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

(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, then 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, 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!.
  • Disclaimer: 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?
    

How did I fix this?

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, 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.

  2. Zero out the boot sector of the Windows partition. Once again, for my system, the command was:

    dd if=/dev/zero of=/dev/hda1 bs=512 count=1
    
  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 boot disk 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 partitions.)

  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.