Partager via


Booting from a VHD

Hosting an OS in a virtual machine can be very useful for developers, but sometimes one pays a price in terms of performance. In Windows 7 developers can get improved performance in virtual machines by native booting a VHD. First create a Virtual PC that hosts Windows 7, then restart your computer, and finally boot directly into the VHD that contains the Windows 7 OS. When you native boot a VHD your virtual machine runs very close to the metal; performance is greatly enhanced and you have much better support for OS features that may not be available inside a standard Virtual PC. I find this very useful, particularly when working with demanding software development projects. When I boot into a VHD that contains Windows 7 and Visual Studio, I can load big projects and still see good performance while coding and debugging.

Though I’m hardly the first to do so, I wanted to share some of what I discovered while experimenting with this technology. This post is not designed as a reference. Instead I wanted to chat about my experiences, and to explain a very simple way to get up and running. Please see the Additional Resources section at the bottom of this post if you want more detailed information, or if you have trouble with the directions I provide here.

Here are a few caveats and notes to keep in mind while reading this post. The VHD native boot technology is supported on Windows 7 Enterprise, Windows 7 Ultimate or Windows Server 2008, R2. The tools mentioned in this post such as bcdboot and bcdedit ship with Windows 7. Bitlocker cannot be used either in the VHD you are hosting, or on the drive that is hosting the VHD. Hibernation is also not supported in the VHD. Windows Virtual PC requires that your CPU supports the Intel® Virtualization Technology or AMD-V™ and that the feature be turned on.

Overview

If you already know a good deal about Virtual PCs, you may find the following synopsis of this post is enough to get you started:

  1. Use the Windows 7 Virtual PC to Create a VHD that hosts one of the versions of Windows 7 mentioned above.
  2. Use the Disk Managment console(diskmgmt.msc) to map (Attach) the VHD to a drive on a machine running Windows 7.
  3. Use BcdBoot to add the drive to the boot menu by typing: bcdboot x:\windows, where is X is the drive to which you mapped your VHD.

That’s all there is to it. Now you can restart your system and boot into your VHD. The rest of this post explains this process in more depth.

How it Looks when You Boot from a VHD

On one of my machines I have created two VHD’s based on Windows 7 that I can boot into as alternatives to my main operating system. The menu I see when I start my computer is shown in Figure 1. The options in Figure 1 labeled Sadhu-B and VHD are both VHDs containing complete versions of Windows 7 Enterprise. The default option, Windows 7, is my main OS, which is not run from a VHD, but is just a normal version of Windows Ultimate.

Figure01

Figure 1: This photograph of the boot menu I see when I start my computer shows that I can choose my main OS, or boot into one of two VHDs.

Creating a VHD

Virtual PC is not a new tool, and the steps necessary to launch a window that contains a virtual OS are covered extensively in many other posts. Nevertheless, I’ll highlight the main points here just in case you need a refresher course.

Install and launch Windows 7 Virtual PC and select Create a Virtual Machine. A wizard whirs to life and guides you through the process of creating your virtual OS. You can accept most of the default values offered by the wizard, but I would consider using the advanced option on the third page of the wizard to create a speedy fixed disk. Native boot tries to expand a dynamic VHD to its maximum size, which can lead to very ugly out of disk space errors. As a result, I prefer to create fixed VHD’s between 16 and 64 GB depending on my needs.

After creating the VHD, pop your Windows 7 DVD in a drive or open an ISO file and step through the normal Windows install procedure. If you want to use an ISO file, open the Settings for the Virtual Machine you created. In the Settings dialog, click on DVD in the left pane and select your ISO in the right pane. Now double click the virtual machine to start it. Your OS will install into your VHD.

Mapping a VHD as a Drive on Your System

After building a VHD that contains an instance of Windows 7 you should close the virtual machine your created and map your new VHD as a drive on your system. I do the mapping with the Disk Management console shown in Figure 2. You can launch this tool by opening the Start menu and typing diskmgmt.msc, or by following these steps:

  • Open the Control Panel
  • Click on System and Security
  • Find the Administrative Tools section and click on Create and Format Hard Disk Partitions

Figure02

