這就是我設法做到的。該代碼看起來可能有點複雜,但這裏的要點是:
查找該主機的所有卷,發現上構建了NFS文件系統的人。對於這些卷中的每個卷,請使用卷名稱=數據存儲名稱條件迭代數據存儲列表。找到匹配項後,從DatastoreInfo對象獲取NASDatastoreInfo對象,並檢查遠程NFS服務器IP和導出目錄。
我檢查HostFileSystemVolume.type ==「NFS」該主機上的所有卷,然後檢查該卷的遠程NFS服務器IP和出口目錄。
代碼已經有一個服務實例「SI」到vCenter和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);