Skip to content

Commit d019388

Browse files
committed
CLOUDSTACK-4249: Do not assume KVM snapshots are in QCOW2 format
RBD snapshots are in RAW format and are stored as such on Secondary Storage. When creating a template from such a snapshot, honor the image format passed down by the management server and act on that. The old code simply assumed it always was QCOW2.
1 parent b3921e5 commit d019388

1 file changed

Lines changed: 32 additions & 9 deletions

File tree

‎services/secondary-storage/src/org/apache/cloudstack/storage/resource/NfsSecondaryStorageResource.java‎

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -407,10 +407,14 @@ protected Answer copySnapshotToTemplateFromNfsToNfs(CopyCommand cmd, SnapshotObj
407407
} else if (srcData.getHypervisorType() == HypervisorType.KVM) {
408408
File srcFile = getFile(srcData.getPath(), srcDataStore.getUrl());
409409
File destFile = getFile(destData.getPath(), destDataStore.getUrl());
410+
411+
ImageFormat srcFormat = srcData.getVolume().getFormat();
412+
410413
// get snapshot file name
411414
String templateName = srcFile.getName();
412-
// add kvm file extension for copied template name
413-
String destFileFullPath = destFile.getAbsolutePath() + File.separator + templateName + "." + ImageFormat.QCOW2.getFileExtension();
415+
// add kvm file extension for copied template name
416+
String fileName = templateName + "." + srcFormat.getFileExtension();
417+
String destFileFullPath = destFile.getAbsolutePath() + File.separator + fileName;
414418
s_logger.debug("copy snapshot " + srcFile.getAbsolutePath() + " to template " + destFileFullPath);
415419
Script.runSimpleBashScript("cp " + srcFile.getAbsolutePath() + " " + destFileFullPath);
416420
try {
@@ -422,38 +426,55 @@ protected Answer copySnapshotToTemplateFromNfsToNfs(CopyCommand cmd, SnapshotObj
422426
BufferedWriter bufferWriter = new BufferedWriter(writer);
423427
bufferWriter.write("uniquename=" + destData.getName());
424428
bufferWriter.write("\n");
425-
bufferWriter.write("filename=" + templateName + "." + ImageFormat.QCOW2.getFileExtension());
429+
bufferWriter.write("filename=" + fileName);
426430
bufferWriter.write("\n");
427431
long size = this._storage.getSize(destFileFullPath);
428432
bufferWriter.write("size=" + size);
429433
bufferWriter.close();
430434
writer.close();
431-
// template post processing
432-
QCOW2Processor processor = new QCOW2Processor();
435+
436+
437+
/**
438+
* Snapshots might be in either QCOW2 or RAW image format
439+
*
440+
* For example RBD snapshots are in RAW format
441+
*/
442+
Processor processor = null;
443+
if (srcFormat == ImageFormat.QCOW2) {
444+
processor = new QCOW2Processor();
445+
} else if (srcFormat == ImageFormat.RAW) {
446+
processor = new RawImageProcessor();
447+
}
448+
433449
Map<String, Object> params = new HashMap<String, Object>();
434450
params.put(StorageLayer.InstanceConfigKey, _storage);
435451

436-
processor.configure("qcow2 processor", params);
452+
processor.configure("template processor", params);
437453
String destPath = destFile.getAbsolutePath();
438454

439455
FormatInfo info = processor.process(destPath, null, templateName);
440456
TemplateLocation loc = new TemplateLocation(_storage, destPath);
441457
loc.create(1, true, srcFile.getName());
442458
loc.addFormat(info);
443459
loc.save();
460+
444461
TemplateProp prop = loc.getTemplateInfo();
445462
TemplateObjectTO newTemplate = new TemplateObjectTO();
446-
newTemplate.setPath(destData.getPath() + File.separator + templateName + "." + ImageFormat.QCOW2.getFileExtension());
447-
newTemplate.setFormat(ImageFormat.QCOW2);
463+
newTemplate.setPath(destData.getPath() + File.separator + fileName);
464+
newTemplate.setFormat(srcFormat);
448465
newTemplate.setSize(prop.getSize());
449466
newTemplate.setPhysicalSize(prop.getPhysicalSize());
467+
450468
return new CopyCmdAnswer(newTemplate);
451469
} catch (ConfigurationException e) {
452470
s_logger.debug("Failed to create template:" + e.toString());
453471
return new CopyCmdAnswer(e.toString());
454472
} catch (IOException e) {
455473
s_logger.debug("Failed to create template:" + e.toString());
456474
return new CopyCmdAnswer(e.toString());
475+
} catch (InternalErrorException e) {
476+
s_logger.debug("Failed to create template:" + e.toString());
477+
return new CopyCmdAnswer(e.toString());
457478
}
458479
}
459480

@@ -711,7 +732,7 @@ private ImageFormat getTemplateFormat(String filePath) {
711732
return ImageFormat.OVA;
712733
} else if (ext.equalsIgnoreCase("tar")) {
713734
return ImageFormat.TAR;
714-
} else if (ext.equalsIgnoreCase("img")) {
735+
} else if (ext.equalsIgnoreCase("img") || ext.equalsIgnoreCase("raw")) {
715736
return ImageFormat.RAW;
716737
}
717738
}
@@ -731,6 +752,8 @@ protected Long getVirtualSize(File file, ImageFormat format) {
731752
processor = new VmdkProcessor();
732753
} else if (format == ImageFormat.VHD) {
733754
processor = new VhdProcessor();
755+
} else if (format == ImageFormat.RAW) {
756+
processor = new RawImageProcessor();
734757
}
735758

736759
if (processor == null) {

0 commit comments

Comments
 (0)