slideshow 6 slideshow 4 slideshow 8 slideshow 1 slideshow 2 slideshow 3 slideshow 5 slideshow 7

Convert a Template to a Virtual Machine from the command line

The normal procedure to convert a template to a virtual machine is to right-click the template in the vSphere Web Client and select the menu-option Convert to Virtual Machine. You would do this for example if you need to update the template with new software, patches or other modifications before using it to deploy additional virtual machines. When the virtual machine is updated you then convert it back to a template.

But what to do if you try to convert the template to a VM and you can't because there's a problem with the template's configuration? In the image below you see a message that prevented me from converting a template to a virtual machine because it pointed to a datastore that was no longer available.  

Template conversion error

It's impossible to remove the disk from the Web Client because you cannot edit the VM settings of a template. So how do we convert it back to a virtual machine?

One solution is to deploy a virtual machine from the template. Then delete the template and use the newly deployed virtual machine as a template after making the corrections and modifications needed. Depending on the type of error this might work.

Another solution is to transform the template into a virtual machine from the command line. And this is simpler than you might think. If you compare the anatomy of a virtual machine to that of a template in the file system you will find that the only difference is that a virtual machine has a .vmx configuration file and a template has a .vmtx configuration file. (With the T of template added.) So if we change the filename it's done. Really.

Convert the Template from the Command Line

First we need to remove the template from the vCenter Inventory so that we don't confuse vCenter with  a non-existent template. You can see in the image below that we can easily do this from the menu when right-clicking the template and selecting All vCenter Actions - Remove from Inventory.

TipTip: Before removing the template from the inventory make sure that you know in what datastore the files are located because once it is removed you will no longer be able to find that information. In the image above it is one of the two listed datastores and it's not going to be the one with the error message;-) 

WarningWarning: Make sure to only proceed if you have verified that you can find the template directory on the datastore (for example by browsing through the files on the datastore from the vSphere Web Client). There is a slight chance that the template was renamed and that the display name in your inventory is not the same as the name of the directory on the file system. If that is the case first try to find the template on the right datastore.

Remove template from vCenter inventory

Now it's time to access the ESXi-host from the command line. You can do this in several ways: directly on the console (by enabling this from the Direct Console User Interface - DCUI) or with an SSH-client  such as Putty or from Linux natively. For SSH-access that service must be enabled. Read all about how to work with the command line in this VMware's document: Getting Started with vSphere
Command-Line Interfaces

Once you're on the command line change your working directory to where the template's files are located (with the cd command. The path from the root of the file system will be in /vmfs/volumes and then the name of the datastore and the name of the template. In the image below you see the files listing of a template, created with the ls -l command.

vCenter template files

Now that you have located the files it's time to rename the .vmtx file to .vmx. Renaming a file is actually done by moving it from the original location (file) to the new location (same file, new name) with the mv command.

TipTip: Use tab-completion to make your life simpler and to prevent from typing mistakes. After the mv command type the first letter of the file name (case sensitive) and press tab to auto-complete the file name. Or press tab twice to display a list of choices if there is more than one.

rename template vmtx to virtual machine vmx

Now that the file is renamed we are not there yet. We have removed the template from the inventory and this virtual machine now will not automatically show up in the inventory. You could either browser to the .vmx file from the vSphere Web Client to add it to the inventory or do in from the command line now that you are there anyway. Here is the command to perform this task:

vim-cmd solo/registervm <path to the vmx file>

register vm from the command line with vim-cmd command

The virtual machine is now registered in the vCenter Inventory and can be managed from the vSphere Web Client. When you are done with your modifications or repair actions on this machine you can convert it to a template from the vSphere Web Client from the menu if you right-click the virtual machine, you will find that option under the All vCenter Actions listing.


Follow us on LinkedIn