Cloning a Virtual Machine

A clone is a copy of a virtual machine. The main difference between a virtual machine and a clone is that the VirtualMachine.config.template property is set to true.

You can create a clone in one of the following ways:

  • If you no longer need a specific instance of a virtual machine, but you want to use the virtual machine’s configuration as a template, use the VirtualMachine.MarkAsTemplate method. This method sets the config.template property to true, and disables the virtual machine.
  • If you want to use an existing virtual machine as a template, but keep the virtual machine, call the VirtualMachine.CloneVM_Task method to create a duplicate of the virtual machine.

If you use the VirtualMachine.CloneVM_Task method, you can customize certain attributes of the clone by specifying them in the VirtualMachineCloneSpec data object you pass in when you call the method.

The following code fragment from VMClone.java illustrates how you can customize a clone and specify a new location for it.

Cloning a Virtual Machine

VirtualMachineCloneSpec cloneSpec = new VirtualMachineCloneSpec();
VirtualMachineRelocateSpec relocSpec = new VirtualMachineRelocateSpec();
cloneSpec.setLocation(relocSpec);
cloneSpec.setPowerOn(false);
cloneSpec.setTemplate(false);

String clonedName = cloneName;

ManagedObjectReference cloneTask
            = service.cloneVM_Task(vmRef, vmFolderRef, clonedName, cloneSpec);

The VirtualMachine.CloneVM_Task method takes the source virtual machine, target folder, name, and VirtualMachineCloneSpec as arguments.

The VirtualMachineCloneSpec data object includes the location, power state, and whether the clone should be a template. The location, in turn, is a VirtualMachineRelocateSpec data object that specifies the target location (datastore, disk, and host or resource pool) and any transformation to be performed on the disk.