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 ;
8386
8487public class SystemVmTemplateRegistration {
8588 private static final Logger LOGGER = Logger .getLogger (SystemVmTemplateRegistration .class );
86- private static final String MOUNT_COMMAND = "sudo mount -t nfs %s %s" ;
8789 private static final String UMOUNT_COMMAND = "sudo umount %s" ;
8890 private static final String RELATIVE_TEMPLATE_PATH = "./engine/schema/dist/systemvm-templates/" ;
8991 private static final String ABSOLUTE_TEMPLATE_PATH = "/usr/share/cloudstack-management/templates/systemvm/" ;
@@ -116,6 +118,8 @@ public class SystemVmTemplateRegistration {
116118 @ Inject
117119 ImageStoreDao imageStoreDao ;
118120 @ Inject
121+ ImageStoreDetailsDao imageStoreDetailsDao ;
122+ @ Inject
119123 ClusterDao clusterDao ;
120124 @ Inject
121125 ConfigurationDao configurationDao ;
@@ -129,6 +133,7 @@ public SystemVmTemplateRegistration() {
129133 templateDataStoreDao = new BasicTemplateDataStoreDaoImpl ();
130134 vmInstanceDao = new VMInstanceDaoImpl ();
131135 imageStoreDao = new ImageStoreDaoImpl ();
136+ imageStoreDetailsDao = new ImageStoreDetailsDaoImpl ();
132137 clusterDao = new ClusterDaoImpl ();
133138 configurationDao = new ConfigurationDaoImpl ();
134139 }
@@ -141,6 +146,14 @@ public SystemVmTemplateRegistration(String systemVmTemplateVersion) {
141146 this .systemVmTemplateVersion = systemVmTemplateVersion ;
142147 }
143148
149+ public static String getMountCommand (String nfsVersion , String device , String dir ) {
150+ String cmd = "sudo mount -t nfs" ;
151+ if (StringUtils .isNotBlank (nfsVersion )) {
152+ cmd = String .format ("%s -o vers=%s" , cmd , nfsVersion );
153+ }
154+ return String .format ("%s %s %s" , cmd , device , dir );
155+ }
156+
144157 public String getSystemVmTemplateVersion () {
145158 if (StringUtils .isEmpty (systemVmTemplateVersion )) {
146159 return String .format ("%s.%s" , CS_MAJOR_VERSION , CS_TINY_VERSION );
@@ -319,14 +332,14 @@ public void setUpdated(Date updated) {
319332 }
320333 };
321334
322- public static boolean validateIfSeeded (String url , String path ) {
335+ public static boolean validateIfSeeded (String url , String path , String nfsVersion ) {
323336 String filePath = null ;
324337 try {
325338 filePath = Files .createTempDirectory (TEMPORARY_SECONDARY_STORE ).toString ();
326339 if (filePath == null ) {
327340 throw new CloudRuntimeException ("Failed to create temporary directory to mount secondary store" );
328341 }
329- mountStore (url , filePath );
342+ mountStore (url , filePath , nfsVersion );
330343 int lastIdx = path .lastIndexOf (File .separator );
331344 String partialDirPath = path .substring (0 , lastIdx );
332345 String templatePath = filePath + File .separator + partialDirPath ;
@@ -426,14 +439,13 @@ private Pair<String, Long> getNfsStoreInZone(Long zoneId) {
426439 return new Pair <>(url , storeId );
427440 }
428441
429- public static void mountStore (String storeUrl , String path ) {
442+ public static void mountStore (String storeUrl , String path , String nfsVersion ) {
430443 try {
431444 if (storeUrl != null ) {
432445 URI uri = new URI (UriUtils .encodeURIComponent (storeUrl ));
433446 String host = uri .getHost ();
434447 String mountPath = uri .getPath ();
435- String mount = String .format (MOUNT_COMMAND , host + ":" + mountPath , path );
436- Script .runSimpleBashScript (mount );
448+ Script .runSimpleBashScript (getMountCommand (nfsVersion , host + ":" + mountPath , path ));
437449 }
438450 } catch (Exception e ) {
439451 String msg = "NFS Store URL is not in the correct format" ;
@@ -772,7 +784,8 @@ public void doInTransactionWithoutResult(final TransactionStatus status) {
772784 throw new CloudRuntimeException ("Failed to create temporary file path to mount the store" );
773785 }
774786 Pair <String , Long > storeUrlAndId = getNfsStoreInZone (zoneId );
775- mountStore (storeUrlAndId .first (), filePath );
787+ String nfsVersion = getNfsVersion (storeUrlAndId .second ());
788+ mountStore (storeUrlAndId .first (), filePath , nfsVersion );
776789 List <String > hypervisorList = fetchAllHypervisors (zoneId );
777790 for (String hypervisor : hypervisorList ) {
778791 Hypervisor .HypervisorType name = Hypervisor .HypervisorType .getType (hypervisor );
@@ -783,7 +796,7 @@ public void doInTransactionWithoutResult(final TransactionStatus status) {
783796 VMTemplateVO templateVO = vmTemplateDao .findById (templateId );
784797 TemplateDataStoreVO templateDataStoreVO = templateDataStoreDao .findByTemplate (templateId , DataStoreRole .Image );
785798 String installPath = templateDataStoreVO .getInstallPath ();
786- if (validateIfSeeded (storeUrlAndId .first (), installPath )) {
799+ if (validateIfSeeded (storeUrlAndId .first (), installPath , nfsVersion )) {
787800 continue ;
788801 } else if (templateVO != null ) {
789802 registerTemplate (hypervisorAndTemplateName , storeUrlAndId , templateVO , templateDataStoreVO , filePath );
@@ -888,4 +901,17 @@ public void doInTransactionWithoutResult(final TransactionStatus status) {
888901 }
889902 });
890903 }
904+
905+ public String getNfsVersion (long storeId ) {
906+ final String configKey = "secstorage.nfs.version" ;
907+ final Map <String , String > storeDetails = imageStoreDetailsDao .getDetails (storeId );
908+ if (storeDetails != null && storeDetails .containsKey (configKey )) {
909+ return storeDetails .get (configKey );
910+ }
911+ ConfigurationVO globalNfsVersion = configurationDao .findByName (configKey );
912+ if (globalNfsVersion != null ) {
913+ return globalNfsVersion .getValue ();
914+ }
915+ return null ;
916+ }
891917}
0 commit comments