Pages

Saturday, November 14, 2009

Build and Configure Native Boot VHDs in Window 7


Windows 7 has the ability to run a VHD in to ways. A 32-bit or 64-bit native boot VHD can be setup to run standalone from a dual-boot to a VHD file or a 32-bit client VHD can be run simultaneously with the Windows 7 host OS. There is also a free VMWare product call VMware Player that will run a 64-bit and 32-bit VHD drive simultaneously with the Windows 7 host OS.
This article will focus on support for a native boot VHD. There are several good internet references (See Sources below) that discuss these issues. I created this article because I wanted to have a source I could reference with all the content I needed to manage my VHDs.

Native boot VHD Advantages and Disadvantages

Advantages:

  • Native Boot VHD supports all three types of VHDs - Fixed, Dynamic, and Differencing. To minimize size requirements, store User/Application Data externally to the VHD.
    • Fixed offers the best performance, closest to native, but requires the most room (full amount of space allocated) to deploy, and this could be challenging for laptop users with smaller HDDs.
    • Dynamic offers the most flexibility with the smallest footprint to deploy, as it only allocates room for the bits used. The VHD file is only as large as the actual data written to it at any given time and as more data is added/written, the VHD is dynamically grown/extended (hence the name). Use only with non-production environments.
    • Differencing is similar to Dynamic, in that only the space used is allocated to the VHD and it grows as more data is added over time. However, it is not independent like a Dynamic VHD, instead it depends on another VHD (any type, called the "parent") to be functional. Use only with non-production environments.
  • Incremental VHD files allow one VHD file to be based on another one. If you have multiple system configurations you can create a base Windows 7 VHD and make Differencing VHDs for all the others. This will save a lot of disk space.
  • The size of the VHD can be extended, in case you need more room made easier with simple tools.
  • VHD files can be copied or moved to quickly repurpose hardware for different workloads and needs.

Disadvantages:

  • A Dynamically Expanding VHD will expand to its maximum size when it is booted. This requires the physical disk volume to have sufficient disk space for the VHD to operate or it will fail to boot.
  • Only supported with Windows 7 Ultimate and Enterprise and all editions of Windows Server 2008 R2.
  • There's a performance decrease of about 3% over a Fixed and Dynamically Expandable VHD.
  • Hibernate and some BitLocker scenarios not supported but Sleep mode still works.
  • (BitLocker CAN be used within the guest VHD though, but not on the disk where the VHD resides).
  • Windows Experience index won't work.

Creating a Native Boot VHD in Windows 7

You can configure the boot manager using the Windows 7 disk management tools or I recommend using EasyBCD 2.0 Beta (Build 76 or later).
Windows 7 has simplified disk image management by adding support in the disk management tools for virtual disks. The Disk Management console has the ability to create a VHD file for either a Fixed size or Dynamically Expanding format. Without this feature you would have to use Diskpart in the command console to create and configure a native boot VHD.

Step 1: Use Disk Management to create the VHD disk

Right Click on My Computer and Click "Manage" that will open up Computer Management and then click on Disk Management.

Next, right click on Disk Management and select "Create VHD".

Figure 1: Create VHD window
This will open the Create and Attach Virtual Hard Disk window.

Figure 2: Creating a VHD using the Windows 7 Disk Management console

In this window, select the location where you want to create the VHD file. If creating a Fixed size VHD then make sure you have sufficient free space for it to be created. Next, provide the maximum disk size and press OK to format the VHD.
By default the Fixed size is recommended but you can select Dynamically Expanding if you don't want to allocate the disk space. There would be a very slight performance difference between the two formats so if you intent to copy the VHD to other locations or have space restrictions or you intend to use multiple VHDs then I would suggest using a Dynamically Expanding format.

Step 2: Initialize the VHD disk

To initialize the VHD disk, in the Disk Management console at the bottom disk partition window you will see Disk0 which is ayour host disk and Disk1 which should be your VHD disk. Right click on the Disk1 "Not Initialized" partition then select "Initialize". This will open the Initialize Disk window.


Figure 3: Creating Initialize Disk window

Select the disk 1 partition and leave the default MBR selection then click OK. The status of the Disk 1 is now changed to "Online".

Step 3: Create a new simple volume on the Disk 1

In the Disk Management console, right click on the Disk1 "Unallocated" area and select "New Simple Volume" which will launch the New Simple Volume Wizard and click on "Next".
  1. Specify the partition size, leave the default NTFS File System and select the complete size then click on "Next".
  2. Select any Drive Letter such as "W:" and click on "Next".
  3. Select the Label for the drive such as "Win7Ent64" and click on "Next".
  4. You will get a brief summary of the choices you selected. Click on "Finish" to exit.

