在虚拟机的备份还原中,快照的引用是一个很重要的参数,获取获取虚拟磁盘、虚拟磁盘ChangeId以及快照信息都需要用到快照托管对象引用作为参数。下面给出根据虚拟机获取快照引用的方法代码:
/**
* @Title: getSnapshotReference
* @Description: 根据虚拟机获取快照引用
* @param vmMOR
* @param vmName
* @param snapName
* @return
* @throws RuntimeFaultFaultMsg
* @throws InvalidPropertyFaultMsg
* @throws Exception
* @version 1.0
*/
public ManagedObjectReference getSnapshotReference(ManagedObjectReference vmMOR, String vmName, String snapName)
throws RuntimeFaultFaultMsg, InvalidPropertyFaultMsg, Exception {
// 获取虚拟机快照信息
VirtualMachineSnapshotInfo snapInfo =
(VirtualMachineSnapshotInfo) retrieveEntityProps(vmMOR, new String[] {"snapshot"}).get("snapshot");
ManagedObjectReference snapmor = null;
if (snapInfo != null) {
List<VirtualMachineSnapshotTree> listvmst = snapInfo.getRootSnapshotList();
snapmor = traverseSnapshotInTree(listvmst, snapName, false);
if (snapmor == null) {
throw new Exception("在虚拟机" + vmName + "中没有找到名为: " + snapName + "的快照");
}
} else {
throw new Exception("VirtualMachine : " + vmName + "中没有任何快照");
}
return snapmor;
}
获取实体属性(参考VMware SDK sample中的代码)
/**
* @Title: entityProps
* @Description: 获取实体属性
* @param entityMor
* @param props
* @return
* @throws InvalidPropertyFaultMsg
* @throws RuntimeFaultFaultMsg
* @version 1.0
* @throws Exception
*/
public Map<String, Object> retrieveEntityProps(ManagedObjectReference entityMor, String[] props)
throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg, Exception {
final HashMap<String, Object> retVal = new HashMap<String, Object>();
// 使用 PropertySpec 、ObjectPec 创建 PropertyFilterSpec
PropertyFilterSpec[] propertyFilterSpecs = {new PropertyFilterSpecBuilder().propSet(
// 创建 Property Spec
new PropertySpecBuilder().all(Boolean.FALSE).type(entityMor.getType()).pathSet(props)).objectSet(
// 创建 Object Spec
new ObjectSpecBuilder().obj(entityMor))};
List<ObjectContent> oCont = vmClientSession.getVimPortType()
.retrievePropertiesEx(
vmClientSession.getServiceContent().getPropertyCollector(),
Arrays.asList(propertyFilterSpecs),
new RetrieveOptions()).getObjects();
if (oCont == null) {
return retVal;
}
for (ObjectContent oc : oCont) {
List<DynamicProperty> dps = oc.getPropSet();
for (DynamicProperty dp : dps) {
retVal.put(dp.getName(), dp.getVal());
}
}
return retVal;
}