Search in sources :

Example 91 with SR

use of com.xensource.xenapi.SR in project cosmic by MissionCriticalCloud.

the class XenServerStorageProcessor method snapshotAndCopy.

// if the source SR needs to be attached to, do so
// take a snapshot of the source VDI (on the source SR)
// create an iSCSI SR based on the new back-end volume
// copy the snapshot to the new SR
// delete the snapshot
// detach the new SR
// if we needed to perform an attach to the source SR, detach from it
@Override
public SnapshotAndCopyAnswer snapshotAndCopy(final SnapshotAndCopyCommand cmd) {
    final Connection conn = hypervisorResource.getConnection();
    try {
        SR sourceSr = null;
        final Map<String, String> sourceDetails = cmd.getSourceDetails();
        if (sourceDetails != null && sourceDetails.keySet().size() > 0) {
            final String iScsiName = sourceDetails.get(DiskTO.IQN);
            final String storageHost = sourceDetails.get(DiskTO.STORAGE_HOST);
            final String chapInitiatorUsername = sourceDetails.get(DiskTO.CHAP_INITIATOR_USERNAME);
            final String chapInitiatorSecret = sourceDetails.get(DiskTO.CHAP_INITIATOR_SECRET);
            sourceSr = hypervisorResource.getIscsiSR(conn, iScsiName, storageHost, iScsiName, chapInitiatorUsername, chapInitiatorSecret, false);
        }
        final VDI vdiToSnapshot = VDI.getByUuid(conn, cmd.getUuidOfSourceVdi());
        final VDI vdiSnapshot = vdiToSnapshot.snapshot(conn, new HashMap<>());
        final Map<String, String> destDetails = cmd.getDestDetails();
        final String iScsiName = destDetails.get(DiskTO.IQN);
        final String storageHost = destDetails.get(DiskTO.STORAGE_HOST);
        final String chapInitiatorUsername = destDetails.get(DiskTO.CHAP_INITIATOR_USERNAME);
        final String chapInitiatorSecret = destDetails.get(DiskTO.CHAP_INITIATOR_SECRET);
        final SR newSr = hypervisorResource.getIscsiSR(conn, iScsiName, storageHost, iScsiName, chapInitiatorUsername, chapInitiatorSecret, false);
        final VDI vdiCopy = vdiSnapshot.copy(conn, newSr);
        final String vdiUuid = vdiCopy.getUuid(conn);
        vdiSnapshot.destroy(conn);
        if (sourceSr != null) {
            hypervisorResource.removeSR(conn, sourceSr);
        }
        hypervisorResource.removeSR(conn, newSr);
        final SnapshotAndCopyAnswer snapshotAndCopyAnswer = new SnapshotAndCopyAnswer();
        snapshotAndCopyAnswer.setPath(vdiUuid);
        return snapshotAndCopyAnswer;
    } catch (final Exception ex) {
        s_logger.warn("Failed to take and copy snapshot: " + ex.toString(), ex);
        return new SnapshotAndCopyAnswer(ex.getMessage());
    }
}
Also used : SnapshotAndCopyAnswer(com.cloud.storage.command.SnapshotAndCopyAnswer) Connection(com.xensource.xenapi.Connection) VDI(com.xensource.xenapi.VDI) XenAPIException(com.xensource.xenapi.Types.XenAPIException) InternalErrorException(com.cloud.exception.InternalErrorException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) SR(com.xensource.xenapi.SR)

Example 92 with SR

use of com.xensource.xenapi.SR in project cosmic by MissionCriticalCloud.

the class XenServerStorageProcessor method createVolumeFromSnapshot.

