To create a virtual machine, you use the Folder.CreateVM_Task method. The method takes a
VirtualMachineConfigSpec data object as input argument.
VirtualMachineConfigSpec allows you to specify the attributes of the virtual machine you are creating.
If you need several identical virtual machines, you can convert an existing virtual machine to a template and create multiple copies (clones) from the template. You can also create multiple virtual machines by cloning an existing virtual machine directly.
Use the Folder.CreateVM_Task method to create a virtual machine by specifying its attributes. You must specify either a host or a resource pool (or both). The virtual machine uses the CPU and memory resources from the host or resource pool.
Create a virtual machine by calling the Folder.CreateVM_Task method with the following arguments:
The actual customization of the virtual machine happens through the properties of the VirtualMachineConfigSpec that is passed in as an argument to
Folder.CreateVM_Task. For example, you can specify the name, boot options, number of CPUs, and memory for the virtual machine. All properties of
VirtualMachineConfigSpec are optional to support incremental changes. See the
API Reference.
The following example fragment from the VMCreate sample program illustrates how to define a
VirtualMachineConfigSpec.
The VMware SDK SDK/samples/Axis/java/com/vmware/apputils/vim/VMUtils.java sample defines a more comprehensive virtual machine that also includes a Floppy, CD-ROM, disk, and virtual NIC. See
Configuring a Virtual Machine for a discussion of commonly set properties.
The VirtualMachineConfigInfo and
VirtualMachineConfigSpec objects provide the
extraConfig property for additional configuration information. The
extraConfig property is an array of key/value pairs that identify configuration options. The Server stores the
extraConfig options in the
.vmx file for the virtual machine. As the vSphere API evolves from version to version, an extraConfig option may become a standard configuration property that is part of the defined inventory data model. In this case, you must use the standard data model property for access; you cannot use the
extraConfig property.
Templates allow you to create multiple virtual machines with the same characteristics, such as resources allocated to CPU and memory, or type of virtual hardware. A virtual machine template is a virtual machine that cannot be powered on and that is not associated with a resource pool.
You can convert any powered off virtual machine to a template by calling VirtualMachine.MarkAsTemplate. After the conversion, the original virtual machine no longer exists. You can use the template to create multiple clones of the same configuration.
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.
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.
When you create a virtual machine, you are also creating a set of default devices, based on the hardware version associated with your SDK. You can see these devices using the
EnvironmentBrowser.QueryConfigOption method. For example, the IDE controllers are created by default. Many of these default devices contain properties that you cannot change.
However, you can add the following optional devices to the default set: VirtualSerialPort, VirtualParallelPort, VirtualFloppy, VirtualCdrom, VirtualUSB, VirtualEthernetCard, VirtualDisk, and
VirtualSCSIPassthrough. See the VirtualDevice Data Object in the
API Reference for more information about each of these optional devices.