Search in sources :

Example 11 with VolumeObjectTO

use of com.cloud.legacymodel.to.VolumeObjectTO in project cosmic by MissionCriticalCloud.

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 = this.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 */
        this.logger.debug("Copying template to primary storage, template format is " + tmplVol.getFormat());
        final KvmStoragePool primaryPool = this.storagePoolMgr.getStoragePool(primaryStore.getPoolType(), primaryStore.getUuid());
        final KvmPhysicalDisk primaryVol;
        if (destData instanceof VolumeObjectTO) {
            final VolumeObjectTO volume = (VolumeObjectTO) destData;
            // rather than cloning on deploy
            if (volume.getSize() != null && volume.getSize() > tmplVol.getVirtualSize()) {
                this.logger.debug("Using configured size of " + volume.getSize());
                tmplVol.setSize(volume.getSize());
                tmplVol.setVirtualSize(volume.getSize());
            } else {
                this.logger.debug("Using template's size of " + tmplVol.getVirtualSize());
            }
            primaryVol = this.storagePoolMgr.copyPhysicalDisk(tmplVol, volume.getUuid(), primaryPool, cmd.getWaitInMillSeconds());
        } else if (destData instanceof TemplateObjectTO) {
            final TemplateObjectTO destTempl = (TemplateObjectTO) destData;
            primaryVol = this.storagePoolMgr.copyPhysicalDisk(tmplVol, destTempl.getUuid(), primaryPool, cmd.getWaitInMillSeconds());
        } else {
            primaryVol = this.storagePoolMgr.copyPhysicalDisk(tmplVol, UUID.randomUUID().toString(), primaryPool, cmd.getWaitInMillSeconds());
        }
        DataTO data = null;
        if (destData.getObjectType() == DataObjectType.TEMPLATE) {
            final TemplateObjectTO newTemplate = new TemplateObjectTO();
            newTemplate.setPath(primaryVol.getName());
            newTemplate.setSize(primaryVol.getSize());
            if (primaryPool.getType() == StoragePoolType.RBD) {
                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) {
            this.logger.debug("Failed to clean up secondary storage", e);
        }
    }
}
Also used : PrimaryDataStoreTO(com.cloud.legacymodel.to.PrimaryDataStoreTO) DataStoreTO(com.cloud.legacymodel.to.DataStoreTO) NfsTO(com.cloud.legacymodel.to.NfsTO) 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) PrimaryStorageDownloadAnswer(com.cloud.legacymodel.communication.answer.PrimaryStorageDownloadAnswer) DataTO(com.cloud.legacymodel.to.DataTO) PrimaryDataStoreTO(com.cloud.legacymodel.to.PrimaryDataStoreTO) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) VolumeObjectTO(com.cloud.legacymodel.to.VolumeObjectTO) TemplateObjectTO(com.cloud.legacymodel.to.TemplateObjectTO) CopyCmdAnswer(com.cloud.legacymodel.communication.answer.CopyCmdAnswer)

Example 12 with VolumeObjectTO

use of com.cloud.legacymodel.to.VolumeObjectTO in project cosmic by MissionCriticalCloud.

the class KvmStorageProcessor method copyVolumeFromImageCacheToPrimary.

