Search in sources :

Example 1 with QCOW2Processor

use of com.cloud.storage.template.QCOW2Processor in project CloudStack-archive by CloudStack-extras.

the class LibvirtComputingResource method execute.

protected CreatePrivateTemplateAnswer execute(CreatePrivateTemplateFromVolumeCommand cmd) {
    String secondaryStorageURL = cmd.getSecondaryStorageUrl();
    KVMStoragePool secondaryStorage = null;
    try {
        Connect conn = LibvirtConnection.getConnection();
        String templateFolder = cmd.getAccountId() + File.separator + cmd.getTemplateId() + File.separator;
        String templateInstallFolder = "/template/tmpl/" + templateFolder;
        secondaryStorage = _storagePoolMgr.getStoragePoolByURI(secondaryStorageURL);
        KVMStoragePool primary = _storagePoolMgr.getStoragePool(cmd.getPrimaryStoragePoolNameLabel());
        KVMPhysicalDisk disk = primary.getPhysicalDisk(cmd.getVolumePath());
        String tmpltPath = secondaryStorage.getLocalPath() + File.separator + templateInstallFolder;
        _storage.mkdirs(tmpltPath);
        Script command = new Script(_createTmplPath, _cmdsTimeout, s_logger);
        command.add("-f", disk.getPath());
        command.add("-t", tmpltPath);
        command.add("-n", cmd.getUniqueName() + ".qcow2");
        String result = command.execute();
        if (result != null) {
            s_logger.debug("failed to create template: " + result);
            return new CreatePrivateTemplateAnswer(cmd, false, result);
        }
        Map<String, Object> params = new HashMap<String, Object>();
        params.put(StorageLayer.InstanceConfigKey, _storage);
        Processor qcow2Processor = new QCOW2Processor();
        qcow2Processor.configure("QCOW2 Processor", params);
        FormatInfo info = qcow2Processor.process(tmpltPath, null, cmd.getUniqueName());
        TemplateLocation loc = new TemplateLocation(_storage, tmpltPath);
        loc.create(1, true, cmd.getUniqueName());
        loc.addFormat(info);
        loc.save();
        return new CreatePrivateTemplateAnswer(cmd, true, null, templateInstallFolder + cmd.getUniqueName() + ".qcow2", info.virtualSize, info.size, cmd.getUniqueName(), ImageFormat.QCOW2);
    } catch (LibvirtException e) {
        s_logger.debug("Failed to get secondary storage pool: " + e.toString());
        return new CreatePrivateTemplateAnswer(cmd, false, e.toString());
    } catch (InternalErrorException e) {
        return new CreatePrivateTemplateAnswer(cmd, false, e.toString());
    } catch (IOException e) {
        return new CreatePrivateTemplateAnswer(cmd, false, e.toString());
    } catch (ConfigurationException e) {
        return new CreatePrivateTemplateAnswer(cmd, false, e.toString());
    } catch (CloudRuntimeException e) {
        return new CreatePrivateTemplateAnswer(cmd, false, e.toString());
    } finally {
        if (secondaryStorage != null) {
            secondaryStorage.delete();
        }
    }
}
Also used : Script(com.cloud.utils.script.Script) QCOW2Processor(com.cloud.storage.template.QCOW2Processor) Processor(com.cloud.storage.template.Processor) LibvirtException(org.libvirt.LibvirtException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) KVMPhysicalDisk(com.cloud.agent.storage.KVMPhysicalDisk) Connect(org.libvirt.Connect) InternalErrorException(com.cloud.exception.InternalErrorException) IOException(java.io.IOException) QCOW2Processor(com.cloud.storage.template.QCOW2Processor) KVMStoragePool(com.cloud.agent.storage.KVMStoragePool) ConfigurationException(javax.naming.ConfigurationException) TemplateLocation(com.cloud.storage.template.TemplateLocation) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) CreatePrivateTemplateAnswer(com.cloud.agent.api.storage.CreatePrivateTemplateAnswer) FormatInfo(com.cloud.storage.template.Processor.FormatInfo)

Example 2 with QCOW2Processor

use of com.cloud.storage.template.QCOW2Processor in project cosmic by MissionCriticalCloud.

