Virtual machine templates are "reference" or "master" copies of a virtual machine which can be used as a to deploy a new virtual machine. The advantage to using VM templates is that you can deploy them very quickly and ensures a consistent configuration for every deployment. This saves lots of time by bypassing the operating system installation, avoids the potential for configuration mistakes and streamlines administration by ensuring a consistent platform across VM's.
A high level overview for the process of using virtual machine templates is:
- Create the base image
- Prepare the base image for copying
- Deploy new virtual machine based on prepared image
There is a method for creating virtual machine templates in every virtualization platform but this guide will be based on Hyper-V Server 2008 R2 using only the built-in management tools.
Creating the Base Image
Creating the base image is the same as any new virtual machine setup.
- Configure the virtual machine settings.
- Install the operating system
- Install drivers and required software (ensure Hyper-V Integration Services are installed)
- Make any additional customization's as required (important to make it generic enough to be deployed to new machines and customized enough to avoid performing the same actions on each new deployment)
Preparing the image for copying
To prepare the image to be copied we want to ensure we perform the following steps on the virtual machine:
- Remove any user specific information and data (downloads, temporary files etc)
- Ensure required setup applications are configured as per your requirements
- Perform application and operating system updates and patches
- Uninstall anything that won't be needed for future deployments
- Sysprep the OS
For our test environment we will simply install the IIS role and create a new folder. The steps for getting your base image would change depending on the requirements. If you wanted to use the template for a variety of purposes you may want to simply perform Windows updates and sysprep the VM without any additional configuration.
Creating a Virtual Machine Based on the Template
Using the "Copy" Method (Best performance, production environment)
The copy method means you will literally copy the reference VHD you created to a new location and rename it. You can then create a new virtual machine and use this as your VHD. As soon as the VM boots it will be ready to use.
To create a VM from the template we created performing the following steps on the Hyper-V host server:
- Ensure the base virtual machine you created is sysprepped and shutdown
- Make a copy of the base VHD file to a new location/server and rename it based on the new VM name
- Create a new virtual machine and choose to use an existing disk (the one we copied)
You can now boot this new virtual machine and you will be presented with the OOBE mini setup where you set the time zone and create a user. Any changes such as the IIS role we installed and folder we made will all be still there. The system is ready to use right away.
Using Differencing Disks (fast deployment and less disk space, testing environment)
An alternative to deploying from a template by copying the entire VHD is to use differncing disk. A diffencing disk will create a parent child relationship where the VHD we first created is the parent and the one we will create here is the child. The result will be that the base OS will be loaded directly from the parent disk and all future changes will be written to the child disk. This means that we can deploy many VM's which share the same base OS VHD saving us some time and lots of disk space without conflicting with each other.
You will see that the first step is to delete the VM we originally created. This is not the same as deleting the actual VHD itself. Deleting the VM ensures that there are no accidental changes to the base VHD which we are creating a differencing disk based on. NOTE: If you attempt to modify the base VHD after creating a child differencing disk you will likely corrupt all VM's and data who contain child disks of the modified parent.
To deploy from a VM template using Hyper-V perform the following steps:
- Delete the base virtual machine (but not the VHD file) (Suggested)
- Move the VHD file to folder which will store template base VHD images
- Set the properties of the VHD file to read-only
- Create a new virtual machine and choose to add a disk later
- Create a new differencing disk pointing to the base read-only disk as the parent
With the differencing disk created the server is now ready to use and will boot in the same way as if you made a copy of the entire VM. Please remember that using differncing disks in this way is not recommended for production/ Special attention must be made to ensure the parent disk is never modified and is backed up safely.
Doing More with SCVMM
SCVMM allows you to manage and deploy your VM templates through a central console and allows you to create a self-service portal to allow users to deploy VM templates themselves based on templates available to the user. Configuring SCVMM is beyond this scope of this article but is an exciting topic I can't wait to cover soon.