I have been trying to create a new vm with using existing vm's .vmx and .vmdk files from ESX host in Python using pyvmomi module. I am new to pyvmomi and there isnt much online help available for pyvmomi. I have checked sample code in github(https://github.com/vmware/pyvmomi-community-samples/tree/298bf74446f3fcc5743d6435763ff6dc16ab4cbc/samples) but no relevent things found. Please provide me pointers.
-
It sounds like you want to create a clone. Is that correct? – Michael Rice Mar 23 '15 at 16:43
-
I am working on backup-restore project where I have to restore existing vm/create a new vm out of previously backed up .vmx and vmdk files. – pythonuser Mar 23 '15 at 17:04
-
When you do this will the original VirtualMachine using that vmx and vmdk file be in the inventory, or will this be at a new location so it will be considered a new virtualmachine to the host or vcenter? – Michael Rice Mar 23 '15 at 17:29
-
Hi Mike, It will be considered new vm. It is stateless, so it could be new host or same host anything. – pythonuser Mar 23 '15 at 17:32
2 Answers
Lets say the Virtualachine name is: 1001-web1.myhost.com
Then the HostSystem you want to put it on is: host1.myesxhost.com
The host has a datastore called: Local1
The host lives in a Folder called: Prod
You need to get the files from the VM into the Local1 datastore. Ill let you figure that part out. They need to be placed into a folder called: 1001-web1.myhost.com
Next you need to find the Folder Prod and the HostSystem host1.myesxhost.com in the inventory so you have the ManagedObjectRef for them. You can use a searchIndex.FindByDnsName to find the host, and a searchIndex.FindByInventoryPath to find the Folder (or a few other methods..)
Once you have located the Folder Prod you can use a RegisterVM_Task to register the vm.
si = connect.SmartConnect(xxx)
host = si.content.searchIndex.FindByDnsName(None,"host1.myesxhost.com", False)
folder = si.content.searchIndex.FindByInventoryPath(xxx)
task = folder.RegisterVM_Task(path="[Local1] 1001-web1.myhost.com/1001-web1.myhost.com.vmx", name="new vm name", asTemplate=False, pool=None, host=host)
From here you should be able to monitor the process of the task once its completed successfully the task.info.result will contain the MOREF of the new VM.
This is pseudo code so the syntax may be off but this is the process you will have to follow.

- 7,974
- 1
- 15
- 18
If you know the datastore path to the vmx file you can use the following:
from pyVim import folder
folder.Register("[datastore]TestVM/TestVM.vmx")

- 5,101
- 3
- 35
- 44