Introduction
A few weeks ago I wrote a blog post about how to use VHD files as bootable sandbox machines. In this article I describe how to actually create a VHD file that contains a bootable Windows 7 OS.
VHD in a Nutshell
Windows 7 can boot from a Virtual Hard Disk VHD file. All the files of this virtual disk are stored in one file that can be mounted as Virtual Disk and behaves almost exactly like a physical disk volume.
Features
- One file contains a bootable OS
- VHDs support incremental VHDs
Limitations
- The .VHD booted OS needs to be Windows 7, Windows Server 2008 R2 or later.
- Performance decrease of about 3%.
- Hibernation and BitLocker work only under certain circumstances.
- BitLocker can be used within the guest VHD, but not on the physical disk where the VHD file is stored.
- Windows Experience index won’t work.
- Booting into the VHD OS you can will notice a D:\ drive which is the drive of your OS that boots from the physical drive.
Virtual PC compatibility
- You can swap physically booted VHD file with Virtual PC VHD files. Just run sysprep /generalize /oobe.
- Another requirement is that the OS needs to be 32-bit because of the limitations of Virtual PC.
Naming conventions
I picked two machine names that I am going to use during this step-by-step tutorial:
- Physical Machine: Boots from real physical disk partition: “Kreutzotter7”
- VHD Machine: Boots from VHD file that resides on physical partition: “Copra7”
Content
The tutorial explains the following the procedure to:
- Create a VHD drive using the DISKPART utility
- Installing Windows 7 into the VDH drive
Preparation
Before you begin make sure you have the following things on hand:
- Windows 7 Installation DVD
- A computer that runs Windows 7 or Windows 2008 Server R2
- Optional: A spare hard drive and a screw driver.
The Procedure
Note: Remember you are messing with the boot partition and are installing Windows 7. To be precautions, you might want to practice first on a system that you easily can loose, if something goes wrong.
Now let’s get started: Boot into Kreutzotter7, or whatever your “physical” machine is called, and create a folder called c:\vhds.
Figure 1: Create folder on C:\ drive called Vhds on Kreutzotter
Now insert your Windows 7 installation DVD into your DVD drive, restart the system and boot from the DVD. At the setup screen, don’t choose “Install Now”, but press “Shift-F10” to get into command line mode.
Figure 2: Booting form Windows 7 installation DVD and press “Shift-F10” to start the command line mode
Figure 3: In command line mode
In the command prompt type:
diskpart

Figure 4: Enter diskpart
Create a new VHD file by entering in the DISKPART prompt:
create vdisk file=”D:\vhds\copra7.vhd” type=expandable maximum=20000

Figure 5: create vdisk command
The “maximum” parameter is the disk size in mega bytes and the “file” parameter specifies the name of the file that is going to hold the VHD. This command is the most critical for two reasons:
1) As you might have noticed I used the D:\ drive instead of the C:\ drive where I created the Vhds folder. This fact caused me a major headache, because I tried to create the copra7.vhd originally on the C: drive and I got an error that indicated the disk didn’t have enough space. But on the c:\ drive of my physical OS was plenty of room. I finally looked at each of the drives using the dir commands until I found the correct drive. In my case it was the D: drive.
Figure 6: Finding the correct drive letter. Trying C:\

Figure 7: Finding the correct drive letter. Trying D:\. Bingo!
2) The disk size is also important. You don’t want to make it two small, but also not two large, because during booting of our VHD OS, the machine will check for the specified disk size and fail with a blue screen, if the physical disk doesn’t have enough room for the indicated virtual disk size that was provided with the maximum parameter.
Note: For differencing VHDs you need to add an additional parameter parent=”D:\vhds\MotherCopra.vhd”.
Now select the new VHD and attach it as a physical disk.
select vdisk file=”D:\vhds\\copra7.vhd”

Figure 8: Selecting new vdisk
Now attach the vdisk using the following command:
attach vdisk
Figure 9: Attaching new vdisk
At this point switch back to the setup window and start the setup.
Figure 10: Switched back to the Windows 7 installation screen
Proceed the normal setup, but make sure you install to the correct disk (normally the last one), ignore the “Windows cannot install to this disk” warning.
Figure 11: Picking the correct partition. Hint: Look at the size when in doubt.
At next startup, you’ll see Windows 7 in the boot menu!
Figure 12: Windows 7 installation progress
Figure 13: Windows 7 installation on VHD almost done
Once the installation of Windows 7 is done, you are going to see two boot entries. In my case there are two Windows 7 lines, because my physical OS is also Windows 7. The top one is the one booting from the VHD, the bottom one is the one booting from the physical partition.
Figure 14: Two boot entries. The one on the top is the one that belongs to the VHD.
Now, if you are booting into Kreutzotter7 (the OS booting form the physical disk), you can see the copra7.vhd file. I recommend backing it up. You can use your VHD OS sandbox and if you are done with it, you can restore the original state by restoring the copra7.vhd file from your backup location.
Figure 15: Finally, here is our vhd file
Links
Here are couple of links that my article is based on. None of them is of course as complete as mine. Hence the reason I created one for my personal reference.
Booting from a VHD
Booting from a VHD (2)
Windows 7 VHD on TechNet
More links about VHD on Windows 7
Boot from VHD on Windows 7 Gotchas
Turning a Windows 7 DVD or ISO into a Bootable VHD Virtual Machine
Native VHD support
Create bootable Windows 7 VHD
Dual boot VHD video for Windows 7 and Windows 2008 Server R2
Create a bootable Windows 7 VHD (2)
Ausblick
I am really curious about differential VHDs maybe sometime I will experiment with this feature as well.