Search in sources :

Example 6 with VolumeObjectTO

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

the class VMSnapshotHelperImpl method getVolumeTOList.

@Override
public List<VolumeObjectTO> getVolumeTOList(final Long vmId) {
    final List<VolumeObjectTO> volumeTOs = new ArrayList<>();
    final List<VolumeVO> volumeVOs = volumeDao.findByInstance(vmId);
    for (final VolumeVO volume : volumeVOs) {
        final VolumeInfo volumeInfo = volumeDataFactory.getVolume(volume.getId());
        volumeTOs.add((VolumeObjectTO) volumeInfo.getTO());
    }
    return volumeTOs;
}
Also used : VolumeVO(com.cloud.storage.VolumeVO) ArrayList(java.util.ArrayList) VolumeObjectTO(com.cloud.legacymodel.to.VolumeObjectTO) VolumeInfo(com.cloud.engine.subsystem.api.storage.VolumeInfo)

Example 7 with VolumeObjectTO

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

the class CitrixResourceBase method mount.

protected VDI mount(final Connection conn, final String vmName, final DiskTO volume) throws XmlRpcException, XenAPIException {
    final DataTO data = volume.getData();
    final VolumeType type = volume.getType();
    if (type == VolumeType.ISO) {
        final TemplateObjectTO iso = (TemplateObjectTO) data;
        final DataStoreTO store = iso.getDataStore();
        if (store == null) {
            // It's a fake iso
            return null;
        }
        // corer case, xenserver pv driver iso
        final String templateName = iso.getName();
        if (templateName.startsWith("xs-tools")) {
            try {
                final Set<VDI> vdis = VDI.getByNameLabel(conn, templateName);
                if (vdis.isEmpty()) {
                    throw new CloudRuntimeException("Could not find ISO with URL: " + templateName);
                }
                return vdis.iterator().next();
            } catch (final XenAPIException e) {
                throw new CloudRuntimeException("Unable to get pv iso: " + templateName + " due to " + e.toString());
            } catch (final Exception e) {
                throw new CloudRuntimeException("Unable to get pv iso: " + templateName + " due to " + e.toString());
            }
        }
        if (!(store instanceof NfsTO)) {
            throw new CloudRuntimeException("only support mount iso on nfs");
        }
        final NfsTO nfsStore = (NfsTO) store;
        final String isoPath = nfsStore.getUrl() + File.separator + iso.getPath();
        final int index = isoPath.lastIndexOf("/");
        final String mountpoint = isoPath.substring(0, index);
        final URI uri;
        try {
            uri = new URI(mountpoint);
        } catch (final URISyntaxException e) {
            throw new CloudRuntimeException("Incorrect uri " + mountpoint, e);
        }
        final SR isoSr = createIsoSRbyURI(conn, uri, vmName, false);
        final String isoname = isoPath.substring(index + 1);
        final VDI isoVdi = getVDIbyLocationandSR(conn, isoname, isoSr);
        if (isoVdi == null) {
            throw new CloudRuntimeException("Unable to find ISO " + isoPath);
        }
        return isoVdi;
    } else {
        final VolumeObjectTO vol = (VolumeObjectTO) data;
        return VDI.getByUuid(conn, vol.getPath());
    }
}
Also used : DataStoreTO(com.cloud.legacymodel.to.DataStoreTO) XenAPIException(com.xensource.xenapi.Types.XenAPIException) URISyntaxException(java.net.URISyntaxException) NfsTO(com.cloud.legacymodel.to.NfsTO) URI(java.net.URI) XenAPIException(com.xensource.xenapi.Types.XenAPIException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) TimeoutException(java.util.concurrent.TimeoutException) SAXException(org.xml.sax.SAXException) ConfigurationException(javax.naming.ConfigurationException) MalformedURLException(java.net.MalformedURLException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) DataTO(com.cloud.legacymodel.to.DataTO) VolumeType(com.cloud.model.enumeration.VolumeType) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) VolumeObjectTO(com.cloud.legacymodel.to.VolumeObjectTO) VDI(com.xensource.xenapi.VDI) TemplateObjectTO(com.cloud.legacymodel.to.TemplateObjectTO) SR(com.xensource.xenapi.SR)