@Override
public Answer copyVolumeFromImageCacheToPrimary(final CopyCommand cmd) {
    final DataTO srcData = cmd.getSrcTO();
    final DataTO destData = cmd.getDestTO();
    final DataStoreTO srcStore = srcData.getDataStore();
    final DataStoreTO destStore = destData.getDataStore();
    final VolumeObjectTO srcVol = (VolumeObjectTO) srcData;
    final ImageFormat srcFormat = srcVol.getFormat();
    final PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO) destStore;
    if (!(srcStore instanceof NfsTO)) {
        return new CopyCmdAnswer("can only handle nfs storage");
    }
    final NfsTO nfsStore = (NfsTO) srcStore;
    final String srcVolumePath = srcData.getPath();
    final String secondaryStorageUrl = nfsStore.getUrl();
    KvmStoragePool secondaryStoragePool = null;
    KvmStoragePool primaryPool;
    try {
        try {
            primaryPool = this.storagePoolMgr.getStoragePool(primaryStore.getPoolType(), primaryStore.getUuid());
        } catch (final CloudRuntimeException e) {
            if (e.getMessage().contains("not found")) {
                primaryPool = this.storagePoolMgr.createStoragePool(primaryStore.getUuid(), primaryStore.getHost(), primaryStore.getPort(), primaryStore.getPath(), null, primaryStore.getPoolType());
            } else {
                return new CopyCmdAnswer(e.getMessage());
            }
        }
        final String volumeName = UUID.randomUUID().toString();
        final int index = srcVolumePath.lastIndexOf(File.separator);
        final String volumeDir = srcVolumePath.substring(0, index);
        String srcVolumeName = srcVolumePath.substring(index + 1);
        secondaryStoragePool = this.storagePoolMgr.getStoragePoolByUri(secondaryStorageUrl + File.separator + volumeDir);
        if (!srcVolumeName.endsWith(".qcow2") && srcFormat == ImageFormat.QCOW2) {
            srcVolumeName = srcVolumeName + ".qcow2";
        }
        final KvmPhysicalDisk volume = secondaryStoragePool.getPhysicalDisk(srcVolumeName);
        volume.setFormat(PhysicalDiskFormat.valueOf(srcFormat.toString()));
        final KvmPhysicalDisk newDisk = this.storagePoolMgr.copyPhysicalDisk(volume, volumeName, primaryPool, cmd.getWaitInMillSeconds());
        final VolumeObjectTO newVol = new VolumeObjectTO();
        newVol.setFormat(ImageFormat.valueOf(newDisk.getFormat().toString().toUpperCase()));
        newVol.setPath(volumeName);
        return new CopyCmdAnswer(newVol);
    } catch (final CloudRuntimeException e) {
        this.logger.debug("Failed to ccopyVolumeFromImageCacheToPrimary: ", e);
        return new CopyCmdAnswer(e.toString());
    } finally {
        if (secondaryStoragePool != null) {
            this.storagePoolMgr.deleteStoragePool(secondaryStoragePool.getType(), secondaryStoragePool.getUuid());
        }
    }
}
Also used : PrimaryDataStoreTO(com.cloud.legacymodel.to.PrimaryDataStoreTO) DataStoreTO(com.cloud.legacymodel.to.DataStoreTO) DataTO(com.cloud.legacymodel.to.DataTO) PrimaryDataStoreTO(com.cloud.legacymodel.to.PrimaryDataStoreTO) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) VolumeObjectTO(com.cloud.legacymodel.to.VolumeObjectTO) NfsTO(com.cloud.legacymodel.to.NfsTO) CopyCmdAnswer(com.cloud.legacymodel.communication.answer.CopyCmdAnswer) ImageFormat(com.cloud.model.enumeration.ImageFormat)

Example 13 with VolumeObjectTO

use of com.cloud.legacymodel.to.VolumeObjectTO in project cosmic by MissionCriticalCloud.

the class KvmStorageProcessor method attachVolume.

@Override
public Answer attachVolume(final AttachCommand cmd) {
    final DiskTO disk = cmd.getDisk();
    final VolumeObjectTO vol = (VolumeObjectTO) disk.getData();
    final PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO) vol.getDataStore();
    final String vmName = cmd.getVmName();
    final String serial = disk.getDiskSeq() + "-" + this.resource.diskUuidToSerial(vol.getUuid());
    try {
        final Connect conn = LibvirtConnection.getConnectionByVmName(vmName);
        this.storagePoolMgr.connectPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), vol.getPath(), disk.getDetails());
        final KvmPhysicalDisk phyDisk = this.storagePoolMgr.getPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), vol.getPath());
        attachOrDetachDisk(conn, true, vmName, phyDisk, disk.getDiskSeq().intValue(), disk.getDiskController(), disk.getDiskFormat(), serial, vol);
        return new AttachAnswer(disk);
    } catch (final LibvirtException e) {
        this.logger.debug("Failed to attach volume: " + vol.getPath() + ", due to ", e);
        this.storagePoolMgr.disconnectPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), vol.getPath());
        return new AttachAnswer(e.getMessage());
    } catch (final InternalErrorException | CloudRuntimeException e) {
        this.logger.debug("Failed to attach volume: " + vol.getPath() + ", due to ", e);
        return new AttachAnswer(e.getMessage());
    }
}
Also used : LibvirtException(org.libvirt.LibvirtException) PrimaryDataStoreTO(com.cloud.legacymodel.to.PrimaryDataStoreTO) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) Connect(org.libvirt.Connect) VolumeObjectTO(com.cloud.legacymodel.to.VolumeObjectTO) InternalErrorException(com.cloud.legacymodel.exceptions.InternalErrorException) DiskTO(com.cloud.legacymodel.to.DiskTO) AttachAnswer(com.cloud.legacymodel.communication.answer.AttachAnswer)

Example 14 with VolumeObjectTO

use of com.cloud.legacymodel.to.VolumeObjectTO in project cosmic by MissionCriticalCloud.

the class KvmStorageProcessor method cloneVolumeFromBaseTemplate.

