Using VM Templates to Clone Hyper-V VMs

One major advantage of hardware virtualization is scalability: you can easily add virtual disks to your VMs, expand those virtual disks, expand the capacity of your hardware storage, and add more CPU performance and memory, in addition to deploying a greater quantity of VMs. You can copy VM files from one Hyper-V host to another, or even copy VM files on the same Hyper-V host to create additional identical VMs. The primary disadvantage of this method is that it requires you to manually change hostnames, the IP address (if a static IP address is used) and SID (system identifier). Using VM templates can help you avoid these issues and allows you to create new VMs more quickly and conveniently.

Today’s blog post explains Hyper-V VM templates in detail and explores how to clone Hyper-V VMs step by step.

What Is Hyper-V VM Template and How Can It Be Used?

A Hyper-V VM template is a base image that can be used to create new Hyper-V VMs while preserving software that was installed and configured on a reference VM from which a template was created. You needn’t worry about spending large amounts of time creating a new empty VM, installing applications and configuring software, nor must you copy virtual disks (VHD or VHDX) or an entire existing VM and lose valuable time reconfiguring said VM. You can create one reference VM, and make a template based on this VM, deploying multiple new VMs from your template. If you are working with a large number of VMs, the most rational approach is to clone Hyper-V VMs in your Hyper-V virtual environment. You can consider your Hyper-V VM template as a master pattern that includes not only software configuration but also configuration for VM CPU, RAM, and other virtual devices.

Hyper-V VM templates can be created with Hyper-V Manager which is a built-in Windows software product, and with System Center Virtual Machine Manager (SCVMM) which is paid software geared towards centralized management of large Hyper-V virtual environments. Today’s blog post explores how to clone a Hyper-V VM with Hyper-V Manager by using Hyper-V VM templates.

How to Clone a Hyper-V VM Using VM Templates?

In brief, the following steps must be performed to successfully clone a Hyper-V VM:

  • Create a new Hyper-V VM.
  • Install a guest OS on the VM.
  • Install and configure all needed software and patches.
  • Prepare the virtual machine image with sysprep.
  • Delete VM checkpoints.
  • Export the Hyper-V VM to a Hyper-V VM template.
  • Import a Hyper-V VM by using the existing VM template.

Let’s explore the entire process of creating Hyper-V VM templates and cloning Hyper-V VMs by using templates in greater detail:

  1. Create a new Hyper-V VM.

The first thing you must do here is create a new VM; you can find more information about how to create a new Hyper-V VM in Hyper-V Manager by exploring this blog post. Alternatively, you can use PowerShell for creating a new VM if you are more inclined to use a command line interface.

  1. Install a guest OS.

Install a guest operating system (OS) that is supported by Hyper-V, e.g., Windows.

  1. Install all needed security patches and updates for your guest OS.

You need to perform this time-consuming procedure once to have VMs without known vulnerabilities as a result.

  1. Install needed software and configure that software.

Uninstall unnecessary applications and components to reduce the size of your Hyper-V VM template.

  1. Use a sysprep utility to prepare a master image for cloning.

Sysprep is a built-in system preparation Windows utility which is available for all editions of desktop and server Windows versions starting with Windows 7 and Windows Server 2008. It is intended to prepare Windows installation for imaging, allows to capture a customized installation, and removes PC-specific information thus “generalizing” installation so that it can be installed on different PCs. This utility can be used for preparing images to be duplicated onto a high number of machines with identical hardware, and is the preferred approach for many major manufacturers of laptops. Most likely you have already observed that when you powered on a new laptop with Windows OS for the first time. Windows was already installed on that laptop and you needed to configure particular settings such as username, hostname, time zone and language. Hyper-V virtual machines of the same generation have the same virtual hardware, which implies that you can also use this method as a preparation step to clone Hyper-V VMs.

Sysprep resets information stored in Windows settings such as SID, event log, temporary folders, hostname, and time zone; you need to input this information for a VM clone with the First Run wizard after cloning a VM from the template. If your Windows copy has been activated, then the activation gets reset. Be aware that activation cannot be reset more than three times. If a source VM is joined to the Active Directory domain, your Hyper-V VM template and VMs cloned from said template will have the same Domain SID (note: this is not the same as the machine SID mentioned above).

Sysprep is located in the C:\Windows\system32\sysprep\ directory and can be executed in command line mode or in GUI (graphical user interface) mode. Connect it to your Hyper-V VM (a VM running Windows 7 is used in today’s example), which will be used as a base VM. Run the sysprep.exe file on your guest Windows and select the following parameters.

System Cleanup Action – Enter System Out-of-Box Experience (OOBE). This option unlinks Windows from particular hardware. This option causes the system to mimic the first boot screens you’ll see when performing a normal installation.

Tick the Generalize checkbox, which will in turn reset Windows activation and remove system-specific information. It is recommended to enable this option for preventing possible activation issues after cloning a Hyper-V VM from a template.

Select Shutdown in the drop-down menu of Shutdown Options. This option is needed to ensure that the virtual disk file is not opened by any application (Hyper-V in this case) and data stored on the virtual disk is not being changed.

Using sysprep for creating a reference image to clone a Hyper-V VM.

Note: If you select Enter System Audit Mode as the System Cleanup Action option, your windows will boot directly to the desktop before getting to the Welcome screen. This option allows you to finalize your Windows configuration before creating a Hyper-V VM template. For example, you can delete unnecessary users from the operating system. Another way to enter a System Audit Mode is by pressing Shift+CTRL+F3 on the Welcome screen (Regional settings).

