Creating a Boot from VHD in Win7

In a previous post last year I explained how you can use BCDEDIT to modify the boot configuration to run your machine from a virtual hard drive built with an operating system on it. I use boot from VHD to give me the opportunity to have multiple configurations of beta and release builds of tools & technologies, and at the same time it makes it easy to have a backup I can reuse if need to go back to a previous working version of my OS. I’ve even taken the VHD from one machine to another and immediately have the same environment on new hardware. The challenge was that to build a VHD with an operating system on it meant that I needed a Server 2008 environment running Hyper-V, then use that drive as my boot from instance. Until I learned a bit more about the magic of DISKPART…

Scott Hanselman recently did a great post that shows how to use the disk partition tool (DISKPART) in Win7 to create a virtual disk, attach it and then install directly from a bootable media to it to create the VHD, without going thru Server 2008 or Hyper-V. The basic process is to take a bootable medium, such as a USB drive with the install OS on it, reboot your machine and then when the installation process is running and you’re at the point of selecting a drive to install to, do a SHIFT-F10 to open a console window and run DISKPART to create and attach the virtual drive. The command sequence looks something like this:

c:\Windows\System32> diskpart

Microsoft DiskPart version 6.1.7601 Copyright (C) 1999-2008 Microsoft Corporation.

DISKPART> create vdisk file=c:\vhd\win7.vhd type=expandable maximum=50000

100 percent completed

DiskPart successfully created the virtual disk file.

DISKPART> select vdisk file=c:\vhd\win7.vhd

DiskPart successfully selected the virtual disk file.

DISKPART> attach vdisk

100 percent completed

DiskPart successfully attached the virtual disk file.

DISKPART> create partition primary

DiskPart succeeded in creating the specified partition.

At this point you can ALT-TAB back to the installation process and you should see your new partition Then in the installation process you will see your new disk in the list of installation target options. Depending on the OS you may need to go back and run BCDEDIT to configure the boot loader to know about and offer the option of booting to your new VHD.

c:\> bcdedit /copy {current} /d “myVHD”

Copy the CSLID that is displayed and then run…

c:\> bcdedit /set {CLSID} device vhd=[C:]\vhds\vhdname.vhd

c:\> bcdedit /set {CLSID} osdevice vhd=[C:]\vhds\vhdname.vhd

c:\> bcdedit /set {CLSID} detecthal on

Enjoy!