the class NfsSecondaryStorageResource method getVirtualSize.

protected long getVirtualSize(final File file, final ImageFormat format) {
    Processor processor = null;
    try {
        if (format == null) {
            return file.length();
        } else if (format == ImageFormat.QCOW2) {
            processor = new QCOW2Processor();
        } else if (format == ImageFormat.VHD) {
            processor = new VhdProcessor();
        } else if (format == ImageFormat.RAW) {
            processor = new RawImageProcessor();
        }
        if (format == ImageFormat.TAR) {
            processor = new TARProcessor();
        }
        if (processor == null) {
            return file.length();
        }
        final Map<String, Object> params = new HashMap<>();
        params.put(StorageLayer.InstanceConfigKey, _storage);
        processor.configure("template processor", params);
        return processor.getVirtualSize(file);
    } catch (final Exception e) {
        s_logger.warn("Failed to get virtual size of file " + file.getPath() + ", returning file size instead: ", e);
        return file.length();
    }
}
Also used : QCOW2Processor(com.cloud.storage.template.QCOW2Processor) VhdProcessor(com.cloud.storage.template.VhdProcessor) QCOW2Processor(com.cloud.storage.template.QCOW2Processor) TARProcessor(com.cloud.storage.template.TARProcessor) Processor(com.cloud.storage.template.Processor) RawImageProcessor(com.cloud.storage.template.RawImageProcessor) HashMap(java.util.HashMap) VhdProcessor(com.cloud.storage.template.VhdProcessor) RawImageProcessor(com.cloud.storage.template.RawImageProcessor) TARProcessor(com.cloud.storage.template.TARProcessor) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) InternalErrorException(com.cloud.exception.InternalErrorException) ConfigurationException(javax.naming.ConfigurationException)

Example 3 with QCOW2Processor

use of com.cloud.storage.template.QCOW2Processor in project cloudstack by apache.

the class KVMStorageProcessor method createTemplateFromVolumeOrSnapshot.

