Search in sources :

Example 46 with PrimaryDataStoreTO

use of org.apache.cloudstack.storage.to.PrimaryDataStoreTO in project cloudstack by apache.

the class LibvirtRevertSnapshotCommandWrapper method execute.

@Override
public Answer execute(final RevertSnapshotCommand command, final LibvirtComputingResource libvirtComputingResource) {
    SnapshotObjectTO snapshot = command.getData();
    VolumeObjectTO volume = snapshot.getVolume();
    PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO) volume.getDataStore();
    DataStoreTO snapshotImageStore = snapshot.getDataStore();
    if (!(snapshotImageStore instanceof NfsTO) && primaryStore.getPoolType() != StoragePoolType.RBD) {
        return new Answer(command, false, String.format("Revert snapshot does not support storage pool of type [%s]. Revert snapshot is supported by storage pools of type 'NFS' or 'RBD'", primaryStore.getPoolType()));
    }
    String volumePath = volume.getPath();
    String snapshotPath = null;
    String snapshotRelPath = null;
    KVMStoragePool secondaryStoragePool = null;
    try {
        snapshotRelPath = snapshot.getPath();
        KVMStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr();
        KVMPhysicalDisk snapshotDisk = storagePoolMgr.getPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), volumePath);
        KVMStoragePool primaryPool = snapshotDisk.getPool();
        if (primaryPool.getType() == StoragePoolType.RBD) {
            Rados rados = new Rados(primaryPool.getAuthUserName());
            rados.confSet(MON_HOST, primaryPool.getSourceHost() + ":" + primaryPool.getSourcePort());
            rados.confSet(KEY, primaryPool.getAuthSecret());
            rados.confSet(CLIENT_MOUNT_TIMEOUT, RADOS_CONNECTION_TIMEOUT);
            rados.connect();
            String[] rbdPoolAndVolumeAndSnapshot = snapshotRelPath.split("/");
            int snapshotIndex = rbdPoolAndVolumeAndSnapshot.length - 1;
            String rbdSnapshotId = rbdPoolAndVolumeAndSnapshot[snapshotIndex];
            IoCTX io = rados.ioCtxCreate(primaryPool.getSourceDir());
            Rbd rbd = new Rbd(io);
            s_logger.debug(String.format("Attempting to rollback RBD snapshot [name:%s], [volumeid:%s], [snapshotid:%s]", snapshot.getName(), volumePath, rbdSnapshotId));
            RbdImage image = rbd.open(volumePath);
            image.snapRollBack(rbdSnapshotId);
            rbd.close(image);
            rados.ioCtxDestroy(io);
        } else {
            NfsTO nfsImageStore = (NfsTO) snapshotImageStore;
            String secondaryStoragePoolUrl = nfsImageStore.getUrl();
            secondaryStoragePool = storagePoolMgr.getStoragePoolByURI(secondaryStoragePoolUrl);
            String ssPmountPath = secondaryStoragePool.getLocalPath();
            snapshotPath = ssPmountPath + File.separator + snapshotRelPath;
            Script cmd = new Script(libvirtComputingResource.manageSnapshotPath(), libvirtComputingResource.getCmdsTimeout(), s_logger);
            cmd.add("-v", snapshotPath);
            cmd.add("-n", snapshotDisk.getName());
            cmd.add("-p", snapshotDisk.getPath());
            String result = cmd.execute();
            if (result != null) {
                s_logger.debug("Failed to revert snaptshot: " + result);
                return new Answer(command, false, result);
            }
        }
        return new Answer(command, true, "RevertSnapshotCommand executes successfully");
    } catch (CloudRuntimeException e) {
        return new Answer(command, false, e.toString());
    } catch (RadosException e) {
        s_logger.error("Failed to connect to Rados pool while trying to revert snapshot. Exception: ", e);
        return new Answer(command, false, e.toString());
    } catch (RbdException e) {
        s_logger.error("Failed to connect to revert snapshot due to RBD exception: ", e);
        return new Answer(command, false, e.toString());
    }
}
Also used : SnapshotObjectTO(org.apache.cloudstack.storage.to.SnapshotObjectTO) Script(com.cloud.utils.script.Script) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) KVMStoragePoolManager(com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager) KVMPhysicalDisk(com.cloud.hypervisor.kvm.storage.KVMPhysicalDisk) Rados(com.ceph.rados.Rados) RadosException(com.ceph.rados.exceptions.RadosException) NfsTO(com.cloud.agent.api.to.NfsTO) Answer(com.cloud.agent.api.Answer) KVMStoragePool(com.cloud.hypervisor.kvm.storage.KVMStoragePool) PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) Rbd(com.ceph.rbd.Rbd) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) RbdImage(com.ceph.rbd.RbdImage) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) IoCTX(com.ceph.rados.IoCTX) RbdException(com.ceph.rbd.RbdException)

