Search in sources :

Example 26 with NfsTO

use of com.cloud.agent.api.to.NfsTO in project cloudstack by apache.

the class SimulatorImageStoreDriverImpl method getStoreTO.

@Override
public DataStoreTO getStoreTO(DataStore store) {
    ImageStoreImpl nfsStore = (ImageStoreImpl) store;
    NfsTO nfsTO = new NfsTO();
    nfsTO.setRole(store.getRole());
    nfsTO.setUrl(nfsStore.getUri());
    nfsTO.setNfsVersion(getNfsVersion(nfsStore.getId()));
    return nfsTO;
}
Also used : ImageStoreImpl(org.apache.cloudstack.storage.image.store.ImageStoreImpl) NfsTO(com.cloud.agent.api.to.NfsTO)

Example 27 with NfsTO

use of com.cloud.agent.api.to.NfsTO 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 28 with NfsTO

use of com.cloud.agent.api.to.NfsTO 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 29 with NfsTO

use of com.cloud.agent.api.to.NfsTO 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)

Example 30 with NfsTO

use of com.cloud.agent.api.to.NfsTO in project cloudstack by apache.

the class Xenserver625StorageProcessor method copyVolumeFromPrimaryToSecondary.

@Override
public Answer copyVolumeFromPrimaryToSecondary(final CopyCommand cmd) {
    final Connection conn = hypervisorResource.getConnection();
    final VolumeObjectTO srcVolume = (VolumeObjectTO) cmd.getSrcTO();
    final VolumeObjectTO destVolume = (VolumeObjectTO) cmd.getDestTO();
    final int wait = cmd.getWait();
    final DataStoreTO destStore = destVolume.getDataStore();
    if (destStore instanceof NfsTO) {
        SR secondaryStorage = null;
        Task task = null;
        try {
            final NfsTO nfsStore = (NfsTO) destStore;
            final String nfsVersion = nfsStore.getNfsVersion();
            final URI uri = new URI(nfsStore.getUrl());
            // Create the volume folder
            if (!hypervisorResource.createSecondaryStorageFolder(conn, uri.getHost() + ":" + uri.getPath(), destVolume.getPath(), nfsVersion)) {
                throw new InternalErrorException("Failed to create the volume folder.");
            }
            // Create a SR for the volume UUID folder
            secondaryStorage = createFileSr(conn, uri.getHost() + ":" + uri.getPath(), destVolume.getPath(), nfsVersion);
            // Look up the volume on the source primary storage pool
            final VDI srcVdi = getVDIbyUuid(conn, srcVolume.getPath());
            // Copy the volume to secondary storage
            task = srcVdi.copyAsync(conn, secondaryStorage, null, null);
            // poll every 1 seconds ,
            hypervisorResource.waitForTask(conn, task, 1000, wait * 1000);
            hypervisorResource.checkForSuccess(conn, task);
            final VDI destVdi = Types.toVDI(task, conn);
            final String destVolumeUUID = destVdi.getUuid(conn);
            final VolumeObjectTO newVol = new VolumeObjectTO();
            newVol.setPath(destVolume.getPath() + File.separator + destVolumeUUID + ".vhd");
            newVol.setSize(srcVolume.getSize());
            return new CopyCmdAnswer(newVol);
        } catch (final Exception e) {
            s_logger.debug("Failed to copy volume to secondary: " + e.toString());
            return new CopyCmdAnswer("Failed to copy volume to secondary: " + e.toString());
        } 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());
                }
            }
            hypervisorResource.removeSR(conn, secondaryStorage);
        }
    }
    return new CopyCmdAnswer("unsupported protocol");
}
Also used : DataStoreTO(com.cloud.agent.api.to.DataStoreTO) PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) Task(com.xensource.xenapi.Task) Connection(com.xensource.xenapi.Connection) InternalErrorException(com.cloud.exception.InternalErrorException) 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) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) VDI(com.xensource.xenapi.VDI) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer) SR(com.xensource.xenapi.SR)

Aggregations

NfsTO (com.cloud.agent.api.to.NfsTO)142 DataStoreTO (com.cloud.agent.api.to.DataStoreTO)110 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)79 DataTO (com.cloud.agent.api.to.DataTO)71 InternalErrorException (com.cloud.exception.InternalErrorException)58 CopyCmdAnswer (org.apache.cloudstack.storage.command.CopyCmdAnswer)52 PrimaryDataStoreTO (org.apache.cloudstack.storage.to.PrimaryDataStoreTO)39 TemplateObjectTO (org.apache.cloudstack.storage.to.TemplateObjectTO)38 XmlRpcException (org.apache.xmlrpc.XmlRpcException)37 XenAPIException (com.xensource.xenapi.Types.XenAPIException)36 URI (java.net.URI)36 Connection (com.xensource.xenapi.Connection)34 SR (com.xensource.xenapi.SR)34 VDI (com.xensource.xenapi.VDI)34 CopyCmdAnswer (com.cloud.storage.command.CopyCmdAnswer)32 VolumeObjectTO (org.apache.cloudstack.storage.to.VolumeObjectTO)32 Answer (com.cloud.agent.api.Answer)29 IOException (java.io.IOException)28 File (java.io.File)27 PrimaryDataStoreTO (com.cloud.storage.to.PrimaryDataStoreTO)26