Previous page

Next page

Locate page in Contents

Adding a Hard Disk Drive

The following sample function demonstrates how to create a new image file and to add a new virtual hard disk to a virtual machine. The steps are:

  1. Mark the beginning of the editing operation.
  2. Create a device object representing the new disk.
  3. Set the device properties, including emulation type (image file or real device), disk type (expanding or fixed), disk size, and disk name.
  4. Create the image file.
  5. Commit the changes.

Example

"""

    Add a new virtual hard disk to the virtual machine.

"""

def add_hdd(vm):

  

    # Begin the virtual machine configuration editing.

    try:

        vm.begin_edit().wait

    except prlsdkapi.PrlSDKError, e:

        print "Error: %s" % e

        return

  

    # Obtain the VmConfig object containing the virtual machine

    # configuration information.

    vm_config = vm.get_config()    

  

    # Create an instance of the prlsdkapi.VmHardDisk class.

    hdd_dev = vm_config.create_vm_dev(consts.PDE_HARD_DISK)

  

    # Populate the object.

    # Set emulated type (image file or real device).

    hdd_dev.set_emulated_type(consts.PDT_USE_IMAGE_FILE)

    

    # Set disk type (expanding or fixed)

    hdd_dev.set_disk_type(consts.PHD_EXPANDING_HARD_DISK)

  

    # Set disk size to 20 Gig.

    hdd_dev.set_disk_size(20000)

  

    # Choose and set a name for the new image file.

    # Both the friendly_name and the sys_name properties must be

    # populated and must contain the same value.

    # The new image file will be created in

    # the virtual machine directory.

    # To create the file in a different directory,

    # the name must contain the full directory path and

    # the hard disk name.

    hdd_name = vm_config.get_name() + "_hdd_sample.hdd"

    hdd_dev.set_friendly_name(hdd_name)

    hdd_dev.set_sys_name(hdd_name)

  

    # Enable the disk.

    hdd_dev.set_enabled(True)

    

    # Create the image file.

    # First parameter - Overwrite the image file if it exists.

    # Second paramerer - Use non-interactive mode.

    try:

        hdd_dev.create_image(True, True)

    except prlsdkapi.PrlSDKError, e:

        print "Error: %s" % e

        return

  

    # Commit the changes.

    try:

       vm.commit().wait()

    except prlsdkapi.PrlSDKError, e:

        print "Error: %s" % e

        return

  

    print("New hard disk was created successfully.")