Saturday, June 20, 2009

Native OS Boot from VHD: The Windows 7 Way

Those of us that need to use a server OS from what’s normally perceived as a client (e.g., demonstrating a Windows Server feature or application on our notebook during a breakout session) have had to work with different ways to achieve it, and none were all that convenient.

Usually it entailed taking a SWAG* at the amount of disk space you needed for your primary OS, then splitting the volume to leave room for the second OS. In the days of small notebook disks, these estimations were precious indeed. Another option was to install a second hard drive – usually requiring you to lose the CD/DVD-ROM drive. (Hey, you usually worked rather than watched movies on the airplane anyway, yah?) The more daring of us simply installed the server OS and used it as we would a client OS, generally sacrificing device support and power management in the process. I could never quite do that; I valued the notebook-specific features too much when I traveled.

The situation has gotten a whole lot better with Windows 7. (What hasn’t?) With Windows 7 on virtualization-compliant hardware like my ThinkPad T61P, you can boot directly to a .VHD file.

What does this mean?

  • No more partition guessing. Your second (or third, or fourth) OS simply resides in a .VHD file on your primary partition.
  • It’s flexible. You can add and remove OSes as easily as moving the files around and modifying the boot menu with BCDEDIT.
  • It’s fast. I’ve not run any tests, but it sure seems to be as fast as right off the metal. I wouldn’t have known the difference from a “real” boot. (What is real any more, anyway?)
  • Your other partitions appear as  D:, E:, etc. on the VHD-booted OS. All your physical disk volume data is visible; I change the location of My Documents on the secondary OS so it points to my primary documents folder.

Steps to book a second OS from VHD

  1. Create a .VHD containing an OS of your choice. I probably wouldn’t bother with Windows XP, because if you’re running Windows 7 you can install Windows Virtual PC for free and have a bootable XP image. You can build your image on either Hyper-V or Virtual PC.
  2. Make a copy of the VHD in case something goes wrong!
  3. run SYSPREP:

    %windir%/system32/sysprep/sysprep.exe /generalize
    /shutdown

    to create an image that will customize itself according to the new hardware it finds itself on. If you don’t run SYSPREP, be sure to remove the integration add-ins from your VHD before you copy it over.
  4. Copy the VHD to your notebook.
  5. Configure your boot menu to recognize the VHD. For this, I lifted – er, “reused” -  the following from a TechNet article:

To add a native-boot VHD to an existing Windows 7 boot menu

If you are deploying the VHD to a computer with an existing Windows 7 or Windows Server® 2008 R2 installation, you can use the BCDedit tool to make the new VHD bootable and add it to the boot menu. For more information about using the BCDedit tool, see this Microsoft Web site.

Note : Before you begin, you can back up your BCD store using the BCDedit tool with the /export option. For example, at a command prompt, type: bcdedit /export c:\bcdbackup

  1. Copy an existing boot entry for a Windows 7 installation. You will then modify the copy for use as the VHD boot entry. At a command prompt, type:

    bcdedit /copy {default} /d "R2 VHD Boot"

    Where “R2 VHD Boot” is what will appear in the boot menu. When the BCDedit command completes successfully, it returns a {GUID} as output in the Command Prompt window.

  2. Locate the {GUID} in the command-prompt output for the previous command. Copy the GUID, including the braces, to use in the following steps. (I recommend enabling QuickEdit in the window’s properties so you can easily cut and paste the GUID. )
  3. Set the device and osdevice options for the VHD boot entry. At a command prompt, type:

    bcdedit /set {guid} device vhd=[c:]\VMs\windows7.vhd
    bcdedit /set {guid} osdevice vhd=[c:]\VMs\windows7.vhd

    or whatever you’ve named the VHD. Note the syntax for the VHD location.

  4. Set the boot entry for the VHD as the default boot entry. When the computer restarts, the boot menu will display all of the Windows installations on the computer and boot into the VHD after the operating-system selection countdown completes. At a command prompt, type:

    bcdedit /default {guid}

    (Being conservative and not familiar with BCDEDIT, I went into the System properties on my Windows 7 boot and made sure that Windows 7 was the default, in case something went horribly wrong.)

  5. Some x86-based systems require a boot configuration option for the kernel in order to detect certain hardware information and successfully native-boot from a VHD. At a command prompt, type:

    bcdedit /set {guid} detecthal on
    (I did this on my x64 system too.)




That’s it. You should now be able to shut down and reboot into your secondary OS. It will complete its installation from the sysprep, and you’ll be ready to go.



There’s one more really useful step if your secondary OS is Windows 2008 R2. Even though it’s from the same code base as Windows 7, R2 doesn’t seem to automatically recognize range of hardware. So, when you open up Device Manager (devmgmt.msc from the Run menu for the lazy like me) you’ll probably end up with a collection of yellow exclamation points for hardware that isn’t recognized. You can correct most of these errors with this trick: Right-click on them, choose Update Drivers, and point the update location to d:\windows\system32\DriverStore\FileRepository. This is where the Windows 7 drivers for your hardware are stored. If the hardware works on Windows 7 you can make it work on R2 with this trick. You can also update drivers that did load on R2 – but with the generic Microsoft drivers -  with the manufacturer-specific drivers automatically downloaded from Windows Update while you were booted in Windows 7.



* Scientific wild-assed guess.


** Update ** Boot from VHD will only be available in Windows 7 Ultimate Edition.







3 comments:

Unknown said...

If only it worked for Linux too...

Anonymous said...

"Create a .VHD containing an OS of your choice..." Only if you want to waste your time. The only 'choice' you have is Windows 7 or Server 2008 R2. What a joke! If you're a savvy home user trying to dual boot your old system this isn't for you. We're still stuck with Virtual PC and related flavors, or a traditional physical dual-boot scenario.

Sean said...

Alas, it's true. When I came across this I didn't realize it would only support W7 Enterprise or Ultimate, or W2K8 R2.