This content is 15 years old. I don't routinely update old blog posts as they are only intended to represent a view at a particular point in time. Please be warned that the information here may be out of date.
I’ve titled this post as “Running Windows from a USB flash drive” because the same principles should be equally applicable to all Windows 7-based operating systems (and even Vista if the Windows 7 bootloader is used) but my specific scenario was based on Hyper-V Server 2008 R2.
I got this working a few hours after Windows 7, Server 2008 and Hyper-V Server 2008 R2 were released to manufacturing but I was still using release candidate code – fingers crossed it still works with the final release!
Boot from VHD is a fantastic new technology in Windows 7/Server 2008 R2 and derivative operating systems and I’ve often wondered if it’s possible to use it to run Hyper-V from a USB flash drive (just like the “embedded” version of VMware ESXi offered by some OEMs). Well, as it happens it is – and this post describes the steps I had to take to make it work.
First of all, I needed to create a virtual hard disk and install an operating system onto it. As Keith Combs noted, there are various ways to do this but only one is supported; however there is also a handy video on TechNet which takes you through the steps of creating a VHD and booting from it.
Using the TechNet video as a guide, I issued the following commands from the command prompt to create my virtual hard disk and apply an image from the Hyper-V Server 2008 R2 release candidate DVD:
diskpart
create vdisk file=driveletter:\virtualharddisk.vhd maximum=15000 type=expandable
select vdisk file=driveletter:\virtualharddisk.vhd
attach vdisk
list disk
(make a note of the disk number.)
select disk disknumber
create partition primary
select partition 1
active
format fs=ntfs quick
assign
exit
(note the drive letter for the newly mounted VHD.)
imagex /info dvddrive:\sources\install.wim
(identify the correctentry.)
imagex /apply dvddrive:\sources\install.wim /check imageindex vhddrive:\
diskpart
select vdisk file=driveletter:\virtualharddisk.vhd
detach vdisk
exit
At this point, Hyper-V Server had been imaged into my new VHD, which could then be copied to the USB flash drive.
Next, to load the VHD from the Boot Manager, I edited the boot configuration data (which is what would be required in a standard boot from VHD scenario); however, as I found later, a different set of actions is needed for booting from the USB flash drive.
bcdedit /copy {current} /d “Hyper-V Server 2008 R2”
bcdedit
(make a note of the GUID for the newly created entry.)
bcdedit /set {guid} device vhd=[usbdrive:]\virtualharddisk.vhd
bcdedit /set {guid} osdevice vhd=[usbdrive:]\virtualharddisk.vhd
bcdedit /set {guid} detecthal on
bcdedit /set {guid} description “Hyper-V Server 2008 R2”
It’s worth understanding that the use of drive letters (which are transient in nature) does not cause a problem as the BCD Editor (bcdedit.exe
) extracts the data about the partition and saves it in the BCD store (i.e. it does not actually save the drive letter).
After rebooting, Hyper-V Server loaded from my USB flash drive and ran through the out of box experience. At this stage I had Hyper-V Server running off the flash drive but only if my original Windows installation (with the boot manager) was available and, as soon as I removed the hard disk (I wanted to be sure that I was booting off the flash drive with no other dependencies), then the whole thing collapsed in a heap. Thanks to Garry Martin, I checked my BIOS configuration and made sure that USB device boots were enabled (they were not) but I then spent about a day playing around with various BCD configurations (as well as various attempts to fix my BCD with bootrec.exe
) until I stumbled on a post from Vineet Sarda (not for the first time, based on the comments that include one from yours truly a few weeks back!) that discusses booting from VHD without a native operating system.
Following Vineet’s example, I booted my system into Windows 7 (I could have used the Windows Recovery Environment), reformatted the USB flash drive before copying my VHD image back onto it, and issued the following commands:
diskpart
select vdisk file=usbdrive:\virtualharddisk.vhd
attach vdisk
list volume
exit
(note the drive letter for the newly mounted VHD.)
bcdboot vhddrive:\Windows /s usbdrive: /v
(i.e. copying the BCD from the operating system image contained within the VHD, to the physical USB drive. Note that, when running on a live system it is important to specify the target drive for the BCD in order to avoid overwriting the live configuration.)
I then shut down the system, removed the hard disk and booted from the USB flash drive, after which the Windows Boot Manager loaded an operating system from within the VHD.
Looking at my BCD configuration (shown here for reference), I can see the source of my many hours of confusion – the Boot Manager resides on the physical media (my USB key – which was allocated drive D: in this case) and loads an operating system from the virtual disk that is given another drive letter (in this case C:):
Windows Boot Manager
——————–
identifier {bootmgr}
device partition=D:
description Windows Boot Manager
locale en-us
inherit {globalsettings}
default {current}
resumeobject {27f66313-771a-11de-90bb-00037ab36ab6}
displayorder {current}
toolsdisplayorder {memdiag}
timeout 30
Windows Boot Loader
——————-
identifier {current}
device partition=C:
path \windows\system32\winload.exe
description Hyper-V Server 2008 R2
locale en-us
inherit {bootloadersettings}
osdevice partition=C:
systemroot \windows
resumeobject {27f66313-771a-11de-90bb-00037ab36ab6}
nx OptOut
detecthal Yes
It took a while to boot (my flash drive was a freebie is not the fastest in the world) but, once loaded into memory, Hyper-V Server seemed to run without any noticeable delay. I figure that, as long as the workload is stored on another disk this should not present any problems and, given suitably fast flash memory, it ought to be possible to improve boot times as well. Running a full Windows operating System (e.g. Windows 7) in this manner is an entirely different matter – very few USB flash drives will be able to stand the constant writes and further testing would be required.
Now that I have Hyper-V Server running from an inexpensive USB flash drive with no reliance on my PC’s internal hard disk, all I need to do is inject the correct network drivers and I will have a virtualisation solution for colleagues who want to run a full hypervisor on their corporate notebooks, without deviating from the company’s standard client build.
Additional information
The following notes/links may provide useful background information: