我可以使用pyvmomi将虚拟机从一个集群克隆到另一个集群吗?
我们有两个VMware数据中心,都连接到我们的vCenter服务器,都是VMware 5.5版本。我有一个虚拟机(不是模板),我想用pyvmomi以自动化的方式进行克隆。如果我指定要把虚拟机克隆到与源虚拟机在同一个数据中心的主机上,脚本运行得很好。但是,如果我指定要把它克隆到另一个数据中心的主机上,克隆就会失败,并出现一个vmodl错误:
指定的参数不正确。
根据我所了解的情况,我在RelocateSpec和CloneSpec中设置的一切都没问题,实际的CloneVM_Task调用也没问题。如果能给我一些正确的方向建议,我将非常感激。谢谢。
这是脚本:
from pyVim.connect import SmartConnect, Disconnect
from pyVmomi import vim, vmodl
import atexit
import sys
import time
import pprint
#import pudb
pp = pprint.PrettyPrinter(indent=4)
def WaitForTasks(tasks, si):
global pp
"""
Given the service instance si and tasks, it returns after all the
tasks are complete
"""
pc = si.content.propertyCollector
task_result = None
taskList = [str(task) for task in tasks]
# Create filter
objSpecs = [vmodl.query.PropertyCollector.ObjectSpec(obj=task) for task in tasks]
propSpec = vmodl.query.PropertyCollector.PropertySpec(type=vim.Task, pathSet=[], all=True)
filterSpec = vmodl.query.PropertyCollector.FilterSpec()
filterSpec.objectSet = objSpecs
filterSpec.propSet = [propSpec]
filter = pc.CreateFilter(filterSpec, True)
try:
version, state = None, None
# Loop looking for updates till the state moves to a completed state.
while len(taskList):
update = pc.WaitForUpdates(version)
for filterSet in update.filterSet:
for objSet in filterSet.objectSet:
task = objSet.obj
for change in objSet.changeSet:
if change.name == 'info':
state = change.val.state
elif change.name == 'info.state':
state = change.val
else:
continue
if not str(task) in taskList:
continue
if state == vim.TaskInfo.State.success:
#save info
task_result = task.info.result
# Remove task from taskList
taskList.remove(str(task))
elif state == vim.TaskInfo.State.error:
raise task.info.error
# Move to next version
version = update.version
except Exception, e:
print "Caught Exception in WaitForTasks : " + pp.pprint(e)
finally:
if filter:
filter.Destroy()
return task_result
"""
Get the vsphere object associated with a given text name
"""
def GetObject(content, vimtype, name):
obj = None
container = content.viewManager.CreateContainerView(content.rootFolder, vimtype, True)
for c in container.view:
if c.name == name:
obj = c
break
return obj
def WaitForIP(target_machine):
while target_machine.guest.ipAddress == None:
print "Waiting for IP to be visible..."
sys.stdout.flush()
time.sleep(1)
def main():
global pp
try:
si = None
server = 'vcenter'
try:
si = SmartConnect(host=server, user='adminuser', pwd='password', port=443)
except IOError, e:
pass
if not si:
print "Could not connect to vCenter using specified username and password"
return -1
atexit.register(Disconnect, si)
# Get references to the needed objects
content = si.content
source_machine = GetObject(content,[vim.VirtualMachine],'ubuntu14.04')
destination_host = GetObject(content, [vim.HostSystem], 'vmhostname')
if "bos" in destination_host.name:
dsname = 'bosdatastore'
dcname = 'Boston'
else:
dsname = 'reddatastore'
dcname = 'Redmond'
# Configure where the new machine is to be located
rs = vim.VirtualMachineRelocateSpec()
cs = vim.VirtualMachineCloneSpec()
dc = GetObject(content, [vim.Datacenter], dsname)
target_folder = dc.vmFolder
rs.host = destination_host
cs.location = rs
cs.powerOn = False
# Clone it
tasks = [source_machine.CloneVM_Task(target_folder, 'newmachine', cs)]
print "Clone initiated..."
sys.stdout.flush()
target_machine = WaitForTasks(tasks, si)
print("Virtual Machine %s has been cloned successfully" % "newmachine")
# update NIC settings if needed
for dev in target_machine.config.hardware.device:
if dev.deviceInfo.label == 'Network adapter 1':
nic = dev
break
if nic.backing.deviceName != 'vlan1':
net = GetObject(content,[vim.Network],'vlan2')
vds = vim.vm.device.VirtualDeviceSpec()
nic.backing.deviceName = 'vlan2'
nic.backing.network = net
nic.deviceInfo.summary = 'vlan2'
vds.device = nic
vds.operation = vim.vm.device.VirtualDeviceSpec.Operation.edit
vmcs = vim.vm.ConfigSpec()
vmcs.deviceChange = [vds]
tasks = [target_machine.ReconfigVM_Task(vmcs)]
print "Network change started..."
sys.stdout.flush()
WaitForTasks(tasks,si)
print "Network update complete."
# Power the machine on
tasks = [target_machine.PowerOnVM_Task()]
print "New machine is starting..."
sys.stdout.flush()
WaitForTasks(tasks,si)
# Wait for target to have IP so we can save it
WaitForIP(target_machine)
except vmodl.MethodFault, e:
print "Caught vmodl fault in main : " + pp.pprint(e)
except Exception, e:
print "Caught Exception in main : " + pp.pprint(e)
print "Done."
# Start program
if __name__ == "__main__":
main()
1 个回答
0
CloneSpec
是一个需要 RelocateSpec
参数的东西。简单来说,就是你在复制一个虚拟机或模板,并把它迁移到另一个主机或集群(我不太确定是热迁移还是冷迁移)。
看到你的问题后,我首先在网上查了“vMotion是否可以跨数据中心”。结果发现,答案是“vMotion不可以跨数据中心,但冷迁移是可以的”。
现在,正如我所说,我不确定 RelocateSpec
是否使用 vMotion。不过看起来即使每个数据中心的所有主机共享一个公共的存储库,这种迁移也是不可能的。
你可以看看这篇文章 从一个数据中心克隆虚拟机到另一个数据中心,它基本上是先克隆并导出为 ovf 格式,然后再把这个 ovf 导入到目标数据中心。