Unlocking the possibilities of native-boot VHD in Windows 7, Part 2

Virtual hard disks could ease development and testing. Follow these steps and links to get a Windows 2008 R2 or Windows 7 machine to boot natively to a VHD.

This is part two in a series on virtual hard disks in Windows 7. Check out part one for a discussion of the benefits of the  native-boot feature in Windows 2008 R2 and Windows 7.

The native-boot feature enables administrators to build a bootable image on a virtual hard disk and store that VHD on a computer's hard disk. The administrator can then use BCDEdit to configure the menu to boot from the desired VHD.

Native booting from VHD images offers certain advantages over virtual machines for development and testing purposes, including the following:

  • You have access to all physical hardware -- including some components like high-end graphics cards that are not available in a virtual machine.

  • The read/write performance of a VHD is surprisingly comparable to that of a physical disk, according to a TechNet blog.

  • Multiple VHD images can be loaded on a single partition.

  • Native-boot VHD also supports differencing disks that act like a snapshot, allowing you to undo configuration changes.

  • You don't need to install any applications or roles to read, modify or use the VHD. They're built into the operating system.

  • Natively supported VHDs are easier to manage than special-purpose files and applications to run them.

The following steps demonstrate how to get a physical machine to boot natively to a VHD.

On a Windows 2008 R2 or Windows 7 computer, create a VHD of the proper size.
This can be done either via the Disk Management utility or the DiskPart command-line tool. Figure 1 shows the new options in Disk Management properties, including Create VHD and Attach VHD.

Figure 1: Disk management options

Creating the VHD allows you to select the location of the VHD file on the hard disk as well as the type of disk -- dynamic or fixed, as shown in Figure 2. When complete, you must use the "Attach VHD" option and point to the VHD file, and it will show up in the Microsoft Management Console (MMC).

Figure 2: Selecting the type of virtual hard disk (click to enlarge)

Windows 7 requires only 7 GB, but make sure you add extra space for applications because VHDs are not expandable.

More on booting from a VHD

There are some good references on booting a Windows system from a VHD on a variety of TechNet blogs, including the three-part series First Impression. Part 1 shows how to make a VHD, Part 2 shows how to make the VHD into a boot VHD, and Part 3 shows how to copy a Windows 7 boot manager to Vista and get Vista to boot from a VHD. If you try these steps on a physical machine (as intended), make sure you can afford to lose and reinstall everything on that machine.

Another good resource is Keith Combs' blog. You can find additional helpful articles on TechNet -- just search for Windows 7 Boot from VHD.

 While dynamically expanding disks is a great way to save disk space, it can bite you if there isn't enough free disk space on the physical disk. I recommend using the fixed disk format. Obviously, this will require a lot of hard disk space.

You can also use DiskPart to perform all the actions in the user interface so VHD creation is scriptable.

Create and apply the Windows image to the VHD.
While you cannot run setup from an installation DVD and point it to the VHD to install, you can use the Imagex.exe utility to apply a WIM image to the VHD. To get Imagex.exe, you can download the WAIK (Windows Automated Installation Kit) ISO from Microsoft, burn a DVD and install it, then extract Imagex.exe to a convenient folder. Alternatively, you can download it from the "Tip and Trick" website. The WIM image can be custom-built using the WAIK, or you can simply use the Install.wim file located on the Windows 7 DVD in "\Sources." The following command line can be used to perform this operation:

Imagex /apply d:\sources\install.wim 1 J:\

"D:\sources\install.wim" is the path to the WIM file (I just ran it from the DVD). The "1" is the image index of the Windows 7 version -- in this case, Enterprise. This is found in the WIM file as "<Image Index="1">" using the following command (see Figure 3): Imagex /info \install.wim

Figure 3: BCDEdit commands (click to enlarge)

You can customize the WIM file with various configurations and applications and create a library to build special-purpose VHDs. To do this:

  1. Install Windows on a separate partition on a physical disk to build the image.

  2. Configure the image with applications and other configuration settings -- even new drivers you want to test.

  3. Run Sysprep on the image (use the generalize option).

  4. Create a Windows image file using a tool such as WAIK to create a WIM file.

  5. Use Imagex or other tools to apply the image to a VHD file.

Add VHD boot entry in the boot menu with BCDEdit.
There are several BCDEdit commands needed to add the VHD as a valid boot option as shown in Figure 3.

In addition, note that:

  • In the Copy command, the "/d" is the description that will show up in the boot menu for this boot option.

  • In the "/set" command, obtain the GUID by going to the command prompt and entering BCDEDIT (no options). The GUID will be listed as the "Identifier" in the existing boot option. Just use cut and paste to extract it into the BCDEDIT line.

  • The "Device" and "OSDevice" options specify the path to the VHD.

  • The "detectHal" must be set to On.

  • You can change the description of the boot option with the "description" option in the /set command as shown in Figure 4.

  • Enter "BCDEDIT" at the command line with no options to see the results of the edit.

Figure 4: Changing the description of the boot option (click to enlarge)

Caution: There are a couple of "gotchas" with native VHD boot. It currently does not support BitLocker to encrypt the host partition, and the VHD does not support hibernation (but sleep mode is supported). You cannot boot over Server Message Block (SMB) shares, and you currently can't store a VHD boot image on an external USB drive. Also, the partition hosting the VHDs cannot be part of a volume snapshot, and VHD files can't be nested --you can't have a VHD in a VHD.

Again, this outline is not intended as a step-by-step set of instructions, but the basics are there -- your mileage will vary. There are a number of other options, including Wim2VHD.scr or Microsoft's WIM2VHD command-line tool that permits creation of sysprepped VHD images.

Remember to use a test machine that doesn't have data on that you want to keep, at least until you get the process down or get a good backup. Also, you'll need lots of local disk space, so expect to use 15 GB to 20 GB per VHD depending on what you have on it. Use a fixed disk rather than a dynamic one, because a dynamic disk will expand on boot, and if you run out of disk space, it will crash. This will definitely be fun to experiment with.

Gary Olsen is a systems software engineer at Hewlett-Packard in Global Solutions Engineering. He authored Windows 2000: Active Directory Design and Deployment and co-authored Windows Server 2003 on HP ProLiant Servers. Olsen is a Microsoft MVP for Directory Services and formerly for Windows File Systems.

Dig Deeper on Virtual desktop management