Benjamin Lerner

I have a feeling the following scenario is more common than you might guess, and certainly is more frustrating than it ought to be. I had a Windows laptop, to which I wanted to add a Linux partition. There are plenty of guides available online; some of the best are here (Linux installed first) and here (Windows installed first). However, at least one piece of backup software assumes that the Windows bootloader, NTLDR, is used as the primary bootloader. So I needed to boot Windows from NTLDR, and Linux from GRUB...but in installing Linux I had wiped NTLDR and replaced it with GRUB. This how-to guides you through restoring NTLDR as the primary bootloader, with GRUB chained onto it. Evidently the Vista bootloader is less ugly than this hack, but here's how it goes:

  1. You have a computer where:
    • There are two primary partitions (a Dell recovery partition and a Windows partition)
    • There's an extended partition containing at least two logical partitions (Linux + its swap partition) as the first logical partitions.
    • GRUB was installed in the MBR of the disk, and overwrote NTLDR MBR.
    • C:\NTLDR exists, and is the Windows bootloader
    • You don't have a Windows installation disk, or you don't have an administrator password to Windows, and hence can't use the Recovery Console.
    You want a computer where GRUB is installed in the boot record of the Linux partition, NTLDR is installed in the MBR of the disk, and chains to GRUB.
  2. Boot into Linux. Run sudo grub. Within grub, run
    • find /boot/grub/stage1 <== This will tell you something like (hd0,4), which is the drive and partition number for Linux
    • root (hd0,4) <== use the same results as the previous step
    • setup (hd0,4) <== Do NOT use setup (hd0), as that overwrites the disk's MBR. Using the partition number installs it into the partition instead
    • quit
  3. Mount a drive that can be read by Windows. (In my case, I've mounted the Windows partition itself as /windows/c/.)
  4. Run sudo dd if=/dev/sda5 of=/windows/c/grub.mbr bs=512 count=1. Here, sda5 is the partition that Linux is installed in, the output file is in the root directory of the Windows partition, and the remaining arguments pull out the first 512 bytes, i.e. the bootloader, of this partition.
  5. Reboot into Windows. Run attrib -r -h -s c:\boot.ini to remove its protections. Edit boot.ini to include the line C:\grub.mbr="Ubuntu Linux". This is the main hack: it will cause NTLDR to load the specified file as a bootloader...and conveniently we've stashed GRUB into that file. It will be configured to look for the right menu.lst file in the Linux partition, because it was setup properly in step 2. Restore boot.ini's privileges with attrib +r +h +s c:\boot.ini.
  6. At this point, if you reboot, GRUB is still the bootloader. Presumably, you've placed a Windows boot entry in menu.lst before, which will cause the Windows partition's NTLDR to run...and now boot.ini contains a line that launches the faked GRUB in C:\... which has a menu entry to boot Windows, which launches NTLDR... So you have a cycle in the loaders, which is mildly amusing...
  7. Download mbrfix.exe from http://www.sysint.no/en/Download.aspx, with description at http://www.sysint.no/nedlasting/mbrfix.htm. Run mbrfix.exe /drive 0 fixmbr from a cmd.exe shell. This overwrites the MBR with the Windows MBR and points it to use C:\boot.ini.
  8. At this point, if you reboot, NTLDR is now the bootloader. It will use boot.ini that will have a Windows entry and the new Ubuntu entry, which in turn launches GRUB (from C:\grub.mbr), which can still launch Windows' NTLDR... If everything works correctly, both OSes should be bootable from their respective bootloaders.
  9. Protect grub.mbr by attrib +r +h +s c:\grub.mbr
  10. Reboot into Linux, and remove Windows' entry from menu.lst.

Contact

download vcard icon
Email (essential):
Location (likely):
Department of Computer Science, Office 309
Post (possible):
work Post-Doctoral Research Associate Brown University / Department of Computer Science / 115 Waterman Street, 4th floor / Providence, RI 02912-1910