Search in sources :

Example 1 with PhysicalDiskFormat

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

the class LibvirtComputingResource method getResizeScriptType.

public String getResizeScriptType(final KvmStoragePool pool, final KvmPhysicalDisk vol) {
    final StoragePoolType poolType = pool.getType();
    final PhysicalDiskFormat volFormat = vol.getFormat();
    if (pool.getType() == StoragePoolType.CLVM && volFormat == PhysicalDiskFormat.RAW) {
        return "CLVM";
    } else if ((poolType == StoragePoolType.NetworkFilesystem || poolType == StoragePoolType.SharedMountPoint || poolType == StoragePoolType.Filesystem || poolType == StoragePoolType.Gluster) && volFormat == PhysicalDiskFormat.QCOW2) {
        return "QCOW2";
    }
    throw new CloudRuntimeException("Cannot determine resize type from pool type " + pool.getType());
}
Also used : StoragePoolType(com.cloud.storage.Storage.StoragePoolType) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) PhysicalDiskFormat(com.cloud.utils.qemu.QemuImg.PhysicalDiskFormat)

Example 2 with PhysicalDiskFormat

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

the class LibvirtStorageAdaptor method copyPhysicalDisk.

@Override
public KvmPhysicalDisk copyPhysicalDisk(final KvmPhysicalDisk disk, final String name, final KvmStoragePool destPool, final int timeout) {
    final KvmStoragePool srcPool = disk.getPool();
    final PhysicalDiskFormat sourceFormat = disk.getFormat();
    final String sourcePath = disk.getPath();
    KvmPhysicalDisk newDisk;
    logger.debug("copyPhysicalDisk: disk size:" + disk.getSize() + ", virtualsize:" + disk.getVirtualSize() + " format:" + disk.getFormat());
    if (destPool.getType() != StoragePoolType.RBD) {
        if (disk.getFormat() == PhysicalDiskFormat.TAR) {
            newDisk = destPool.createPhysicalDisk(name, PhysicalDiskFormat.DIR, Storage.ProvisioningType.THIN, disk.getVirtualSize());
        } else {
            newDisk = destPool.createPhysicalDisk(name, Storage.ProvisioningType.THIN, disk.getVirtualSize());
        }
    } else {
        newDisk = new KvmPhysicalDisk(destPool.getSourceDir() + "/" + name, name, destPool);
        newDisk.setFormat(PhysicalDiskFormat.RAW);
        newDisk.setSize(disk.getVirtualSize());
        newDisk.setVirtualSize(disk.getSize());
    }
    final String destPath = newDisk.getPath();
    final PhysicalDiskFormat destFormat = newDisk.getFormat();
    final QemuImg qemu = new QemuImg(timeout);
    QemuImgFile srcFile = null;
    QemuImgFile destFile = null;
    if (srcPool.getType() != StoragePoolType.RBD && destPool.getType() != StoragePoolType.RBD) {
        if (sourceFormat == PhysicalDiskFormat.TAR && destFormat == PhysicalDiskFormat.DIR) {
            // LXC template
            Script.runSimpleBashScript("cp " + sourcePath + " " + destPath);
        } else if (sourceFormat == PhysicalDiskFormat.TAR) {
            Script.runSimpleBashScript("tar -x -f " + sourcePath + " -C " + destPath, timeout);
        } else if (sourceFormat == PhysicalDiskFormat.DIR) {
            Script.runSimpleBashScript("mkdir -p " + destPath);
            Script.runSimpleBashScript("chmod 755 " + destPath);
            Script.runSimpleBashScript("cp -p -r " + sourcePath + "/* " + destPath, timeout);
        } else {
            srcFile = new QemuImgFile(sourcePath, sourceFormat);
            try {
                final Map<String, String> info = qemu.info(srcFile);
                final String backingFile = info.get(new String("backing_file"));
                // qcow2 templates can just be copied into place
                if (sourceFormat.equals(destFormat) && backingFile == null && sourcePath.endsWith(".qcow2")) {
                    final String result = Script.runSimpleBashScript("cp -f " + sourcePath + " " + destPath, timeout);
                    if (result != null) {
                        throw new CloudRuntimeException("Failed to create disk: " + result);
                    }
                } else {
                    destFile = new QemuImgFile(destPath, destFormat);
                    try {
                        qemu.convert(srcFile, destFile);
                        final Map<String, String> destInfo = qemu.info(destFile);
                        final Long virtualSize = Long.parseLong(destInfo.get(new String("virtual_size")));
                        newDisk.setVirtualSize(virtualSize);
                        newDisk.setSize(virtualSize);
                    } catch (final QemuImgException e) {
                        logger.error("Failed to convert " + srcFile.getFileName() + " to " + destFile.getFileName() + " the error was: " + e.getMessage());
                        newDisk = null;
                    }
                }
            } catch (final QemuImgException e) {
                logger.error("Failed to fetch the information of file " + srcFile.getFileName() + " the error was: " + e.getMessage());
                newDisk = null;
            }
        }
    } else if (srcPool.getType() != StoragePoolType.RBD && destPool.getType() == StoragePoolType.RBD) {
        /**
         * Using qemu-img we copy the QCOW2 disk to RAW (on RBD) directly. To do so it's mandatory that librbd on the
         * system is at least 0.67.7 (Ceph Dumpling)
         */
        logger.debug("The source image is not RBD, but the destination is. We will convert into RBD format 2");
        try {
            srcFile = new QemuImgFile(sourcePath, sourceFormat);
            final String rbdDestPath = destPool.getSourceDir() + "/" + name;
            final String rbdDestFile = KvmPhysicalDisk.rbdStringBuilder(destPool.getSourceHost(), destPool.getSourcePort(), destPool.getAuthUserName(), destPool.getAuthSecret(), rbdDestPath);
            destFile = new QemuImgFile(rbdDestFile, destFormat);
            logger.debug("Starting copy from source image " + srcFile.getFileName() + " to RBD image " + rbdDestPath);
            qemu.convert(srcFile, destFile);
            logger.debug("Succesfully converted source image " + srcFile.getFileName() + " to RBD image " + rbdDestPath);
            /* We have to stat the RBD image to see how big it became afterwards */
            final Rados r = new Rados(destPool.getAuthUserName());
            r.confSet("mon_host", destPool.getSourceHost() + ":" + destPool.getSourcePort());
            r.confSet("key", destPool.getAuthSecret());
            r.confSet("client_mount_timeout", "30");
            r.connect();
            logger.debug("Succesfully connected to Ceph cluster at " + r.confGet("mon_host"));
            final IoCTX io = r.ioCtxCreate(destPool.getSourceDir());
            final Rbd rbd = new Rbd(io);
            final RbdImage image = rbd.open(name);
            final RbdImageInfo rbdInfo = image.stat();
            newDisk.setSize(rbdInfo.size);
            newDisk.setVirtualSize(rbdInfo.size);
            logger.debug("After copy the resulting RBD image " + rbdDestPath + " is " + rbdInfo.size + " bytes long");
            rbd.close(image);
            r.ioCtxDestroy(io);
        } catch (final QemuImgException e) {
            logger.error("Failed to convert from " + srcFile.getFileName() + " to " + destFile.getFileName() + " the error was: " + e.getMessage());
            newDisk = null;
        } catch (final RadosException e) {
            logger.error("A Ceph RADOS operation failed (" + e.getReturnValue() + "). The error was: " + e.getMessage());
            newDisk = null;
        } catch (final RbdException e) {
            logger.error("A Ceph RBD operation failed (" + e.getReturnValue() + "). The error was: " + e.getMessage());
            newDisk = null;
        }
    } else {
        /**
         * We let Qemu-Img do the work here. Although we could work with librbd and have that do the cloning it doesn't
         * benefit us. It's better to keep the current code in place which works
         */
        srcFile = new QemuImgFile(KvmPhysicalDisk.rbdStringBuilder(srcPool.getSourceHost(), srcPool.getSourcePort(), srcPool.getAuthUserName(), srcPool.getAuthSecret(), sourcePath));
        srcFile.setFormat(sourceFormat);
        destFile = new QemuImgFile(destPath);
        destFile.setFormat(destFormat);
        try {
            qemu.convert(srcFile, destFile);
        } catch (final QemuImgException e) {
            logger.error("Failed to convert " + srcFile.getFileName() + " to " + destFile.getFileName() + " the error was: " + e.getMessage());
            newDisk = null;
        }
    }
    if (newDisk == null) {
        throw new CloudRuntimeException("Failed to copy " + disk.getPath() + " to " + name);
    }
    return newDisk;
}
Also used : Rados(com.ceph.rados.Rados) RadosException(com.ceph.rados.exceptions.RadosException) RbdImageInfo(com.ceph.rbd.jna.RbdImageInfo) PhysicalDiskFormat(com.cloud.utils.qemu.QemuImg.PhysicalDiskFormat) QemuImg(com.cloud.utils.qemu.QemuImg) QemuImgFile(com.cloud.utils.qemu.QemuImgFile) Rbd(com.ceph.rbd.Rbd) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) QemuImgException(com.cloud.utils.qemu.QemuImgException) RbdImage(com.ceph.rbd.RbdImage) IoCTX(com.ceph.rados.IoCTX) RbdException(com.ceph.rbd.RbdException)

