state = _vms.get(vmName);
_vms.put(vmName, State.Stopping);
}
try {
VmwareHypervisorHost hyperHost = getHyperHost(getServiceContext());
ManagedObjectReference morDc = hyperHost.getHyperHostDatacenter();
// find VM through datacenter (VM is not at the target host yet)
VirtualMachineMO vmMo = hyperHost.findVmOnPeerHyperHost(vmName);
if (vmMo == null) {
String msg = "VM " + vmName + " does not exist in VMware datacenter";
s_logger.error(msg);
throw new Exception(msg);
}
VmwareHypervisorHost destHyperHost = getTargetHyperHost(new DatacenterMO(hyperHost.getContext(), morDc), cmd.getDestinationIp());
ManagedObjectReference morTargetPhysicalHost = destHyperHost.findMigrationTarget(vmMo);
if (morTargetPhysicalHost == null) {
throw new Exception("Unable to find a target capable physical host");
}
if (!vmMo.migrate(destHyperHost.getHyperHostOwnerResourcePool(), morTargetPhysicalHost)) {
throw new Exception("Migration failed");
}
state = State.Stopping;
return new MigrateAnswer(cmd, true, "migration succeeded", null);