Customize basic HW settings for template or VMclone

1,200 views
Skip to first unread message

maumercado

unread,
May 21, 2012, 10:26:08 AM5/21/12
to pysp...@googlegroups.com
Hello Seba,

I was wondering how could I customize a template, or a vm, standard hardware, I mean I want to add more disk space, or more ram memory into a vm or a template... How would I do that?

Im using pysphere 0.1.6

Thank you!

maumercado

unread,
May 21, 2012, 10:42:46 AM5/21/12
to pysp...@googlegroups.com
I upgraded the source Im using to the trunk to access set_extra_config, and when I do the function over a vm instance the ESX vcenter does receive the task but it does not change the memory size, I did the following

vm = s.get_vm_by_name('X')
vm.set_extra_config({'memory':'256'}, sync_run=False)

As stated before, the VCenter server receives the task as it states on the task list reconfiguring vm, but when I check the vm properties is has the original memory size, meaning that it changed nothing...

So how could I change the memory size, or maybe the cpu core count? or disk size? ... Standard HW configuration devices in general...

Thank you!

Seba

unread,
May 30, 2012, 8:42:22 AM5/30/12
to pysphere
Hi Mauricio,

set_extra_config won't work (I think that configuring a VM from
the .vmx file only works on VMWare workstation).

Here's a code snippet I've written to change the RAM size and disks
sizes on an existing VM. Modify these values to your needs:
* 'vm_path': the VM configuration file path (you can change this to
the VM name and use get_vm_by_name instead)
* 'memory_mb': the new size of RAM you want the VM to have (in MB)
* 'hd_sizes_kb': a dictionary with the virtual disks you want to
modify in size, the keys are the disk labels (as shown from the
vSphere Client tool), and the values are the size in KB you want each
disk to have.

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

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

vm_path = "[datastore1] My VM3/My VM.vmx"
memory_mb = 512
hd_sizes_kb = {'Hard disk 1': 1048576} #1 GB


vm_obj = server.get_vm_by_path(vm_path)

hd_to_modify = []
for dev in vm_obj.properties.config.hardware.device:
if dev._type == "VirtualDisk" and dev.deviceInfo.label in
hd_sizes_kb:
dev_obj = dev._obj

dev_obj.set_element_capacityInKB(hd_sizes_kb[dev.deviceInfo.label])
hd_to_modify.append(dev_obj)

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

#set the new RAM size
spec.set_element_memoryMB(memory_mb)

#Change the HDs sizes
dev_changes = []
for hd in hd_to_modify:
dev_change = spec.new_deviceChange()
dev_change.set_element_operation("edit")
dev_change.set_element_device(hd)
dev_changes.append(dev_change)
if dev_changes:
spec.set_element_deviceChange(dev_changes)


request.set_element_spec(spec)
ret = server._proxy.ReconfigVM_Task(request)._returnval

#Wait for the task to finish
task = VITask(ret, server)
status = task.wait_for_state([task.STATE_SUCCESS, task.STATE_ERROR])
if status == task.STATE_SUCCESS:
print "VM successfully reconfigured"
elif status == task.STATE_ERROR:
print "Error reconfiguring vm: %s" % task.get_error_message()

server.disconnect()

maumercado

unread,
Jun 12, 2012, 6:15:09 PM6/12/12
to pysp...@googlegroups.com
Hey seba nice script, and congratulations on the library...

Im having a problem on the last line, my server is just s, and another question, on the part that says if dev_changes, does that compare if Im really changing de devices configuration or just checks if there is something to be made?



>>> ret = s._proxy.ReconfigVM_Task(request)._returnval
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Users/maumercado/.venv/iscloud/src/pysphere/pysphere/resources/VimService_services.py", line 2863, in ReconfigVM_Task
    self.binding.Send(None, None, request, soapaction="urn:vim25/5.0", **kw)
  File "/Users/maumercado/.venv/iscloud/src/pysphere/pysphere/ZSI/client.py", line 250, in Send
    sw.serialize(obj, tc)
  File "/Users/maumercado/.venv/iscloud/src/pysphere/pysphere/ZSI/writer.py", line 141, in serialize
    elt = typecode.serialize(self.body, self, pyobj, **kw)
  File "/Users/maumercado/.venv/iscloud/src/pysphere/pysphere/ZSI/TC.py", line 617, in serialize
    pyobj.typecode.serialize(elt, sw, pyobj, **kw)
  File "/Users/maumercado/.venv/iscloud/src/pysphere/pysphere/ZSI/TCcompound.py", line 248, in serialize
    self.cb(elt, sw, pyobj, name=name, **kw)
  File "/Users/maumercado/.venv/iscloud/src/pysphere/pysphere/ZSI/TCcompound.py", line 397, in cb
    what.serialize(elem, sw, v, **kw)
  File "/Users/maumercado/.venv/iscloud/src/pysphere/pysphere/ZSI/TCcompound.py", line 248, in serialize
    self.cb(elt, sw, pyobj, name=name, **kw)
  File "/Users/maumercado/.venv/iscloud/src/pysphere/pysphere/ZSI/TCcompound.py", line 397, in cb
    what.serialize(elem, sw, v, **kw)
  File "/Users/maumercado/.venv/iscloud/src/pysphere/pysphere/ZSI/TC.py", line 512, in serialize
    self.serialize_text_node(el, sw, pyobj)
  File "/Users/maumercado/.venv/iscloud/src/pysphere/pysphere/ZSI/TC.py", line 464, in serialize_text_node
    text = self.get_formatted_content(pyobj)
  File "/Users/maumercado/.venv/iscloud/src/pysphere/pysphere/ZSI/TC.py", line 1004, in get_formatted_content
    return self.format %pyobj
TypeError: %d format: a number is required, not str


Thanks!

Seba

unread,
Jun 19, 2012, 8:50:51 AM6/19/12
to pysphere
Hi,
the "if dev_changes" line, checks that list isn't empty. I.e. the vm
has at least one virtual disk.

If the vm has not virtual disks, "hd_to_modify" will be an empty list,
therefore, nothing will be appended to dev_changes in the "for hd in
hd_to_modify:" loop, therefore there's no disk to resize.

Regarding the error, it seems you're passing a number parameter as
string, check your script, make sure that RAM and hard disk sizes are
integers (if you're passing those arguments from the command line,
remember to cast them to integers)

Regards,

Seba.

On 12 jun, 19:15, maumercado <maumerc...@gmail.com> wrote:
> Hey seba nice script, and congratulations on the library...
>
> Im having a problem on the last line, my server is just s, and another
> question, on the part that says if dev_changes, does that compare if Im
> really changing de devices configuration or just checks if there is
> something to be made?
>
> >>> ret = s._proxy.ReconfigVM_Task(request)._returnval
>
> Traceback (most recent call last):
>   File "<console>", line 1, in <module>
>   File
> "/Users/maumercado/.venv/iscloud/src/pysphere/pysphere/resources/VimService _services.py",
Reply all
Reply to author
Forward
0 new messages