Previous page

Next page

Locate page in Contents

Print this page

Creating a New Virtual Machine

The first step in creating a new virtual machine is to create a blank virtual machine and register it with the Parallels Service. A blank virtual machine is an equivalent of a hardware box with no operating system installed on the hard drive. Once a blank virtual machine is created and registered, it can be powered on and an operating system can be installed on it.

In this section, we will discuss how to create a typical virtual machine for a particular OS type using a sample configuration. By using this approach, you can easily create a virtual machine without knowing all of the little details about configuring a virtual machine for a particular operating system type.

The steps involved in creating a typical virtual machine are:

  1. Obtain a prlsdkapi.SrvConfig object containing the host configuration information.
  2. Obtain a prlsdkapi.Vm object identifying the new virtual machine. This must be performed using the prlsdkapi.Server.create_vm() method.
  3. Set the default configuration based on the version of the OS that you will later install in the machine. This step is performed using the prlsdkapi.Vm.set_default_config() method. You supply the version of the target OS, and the method will generate the appropriate configuration parameters automatically. The OS version is specified using predefined constants. The constants are located in the prlsdkapi.prlsdk.consts module and their names are prefixed with PVS_GUEST_VER_.
  4. Choose a name for the virtual machine and set it using the Vm.name property.
  5. Modify the default configuration parameters if needed. For example, you may want to modify the hard disk image type and size, the amount of memory available to the machine, and the networking options. When modifying a device, an object identifying it must first be obtained and then its methods and properties can be used to make the modifications. The code sample provided in this section shows how to modify some of the default configuration values.
  6. Create and register the new machine using the Vm.reg() method. This step will create the necessary virtual machine files on the host and register the machine. The virtual machine directory will have the same name as the name you've chosen for your virtual machine and will be created in the default location for this Parallels Service. You may also specify a different virtual machine directory name and path if you wish.

The following sample function demonstrates how to create a new virtual machine.

def create_vm(server):

  

    # Obtain an object containing the host configuration info.

    srv_config = server.server_config

  

    # Obtain Vm object identifying the new virtual machine.

    vm =  server.create_vm()

  

    # Set the default configuration.

    # The second parameter specifies the target OS type and version.

    # The third parameter specifies to create virtual machine devices using

    # default values (the device settings can be modified later if needed).

    vm.set_default_config(srv_config, consts.PVS_GUEST_VER_WIN_XP, True)

  

    # Set the virtual machine name and description.

    vm.name = "My WinXP machine"

    vm.description = "Parallels Python API sample"

  

    # Modify the default RAM size and HDD capacity.

    # These two steps are optional. They are provided purely

    # as a demonstration.

  

    # Set RAM size to 256 MB

    vm.ram_size = 256

  

    # Set HDD size to 10 gig.

    # The get_device() method obtains an object identifying a device.

    # The first parameter specifies the device type. In this instance,

    # an object of class prlsdkapi.VmHddDev will be returned.

    # The second parameter specifies an index of the

    # device in the list. The value of 0 is used because the

    # configuration has just one hard disk.

    dev_hdd = vm.get_device(consts.PDE_HARD_DISK, 0)

    dev_hdd.disk_size = 10000

  

    # Register the virtual machine.

    # The first parameter (empty string) specifies to create a machine in the

    # default virtual machine directory.

    # The second parameter specifies to use the non-interactive mode.

    print "Creating a virtual machine..."

    try:

        vm.reg("", True).wait()

    except prlsdkapi.PrlSDKAsyncError, e:

            print "Error: %s" % e

            return

  

    print "Virtual machine created successfully!"    

Please send us your feedback on this help page