29 août
Share a Common VHD Disk Image in Hyper-V
One of the great features in Hyper-V is the ability to share a common base (OS) image between multiple Virtual Machines. This would allow you, for example, to create a single Windows Server 2008 OS image and base several virtual servers on that image without duplicating or copying any data. In theory there is also a higher change that parts of this single image is in the file cache, resulting in better performance... in theory.
Now, this is all great and potentially very compelling, however Microsoft, or any other source I looked at, does not actually explain the process of sharing a disk between multiple VMs, so here goes:
- Create a new virtual machine (or use an existing one if already available).
- Install it, tune it, optimise it, configure whatever you want to be the same between all virtual machines that inherit this disk. Note that once other virtual machines start using this disk image you can longer make changes to it, at least to the common bit. Any changes made will become specific to the VM the changes are made in.
- Once happy with the common image, power down the VM and compact the disk using the Hyper-V Manager to make it as small as possible. I even remove the pagefile as that is dead space that cannot be shared anyway.
- Mark the common disk image as read only to prevent accidental changes and corruption to VMs using it.
- Create a new VM and assign the common disk image to it. DO NOT POWER ON!
- Now here is the trick, immediately make a snapshot of the new VM. All disk changes will from now on be written to the differential disk specific to the VM.
- You can now safely boot the new VM.
- Run NewSID, SysPrep or whatever your policy is to configure imaged ('ghosted') servers (I use NewSID) and install all software specific to that server.
Repeat steps 5-8 for each new server.
There you go, not difficult at all once you know what to do. In my environment all common disk images are located on a solid state drive, which provides phenomenal read-only speeds and seek times. The differential disks (snapshots) are located on a more traditional SATA drive that provides better write speed compared to the SSD.
The 10+ virtual machines that use the same disk image quite happily boot in parallel without fighting over the same 'drive head'. For more details about the server see this blog post.
Update: There is another way to achieve the same, which works better when you have multiple disks attached to a VM, but don't want to snapshot everything. The procedure is as follows:
- Create a new VM
- When asked to configure a hard disk select the option to 'configure one later'.
- Finish the new VM wizard and open its settings.
- Create a new disk of type 'Differential' and select the common image you previously created as the parent.