How to Install an Image onto a VHD file.

The last post I made talked about customizing windows image (.WIM) files, and the post before that talked about creating Virtual hard disk (.VHD) files. So the last step is to look at putting an image onto a VHD and making it bootable

So the steps are

  1. Identify your WIM file and if it has multiple images in, which image you are going to install. This might be (a) from the INSTALL.WIM on the windows setup disk (b) a customized version of INSTALL.WIM (see yesterday’s post), (c) an image which you have captured using the IMAGEX tool from the Windows Automated Installation Kit
  2. Create your VHD file. (See this post )
  3. Apply the image to the VHD, and make any additional customizations (enabling or disabling components, applying patches, or adding drivers (all of which can be done with DISM, see yesterdays post). adding files, changing registry entries)
  4. If the VHD is to be used to boot a physical machine, add an entry to the machines boot partition to point to the VHD (which I covered here) If the VHD is to be used for a virtual machine make the VHD itself bootable by creating a Boot Configuration database inside it.

Step 3, applying the image can be done using image X if you have installed the Windows Automated Installation Kit  using the command

 "<path to AIK>\tools\<architecture>\Imagex.exe" /apply <path to wim> <image number> V:  

V: is the drive letter assigned to the mounted VHD use 1 as the <image number> for the first or only image

I mentioned a post by Mike Kolitz, Mike has also got a script on the MSDN code site which goes by the name of WIM2VHD this will create the VHD, apply the WIM file,and patches you provide and copy files into the VHD. Unless you want to customize the registry or turn components on or off this is the ideal tool* but it depends on having the tools from the Automated Installation kit. Mike, to prove what an all-round good chap he is has a PowerShell script on the same site named Install_windowsImage (this script also shows how other languages can be embedded in PowerShell scripts) which removes that dependency so the alternative is to download this and run

 <path>\Install-WindowsImage.ps1 -WIM <path to wim> -Apply -Index <image number> -Destination V: 

As before V: is the drive letter assigned to the mounted VHD, and you use the number of the image starting at 1. Both imageX and InstallWindowsImage.ps1 can provide a list of images in the WIM file if required.

This process takes a few minutes but at the end you have an image which is ready to boot for the first time. Then you’re ready for step 4: making sure the windows boot loader will work.

If the VHD is going to appear as the System disk in a virtual machine the VM will use the boot loader and BCD on the that disk – i.e. we need a boot configuration database inside the VHD. Windows 7 and Server 2008 R2 now have a tool in the \windows\system32 directory named BCDBOOT which recreates the BCD. If you run

 <Path\>bcdboot  V:\windows V:

It will create a BCD inside the VHD file and when a VM comes to boot from it all will be well.

I’ve discussed adding an entry to the BCD on a machine which is already running Windows Vista/7 /Server 2008 / Server 2008R2 , which needs an entry in the BCD on the physical hard disk which points to the VHD. An alternative way to create the entry  uses BCDboot.  If you run it with a /M switch it merges the boot information into an existing BCD. So if you are adding a VHD to boot an alternate OS from, then you can use that command. If you’re doing something odd like running windows PE to setup a new machine to boot from a VHD on a newly formatted drive you can use the same bcdboot V:\windows C: to create the store. If you are adding a Windows 7 / Server R2 VHD to a machine with a Vista / Server 2008 installation on it, don’t forget to update the machine to support the new features with bootsect from the windows Install disk.

 

 

* I’m assuming you want to know what the steps are and how you could take them manually, rather than just going straight to Wim2VHD.