简体   繁体   English

从.vmx和.vmd​​k文件使用pyvmomi模块创建vm

[英]create vm using pyvmomi module from .vmx and .vmdk files

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. 我一直在尝试使用pyvmomi模块从Python中的ESX主机使用现有vm的.vmx和.vmd​​k文件来创建新的vm。 I am new to pyvmomi and there isnt much online help available for pyvmomi. 我是pyvmomi的新手,并且pyvmomi没有太多在线帮助。 I have checked sample code in github( https://github.com/vmware/pyvmomi-community-samples/tree/298bf74446f3fcc5743d6435763ff6dc16ab4cbc/samples ) but no relevent things found. 我已经在github( https://github.com/vmware/pyvmomi-community-samples/tree/298bf74446f3fcc5743d6435763ff6dc16ab4cbc/samples )中检查了示例代码,但是没有找到相关的东西。 Please provide me pointers. 请给我指点。

Lets say the Virtualachine name is: 1001-web1.myhost.com 可以说Virtualachine名称是: 1001-web1.myhost.com

Then the HostSystem you want to put it on is: host1.myesxhost.com 然后,要放入的主机系统是: host1.myesxhost.com

The host has a datastore called: Local1 主机有一个名为: Local1的数据存储

The host lives in a Folder called: Prod 主机位于一个名为: Prod的文件夹中

You need to get the files from the VM into the Local1 datastore. 您需要将文件从VM移到Local1数据存储中。 Ill let you figure that part out. 病会让你弄清楚那部分。 They need to be placed into a folder called: 1001-web1.myhost.com 它们需要放置在名为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. 接下来,您需要在清单中找到Folder Prod和HostSystem host1.myesxhost.com ,以便为它们提供ManagedObjectRef。 You can use a searchIndex.FindByDnsName to find the host, and a searchIndex.FindByInventoryPath to find the Folder (or a few other methods..) 您可以使用searchIndex.FindByDnsName查找主机,并使用searchIndex.FindByInventoryPath查找Folder(或其他一些方法)。

Once you have located the Folder Prod you can use a RegisterVM_Task to register the vm. 找到文件夹产品后 ,就可以使用RegisterVM_Task注册虚拟机。

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. 成功完成任务后,您应该可以从此处监视任务的过程,task.info.result将包含新VM的MOREF。

This is pseudo code so the syntax may be off but this is the process you will have to follow. 这是伪代码,因此语法可能不正确,但这是您必须遵循的过程。

If you know the datastore path to the vmx file you can use the following: 如果知道vmx文件的数据存储路径,则可以使用以下命令:

from pyVim import folder

folder.Register("[datastore]TestVM/TestVM.vmx")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM