How to clone VMs if VMware vCenter is unavailable

Introduction

There are various tricks and hints we all use to make our daily system administration routines easier. One of them is virtual machine cloning provided by VMware vCenter Server. Great and simple thing allowing you to deploy many identical virtual machines to a group – no need to repeat the same process all over again. This is usually done in vCenter but there are several other ways you can go if it becomes unavailable.

Today, I’ll discuss three workarounds allowing you to clone VMs when running unmanaged ESXi:

  • VMware vSphere Web Client
  • SSH
  • VMware PowerCLI

You can view this scribbling as a thing about not only cloning VMs without vCenter but also as a principle in the wide sense. In fact, all operations you usually carry out in your environment via GUI can be performed with either the vSphere Web Client or PowerShell commands. Just think through them and learn the right commands if needed. Nothing more than that!

Let’s break the cloning process without vCenter in detail

Before I go deeper, I want you to understand the stages that cloning process consists of. I believe this to be essential for understanding what’s exactly going on in my article.

The gist of cloning a source VM in case something happened to your vCenter is creating a target VM with just the same resources assigned to it as the source VM has. Next, the source *.vmdk and *.vmx files or disks are copied to the target’s folder. Eventually, the virtual disks are assigned to the target VM, and the clone gets registered and booted up.

Hey, where are the snapshots? VMware does not recommend cloning VMs with snapshots. So, either consolidate them or get rid of them.

VMware vSphere Web Client

Thanks to the familiar GUI, this is the easiest way to copy VM if VMware vCenter is unavailable. You’ll need just a web browser and several clicks to have the job done.

First, type your credentials and access the VMware ESXi server:

vmware esxi

Navigate to the Virtual Machines menu and select the VM you need to clone. In my case, there is only one VM (VM-Main), running on the host so I’ll clone it:

vmware navigator

Now, learn more about the VM properties. In particular, find out where the source VM disk is located. In my case, it is kept on Datastore-f:

VM-Main Datastore

Next, click Datastore browser to find the disk itself:

vmware esxi datastore browser

Create a new directory on the datastore:

Datastore create

Copy *.vmdk and *.vmx files from the source VM directory. Once files are copied, register the VM:

source VM

Afterward, go to Edit Setting and check whether the recently copied *.vmdk is assigned to the target VM:

Virtual Machines settings

Well, that’s it! Settings say that the target VM uses the right *.vmdk file, so the source VM has been cloned:

VM-Main Datastore Second

Eventually, rename the target VM before starting. For that purpose, go to Actions and press the appropriate button:

Rename Virtual Machine

Now, the cloned VM is ready to go. Click Power on to boot it:

VM power on

SSH

By default, SSH is disabled on an ESXi host. In order to enable it, navigate via a web browser to the Services tab, select SSH, and press the Start button:

TSM-SSH

Below, I’ve provided several screenshots of the deployed commands. I do not want the entire thing to appear kinda of a black box, so I briefly explain what each command actually does. The numbers in the text refer the number of the step in the screenshots.

Just like in the previous part, you need to access the host using the root credentials first:

  1. Login: root
  2. Password: xxx

Next, execute the following commands.

  1. To get the list of all registered VMs, execute vim-cmd vmsvc/getallvms. In my case, there is only VM-Main listed.
  2. To obtain the list of datastores, execute ls -lh vmfs/volumes. In my case, there will be only one datastore listed: Datastore-f.
  3. Now, let’s move to another directory. Type the cd command for that purpose. Here, I navigate to the Datastore-f directory.
  4. Type ls. This command provides you with the directory content. Here, the content of Datastore-f.
  5. To create a new folder with the specified name, enter mkdir <name folder>. Now, I use this command to create a directory where VM files will be copied.
  6. Execute ls again. This time, it serves for verifying a new directory creation.
  7. Next, use cd to move into to the recently created VM directory.
  8. Afterward, execute the ls command to check the VM directory content.
  9. Copy files from the source directory (VM-Main) to the target one (VM-Second) with the cp command.

After executing the cp command, the console won’t respond for a while. Just wait a bit until the command works out.

codes

Next, run the following set of commands:

  1. Type cd .. to navigate to the upper directory.
  2. Next, deploy the ls VM-Second command to acquire the VM-Second directory content.
  3. ls VM-Main, in its turn, provides the VM-Main content.
  4. Now, navigate to the source VM-Second directory with the cd VM-Second command.
  5. vmkfstools -E VM-Main.vmdk VM-Second.vmdk. This command allows renaming the *.vmdk file.
  6. Then, check whether the renaming worked out with the ls
  7. Afterward, type mv VM-Main.vmx VM-Second.vmx to rename the *.vmx file.
  8. Again, execute ls to check if the *.vmx file was renamed.
  9. vi VM-Second.vmx enables to modify the specified *.vmx file.

VM-Main codes

Once the last command is executed, change the fileName and displayName parameters as it is shown in the screenshot below.

VM-Main Second codes

Once these parameters are changed, check out whether the VM has been copied properly and register the VM:

  1. Type ls to check the directory content. Here, the VM-Second content.
  2. execute vim-cmd vmsvc/getallvms to get the list of the registered virtual machines.
  3. Execute cd .. to go up a directory level.
  4. Register the VM with the vim-cmd solo/registervm /vmfs/volumes/5a251c85-badd3a98-c863/VM-Second/VM-Second.vmx command.
  5. Enter vim-cmd vmsvc/getallvms to acquire the list of registered VMs. I’ve executed this command to check whether VM-Second has been registered properly.

VM-Second codes

You can also get the web-interface and check that the VM has been registered:

Manage VMware esxi

VMware PowerCLI

PowerCLI is a perfect tool for ones who want to automate the cloning jobs.

First, install VMware PowerCLI. Then, save the script I provide below as the Clone-vm.ps1 file, and run it via PowerShell as administrator.

That’s how to start the script:

.\Clone-VM.ps1 -EsxiHostName 10.0.0.10 -EsxiUser root -EsxiPassword -VMName VM-Main

Administration PowerShell

Pay attention to the VM size, since during the cloning, the Temp folder of the current user will be used.

The cloning speed depends on the connection between the workstation and the ESXi host.

Here is the script itself:

That’s it, the VM has been cloned successfully:

VMware localhost

Conclusion

Here, I’ve discussed three ways to clone a VM without having vCenter involved. It comes in handy when vCenter is unavailable for some reason. The workarounds shown here for sure are not that convenient as cloning the VM with VMware vCenter. Using the vSphere Web Client is probably the most simple way among these three options. However, both SSH and PowerCLI do the job, plus, you now have the script to automate cloning jobs. Hope, the things I wrote here come in handy!