Skip to content

Commit 966b75d

Browse files
committed
Verify QCOW2 features on direct download of template
1 parent 54b3519 commit 966b75d

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

+18
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.io.FileOutputStream;
2727
import java.io.IOException;
2828
import java.nio.file.Files;
29+
import java.nio.file.Path;
2930
import java.nio.file.Paths;
3031
import java.text.DateFormat;
3132
import java.text.SimpleDateFormat;
@@ -63,6 +64,7 @@
6364
import org.apache.cloudstack.storage.command.SnapshotAndCopyAnswer;
6465
import org.apache.cloudstack.storage.command.SnapshotAndCopyCommand;
6566
import org.apache.cloudstack.storage.command.SyncVolumePathCommand;
67+
import org.apache.cloudstack.storage.formatinspector.Qcow2Inspector;
6668
import org.apache.cloudstack.storage.to.PrimaryDataStoreTO;
6769
import org.apache.cloudstack.storage.to.SnapshotObjectTO;
6870
import org.apache.cloudstack.storage.to.TemplateObjectTO;
@@ -2394,6 +2396,22 @@ public Answer handleDownloadTemplateToPrimaryStorage(DirectDownloadCommand cmd)
23942396

23952397
template = storagePoolMgr.createPhysicalDiskFromDirectDownloadTemplate(tempFilePath, destTemplatePath, destPool, cmd.getFormat(), cmd.getWaitInMillSeconds());
23962398

2399+
String templatePath = template.getPath();
2400+
if (templatePath != null) {
2401+
try {
2402+
Qcow2Inspector.validateQcow2File(templatePath);
2403+
} catch (RuntimeException e) {
2404+
try {
2405+
Files.deleteIfExists(Path.of(templatePath));
2406+
} catch (IOException ioException) {
2407+
s_logger.warn(String.format("Unable to remove file [%s]; consider removing it manually.", templatePath), ioException);
2408+
}
2409+
2410+
s_logger.error(String.format("The downloaded file [%s] is not a valid QCOW2.", templatePath), e);
2411+
return new DirectDownloadAnswer(false, "The downloaded file is not a valid QCOW2. Ask the administrator to check the logs for more details.", true);
2412+
}
2413+
}
2414+
23972415
if (!storagePoolMgr.disconnectPhysicalDisk(pool.getPoolType(), pool.getUuid(), destTemplatePath)) {
23982416
s_logger.warn("Unable to disconnect physical disk at path: " + destTemplatePath + ", in storage pool id: " + pool.getUuid());
23992417
}

0 commit comments

Comments
 (0)