clone VMS using pysphere

1,349 views
Skip to first unread message

Minu

unread,
Jan 4, 2012, 10:33:24 AM1/4/12
to pysphere
Hi,

Is it possible to clone VM using pysphere? I would like to be able to
clone a VM (like ubuntu or windows VM) from a script.

Any pointers appreciated.

Thanks
Minu

Seba

unread,
Jan 4, 2012, 2:29:28 PM1/4/12
to pysphere, arora...@gmail.com
Hi Minu,

I found your request pretty interesting so I've added the clone()
method to the VIVirtualMachine object (in order to hide the vSphere
complexity).

So please check the current trunk version with this method implemented
(let me know if you don't know how to check out the last revision so I
can provide you with a binary installer or an egg)

I haven't added support to chose the datastore and resource pool where
the VM will be cloned (it uses the same settings than the original
VM).

Please notice that cloning VMs is not supported on ESX/ESXi servers,
you will need a Virtual Center infrastructure

This is how it works:

> from pysphere import *
> from pysphere.vi_virtual_machine import VIVirtualMachine
>
> s = VIServer()
> s.connect("virtualcenterhost", "user", "password")

#intance the VM
> vm = s.get_vm_by_path("[Datastore1] images/Debian 2.2 /Debian 2.2.vmx")

#here you might want to revert to a particular snapshot, to take the
clone
#from that state
#vm.revert_to_named_snapshot("my snpshot with updates")

#now invoke the clone method which returns a new vm instance
#this is how you run it synchronously it might take a while
> vm2 = vm1.clone("Name Of the Clone")
> vm2.get_status()
'POWERED ON'
> vm2.power_off()

There are other parameters you can provide.

1) By default, the VM will be powered on after it is cloned, you can
change this by doing:

> vm2 = vm.clone("Name of the Clone", power_on=False)

2) By default, the new VM will be added to the same Folder than to
original VM you can change this by providing the Folder name you wish
to use (it must be an existing folder)
> vm2 = vm.clone("Name of the Clone", folder="my folder")

3) If you want to run the clone task asynchronously do this
> task = vm.clone("Name of the Clone", sync_run=False)
> task.get_state()
'running'
> task.get_progress()
95
> task.get_state()
'success'
> vm2 = VIVirtualMachine(s, task.get_result())
> vm2.power_off()



Regards,


Sebastian.

craig

unread,
Jan 5, 2012, 5:25:53 AM1/5/12
to pysphere
Fantastic work Sebastian but I have a few questions.

1) You say we have to create a new folder but how do we do this via
pysphere?
2) Will the clone maintain the datastore for all partions? eg.
partition1 = datastore1, partition2 = datastore2

Seba

unread,
Jan 5, 2012, 9:20:36 AM1/5/12
to pysp...@googlegroups.com, craig...@gmail.com
Hi Minu,

  Please find my answers inline

Fantastic work Sebastian but I have a few questions.

1) You say we have to create a new folder but how do we do this via
pysphere?

No, you don't need to create a new folder. 
Without providing the folder attribute the same folder as the original VM will be used (default)
If you wish to use a different existing folder you should provide the folder's name
Then If you want to create a new folder, that's another separate method would need to implement.
 
2) Will the clone maintain the datastore for all partions? eg.
partition1 = datastore1, partition2 = datastore2

Truly I don't know, as it uses by default the same configuration than the original VM I suppose it must keep separate virtual disk partitions in different datastores if the original VM was configured like that. We'll have to test that.
However we can improve the method to allow more parameters and let the user choose which datastore to use for each particular virtual disk, and other settings like changing the size of RAM and number of CPUs.


Regards,

Sebastian. 

On Jan 4, 7:29 pm, Seba wrote:
> On Jan 4, 12:33 pm, Minu wrote:

craig

unread,
Jan 6, 2012, 5:00:47 AM1/6/12
to pysphere
Hi Seba,

