Skip to content

Commit 53aa921

Browse files
authored
server: fix nfs version option during mount (#9559)
Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>
1 parent 2398b5c commit 53aa921

File tree

3 files changed

+42
-11
lines changed

3 files changed

+42
-11
lines changed

engine/schema/src/main/java/com/cloud/upgrade/SystemVmTemplateRegistration.java

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,11 @@
4949
import org.apache.cloudstack.engine.subsystem.api.storage.ObjectInDataStoreStateMachine;
5050
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
5151
import org.apache.cloudstack.framework.config.dao.ConfigurationDaoImpl;
52+
import org.apache.cloudstack.framework.config.impl.ConfigurationVO;
5253
import org.apache.cloudstack.storage.datastore.db.ImageStoreDao;
5354
import 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;
5457
import org.apache.cloudstack.storage.datastore.db.ImageStoreVO;
5558
import org.apache.cloudstack.storage.datastore.db.TemplateDataStoreDao;
5659
import org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO;
@@ -83,7 +86,6 @@
8386

8487
public 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
}

server/src/main/java/com/cloud/storage/StorageManagerImpl.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,8 @@ public class StorageManagerImpl extends ManagerBase implements StorageManager, C
391391
ConfigDepot configDepot;
392392
@Inject
393393
ConfigurationDao configurationDao;
394+
@Inject
395+
private ImageStoreDetailsUtil imageStoreDetailsUtil;
394396

395397
protected List<StoragePoolDiscoverer> _discoverers;
396398

@@ -3425,6 +3427,7 @@ public void doInTransactionWithoutResult(final TransactionStatus status) {
34253427
throw new CloudRuntimeException("Failed to create temporary file path to mount the store");
34263428
}
34273429
Pair<String, Long> storeUrlAndId = new Pair<>(url, store.getId());
3430+
String nfsVersion = imageStoreDetailsUtil.getNfsVersion(store.getId());
34283431
for (HypervisorType hypervisorType : hypSet) {
34293432
try {
34303433
if (HypervisorType.Simulator == hypervisorType) {
@@ -3441,15 +3444,16 @@ public void doInTransactionWithoutResult(final TransactionStatus status) {
34413444
templateVO = _templateStoreDao.findByTemplate(templateId, DataStoreRole.Image);
34423445
if (templateVO != null) {
34433446
try {
3444-
if (SystemVmTemplateRegistration.validateIfSeeded(url, templateVO.getInstallPath())) {
3447+
if (SystemVmTemplateRegistration.validateIfSeeded(
3448+
url, templateVO.getInstallPath(), nfsVersion)) {
34453449
continue;
34463450
}
34473451
} catch (Exception e) {
34483452
s_logger.error("Failed to validated if template is seeded", e);
34493453
}
34503454
}
34513455
}
3452-
SystemVmTemplateRegistration.mountStore(storeUrlAndId.first(), filePath);
3456+
SystemVmTemplateRegistration.mountStore(storeUrlAndId.first(), filePath, nfsVersion);
34533457
if (templateVO != null && vmTemplateVO != null) {
34543458
systemVmTemplateRegistration.registerTemplate(hypervisorAndTemplateName, storeUrlAndId, vmTemplateVO, templateVO, filePath);
34553459
} else {

server/src/main/java/org/apache/cloudstack/diagnostics/DiagnosticsServiceImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,8 @@ public Long getDelay() {
480480

481481
private void cleanupOldDiagnosticFiles(DataStore store) {
482482
String mountPoint = null;
483-
mountPoint = serviceImpl.mountManager.getMountPoint(store.getUri(), null);
483+
mountPoint = serviceImpl.mountManager.getMountPoint(store.getUri(),
484+
serviceImpl.imageStoreDetailsUtil.getNfsVersion(store.getId()));
484485
if (StringUtils.isNotBlank(mountPoint)) {
485486
File directory = new File(mountPoint + File.separator + DIAGNOSTICS_DIRECTORY);
486487
if (directory.isDirectory()) {

0 commit comments

Comments
 (0)