Skip to content

Commit ed97bb7

Browse files
GutoVeronezidhslove
authored andcommitted
Verify QCOW2 features on direct download of template
1 parent 27247ad commit ed97bb7

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
@@ -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;
@@ -65,6 +66,7 @@
6566
import org.apache.cloudstack.storage.command.SnapshotAndCopyAnswer;
6667
import org.apache.cloudstack.storage.command.SnapshotAndCopyCommand;
6768
import org.apache.cloudstack.storage.command.SyncVolumePathCommand;
69+
import org.apache.cloudstack.storage.formatinspector.Qcow2Inspector;
6870
import org.apache.cloudstack.storage.to.PrimaryDataStoreTO;
6971
import org.apache.cloudstack.storage.to.SnapshotObjectTO;
7072
import org.apache.cloudstack.storage.to.TemplateObjectTO;
@@ -2637,6 +2639,22 @@ public Answer handleDownloadTemplateToPrimaryStorage(DirectDownloadCommand cmd)
26372639

26382640
template = storagePoolMgr.createPhysicalDiskFromDirectDownloadTemplate(tempFilePath, destTemplatePath, destPool, cmd.getFormat(), cmd.getWaitInMillSeconds());
26392641

2642+
String templatePath = template.getPath();
2643+
if (templatePath != null) {
2644+
try {
2645+
Qcow2Inspector.validateQcow2File(templatePath);
2646+
} catch (RuntimeException e) {
2647+
try {
2648+
Files.deleteIfExists(Path.of(templatePath));
2649+
} catch (IOException ioException) {
2650+
s_logger.warn(String.format("Unable to remove file [%s]; consider removing it manually.", templatePath), ioException);
2651+
}
2652+
2653+
s_logger.error(String.format("The downloaded file [%s] is not a valid QCOW2.", templatePath), e);
2654+
return new DirectDownloadAnswer(false, "The downloaded file is not a valid QCOW2. Ask the administrator to check the logs for more details.", true);
2655+
}
2656+
}
2657+
26402658
if (!storagePoolMgr.disconnectPhysicalDisk(pool.getPoolType(), pool.getUuid(), destTemplatePath)) {
26412659
logger.warn("Unable to disconnect physical disk at path: " + destTemplatePath + ", in storage pool id: " + pool.getUuid());
26422660
}

0 commit comments

Comments
 (0)