Creating a bootable VHD image

Whenever I first starting trying to figure out how to create a bootable VHD image and boot into it, I received plenty of links from people but it just seemed that no matter which link or advice I would follow, there was always something that just wouldn’t quite work with my specific hardware.  Of course, if anyone is ever going to have hardware issues, it always seems to be me!

What I finally came upon was a list of steps, a combination of instructions from various other sources that seem to work every time, no matter what (at least for my hardware).  A couple of customers requested that I post these steps, so here we go.  If you already have a procedure that works for you, that’s fine, this is just for those that need a concise list of steps.

The first thing to know is, to boot into an OS as a bootable VHD image, the OS needs to be either Windows 7 or Windows Server 2008 R2.  And of course you need to setup your hardware BIOS to allow for hardware virtualization.  From there…

1. Starting from scratch, what you will need to do is build a .vhd image that has an OS installed on it, but yet has not  had the Hardware Abstraction Layer (HAL) associated with it for the machine you are building it on.  I will use a tool called Wim2Vhd. This is a command line tool used to extract image information from the OS image on disk and add it to the vhd. You can find more information about this tool here: https://code.msdn.microsoft.com/wim2vhd.

2. Since I have an MSDN subscription , I decided to pull down an image (.iso) from there and then I extracted this .iso image onto a DVD. Make sure you get your product key because you will need that to activate Windows once you get booted into the new OS.

3.  I have a C:\Wim2Vhd directory on my machine with the necessary tools inside of it, so I will reference everything from this path.  In my case, I extracted a Windows Server 2008 Enterprise R2 .iso onto my F: drive at F:\W2008ISOs\Extracted. You can extract the ISO, or burn it onto a DVD. (A special note here, you need to put your bootable VHD onto a drive that is not Bitlocker encrypted to be able to boot into it).

4. Open a command prompt and change the directory to C:\Wim2vhd.

5.  In order to use the Wim2Vhd exe appropriately on the command line, we need to make sure that we get the 'Name' of the OS that we are going to be building our image with.  In order to get the name out of the extracted iso files, we use an .exe named Imagex which is located in the Wim2Vhd directory. You can get all these tools from the Windows Automated Installation Kit (AIK) for Windows 7 https://www.microsoft.com/downloads/details.aspx?familyid=696DD665-9F76-4177-A811-39C26D3B3B34&displaylang=en.  Type the following command at the command prompt:

Imagex /info F:\W2008ISOs\Extracted\Sources\install.wim

Imagex will look out onto my F: drive where I have the extracted OS install files at.

Notice that I point to a directory named Sources and the file that has the information in it is Install.wim. When I execute this command I get back something that looks like this:

================================================================================

ImageX Tool for Windows
Copyright (C) Microsoft Corp. All rights reserved.
Version: 6.1.7600.16385

WIM Information:
----------------
Path:        F:\W2008ISOs\Extracted\Sources\install.wim
GUID:      
Image Count: 1
Compression: LZX
Part Number: 1/1
Attributes:  0x8
             Relative path junction

Available Image Choices:
------------------------
<WIM>
  <TOTALBYTES>2434613007</TOTALBYTES>
  <IMAGE INDEX="1">
    <DIRCOUNT>13394</DIRCOUNT>
    <FILECOUNT>59593</FILECOUNT>
    <TOTALBYTES>10132413502</TOTALBYTES>
    <CREATIONTIME>
      <HIGHPART>...</HIGHPART>
      <LOWPART>..<LOWPART>
    </CREATIONTIME>
    <LASTMODIFICATIONTIME>
      <HIGHPART>..</HIGHPART>
      <LOWPART>...</LOWPART>
    </LASTMODIFICATIONTIME>
    <WINDOWS>
      <ARCH>9</ARCH>
      <PRODUCTNAME>Microsoftr Windowsr Operating System</PRODUCTNAME>
      <EDITIONID>ServerEnterprise</EDITIONID>
      <INSTALLATIONTYPE>Server</INSTALLATIONTYPE>
      <HAL>acpiapic</HAL>
      <PRODUCTTYPE>ServerNT</PRODUCTTYPE>
      <PRODUCTSUITE>Terminal Server</PRODUCTSUITE>
      <LANGUAGES>
        <LANGUAGE>en-US</LANGUAGE>
        <DEFAULT>en-US</DEFAULT>
      </LANGUAGES>
      <VERSION>
        <MAJOR>6</MAJOR>
        <MINOR>1</MINOR>
        <BUILD>7600</BUILD>
        <SPBUILD>16385</SPBUILD>
        <SPLEVEL>0</SPLEVEL>
      </VERSION>
      <SYSTEMROOT>WINDOWS</SYSTEMROOT>
    </WINDOWS>
    <NAME>Windows Server 2008 R2 SERVERENTERPRISE</NAME>
    <DESCRIPTION>Windows Server 2008 R2 SERVERENTERPRISE</DESCRIPTION>
    <FLAGS>ServerEnterprise</FLAGS>
    <HARDLINKBYTES>3549409854</HARDLINKBYTES>
    <DISPLAYNAME>Windows Server 2008 R2 Enterprise (Full Installation)</DISPLAYN
AME>
    <DISPLAYDESCRIPTION>This option installs the complete installation of Window
s Server. This installation includes the entire user interface, and it supports
all of the server roles.</DISPLAYDESCRIPTION>
  </IMAGE>
</WIM>

    ====================================================================================

What you are looking for is this line:

<NAME>Windows Server 2008 R2 SERVERENTERPRISE</NAME>

This name is important, because if you do not have a product key that matches this SKU, you will not be able to activate your OS.

6.  Now that you have the 'Name' of the software you will be using, we are going to use Wim2Vhd to create our Vhd image.  Again, make sure that the drive where you put your vhd is a drive that does not have BitLocker encryption turn on on it. Use this command

CSCRIPT WIM2VHD.WSF /WIM:D:\W2008ISOs\Extracted\sources\install.wim /SKU:ServerHpc /SIZE:51200 /DISKTYPE:Dynamic /VHD:D:\W2008ISOs\W2008R2EntBase.vhd

This command will create a sys-prepped, Windows Server 2008 Enterprise R2 VHD that has a maximum, dynamically expanding size of 50GB. I chose 50GB because all I really intend to do from this image is run hyper-v images.  If you intend to use this OS to start installing a lot of software on, you may want to make it larger.

7. To create your boot menu, on the command line (the menu item string will be what you see in between the double quotes):

bcdedit /copy {current} /d "Win2008 R2 Ent"

8. A GUID will be output on the command line, copy this by right clicking in the command window and selecting Mark. Use these other command lines to create your menu item and set it up so it knows where to find the .vhd. You need to enter these commands and select Enter after each line entry.

bcdedit /set {paste_guid_here_including_braces} device vhd="[D:]\W2008ISOs\W2008R2EntBase.vhd"

bcdedit /set {paste_guid_here_including_braces} osdevice vhd="[D:]\W2008ISOs\W2008R2EntBase.vhd"

bcdedit /set {paste_guid_here_including_braces} detecthal on

bcdedit /v (…this is used to confirm that what you just did did modify the boot record)

 

The first time you boot into this image, the image will start reading the HAL and setting things up as if you were installing a new OS except for the fact that those files have already been installed.

Hope this helps someone out there achieve their bootable VHD goals!