Previous page

Next page

Locate page in Contents

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 machine configuration information. This information is needed to configure the new virtual machine, so it will run properly on the given host.
  2. Obtain a new prlsdkapi.Vm object that will identify the new virtual machine. This must be performed using the prlsdkapi.Server.create_vm method.
  3. Obtain an instance of the prlsdkapi.VmConfig object that will contain the new virtual machine configuration information. This step must be performed using the prlsdkapi.Vm.get_config method.
  4. 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.VmConfig.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 that have the PVS_GUEST_VER_ prefix in their names.
  5. Choose a name for the virtual machine and set it using the VmConfing.set_name method.
  6. 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.
  7. 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 with Parallels Service. 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 specify a different virtual machine directory name and path if you wish.

Sample

"""

    Create a new virtual machine.

"""

def create_vm(server):

  

    # Obtain the prlsdkapi.ServerConfig object.

    # The object contains the host machine configuration

    # information.

    try:

        result = server.get_srv_config().wait()

    except prlsdkapi.PrlSDKError, e:

        print "Error: %s" % e

  

    srv_config = result.get_param()

  

    # Obtain a new prlsdkapi.Vm object.

    vm = server.create_vm()

  

    # Obtain a prlsdkapi.VmConfig object.

    # The new virtual machine configuration will be performed

    # using this object. At this time the object will be empty.

    vm_config = vm.get_config()      

  

    # Use the default configuration.

    # Parameters of the  set_default_config method:

    #   param_1: The host machine configuration object.

    #   param_2: Target OS type and version.

    #   param_3: Specifies to create the virtual machine devices using

    #            default values (the settings can be modified

                 later if needed).

    vm_config.set_default_config(srv_config, \

                             consts.PVS_GUEST_VER_WIN_XP, True)

  

    # Set the virtual machine name and description.

    vm_config.set_name("My New XP machine")

    vm_config.set_description("Parallels Python API sample")

  

    # Modify the default RAM size and HDD size.

    # These two steps are optional. If you omit them, the

    # default values will be used.

    vm_config.set_ram_size(256)

    # Set HDD size to 10 gig.

    # The get_device method obtains a prlsdkapi.VmHardDisk object.

    # The index 0 is used because the default configuration has a

    # single hard disk.

    dev_hdd = vm.get_hard_disk(0)

    dev_hdd.set_disk_size(10000)

  

    # Register the virtual machine with the Parallels Service.

    # The first parameter specifies to create the machine in the

    # default directory on the host computer.

    # The second parameter specifies that non-interactive mode

    # should be used.

    print "Creating a virtual machine..."

    try:

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

    except prlsdkapi.PrlSDKError, e:

            print "Error: %s" % e

            return

  

    print "Virtual machine was created successfully."