Example 8 with VolumeObjectTO

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

the class KvmStorageProcessor method copyVolumeFromPrimaryToSecondary.

@Override
public Answer copyVolumeFromPrimaryToSecondary(final CopyCommand cmd) {
    final DataTO srcData = cmd.getSrcTO();
    final DataTO destData = cmd.getDestTO();
    final VolumeObjectTO srcVol = (VolumeObjectTO) srcData;
    final VolumeObjectTO destVol = (VolumeObjectTO) destData;
    final ImageFormat srcFormat = srcVol.getFormat();
    final ImageFormat destFormat = destVol.getFormat();
    final DataStoreTO srcStore = srcData.getDataStore();
    final DataStoreTO destStore = destData.getDataStore();
    final PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO) srcStore;
    if (!(destStore instanceof NfsTO)) {
        return new CopyCmdAnswer("can only handle nfs storage");
    }
    final NfsTO nfsStore = (NfsTO) destStore;
    final String srcVolumePath = srcData.getPath();
    final String destVolumePath = destData.getPath();
    final String secondaryStorageUrl = nfsStore.getUrl();
    KvmStoragePool secondaryStoragePool = null;
    try {
        final String volumeName = UUID.randomUUID().toString();
        final String destVolumeName = volumeName + "." + destFormat.toString().toLowerCase();
        final KvmPhysicalDisk volume = this.storagePoolMgr.getPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), srcVolumePath);
        volume.setFormat(PhysicalDiskFormat.valueOf(srcFormat.toString()));
        secondaryStoragePool = this.storagePoolMgr.getStoragePoolByUri(secondaryStorageUrl);
        secondaryStoragePool.createFolder(destVolumePath);
        this.storagePoolMgr.deleteStoragePool(secondaryStoragePool.getType(), secondaryStoragePool.getUuid());
        secondaryStoragePool = this.storagePoolMgr.getStoragePoolByUri(secondaryStorageUrl + File.separator + destVolumePath);
        this.storagePoolMgr.copyPhysicalDisk(volume, destVolumeName, secondaryStoragePool, cmd.getWaitInMillSeconds());
        final VolumeObjectTO newVol = new VolumeObjectTO();
        newVol.setPath(destVolumePath + File.separator + destVolumeName);
        newVol.setFormat(destFormat);
        return new CopyCmdAnswer(newVol);
    } catch (final CloudRuntimeException e) {
        this.logger.debug("Failed to copyVolumeFromPrimaryToSecondary: ", 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 9 with VolumeObjectTO

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

the class KvmStorageProcessor method createTemplateFromVolume.

@Override
public Answer createTemplateFromVolume(final CopyCommand 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;
    final KvmStoragePool primary;
    try {
        final String templateFolder = template.getPath();
        secondaryStorage = this.storagePoolMgr.getStoragePoolByUri(nfsImageStore.getUrl());
        primary = this.storagePoolMgr.getStoragePool(primaryStore.getPoolType(), primaryStore.getUuid());
        final KvmPhysicalDisk disk = this.storagePoolMgr.getPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), volume.getPath());
        final String tmpltPath = secondaryStorage.getLocalPath() + File.separator + templateFolder;
        this.storageLayer.mkdirs(tmpltPath);
        final String templateName = UUID.randomUUID().toString();
        if (primary.getType() != StoragePoolType.RBD) {
            final Script command = new Script(this.createTmplPath, wait, this.logger);
            command.add("-f", disk.getPath());
            command.add("-t", tmpltPath);
            command.add("-n", templateName + ".qcow2");
            final String result = command.execute();
            if (result != null) {
                this.logger.debug("failed to create template: " + result);
                return new CopyCmdAnswer(result);
            }
        } else {
            this.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 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 (final FileOutputStream templFo = new FileOutputStream(templateProp)) {
                templFo.write(templateContent.getBytes());
                templFo.flush();
            } catch (final IOException e) {
                throw e;
            }
        }
        final Map<String, Object> params = new HashMap<>();
        params.put(StorageLayer.InstanceConfigKey, this.storageLayer);
        final Processor qcow2Processor = new QCOW2Processor();
        qcow2Processor.configure("QCOW2 Processor", params);
        final TemplateFormatInfo info = qcow2Processor.process(tmpltPath, null, templateName);
        final TemplateLocation loc = new TemplateLocation(this.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) {
        this.logger.error(e.getMessage());
        return new CopyCmdAnswer(e.toString());
    } catch (final Exception e) {
        this.logger.debug("Failed to createTemplateFromVolume: ", e);
        return new CopyCmdAnswer(e.toString());
    } finally {
        if (secondaryStorage != null) {
            secondaryStorage.delete();
        }
    }
}
Also used : QCOW2Processor(com.cloud.common.storageprocessor.QCOW2Processor) StorageProcessor(com.cloud.common.storageprocessor.resource.StorageProcessor) Processor(com.cloud.common.storageprocessor.Processor) HashMap(java.util.HashMap) DataTO(com.cloud.legacymodel.to.DataTO) TemplateLocation(com.cloud.common.storageprocessor.TemplateLocation) QemuImgException(com.cloud.agent.resource.kvm.storage.utils.QemuImgException) VolumeObjectTO(com.cloud.legacymodel.to.VolumeObjectTO) Script(com.cloud.utils.script.Script) PrimaryDataStoreTO(com.cloud.legacymodel.to.PrimaryDataStoreTO) DataStoreTO(com.cloud.legacymodel.to.DataStoreTO) IOException(java.io.IOException) NfsTO(com.cloud.legacymodel.to.NfsTO) Date(java.util.Date) 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) QemuImg(com.cloud.agent.resource.kvm.storage.utils.QemuImg) QCOW2Processor(com.cloud.common.storageprocessor.QCOW2Processor) PrimaryDataStoreTO(com.cloud.legacymodel.to.PrimaryDataStoreTO) QemuImgFile(com.cloud.agent.resource.kvm.storage.utils.QemuImgFile) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat) FileOutputStream(java.io.FileOutputStream) TemplateFormatInfo(com.cloud.legacymodel.storage.TemplateFormatInfo) TemplateObjectTO(com.cloud.legacymodel.to.TemplateObjectTO) File(java.io.File) QemuImgFile(com.cloud.agent.resource.kvm.storage.utils.QemuImgFile) SimpleDateFormat(java.text.SimpleDateFormat) CopyCmdAnswer(com.cloud.legacymodel.communication.answer.CopyCmdAnswer)