@Override
public Answer createVolumeFromSnapshot(final CopyCommand cmd) {
    final Connection conn = hypervisorResource.getConnection();
    final DataTO srcData = cmd.getSrcTO();
    final SnapshotObjectTO snapshot = (SnapshotObjectTO) srcData;
    final DataTO destData = cmd.getDestTO();
    final DataStoreTO imageStore = srcData.getDataStore();
    if (srcData.getDataStore() instanceof PrimaryDataStoreTO && destData.getDataStore() instanceof PrimaryDataStoreTO) {
        return createVolumeFromSnapshot2(cmd);
    }
    if (!(imageStore instanceof NfsTO)) {
        return new CopyCmdAnswer("unsupported protocol");
    }
    final NfsTO nfsImageStore = (NfsTO) imageStore;
    final String primaryStorageNameLabel = destData.getDataStore().getUuid();
    final String secondaryStorageUrl = nfsImageStore.getUrl();
    final int wait = cmd.getWait();
    boolean result = false;
    // Generic error message.
    String details = null;
    String volumeUUID = null;
    if (secondaryStorageUrl == null) {
        details += " because the URL passed: " + secondaryStorageUrl + " is invalid.";
        return new CopyCmdAnswer(details);
    }
    try {
        final SR primaryStorageSR = hypervisorResource.getSRByNameLabelandHost(conn, primaryStorageNameLabel);
        if (primaryStorageSR == null) {
            throw new InternalErrorException("Could not create volume from snapshot because the primary Storage SR could not be created from the name label: " + primaryStorageNameLabel);
        }
        // Get the absolute path of the snapshot on the secondary storage.
        String snapshotInstallPath = snapshot.getPath();
        final int index = snapshotInstallPath.lastIndexOf(nfsImageStore.getPathSeparator());
        final String snapshotName = snapshotInstallPath.substring(index + 1);
        if (!snapshotName.startsWith("VHD-") && !snapshotName.endsWith(".vhd")) {
            snapshotInstallPath = snapshotInstallPath + ".vhd";
        }
        final URI snapshotURI = new URI(secondaryStorageUrl + nfsImageStore.getPathSeparator() + snapshotInstallPath);
        final String snapshotPath = snapshotURI.getHost() + ":" + snapshotURI.getPath();
        final String srUuid = primaryStorageSR.getUuid(conn);
        volumeUUID = copy_vhd_from_secondarystorage(conn, snapshotPath, srUuid, wait);
        result = true;
        final VDI volume = VDI.getByUuid(conn, volumeUUID);
        final VDI.Record vdir = volume.getRecord(conn);
        final VolumeObjectTO newVol = new VolumeObjectTO();
        newVol.setPath(volumeUUID);
        newVol.setSize(vdir.virtualSize);
        return new CopyCmdAnswer(newVol);
    } catch (final XenAPIException e) {
        details += " due to " + e.toString();
        s_logger.warn(details, e);
    } catch (final Exception e) {
        details += " due to " + e.getMessage();
        s_logger.warn(details, e);
    }
    if (!result) {
        // Is this logged at a higher level?
        s_logger.error(details);
    }
    // In all cases return something.
    return new CopyCmdAnswer(details);
}
Also used : SnapshotObjectTO(com.cloud.storage.to.SnapshotObjectTO) PrimaryDataStoreTO(com.cloud.storage.to.PrimaryDataStoreTO) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) Connection(com.xensource.xenapi.Connection) XenAPIException(com.xensource.xenapi.Types.XenAPIException) InternalErrorException(com.cloud.exception.InternalErrorException) NfsTO(com.cloud.agent.api.to.NfsTO) URI(java.net.URI) XenAPIException(com.xensource.xenapi.Types.XenAPIException) InternalErrorException(com.cloud.exception.InternalErrorException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DataTO(com.cloud.agent.api.to.DataTO) PrimaryDataStoreTO(com.cloud.storage.to.PrimaryDataStoreTO) VolumeObjectTO(com.cloud.storage.to.VolumeObjectTO) VDI(com.xensource.xenapi.VDI) CopyCmdAnswer(com.cloud.storage.command.CopyCmdAnswer) SR(com.xensource.xenapi.SR)

Example 93 with SR

use of com.xensource.xenapi.SR in project cosmic by MissionCriticalCloud.

the class XenServerStorageProcessor method getNfsSR.

protected SR getNfsSR(final Connection conn, final StorageFilerTO pool) {
    final Map<String, String> deviceConfig = new HashMap<>();
    try {
        final String server = pool.getHost();
        String serverpath = pool.getPath();
        serverpath = serverpath.replace("//", "/");
        final Set<SR> srs = SR.getAll(conn);
        for (final SR sr : srs) {
            if (!SRType.NFS.equals(sr.getType(conn))) {
                continue;
            }
            final Set<PBD> pbds = sr.getPBDs(conn);
            if (pbds.isEmpty()) {
                continue;
            }
            final PBD pbd = pbds.iterator().next();
            final Map<String, String> dc = pbd.getDeviceConfig(conn);
            if (dc == null) {
                continue;
            }
            if (dc.get("server") == null) {
                continue;
            }
            if (dc.get("serverpath") == null) {
                continue;
            }
            if (server.equals(dc.get("server")) && serverpath.equals(dc.get("serverpath"))) {
                throw new CloudRuntimeException("There is a SR using the same configuration server:" + dc.get("server") + ", serverpath:" + dc.get("serverpath") + " for pool " + pool.getUuid() + "on host:" + hypervisorResource.getHost().getUuid());
            }
        }
        deviceConfig.put("server", server);
        deviceConfig.put("serverpath", serverpath);
        final Host host = Host.getByUuid(conn, hypervisorResource.getHost().getUuid());
        final Map<String, String> smConfig = new HashMap<>();
        smConfig.put("nosubdir", "true");
        final SR sr = SR.create(conn, host, deviceConfig, new Long(0), pool.getUuid(), Long.toString(pool.getId()), SRType.NFS.toString(), "user", true, smConfig);
        sr.scan(conn);
        return sr;
    } catch (final XenAPIException e) {
        throw new CloudRuntimeException("Unable to create NFS SR " + pool.toString(), e);
    } catch (final XmlRpcException e) {
        throw new CloudRuntimeException("Unable to create NFS SR " + pool.toString(), e);
    }
}
Also used : PBD(com.xensource.xenapi.PBD) HashMap(java.util.HashMap) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) XenAPIException(com.xensource.xenapi.Types.XenAPIException) Host(com.xensource.xenapi.Host) XmlRpcException(org.apache.xmlrpc.XmlRpcException) SR(com.xensource.xenapi.SR)

Example 94 with SR

use of com.xensource.xenapi.SR in project cosmic by MissionCriticalCloud.

the class XenServerStorageProcessor method copyTemplateToPrimaryStorage.

@Override
public Answer copyTemplateToPrimaryStorage(final CopyCommand cmd) {
    final DataTO srcDataTo = cmd.getSrcTO();
    final DataTO destDataTo = cmd.getDestTO();
    final int wait = cmd.getWait();
    final DataStoreTO srcDataStoreTo = srcDataTo.getDataStore();
    try {
        if (srcDataStoreTo instanceof NfsTO && srcDataTo.getObjectType() == DataObjectType.TEMPLATE) {
            final NfsTO srcImageStore = (NfsTO) srcDataStoreTo;
            final TemplateObjectTO srcTemplateObjectTo = (TemplateObjectTO) srcDataTo;
            final String storeUrl = srcImageStore.getUrl();
            final URI uri = new URI(storeUrl);
            final String tmplPath = uri.getHost() + ":" + uri.getPath() + "/" + srcDataTo.getPath();
            final DataStoreTO destDataStoreTo = destDataTo.getDataStore();
            boolean managed = false;
            String storageHost = null;
            String managedStoragePoolName = null;
            String managedStoragePoolRootVolumeName = null;
            String managedStoragePoolRootVolumeSize = null;
            String chapInitiatorUsername = null;
            String chapInitiatorSecret = null;
            if (destDataStoreTo instanceof PrimaryDataStoreTO) {
                final PrimaryDataStoreTO destPrimaryDataStoreTo = (PrimaryDataStoreTO) destDataStoreTo;
                final Map<String, String> details = destPrimaryDataStoreTo.getDetails();
                if (details != null) {
                    managed = Boolean.parseBoolean(details.get(PrimaryDataStoreTO.MANAGED));
                    if (managed) {
                        storageHost = details.get(PrimaryDataStoreTO.STORAGE_HOST);
                        managedStoragePoolName = details.get(PrimaryDataStoreTO.MANAGED_STORE_TARGET);
                        managedStoragePoolRootVolumeName = details.get(PrimaryDataStoreTO.MANAGED_STORE_TARGET_ROOT_VOLUME);
                        managedStoragePoolRootVolumeSize = details.get(PrimaryDataStoreTO.VOLUME_SIZE);
                        chapInitiatorUsername = details.get(PrimaryDataStoreTO.CHAP_INITIATOR_USERNAME);
                        chapInitiatorSecret = details.get(PrimaryDataStoreTO.CHAP_INITIATOR_SECRET);
                    }
                }
            }
            final Connection conn = hypervisorResource.getConnection();
            final SR sr;
            if (managed) {
                final Map<String, String> details = new HashMap<>();
                details.put(DiskTO.STORAGE_HOST, storageHost);
                details.put(DiskTO.IQN, managedStoragePoolName);
                details.put(DiskTO.VOLUME_SIZE, managedStoragePoolRootVolumeSize);
                details.put(DiskTO.CHAP_INITIATOR_USERNAME, chapInitiatorUsername);
                details.put(DiskTO.CHAP_INITIATOR_SECRET, chapInitiatorSecret);
                sr = hypervisorResource.prepareManagedSr(conn, details);
            } else {
                final String srName = destDataStoreTo.getUuid();
                final Set<SR> srs = SR.getByNameLabel(conn, srName);
                if (srs.size() != 1) {
                    final String msg = "There are " + srs.size() + " SRs with same name: " + srName;
                    s_logger.warn(msg);
                    return new CopyCmdAnswer(msg);
                } else {
                    sr = srs.iterator().next();
                }
            }
            final String srUuid = sr.getUuid(conn);
            final String tmplUuid = copy_vhd_from_secondarystorage(conn, tmplPath, srUuid, wait);
            final VDI tmplVdi = getVDIbyUuid(conn, tmplUuid);
            final String uuidToReturn;
            final Long physicalSize = tmplVdi.getPhysicalUtilisation(conn);
            if (managed) {
                uuidToReturn = tmplUuid;
                tmplVdi.setNameLabel(conn, managedStoragePoolRootVolumeName);
            } else {
                final VDI snapshotVdi = tmplVdi.snapshot(conn, new HashMap<>());
                uuidToReturn = snapshotVdi.getUuid(conn);
                snapshotVdi.setNameLabel(conn, "Template " + srcTemplateObjectTo.getName());
                tmplVdi.destroy(conn);
            }
            sr.scan(conn);
            try {
                Thread.sleep(5000);
            } catch (final InterruptedException e) {
            }
            final TemplateObjectTO newVol = new TemplateObjectTO();
            newVol.setUuid(uuidToReturn);
            newVol.setPath(uuidToReturn);
            if (physicalSize != null) {
                newVol.setSize(physicalSize);
            }
            newVol.setFormat(ImageFormat.VHD);
            return new CopyCmdAnswer(newVol);
        }
    } catch (final Exception e) {
        final String msg = "Catch Exception " + e.getClass().getName() + " for template + " + " due to " + e.toString();
        s_logger.warn(msg, e);
        return new CopyCmdAnswer(msg);
    }
    return new CopyCmdAnswer("not implemented yet");
}
Also used : PrimaryDataStoreTO(com.cloud.storage.to.PrimaryDataStoreTO) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) HashMap(java.util.HashMap) Connection(com.xensource.xenapi.Connection) NfsTO(com.cloud.agent.api.to.NfsTO) URI(java.net.URI) XenAPIException(com.xensource.xenapi.Types.XenAPIException) InternalErrorException(com.cloud.exception.InternalErrorException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DataTO(com.cloud.agent.api.to.DataTO) PrimaryDataStoreTO(com.cloud.storage.to.PrimaryDataStoreTO) VDI(com.xensource.xenapi.VDI) TemplateObjectTO(com.cloud.storage.to.TemplateObjectTO) CopyCmdAnswer(com.cloud.storage.command.CopyCmdAnswer) SR(com.xensource.xenapi.SR)

Example 95 with SR

use of com.xensource.xenapi.SR in project cosmic by MissionCriticalCloud.

the class XenServerStorageProcessor method getSRByNameLabel.

protected SR getSRByNameLabel(final Connection conn, final String nameLabel) throws BadServerResponse, XenAPIException, XmlRpcException {
    final Set<SR> srs = SR.getByNameLabel(conn, nameLabel);
    if (srs.size() != 1) {
        throw new CloudRuntimeException("storage uuid: " + nameLabel + " is not unique");
    }
    final SR poolsr = srs.iterator().next();
    return poolsr;
}
Also used : CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) SR(com.xensource.xenapi.SR)

Aggregations

SR (com.xensource.xenapi.SR)165 XenAPIException (com.xensource.xenapi.Types.XenAPIException)105 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)102 XmlRpcException (org.apache.xmlrpc.XmlRpcException)97 Connection (com.xensource.xenapi.Connection)92 VDI (com.xensource.xenapi.VDI)81 InternalErrorException (com.cloud.exception.InternalErrorException)64 HashMap (java.util.HashMap)45 Host (com.xensource.xenapi.Host)40 PBD (com.xensource.xenapi.PBD)37 URI (java.net.URI)36 NfsTO (com.cloud.agent.api.to.NfsTO)34 Test (org.junit.Test)27 DataStoreTO (com.cloud.agent.api.to.DataStoreTO)26 Task (com.xensource.xenapi.Task)26 DataTO (com.cloud.agent.api.to.DataTO)25 Answer (com.cloud.agent.api.Answer)22 CopyCmdAnswer (org.apache.cloudstack.storage.command.CopyCmdAnswer)21 IOException (java.io.IOException)20 MalformedURLException (java.net.MalformedURLException)20