2009-09-23

Booting Linux and Windows on separate drives

Normally, installing Windows isn't something I'd do. Not for friends. Not for family, and not for myself. My wife dual-boots Ubuntu and Vista on her laptop -- Vista because that's what shipped with it, and World Of Warcraft runs fine under it. She's plenty competent to keep it cleaned up, secure, and able to restore her stuff from backups if something goes wrong. She's probably better at Windows (at least Vista) than I am, and certainly doesn't need my help very often. As for me, I just didn't think I NEEDED Windows for much...

That is, until I found out how much better my employer's VPN works from Windows. It doesn't work well from MacOS, barely works under Ubuntu, and oddly, works okay under Solaris 10, but it's far from perfect. A few days ago, I logged into the VPN from the Corporate-mandated Windows XP Work PC in the office and was kind of in awe. We're talking an order of magnitude better, on a logarithmic scale. Figures, right? With all the after-hours remote work I'm finding myself doing more and more often these days, it looks like I'm installing Windows!

As a self-proclaimed Operating System Junkie, I suppose it wouldn't hurt to dabble in Windows just a little. After all, my wife's already running a game server on Win2k. What can it hurt?

The only machine I have laying around that I felt would do Windows justice is an old Dell PowerEdge tower server, which spends most of its time running Ubuntu. I didn't feel like re-partitioning or re-installing everything, so I unplugged the Ubuntu hard drive, scared up an old 20GB drive for Windows, bolted it into place, then went to town installing Windows. My goal was to move the Windows hard drive to the secondary IDE controller once installed, then figure out how to get GRUB to boot Windows.

From here, I'm assuming that:

  • You have a Linux distro installed on the first hard drive booting with GRUB
  • You have swapped the Linux hard drive out for a fresh one (also the first hard drive) and installed Windows to it.
  • Afterward, you have put both hard drives in, with Linux as the Master on the Primary IDE controller (or the first SATA drive)
First, I wanted to make sure that the BIOS saw all my hardware. At this point, my setup was like this:

hd0 - Primary Master: 80GB HDD, Linux
hd1 - Primary Slave: Optical drive (DVD±RW, etc)
hd2 - Secondary Master: 20GB HDD, Windows

Next, I made certain that Linux booted properly. This, as expected, worked just fine. I rebooted, and paused GRUB's boot process and entered CLI mode to try to boot Windows. Initially, I tried this, which I thought should work:
grub> rootnoverify (hd2,0)  # Select partition, don't mount it
grub> chainloader +1 # Calls the first sector, should be Windows loader
grub> boot # What do you think?

Starting up ...

Yeah, right. It locks up. Doesn't even try.

Reading up on the GRUB documentation, I found the map command. Score! This tricks the BIOS into swapping drives around.
grub> map (hd0) (hd2)       # Maps hd2 (as above) to hd0
grub> map (hd2) (hd0) # ... and vice versa ...
grub> rootnoverify (hd2,0)
grub> chainloader +1
grub> boot
Amazingly, map did the trick and Windows started booting. It thinks it's running on C: and that Linux is on the secondary Master. Now, to take this and make a "Windows" option in the GRUB menu. Boot into Linux and add these lines to the end of /boot/grub/menu.lst:
title          Windows
map (hd0) (hd2)
map (hd2) (hd0)
rootnoverify (hd2,0)
chainloader +1
While you're in there, you may want to look for the Timeout line as well, and increase it. I chose not to, because I'll be booting to Windows very rarely.

Then, update GRUB's configuration, since it has to write data to the boot sector on the Linux drive. On debian-based systems, it's:
$ sudo update-grub
Now, give it a reboot and make sure that both Windows and Linux boot from GRUB as expected. This little project actually went easier than I'd expected, mostly thanks to GRUB's documentation. While extensive and technical, it is well-organized.

By the way, I tested the VPN for about 9 hours today and it was rock solid the whole time. Better than I can say for the other operating systems I've tried it with. At least I got some benefit from using Windows. If only I had awesome coffee, an IBM Model M and my MX Revolution mouse at the office every day. And if I could work in my pajamas.


Now, if you'll excuse me, I need to go take a shower with concentrated chlorine bleach and a cheese grater to get rid of all this Microsoft residue.