I've been looking into how to create a new folder for my cloned VM
destination, but getting a little lost in python code & web sdk. I've
only recently started to learn python so struggling on how to do this
the vmware way. Would you be able to give me a little code snippet? It
would be great if the clone function could optionally create the
folder if it is not present.

Seba

unread,
Jan 9, 2012, 9:35:46 AM1/9/12
to pysphere, craig...@gmail.com
Hi Craig,
As folders are a hierarchical structure you would need to specify
under which existing folder you want to create the new folder.

See the code below, use either the portion #1 or #2. The first uses
the default 'vm' folder in a datacenter as parent for your new folder.
The second looks for a folder named "AnExistingFolder" in the
datacenter to be used as parent folder.

This is not working in my test environment, I'm getting an Operation
Not Supported error, I think folder creation is not supported on ESXi
(as the option is not shown either from the VI Client tool).

Regards,

Sebastian.


from pysphere import *
from pysphere.resources import VimService_services as VI

s = VIServer()
s.connect("host", "user", "password")

folder_name = "My New Folder"
datacenter_name = "ha-datacenter"
datacenters = s._get_datacenters()
dc = datacenters[datacenter_name]

#1) TO use a datacenter's main 'vm' folder as parent folder
dc_props = VIProperty(s, dc)
vm_folder = dc_props.vmFolder._obj

#2) Pick a parent folder by name (withing the selected datacenter)
vm_folder = None
parent_folder_name = "AnExistingFolder"
folders = s._retrieve_properties_traversal(property_names=['name'],
from_node=dc, obj_type='Folder')
for f in folders:
    print "Folder:", f.PropSet[0].Val
    if f.PropSet[0].Val == parent_folder_name:
        vm_folder = f.Obj
        break

if not vm_folder:
    raise Exception("Couldn't find folder %s in datacenter %s"
                    % (parent_folder_name, datacenter_name))

#create folder
request = VI.CreateFolderRequestMsg()
_this = request.new__this(vm_folder)
_this.set_attribute_type(vm_folder.get_attribute_type())
request.set_element__this(_this)
request.set_element_name(folder_name)
s._proxy.CreateFolder(request)

s.disconnect()

chuck j

unread,
Jul 5, 2013, 9:08:52 AM7/5/13
to pysp...@googlegroups.com, craig...@gmail.com
Can you please let me know state of VM once VM is Autologon. We need this because after cloning gets completed windows VM gets reboot for 2 to 3 times hence by that time we would not know the state of the machine. Are there any such flag were we keep on looking. based on the results our automation will decide the next step else we may need to wait for some time internal which does not looks to be optimal.

it would be great help if we get this information.

chuck j

unread,
Jul 5, 2013, 9:30:32 AM7/5/13
to pysp...@googlegroups.com, craig...@gmail.com

Can you please let me know state of VM once VM is Autologon. We need this because after cloning gets completed windows VM gets reboot for 2 to 3 times hence we need to know the exact state by which our automation script proceed with next steps. We know the PowerON, PowerOff state, but that is not solving our purpose. if we get to know some state of VM some thing like "READY TO USE" or AUTOLOGON, where we can proceed with installation of software on VM remotely.

Thanks in advance.

Harish Bommireddy

unread,
Sep 26, 2018, 3:16:49 AM9/26/18
to pysphere
Hi Sebastian!

Your comment was really helpful. But for some reason i am seeing the below error. Can you please let me know what might be happening ?

Here is a snippet of my code. I see below error while calling the clone method.

>>> vm = s.get_vm_by_name("retail-app-server-C11")

>>> vm.revert_to_named_snapshot("harish snapshot") 

>>> vm1 = vm.clone("harish-retail-app")

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

  File "/usr/local/lib/python2.7/dist-packages/pysphere/vi_virtual_machine.py", line 535, in clone

    FaultTypes.TASK_ERROR)

pysphere.resources.vi_exception.VIException: [Task Error]: An error occurred while saving the snapshot: Failed to quiesce the virtual machine.

Reply all
Reply to author
Forward
0 new messages