Skip to content

Commit 256e77a

Browse files
committed
Verify QCOW2 features on direct download of template
1 parent 18fe422 commit 256e77a

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/storage/KVMStorageProcessor.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
import org.apache.cloudstack.storage.command.SnapshotAndCopyAnswer;
8989
import org.apache.cloudstack.storage.command.SnapshotAndCopyCommand;
9090
import org.apache.cloudstack.storage.command.SyncVolumePathCommand;
91+
import org.apache.cloudstack.storage.formatinspector.Qcow2Inspector;
9192
import org.apache.cloudstack.storage.to.PrimaryDataStoreTO;
9293
import org.apache.cloudstack.storage.to.SnapshotObjectTO;
9394
import org.apache.cloudstack.storage.to.TemplateObjectTO;
@@ -119,6 +120,7 @@
119120
import java.io.FileOutputStream;
120121
import java.io.IOException;
121122
import java.nio.file.Files;
123+
import java.nio.file.Path;
122124
import java.nio.file.Paths;
123125
import java.text.DateFormat;
124126
import java.text.SimpleDateFormat;
@@ -2440,6 +2442,22 @@ public Answer handleDownloadTemplateToPrimaryStorage(DirectDownloadCommand cmd)
24402442

24412443
template = storagePoolMgr.createPhysicalDiskFromDirectDownloadTemplate(tempFilePath, destTemplatePath, destPool, cmd.getFormat(), cmd.getWaitInMillSeconds());
24422444

2445+
String templatePath = template.getPath();
2446+
if (templatePath != null) {
2447+
try {
2448+
Qcow2Inspector.validateQcow2File(templatePath);
2449+
} catch (RuntimeException e) {
2450+
try {
2451+
Files.deleteIfExists(Path.of(templatePath));
2452+
} catch (IOException ioException) {
2453+
s_logger.warn(String.format("Unable to remove file [%s]; consider removing it manually.", templatePath), ioException);
2454+
}
2455+
2456+
s_logger.error(String.format("The downloaded file [%s] is not a valid QCOW2.", templatePath), e);
2457+
return new DirectDownloadAnswer(false, "The downloaded file is not a valid QCOW2. Ask the administrator to check the logs for more details.", true);
2458+
}
2459+
}
2460+
24432461
if (!storagePoolMgr.disconnectPhysicalDisk(pool.getPoolType(), pool.getUuid(), destTemplatePath)) {
24442462
s_logger.warn("Unable to disconnect physical disk at path: " + destTemplatePath + ", in storage pool id: " + pool.getUuid());
24452463
}

0 commit comments

Comments
 (0)