Add a native boot menu when you install Windows 7

  1. Boot from the Windows 7 Enterprise 64 DVD.
  2. Click past the Language Selection screen.
  3. When you're on the Install Now screen (with the "Install Now" button), stop. Press "SHIFT+F10" to launch the Command Prompt.

  4. Open DISKPART by entering the following:
    >diskpart
    When "DISKPART>" appears, DISKPART is loaded.

  5. Enter the following DISKPART commands to attach the VHD for installing Window 7.
    DISKPART>select vdisk file=[c:]\path\to\vhd\file.vhd
    DISKPART>attach vdisk
    DISKPART>exit
    This will close DISKPART.
  6. Enter "exit" again or press ALT+TAB to return to the install window.
  7. Click "Install Now" and go through the install as usual, making sure to select the "Custom" option when it appears. Ignore any warnings and select the partition that matches the size that you used for your VHD. Windows.
  8. Continue with the install. When the install reboots the first time it will select the VHD without you doing anything. Windows 7 will automatically configure your boot loader for the VHD as "Windows 7". This creates two "Windows 7? options in the boot loader, but this is easily fixed later. Usually the top "Windows 7" option is the VHD.
  9. When the installation completes and it prepares to reboot, it will show you the boot loader. Select either OS and continue.

  10. Open an elevated Command Prompt (right click and select "Run as Administrator") and enter the following.
    >bcdedit /v

  11. Determine the VHD install and copy the GUID and create a new command like the following.
    >bcdedit /set {GUID} description "Windows 7 (VHD)".

  12. If you want your VHD install to appear last in the boot loader, then execute this.
    >bcdedit /displayorder {GUID} /addlast.

Using EasyBCD Beta to add a native boot VHD to the boot loader

I recommend EasyBCD 2.0 Beta to manage the boot loader. This image shows how easy it is to add a native noot VHD to the boot loader.


Figure 4: Add native boot VHD

Creating and Attaching VHD using Diskpart utility


  1. Open the Elevated Command Prompt with Administrator Privileges and type the following commands.
    >DISKPART
    DISKPART>CREATE VDISK FILE="c:\win7\win7.vhd" MAXIMUM=20000
    DISKPART>SELECT VDISK FILE="c:\win7\win7.vhd"
    DISKPART>ATTACH VDISK
    DISKPART>CREATE PARTITION PRIMARY
    DISKPART>ASSIGN LETTER=W
    DISKPART>FORMAT QUICK LABEL=Windows7
    DISKPART>EXIT
This will create the VHD file of primary partition. You will see the newly attached disk in Disk Management with Drive Letter W:

Add a native boot menu entry

When you want to configure the boot options of windows you can use bcdedit.exe or EasyBCD Beta to modify the boot settings.

  1. To use BCDEdit you start by making a copy of the origin Windows 7 boot menu.
    >bcdedit /copy {current} /d "Windows 7 (VHD)"

  2. Type the following to see the new GUID for your new copied boot option
    >bcdedit /v

  3. Make a copy of the new GUID using MARK then change the { new_GUID } pointers within the menu option so that they point to the new VHD file.
    >bcdedit /set { new_GUID } device vhd=[C:]\path\to\vhd\file.vhd
    >bcdedit /set { new_GUID } osdevice vhd=[C:]\path\to\vhd\file.vhd
    >bcdedit /set { new_GUID } detecthal on

Rename the "Windows 7" boot loader option

You will use the bcdedit command-line tool for this operation (use EasyBCD Beta). Open an elevated Command Prompt (right click and select "Run as Administrator") and type the following commands.
  1. Run BCDEDIT to display boot information.
    >bcdedit /v
    This will display a list of entries in the Windows 7 boot loader.
  2. Find the boot entry where the value of the "device" data type reads "vhd= C:\path\to\vhd\file.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 then highlight the GUID – including the curly braces – and press Enter.

  3. Type the following commands into the command prompt. Replace { GUID } below with the GUID you copied above.
    >bcdedit /set {GUID} description "Windows 7 (VHD)"
    Note that you can edit the description as you wish. This is what will appear in the boot loader.

Compact a Dynamic VHD to Reduce the File Size

Run compact to reduce the file size of a dynamic VHD. If you have formatted the VHD as NTFS (default for Windows 7) you do not need to run the Virtual Disk Precompactor that ships with Virtual Server and Virtual PC.
  1. Boot into a different Windows 7 OS partition then the one being compacted.
  2. Attach the VHD using Disk Manager then defragment your VHD.

  3. Open an elevated Command Prompt (right click and select "Run as Administrator") and type the following commands.
    >diskpart
    DISKPART>select vdisk file= C:\path\to\vhd\file.vhd
    DISKPART>compact vdisk
This may take some time to complete, but you should notice a decrease in VHD file size.
Also see my earlier article on How To Compact Virtual Hard Drives

Incremental / differencing VHD configurations

This illustrates the basic incremental / differencing VHD relationship:

  • system (parent/dynamically expanding)

    • environment (child of system/differencing)
      • task (child of environment/differencing)
Here is a typical Windows 7 incremental / differencing VHD setup.

  • Windows 7 (win7.vhd)

    • VS2008 + tools (vs2008.vhd)
      • "production" work (projectname.vhd)
      • freelance/open source work (freedev1.vhd)
      • tinkering (tinkerdev3.vhd)

    • VS2010 + SharePoint 2010 + tools (sp2010.vhd)
      • tinkering (spdev2.vhd
Optionally you may want save your "production" work to the main disk volume or a network drive.
Sources:
Windows 7 VHD and Native VHD Boot
Windows 7: Boot from VHD First Impression: Part 1
How to install Windows 7 to a VHD
How To Turn a Physical Computer Into A Virtual Machine with Disk2vhd
Windows 7 - Natively Booting from a .VHD file (Virtual PC image)
Booting Windows to a Differencing Virtual Hard Disk
How to Use the BCDEDIT Command Line Tool
Native boot from VHD on a Windows XP computer
Add a Native-Boot Virtual Hard Disk to the Boot Menu
Hyper-V How To: Shrink a VHD File
Convert virtual hard drive images from Microsoft’s VHD to VMWare VMDK
Windows 7 with BitLocker and Still Booting To VHD
VMware Player 3.0

No comments: