Search in sources :

Example 6 with SR

use of com.xensource.xenapi.SR in project cloudstack by apache.

the class Xenserver625StorageProcessor method createFileSr.

protected SR createFileSr(final Connection conn, final String remotePath, final String dir) {
    final String localDir = "/var/cloud_mount/" + UUID.nameUUIDFromBytes(remotePath.getBytes());
    mountNfs(conn, remotePath, localDir);
    final SR sr = createFileSR(conn, localDir + "/" + dir);
    return sr;
}
Also used : SR(com.xensource.xenapi.SR)

Example 7 with SR

use of com.xensource.xenapi.SR in project cloudstack by apache.

the class Xenserver625StorageProcessor 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 PrimaryDataStoreTO pool = (PrimaryDataStoreTO) destData.getDataStore();
    final VolumeObjectTO volume = (VolumeObjectTO) destData;
    final DataStoreTO imageStore = srcData.getDataStore();
    if (isCreateManagedVolumeFromManagedSnapshot(cmd.getOptions2(), cmd.getOptions())) {
        return createManagedVolumeFromManagedSnapshot(cmd);
    }
    if (isCreateNonManagedVolumeFromManagedSnapshot(cmd.getOptions2(), cmd.getOptions())) {
        return createNonManagedVolumeFromManagedSnapshot(cmd);
    }
    if (!(imageStore instanceof NfsTO)) {
        return new CopyCmdAnswer("unsupported protocol");
    }
    final NfsTO nfsImageStore = (NfsTO) imageStore;
    final String nfsVersion = nfsImageStore.getNfsVersion();
    final String primaryStorageNameLabel = pool.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);
    }
    SR srcSr = null;
    VDI destVdi = null;
    SR primaryStorageSR = null;
    try {
        if (pool.isManaged()) {
            Map<String, String> destDetails = cmd.getOptions2();
            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 String srType = CitrixResourceBase.SRType.LVMOISCSI.toString();
            primaryStorageSR = hypervisorResource.getIscsiSR(conn, iScsiName, storageHost, iScsiName, chapInitiatorUsername, chapInitiatorSecret, false, srType, true);
        } else {
            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);
        }
        final String nameLabel = "cloud-" + UUID.randomUUID().toString();
        destVdi = createVdi(conn, nameLabel, primaryStorageSR, volume.getSize());
        volumeUUID = destVdi.getUuid(conn);
        final String snapshotInstallPath = snapshot.getPath();
        final int index = snapshotInstallPath.lastIndexOf(File.separator);
        final String snapshotDirectory = snapshotInstallPath.substring(0, index);
        final String snapshotUuid = getSnapshotUuid(snapshotInstallPath);
        final URI uri = new URI(secondaryStorageUrl);
        srcSr = createFileSr(conn, uri.getHost() + ":" + uri.getPath(), snapshotDirectory, nfsVersion);
        final String[] parents = snapshot.getParents();
        final List<VDI> snapshotChains = new ArrayList<VDI>();
        if (parents != null) {
            for (int i = 0; i < parents.length; i++) {
                final String snChainPath = parents[i];
                final String uuid = getSnapshotUuid(snChainPath);
                final VDI chain = VDI.getByUuid(conn, uuid);
                snapshotChains.add(chain);
            }
        }
        final VDI snapshotVdi = VDI.getByUuid(conn, snapshotUuid);
        snapshotChains.add(snapshotVdi);
        for (final VDI snapChain : snapshotChains) {
            final Task task = snapChain.copyAsync(conn, null, null, destVdi);
            // poll every 1 seconds ,
            hypervisorResource.waitForTask(conn, task, 1000, wait * 1000);
            hypervisorResource.checkForSuccess(conn, task);
            task.destroy(conn);
        }
        result = true;
        destVdi = VDI.getByUuid(conn, volumeUUID);
        final VDI.Record vdir = destVdi.getRecord(conn);
        final VolumeObjectTO newVol = new VolumeObjectTO();
        newVol.setPath(volumeUUID);
        newVol.setSize(vdir.virtualSize);
        return new CopyCmdAnswer(newVol);
    } catch (final Types.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);
    } finally {
        if (srcSr != null) {
            hypervisorResource.skipOrRemoveSR(conn, srcSr);
        }
        if (pool.isManaged()) {
            hypervisorResource.removeSR(conn, primaryStorageSR);
        }
        if (!result && destVdi != null) {
            try {
                destVdi.destroy(conn);
            } catch (final Exception e) {
                s_logger.debug("destroy dest vdi failed", 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(org.apache.cloudstack.storage.to.SnapshotObjectTO) Types(com.xensource.xenapi.Types) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) Task(com.xensource.xenapi.Task) Connection(com.xensource.xenapi.Connection) ArrayList(java.util.ArrayList) 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) XenAPIException(com.xensource.xenapi.Types.XenAPIException) DataTO(com.cloud.agent.api.to.DataTO) PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) VDI(com.xensource.xenapi.VDI) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer) SR(com.xensource.xenapi.SR)

Example 8 with SR

use of com.xensource.xenapi.SR in project cloudstack by apache.

the class Xenserver625StorageProcessor method createTemplateFromVolume.

@Override
public Answer createTemplateFromVolume(final CopyCommand cmd) {
    final Connection conn = hypervisorResource.getConnection();
    final VolumeObjectTO volume = (VolumeObjectTO) cmd.getSrcTO();
    final TemplateObjectTO template = (TemplateObjectTO) cmd.getDestTO();
    final NfsTO destStore = (NfsTO) cmd.getDestTO().getDataStore();
    final int wait = cmd.getWait();
    final String secondaryStoragePoolURL = destStore.getUrl();
    final String volumeUUID = volume.getPath();
    final String nfsVersion = destStore.getNfsVersion();
    final String userSpecifiedName = template.getName();
    String details = null;
    SR tmpltSR = null;
    boolean result = false;
    String secondaryStorageMountPath = null;
    String installPath = null;
    Task task = null;
    try {
        final URI uri = new URI(secondaryStoragePoolURL);
        secondaryStorageMountPath = uri.getHost() + ":" + uri.getPath();
        installPath = template.getPath();
        if (!hypervisorResource.createSecondaryStorageFolder(conn, secondaryStorageMountPath, installPath, nfsVersion)) {
            details = " Filed to create folder " + installPath + " in secondary storage";
            s_logger.warn(details);
            return new CopyCmdAnswer(details);
        }
        final VDI vol = getVDIbyUuid(conn, volumeUUID);
        // create template SR
        tmpltSR = createFileSr(conn, uri.getHost() + ":" + uri.getPath(), installPath, nfsVersion);
        // copy volume to template SR
        task = vol.copyAsync(conn, tmpltSR, null, null);
        // poll every 1 seconds ,
        hypervisorResource.waitForTask(conn, task, 1000, wait * 1000);
        hypervisorResource.checkForSuccess(conn, task);
        final VDI tmpltVDI = Types.toVDI(task, conn);
        // scan makes XenServer pick up VDI physicalSize
        tmpltSR.scan(conn);
        if (userSpecifiedName != null) {
            tmpltVDI.setNameLabel(conn, userSpecifiedName);
        }
        final String tmpltUUID = tmpltVDI.getUuid(conn);
        final String tmpltFilename = tmpltUUID + ".vhd";
        final long virtualSize = tmpltVDI.getVirtualSize(conn);
        final long physicalSize = tmpltVDI.getPhysicalUtilisation(conn);
        // create the template.properties file
        final String templatePath = secondaryStorageMountPath + "/" + installPath;
        result = hypervisorResource.postCreatePrivateTemplate(conn, templatePath, tmpltFilename, tmpltUUID, userSpecifiedName, null, physicalSize, virtualSize, template.getId(), nfsVersion);
        if (!result) {
            throw new CloudRuntimeException("Could not create the template.properties file on secondary storage dir");
        }
        installPath = installPath + "/" + tmpltFilename;
        hypervisorResource.removeSR(conn, tmpltSR);
        tmpltSR = null;
        final TemplateObjectTO newTemplate = new TemplateObjectTO();
        newTemplate.setPath(installPath);
        newTemplate.setFormat(Storage.ImageFormat.VHD);
        newTemplate.setSize(virtualSize);
        newTemplate.setPhysicalSize(physicalSize);
        newTemplate.setName(tmpltUUID);
        final CopyCmdAnswer answer = new CopyCmdAnswer(newTemplate);
        return answer;
    } catch (final Exception e) {
        if (tmpltSR != null) {
            hypervisorResource.removeSR(conn, tmpltSR);
        }
        if (secondaryStorageMountPath != null) {
            hypervisorResource.deleteSecondaryStorageFolder(conn, secondaryStorageMountPath, installPath, nfsVersion);
        }
        details = "Creating template from volume " + volumeUUID + " failed due to " + e.toString();
        s_logger.error(details, e);
    } finally {
        if (task != null) {
            try {
                task.destroy(conn);
            } catch (final Exception e) {
                s_logger.warn("unable to destroy task(" + task.toWireString() + ") due to " + e.toString());
            }
        }
    }
    return new CopyCmdAnswer(details);
}
Also used : Task(com.xensource.xenapi.Task) Connection(com.xensource.xenapi.Connection) NfsTO(com.cloud.agent.api.to.NfsTO) URI(java.net.URI) InternalErrorException(com.cloud.exception.InternalErrorException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) XenAPIException(com.xensource.xenapi.Types.XenAPIException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) VDI(com.xensource.xenapi.VDI) TemplateObjectTO(org.apache.cloudstack.storage.to.TemplateObjectTO) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer) SR(com.xensource.xenapi.SR)

