Running Windows from a USB flash drive

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:

28 Comments

  • Keni
    Friday 24 July 2009 - 16:23 | Permalink


    grrrr – bcdedit /set {guid} device vhd=[usbdrive]:\virtualharddisk.vhd throws an error.

    The device is not valid…

    Any ideas?

  • Friday 24 July 2009 - 22:21 | Permalink


    Hey Keni – good to hear from you!

    Yes, it’s a typo I think – the : should be inside the [] (e.g. [c:]\virtualharddisk.vhd

    I’ll go back and correct the original post.

    The other thing that might make a difference is whether your USB drive is detected as an HDD or an FDD. Mine was picked up as USB 2.0 HDD but diskpart still sees it as a removable drive.

  • Tuesday 28 July 2009 - 23:42 | Permalink


    Arghhh! Now a Microsoft blog has publicised a free tool for doing all the heavy lifting! Pity I can’t get back all the time I spent on this last week :-(

  • Anonymous
    Wednesday 12 August 2009 - 8:55 | Permalink


    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?

  • Wednesday 12 August 2009 - 14:55 | Permalink


    @Anonymous – not sure what’s causing your BSOD but try mounting the VHD and looking for details in a folder called %SystemRoot%\Minidump. There are various tools that will help to analyse the dump but they tend to work on a running system… which doesn’t help you much.

  • Pingback: Hyper-V Server 2008 R2 の Flash Media Boot について - SE の雑記

  • Pingback: markwilson.it » Injecting network drivers into a Hyper-V Server (or Windows Server) installation

  • Friday 21 August 2009 - 17:09 | Permalink


    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.

  • Friday 21 August 2009 - 22:28 | Permalink


    Hi Ian, thanks for your comments – I enjoy working on the projects like this one… keeps me from losing my technical skills!

    You’re right that, even though Hyper-V Server 2008 R2 is now released, Microsoft is still only offering the RC on the website. If it’s like Windows 7 and Server 2008 R2 then they are drip feeding the new releases into different channels over a few weeks – it went onto MSDN/TechNet last week, and I think Volume License customers have it too now… I guess the public download will follow soon.

    Mark

  • Robert Jefferis
    Thursday 27 August 2009 - 0:06 | Permalink


    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.

  • Thursday 27 August 2009 - 10:32 | Permalink


    One of my colleagues was having trouble getting this working off a bootable USB HDD a few days back (BSODs) – I don’t know how he got on (i.e. if he resolved it) but the difference between that and what I did is that it’s a bootable HDD – i.e. not flash masquerading as a HDD.

    I suspect the BSOD is because as control is switched from the Windows bootloader to Windows, it’s failing to find a driver for the USB-SATA bridge and failing. If you could locate the correct mass storage driver and inject that into your VHD then you might resolve the problem.

    Note that this is just a suggestion, I have not confirmed it.

  • Saturday 29 August 2009 - 16:09 | Permalink


    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.

  • Lund
    Tuesday 15 September 2009 - 21:36 | Permalink


    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!

  • PhilT
    Wednesday 23 September 2009 - 11:31 | Permalink


    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.

  • Wednesday 21 October 2009 - 12:13 | Permalink


    Thanks for sharing

  • Wednesday 18 November 2009 - 18:07 | Permalink
  • Pingback: markwilson.it » Windows native boot from VHD roundup

  • frank baker
    Saturday 23 October 2010 - 22:34 | Permalink


    Hi I was wondering that you can show me how to load Windows server 2008 or V from a FLASHDRIVE. I didnt know that you can actually do that. Do you have to have a certain type of USB drive or….????Hope to hear from you soon.

  • Adz
    Sunday 24 April 2011 - 6:41 | Permalink


    Hey

    I know this is an old post but I’ve just come across it now and wanted to say thank you.

    Also, I can confirm that PWBoot does resolve the 0x7B BSOD error when launching the Win 7 VHD from a USB stick, as suggested by Lund. The tool itself has since been updated, so you don’t even need to boot to the VHD locally first – just attach the VHD (through diskpart or diskmgmt.msc) and point the tool at that drive. Works flawlessly.

    ~Adz

  • Jonny
    Monday 3 February 2014 - 23:50 | Permalink


    Mr wilson, you clever bean!

    I bought the hype and made a native VHD on barebones. It died :(
    The VHD can be mounted, has been re-sized, and the little un (70gig) can be mounted, but not booted from.

    I thought I understood the relationship, between the VHD on the c partition, but every guide I’ve followed to fix the now unbootable (native) VHD has so far failed.

    (I’ve tried it, but) it sounds like I need to get the

    bcdboot vhddrive:\Windows /s usbdrive: /v

    to work. I’m sure this is the first time I’ve seen the bcdboot being used with different source and destination params…

    Can you tell me one thing? If a VHD still has ‘some integrity’ (can be attached, assigned, but not bootable) is it worth trying to salvage?

    The main reason I went for VHD was the allure of NOT having to reinstall, with my hundreds of applications installed with tailored configurations and hotkeys…. I can live without. That’s why I’m being so stubborn and not ‘just’ going diskpart, sel disk, clean!

    Great guide btw. Well done

  • Thursday 6 February 2014 - 18:29 | Permalink


    Sorry Jonny – it’s been a while since I looked at this and I think technology has moved on. It was nice at the time to be able to tell certain people at Microsoft that it was possible to do what they said was not!

  • Thursday 6 February 2014 - 21:25 | Permalink


    Thanks for the reply… It has been a while, but the technology is still cutting edge, and what an allure! a file, a VHD, with EVERYTHING… I don’t want USB boot, not even booting into linux… I’d just like to know how to copy a native VHD, and whether that VHD needs ‘prepping before it can be booted form.. Windows 8 and it’s successors will come and go. I’d like to stay with virtualized win7 for a bit… I just need to understand the backup strategy (how bccdboot and MBR work with VHD).

    (one of) The most useful guide(s) I’ve found is Scott’s, on VHD magic (http://www.hanselman.com/blog/LessVirtualMoreMachineWindows7AndTheMagicOfBootToVHD.aspx)

    comments are closed, the last being Marc Stacey’s inaccurate statement

    The local disk must have at least two partitions: a system partition that contains the Windows 7 boot-environment files and Boot Configuration Data (BCD) store, and a partition to store the VHD file. For more information about disk partitions, see Understanding Disk Partitions. For more information about adding a Windows 7 boot environment for native VHD boot, see Add a Native-Boot Virtual Hard Disk to the Boot Menu.
    http://technet.microsoft.com/en-us/library/dd799282(WS.10).aspx#BKMK_whatIsVHD
    The way I read that, is , if you create a new system partition on the external, it will work

    I’m wondering if it’s because I’m not separating/distinguishing my ‘HD boot’ partition (< 300Mb) from the partition that holds the VHD(s?) that I'm having repeated boot fails (once on an SSD, now replaced awaiting install) once on my original 700g HD (currently in use)

    Hope that makes some sense. Looks like a new blog is being asked for… I certainly have .some observations to share!

    Plan is to try and copy (new) native vhd, and make a (bcd)boot entry (not sure if hardware Id conflicts will ensue, if any manual detaching needs to be completed of VHDs, before rebooting and selecting the (new) boot option…

    If this works, I may try copying the contents of my (defunct) original VHD, to a newly prepped VHD … More of a nice to have, tbh (extensive shell configuration) especially seeing as it seems that Sandboxie folders are reportedly highly portable, and thankfully much of my 'setup' was via sandboxies (an MS Office sandboxie and a 'direct audio workstation' sandboxie)… Priority #1 is PRESERVING THE INTEGRITY OF A CLEAN INSTALL, including all device drivers (it's not easy setup a Dell L502x with wireles display technology…) before "wrecking"/tweaking/using…

    Thoughts and observations welcome!!

    Jonny (100gig new native win7 ult x64 VHD on BARE METAL!))
    http://i.imgur.com/NdJH1JI.png

  • Thursday 6 February 2014 - 22:43 | Permalink


    after reading more, I’m thinking a critical (and bad!) decision is being made by me, in following scott’s steps to the letter (http://www.hanselman.com/blog/LessVirtualMoreMachineWindows7AndTheMagicOfBootToVHD.aspx) , instructions that are echoed by sambul61 @ http://reboot.pro/topic/15815-boot-win-7-vhd-on-bare-metal-pc-from-empty-drive/ … advice that contradicts the following:

    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    During the default installation of Windows 7 a 100MB System Reserved partition is created that supports BitLocker encryption and protects the boot environment from inadvertent changes.

    It is possible to deploy Windows 7 without this partition, but BitLocker cannot be enabled later.

    When deploying Windows 7 in an unattended manner there may be a requirement to format the drive first, possibly to allow additional installation files, such as network drivers, to be copied to the local drive.

    At next startup, you’ll see Windows 7 in the boot menu!
    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    Optional: If you want to add a VHD manually to the boot menu, you just need to copy an existing entry and set some parameters:

    bcdedit /copy {originalguid} /d “New Windows 7 Installation”
    bcdedit /set {newguid} device vhd=[D:]\Image.vhd
    bcdedit /set {newguid} osdevice vhd=[D:]\Image.vhd
    bcdedit /set {newguid} detecthal on

    (Q: why would you want to add bootmenu entry manually, if it’s automatically added? Is this for attaching a ready-to-rock VHD?)

    thanks

  • Thursday 6 February 2014 - 22:48 | Permalink


    (what I mean is)

    “Resolution”

    The following diskpart commands will create partitions that will allow Windows 7 to install with a System Reserved partition on a pre-formatted disk:
    SEL DISK 0
    CLEAN
    CRE PAR PRI SIZE=100
    SEL PAR 1
    FOR FS=NTFS QUICK
    ACTIVE
    CRE PAR PRI
    SEL PAR 2
    FOR FS=NTFS QUICK
    ASSIGN LETTER=C:

    Note: The System Reserved partition can be increased and hold the Windows Recovery Environment.

    (from http://theether.net/kb/100164)

  • Thursday 6 February 2014 - 23:03 | Permalink


    info: (http://technet.microsoft.com/en-us/library/dd744364.aspx)

    Default Configuration
    For new installations, by default, Windows Setup creates a system partition and a Windows partition.

    (img: http://i.technet.microsoft.com/dynimg/IC570854.jpg)
    “Windows Setup automatically creates these disk partitions when:
    There is no existing system partition on the computer.

    The computer does not already have three existing partitions.

    There is enough disk space for the partitions.”

    I think I’ll reboot, move the vhd, reboot to partition magic, shrink the MBR partition, then I’ll have to review the bcd entries…. think I’ve just found one reason one might want to ‘manually add a bcd entry!’ (see above)

    Thoughts welcome!

  • Thursday 6 February 2014 - 23:03 | Permalink
  • Sunday 9 February 2014 - 19:31 | Permalink


    one last q Mark: are you advising against native vhd boot these days? does anyone still do it?

    learned the hard way that shrinking (native) ain’t easy, and copying ie backup etc of the vhd (robocopy) also not working (yet… binary mode? make unbootable mod reghive like tweak to make “hyper-v ready”?)

    It’s been occasionally interesting, if nothing else!

  • Wednesday 12 February 2014 - 0:19 | Permalink


    Not really sure that I have a view Jonny – Native VHD boot has its uses, but I’m not sure it’s caught on in a big way.

  • Leave a Reply

    %d bloggers like this: