Home > Technet > Booting from a VHD in Windows 7

Booting from a VHD in Windows 7

April 29th, 2010

In a previous post, I looked at the reasons one might boot from a Virtual Hard Disk (VHD).  Here I plan to go through the process of doing so.

Creating a VHD

Before your Windows 7 PC can boot from a VHD, you have to create it.  To do so you will need the following:

  • Windows 7 media
  • Free hard disk space to contain the VHD file

As with any process there are typically multiple ways to accomplish the task, however I find the Pre-boot environment the easiest way to get started.

Question: Why do I need Windows 7 loaded to use the boot from VHD feature?

Answer: The boot from VHD feature is available only in Windows 7 and it makes use of the Windows 7 boot loader application to allow the file to boot.

Before we can load Windows 7 into a virtual hard disk, like a physical hard disk it must exist.  Let’s create a VHD file.

Open a command prompt window with elevated permissions by right clicking the command prompt shortcut and selecting Run as Administrator.

On the command line enter the diskpart command to enter the built in disk partition manager.

From the Diskpart command line, enter the following to create a fixed size VHD file:

Create VDISK File=”c:\my vdisks\win7vdisk.vhd” maximum=16000 type=fixed

Figure A

The Create vdisk command

This will create a vhd file of a fixed size on the C drive at the my vidisks folder location.  Now that the VHD exists we can load Windows 7 into it.

Insert the media for the installation and reboot the computer to boot the system from the DVD.  When the boot environment starts, press Shift + F10 to open a command prompt and enter the diskpart command.

Question: Didn’t we already use diskpart command to create the VHD?

Answer: Yes we did, but now we need to attach the VHD file so Windows can use it during the installation.

Attaching a VHD file will make it visible to the computer during the startup process so you can load Windows into the VHD.  Because it works similarly to a physical computer, with the only difference being the location where the installation will be placed, on a file rather than consuming the entire physical hard disk.

To attach the VHD you will need to select the file in Diskpart by entering

select vdisk file=”c:\path_to_vhd\disk.vhd”

Where path_to_vhd\disk.vhd is the path and filename of your newly minted virtual hard disk file.

Once the file is selected, you can attach it by running the attach command in diskpart

attach vdisk

This puts the virtual hard disk onto the system so that it can be assigned a letter and get Windows installed.

Enter assign letter=X where X is the letter of the drive you want to add to the vhd

Now you can exit Diskpart and proceed with the installation of Windows 7.  When you are asked where to place the installation, select the drive letter you assigned to the VHD as the installation location.

Once the install gets rolling, it will behave just like a usual Windows installation, the next part of the process makes the VHD a bootable option for the PC.

Note: without adding the vhd instance to the boot menu, the PC will never know that it is an option.

Adding the boot option for your new VHD is fairly simple, begin by opening an elevated command prompt by entering CMD into the search box on the start menu.  When CMD appears, right click the shortcut and select run as Administrator.

Within the command window, enter the following two commands and press enter between each as follows:

bcdedit /copy {current}  /d “My Win 7 VHD” <enter>

After entering the first bcdedit command, the prompt should display a success message and the CLSID information for the new entry.  Note: You will need the CLSID data for the next command.

bcdedit /set {CLSID} osdevice vhd=”[C:]\<path to vhd>” <enter>

Note: The [C:] is required to be entered as shown, the remaining portion of the path to your vhd file should be outside of the braces.

After entering the BCDEdit commands you can exit the command prompt and reboot the system.  If everything was done correctly, you should see additional options when booting your computer.  Selecting the VHD option will boot your computer into the VHD copy of Windows 7.

Bottom Line

If you do a considerable amount of demo work or need a Out of Box experience for any reason creating a VHD boot environment is a great way to accomplish this without carting multiple systems around.  You can create multiple VHDs and configure them to boot, keep in mind though that physical hard disk space is consumed by vhd files and should be considered when planning to use multiple vhds.

My recommendation is 5 or fewer VHD boot options, one of which being your primary system.  Doing that allows you to backup everything you use within your environment by backing up a VHD.  If and when your hardware fails, you can then get the things rolling with a new PC and vhd boot.

Lots of possibilities and points of view

Windows 7 brings lots of new possibilities to the table by allowing boot from VHD and other things Microsoft has really come along way.  Hopefully you will give boot from VHD a try and post any gotchas or success stories in the comments.  Good Luck!

Comments are closed.