private Answer createTemplateFromVolumeOrSnapshot(CopyCommand cmd) {
    DataTO srcData = cmd.getSrcTO();
    final boolean isVolume;
    if (srcData instanceof VolumeObjectTO) {
        isVolume = true;
    } else if (srcData instanceof SnapshotObjectTO) {
        isVolume = false;
    } else {
        return new CopyCmdAnswer("unsupported object type");
    }
    PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO) srcData.getDataStore();
    DataTO destData = cmd.getDestTO();
    TemplateObjectTO template = (TemplateObjectTO) destData;
    DataStoreTO imageStore = template.getDataStore();
    if (!(imageStore instanceof NfsTO)) {
        return new CopyCmdAnswer("unsupported protocol");
    }
    NfsTO nfsImageStore = (NfsTO) imageStore;
    KVMStoragePool secondaryStorage = null;
    try {
        Map<String, String> details = cmd.getOptions();
        String path = details != null ? details.get(DiskTO.IQN) : null;
        if (path == null) {
            new CloudRuntimeException("The 'path' field must be specified.");
        }
        storagePoolMgr.connectPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), path, details);
        KVMPhysicalDisk srcDisk = storagePoolMgr.getPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), path);
        secondaryStorage = storagePoolMgr.getStoragePoolByURI(nfsImageStore.getUrl());
        String templateFolder = template.getPath();
        String tmpltPath = secondaryStorage.getLocalPath() + File.separator + templateFolder;
        storageLayer.mkdirs(tmpltPath);
        String templateName = UUID.randomUUID().toString();
        s_logger.debug("Converting " + srcDisk.getFormat().toString() + " disk " + srcDisk.getPath() + " into template " + templateName);
        String destName = templateFolder + "/" + templateName + ".qcow2";
        storagePoolMgr.copyPhysicalDisk(srcDisk, destName, secondaryStorage, cmd.getWaitInMillSeconds());
        File templateProp = new File(tmpltPath + "/template.properties");
        if (!templateProp.exists()) {
            templateProp.createNewFile();
        }
        String templateContent = "filename=" + templateName + ".qcow2" + System.getProperty("line.separator");
        DateFormat dateFormat = new SimpleDateFormat("MM_dd_yyyy");
        Date date = new Date();
        if (isVolume) {
            templateContent += "volume.name=" + dateFormat.format(date) + System.getProperty("line.separator");
        } else {
            templateContent += "snapshot.name=" + dateFormat.format(date) + System.getProperty("line.separator");
        }
        FileOutputStream templFo = new FileOutputStream(templateProp);
        templFo.write(templateContent.getBytes());
        templFo.flush();
        templFo.close();
        Map<String, Object> params = new HashMap<>();
        params.put(StorageLayer.InstanceConfigKey, storageLayer);
        Processor qcow2Processor = new QCOW2Processor();
        qcow2Processor.configure("QCOW2 Processor", params);
        FormatInfo info = qcow2Processor.process(tmpltPath, null, templateName);
        TemplateLocation loc = new TemplateLocation(storageLayer, tmpltPath);
        loc.create(1, true, templateName);
        loc.addFormat(info);
        loc.save();
        storagePoolMgr.disconnectPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), path);
        TemplateObjectTO newTemplate = new TemplateObjectTO();
        newTemplate.setPath(templateFolder + File.separator + templateName + ".qcow2");
        newTemplate.setSize(info.virtualSize);
        newTemplate.setPhysicalSize(info.size);
        newTemplate.setFormat(ImageFormat.QCOW2);
        newTemplate.setName(templateName);
        return new CopyCmdAnswer(newTemplate);
    } catch (Exception ex) {
        if (isVolume) {
            s_logger.debug("Failed to create template from volume: ", ex);
        } else {
            s_logger.debug("Failed to create template from snapshot: ", ex);
        }
        return new CopyCmdAnswer(ex.toString());
    } finally {
        if (secondaryStorage != null) {
            secondaryStorage.delete();
        }
    }
}
Also used : SnapshotObjectTO(org.apache.cloudstack.storage.to.SnapshotObjectTO) QCOW2Processor(com.cloud.storage.template.QCOW2Processor) StorageProcessor(com.cloud.storage.resource.StorageProcessor) Processor(com.cloud.storage.template.Processor) HashMap(java.util.HashMap) DataTO(com.cloud.agent.api.to.DataTO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) TemplateLocation(com.cloud.storage.template.TemplateLocation) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) NfsTO(com.cloud.agent.api.to.NfsTO) Date(java.util.Date) RbdException(com.ceph.rbd.RbdException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) LibvirtException(org.libvirt.LibvirtException) QemuImgException(org.apache.cloudstack.utils.qemu.QemuImgException) FileNotFoundException(java.io.FileNotFoundException) InternalErrorException(com.cloud.exception.InternalErrorException) ConfigurationException(javax.naming.ConfigurationException) RadosException(com.ceph.rados.exceptions.RadosException) QCOW2Processor(com.cloud.storage.template.QCOW2Processor) PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) FileOutputStream(java.io.FileOutputStream) TemplateObjectTO(org.apache.cloudstack.storage.to.TemplateObjectTO) FormatInfo(com.cloud.storage.template.Processor.FormatInfo) QemuImgFile(org.apache.cloudstack.utils.qemu.QemuImgFile) File(java.io.File) S3Utils.putFile(com.cloud.utils.storage.S3.S3Utils.putFile) SimpleDateFormat(java.text.SimpleDateFormat) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer)

Example 4 with QCOW2Processor

use of com.cloud.storage.template.QCOW2Processor in project cloudstack by apache.

the class KVMStorageProcessor method createTemplateFromVolume.

