Search in sources :

Example 1 with PhysicalDiskFormat

use of com.cloud.model.enumeration.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.model.enumeration.PhysicalDiskFormat) Test(org.junit.Test)

Example 2 with PhysicalDiskFormat

use of com.cloud.model.enumeration.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();
    final KvmStoragePool primaryPool;
    final KvmPhysicalDisk vol;
    final long disksize;
    try {
        primaryPool = this.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) {
        this.logger.debug("Failed to create volume: ", e);
        return new CreateObjectAnswer(e.toString());
    }
}
Also used : PrimaryDataStoreTO(com.cloud.legacymodel.to.PrimaryDataStoreTO) CreateObjectAnswer(com.cloud.legacymodel.communication.answer.CreateObjectAnswer) VolumeObjectTO(com.cloud.legacymodel.to.VolumeObjectTO) LibvirtException(org.libvirt.LibvirtException) QemuImgException(com.cloud.agent.resource.kvm.storage.utils.QemuImgException) FileNotFoundException(java.io.FileNotFoundException) InternalErrorException(com.cloud.legacymodel.exceptions.InternalErrorException) ConfigurationException(javax.naming.ConfigurationException) IOException(java.io.IOException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) PhysicalDiskFormat(com.cloud.model.enumeration.PhysicalDiskFormat)

Example 3 with PhysicalDiskFormat

use of com.cloud.model.enumeration.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;
    this.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, StorageProvisioningType.THIN, disk.getVirtualSize());
        } else {
            newDisk = destPool.createPhysicalDisk(name, StorageProvisioningType.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("backing_file");
                // qcow2 templates can just be copied into place
                if (sourceFormat.equals(destFormat) && backingFile == null) {
                    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("virtual_size"));
                        newDisk.setVirtualSize(virtualSize);
                        newDisk.setSize(virtualSize);
                    } catch (final QemuImgException e) {
                        this.logger.error("Failed to convert " + srcFile.getFileName() + " to " + destFile.getFileName() + " the error was: " + e.getMessage());
                        newDisk = null;
                    }
                }
            } catch (final QemuImgException e) {
                this.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)
         */
        this.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);
            this.logger.debug("Starting copy from source image " + srcFile.getFileName() + " to RBD image " + rbdDestPath);
            qemu.convert(srcFile, destFile);
            this.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();
            this.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);
            this.logger.debug("After copy the resulting RBD image " + rbdDestPath + " is " + rbdInfo.size + " bytes long");
            rbd.close(image);
            r.ioCtxDestroy(io);
        } catch (final QemuImgException e) {
            this.logger.error("Failed to convert from " + srcFile.getFileName() + " to " + destFile.getFileName() + " the error was: " + e.getMessage());
            newDisk = null;
        } catch (final RadosException e) {
            this.logger.error("A Ceph RADOS operation failed (" + e.getReturnValue() + "). The error was: " + e.getMessage());
            newDisk = null;
        } catch (final RbdException e) {
            this.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) {
            this.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.model.enumeration.PhysicalDiskFormat) QemuImg(com.cloud.agent.resource.kvm.storage.utils.QemuImg) QemuImgFile(com.cloud.agent.resource.kvm.storage.utils.QemuImgFile) Rbd(com.ceph.rbd.Rbd) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) QemuImgException(com.cloud.agent.resource.kvm.storage.utils.QemuImgException) RbdImage(com.ceph.rbd.RbdImage) IoCTX(com.ceph.rados.IoCTX) RbdException(com.ceph.rbd.RbdException)

Example 4 with PhysicalDiskFormat

use of com.cloud.model.enumeration.PhysicalDiskFormat in project cosmic by MissionCriticalCloud.

the class QemuImgFileTest method testFileNameAndSizeAndFormatAtContructor.

@Test
public void testFileNameAndSizeAndFormatAtContructor() {
    final PhysicalDiskFormat format = PhysicalDiskFormat.RAW;
    final long size = 1024;
    final String filename = "/tmp/test-image.qcow2";
    final QemuImgFile file = new QemuImgFile(filename, size, format);
    assertEquals(file.getFileName(), filename);
    assertEquals(file.getSize(), size);
    assertEquals(file.getFormat(), format);
}
Also used : PhysicalDiskFormat(com.cloud.model.enumeration.PhysicalDiskFormat) Test(org.junit.Test)

Example 5 with PhysicalDiskFormat

use of com.cloud.model.enumeration.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.model.enumeration.PhysicalDiskFormat) Test(org.junit.Test)

Aggregations

PhysicalDiskFormat (com.cloud.model.enumeration.PhysicalDiskFormat)6 QemuImgException (com.cloud.agent.resource.kvm.storage.utils.QemuImgException)3 Test (org.junit.Test)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 QemuImg (com.cloud.agent.resource.kvm.storage.utils.QemuImg)2 QemuImgFile (com.cloud.agent.resource.kvm.storage.utils.QemuImgFile)2 CloudRuntimeException (com.cloud.legacymodel.exceptions.CloudRuntimeException)2 RbdImageInfo (com.ceph.rbd.jna.RbdImageInfo)1 RbdSnapInfo (com.ceph.rbd.jna.RbdSnapInfo)1 CreateObjectAnswer (com.cloud.legacymodel.communication.answer.CreateObjectAnswer)1 InternalErrorException (com.cloud.legacymodel.exceptions.InternalErrorException)1 PrimaryDataStoreTO (com.cloud.legacymodel.to.PrimaryDataStoreTO)1 VolumeObjectTO (com.cloud.legacymodel.to.VolumeObjectTO)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1