Example 3 with PhysicalDiskFormat

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

the class QemuImgFileTest method testFileNameAndFormatAtContructor.

@Test
public void testFileNameAndFormatAtContructor() {
    final PhysicalDiskFormat format = PhysicalDiskFormat.RAW;
    final String filename = "/tmp/test-image.qcow2";
    final QemuImgFile file = new QemuImgFile(filename, format);
    assertEquals(file.getFileName(), filename);
    assertEquals(file.getFormat(), format);
}
Also used : PhysicalDiskFormat(com.cloud.utils.qemu.QemuImg.PhysicalDiskFormat) Test(org.junit.Test)

Example 4 with PhysicalDiskFormat

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

the class QemuImgTest method testConvertAdvanced.

@Test
public void testConvertAdvanced() throws QemuImgException {
    final long srcSize = 4019200;
    final String srcFileName = "/tmp/" + UUID.randomUUID() + ".qcow2";
    final String destFileName = "/tmp/" + UUID.randomUUID() + ".qcow2";
    final PhysicalDiskFormat srcFormat = PhysicalDiskFormat.RAW;
    final PhysicalDiskFormat destFormat = PhysicalDiskFormat.QCOW2;
    final QemuImgFile srcFile = new QemuImgFile(srcFileName, srcSize, srcFormat);
    final QemuImgFile destFile = new QemuImgFile(destFileName, destFormat);
    final QemuImg qemu = new QemuImg(0);
    qemu.create(srcFile);
    qemu.convert(srcFile, destFile);
    final Map<String, String> info = qemu.info(destFile);
    final PhysicalDiskFormat infoFormat = PhysicalDiskFormat.valueOf(info.get(new String("format")).toUpperCase());
    assertEquals(destFormat, infoFormat);
    final Long infoSize = Long.parseLong(info.get(new String("virtual_size")));
    assertEquals(Long.valueOf(srcSize), Long.valueOf(infoSize));
    final File sf = new File(srcFileName);
    sf.delete();
    final File df = new File(destFileName);
    df.delete();
}
Also used : File(java.io.File) PhysicalDiskFormat(com.cloud.utils.qemu.QemuImg.PhysicalDiskFormat) Test(org.junit.Test)