Figure 2: In this screen shot of the Disk Management console, Disk 2 is actually a VHD mapped as drive X.

To map a drive, open the Action menu shown in Figure 2 and choose Attach VHD. You will be able to browse across your system for your VHD file. When you select the file and click OK, the drive will be mapped automatically. You can right click on the drive to assign it a particular drive letter if you do not like the default letter selected by the system.

In Figure 2, you can see both the main VHD drive, which I have assigned the letter X, and a second smaller partition assigned the letter G. Windows creates this latter partition automatically when it is installed. It is used by Windows both in the normal boot process, and in the native boot from VHD process described in this post.

Add a Mapped VHD to Your Boot Menu

Once you have attached a VHD, here is how to insert the drive into the boot menu. Open up an elevated command prompt with Administrative privileges and type the following, where X is the letter for your mapped drive:

 bcdboot x:\windows

NOTE: Recall that you can open an elevated command window by choosing All Programs | Accessories, then right clicking on the Command Prompt icon and select Run as Administrator.

I should add that you can also use bcdedit to add an item to the boot menu. However, I find that the task is much easier to perform using bcdboot, so I’ve used that tool in this post.

After performing the steps outlined above, reboot your system so you can view your handiwork. You should now have the option to boot into you main operating system, or into your VHD. By default, both options will be labeled Windows 7, and the first, and default option will be the one which allows you to boot into your VHD.

Cleaning up Your System

You may wish to give a unique name to the label for your VHD, and you might also want to boot by default into your main operating system rather than the VHD. To set the default item in the boot menu to your currently running OS open an elevated command prompt and type the following:

 bcdedit /default {current}

Here is a typical session:

 C:\Windows\system32>bcdedit /default {current} 
The operation completed successfully.

The label {current} is the ID of a particular item in the boot menu. If you want to work with an item other than the current OS, you need to find the GUID that represents that item. You can do this by typing bcdedit at the command prompt with no parameters. Many lines of information will appear, but the ID for any particular item in the menu is easy to identify. For instance, here is the section that describes the boot menu item mapped to my drive X:

 Windows Boot Loader
-------------------
identifier              {cd825a48-864b-11de-b4d2-c6ca8bc0c584}
device                  partition=X:
path                    \windows\system32\winload.exe
description             Windows 7
locale                  en-us
inherit                 {bootloadersettings}
osdevice                partition=X:
systemroot              \windows
resumeobject            {cd825a47-864b-11de-b4d2-c6ca8bc0c584}
nx                      OptIn
detecthal               Yes

The first item in this excerpt from the output of bcdedit, called identifier, is the ID for this particular menu item. Notice also that the description field is set to Windows 7. Use the identifier to set the default of OS, or type the following to modify the description field:

 C:\>bcdedit /set {cd825a48-864b-11de-b4d2-c6ca8bc0c584} description "Sadhu-B" 
The operation completed successfully.

If you run bcdedit again, you will see this output:

 Windows Boot Loader
-------------------
identifier              {cd825a48-864b-11de-b4d2-c6ca8bc0c584}
device                  partition=X:
path                    \windows\system32\winload.exe
description             Sadhu-B
locale                  en-us
inherit                 {bootloadersettings}
osdevice                partition=X:
systemroot              \windows
resumeobject            {cd825a47-864b-11de-b4d2-c6ca8bc0c584}
nx                      OptIn
detecthal               Yes

Notice that the description field has been changed to Sadhu-B. This name will appear in your boot menu, as shown in Figure 1.

Summary

In this post you have learned how to boot from a VHD. This involves three simple steps:

  1. Create a Windows 7 Virtual PC
  2. Use the Disk Managment tool (diskmgmt.msc) to map the VHD to a drive on a Windows 7 machine
  3. Use BcdBoot to add the drive to the boot menu: bcdboot x:\windows

The text of this post also covered some simple techniques for cleaning up your boot menu by setting the description and default menu item. On my system, I can still boot back into my main OS and run the VHD as a Virtual PC, even after I have added it to my boot menu and booted into it one or more times.

