In this post I will look at creating a virtual machine (VM) template that you can use to quickly deploy new VM’s from, without having to install and patch them each time.
It is particularly useful if you are in a small Hyper-V environment and don’t have System Center Virtual Machine Manager (VMM).
The beauty of using a template for VM deployment is standardisation. You deploy a known configuration which can be pre-patched which reduces time during the post deployment tasks.
1. First, create a VM, install the operating system and patch it. Unfortunately you have to do this at least once. In the image below you can see I’ve created a gerenation 1 and generation 2 Windows 2012 R2 virtual machine template.
2. On your template VM browse to C:WindowsSystem32Sysprep folder, and run Sysprep. Select the option to Generalize, and select Shutdown from the drop down menu.
3. In the Hyper-V Manager export the VM, right click your template VM and select Export.
4. Select a folder and click Export.
I would leave the original virtual machine template in Hyper-V Manager, as every few months after more patches have been released you will want to update it and create a new template.
Now let’s look at how to deploy a new virtual machine from the template. It’s not just a case of selecting Import in the Hyper-V Manager, because the exported virtual machine and it’s disks still exist and when you import a VM you don’t have the option to rename them.
1. Create new VM without the disks, select New then choose Virtual Machine, configure the machine, selecting the same generation as the template you created, but CPU, memory and networking settings can be different. When you get to the Connect Virtual Hard Disk option, select Attach a virtual hard disk later, click Next and Finish.
2. Browse to your template VM folder (D:\Templates\Win 2012 R2 G2\Virtual Hard Disks) and copy the VHDX file locally.
3. Rename the copied VHDX to be the same as the virtual machine you have just created. In the example below, Phoenix.VHDX
4. Cut and paste the disk into the default location on your Hyper-V server for your VHDX files. In this example, D:\Hyper-V\Virtual Hard Disks.
5. In Hyper-V Manager, right click the new VM you have deployed and select Settings, then select the SCSI Controller option (for Generation 1 virtual machines, select the IDE Controller 0). Highlight Hard Drive and click Add.
6. Browse to D:\Hyper-V\Virtual Hard Disks and select the VHDX file you cut and pasted earlier in step 4. Click Apply.
7. Now go to the Firmware option, select the hard drive you have just added and click Move Up, so it is the first boot entry. Click Apply. Click OK.
You can now boot the virtual machine, once it has finished detecting devices it will run through a short wizard to select the language, license terms and set the administrator password, and you’ve got a patched virtual machine ready to be deployed.