Running Windows from a USB flash drive
Written by: Mark Wilson
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=<em>driveletter</em>:\<em>virtualharddisk</em>.vhd maximum=15000 type=expandable
select vdisk file=<em>driveletter</em>:\<em>virtualharddisk</em>.vhd
attach vdisk
list disk
(make a note of the disk number.)
select disk <em>disknumber</em>
create partition primary
select partition 1
active
format fs=ntfs quick
assign
exit
(note the drive letter for the newly mounted VHD.)
imagex /info <em>dvddrive</em>:\sources\install.wim
(identify the correct
imagex /apply <em>dvddrive</em>:\sources\install.wim /check <em>imageindex</em> <em>vhddrive</em>:\
diskpart
select vdisk file=<em>driveletter</em>:\<em>virtualharddisk</em>.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 {<em>guid</em>} device vhd=[<em>usbdrive</em>:]\<em>virtualharddisk</em>.vhd
bcdedit /set {<em>guid</em>} osdevice vhd=[<em>usbdrive</em>:]\<em>virtualharddisk</em>.vhd
bcdedit /set {<em>guid</em>} detecthal on
bcdedit /set {<em>guid</em>} 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=<em>usbdrive</em>:\<em>virtualharddisk</em>.vhd
attach vdisk
list volume
exit
(note the drive letter for the newly mounted VHD.)
bcdboot <em>vhddrive</em>:\Windows /s <em>usbdrive</em>: /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:
- Boot Configuration Data Editor Frequently Asked Questions on Microsoft TechNet.
- Note that the boot from VHD functionality in Windows 7/Windows Server 2008 R2 does not support the use of BitLocker or hibernation.
Posted: 3:02 on Thursday 23 July 2009 under Virtual Server/Hyper-V, Windows 7, Windows Server 2008 R2.
Comments: 17
RSS (for comments on this post only)Share This
Comments
Comment from Keni
Time: Friday 24 July 2009, 17:23
grrrr – bcdedit /set {guid} device vhd=[usbdrive]:\virtualharddisk.vhd throws an error.
The device is not valid…
Any ideas?
Comment from Anonymous
Time: Wednesday 12 August 2009, 9:55
Hallo,
I have used this excelent HowTo on preparing external USB HDD installation of Win 7 x64 RTM. Diskpart, imagex, bcdboot – everything without any reported problem. But – Win 7 is booting, doing BSOD and automatically rebooting (grrr, no time to read BSOD, stupid default setting in MS install image…). Again, Safe boot and the problem is loading System32\Disk.sys (Why System32 on x64??). Some delay and reboot…
Any idea, please?
Pingback from Hyper-V Server 2008 R2 の Flash Media Boot について – SE の雑記
Time: Sunday 16 August 2009, 15:43
[...] http://www.markwilson.co.uk/blog/2009/07/running-windows-from-a-usb-flash-drive.htm http://blogs.technet.com/virtualization/archive/2009/07/30/microsoft-hyper-v-server-2008-r2-rtm-more.aspx http://blogs.technet.com/stephenrose/archive/2009/07/28/a-bootable-usb-utility-to-create-bootable-usb-drives-for-win-7-and-server-2008.aspx http://www.microsoft.com/oem/sblicense/OPK/default.mspx # OEM Preinstallation Kit が必須かと思っていたのですが、なくても実現できました。 Published 2009/8/16 23:40 by ちょう タグ: Hyper-V Server R2 [...]
Pingback from markwilson.it » Injecting network drivers into a Hyper-V Server (or Windows Server) installation
Time: Tuesday 18 August 2009, 0:12
[...] couple of weeks ago, I blogged about running Windows from a flash drive – specifically running Hyper-V Server 2008 R2. One thing I hadn’t got around to at that time [...]
Comment from Ian
Time: Friday 21 August 2009, 18:09
Mark, hope you don’t mind me asking, but I followed your guide, worked a treat (I used an old 40gb laptop disk, inside a bus powered drive enclosure, small and quite quick!). But I cant of been reading the download page properly (Microsoft), because I have ended up with RC code of Hyper-V R2. Can’t seem to find the download for it, although it is RTM`d I believe. We are not MSDN, just Action Pack if that makes a difference (although as it is free I don’t see why this is a problem).
Oh and I read your blog religiously, I know you dont make any money out of it, so I appreciate the effort you go to.
Comment from Robert Jefferis
Time: Thursday 27 August 2009, 1:06
hmmm, weird i get the bsod as well.
It starts booting and then just bsod and reboots. Trying in safe mode it gets to disk.sys and then bsod’s.
Did anyone that has had this ever resolve it i wonder.
Thanks
Rob.
Comment from Ian
Time: Saturday 29 August 2009, 17:09
Just a follow up in case anyone is interested. Hyper-V r2 appears to have become available for the general “hoi polloi” to download.
http://www.microsoft.com/downloads/details.aspx?familyid=48359dd2-1c3d-4506-ae0a-232d0314ccf6
So, i will update my USB boot disk and start testing again.
Comment from Lund
Time: Tuesday 15 September 2009, 22:36
Hey Mark,
Thanks for the great instructions! I followed them using a copy of Windows 7 Ultimate instead of Hyper-V, and found that it would not boot off USB. Windows would start loading, but it would BSOD and reboot. After a lot of searching I found a utility called PWBoot (http://www.boot-land.net/forums/index.php?showtopic=6851) that was originally written for Vista. The problem is that you must run PWBoot from in the OS itself.
I copied the VHD to an internal hard disk, added it to my local bootloader, and went through the OOBE. Once I was at the desktop I ran PWBoot, rebooted into my primary OS and copied the VHD back onto the USB drive. It worked like a charm! I now have a fully functional Native Windows 7 Ultimate VHD on my USB hard drive. Also note that this will only work with Enterprise and Ultimate. I initially tried it with Professional and it gave me a licensing error (Pro cannot run in a VHD).
Thanks again, and hopefully this information is helpful to someone!
Comment from PhilT
Time: Wednesday 23 September 2009, 12:31
Thanks fot that Lund, I too had the auto-reboot (no BSOD though) when starting the OOBE from a VHD image. Will try your suggestion of PWBoot.
Pingback from markwilson.it » Windows native boot from VHD roundup
Time: Wednesday 18 November 2009, 19:25
[...] file, attaching (mounting) it, assigning a drive letter and formatting the volume can be found in my previous post on running Windows from a USB flash drive (as well as elsewhere on the [...]


Write a comment
Please note the rules for comments and the privacy policy and data protection notice. I'm sorry but, because not everyone sticks to the rules, I've had to implement some spam prevention measures - if you're experiencing difficulties leaving a comment, please let me know.