Booting Windows to a Differencing Virtual Hard Disk

Heath Stewart

Testing software can be fun and rewarding. You get to see new, upcoming features and provide valuable feedback to the developer. But as with most pre-release software, we recommend that you don’t install it on production machines. You could dedicate extra machines for testing, or even test in a virtual machine. Virtual machines are great for testing operating systems and applications, and maximizing system resource usage. You can even take snapshots of the system disk and memory states and roll back to previous states.

Besides requiring additional memory and processor time, another problem with virtual machines is that they run under a standard configuration of a basic display driver and no sound driver in Hyper-V. All your other devices attached to your machine are not always accessible, including your graphics card that provides a nice Aero glass experience in Windows Vista and Windows 7.

But by installing Windows to a virtual hard disk (VHD) – the same format used by virtual machines – and creating a differencing disk on top of that you can test multiple configurations on the same base platform. You can choose with physical or virtual hard disk to boot and also take advantage of all the system devices your machine has to offer.

Step 1 (Optional): Partition your hard disk to store your VHD files

If you already have installed Windows Vista or Windows 7 and have plenty of free space on your system partition or in another partition – almost twice the amount of the virtual partitions you want to create – you may proceed to step 2. This step is useful if you want to install only to VHD files on your machine.

You’ll need to create a partition to store the boot configuration data (BCD) as well as the virtual hard disk (VHD) files. These can exist in the same partition, but Windows will typically create separate partitions as you see in the screenshot below. This also allow you to encrypt the system partition with BitLocker drive encryption (BDE) while maintaining a boot-readable partition.

  1. Insert your Windows Vista or Windows 7 installation disk.
  2. Proceed through the installation wizard until you see the installation type selection dialog which reads, “Which type of installation do you want?” Click Custom (advanced).
  3. Select the unallocated space and click the New button. Windows installation will warn you that it will create another partition. This will appear as “System Reserved”.
  4. Select the other partition and click the Format button.
  5. Press Shift+F10 to bring up a command prompt.

clip_image002clip_image004

Step 2: Create the VHD file

You’ll create a base virtual hard disk (VHD) in a partition with enough space available to hold the base installation and the differencing disk. This should be roughly twice the amount of space you pass to the “maximum” argument in the steps below. The number you pass to the “maximum” argument is in MB. A typical Windows 7 (x86) installation with a half-dozen or so driver updates and Microsoft ForeFront took a little over 6GB before being compressed.

You’ll create an expandable VHD as the base since it won’t be modified when booting to a differencing VHD. The installation will take a little longer since the VHD will be expanded as necessary, but this will result in a small base VHD instead of wasting unused space for a fixed VHD.

For the following steps, if you’re already logged into Windows Vista or Windows 7 you’ll need to open an elevated command prompt. If you’ve completed step 1, you continue in the command prompt you already opened.

  1. In the command prompt, type the following commands. Substitute the path to the VHD with a path of your choosing. You may also specify a different size (in MB), but make sure the partition you create them in contains about twice the space you specify.

diskpart
create vdisk file=C:Win7-base.vhd maximum=65536 type=expandable
select vdisk file=C:Win7-base.vhd
attach vdisk
exit

  1. Switch back to the installation wizard and click the Refresh button.
  2. Select the new unallocated partition and click Next. You may see a warning that reads, “Windows cannot be installed to this disk. (Show details)” but can often safely ignore this.

clip_image006clip_image008

Step 3 (Optional): Update drivers and install minimal software

After the installation has finished and you have logged in, you may want to update or install additional drivers for your hardware. You might also consider installing a minimal amount of software like a virus scanner. This will yield a smaller differencing disk.

For example, with virus software there are program files that need to be installed. Incremental updates may be provided for virus definitions. By installing the virus software first before creating the differencing disk, o nly definition updates will be contained within the differencing disk.

If you have installed the same Windows version that was already installed on another partition (if any) you may also have duplicate boot entry descriptions. You can rename the VHD boot entry to be unique.

  1. Type the following in an elevated command prompt.

bcdedit /v

  1. Find the boot entry where the value of the “device” data type reads “vhd=C:Win7-base.vhd”. Copy the GUID value of the “identifier” data type. If quick edit isn’t enabled for the command prompt, right click and select Mark. Highlight the GUID – including the curly braces – and press Enter. An example GUID looks like {f7bc2aa7-8ca2-11dc-936e-edd86d023230}.
  2. Type the following commands into the command prompt. Replace {guid} below with the GUID you copied above.

bcdedit /set {guid} description “Windows 7 (VHD)”

Finally, you may wish to run Windows Update one last time before attempting to reduce the VHD file size. Be sure to reboot so that any changes made to disk after the reboot are contained within the base VHD.

Step 4 (Optional): Reduce the base VHD file size

If you wish to reduce the amount of physical disk space consumed by the base VHD, you can attempt to reduce space within the VHD and then compress it from another Windows installation. It’s interesting to note that if you are viewing the VHD file on a separate partition while booted into the VHD file, the file size will appear to be the maximum size of the VHD. If you created an expandable disk as described in these instructions that is not accurate. You will see the correct file size when booted into a different partition.

You might also wish to relocate the paging file to the physical hard disk to reduce space consumed in the base VHD and reduce changes in the differencing VHD.

  1. While booted to the VHD, click the Start button.
  2. Type “Disk Cleanup” (without quotes), right click on program shortcut, and click Run as administrator.
  3. Select the VHD drive letter and click the OK button.
  4. Check all the options under “Files to delete” and click the OK button.
  5. Click the Start button again.
  6. Type “Disk Defragmenter” (without quotes) and select the program shortcut.
  7. (Optional) You might want to click the Configure schedule button and disable automatic defragmentation. While booted into a differencing VHD, defragmenting the disk can lead to a big increase in the differencing disk size.
  8. Click the Defragment disk button and wait for defragmentation to complete.

If you have formatted the VHD as NTFS (default for Windows Vista and Windows 7) you do not need to run the Virtual Disk Precompactor that ships with Virtual Server and Virtual PC. This may help for FAT-formatted partitions, though.

After you have defragmented, reboot into a different partition. You may boot into Windows and open an elevated command prompt, or start Windows installation and open a command prompt as described in step 1. Type the following commands.

diskpart
select vdisk file=C:Win7-base.vhd
compact vdisk

This may take some time to complete, but you should notice a decrease in file size of C:Win7-base.vhd.

Step 5: Create the differencing VHD and configure the boot entry

Next you’ll create a differencing VHD that will store all the differences between what you write to the disk and the base VHD. When you install software, it’s all contained within the differencing VHD and the base VHD is not modified. You can even create multiple differencing VHDs and boot to each of them separately, or create a chain of differencing VHDs.

In the following example you’ll create a differencing VHD for installing Visual Studio 2010 Beta 2, but you could use any file name and path or install any software you like instead.

You cannot be booted into the base VHD when creating the differencing VHD. If you have rebooted into another partition with Windows Vista or Windows 7, or into a Windows installation, do so now. Open an elevated command prompt as described in previous steps.

  1. Type the following into the command prompt. Substitute the path to the differencing VHD with a path of your choosing.

diskpart
create vdisk file=C:Dev10-Beta2.vhd parent=C:Win7-base.vhd
exit

  1. Enumerate the existing boot entries. Find the boot entry where the value of the “device” data type reads “vhd=C:Win7-base.vhd”. This is the same GUID as the one you may have copied in optional step 3.
  2. Type the following into the command prompt. Replace {guid} below with the GUID you copied.

bcdedit /v
bcdedit /copy {guid} /d “Dev10”

  1. You’ll see a message printed like, “The entry was successfully copied to {f7bc2aaa-8ca2-11dc-936e-edd86d023230}.” Copy this new GUID and replace {guid} below with this new GUID when typing the following commands. The last command will set this boot entry as the default. You should not boot to the base VHD again or you will invalid the differencing VHD.

bcdedit /set {guid} device vhd=[locate]Dev10-Beta2.vhd
bcdedit /set {guid} osdevice vhd=[locate]Dev10-Beta2.vhd
bcdedit /default {guid}

clip_image010clip_image012

Step 6: Install Visual Studio 2010

After you have rebooted the machine into the new differencing VHD you can begin installing software. In this example you’ll install Visual Studio 2010. Download and run the web installer or insert your installation media and run setup.exe.

Follow the installation wizard choosing either the default installation or a custom installation. After the install is completed explore the new features of Visual Studio 2010 and enjoy.

clip_image014clip_image016

Summary

Virtual hard disks are an integral part of virtual machines, but if you want to test on real hardware booting to a VHD is a good alternative. Microsoft TechNet has a lot of details about preparing, deploying, and booting to VHDs; and the Microsoft Virtualization team blog has more great information.

0 comments

Discussion is closed.

Feedback usabilla icon