Search
StarWind is a hyperconverged (HCI) vendor with focus on Enterprise ROBO, SMB & Edge

How to clone VMs if VMware vCenter is unavailable

  • May 22, 2018
  • 21 min read
Cloud and Virtualization Architect. Kevin focuses on VMware technologies and has vast expertise in cloud solutions, virtualization, storage, networking, and IT infrastructure administration.
Cloud and Virtualization Architect. Kevin focuses on VMware technologies and has vast expertise in cloud solutions, virtualization, storage, networking, and IT infrastructure administration.

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 server

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:

You can check whether copying is over in Recent Tasks (the task must have the Completed successfully status). Please note that cloning always takes some time. Even if you clone the VM within the datastore, you should better have a break as that process may take a while. How long will it take? Well, it depends on your hardware and virtual disk volume. PLEASE, NEVER REGISTER THE VM AND DON’T START IT UNTIL THE DISK IS COPIED, OTHERWISE YOU WON’T BE ABLE TO START IT!

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: xx

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. Once you modify parameters, save the changes. Find out more about working with vi here: https://kb.vmware.com/s/article/1020302

VM-Main Second codes

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

Type ls to check the directory content. Here, the VM-Second content.

execute vim-cmd vmsvc/getallvms to get the list of the registered virtual machines.

Execute cd .. to go up a directory level.

Register the VM with the vim-cmd solo/registervm /vmfs/volumes/5a251c85-badd3a98-c863/VM-Second/VM-Second.vmx command.

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. If you encounter the issue with wrong certificate, here’s how you can fix it: https://www.ivobeerens.nl/2018/07/18/quick-tip-powercli-invalid-server-certificate-error/?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+VirtualIef+%28Virtual+Ief%29. 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:

http://www.starwindsoftware.com/blog/vmware/wp-content/uploads/2018/05/1-1-1-1024x25.png

Note that I the script copies VM files to the temporary directory on the PC from which you run it. Note that the VM should be powered off and it should not have snapshots. The time needed for copying virtual disks depends on their disk volume and the network bandwidth between ESXi host and VM. Note that the script provided below can clone the VM only within the datastore! Datastore name is detected automatically.

Now, let’s take a look at the script.

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

The VM has been cloned successfully

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 simplest 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!

Found Kevin’s article helpful? Looking for a reliable, high-performance, and cost-effective shared storage solution for your production cluster?
Dmytro Malynka
Dmytro Malynka StarWind Virtual SAN Product Manager
We’ve got you covered! StarWind Virtual SAN (VSAN) is specifically designed to provide highly-available shared storage for Hyper-V, vSphere, and KVM clusters. With StarWind VSAN, simplicity is key: utilize the local disks of your hypervisor hosts and create shared HA storage for your VMs. Interested in learning more? Book a short StarWind VSAN demo now and see it in action!