Tips for Booting/Using VHDs in Windows 7
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)
- environment (child of system/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)
- VS2008 + tools (vs2008.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: 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.”
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.
- Boot into your parent operating system and attach the VHD as a partition using either DISKPART or the Disk Management GUI.
- select vdisk file=”d:\vm\basedrives\2008r2.vhd”
- attach vdisk
- 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.
- 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.
- 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.
- Restart and continue along with configuring your new system.
-
Using DISKPART, select your parent VHD, compact it, and create a child (differencing) disk.
-
select vdisk file=”d:\vm\basedrives\2008r2.vhd”
-
compact vdisk
-
create vdisk file=”d:\vm\vs2008.vhd” parent=”d:\vm\basedrives\2008r2.vhd”
-
-
Run bcdedit /v and grab the {guid} of your existing VHD boot loader.
-
Use BCDEDIT to replace the ‘device’ and ‘osdevice’ VHD paths.
- bcdedit /set {guid} device vhd=[LOCATE]\vm\vs2008.vhd
- bcdedit /set {guid} osdevice vhd=[LOCATE]\vm\vs2008.vhd
- 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.
- 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).
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.
Hi David, thank you for a very useful Windows 7 tip, very well explained, exactly what I was looking for.
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.
Ahh, thanks much! I hadn’t come across that–great find!