Example 10 with VolumeObjectTO

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

the class KvmStorageProcessor method dettachVolume.

@Override
public Answer dettachVolume(final DettachCommand 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 = this.resource.diskUuidToSerial(vol.getUuid());
    try {
        final Connect conn = LibvirtConnection.getConnectionByVmName(vmName);
        final KvmPhysicalDisk phyDisk = this.storagePoolMgr.getPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), vol.getPath());
        attachOrDetachDisk(conn, false, vmName, phyDisk, disk.getDiskSeq().intValue(), disk.getDiskController(), disk.getDiskFormat(), serial, vol);
        this.storagePoolMgr.disconnectPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), vol.getPath());
        return new DettachAnswer(disk);
    } catch (final LibvirtException | InternalErrorException e) {
        this.logger.debug("Failed to attach volume: " + vol.getPath() + ", due to ", e);
        return new DettachAnswer(e.toString());
    }
}
Also used : LibvirtException(org.libvirt.LibvirtException) PrimaryDataStoreTO(com.cloud.legacymodel.to.PrimaryDataStoreTO) DettachAnswer(com.cloud.legacymodel.communication.answer.DettachAnswer) Connect(org.libvirt.Connect) VolumeObjectTO(com.cloud.legacymodel.to.VolumeObjectTO) InternalErrorException(com.cloud.legacymodel.exceptions.InternalErrorException) DiskTO(com.cloud.legacymodel.to.DiskTO)

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