VMware VM Templates A to Z
Manual deployment of many similar VMs in large virtual environments can be a boring task. VMware provides multiple methods of virtual machine (VM) cloning in vSphere virtual environment, one of which is using VM templates. This blog post explores all aspects of VM templates: how they work, how to customize them, their use cases, and their advantages. The article also compares the concepts of VM templates and VM clones.
What Is a VM Template?
A VM template is a master copy image of a virtual machine that includes VM disks, virtual devices, and settings. A VM template can be used many times over for the purposes of VM cloning. You cannot power on and edit the template once it has been created. This is by design, so that nobody can accidentally edit the virtual machine that is used as a template.
This approach provides greater security (and a more “foolproof” method) for VM cloning. After cloning the VM from a template, VM clones are not linked to a VM template and are independent. If you want to edit a template, you should convert a template to a VM, edit the VM, and then convert the edited VM to a new template.
When Can VM Templates Be Used?
VM templates are useful for deploying high numbers of similar VMs, since they preserve VM consistency. For example, suppose a team of testers needs to do their testing work on VMs with a new build of the company’s proprietary software product. The company’s system administrator creates a VM template and clones an identical VM from that template for each worker who needs one.
If something goes wrong during testing or a new test scenario must be implemented, then a fresh VM can be cloned from the template with minimal effort. You can create multiple templates from Linux and Windows virtual machines for different use cases (testing, development, education, etc.). Different templates can include different sets of software. One template can be used for deploying VMs for a financial team, another VM template can be used for development team, etc.
The Difference Between a VM Clone and a VM Template
A regular VM clone is an exact copy of the source VM at the appropriate point of time. If you were to periodically clone a VM that is in a running state, the resulting clones would differ from one another. It is preferable to clone a powered-off VM, although standard cloning still doesn’t offer all the benefits of a template.
When a VM is cloned, all settings such as static IP network, computer name, and identifiers are left identical for each VM clone. As a result, network conflicts can occur. A VM template helps you prevent these issues, because a VM template cannot be edited, nor can it ever be in a running state. VMware provides tools that simplify guest OS customization for VM clones.
The Difference Between a VM Template and an OVA/OVF Template
OVA and OVF templates are used for distributing pre-configured software as virtual appliances. OVA and OVF are compressed file formats. They can contain multiple VMs, which is useful for cases where software consists of multiple components that must be deployed on different machines. For example, NAKIVO Backup & Replication is available as a virtual appliance (in addition to the other deployment options).
Deploying VMs from OVA/OVF templates is similar to deploying from VM templates. OVA/OVF can be accessed from vCenter inventory. Users can also deploy VMs using OVA/OVF from local storage (e.g., disk C: on a user’s computer), mounted USB devices, and remote web servers that are accessible from vSphere Web Client.
The OVF (Open Virtualization Format) package can contain certificate files, ISO optical disk image files, etc. The OVA (Open Virtual Appliance) file is similar to the TAR archive file; it can contain multiple OVF files.
VM templates are not compressed. They can be accessed only from locations available for vCenter (local ESXi datastore, shared datastore) and are not intended for distribution among customers.
VM Guest OS Customization
Guest OS customization is a vSphere feature that allows users to change the settings of a VM clone’s Linux or Windows guest OS. If the traditional approach were used, you would have to manually start a VM, log into the guest OS, then change the configuration in different parts of the OS via a GUI or the command prompt provided by the OS.
This method is cumbersome and consumes significant amounts of time. With VMware’s Guest OS customization, you can create a guest OS customization specification and use this specification for every VM clone produced from the template. You can easily change the IP network settings, hostname, username, password, time zone, license settings, SID (security identifier), and Domain/Workgroup for a guest OS.
Note: the customization parameters for Linux and Windows differ slightly.
Using VM guest OS customization is convenient and saves you time. Configure your clone settings from one place in just a few steps with the customization wizard.
You can create multiple customization specifications for Linux and Windows virtual machines to make them useable as soon as the VM clone is deployed from the VM template. If you are going to use guest OS customization specification, prepare your VMs so that they meet the requirements.
The following components must be installed on a guest VM for this purpose:
- VMware Tools
- Sysprep tool (Windows)
- Perl (Linux)
- Net tools (Linux)
The guest OS must be installed on a virtual disk connected to the SCSI 0:0 node.
Check whether or not your guest OS of choice is supported by your version of vSphere in the VMware Guest OS Customization Support Matrix.
Preparing a VM for Template Creation
Before creating a template, you should prepare your VM. Let’s consider the main points of VM preparation.
Creating a new VM. Create a new VM with sufficient CPU, disk, and memory resources. Select the appropriate VM compatibility options. For example, if you have hosts with ESXi 6.5 and ESXi 6.0, your VM should be compatible with ESXi 6.0 or later (virtual hardware version 11), so that option should be selected. Optionally, you can enable CPU Hot Plug and Memory Hot Plug; this lets you add processor and memory resources while a VM is running.
Disconnecting unused devices from a VM. If you are using a VM that already exists, disconnect unnecessary hardware devices such as virtual floppy disk drives, virtual COM controllers, LPT controllers, etc. If USB devices and CD-ROMs will not be used after software installation, they can also be disconnected. This approach reduces the risk of potential attacks and, as a result, improves security. You can also enter the virtual BIOS and verify that all unnecessary devices are disabled in the VM’s BIOS.
Installing the required software on a VM. Install the operating system on the VM. Then install all the necessary updates, security patches, antivirus protection, and specific software needed for your purposes. Configure your OS settings, such as environment variables, remote access, etc. Set passwords for users and the administrator(s). Passwords should never expire; this way, you can prevent accounts from getting locked. You can also configure a default user profile.
Installing VMware Tools. VMware Tools is a set of drivers and utilities that increases the performance of a guest OS running on a VM as well as facilitating VM management. Install VMware Tools on the guest OS. This is an important requirement for using VM templates because VMware Tools are used when customization specifications are applied for changing IP configuration, host name, time zone, or other guest OS settings when VMs are deployed from a template.
Removing unwanted software components from a VM. You can make your VM template more lightweight by optimizing the guest OS. The following are some general recommendations for Windows systems that may be useful for configuring your VMs:
- Disable unnecessary system services, such as wireless services, themes, Windows Audio, Windows Search, etc.
- Disable Windows Aero, as it includes graphic effects that consume significant amounts of computing resources.
- Disable the screensaver to save some CPU resources.
- Uninstall tablet PC components.
- Uninstall unnecessary Windows components, such as Games, Messenger, etc.
- Disable all unnecessary startup applications and services.
- Delete temporary files, including files used for software iInstall the SSH server nstallation.
- System restore can be disabled. You can use VMware snapshots instead of the Windows system restore functionality. Use NAKIVO software to back up your VMs for robust data protection.
You can carry out a deeper cleanup of your guest OS to optimize VM performance depending on your demands.
In the following sections, you can learn about configuring a Linux virtual machine for the purposes of making a VM template (with which you could use guest OS customization specification). This walkthrough uses the example of Xubuntu 16 x64. It also discusses how to create a customization specification for further cloning of VMs from your template.
In order to prepare Xubuntu Linux for guest OS specification customization, enter the Linux console (Terminal) and run the following commands as a user with root privileges:
Install the SSH server for remote access via SSH.
apt-get install openssh-server
Restart the SSH server after installation.
service ssh restart
Install VMware Tools for Linux from repositories (The alternative way to install VMware Tools is by inserting an ISO image into a virtual CD drive of a VM).
apt-get install open-vm-tools
Install the networking toolkit.
apt-get install net-tools
apt-get install perl
Once the packages have been installed, check the computer name and IP configuration of the Linux VM. Type ifconfig to view the IP configuration of network interfaces.
In this example, the virtual network interface used by the source VM has an IP address of 192.168.77.2
Creating a VM Template
Once your VM has been prepared, you can create a VM template. You must have access to vCenter Server for managing your ESXi hosts, virtual machines, storage, networks and other vSphere components. As mentioned above, in the configuration examples considered in this blog post, a VM running Xubuntu Linux is used. Two methods can be used for VM template creation – converting to a template and cloning to a template.
Converting to a template
When you convert a VM to a template, your source VM gets replaced by the resulting template, and as a result, your source VM would be unavailable. In order to convert a VM to a template, take the following steps:
- Log into vCenter with VMware vSphere Web Client. Go to Hosts and Clusters or VMs and Templates, then select the VM that you have prepared for conversion.
- Ensure that the VM is powered off when you convert it into a template.
- Right-click the VM, and in the resulting context menu, select Template > Convert to Template.
- A confirmation message is displayed: Convert the virtual machine “VMname” to a template? Click Yes to proceed.
Cloning to a template
This option allows you to preserve the original VM and copy the VM data to a template. The source VM can be powered on or off while you are cloning the VM to a template. Let’s consider how to create a template by cloning the original VM. Similarly to the previous method, navigate to your VM in the vSphere Web client. Right-click the VM, and in the context menu click Clone > Clone to Template.
A cloning assistant is launched. Proceed through the steps as follows.
- Select a name and folder. Define a name for your VM template and select a target location. In this example, Xubuntu16-test is the template name, because this template is going to be used for testing purposes. Click Next.
- Select a compute resource. You can select an ESXi host or a cluster as your resource. Click Next.
- Select the storage location in which to store the template. You can set a virtual disk format (thin-provisioned, lazy zero thick-provisioned, or eager zero thick-provisioned) or simply let the virtual disk format be defined by that of its source. You can define a custom VM storage policy here, if needed. Click Next.
- Ready to complete. Check the summary and click Finish to proceed with the template creation.
- Now, your newly created VM template is displayed at the end of the list in the VMs and Templates section. As you can see, the storage usage of the original VM and the VM template is the same (compression is not used for VM templates, as mentioned above).
If you go to the Storage section, then select the datastore and the directory of your VM template, you should see that all the files are similar to the files of your source VM except for one – the VMTX file. The VM configuration file has a .VMTX extension instead of a .VMX extension, which is used for configuration files of regular VMware VMs.
Creating a VM Guest Customization Specification
Now that a VM template is created, you can create a VM guest customization specification. In order to do this, open the Menu, then click Policies and Profiles.
Make sure that VM Customization Specifications are selected in the left pane. Click New to create a new VM customization specification. You should then see a new wizard window launched.
- Name and target OS. Set a unique name for the specification, enter a description, select the vCenter server, and specify the guest OS of the target VM (Windows or Linux). In this walkthrough, Linux is used, and the specification is called Linux test spec. Click Next.
- Computer Name. Set a computer name (host name) and domain name. There are three available options to specify a computer name:
- Use the virtual machine name. The computer name of the guest OS inside the VM would be the same as the VM name. Using this option can be convenient for administration purposes.
- Enter a name in the Clone/Deploy Wizard. You can manually enter a computer name when you apply the customization specification for a VM clone. You can use this option if you don’t know which names you want to use for these VMs in the future.
- Enter a name. You can enter the computer name right now. In order to ensure uniqueness, tick the Append a numeric value checkbox (if you deploy multiple VMs, each VM must have a unique computer name for the guest OS).
- A computer name cannot be more than 63 characters in length – if you try to input anything longer, the name gets truncated. Click the Next button to continue.
- Time Zone. Specify a time zone for the virtual machine. Then click Next.
- Network. Specify the network settings for the virtual machine. There are two options:
- Use standard network settings for the guest OS, including enabling DHCP on all network interfaces.
- Manually select custom settings.
- For the purposes of this walkthrough, the second option is used; let’s configure the network settings manually. You can click the Add button to add a virtual network interface controller (NIC) to your VM. Click the icon with the row of three dots and select Edit in the resulting context menu.
- A new configuration window is launched. There are three options available in the IPv4 tab.
- Use DHCP to obtain an IP address automatically.
- Prompt the user for an IP address when the specification is used. You can preconfigure a subnet mask and default gateways using this option, but the IP address must be entered when the specification is applied to the VM.
- Use custom settings. This option is similar to the previous one, but the IP address can be set right now.
- IPv6 can be disabled. Click OK when you have finished configuring the network options. Then click Next.
- DNS settings. Specify the DNS servers and DNS search paths for the VM. You can set up to three DNS servers. Click Next.
- Ready to complete. Review your settings and click Finish when you are satisfied.
- You should now be able to see your guest OS customization specification in the list of VM customization specifications. You can edit, duplicate, export, and delete the specifications from here.
Creating a VM from a Template
Now that you have a VM template and a guest OS customization specification, you can create a virtual machine from your template. Go to the VMs and Templates section, right-click the template you created, and select New VM from This Template. In this case, the template is preserved. If you use the Convert to Virtual Machine option, then your template gets converted to a VM; it would no longer exist as a template.
The Deploy From Template wizard is launched.
- Select a name and folder. At this stage, you should input a unique VM name and a target location. In this walkthrough example, the virtual machine is named Xubuntu16-test01. Click Next to continue.
- Select a compute resource. Here, you can select the ESXi host or cluster on which the VM should run. Click Next.
- Select storage. Select the datastore in which to store configuration and disk files. Select the virtual disk format. You can manually set the virtual disk format (thin-provisioned, lazy zero thick-provisioned, or eager zero thick-provisioned) or leave the virtual disk format the same as that of the source. Select a VM storage policy if necessary. Click Next.
- Select clone options. In order to apply your recently-created guest OS customization specification, tick the Customize the operating system checkbox. Then click Next.
- Customize guest OS. Select the guest OS customization specification you created from the list. Recall that in this walkthrough, the name of the specification is Linux test spec. Click Next to proceed.
- User settings. Recall from earlier in this walkthrough that the guest OS specification customization used here allows us to set the IP address during VM clone creation. Let’s define the IP address for a virtual network adapter of this VM. In this case, the IP address is 192.168.77.240. Click Next.
- Ready to complete. Check the configuration summary and click Finish to initiate VM creation.
Once the VM has been created from a template with your guest OS customization specification, run the VM and launch the VMware Web Console to access the guest OS interface. Once you log into the VM, the VM is restarted and your customized settings are applied. After the VM has rebooted, open the Linux console and verify the host name, IP configuration, and time zone. You can check these parameters by entering the following commands.
View the host name:
You can also see the host name in the primary prompt string.
Check the IP configuration of network interfaces:
Check the time zone set in the system:
timedatectl | grep "Time zone"
Check the gateways in the routing table:
As you can see from the above screenshot with the Linux console and the below screenshot showing VMware vSphere client, the IP address, computer name, and time zone have been changed according to the settings defined in our guest OS customization specification. The VM name and the host name are the same.
Automation with PowerCLI
The walkthrough above has explained the creation and application of VM templates with the vSphere Web Client GUI (graphical user interface). However, alternatively, you could use PowerCLI to manually create complex scripts and make VM deployment more automated. Some of the cmdlets that could be useful for these purposes are listed below:
- New-Template – creates a new VM template.
- New-OSCustomizationSpec – creates a new guest OS customization specification.
- Get-OSCustomizationSpec – retrieves the OS customization specifications from vCenter.
- Set-OSCustomizationSpec – changes the specified OS customization specification.
- Get-OSCustomizationNicMapping – retrieves the NIC mapping settings for OS customization specification.
- Set-OSCustomizationNicMapping – change the NIC mapping settings for the specified OS customization specification.
- New-OSCustomizationNicMapping – adds the NIC mapping settings to the OS customization specification.
For the full list of PowerCLI cmdlets, their syntax, examples and other related information, refer to the VMware PowerCLI Reference web page. You can also read our blog post about PowerCLI scripting.
Advantages of VM Templates
VM templates with guest OS customization specification are useful for VM deployment, especially in large environments. The primary advantages of using VM templates are summarized here:
- Standardize VM deployments. Standardization allows you to avoid redundant efforts as well as improving quality and productivity. Administration of standardized VMs is easier than administration of non-standardized VMs.
- Fewer errors during deployment. When several virtual machines must be deployed manually in succession, there are many opportunities for a system administrator to forget something, which could cause errors. As an alternative, the system administrator can spend time once to create a perfect VM template; this can then be used for all further (similar) VM deployments going forward.
- Automation of the VM deployment process. Automation makes deployment easier and faster, increasing productivity while improving security.
- Time savings. This benefit is related to the advantages listed above that make templates an efficient solution. Furthermore, saving time means reducing costs.
A VM template is a master image that can be used for making clones during mass VM deployment. VM templates cannot be edited and powered on like regular VMs, which improves security. Guest OS customization specification helps you customize OS settings like network configuration, computer name, time zone etc. after VM cloning by using the single wizard, thus making VM deployment faster and more convenient. As a result, you gain important advantages such as automation, reduced error count, standardization, and rational time consumption – which can also help you save your budget!