Insufficient data from Andrew Fryer

The place where I page to when my brain is full up of stuff about the Microsoft platform

.VHD, one of my favourite file formats

Virtual Hard Disks or VHD’s are not just the format used to hold a hard disk for use with they also have at least three other uses I know of:

  • They are the format used for windows backup so making a virtual machine from a physical one (aka P2V) is simply a matter of making a backup, creaing a new hyper-V virtual machine and pointing it at this file.
  • You can also boot directly from a VHD if the operating system on the VHD is Windows 7 or Windows Server 2008 R2.  This may sound like virtualisation but it isn’t because there is no hypervisor technology needs and if there is you can actually setup Hyper-V in this VHD and run virtual machines from it.  (I have more on this here).
  • For me the most useful feature is that they can be mounted into Windows 7 or Windows Server 2008 R2 using the disk manager in either OS. 

I use this a lot in building demo environments because I can mount a VHD on a running environment (physical or virtual) and copy stuff to and from it.  

First of all I need to create a VHD which can either be done under the hyper-V role or under disk management in server manager (which I find is faster). I simply go to the actions and select create VHD:

a create VHD

in the wizard specify it’s size, location and what kind of disk (dynamically expanding or fixed). In the demo world dynamically expanding is fine and even though I have specified 10Gb the VHD will start at 4Kb until I start to do anything with it.

b create VHD size

Click OK to finish and the VHD now shows up in Disk Manager(with a pale blue icon to show it’s a VHD)..

c create VHD done

Now you can initialise it , create partition(s), format it and assign it a drive letter as you would any physical disk(in this case I now have disk manager open in server manager)..

k vhd mounted and ready to use

So now I can copy whatever I want into it such as installation files databases scripts etc.

When I have all the stuff I need on the VHD, I just come back to disk manager and detach it, by selecting it, right clicking and choosing detach 

l detach vhd from physical OS

as I can’t use the same vhd in more than OS at the same time.

Now it’s detached I can attach it a virtual machine.  The virtual machine must be off to do this So this is unlike an .iso file which you can mount at anytime but is of course read only once you’ve made it.

Anyway I can now go to the settings of a non running virtual machine in server manager  in this case my windows 7 client virtual machine. I select the IDE controller 0 and the option to add a hard disk

m add new hard drive to vm

I specify the path of the resources VHD I created above…

n specify vhd  to add to vm

..and click OK to attach it to the VM. 

No I can start the VM and see my resource disk with all the stuff I put on it..

o VHD in guest VM

At this point I have usually forgotten to add something to this VHD that I needed. All I need to do now is stop the VM, attach the VHD on the physical OS again, copy the extra stuff, detach and start the VM again.

It gets better in that the VHDs can be reused in this way on any VM and I could even do all this attach/detach business in PowerShell.  BTW If you want to use PowerShell then refer to this excellent post from Taylor Brown a member of the Windows Server core team.

You can do all of this in Virtual PC as well and move a VHD’s between the two.