Creating a Virtual Machine

15 06 2010

A virtual machine (VM) is, simply speaking, a software computer.  Conceptually, creating a virtual machine is not difficult. It is almost identical to assembling a physical computer: you need a CPU, memory, and other devices. There are additional hoops to jump through: you’ll need to specify where the virtual machine will draw its resources, which host it will run on, the datastore which will hold its files, and so on. Implementing it all in a program can be overwhelming. This Lightning Guide will walk you through the process of creating a functional, usable VM… in a flash.

Three steps are required before calling CreateVM_Task():

  1. Retrieve data from the server
  2. Configure basic virtual machine settings
  3. Add key devices

1. Retrieve data from the server

The following is a list of server-side objects and properties required to create a virtual machine:

  • Managed Object Reference (MOR) to a Folder to hold the VM. Argument for CreateVM_Task(). Property to retrieve:
    • childType to find a folder that can hold virtual machines.
  • MOR to a ComputeResource. Properties to retrieve:
    • resourcePool to obtain MOR to a ResourcePool the VM will belong to. Argument for CreateVM_Task()
    • host to obtain MOR to a HostSystem on which the VM will run.  Argument for CreateVM_Task()
    • environmentBrowser to obtain MOR to an EnvironmentBrowser.
  • MOR to a Datastore. Used to store VM’s files and back the virtual hard disk. Properties to retrieve:
    • host to ensure that the VM’s host can access the datastore
    • name to specify file paths for VM’s files.

Use the RetrievePropertiesEx() method of the property collector to retrieve the array of ObjectContent data objects. Use loops to extract the data into appropriate variables.

  • Folder.childType is an array of strings specifying the types of managed objects the folder can hold. Make sure “VirtualMachine” is one of these.
  • Datastore.host is an array of hosts that can access the datastore. Make sure the target host is included in the array.

Use the QueryConfigOption() method of the environment browser to obtain a VirtualMachineConfigOption data object for the target host.

  • The defaultDevices property contains an array of VirtualDevice data objects. Cycle through the array and find a VirtualIDEController and a VirtualPCIController. Store their keys into variables.

Use the QueryConfigTarget() method of the environment browser to obtain a ConfigTarget data object for the target host.

  • The cdRom property contains an array of VirtualMachineCdromInfo data objects. Select one, and store its name property in a variable.
  • The network property contains an array of VirtualMachineNetworkInfo data objects. Select one, and store its name property in a variable.

You will use the keys and names during device addition.

CHECK OUT THE CODE!

2. Configure basic virtual machine settings

Create a VirtualMachineConfigSpec data object. Set: (required are underlined)

  • The name property to your name of choice.
  • The memoryMB property to the desired memory size. Keep in mind that different operating systems have different minimum requirements! Default is 32 MB.
  • The guestId property to the desired operating system identifier (Here’s a list).
  • The cpuAllocation and memoryAllocation properties to separate ResourceAllocationInfo data objects specifying reservations and limits on memory/CPU use.
  • The fileInfo property to a VirtualMachineFileInfo data object.  Setting the VirtualMachineFileInfo.vmPathName property to “[<datastoreName>]”  will create a folder on the specified datastore with the same name as name and store all virtual machine files in said folder.

CHECK OUT THE CODE!

3. Add Key Devices

There are three (3) key virtual devices a virtual machine should have:

  • Hard Disk for data storage
  • CDRom for operating system installation
  • Ethernet card to connect to network

In addition, a SCSI Controller is required to connect the Hard Disk to the virtual machine.

You add devices using the VirtualMachineConfigSpec.deviceChange property. There are three (3) general steps to adding a device:

  1. Specify the physical backing for the device using the appropriate Backing data object.
  2. Create the VirtualDevice data object for your particular device. Set the key property to a unique integer. No two devices on a virtual machine should have the same key.
  3. Create a VirtualDeviceConfigSpec data object for you device. Specify that the device is to be added using the operation property and, if applicable, specify that files associated with the device are to be created using the fileOperation property.

SCSI Controller

<CODE?>

  1. No backing is required for controllers.
  2. Create a VirtualBusLogicController data object.
    1. Set the sharedBus property to VirtualSCSISharing.noSharing
    2. Set the busNumber property to a number of your choice. Bus numbers should be unique among controllers.
    3. Set the key property to a number of your choice. You will need the key to connect devices to the controller
  3. No files associated with the device.

Hard Disk

<CODE?>

  1. Create a VirtualDiskFlatVer2BackingInfo data object to back the device using a file on the datastore
    1. Set the diskMode property to the desired disk persistence mode.
    2. Set the fileName property to the target location for the file using the “[<datastoreName>]<folderName>/<folderName2>/” format. A potential location is the location of the other virtual machine files (Step 2).
    3. Set the datastore property to the datastore reference acquired in Step 1.
  2. Create a VirtualDisk data object
    1. Set the backing property to the backing object
    2. Set the capacityInKB property
    3. Set the

Ethernet Card

<CODE?>

CDRom

<CODE?>

Finally, store all the VirtualDeviceConfigSpec objects into an array, and set the VirtualMachineConfigSpec.deviceChange property to the array.

You now have everything you need to create a virtual machine. To complete this Herculean task, call the CreateVM_Task() method.

<Code?>

Congratulations! A virtual machine is on its way to your server.

NEXT STEPS:

  • Installing Guest OS
  • Monitoring progress of operations (such as CreateVM!)

RELATED TOPICS:

  • Using the Property Collector
  • Managing a Virtual Machine







Follow

Get every new post delivered to your Inbox.