Example 9 with SR

use of com.xensource.xenapi.SR in project cloudstack by apache.

the class Xenserver625StorageProcessor method backupSnapshot.

protected String backupSnapshot(final Connection conn, final String primaryStorageSRUuid, final String localMountPoint, final String path, final String secondaryStorageMountPath, final String snapshotUuid, String prevBackupUuid, final String prevSnapshotUuid, final Boolean isISCSI, int wait, String nfsVersion) {
    boolean filesrcreated = false;
    if (prevBackupUuid == null) {
        prevBackupUuid = "";
    }
    SR ssSR = null;
    final String remoteDir = secondaryStorageMountPath;
    try {
        ssSR = createFileSr(conn, remoteDir, path, nfsVersion);
        filesrcreated = true;
        final VDI snapshotvdi = VDI.getByUuid(conn, snapshotUuid);
        if (wait == 0) {
            wait = 2 * 60 * 60;
        }
        VDI dvdi = null;
        Task task = null;
        try {
            VDI previousSnapshotVdi = null;
            if (prevSnapshotUuid != null) {
                previousSnapshotVdi = VDI.getByUuid(conn, prevSnapshotUuid);
            }
            task = snapshotvdi.copyAsync(conn, ssSR, previousSnapshotVdi, null);
            // poll every 1 seconds ,
            hypervisorResource.waitForTask(conn, task, 1000, wait * 1000);
            hypervisorResource.checkForSuccess(conn, task);
            dvdi = Types.toVDI(task, conn);
            ssSR.scan(conn);
        // copied = true;
        } finally {
            if (task != null) {
                try {
                    task.destroy(conn);
                } catch (final Exception e) {
                    s_logger.warn("unable to destroy task(" + task.toWireString() + ") due to " + e.toString());
                }
            }
        }
        final String result = dvdi.getUuid(conn).concat("#").concat(dvdi.getPhysicalUtilisation(conn).toString());
        return result;
    } catch (final Exception e) {
        final String msg = "Exception in backupsnapshot stage due to " + e.toString();
        s_logger.debug(msg);
        throw new CloudRuntimeException(msg, e);
    } finally {
        try {
            if (filesrcreated && ssSR != null) {
                hypervisorResource.removeSR(conn, ssSR);
            }
        } catch (final Exception e) {
            s_logger.debug("Exception in backupsnapshot cleanup stage due to " + e.toString());
        }
    }
}
Also used : Task(com.xensource.xenapi.Task) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VDI(com.xensource.xenapi.VDI) InternalErrorException(com.cloud.exception.InternalErrorException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) XenAPIException(com.xensource.xenapi.Types.XenAPIException) SR(com.xensource.xenapi.SR)

Example 10 with SR

use of com.xensource.xenapi.SR in project cloudstack by apache.

the class Xenserver625StorageProcessor method createTemplateFromSnapshot2.