Example 47 with PrimaryDataStoreTO

use of org.apache.cloudstack.storage.to.PrimaryDataStoreTO in project cloudstack by apache.

the class KVMStorageProcessor method copyTemplateToPrimaryStorage.

@Override
public Answer copyTemplateToPrimaryStorage(final CopyCommand cmd) {
    final DataTO srcData = cmd.getSrcTO();
    final DataTO destData = cmd.getDestTO();
    final TemplateObjectTO template = (TemplateObjectTO) srcData;
    final DataStoreTO imageStore = template.getDataStore();
    final PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO) destData.getDataStore();
    if (!(imageStore instanceof NfsTO)) {
        return new CopyCmdAnswer("unsupported protocol");
    }
    final NfsTO nfsImageStore = (NfsTO) imageStore;
    final String tmplturl = nfsImageStore.getUrl() + File.separator + template.getPath();
    final int index = tmplturl.lastIndexOf("/");
    final String mountpoint = tmplturl.substring(0, index);
    String tmpltname = null;
    if (index < tmplturl.length() - 1) {
        tmpltname = tmplturl.substring(index + 1);
    }
    KVMPhysicalDisk tmplVol = null;
    KVMStoragePool secondaryPool = null;
    try {
        secondaryPool = storagePoolMgr.getStoragePoolByURI(mountpoint);
        /* Get template vol */
        if (tmpltname == null) {
            secondaryPool.refresh();
            final List<KVMPhysicalDisk> disks = secondaryPool.listPhysicalDisks();
            if (disks == null || disks.isEmpty()) {
                return new PrimaryStorageDownloadAnswer("Failed to get volumes from pool: " + secondaryPool.getUuid());
            }
            for (final KVMPhysicalDisk disk : disks) {
                if (disk.getName().endsWith("qcow2")) {
                    tmplVol = disk;
                    break;
                }
            }
        } else {
            tmplVol = secondaryPool.getPhysicalDisk(tmpltname);
        }
        if (tmplVol == null) {
            return new PrimaryStorageDownloadAnswer("Failed to get template from pool: " + secondaryPool.getUuid());
        }
        /* Copy volume to primary storage */
        s_logger.debug("Copying template to primary storage, template format is " + tmplVol.getFormat());
        final KVMStoragePool primaryPool = storagePoolMgr.getStoragePool(primaryStore.getPoolType(), primaryStore.getUuid());
        KVMPhysicalDisk primaryVol = null;
        if (destData instanceof VolumeObjectTO) {
            final VolumeObjectTO volume = (VolumeObjectTO) destData;
            // pass along volume's target size if it's bigger than template's size, for storage types that copy template rather than cloning on deploy
            if (volume.getSize() != null && volume.getSize() > tmplVol.getVirtualSize()) {
                s_logger.debug("Using configured size of " + toHumanReadableSize(volume.getSize()));
                tmplVol.setSize(volume.getSize());
                tmplVol.setVirtualSize(volume.getSize());
            } else {
                s_logger.debug("Using template's size of " + toHumanReadableSize(tmplVol.getVirtualSize()));
            }
            primaryVol = storagePoolMgr.copyPhysicalDisk(tmplVol, volume.getUuid(), primaryPool, cmd.getWaitInMillSeconds());
        } else if (destData instanceof TemplateObjectTO) {
            TemplateObjectTO destTempl = (TemplateObjectTO) destData;
            Map<String, String> details = primaryStore.getDetails();
            String path = details != null ? details.get("managedStoreTarget") : null;
            if (!storagePoolMgr.connectPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), path, details)) {
                s_logger.warn("Failed to connect physical disk at path: " + path + ", in storage pool id: " + primaryStore.getUuid());
            }
            primaryVol = storagePoolMgr.copyPhysicalDisk(tmplVol, path != null ? path : destTempl.getUuid(), primaryPool, cmd.getWaitInMillSeconds());
            if (!storagePoolMgr.disconnectPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), path)) {
                s_logger.warn("Failed to disconnect physical disk at path: " + path + ", in storage pool id: " + primaryStore.getUuid());
            }
        } else {
            primaryVol = storagePoolMgr.copyPhysicalDisk(tmplVol, UUID.randomUUID().toString(), primaryPool, cmd.getWaitInMillSeconds());
        }
        DataTO data = null;
        /**
         * Force the ImageFormat for RBD templates to RAW
         */
        if (destData.getObjectType() == DataObjectType.TEMPLATE) {
            final TemplateObjectTO newTemplate = new TemplateObjectTO();
            newTemplate.setPath(primaryVol.getName());
            newTemplate.setSize(primaryVol.getSize());
            if (primaryPool.getType() == StoragePoolType.RBD || primaryPool.getType() == StoragePoolType.PowerFlex || primaryPool.getType() == StoragePoolType.Linstor) {
                newTemplate.setFormat(ImageFormat.RAW);
            } else {
                newTemplate.setFormat(ImageFormat.QCOW2);
            }
            data = newTemplate;
        } else if (destData.getObjectType() == DataObjectType.VOLUME) {
            final VolumeObjectTO volumeObjectTO = new VolumeObjectTO();
            volumeObjectTO.setPath(primaryVol.getName());
            volumeObjectTO.setSize(primaryVol.getSize());
            if (primaryVol.getFormat() == PhysicalDiskFormat.RAW) {
                volumeObjectTO.setFormat(ImageFormat.RAW);
            } else if (primaryVol.getFormat() == PhysicalDiskFormat.QCOW2) {
                volumeObjectTO.setFormat(ImageFormat.QCOW2);
            }
            data = volumeObjectTO;
        }
        return new CopyCmdAnswer(data);
    } catch (final CloudRuntimeException e) {
        return new CopyCmdAnswer(e.toString());
    } finally {
        try {
            if (secondaryPool != null) {
                secondaryPool.delete();
            }
        } catch (final Exception e) {
            s_logger.debug("Failed to clean up secondary storage", e);
        }
    }
}
Also used : PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) NfsTO(com.cloud.agent.api.to.NfsTO) 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) PrimaryStorageDownloadAnswer(com.cloud.agent.api.storage.PrimaryStorageDownloadAnswer) DataTO(com.cloud.agent.api.to.DataTO) PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) TemplateObjectTO(org.apache.cloudstack.storage.to.TemplateObjectTO) Map(java.util.Map) HashMap(java.util.HashMap) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer)