@Override
public Answer createTemplateFromVolume(final CopyCommand cmd) {
    Map<String, String> details = cmd.getOptions();
    if (details != null && details.get(DiskTO.IQN) != null) {
        // use the managed-storage approach
        return createTemplateFromVolumeOrSnapshot(cmd);
    }
    final DataTO srcData = cmd.getSrcTO();
    final DataTO destData = cmd.getDestTO();
    final int wait = cmd.getWaitInMillSeconds();
    final TemplateObjectTO template = (TemplateObjectTO) destData;
    final DataStoreTO imageStore = template.getDataStore();
    final VolumeObjectTO volume = (VolumeObjectTO) srcData;
    final PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO) volume.getDataStore();
    if (!(imageStore instanceof NfsTO)) {
        return new CopyCmdAnswer("unsupported protocol");
    }
    final NfsTO nfsImageStore = (NfsTO) imageStore;
    KVMStoragePool secondaryStorage = null;
    KVMStoragePool primary;
    try {
        final String templateFolder = template.getPath();
        secondaryStorage = storagePoolMgr.getStoragePoolByURI(nfsImageStore.getUrl());
        primary = storagePoolMgr.getStoragePool(primaryStore.getPoolType(), primaryStore.getUuid());
        final KVMPhysicalDisk disk = storagePoolMgr.getPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), volume.getPath());
        final String tmpltPath = secondaryStorage.getLocalPath() + File.separator + templateFolder;
        storageLayer.mkdirs(tmpltPath);
        final String templateName = UUID.randomUUID().toString();
        if (primary.getType() != StoragePoolType.RBD) {
            final Script command = new Script(_createTmplPath, wait, s_logger);
            command.add("-f", disk.getPath());
            command.add("-t", tmpltPath);
            command.add(NAME_OPTION, templateName + ".qcow2");
            final String result = command.execute();
            if (result != null) {
                s_logger.debug("failed to create template: " + result);
                return new CopyCmdAnswer(result);
            }
        } else {
            s_logger.debug("Converting RBD disk " + disk.getPath() + " into template " + templateName);
            final QemuImgFile srcFile = new QemuImgFile(KVMPhysicalDisk.RBDStringBuilder(primary.getSourceHost(), primary.getSourcePort(), primary.getAuthUserName(), primary.getAuthSecret(), disk.getPath()));
            srcFile.setFormat(PhysicalDiskFormat.RAW);
            final QemuImgFile destFile = new QemuImgFile(tmpltPath + "/" + templateName + ".qcow2");
            destFile.setFormat(PhysicalDiskFormat.QCOW2);
            final QemuImg q = new QemuImg(cmd.getWaitInMillSeconds());
            try {
                q.convert(srcFile, destFile);
            } catch (final QemuImgException | LibvirtException e) {
                final String message = "Failed to create new template while converting " + srcFile.getFileName() + " to " + destFile.getFileName() + " the error was: " + e.getMessage();
                throw new QemuImgException(message);
            }
            final File templateProp = new File(tmpltPath + "/template.properties");
            if (!templateProp.exists()) {
                templateProp.createNewFile();
            }
            String templateContent = "filename=" + templateName + ".qcow2" + System.getProperty("line.separator");
            final DateFormat dateFormat = new SimpleDateFormat("MM_dd_yyyy");
            final Date date = new Date();
            templateContent += "snapshot.name=" + dateFormat.format(date) + System.getProperty("line.separator");
            try (FileOutputStream templFo = new FileOutputStream(templateProp)) {
                templFo.write(templateContent.getBytes());
                templFo.flush();
            } catch (final IOException e) {
                throw e;
            }
        }
        final Map<String, Object> params = new HashMap<String, Object>();
        params.put(StorageLayer.InstanceConfigKey, storageLayer);
        final Processor qcow2Processor = new QCOW2Processor();
        qcow2Processor.configure("QCOW2 Processor", params);
        final FormatInfo info = qcow2Processor.process(tmpltPath, null, templateName);
        final TemplateLocation loc = new TemplateLocation(storageLayer, tmpltPath);
        loc.create(1, true, templateName);
        loc.addFormat(info);
        loc.save();
        final TemplateObjectTO newTemplate = new TemplateObjectTO();
        newTemplate.setPath(templateFolder + File.separator + templateName + ".qcow2");
        newTemplate.setSize(info.virtualSize);
        newTemplate.setPhysicalSize(info.size);
        newTemplate.setFormat(ImageFormat.QCOW2);
        newTemplate.setName(templateName);
        return new CopyCmdAnswer(newTemplate);
    } catch (final QemuImgException e) {
        s_logger.error(e.getMessage());
        return new CopyCmdAnswer(e.toString());
    } catch (final IOException e) {
        s_logger.debug("Failed to createTemplateFromVolume: ", e);
        return new CopyCmdAnswer(e.toString());
    } catch (final Exception e) {
        s_logger.debug("Failed to createTemplateFromVolume: ", e);
        return new CopyCmdAnswer(e.toString());
    } finally {
        if (secondaryStorage != null) {
            secondaryStorage.delete();
        }
    }
}
Also used : LibvirtException(org.libvirt.LibvirtException) QCOW2Processor(com.cloud.storage.template.QCOW2Processor) StorageProcessor(com.cloud.storage.resource.StorageProcessor) Processor(com.cloud.storage.template.Processor) HashMap(java.util.HashMap) DataTO(com.cloud.agent.api.to.DataTO) TemplateLocation(com.cloud.storage.template.TemplateLocation) QemuImgException(org.apache.cloudstack.utils.qemu.QemuImgException) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) Script(com.cloud.utils.script.Script) PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) IOException(java.io.IOException) NfsTO(com.cloud.agent.api.to.NfsTO) Date(java.util.Date) RbdException(com.ceph.rbd.RbdException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) LibvirtException(org.libvirt.LibvirtException) QemuImgException(org.apache.cloudstack.utils.qemu.QemuImgException) FileNotFoundException(java.io.FileNotFoundException) InternalErrorException(com.cloud.exception.InternalErrorException) ConfigurationException(javax.naming.ConfigurationException) RadosException(com.ceph.rados.exceptions.RadosException) QemuImg(org.apache.cloudstack.utils.qemu.QemuImg) QCOW2Processor(com.cloud.storage.template.QCOW2Processor) PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) QemuImgFile(org.apache.cloudstack.utils.qemu.QemuImgFile) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) FileOutputStream(java.io.FileOutputStream) TemplateObjectTO(org.apache.cloudstack.storage.to.TemplateObjectTO) FormatInfo(com.cloud.storage.template.Processor.FormatInfo) QemuImgFile(org.apache.cloudstack.utils.qemu.QemuImgFile) File(java.io.File) S3Utils.putFile(com.cloud.utils.storage.S3.S3Utils.putFile) SimpleDateFormat(java.text.SimpleDateFormat) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer)