private Answer createTemplateFromSnapshot2(final CopyCommand cmd) {
    final Connection conn = hypervisorResource.getConnection();
    final SnapshotObjectTO snapshotObjTO = (SnapshotObjectTO) cmd.getSrcTO();
    final TemplateObjectTO templateObjTO = (TemplateObjectTO) cmd.getDestTO();
    if (!(snapshotObjTO.getDataStore() instanceof PrimaryDataStoreTO) || !(templateObjTO.getDataStore() instanceof NfsTO)) {
        return null;
    }
    NfsTO destStore;
    URI destUri;
    try {
        destStore = (NfsTO) templateObjTO.getDataStore();
        destUri = new URI(destStore.getUrl());
    } catch (final Exception ex) {
        s_logger.debug("Invalid URI", ex);
        return new CopyCmdAnswer("Invalid URI: " + ex.toString());
    }
    SR srcSr = null;
    SR destSr = null;
    final String destDir = templateObjTO.getPath();
    VDI destVdi = null;
    boolean result = false;
    try {
        final Map<String, String> srcDetails = cmd.getOptions();
        final String iScsiName = srcDetails.get(DiskTO.IQN);
        final String storageHost = srcDetails.get(DiskTO.STORAGE_HOST);
        final String chapInitiatorUsername = srcDetails.get(DiskTO.CHAP_INITIATOR_USERNAME);
        final String chapInitiatorSecret = srcDetails.get(DiskTO.CHAP_INITIATOR_SECRET);
        String srType;
        srType = CitrixResourceBase.SRType.LVMOISCSI.toString();
        srcSr = hypervisorResource.getIscsiSR(conn, iScsiName, storageHost, iScsiName, chapInitiatorUsername, chapInitiatorSecret, false, srType, true);
        final String destNfsPath = destUri.getHost() + ":" + destUri.getPath();
        final String localDir = BASE_MOUNT_POINT_ON_REMOTE + UUID.nameUUIDFromBytes(destNfsPath.getBytes());
        String nfsVersion = destStore.getNfsVersion();
        mountNfs(conn, destNfsPath, localDir, nfsVersion);
        makeDirectory(conn, localDir + "/" + destDir);
        destSr = createFileSR(conn, localDir + "/" + destDir);
        // there should only be one VDI in this SR
        final VDI srcVdi = srcSr.getVDIs(conn).iterator().next();
        destVdi = srcVdi.copy(conn, destSr);
        final String nameLabel = "cloud-" + UUID.randomUUID().toString();
        destVdi.setNameLabel(conn, nameLabel);
        // scan makes XenServer pick up VDI physicalSize
        destSr.scan(conn);
        final String templateUuid = destVdi.getUuid(conn);
        final String templateFilename = templateUuid + ".vhd";
        final long virtualSize = destVdi.getVirtualSize(conn);
        final long physicalSize = destVdi.getPhysicalUtilisation(conn);
        // create the template.properties file
        String templatePath = destNfsPath + "/" + destDir;
        templatePath = templatePath.replaceAll("//", "/");
        result = hypervisorResource.postCreatePrivateTemplate(conn, templatePath, templateFilename, templateUuid, nameLabel, null, physicalSize, virtualSize, templateObjTO.getId(), nfsVersion);
        if (!result) {
            throw new CloudRuntimeException("Could not create the template.properties file on secondary storage dir");
        }
        final TemplateObjectTO newTemplate = new TemplateObjectTO();
        newTemplate.setPath(destDir + "/" + templateFilename);
        newTemplate.setFormat(Storage.ImageFormat.VHD);
        newTemplate.setHypervisorType(HypervisorType.XenServer);
        newTemplate.setSize(virtualSize);
        newTemplate.setPhysicalSize(physicalSize);
        newTemplate.setName(templateUuid);
        result = true;
        return new CopyCmdAnswer(newTemplate);
    } catch (final BadServerResponse e) {
        s_logger.error("Failed to create a template from a snapshot due to incomprehensible server response", e);
        return new CopyCmdAnswer("Failed to create a template from a snapshot: " + e.toString());
    } catch (final XenAPIException e) {
        s_logger.error("Failed to create a template from a snapshot due to xenapi error", e);
        return new CopyCmdAnswer("Failed to create a template from a snapshot: " + e.toString());
    } catch (final XmlRpcException e) {
        s_logger.error("Failed to create a template from a snapshot due to rpc error", e);
        return new CopyCmdAnswer("Failed to create a template from a snapshot: " + e.toString());
    } finally {
        if (!result) {
            if (destVdi != null) {
                try {
                    destVdi.destroy(conn);
                } catch (final Exception e) {
                    s_logger.debug("Cleaned up leftover VDI on destination storage due to failure: ", e);
                }
            }
        }
        if (srcSr != null) {
            hypervisorResource.removeSR(conn, srcSr);
        }
        if (destSr != null) {
            hypervisorResource.removeSR(conn, destSr);
        }
    }
}
Also used : SnapshotObjectTO(org.apache.cloudstack.storage.to.SnapshotObjectTO) BadServerResponse(com.xensource.xenapi.Types.BadServerResponse) Connection(com.xensource.xenapi.Connection) XenAPIException(com.xensource.xenapi.Types.XenAPIException) NfsTO(com.cloud.agent.api.to.NfsTO) URI(java.net.URI) InternalErrorException(com.cloud.exception.InternalErrorException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) XenAPIException(com.xensource.xenapi.Types.XenAPIException) PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VDI(com.xensource.xenapi.VDI) TemplateObjectTO(org.apache.cloudstack.storage.to.TemplateObjectTO) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer) XmlRpcException(org.apache.xmlrpc.XmlRpcException) 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