Example 5 with PhysicalDiskFormat

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

the class KvmStorageProcessor method createVolume.

@Override
public Answer createVolume(final CreateObjectCommand cmd) {
    final VolumeObjectTO volume = (VolumeObjectTO) cmd.getData();
    final PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO) volume.getDataStore();
    KvmStoragePool primaryPool = null;
    KvmPhysicalDisk vol = null;
    final long disksize;
    try {
        primaryPool = storagePoolMgr.getStoragePool(primaryStore.getPoolType(), primaryStore.getUuid());
        disksize = volume.getSize();
        final PhysicalDiskFormat format;
        if (volume.getFormat() == null) {
            format = primaryPool.getDefaultFormat();
        } else {
            format = PhysicalDiskFormat.valueOf(volume.getFormat().toString().toUpperCase());
        }
        vol = primaryPool.createPhysicalDisk(volume.getUuid(), format, volume.getProvisioningType(), disksize);
        final VolumeObjectTO newVol = new VolumeObjectTO();
        if (vol != null) {
            newVol.setPath(vol.getName());
        }
        newVol.setSize(volume.getSize());
        newVol.setFormat(ImageFormat.valueOf(format.toString().toUpperCase()));
        return new CreateObjectAnswer(newVol);
    } catch (final Exception e) {
        logger.debug("Failed to create volume: ", e);
        return new CreateObjectAnswer(e.toString());
    }
}
Also used : PrimaryDataStoreTO(com.cloud.storage.to.PrimaryDataStoreTO) CreateObjectAnswer(com.cloud.storage.command.CreateObjectAnswer) VolumeObjectTO(com.cloud.storage.to.VolumeObjectTO) URISyntaxException(java.net.URISyntaxException) LibvirtException(org.libvirt.LibvirtException) QemuImgException(com.cloud.utils.qemu.QemuImgException) FileNotFoundException(java.io.FileNotFoundException) InternalErrorException(com.cloud.exception.InternalErrorException) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) PhysicalDiskFormat(com.cloud.utils.qemu.QemuImg.PhysicalDiskFormat)

Aggregations

PhysicalDiskFormat (com.cloud.utils.qemu.QemuImg.PhysicalDiskFormat)6 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)3 Test (org.junit.Test)3 QemuImgException (com.cloud.utils.qemu.QemuImgException)2 IoCTX (com.ceph.rados.IoCTX)1 Rados (com.ceph.rados.Rados)1 RadosException (com.ceph.rados.exceptions.RadosException)1 Rbd (com.ceph.rbd.Rbd)1 RbdException (com.ceph.rbd.RbdException)1 RbdImage (com.ceph.rbd.RbdImage)1 RbdImageInfo (com.ceph.rbd.jna.RbdImageInfo)1 InternalErrorException (com.cloud.exception.InternalErrorException)1 StoragePoolType (com.cloud.storage.Storage.StoragePoolType)1 CreateObjectAnswer (com.cloud.storage.command.CreateObjectAnswer)1 PrimaryDataStoreTO (com.cloud.storage.to.PrimaryDataStoreTO)1 VolumeObjectTO (com.cloud.storage.to.VolumeObjectTO)1 QemuImg (com.cloud.utils.qemu.QemuImg)1 QemuImgFile (com.cloud.utils.qemu.QemuImgFile)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1