diff --git a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/storage/LibvirtStorageAdaptor.java b/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/storage/LibvirtStorageAdaptor.java index c0d58c579081..7c66a91876f5 100644 --- a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/storage/LibvirtStorageAdaptor.java +++ b/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/storage/LibvirtStorageAdaptor.java @@ -224,10 +224,35 @@ public KVMPhysicalDisk createTemplateFromDirectDownloadFile(String templateFileP } else { Script.runSimpleBashScript("mv " + templateFilePath + " " + destinationFile); } + } else if (destPool.getType() == StoragePoolType.RBD) { + String temporaryExtractFilePath = sourceFile.getParent() + File.separator + templateUuid; + extractDownloadedTemplate(templateFilePath, destPool, temporaryExtractFilePath); + createTemplateOnRBDFromDirectDownloadFile(temporaryExtractFilePath, templateUuid, destPool, timeout); } return destPool.getPhysicalDisk(templateUuid); } + private void createTemplateOnRBDFromDirectDownloadFile(String srcTemplateFilePath, String templateUuid, KVMStoragePool destPool, int timeout) { + try { + QemuImg.PhysicalDiskFormat srcFileFormat = QemuImg.PhysicalDiskFormat.QCOW2; + QemuImgFile srcFile = new QemuImgFile(srcTemplateFilePath, srcFileFormat); + QemuImg qemu = new QemuImg(timeout); + Map info = qemu.info(srcFile); + Long virtualSize = Long.parseLong(info.get(QemuImg.VIRTUAL_SIZE)); + KVMPhysicalDisk destDisk = new KVMPhysicalDisk(destPool.getSourceDir() + "/" + templateUuid, templateUuid, destPool); + destDisk.setFormat(PhysicalDiskFormat.RAW); + destDisk.setSize(virtualSize); + destDisk.setVirtualSize(virtualSize); + QemuImgFile destFile = new QemuImgFile(KVMPhysicalDisk.RBDStringBuilder(destPool, destDisk.getPath())); + destFile.setFormat(PhysicalDiskFormat.RAW); + qemu.convert(srcFile, destFile); + } catch (LibvirtException | QemuImgException e) { + String err = String.format("Error creating template from direct download file on pool %s: %s", destPool.getUuid(), e.getMessage()); + logger.error(err, e); + throw new CloudRuntimeException(err, e); + } + } + public StorageVol getVolume(StoragePool pool, String volName) { StorageVol vol = null;