前言
想要实现这个功能,需要有一台服务器安装了NFS的服务端,本人使用的Centos6.5,完全安装之后,nfs也默认安装了。需要注意如下几个要点:
1、修改/etc/exports 文件
/home/xxx *(rw,sync,no_root_squash,no_subtree_check)
前面是你想作为datastore的目录,后面是权限,注意中间有个空格。
2、重启nfs服务
如果目录不存在或者权限错误,这里会报错。
代码实现
void createDataStore() {
String datastore = "datastore-NFS"; //datastore名称
String ESXiHost = "192.168.0.xxx"; //Esxi服务器ip
String clientHost = "192.168.0.xx"; //需要映射目录的服务器ip
String path = "/home/xxx"; //映射的目录
try {
getMOREFs = new GetMOREF(connection);
Map<String, ManagedObjectReference> hostList =
getMOREFs.inFolderByType(connection.getServiceContent().getRootFolder(),
"HostSystem");
ManagedObjectReference hostMor = hostList.get(ESXiHost);
if (hostMor != null) {
HostConfigManager configMgr =
(HostConfigManager) getMOREFs.entityProps(hostMor,
new String[]{"configManager"}).get("configManager");
ManagedObjectReference nwSystem = configMgr.getDatastoreSystem();
HostNasVolumeSpec spec = new HostNasVolumeSpec();
spec.setType("NFS");
spec.setAccessMode("readWrite");
spec.setLocalPath(datastore);
spec.setRemoteHost(clientHost);
spec.setRemotePath(path);
connection.getVimPort().createNasDatastore(nwSystem, spec); //调用Web Service接口创建datastore
System.out.println("create NFS datastore success");
} else {
System.out.println("Host not found");
}
} catch (Exception e) {
e.printStackTrace();
}
}
上述代码都还蛮简单的,只是需要对一些数据类型要比较熟悉,比如HostNasVolumeSpec,整个SDK中,对于esxi主机、虚拟机或者其他实体对象进行配置,都需要这种以Spec结尾的数据类型。