@Override
public Answer cloneVolumeFromBaseTemplate(final CopyCommand cmd) {
    final DataTO srcData = cmd.getSrcTO();
    final DataTO destData = cmd.getDestTO();
    final TemplateObjectTO template = (TemplateObjectTO) srcData;
    final DataStoreTO imageStore = template.getDataStore();
    final VolumeObjectTO volume = (VolumeObjectTO) destData;
    final PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO) volume.getDataStore();
    final KvmPhysicalDisk baseVolume;
    final KvmStoragePool primaryPool;
    final KvmPhysicalDisk vol;
    try {
        primaryPool = this.storagePoolMgr.getStoragePool(primaryStore.getPoolType(), primaryStore.getUuid());
        String templatePath = template.getPath();
        if (primaryPool.getType() == StoragePoolType.CLVM) {
            templatePath = imageStore.getUrl() + File.separator + templatePath;
            vol = templateToPrimaryDownload(templatePath, primaryPool, volume.getUuid(), volume.getSize(), cmd.getWaitInMillSeconds());
        } else if (primaryPool.getType() == StoragePoolType.LVM) {
            templatePath = imageStore.getUrl() + File.separator + templatePath;
            vol = templateToPrimaryDownload(templatePath, primaryPool, volume.getUuid(), volume.getSize(), cmd.getWaitInMillSeconds());
        } else {
            if (templatePath.contains("/mnt")) {
                // upgrade issue, if the path contains path, need to extract the volume uuid from path
                templatePath = templatePath.substring(templatePath.lastIndexOf(File.separator) + 1);
            }
            baseVolume = this.storagePoolMgr.getPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), templatePath);
            vol = this.storagePoolMgr.createDiskFromTemplate(baseVolume, volume.getUuid(), volume.getProvisioningType(), baseVolume.getPool(), volume.getSize(), cmd.getWaitInMillSeconds());
        }
        if (vol == null) {
            return new CopyCmdAnswer(" Can't create storage volume on storage pool");
        }
        final VolumeObjectTO newVol = new VolumeObjectTO();
        newVol.setPath(vol.getName());
        newVol.setSize(volume.getSize());
        if (vol.getFormat() == PhysicalDiskFormat.RAW) {
            newVol.setFormat(ImageFormat.RAW);
        } else if (vol.getFormat() == PhysicalDiskFormat.QCOW2) {
            newVol.setFormat(ImageFormat.QCOW2);
        } else if (vol.getFormat() == PhysicalDiskFormat.DIR) {
            newVol.setFormat(ImageFormat.DIR);
        }
        return new CopyCmdAnswer(newVol);
    } catch (final CloudRuntimeException e) {
        this.logger.debug("Failed to create volume: ", e);
        return new CopyCmdAnswer(e.toString());
    }
}
Also used : PrimaryDataStoreTO(com.cloud.legacymodel.to.PrimaryDataStoreTO) DataStoreTO(com.cloud.legacymodel.to.DataStoreTO) DataTO(com.cloud.legacymodel.to.DataTO) PrimaryDataStoreTO(com.cloud.legacymodel.to.PrimaryDataStoreTO) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) VolumeObjectTO(com.cloud.legacymodel.to.VolumeObjectTO) TemplateObjectTO(com.cloud.legacymodel.to.TemplateObjectTO) CopyCmdAnswer(com.cloud.legacymodel.communication.answer.CopyCmdAnswer)

Example 15 with VolumeObjectTO

use of com.cloud.legacymodel.to.VolumeObjectTO 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)

Aggregations

VolumeObjectTO (com.cloud.legacymodel.to.VolumeObjectTO)61 CloudRuntimeException (com.cloud.legacymodel.exceptions.CloudRuntimeException)38 CopyCmdAnswer (com.cloud.legacymodel.communication.answer.CopyCmdAnswer)23 PrimaryDataStoreTO (com.cloud.legacymodel.to.PrimaryDataStoreTO)23 InternalErrorException (com.cloud.legacymodel.exceptions.InternalErrorException)19 Connection (com.xensource.xenapi.Connection)19 DataTO (com.cloud.legacymodel.to.DataTO)17 NfsTO (com.cloud.legacymodel.to.NfsTO)17 VDI (com.xensource.xenapi.VDI)17 DataStoreTO (com.cloud.legacymodel.to.DataStoreTO)16 ArrayList (java.util.ArrayList)14 SR (com.xensource.xenapi.SR)13 XenAPIException (com.xensource.xenapi.Types.XenAPIException)13 XmlRpcException (org.apache.xmlrpc.XmlRpcException)13 LibvirtException (org.libvirt.LibvirtException)12 IOException (java.io.IOException)11 VMSnapshotTO (com.cloud.legacymodel.to.VMSnapshotTO)10 DiskTO (com.cloud.legacymodel.to.DiskTO)9 VolumeVO (com.cloud.storage.VolumeVO)9 URI (java.net.URI)9