Aggregations

PrimaryDataStoreTO (org.apache.cloudstack.storage.to.PrimaryDataStoreTO)47 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)35 DataStoreTO (com.cloud.agent.api.to.DataStoreTO)28 VolumeObjectTO (org.apache.cloudstack.storage.to.VolumeObjectTO)28 InternalErrorException (com.cloud.exception.InternalErrorException)24 CopyCmdAnswer (org.apache.cloudstack.storage.command.CopyCmdAnswer)24 NfsTO (com.cloud.agent.api.to.NfsTO)23 DataTO (com.cloud.agent.api.to.DataTO)22 TemplateObjectTO (org.apache.cloudstack.storage.to.TemplateObjectTO)15 SnapshotObjectTO (org.apache.cloudstack.storage.to.SnapshotObjectTO)13 IOException (java.io.IOException)12 LibvirtException (org.libvirt.LibvirtException)12 Connection (com.xensource.xenapi.Connection)11 SR (com.xensource.xenapi.SR)11 XenAPIException (com.xensource.xenapi.Types.XenAPIException)11 XmlRpcException (org.apache.xmlrpc.XmlRpcException)11 RadosException (com.ceph.rados.exceptions.RadosException)10 RbdException (com.ceph.rbd.RbdException)10 URI (java.net.URI)10 ConfigurationException (javax.naming.ConfigurationException)10