Home > Hardware and Software, Microsoft, Windows 7, Windows Server > Tips for Booting/Using VHDs in Windows 7

Tips for Booting/Using VHDs in Windows 7

August 6, 2009

Both Windows 7 and Windows Server 2008 R2 (aka Windows 7 Server) support booting directly from a VHD.  This is FANTASTIC, AWESOME, and other bolded, all-caps words.  For the full details, check out Hanselman’s handy post.

I’m a HUGE user of differencing disks.  My layout follows the basic structure of:

  • system (parent/dynamically expanding)
    • environment (child of system/differencing)
      • task (child of environment/differencing)
  • Windows Server 2008 R2 (2008r2.vhd)
    • VS2008 + tools (vs2008.vhd)
      • “production” work (projectName.vhd)
      • freelance/open source work (dev1.vhd)
      • tinkering (dev3.vhd)
    • VS2010 + tools (vs2010.vhd)
      • tinkering (dev2.vhd)
  • Windows 7 (win7.vhd)
    • Simulated client “a” environment (client-a.vhd)
    • Simulated client “b” environment (client-b.vhd)

The great thing is, I have a single “2008r2.vhd” and “win7.vhd” as a baseline.  A customer calls and needs a quick mockup?  I can instanciate a new development environment in moments (or quicker via PowerShell scripts).  Who really wants to walk through reinstalling the operating system again anyway?  Not me.

With that, here’s a few tips for situations I ran into building up my environment.

Q: I had a series of VHDs from [Virtual Server 2005 R2 | Virtual PC 2007 | The Interwebz] and they won’t work.
Correct.  Only VHDs from HyperV or created directly in Windows 7 or Server 2008 R2 (that R2 part is important) using DISKPART are bootable.

Q: My system will not boot after installing!  I just get a BSOD!

If you can catch the BSOD message or press F8 and turn off automatic reboot, the error reads:

“An initialization failure occurred while attempting to boot from a VHD.  The volume that hosts the VHD does not have enough free space to expand the VHD.”
What?  Huh?  We setup dynamically expanding VHDs.. why would it need all of the free space?  Well, it seems that to boot from a VHD, it expands it to full capacity (assuming with zeros because I don’t see a latency on boot-up).  If you’re like me, you probably set your “dynamically expanding disk” to a wild maximum capacity, such as 200GB.  Even if you get close to that number, it’s likely that the parent/child VHD chains are split across multiple partitions/spindles.

That’s a gotcha.

Lesson: Be prudent with how you size your VHDs.  Ensure you have room for you’re intentions, but also ensure you have enough physical capacity.

Here’s how to fix the problem without totally reinstalling your VHD.

  1. Boot into your parent operating system and attach the VHD as a partition using either DISKPART or the Disk Management GUI.
    1. select vdisk file=”d:\vm\basedrives\2008r2.vhd”
    2. attach vdisk
  2. Shrink the VHD using the Disk Management GUI (it’s just easier, trust me).  If your original maximum capacity was 200GB and you only have 150GB free, set it to 120GB or something reasonable.
  3. Use the free VHDResizer tool to trim off the excess “maximum capacity” of your newly shrunken VHD.  You can get VHDResizer here. Set the maximum size to the same size as your new partition size.
    1. VHDResizer will require you to specify a new name for the resized VHD.  After it’s done, rename the old VHD to “file_old.vhd” and the new VHD to the same as your old file to ensure the boot manager picks up the VHD.
  4. Restart and continue along with configuring your new system.
Q: The Parent Disk is Complete.  How do I create a Differencing Disk?
Creating a differencing disk is pretty easy–a few commands in DISKPART and an administrator-privilaged console window and you’re set.
Before doing any of this, be sure that you’ve defragmented and ran the precompactor in your VHD.  This cleans up the data and zeros out the free space so that it compacts nicely.  If you don’t want to install Virtual Server to get ISO image for the PreCompactor (though I recommend this just to be safe), you can download an ‘extracted’ version from eeSJae.com.  Here’s a direct link to the precompact.exe file.
  1. Using DISKPART, select your parent VHD, compact it, and create a child (differencing) disk.
    1. select vdisk file=”d:\vm\basedrives\2008r2.vhd”
    2. compact vdisk
    3. create vdisk file=”d:\vm\vs2008.vhd” parent=”d:\vm\basedrives\2008r2.vhd”
  2. Run bcdedit /v and grab the {guid} of your existing VHD boot loader.
  3. Use BCDEDIT to replace the ‘device’ and ‘osdevice’ VHD paths.
    1. bcdedit /set {guid} device vhd=[LOCATE]\vm\vs2008.vhd
    2. bcdedit /set {guid} osdevice vhd=[LOCATE]\vm\vs2008.vhd
  4. Browse (using Windows Explorer, command window, etc) to your original, newly parent VHD (2008r2.vhd in this example) and mark it as read-only for safe keeping.
  5. Reboot and load up your new differencing disk.
Quick note:  In BCDEdit, the [LOCATE] tag is super—it allows the boot loader to FIND the location of the file rather than you specifying it.  This is great if your drive letters tend to bounce around (which they will… a bit).
Be aware that the previous note that your VHDs will expand to their full size remains.  You now, however, have the static size of your parent VHD and the “full size” of your new differencing disk (which inherits the parent’s maximum size).  If your parent is 8GB and the maximum size is 120GB, you’re now using 128GB, not 120GB.  Keep that in mind as you chain differencing disks.🙂

Q: DVDs are annoying.  I can mount VHDs, why can’t I mount ISOs?

Who knows.  At least with Windows 7, we can actually BURN ISO images… much like 1999.  In either case, I recommend tossing SlySoft’s Virtual CloneDrive on your images (and your host).  It’s fast, mounts ISOs super easy, and saves a TON of time.

  1. August 7, 2009 at 2:56 am

    Hi David, thank you for a very useful Windows 7 tip, very well explained, exactly what I was looking for.

  2. Martin
    August 22, 2009 at 12:48 am

    BTW, the latest Windows Virtual PC RC for Windows 7 contains the precompactor as well. It’s an ISO image located in the “Integration Components” subfolder.

    • August 22, 2009 at 8:53 pm

      Ahh, thanks much! I hadn’t come across that–great find!

  1. No trackbacks yet.
Comments are closed.
%d bloggers like this: