使用vmware vsphere通过模板clone虚拟机器错误,报制定的参数错误。环境是集群,模板在其中一台主机上。
代码如下:
Map aa = getMOREFsInContainerByType(serviceContent.getRootFolder(),
"ResourcePool");
Set bb = aa.keySet();
String pool = null;
for( String poolName : bb)
{
pool = poolName;
break;
}
ManagedObjectReference datacenterRef =
vimPort.findByInventoryPath(serviceContent.getSearchIndex(),
dataCenterName);
if (datacenterRef == null) {
System.out.printf("The specified datacenter [ %s ]is not found %n",
dataCenterName);
return;
}
// Find the virtual machine folder for this datacenter.
ManagedObjectReference vmFolderRef =
(ManagedObjectReference) getDynamicProperty(datacenterRef,
"vmFolder");
if (vmFolderRef == null) {
System.out.println("The virtual machine is not found");
return;
}
// vimPort.markAsVirtualMachine(_this, pool, host);
ManagedObjectReference vmRef =
vimPort.findByInventoryPath(serviceContent.getSearchIndex(),
vmPathName);
//
if (vmRef == null) {
System.out.printf("The VMPath specified [ %s ] is not found %n",
vmPathName);
return;
}
ManagedObjectReference host =
getDecendentMoRef(null, "HostSystem","10.134.98.186");
ManagedObjectReference hMOR = getHostByHostName("10.134.98.186");
List<DynamicProperty> datastoresSource =
getDynamicProarray(hMOR, "HostSystem", "datastore");
ArrayOfManagedObjectReference dsSourceArr =
((ArrayOfManagedObjectReference) (datastoresSource.get(0)).getVal());
List<ManagedObjectReference> dsTarget =
dsSourceArr.getManagedObjectReference();
ManagedObjectReference dsMOR = browseDSMOR(dsTarget);
// ManagedObjectReference dataStore =
// getDecendentMoRef(null, "DataStore","datastore (98186)");
ManagedObjectReference poolMOR =
getDecendentMoRef(null, "ResourcePool",pool);
VirtualMachineCloneSpec cloneSpec = new VirtualMachineCloneSpec();
VirtualMachineRelocateSpec relocSpec = new VirtualMachineRelocateSpec();
cloneSpec.setLocation(relocSpec);
cloneSpec.setPowerOn(true);
cloneSpec.setTemplate(false);
cloneSpec.getLocation().setPool(poolMOR);
cloneSpec.getLocation().setHost(host);
cloneSpec.getLocation().setDatastore(dsMOR);
VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec();
vmConfigSpec.setMemoryMB(new Long(memory));
vmConfigSpec.setNumCPUs(Integer.valueOf(cpu));
cloneSpec.setConfig(vmConfigSpec);
System.out.printf("Cloning Virtual Machine [%s] to clone name [%s] %n",
vmPathName.substring(vmPathName.lastIndexOf("/") + 1), cloneName);
try {
ManagedObjectReference cloneTask =
vimPort.cloneVMTask(vmRef, vmFolderRef, cloneName, cloneSpec);
if (getTaskResultAfterDone(cloneTask)) {
System.out
.printf(
"Successfully cloned Virtual Machine [%s] to clone name [%s] %n",
vmPathName.substring(vmPathName.lastIndexOf("/") + 1),
cloneName);
} else {
System.out.printf(
"Failure Cloning Virtual Machine [%s] to clone name [%s] %n",
vmPathName.substring(vmPathName.lastIndexOf("/") + 1),
cloneName);
}
} catch (SOAPFaultException sfe) {
printSoapFaultException(sfe);
} catch (Exception e) {
e.printStackTrace();
}
求解