这就是我设法做到的方式。代码可能看起来有点复杂,但要点如下:
查找该主机的所有卷,并找到在其上构建了 NFS 文件系统的卷。对于这些卷中的每一个,使用卷名 = 数据存储名称条件迭代数据存储列表。找到匹配项后,从 DatastoreInfo 对象获取 NASDatastoreInfo 对象并检查远程 NFS 服务器 IP 和导出目录。
我为此主机上的所有卷检查了 HostFileSystemVolume.type=='NFS',然后检查了该卷的远程 NFS 服务器 IP 和导出目录。
代码已经有一个指向 vCenter 的服务实例“si”和一个名为“host”的 HostSystem 对象
HostDatastoreSystem hds = host.getHostDatastoreSystem();
HostDatastoreBrowser hdb = host.getDatastoreBrowser();
Datastore[] allDS = hdb.getDatastores();
HostConfigInfo hostConfigInfo = host.getConfig();
HostFileSystemVolumeInfo hostFSVolumeInfo = hostConfigInfo.getFileSystemVolume();
HostFileSystemMountInfo[] hostFSMountInfo= hostFSVolumeInfo.getMountInfo();
for (HostFileSystemMountInfo hfsmi : hostFSMountInfo) {
HostFileSystemVolume hfsv = hfsmi.getVolume();
if (hfsv.getType().equalsIgnoreCase("nfs")){
String dsName = hfsv.getName();
for(Datastore ds: allDS) {
DatastoreInfo di = ds.getInfo();
if (di.getName().equals(dsName)){
HostNasVolume nas = ((NasDatastoreInfo)di).getNas();
if (nas.getRemoteHost().equals(nfsServer) & nas.getRemotePath().equals(datastorePathOnNfsServer)) {
try {
} catch (HostConfigFault e) {
logger.error("ERROR : unmountNfsDatastore : Could not remove Datastore named: " + ds.getName() + " on " + "host: " + hostname , e);
ret = 1;
return ret;
} catch (ResourceInUse e) {
logger.error("ERROR : unmountNfsDatastore : Could not remove Datastore named: " + ds.getName() + " on " + "host: " + hostname , e);
ret = 1;
return ret;
} catch (NotFound e) {
logger.error("ERROR : unmountNfsDatastore : Could not remove Datastore named: " + ds.getName() + " on " + "host: " + hostname , e);
ret = 1;
return ret;
} catch (RuntimeFault e) {
logger.error("ERROR : unmountNfsDatastore : Could not remove Datastore named: " + ds.getName() + " on " + "host: " + hostname , e);
ret = 1;
return ret;
} catch (RemoteException e) {
logger.error("ERROR : unmountNfsDatastore : Could not remove Datastore named: " + ds.getName() + " on " + "host: " + hostname , e);
ret = 1;
return ret;
}
return 0;
}
}
}
}
}
logger.error("ERROR : unmountNfsDatastore : Could not find Datastore exported by " + nfsServer + " on " + "host: " + hostname);
ret = 0;
return ret;
hds.removeDatastore(ds);