Search in sources :

Example 6 with QemuImg

use of com.cloud.utils.qemu.QemuImg in project cosmic by MissionCriticalCloud.

the class LibvirtStorageAdaptor method createDiskFromTemplate.

@Override
public KvmPhysicalDisk createDiskFromTemplate(final KvmPhysicalDisk template, final String name, final PhysicalDiskFormat format, final Storage.ProvisioningType provisioningType, final long size, final KvmStoragePool destPool, final int timeout) {
    logger.info("Creating volume " + name + " from template " + template.getName() + " in pool " + destPool.getUuid() + " (" + destPool.getType().toString() + ") with size " + size);
    KvmPhysicalDisk disk = null;
    if (destPool.getType() == StoragePoolType.RBD) {
        disk = createDiskFromTemplateOnRbd(template, name, format, provisioningType, size, destPool, timeout);
    } else {
        try {
            final String newUuid = name;
            disk = destPool.createPhysicalDisk(newUuid, format, provisioningType, template.getVirtualSize());
            if (disk == null) {
                throw new CloudRuntimeException("Failed to create disk from template " + template.getName());
            }
            if (template.getFormat() == PhysicalDiskFormat.TAR) {
                Script.runSimpleBashScript("tar -x -f " + template.getPath() + " -C " + disk.getPath(), timeout);
            } else if (template.getFormat() == PhysicalDiskFormat.DIR) {
                Script.runSimpleBashScript("mkdir -p " + disk.getPath());
                Script.runSimpleBashScript("chmod 755 " + disk.getPath());
                Script.runSimpleBashScript("tar -x -f " + template.getPath() + "/*.tar -C " + disk.getPath(), timeout);
            } else if (format == PhysicalDiskFormat.QCOW2) {
                final QemuImg qemu = new QemuImg(timeout);
                final QemuImgFile destFile = new QemuImgFile(disk.getPath(), format);
                if (size > template.getVirtualSize()) {
                    destFile.setSize(size);
                } else {
                    destFile.setSize(template.getVirtualSize());
                }
                final Map<String, String> options = new HashMap<>();
                options.put("preallocation", QemuImg.PreallocationType.getPreallocationType(provisioningType).toString());
                switch(provisioningType) {
                    case THIN:
                        final QemuImgFile backingFile = new QemuImgFile(template.getPath(), template.getFormat());
                        qemu.create(destFile, backingFile, options);
                        break;
                    case SPARSE:
                    case FAT:
                        final QemuImgFile srcFile = new QemuImgFile(template.getPath(), template.getFormat());
                        qemu.convert(srcFile, destFile, options);
                        break;
                    default:
                        break;
                }
            } else if (format == PhysicalDiskFormat.RAW) {
                final QemuImgFile sourceFile = new QemuImgFile(template.getPath(), template.getFormat());
                final QemuImgFile destFile = new QemuImgFile(disk.getPath(), PhysicalDiskFormat.RAW);
                if (size > template.getVirtualSize()) {
                    destFile.setSize(size);
                } else {
                    destFile.setSize(template.getVirtualSize());
                }
                final QemuImg qemu = new QemuImg(timeout);
                final Map<String, String> options = new HashMap<>();
                qemu.convert(sourceFile, destFile, options);
            }
        } catch (final QemuImgException e) {
            logger.error("Failed to create " + disk.getPath() + " due to a failed executing of qemu-img: " + e.getMessage());
        }
    }
    return disk;
}
Also used : QemuImgFile(com.cloud.utils.qemu.QemuImgFile) HashMap(java.util.HashMap) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) QemuImgException(com.cloud.utils.qemu.QemuImgException) HashMap(java.util.HashMap) Map(java.util.Map) QemuImg(com.cloud.utils.qemu.QemuImg)

Example 7 with QemuImg

use of com.cloud.utils.qemu.QemuImg in project cosmic by MissionCriticalCloud.

the class LibvirtStorageAdaptor method createPhysicalDiskByQemuImg.

private KvmPhysicalDisk createPhysicalDiskByQemuImg(final String name, final KvmStoragePool pool, final PhysicalDiskFormat format, final Storage.ProvisioningType provisioningType, final long size) {
    final String volPath = pool.getLocalPath() + "/" + name;
    final String volName = name;
    long virtualSize = 0;
    long actualSize = 0;
    final int timeout = 0;
    final QemuImgFile destFile = new QemuImgFile(volPath);
    destFile.setFormat(format);
    destFile.setSize(size);
    final QemuImg qemu = new QemuImg(timeout);
    final Map<String, String> options = new HashMap<>();
    if (pool.getType() == StoragePoolType.NetworkFilesystem) {
        options.put("preallocation", QemuImg.PreallocationType.getPreallocationType(provisioningType).toString());
    }
    try {
        qemu.create(destFile, options);
        final Map<String, String> info = qemu.info(destFile);
        virtualSize = Long.parseLong(info.get(new String("virtual_size")));
        actualSize = new File(destFile.getFileName()).length();
    } catch (final QemuImgException e) {
        logger.error("Failed to create " + volPath + " due to a failed executing of qemu-img: " + e.getMessage());
    }
    final KvmPhysicalDisk disk = new KvmPhysicalDisk(volPath, volName, pool);
    disk.setFormat(format);
    disk.setSize(actualSize);
    disk.setVirtualSize(virtualSize);
    return disk;
}
Also used : QemuImgFile(com.cloud.utils.qemu.QemuImgFile) HashMap(java.util.HashMap) QemuImgException(com.cloud.utils.qemu.QemuImgException) QemuImgFile(com.cloud.utils.qemu.QemuImgFile) File(java.io.File) QemuImg(com.cloud.utils.qemu.QemuImg)

Aggregations

QemuImg (com.cloud.utils.qemu.QemuImg)7 QemuImgException (com.cloud.utils.qemu.QemuImgException)7 QemuImgFile (com.cloud.utils.qemu.QemuImgFile)7 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)5 File (java.io.File)4 HashMap (java.util.HashMap)4 InternalErrorException (com.cloud.exception.InternalErrorException)3 Script (com.cloud.utils.script.Script)3 IOException (java.io.IOException)3 ConfigurationException (javax.naming.ConfigurationException)3 IoCTX (com.ceph.rados.IoCTX)2 Rados (com.ceph.rados.Rados)2 RadosException (com.ceph.rados.exceptions.RadosException)2 Rbd (com.ceph.rbd.Rbd)2 RbdException (com.ceph.rbd.RbdException)2 RbdImage (com.ceph.rbd.RbdImage)2 DataStoreTO (com.cloud.agent.api.to.DataStoreTO)2 DataTO (com.cloud.agent.api.to.DataTO)2 NfsTO (com.cloud.agent.api.to.NfsTO)2 CopyCmdAnswer (com.cloud.storage.command.CopyCmdAnswer)2