Example 5 with QCOW2Processor

use of com.cloud.storage.template.QCOW2Processor in project cloudstack by apache.

the class LibvirtUtilitiesHelper method buildQCOW2Processor.

public Processor buildQCOW2Processor(final StorageLayer storage) throws ConfigurationException {
    final Map<String, Object> params = new HashMap<String, Object>();
    params.put(StorageLayer.InstanceConfigKey, storage);
    final Processor qcow2Processor = new QCOW2Processor();
    qcow2Processor.configure("QCOW2 Processor", params);
    return qcow2Processor;
}
Also used : QCOW2Processor(com.cloud.storage.template.QCOW2Processor) Processor(com.cloud.storage.template.Processor) QCOW2Processor(com.cloud.storage.template.QCOW2Processor) HashMap(java.util.HashMap)

Aggregations

Processor (com.cloud.storage.template.Processor)12 QCOW2Processor (com.cloud.storage.template.QCOW2Processor)12 HashMap (java.util.HashMap)11 ConfigurationException (javax.naming.ConfigurationException)10 InternalErrorException (com.cloud.exception.InternalErrorException)9 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)9 IOException (java.io.IOException)9 FormatInfo (com.cloud.storage.template.Processor.FormatInfo)7 TemplateLocation (com.cloud.storage.template.TemplateLocation)7 Script (com.cloud.utils.script.Script)6 File (java.io.File)5 FileOutputStream (java.io.FileOutputStream)5 DateFormat (java.text.DateFormat)5 SimpleDateFormat (java.text.SimpleDateFormat)5 Date (java.util.Date)5 LibvirtException (org.libvirt.LibvirtException)5 CreatePrivateTemplateAnswer (com.cloud.agent.api.storage.CreatePrivateTemplateAnswer)4 DataStoreTO (com.cloud.agent.api.to.DataStoreTO)3 DataTO (com.cloud.agent.api.to.DataTO)3 NfsTO (com.cloud.agent.api.to.NfsTO)3