Examples of QemuImg


Examples of org.apache.cloudstack.utils.qemu.QemuImg

                    Script.runSimpleBashScript("chmod 755 " + disk.getPath());
                    Script.runSimpleBashScript("cp -p -r " + template.getPath() + "/* " + disk.getPath());
                } else if (format == PhysicalDiskFormat.QCOW2) {
                    QemuImgFile backingFile = new QemuImgFile(template.getPath(), template.getFormat());
                    QemuImgFile destFile = new QemuImgFile(disk.getPath());
                    QemuImg qemu = new QemuImg();
                    qemu.create(destFile, backingFile);
                } else if (format == PhysicalDiskFormat.RAW) {
                    QemuImgFile sourceFile = new QemuImgFile(template.getPath(), template.getFormat());
                    QemuImgFile destFile = new QemuImgFile(disk.getPath(), PhysicalDiskFormat.RAW);
                    QemuImg qemu = new QemuImg();
                    qemu.convert(sourceFile, destFile);
                }
            } else {
                format = PhysicalDiskFormat.RAW;
                disk = new KVMPhysicalDisk(destPool.getSourceDir() + "/" + newUuid, newUuid, destPool);
                disk.setFormat(format);
                disk.setSize(template.getVirtualSize());
                disk.setVirtualSize(disk.getSize());

                QemuImg qemu = new QemuImg();
                QemuImgFile srcFile;
                QemuImgFile destFile = new QemuImgFile(KVMPhysicalDisk.RBDStringBuilder(destPool.getSourceHost(),
                        destPool.getSourcePort(),
                        destPool.getAuthUserName(),
                        destPool.getAuthSecret(),
                        disk.getPath()));
                destFile.setFormat(format);

                if (srcPool.getType() != StoragePoolType.RBD) {
                    srcFile = new QemuImgFile(template.getPath(), template.getFormat());
                    qemu.convert(srcFile, destFile);
                } else {

                    /**
                     * We have to find out if the source file is in the same RBD pool and has
                     * RBD format 2 before we can do a layering/clone operation on the RBD image
View Full Code Here

Examples of org.apache.cloudstack.utils.qemu.QemuImg

        }

        String destPath = newDisk.getPath();
        PhysicalDiskFormat destFormat = newDisk.getFormat();

        QemuImg qemu = new QemuImg();
        QemuImgFile srcFile = null;
        QemuImgFile destFile = null;

        if ((srcPool.getType() != StoragePoolType.RBD) && (destPool.getType() != StoragePoolType.RBD)) {
            if (sourceFormat == PhysicalDiskFormat.TAR) {
                Script.runSimpleBashScript("tar -x -f " + sourcePath + " -C " + destPath);
            } else if (sourceFormat == PhysicalDiskFormat.DIR) {
                Script.runSimpleBashScript("mkdir -p " + destPath);
                Script.runSimpleBashScript("chmod 755 " + destPath);
                Script.runSimpleBashScript("cp -p -r " + sourcePath + "/* " + destPath);
            } else {
                srcFile = new QemuImgFile(sourcePath, sourceFormat);
                try {
                    Map<String, String> info = qemu.info(srcFile);
                    String backingFile = info.get(new String("backing_file"));
                    if (sourceFormat.equals(destFormat) && backingFile == null) {
                        Script.runSimpleBashScript("cp -f " + sourcePath + " " + destPath);
                    } else {
                        destFile = new QemuImgFile(destPath, destFormat);
                        try {
                            qemu.convert(srcFile, destFile);
                        } catch (QemuImgException e) {
                            s_logger.error("Failed to convert " + srcFile.getFileName() + " to "
                                    + destFile.getFileName() + " the error was: " + e.getMessage());
                            newDisk = null;
                        }
                    }
                } catch (QemuImgException e) {
                    s_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))  {
            /**
              * Qemu doesn't support writing to RBD format 2 directly, so we have to write to a temporary RAW file first
              * which we then convert to RBD format 2.
              *
              * A HUGE performance gain can be achieved here if QCOW2 -> RBD format 2 can be done in one step
              */
            s_logger.debug("The source image is not RBD, but the destination is. We will convert into RBD format 2");
            String sourceFile;
            boolean useTmpFile = false;

            try {
                if (sourceFormat != destFormat) {
                    srcFile = new QemuImgFile(sourcePath, sourceFormat);
                    destFile = new QemuImgFile("/tmp/" + name);
                    s_logger.debug("Converting " + srcFile.getFileName() " to " + destFile.getFileName() " as a temporary file for RBD conversion");
                    qemu.convert(srcFile, destFile);
                    sourceFile = destFile.getFileName();
                    useTmpFile = true;
                } else {
                    // Source file is RAW, we can write directly to RBD
                    sourceFile = sourcePath;
                }

                // We now convert the temporary file to a RBD image with format 2
                Rados r = new Rados(destPool.getAuthUserName());
                r.confSet("mon_host", destPool.getSourceHost() + ":" + destPool.getSourcePort());
                r.confSet("key", destPool.getAuthSecret());
                r.connect();
                s_logger.debug("Succesfully connected to Ceph cluster at " + r.confGet("mon_host"));

                IoCTX io = r.ioCtxCreate(destPool.getSourceDir());
                Rbd rbd = new Rbd(io);

                s_logger.debug("Creating RBD image " + name + " in Ceph pool " + destPool.getSourceDir() + " with RBD format 2");
                rbd.create(name, disk.getVirtualSize(), this.rbdFeatures, this.rbdOrder);

                RbdImage image = rbd.open(name);

                File fh = new File(sourceFile);
                BufferedInputStream bis = new BufferedInputStream(new FileInputStream(fh));

                int chunkSize = 4194304;
                long offset = 0;
                s_logger.debug("Reading file " + sourceFile + " (" + fh.length() + " bytes) into RBD image " + name + " in chunks of " + chunkSize + " bytes");
                while(true) {
                    byte[] buf = new byte[chunkSize];

                    int bytes = bis.read(buf);
                    if (bytes <= 0) {
                        break;
                    }
                    image.write(buf, offset, bytes);
                    offset += bytes;
                }
                s_logger.debug("Completed writing " + sourceFile + " to RBD image " + name + ". Bytes written: " + offset);
                bis.close();

                if (useTmpFile) {
                    s_logger.debug("Removing temporary file " + sourceFile);
                    fh.delete();
                }

                /* Snapshot the image and protect that snapshot so we can clone (layer) from it */
                s_logger.debug("Creating RBD snapshot " + this.rbdTemplateSnapName + " on image " + name);
                image.snapCreate(this.rbdTemplateSnapName);
                s_logger.debug("Protecting RBD snapshot " + this.rbdTemplateSnapName + " on image " + name);
                image.snapProtect(this.rbdTemplateSnapName);

                rbd.close(image);
                r.ioCtxDestroy(io);
            } catch (QemuImgException e) {
                s_logger.error("Failed to do a temp convert from " + srcFile.getFileName() + " to "
                        + destFile.getFileName() + " the error was: " + e.getMessage());
                newDisk = null;
            } catch (RadosException e) {
                s_logger.error("A Ceph RADOS operation failed (" + e.getReturnValue() + "). The error was: " + e.getMessage());
                newDisk = null;
            } catch (RbdException e) {
                s_logger.error("A Ceph RBD operation failed (" + e.getReturnValue() + "). The error was: " + e.getMessage());
                newDisk = null;
            } catch (IOException e) {
                s_logger.error("Failed reading the temporary file during the conversion to RBD: " + 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 (QemuImgException e) {
                s_logger.error("Failed to convert " + srcFile.getFileName() + " to "
                        + destFile.getFileName() + " the error was: " + e.getMessage());
                newDisk = null;
            }
View Full Code Here

Examples of org.apache.cloudstack.utils.qemu.QemuImg

                srcFile.setFormat(PhysicalDiskFormat.RAW);

                QemuImgFile destFile = new QemuImgFile(tmpltPath + "/" + cmd.getUniqueName() + ".qcow2");
                destFile.setFormat(PhysicalDiskFormat.QCOW2);

                QemuImg q = new QemuImg();
                try {
                    q.convert(srcFile, destFile);
                } catch (QemuImgException e) {
                    s_logger.error("Failed to create new template while converting "
                                    + srcFile.getFileName() + " to " + destFile.getFileName() + " the error was: " + e.getMessage());
                }
View Full Code Here

Examples of org.apache.cloudstack.utils.qemu.QemuImg

                srcFile.setFormat(PhysicalDiskFormat.RAW);

                QemuImgFile destFile = new QemuImgFile(tmpltPath + "/" + templateName + ".qcow2");
                destFile.setFormat(PhysicalDiskFormat.QCOW2);

                QemuImg q = new QemuImg();
                try {
                    q.convert(srcFile, destFile);
                } catch (QemuImgException e) {
                    s_logger.error("Failed to create new template while converting " + srcFile.getFileName() + " to "
                            + destFile.getFileName() + " the error was: " + e.getMessage());
                }
View Full Code Here

Examples of org.apache.cloudstack.utils.qemu.QemuImg

                    Script.runSimpleBashScript("chmod 755 " + disk.getPath());
                    Script.runSimpleBashScript("cp -p -r " + template.getPath() + "/* " + disk.getPath(), timeout);
                } else if (format == PhysicalDiskFormat.QCOW2) {
                    QemuImgFile backingFile = new QemuImgFile(template.getPath(), template.getFormat());
                    QemuImgFile destFile = new QemuImgFile(disk.getPath());
                    QemuImg qemu = new QemuImg(timeout);
                    qemu.create(destFile, backingFile);
                } else if (format == PhysicalDiskFormat.RAW) {
                    QemuImgFile sourceFile = new QemuImgFile(template.getPath(), template.getFormat());
                    QemuImgFile destFile = new QemuImgFile(disk.getPath(), PhysicalDiskFormat.RAW);
                    QemuImg qemu = new QemuImg(timeout);
                    qemu.convert(sourceFile, destFile);
                }
            } else {
                format = PhysicalDiskFormat.RAW;
                disk = new KVMPhysicalDisk(destPool.getSourceDir() + "/" + newUuid, newUuid, destPool);
                disk.setFormat(format);
                disk.setSize(template.getVirtualSize());
                disk.setVirtualSize(disk.getSize());

                QemuImg qemu = new QemuImg(timeout);
                QemuImgFile srcFile;
                QemuImgFile destFile = new QemuImgFile(KVMPhysicalDisk.RBDStringBuilder(destPool.getSourceHost(),
                        destPool.getSourcePort(),
                        destPool.getAuthUserName(),
                        destPool.getAuthSecret(),
                        disk.getPath()));
                destFile.setFormat(format);

                if (srcPool.getType() != StoragePoolType.RBD) {
                    srcFile = new QemuImgFile(template.getPath(), template.getFormat());
                    qemu.convert(srcFile, destFile);
                } else {

                    /**
                     * We have to find out if the source file is in the same RBD pool and has
                     * RBD format 2 before we can do a layering/clone operation on the RBD image
View Full Code Here

Examples of org.apache.cloudstack.utils.qemu.QemuImg

        }

        String destPath = newDisk.getPath();
        PhysicalDiskFormat destFormat = newDisk.getFormat();

        QemuImg qemu = new QemuImg(timeout);
        QemuImgFile srcFile = null;
        QemuImgFile destFile = null;

        if ((srcPool.getType() != StoragePoolType.RBD) && (destPool.getType() != StoragePoolType.RBD)) {
            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 {
                    Map<String, String> info = qemu.info(srcFile);
                    String backingFile = info.get(new String("backing_file"));
                    if (sourceFormat.equals(destFormat) && backingFile == null) {
                        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);
                        } catch (QemuImgException e) {
                            s_logger.error("Failed to convert " + srcFile.getFileName() + " to "
                                    + destFile.getFileName() + " the error was: " + e.getMessage());
                            newDisk = null;
                        }
                    }
                } catch (QemuImgException e) {
                    s_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))  {
            /**
              * Qemu doesn't support writing to RBD format 2 directly, so we have to write to a temporary RAW file first
              * which we then convert to RBD format 2.
              *
              * A HUGE performance gain can be achieved here if QCOW2 -> RBD format 2 can be done in one step
              */
            s_logger.debug("The source image is not RBD, but the destination is. We will convert into RBD format 2");
            String sourceFile;
            boolean useTmpFile = false;

            try {
                if (sourceFormat != destFormat) {
                    srcFile = new QemuImgFile(sourcePath, sourceFormat);
                    destFile = new QemuImgFile("/tmp/" + name);
                    s_logger.debug("Converting " + srcFile.getFileName() " to " + destFile.getFileName() " as a temporary file for RBD conversion");
                    qemu.convert(srcFile, destFile);
                    sourceFile = destFile.getFileName();
                    useTmpFile = true;
                } else {
                    // Source file is RAW, we can write directly to RBD
                    sourceFile = sourcePath;
                }

                // We now convert the temporary file to a RBD image with format 2
                Rados r = new Rados(destPool.getAuthUserName());
                r.confSet("mon_host", destPool.getSourceHost() + ":" + destPool.getSourcePort());
                r.confSet("key", destPool.getAuthSecret());
                r.connect();
                s_logger.debug("Succesfully connected to Ceph cluster at " + r.confGet("mon_host"));

                IoCTX io = r.ioCtxCreate(destPool.getSourceDir());
                Rbd rbd = new Rbd(io);

                s_logger.debug("Creating RBD image " + name + " in Ceph pool " + destPool.getSourceDir() + " with RBD format 2");
                rbd.create(name, disk.getVirtualSize(), this.rbdFeatures, this.rbdOrder);

                RbdImage image = rbd.open(name);

                File fh = new File(sourceFile);
                BufferedInputStream bis = new BufferedInputStream(new FileInputStream(fh));

                int chunkSize = 4194304;
                long offset = 0;
                s_logger.debug("Reading file " + sourceFile + " (" + fh.length() + " bytes) into RBD image " + name + " in chunks of " + chunkSize + " bytes");
                while(true) {
                    byte[] buf = new byte[chunkSize];

                    int bytes = bis.read(buf);
                    if (bytes <= 0) {
                        break;
                    }
                    image.write(buf, offset, bytes);
                    offset += bytes;
                }
                s_logger.debug("Completed writing " + sourceFile + " to RBD image " + name + ". Bytes written: " + offset);
                bis.close();

                if (useTmpFile) {
                    s_logger.debug("Removing temporary file " + sourceFile);
                    fh.delete();
                }

                /* Snapshot the image and protect that snapshot so we can clone (layer) from it */
                s_logger.debug("Creating RBD snapshot " + this.rbdTemplateSnapName + " on image " + name);
                image.snapCreate(this.rbdTemplateSnapName);
                s_logger.debug("Protecting RBD snapshot " + this.rbdTemplateSnapName + " on image " + name);
                image.snapProtect(this.rbdTemplateSnapName);

                rbd.close(image);
                r.ioCtxDestroy(io);
            } catch (QemuImgException e) {
                s_logger.error("Failed to do a temp convert from " + srcFile.getFileName() + " to "
                        + destFile.getFileName() + " the error was: " + e.getMessage());
                newDisk = null;
            } catch (RadosException e) {
                s_logger.error("A Ceph RADOS operation failed (" + e.getReturnValue() + "). The error was: " + e.getMessage());
                newDisk = null;
            } catch (RbdException e) {
                s_logger.error("A Ceph RBD operation failed (" + e.getReturnValue() + "). The error was: " + e.getMessage());
                newDisk = null;
            } catch (IOException e) {
                s_logger.error("Failed reading the temporary file during the conversion to RBD: " + 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 (QemuImgException e) {
                s_logger.error("Failed to convert " + srcFile.getFileName() + " to "
                        + destFile.getFileName() + " the error was: " + e.getMessage());
                newDisk = null;
            }
View Full Code Here

Examples of org.apache.cloudstack.utils.qemu.QemuImg

                srcFile.setFormat(PhysicalDiskFormat.RAW);

                QemuImgFile destFile = new QemuImgFile(tmpltPath + "/" + cmd.getUniqueName() + ".qcow2");
                destFile.setFormat(PhysicalDiskFormat.QCOW2);

                QemuImg q = new QemuImg(0);
                try {
                    q.convert(srcFile, destFile);
                } catch (QemuImgException e) {
                    s_logger.error("Failed to create new template while converting "
                                    + srcFile.getFileName() + " to " + destFile.getFileName() + " the error was: " + e.getMessage());
                }
View Full Code Here

Examples of org.apache.cloudstack.utils.qemu.QemuImg

                srcFile.setFormat(PhysicalDiskFormat.RAW);

                QemuImgFile destFile = new QemuImgFile(tmpltPath + "/" + cmd.getUniqueName() + ".qcow2");
                destFile.setFormat(PhysicalDiskFormat.QCOW2);

                QemuImg q = new QemuImg(0);
                try {
                    q.convert(srcFile, destFile);
                } catch (QemuImgException e) {
                    s_logger.error("Failed to create new template while converting " + srcFile.getFileName() + " to " + destFile.getFileName() + " the error was: " +
                            e.getMessage());
                }
View Full Code Here

Examples of org.apache.cloudstack.utils.qemu.QemuImg

                    if (size > template.getVirtualSize()) {
                        destFile.setSize(size);
                    } else {
                        destFile.setSize(template.getVirtualSize());
                    }
                    QemuImg qemu = new QemuImg(timeout);
                    qemu.create(destFile, backingFile);
                } else if (format == PhysicalDiskFormat.RAW) {
                    QemuImgFile sourceFile = new QemuImgFile(template.getPath(), template.getFormat());
                    QemuImgFile destFile = new QemuImgFile(disk.getPath(), PhysicalDiskFormat.RAW);
                    if (size > template.getVirtualSize()) {
                        destFile.setSize(size);
                    } else {
                        destFile.setSize(template.getVirtualSize());
                    }
                    QemuImg qemu = new QemuImg(timeout);
                    qemu.convert(sourceFile, destFile);
                }
            } else {
                format = PhysicalDiskFormat.RAW;
                disk = new KVMPhysicalDisk(destPool.getSourceDir() + "/" + newUuid, newUuid, destPool);
                disk.setFormat(format);
                if (size > template.getVirtualSize()) {
                    disk.setSize(size);
                    disk.setVirtualSize(size);
                } else {
                    // leave these as they were if size isn't applicable
                    disk.setSize(template.getVirtualSize());
                    disk.setVirtualSize(disk.getSize());
                }

                QemuImg qemu = new QemuImg(timeout);
                QemuImgFile srcFile;
                QemuImgFile destFile =
                        new QemuImgFile(KVMPhysicalDisk.RBDStringBuilder(destPool.getSourceHost(), destPool.getSourcePort(), destPool.getAuthUserName(),
                                destPool.getAuthSecret(), disk.getPath()));
                destFile.setFormat(format);
                if (size > template.getVirtualSize()) {
                    destFile.setSize(size);
                } else {
                    destFile.setSize(template.getVirtualSize());
                }

                if (srcPool.getType() != StoragePoolType.RBD) {
                    srcFile = new QemuImgFile(template.getPath(), template.getFormat());
                    qemu.convert(srcFile, destFile);
                } else {

                    /**
                     * We have to find out if the source file is in the same RBD pool and has
                     * RBD format 2 before we can do a layering/clone operation on the RBD image
View Full Code Here

Examples of org.apache.cloudstack.utils.qemu.QemuImg

        }

        String destPath = newDisk.getPath();
        PhysicalDiskFormat destFormat = newDisk.getFormat();

        QemuImg qemu = new QemuImg(timeout);
        QemuImgFile srcFile = null;
        QemuImgFile destFile = null;

        if ((srcPool.getType() != StoragePoolType.RBD) && (destPool.getType() != StoragePoolType.RBD)) {
            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 {
                    Map<String, String> info = qemu.info(srcFile);
                    String backing_file_str = "backing_file";
                    String backingFile = info.get(backing_file_str);
                    // qcow2 templates can just be copied into place
                    if (sourceFormat.equals(destFormat) && backingFile == null && sourcePath.endsWith(".qcow2")) {
                        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);
                        } catch (QemuImgException e) {
                            s_logger.error("Failed to convert " + srcFile.getFileName() + " to " + destFile.getFileName() + " the error was: " + e.getMessage());
                            newDisk = null;
                        }
                    }
                } catch (QemuImgException e) {
                    s_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)
             */
            s_logger.debug("The source image is not RBD, but the destination is. We will convert into RBD format 2");
            String sourceFile;
            boolean useTmpFile = false;

            try {
                srcFile = new QemuImgFile(sourcePath, sourceFormat);
                String rbdDestFile = KVMPhysicalDisk.RBDStringBuilder(destPool.getSourceHost(),
                                                                      destPool.getSourcePort(),
                                                                      destPool.getAuthUserName(),
                                                                      destPool.getAuthSecret(),
                                                                      destPool.getSourceDir() + "/" + name);
                destFile = new QemuImgFile(rbdDestFile, destFormat);

                s_logger.debug("Starting copy from source image " + srcFile.getFileName() + " to RBD image " + destPool.getSourceDir() + "/" + name);
                qemu.convert(srcFile, destFile);
                s_logger.debug("Succesfully converted source image " + srcFile.getFileName() + " to RBD image " + destPool.getSourceDir() + "/" + name);

                /* We still have to create and protect a RBD snapshot in order to do cloning */
                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();
                s_logger.debug("Succesfully connected to Ceph cluster at " + r.confGet("mon_host"));

                IoCTX io = r.ioCtxCreate(destPool.getSourceDir());
                Rbd rbd = new Rbd(io);

                RbdImage image = rbd.open(name);

                /* Snapshot the image and protect that snapshot so we can clone (layer) from it */
                s_logger.debug("Creating RBD snapshot " + rbdTemplateSnapName + " on image " + name);
                image.snapCreate(rbdTemplateSnapName);
                s_logger.debug("Protecting RBD snapshot " + rbdTemplateSnapName + " on image " + name);
                image.snapProtect(rbdTemplateSnapName);

                rbd.close(image);
                r.ioCtxDestroy(io);
            } catch (QemuImgException e) {
                s_logger.error("Failed to convert from " + srcFile.getFileName() + " to " + destFile.getFileName() + " the error was: " + e.getMessage());
                newDisk = null;
            } catch (RadosException e) {
                s_logger.error("A Ceph RADOS operation failed (" + e.getReturnValue() + "). The error was: " + e.getMessage());
                newDisk = null;
            } catch (RbdException e) {
                s_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 (QemuImgException e) {
                s_logger.error("Failed to convert " + srcFile.getFileName() + " to " + destFile.getFileName() + " the error was: " + e.getMessage());
                newDisk = null;
            }
        }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.