4949import org .apache .cloudstack .engine .subsystem .api .storage .ObjectInDataStoreStateMachine ;
5050import org .apache .cloudstack .framework .config .dao .ConfigurationDao ;
5151import org .apache .cloudstack .framework .config .dao .ConfigurationDaoImpl ;
52+ import org .apache .cloudstack .framework .config .impl .ConfigurationVO ;
5253import org .apache .cloudstack .storage .datastore .db .ImageStoreDao ;
5354import org .apache .cloudstack .storage .datastore .db .ImageStoreDaoImpl ;
55+ import org .apache .cloudstack .storage .datastore .db .ImageStoreDetailsDao ;
56+ import org .apache .cloudstack .storage .datastore .db .ImageStoreDetailsDaoImpl ;
5457import org .apache .cloudstack .storage .datastore .db .ImageStoreVO ;
5558import org .apache .cloudstack .storage .datastore .db .TemplateDataStoreDao ;
5659import org .apache .cloudstack .storage .datastore .db .TemplateDataStoreVO ;
@@ -117,6 +120,8 @@ public class SystemVmTemplateRegistration {
117120 @ Inject
118121 ImageStoreDao imageStoreDao ;
119122 @ Inject
123+ ImageStoreDetailsDao imageStoreDetailsDao ;
124+ @ Inject
120125 ClusterDao clusterDao ;
121126 @ Inject
122127 ConfigurationDao configurationDao ;
@@ -130,6 +135,7 @@ public SystemVmTemplateRegistration() {
130135 templateDataStoreDao = new BasicTemplateDataStoreDaoImpl ();
131136 vmInstanceDao = new VMInstanceDaoImpl ();
132137 imageStoreDao = new ImageStoreDaoImpl ();
138+ imageStoreDetailsDao = new ImageStoreDetailsDaoImpl ();
133139 clusterDao = new ClusterDaoImpl ();
134140 configurationDao = new ConfigurationDaoImpl ();
135141 }
@@ -142,6 +148,14 @@ public SystemVmTemplateRegistration(String systemVmTemplateVersion) {
142148 this .systemVmTemplateVersion = systemVmTemplateVersion ;
143149 }
144150
151+ public static String getMountCommand (String nfsVersion , String device , String dir ) {
152+ String cmd = "sudo mount -t nfs" ;
153+ if (StringUtils .isNotBlank (nfsVersion )) {
154+ cmd = String .format ("%s -o vers=%s" , cmd , nfsVersion );
155+ }
156+ return String .format ("%s %s %s" , cmd , device , dir );
157+ }
158+
145159 public String getSystemVmTemplateVersion () {
146160 if (StringUtils .isEmpty (systemVmTemplateVersion )) {
147161 return String .format ("%s.%s" , CS_MAJOR_VERSION , CS_TINY_VERSION );
@@ -320,14 +334,14 @@ public void setUpdated(Date updated) {
320334 }
321335 };
322336
323- public static boolean validateIfSeeded (String url , String path ) {
337+ public static boolean validateIfSeeded (String url , String path , String nfsVersion ) {
324338 String filePath = null ;
325339 try {
326340 filePath = Files .createTempDirectory (TEMPORARY_SECONDARY_STORE ).toString ();
327341 if (filePath == null ) {
328342 throw new CloudRuntimeException ("Failed to create temporary directory to mount secondary store" );
329343 }
330- mountStore (url , filePath );
344+ mountStore (url , filePath , nfsVersion );
331345 int lastIdx = path .lastIndexOf (File .separator );
332346 String partialDirPath = path .substring (0 , lastIdx );
333347 String templatePath = filePath + File .separator + partialDirPath ;
@@ -427,14 +441,13 @@ private Pair<String, Long> getNfsStoreInZone(Long zoneId) {
427441 return new Pair <>(url , storeId );
428442 }
429443
430- public static void mountStore (String storeUrl , String path ) {
444+ public static void mountStore (String storeUrl , String path , String nfsVersion ) {
431445 try {
432446 if (storeUrl != null ) {
433447 URI uri = new URI (UriUtils .encodeURIComponent (storeUrl ));
434448 String host = uri .getHost ();
435449 String mountPath = uri .getPath ();
436- String mount = String .format (MOUNT_COMMAND , host + ":" + mountPath , path );
437- Script .runSimpleBashScript (mount );
450+ Script .runSimpleBashScript (getMountCommand (nfsVersion , host + ":" + mountPath , path ));
438451 }
439452 } catch (Exception e ) {
440453 String msg = "NFS Store URL is not in the correct format" ;
@@ -773,7 +786,8 @@ public void doInTransactionWithoutResult(final TransactionStatus status) {
773786 throw new CloudRuntimeException ("Failed to create temporary file path to mount the store" );
774787 }
775788 Pair <String , Long > storeUrlAndId = getNfsStoreInZone (zoneId );
776- mountStore (storeUrlAndId .first (), filePath );
789+ String nfsVersion = getNfsVersion (storeUrlAndId .second ());
790+ mountStore (storeUrlAndId .first (), filePath , nfsVersion );
777791 List <String > hypervisorList = fetchAllHypervisors (zoneId );
778792 for (String hypervisor : hypervisorList ) {
779793 Hypervisor .HypervisorType name = Hypervisor .HypervisorType .getType (hypervisor );
@@ -784,7 +798,7 @@ public void doInTransactionWithoutResult(final TransactionStatus status) {
784798 VMTemplateVO templateVO = vmTemplateDao .findById (templateId );
785799 TemplateDataStoreVO templateDataStoreVO = templateDataStoreDao .findByTemplate (templateId , DataStoreRole .Image );
786800 String installPath = templateDataStoreVO .getInstallPath ();
787- if (validateIfSeeded (storeUrlAndId .first (), installPath )) {
801+ if (validateIfSeeded (storeUrlAndId .first (), installPath , nfsVersion )) {
788802 continue ;
789803 } else if (templateVO != null ) {
790804 registerTemplate (hypervisorAndTemplateName , storeUrlAndId , templateVO , templateDataStoreVO , filePath );
@@ -889,4 +903,17 @@ public void doInTransactionWithoutResult(final TransactionStatus status) {
889903 }
890904 });
891905 }
906+
907+ public String getNfsVersion (long storeId ) {
908+ final String configKey = "secstorage.nfs.version" ;
909+ final Map <String , String > storeDetails = imageStoreDetailsDao .getDetails (storeId );
910+ if (storeDetails != null && storeDetails .containsKey (configKey )) {
911+ return storeDetails .get (configKey );
912+ }
913+ ConfigurationVO globalNfsVersion = configurationDao .findByName (configKey );
914+ if (globalNfsVersion != null ) {
915+ return globalNfsVersion .getValue ();
916+ }
917+ return null ;
918+ }
892919}
0 commit comments