The Blog

Creating & Deploying Hyper-V Virtual Machine Templates

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:

  1. Create the base image
  2. Prepare the base image for copying
  3. 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.

  1. Configure the virtual machine settings. 
  2. Install the operating system
  3. Install drivers and required software (ensure Hyper-V Integration Services are installed)
  4. 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)
New Hyper-V VM

Create a new virtual machine in Hyper-V Manager

Hyper-V VM memory

Configure VM Memory

Hyper-V VM Network

Configure VM Network

Hyper-V VM VHD

Create the base VM VHD

Hyper-V VM Summary

Click Finish on the VM Summary

Hyper-V VM OS Install

Install the OS as your normally would

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:

  1. Remove any user specific information and data (downloads, temporary files etc)
  2. Ensure required setup applications are configured as per your requirements
  3. Perform application and operating system updates and patches
  4. Uninstall anything that won't be needed for future deployments
  5. 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.

The IIS Role has been installed to our base VHD

The IIS Role has been installed to our base VHD

A folder which is needed for all of our future deployments is created

A folder which is needed for all of our future deployments is created

Sysprep CMD Prompt

Run sysprep through the CMD or GUI

 

Sysprep Generalize

Choose the generalize and shutdown options

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:

  1. Ensure the base virtual machine you created is sysprepped and shutdown
  2. Make a copy of the base VHD file to a new location/server and rename it based on the new VM name
  3. Create a new virtual machine and choose to use an existing disk (the one we copied)

 

Copy VHD file

Copy the template VHD to a new folder or server

Rename the copied VHD to be unique and match the new VM you will create

Rename the copied VHD to be unique and match the new VM you will create

 

Create a new VM using Hyper-V Manager and choose to use the copied VHD as your disk

Create a new VM using Hyper-V Manager and choose to use the copied VHD as your disk

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:

  1. Delete the base virtual machine (but not the VHD file) (Suggested)
  2. Move the VHD file to folder which will store template base VHD images
  3. Set the properties of the VHD file to read-only
  4. Create a new virtual machine and choose to add a disk later
  5. Create a new differencing disk pointing to the base read-only disk as the parent
Delete the base VM in Hyper-V Manager to avoid making accidental changes

Delete the base VM in Hyper-V Manager to avoid making accidental changes

Mark the base template VHD file ad Read-only.

Mark the base template VHD file ad Read-only.

Create a new VM in Hyper-V Manager and choose to attach a disk later

Create a new VM in Hyper-V Manager and choose to attach a disk later

In the Hyper-V Manager, create a new virtual disk. Ensure you select differncing disk. Choose the location for the new VHD and the location of the parent disk.

In the Hyper-V Manager, create a new virtual disk. Ensure you select differncing disk. Choose the location for the new VHD and the location of the parent disk.

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.

 

Neil Bryan
IT Consultant & Technical Trainer
Neil Bryan is an IT consultant and technical trainer specializing in VMware, Citrix and Microsoft virtualization and infrastructure.

1 Comment

  1. Aboodnet

    Thanks a lot. 🙂

Leave a Reply

Skip to toolbar