Tuesday, February 7, 2012

Boot from VHD is Wicked Awesome

Boot from VHD is a great technology that enables you to host multiple self-contained operating systems on a single hard drive. Similar to dual-booting, but you don't have to worry about installing applications over each other or overwriting program file updates because your secondary OS is fully contained within a single virtual hard disk file.

As someone who spends a lot of time at customer sites, I find this invaluable to maintaining a clean, portable secondary environment. My boot-from-vhd enviornment can be joined/disjoined from customer domains without affecting my corporate laptop build; I can run beta apps and install troubleshooting tools without jeopardizing the stability of my daily work computer.

For my installation, I'm using Windows 7 Ultimate as my primary host computer and will install Windows 2008 R2 inside my VM environment.

- Windows Automated Install Kit (necessary for ImageX)
- Windows 2008 R2 Physical Media (or ISO + utility such as Virtual CloneDrive to mount it)

Now, some of you will probaby grumble about needing to download the whole 1.3GB+ WAIK package for less than 1MB of software necessary to perform this task. It does seem ridiculous, that Microsoft has not made it available separately for this specific purpose (and, since WAIK is a free download, most people don't see why you can't just get the tools you need out of it). If you search around, you can find the ImageX pieces available, such as here or here.

If you want to install just the ImageX componets, you need:
- imagex.exe
- intlcfg.exe
- vsp1cln.exe
- wimfltr.inf
- wimfltr.sys
- wimgapi.dll

Extract the files from such a download to C:\Windows\System32; copy wimfltr.sys to C:\Windows\System32\drivers, and then right-click > Install on wimfltr.inf.

Alternatively, download and install WAIK.

Moving on ...

Create a Directory to Store VHDs
  1. From Windows Explorer, create a new directory (I used C:\VHD).
  2. Yep, that's it.

Create a VHD
  1. On the start menu, click Start, type in cmd, and then press Ctrl+Shift+Enter to start an Elevated Command Prompt. right-click Computer, and then click Manage.
  2. In the User Account Control dialog box, click Yes.
  3. Type compmgmt.msc and press Enter.
  4. In the left pane of the Computer Management console, click Disk Management.
  5. In the right pane, click More Actions, and then click Create VHD.
  6. On the Create and attach Virtual Hard Disk page, specify the location as C:\VHD\Windows2008R2.vhd, the virtual hard disk size as at 50GB, and then set the virtual hard disk format to Fixed or Dynamically expanding (depending on your preference). If it's not something you're going to use often, you can use a Dynamically Expanding VHD to save space. If you choose Fixed, you're going to have to wait for the entire 50GB (or whatever size file it is) VHD to be formatted.
  7. Click OK.
  8. In Disk Management, right-click the newly-created 50 GB disk, and then select Initialize Disk.
  9. On the Initialize Disk page, verify that the VHD is selected. Choose MBR as the partition style, and then click OK.
  10. Once the disk is initialized, right-click the unallocated space of the disk, and then select New Simple Volume.
  11. When the Welcome to the New Simple Volume Wizard starts, click Next.
  12. On the Specify Volume Size page, leave the default values to use the entire disk, and then click Next.
  13. In the Assign Drive Letter or Path list, choose an unused drive letter (I chose V:), and then click Next.
  14. Under Format this volume with the following settings, specify NTFS for the file system, use the default Allocation unit size, specify a volume name Next.
  15. On the Completing the New Simple Volume Wizard page, click Finish.

Mount the Windows Server 2008 R2 Media (If Necessary)

This example uses Slysoft Virtual CloneDrive

  1. Navigate to the folder where you have downloaded the Windows 2008 R2 media.
  2. Right-click the ISO file, and then select Mount (Virtual CloneDrive E:), where E: is the virtual DVD-ROM letter. Mine just so happens to be E:.

Apply the Windows Server 2008 R2 Image to the VHD

  1. Go back to your Elevated Command Prompt (or launch a new one).
  2. Run the following command to determine which Windows Server edition you wish to use:
    dism /Get-Wiminfo /Wimfile:"E:\Sources\install.wim"
  3. Select an index number that corresponds to the edition that you wish to install. In my case, I chose Index 3.
  4. Run the following command to apply the installation image in the appropriate index to the VHD that is currently mounted as V:\:
    imagex /apply "E:\Sources\install.wim" 3 v:

Update the Boot Configuration Data

We'll use BCDEdit /copy to use the current boot configuration entry as the starting point for our new entry.

  1. Run the following command from your Elevated Command Prompt:
    bcdedit /copy {current} /d "Windows 2008 R2 VHD Boot"
  2. Copy the newly-created GUID to the clipboard.
  3. Run the following commands (using the new GUID) to instruct the Boot Manager where to look for the VHD:
    bcdedit /set {enter-new-GUID-here} device vhd="[locate]\VHD\Windows2008R2.vhd”
    bcdedit /set {enter-new-GUID-here} osdevice vhd="[locate]\VHD\Windows2008R2.vhd”
  4. Run the following command to force hardware detection during the initial boot:
    bcdedit /set {enter-new-GUID-here} detecthal yes
  5. Close the elevated command prompt.

Detach VHD

  1. In Disk Management, right-click the disk that is the mounted VHD, and then click Detach VHD.
  2. On the Detach Virtual Hard Disk page, ensure that the Delete the virtual hard disk file after removing the disk check box is NOT selected, and then click OK.
  3. Close the Computer Management console.