Click OK to proceed.

Alternatively, you can run CMD, change the directory to %WINDIR%\system32\sysprep and run the command:

sysprep /generalize /oobe /shutdown /mode:vm

Where /mode:vm is a new option that should be used only to create reference images for virtual environments such as Hyper-V. Using this option makes the first VM boot faster because there is no need to detect new hardware, as the same generation of VMs uses the same virtual hardware.

After that, you can see a window with a “Sysprep is working” message. At this point, you should wait until the process is finished. If you have selected the same sysprep options, your VM will be powered off once the OS preparation process is finished. Don’t power on the machine until a template is created, lest you be required to run sysprep once again to prepare the VM for creating a Hyper-V VM template.

  1. Delete all checkpoints.

If you have any checkpoints in place for this VM, be sure to delete them all before exporting a Hyper-V VM to a template, which you can do in the Checkpoints section of Hyper-V Manager. Right click each checkpoint and click the Delete Checkpoint option in the context menu (see the screenshot below). Differencing virtual disks (AVHDX files) will merge with parent virtual disks (VHDX files) if you delete checkpoints which represent a VM state at a point before where you are now.

Deleting checkpoints before creating a Hyper-V VM template.

  1. Export a Hyper-V VM to a VM template.

Select the VM you have prepared, right click the VM name in Hyper-V Manager and click Export.

Exporting a VM to a Hyper-V VM template.

Specify where you want to save the files. D:\Hyper-V VM Templates\ is the path used in this example. You can enter this path manually or by clicking the Browse button to select the necessary folder. Click Export and wait until the exporting process is finished.

Selecting a folder to store Hyper-V VM templates.

Now you can see VM files in the directory you specified before (in this case in D:\Hyper-V VM Templates).

Note: If you have a file server that uses SMB protocol for accessing files in your network, you can create a shared folder on a file server and copy various Hyper-V VM templates there. This scenario is convenient when you have multiple Hyper-V servers on which you need to deploy VMs from templates.

A Hyper-V VM is cloned to a Hyper-V VM template.

  1. Import a virtual machine from a Hyper-V VM template.

Right click the name of the Hyper-V host on which you wish to deploy a new VM from a Hyper-V VM template. In the context menu click Import Virtual Machine.

Importing a Hyper-V VM from a Hyper-V VM template.

The Import Virtual Machine wizard opens. Click Next on each step to advance to the next step of the wizard.

Before You Begin. As there is nothing to configure at this introductive step, you can go on to the next step.

Locate Folder. Indicate the folder which contains the virtual machine you’d like to import. In the current example D:\Hyper-V VM Templates\Win7 is being used.

Selecting a folder with Hyper-V VM templates.

Select Virtual Machine. Select the virtual machine you wish to import from the list. In this case, files of only one VM are stored in the selected folder.

Selecting a Hyper-V VM template from the list in the Import Virtual Machine wizard.

Choose Import Type. There are three import type options:

  • Register the virtual machine in-place. This option should be used if a VM is located on the same Hyper-V host, but was unregistered on the host for some reason. The machine’s unique ID is preserved in this case; the VM will function as if it has not been moved or copied.
  • Restore the virtual machine. This option is similar to the one above, but an important distinction between the two is that this option should be used if a virtual machine is copied from a source location to a target location, but said VM is no longer run on the source location (for example, if VM files have been manually copied as backup of a Hyper-V VM and then restored from that backup). The unique ID of the VM is also preserved.
  • Copy the virtual machine. This is the most universal option of the three listed. You can import a VM multiple times and run those VMs. A new unique ID is generated each time you use this option to import a VM. For sake of simplicity, this option has been selected in the current example and displayed on a screenshot below.

Choosing the VM import type during cloning a VM from a Hyper-V VM template.

Choose Folders for Virtual Machine Files. Select where you want to store your VM configuration, checkpoints and smart paging.

Choose folders for virtual machine files during cloning a VM from a Hyper-V VM template.

Choose Folders to Store Virtual Hard Disks. You should also select where virtual disks will be stored.

Choosing folders to store the virtual hard disks of a VM clone created from a Hyper-V VM template.

Now you will be able to find a VM clone in the list of your VMs in Hyper-V Manager, the name of which is the same as the name of the source VM that was used as a base for creating a Hyper-V VM template and can be renamed if necessary.

Configuring a VM After Cloning

The name of the VM clone is that of the source VM, but can be renamed if necessary. Start your VM, select your country, keyboard layout, hostname, and create a new user. Make note that existing users are not deleted automatically. Next, the Microsoft Windows License agreement is displayed to ensure that new users can read this agreement. Select your time zone and your Windows running on a Hyper-V VM-clone will display the login screen. You can delete any new users created during Windows “first run” if such users are no longer necessary.

Windows Setup Wizard is running after creating a VM-clone from a Hyper-V VM template similarly as during the first system load after installation.

Conclusion

Hyper-V VM templates are not difficult to use, can help you save time, and can be created directly in Hyper-V Manager. Today’s blog post covered the process of creating templates and VM clones with Hyper-V Manager. A sysprep tool allows you to prepare your guest Windows system before creating a Hyper-V VM template by deleting the machine’s SID and cleaning the information in the Windows registry. You can use VM templates in highly scalable virtual environments for more efficiency and agility. Don’t forget to protect your data by making backups of your Hyper-V VMs. Download NAKIVO Backup & Replication and back up all your Hyper-V virtual machines.

Backup Solution for Hyper-V

People also read