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:
def add_hdd(vm):
# Begin the virtual machine editing operation.
try:
vm.begin_edit()
except prlsdkapi.PrlSDKAsyncError, e:
print "Unable to modify vm configuration: %s" % e
return
# Create a VmHddDev object.
hdd_dev = vm.create_device(consts.PDE_HARD_DISK)
# Populate the object.
# Set emulated type (image file or real device).
hdd_dev.emulated_type = consts.PDT_USE_IMAGE_FILE
# Set disk type (expanding or fixed)
hdd_dev.disk_type = consts.PHD_EXPANDING_HARD_DISK
# Se disk size to 20 Gig.
hdd_dev.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
# a full directory path and the hard disk name.
hdd_dev.friendly_name = vm.name + "_hdd_2.hdd"
hdd_dev.sys_name = vm.name + "_hdd_2.hdd"
# Enable the disk.
hdd_dev.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.PrlSDKAsyncError, e:
print "Error: %s" % e
return
# Commit the changes.
try:
vm.commit().wait()
except prlsdkapi.PrlSDKAsyncError, e:
print "Unable to commit vm config changes: %s" % e
return
print("Added new hard disk to " + '"' + vm.name + '"')