Needless to say, I’m interested in native booting VHDs because it allows me to easily dog food different versions of Visual Studio. If you have a similar need for hosting multiple versions of an OS that have good performance characteristics, then you might find this technology valuable. Certainly I’ve found it useful, and fun, and I’ve enjoyed writing a little something about it. I’m aware that this subject relates only tangentially to my normal focus on posting about C# and Visual Studio, but I think many developers will be interested in reaping the obvious benefits of this technology.

In this post, I focused on a simple, popular technologies that make it easy for your to start booting from a VHD. You can also use Hyper V with this technology. There are a number of tools available for automating the production of VHDs, including the ImageX utility mentioned below and WIM2VHD. If you are interested in learning more, pursue some of the links shown below.

Additional References

kick it on DotNetKicks.com

Comments

  • Anonymous
    September 02, 2009
    What we really need is the ability to take that one-step further and produce a mini CD or USB stick that boots a Windows 7 VHD natively...

  • Anonymous
    September 03, 2009
    If you're still looking for a way to use VHDs inside Win 7 without the need for boot, you cannot use Virtual Server anymore like in past OSes. The real reason for that is Hard Blocks. More on that http://aboutdev.wordpress.com/2009/05/21/virtual-server-2005-and-windows-7/

  • Anonymous
    September 03, 2009
    The comment has been removed

  • Anonymous
    September 03, 2009
    The comment has been removed

  • Anonymous
    September 06, 2009
    Are you sure that's all required? I have tried this procedure on two different machines with W7 Professional. In both cases, upon bootup, the VHD will blue screen and reboot during the boot sequence (just as the Windows logo starts to come together). When doing a Safe Mode, it's the PNP system coming up. Other websites indicate the sysprep needs to be run prior to booting teh VHD.

  • Anonymous
    September 08, 2009
    Instructions are incomplete. Prior to dual-booting, you have to run sysprep. Once you dual-boot, then Windows runs through its configuration process and it works. However, once you've done that, it won't go back into VPC without re-running sysprep. Interesting feature, but far less useful if you can't switch from VPC to dual-boot at-will. Hopefully, this will get fixed in W7 SP1.

  • Anonymous
    September 09, 2009
    James, yes, I have read that you need to run sysprep, but I did not on my system and it worked fine. Perhaps I was just lucky. One thing to check though. Did you look carefully at your blue screen? I did get a blue screen once on a laptop when booting to a VHD, and found that I was simply out of memory. I rebuilt the VHD with a smaller size, and was okay. Another thing to check is to be sure your PC supports the virtual extensions.

  • Anonymous
    September 09, 2009
    Hi Charlie Thanks for the info. Never knew that was possible. I definitely intend on using it. Much appreciated. Cherio Moosa

  • Anonymous
    September 11, 2009
    James, another issue here might be the way one prepares the VHD. Note that I did a regular install from an ISO of Win 7 to my VHD when the VHD was open as a virtual machine. This might yield different results than one gets using a tool like WIM2VHD and may be why I did not need to run SysPrep. All I really know is that the above worked for me and continues to work for me. I use these VHDs nearly every day and find them very useful.

  • Anonymous
    September 12, 2009
    The blue-screen would go away too fast for me to read or capture data from it--would reboot in under a second. I tried changing the Windows settings in the VM to prevent, but it doesn't respect it. VHD was built with 512MB (machine has 4GB) and the CPU virtual extensions are enabled. I too built it using an ISO of Win 7 Pro while the VHD was open as a VM... Too bad... if I could switch between VM and VHD-Boot at-will, that would be a huge productivity gain.

  • Anonymous
    September 17, 2009
    Will this allow the OS to use all the processor cores on a multi-core CPU ? Virtual PC can use only a single core. Thanks

  • Anonymous
    December 23, 2012
    I get the following error when I bcdboot I:Windows BFSVC: Failed to set element application device. Status = [c00000bb] Any ideas? Thanks.

  • Anonymous
    January 11, 2013
    I still don't know the reason of the error. However, I was able to achieve the desired result by using the EasyBCD tool (neosmart.net/.../1)

  • Anonymous
    January 17, 2013
    I am sorry it didn't work properly. Using EasyBCD tool I was able to make the boot entries. But, while booting I got the BSOD.