Add new virtual hard disk to VM, specifying a datastore

1,641 views
Skip to first unread message

Dan

unread,
Aug 8, 2012, 1:43:24 PM8/8/12
to pysp...@googlegroups.com
Hi,

I am trying to write a script to add another virtual hard disk to a VM that I have already created and I would like the vmdk created by adding the hard disk to be stored in one of the datastores I already have created.  To be clear, the steps I would take in vCenter are as follows...

Right click a VM
Edit settings...
Hardware tab -> Add...
Hard Disk (Next)
Create new virtual disk (Next)
// This is key for me, being able to specify a datastore
Location: Specify a datastore [Browse] (Next)
//The rest is just default clicking next and finish.

I've look through previous posts and I found this that seemed to be asking a similar thing (https://groups.google.com/forum/?fromgroups#!topic/pysphere/nY2fUDvxVjw%5B1-25%5D) but there is no reply to it.  I've also tried poking around the code but the details of creating these request objects are quite difficult for me.  I would much appreciate any help on this issue.

Thanks,
Dan

Seba

unread,
Sep 7, 2012, 9:28:20 AM9/7/12
to pysp...@googlegroups.com
Hey Dan,

The following code snippet should do what you need, tweak the parameters to your needs. If the unit number is taken by another device you should try incrementing it. For simplicity, I haven't added here logic to scan pre-existing unit numbers.

Regards,

Seba.


USER = "root"
PASSWORD = "secret"

DATASTORE_NAME = "datastore1" #WHERE THE DISK WILL BE CREATED AT
DISK_SIZE_IN_MB = 500
VM_PATH = "[datastore1] path/to/the/config/file.vmx"
UNIT_NUMBER = 1

from pysphere import VIServer, VITask
from pysphere.resources import VimService_services as VI


s = VIServer()
s.connect(HOST, USER, PASSWORD)

vm = s.get_vm_by_path(VM_PATH)


request = VI.ReconfigVM_TaskRequestMsg()
_this = request.new__this(vm._mor)
_this.set_attribute_type(vm._mor.get_attribute_type())
request.set_element__this(_this)
    
spec = request.new_spec()

dc = spec.new_deviceChange()
dc.Operation = "add"
dc.FileOperation = "create"

hd = VI.ns0.VirtualDisk_Def("hd").pyclass()
hd.Key = -100
hd.UnitNumber = UNIT_NUMBER
hd.CapacityInKB = DISK_SIZE_IN_MB * 1024
hd.ControllerKey = 1000

backing = VI.ns0.VirtualDiskFlatVer2BackingInfo_Def("backing").pyclass()
backing.FileName = "[%s]" % DATASTORE_NAME
backing.DiskMode = "persistent"
backing.Split = False
backing.WriteThrough = False
backing.ThinProvisioned = False
backing.EagerlyScrub = False
hd.Backing = backing

dc.Device = hd

spec.DeviceChange = [dc]
request.Spec = spec

task = s._proxy.ReconfigVM_Task(request)._returnval
vi_task = VITask(task, s)

#Wait for task to finis
status = vi_task.wait_for_state([vi_task.STATE_SUCCESS,
                                 vi_task.STATE_ERROR])
if status == vi_task.STATE_ERROR:
    print "ERROR CONFIGURING VM:", vi_task.get_error_message()
else:
    print "VM CONFIGURED SUCCESSFULLY"

s.disconnect()

Sriram Ramakrishnan

unread,
Nov 30, 2012, 11:23:29 AM11/30/12
to pysp...@googlegroups.com
First things first ... Great work!!  

I wanted to add a disk to a Datastore cluster. Is that possible.?

Thanks
Sriram

bande...@gmail.com

unread,
Dec 15, 2012, 11:13:52 AM12/15/12
to pysp...@googlegroups.com


Can I also add a pci pass through device to a Virtual Machine(VM) using this?  Have some devices on my ESXi Host configured in PCI passthrough(Direct Path IO configuration).

Seba

unread,
Jan 2, 2013, 10:47:14 AM1/2/13
to pysp...@googlegroups.com
Hi Sriram,

  I'm not sure about what you need to do. Just to have a better understanding, can you do it from the vSphere client? How?

Regards,


Seba

Seba

unread,
Jan 2, 2013, 10:56:27 AM1/2/13
to pysp...@googlegroups.com
Yes, you should be able to do that. However I'm not sure how that should be configured (I don't have that device configuration to test this).

But try changing this (from the code snippet):


hd = VI.ns0.VirtualDisk_Def("hd").pyclass()

hd.Key = -100
hd.UnitNumber = UNIT_NUMBER
hd.CapacityInKB = DISK_SIZE_IN_MB * 1024
hd.ControllerKey = 1000


To this:

hd = VI.ns0.VirtualPCIPassthrough_Def("hd").pyclass()

hd.Key = -100
hd.UnitNumber = UNIT_NUMBER
hd.ControllerKey = 1000


Let me know if that doesn't work so I can help you configuring your PCI Passthrough device

Regards,

Seba

Sylus Anuranjan

unread,
May 13, 2014, 6:45:50 AM5/13/14
to pysp...@googlegroups.com
Hi Seba,

Thank you for your post. It is very helpful and i was able to add a hard disk.

However , i am trying to add a pci device and am not sure what the values are

hd.Key = -100
hd.UnitNumber = UNIT_NUMBER
hd.ControllerKey = 1000

I would be very grateful if someone  could explain the above values. From vsphere , i use the pci address to associate a  pci device to the vm.

Regards
Message has been deleted

孟岩

unread,
Dec 10, 2017, 7:17:53 AM12/10/17
to pysphere
Hi Seba,

I have added a vdisk to VM successfully, that's great, thanks your codes.
Could give me some suggestions about how to scan pre-existing unit numbers?

thanks!

在 2012年9月7日星期五 UTC+8下午9:28:20,Seba写道:
Reply all
Reply to author
Forward
0 new messages