Search in sources :

Example 56 with SR

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

the class XenServerStorageProcessor method backupSnapshot.

@Override
public Answer backupSnapshot(final CopyCommand cmd) {
    final Connection conn = hypervisorResource.getConnection();
    final DataTO srcData = cmd.getSrcTO();
    final DataTO cacheData = cmd.getCacheTO();
    final DataTO destData = cmd.getDestTO();
    final int wait = cmd.getWait();
    final String primaryStorageNameLabel = srcData.getDataStore().getUuid();
    String secondaryStorageUrl = null;
    NfsTO cacheStore = null;
    String destPath = null;
    if (cacheData != null) {
        cacheStore = (NfsTO) cacheData.getDataStore();
        secondaryStorageUrl = cacheStore.getUrl();
        destPath = cacheData.getPath();
    } else {
        cacheStore = (NfsTO) destData.getDataStore();
        secondaryStorageUrl = cacheStore.getUrl();
        destPath = destData.getPath();
    }
    final SnapshotObjectTO snapshotTO = (SnapshotObjectTO) srcData;
    final SnapshotObjectTO snapshotOnImage = (SnapshotObjectTO) destData;
    final String snapshotUuid = snapshotTO.getPath();
    final String volumeUuid = snapshotTO.getVolume().getPath();
    final String prevBackupUuid = snapshotOnImage.getParentSnapshotPath();
    final String prevSnapshotUuid = snapshotTO.getParentSnapshotPath();
    // By default assume failure
    String details = null;
    String snapshotBackupUuid = null;
    Long physicalSize = null;
    final Map<String, String> options = cmd.getOptions();
    boolean fullbackup = Boolean.parseBoolean(options.get("fullSnapshot"));
    boolean result = false;
    try {
        final SR primaryStorageSR = hypervisorResource.getSRByNameLabelandHost(conn, primaryStorageNameLabel);
        if (primaryStorageSR == null) {
            throw new InternalErrorException("Could not backup snapshot because the primary Storage SR could not be created from the name label: " + primaryStorageNameLabel);
        }
        final String psUuid = primaryStorageSR.getUuid(conn);
        final Boolean isISCSI = IsISCSI(primaryStorageSR.getType(conn));
        final VDI snapshotVdi = getVDIbyUuid(conn, snapshotUuid);
        String snapshotPaUuid = null;
        if (prevSnapshotUuid != null && !fullbackup) {
            try {
                snapshotPaUuid = getVhdParent(conn, psUuid, snapshotUuid, isISCSI);
                if (snapshotPaUuid != null) {
                    final String snashotPaPaPaUuid = getVhdParent(conn, psUuid, snapshotPaUuid, isISCSI);
                    final String prevSnashotPaUuid = getVhdParent(conn, psUuid, prevSnapshotUuid, isISCSI);
                    if (snashotPaPaPaUuid != null && prevSnashotPaUuid != null && prevSnashotPaUuid.equals(snashotPaPaPaUuid)) {
                        fullbackup = false;
                    } else {
                        fullbackup = true;
                    }
                }
            } catch (final Exception e) {
                s_logger.debug("Failed to get parent snapshots, take full snapshot", e);
                fullbackup = true;
            }
        }
        final URI uri = new URI(secondaryStorageUrl);
        final String secondaryStorageMountPath = uri.getHost() + ":" + uri.getPath();
        final DataStoreTO destStore = destData.getDataStore();
        final String folder = destPath;
        String finalPath = null;
        final String localMountPoint = BaseMountPointOnHost + File.separator + UUID.nameUUIDFromBytes(secondaryStorageUrl.getBytes()).toString();
        if (fullbackup) {
            if (!hypervisorResource.createSecondaryStorageFolder(conn, secondaryStorageMountPath, folder)) {
                details = " Filed to create folder " + folder + " in secondary storage";
                s_logger.warn(details);
                return new CopyCmdAnswer(details);
            }
            final String snapshotMountpoint = secondaryStorageUrl + "/" + folder;
            SR snapshotSr = null;
            try {
                snapshotSr = hypervisorResource.createNfsSRbyURI(conn, new URI(snapshotMountpoint), false);
                final VDI backedVdi = hypervisorResource.cloudVDIcopy(conn, snapshotVdi, snapshotSr, wait);
                snapshotBackupUuid = backedVdi.getUuid(conn);
                final String primarySRuuid = snapshotSr.getUuid(conn);
                physicalSize = getSnapshotSize(conn, primarySRuuid, snapshotBackupUuid, isISCSI, wait);
                finalPath = folder + cacheStore.getPathSeparator() + snapshotBackupUuid;
            } finally {
                if (snapshotSr != null) {
                    hypervisorResource.removeSR(conn, snapshotSr);
                }
            }
        } else {
            final String primaryStorageSRUuid = primaryStorageSR.getUuid(conn);
            final String results = backupSnapshot(conn, primaryStorageSRUuid, localMountPoint, folder, secondaryStorageMountPath, snapshotUuid, prevBackupUuid, isISCSI, wait);
            final String[] tmp = results.split("#");
            snapshotBackupUuid = tmp[1];
            physicalSize = Long.parseLong(tmp[2]);
            finalPath = folder + cacheStore.getPathSeparator() + snapshotBackupUuid;
        }
        // delete primary snapshots with only the last one left
        destroySnapshotOnPrimaryStorageExceptThis(conn, volumeUuid, snapshotUuid);
        final SnapshotObjectTO newSnapshot = new SnapshotObjectTO();
        newSnapshot.setPath(finalPath);
        newSnapshot.setPhysicalSize(physicalSize);
        if (fullbackup) {
            newSnapshot.setParentSnapshotPath(null);
        } else {
            newSnapshot.setParentSnapshotPath(prevBackupUuid);
        }
        result = true;
        return new CopyCmdAnswer(newSnapshot);
    } catch (final XenAPIException e) {
        details = "BackupSnapshot Failed due to " + e.toString();
        s_logger.warn(details, e);
    } catch (final Exception e) {
        details = "BackupSnapshot Failed due to " + e.getMessage();
        s_logger.warn(details, e);
    } finally {
        if (!result) {
            // remove last bad primary snapshot when exception happens
            try {
                destroySnapshotOnPrimaryStorage(conn, snapshotUuid);
            } catch (final Exception e) {
                s_logger.debug("clean up snapshot failed", e);
            }
        }
    }
    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) VDI(com.xensource.xenapi.VDI) CopyCmdAnswer(com.cloud.storage.command.CopyCmdAnswer) SR(com.xensource.xenapi.SR)

Example 57 with SR

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

the class XenServerStorageProcessor method createVolumeFromSnapshot2.

protected Answer createVolumeFromSnapshot2(final CopyCommand cmd) {
    try {
        final Connection conn = hypervisorResource.getConnection();
        final Map<String, String> srcOptions = cmd.getOptions();
        final String src_iScsiName = srcOptions.get(DiskTO.IQN);
        final String srcStorageHost = srcOptions.get(DiskTO.STORAGE_HOST);
        final String srcChapInitiatorUsername = srcOptions.get(DiskTO.CHAP_INITIATOR_USERNAME);
        final String srcChapInitiatorSecret = srcOptions.get(DiskTO.CHAP_INITIATOR_SECRET);
        final SR srcSr = hypervisorResource.getIscsiSR(conn, src_iScsiName, srcStorageHost, src_iScsiName, srcChapInitiatorUsername, srcChapInitiatorSecret, false);
        final Map<String, String> destOptions = cmd.getOptions2();
        final String dest_iScsiName = destOptions.get(DiskTO.IQN);
        final String destStorageHost = destOptions.get(DiskTO.STORAGE_HOST);
        final String destChapInitiatorUsername = destOptions.get(DiskTO.CHAP_INITIATOR_USERNAME);
        final String destChapInitiatorSecret = destOptions.get(DiskTO.CHAP_INITIATOR_SECRET);
        final SR destSr = hypervisorResource.getIscsiSR(conn, dest_iScsiName, destStorageHost, dest_iScsiName, destChapInitiatorUsername, destChapInitiatorSecret, false);
        // there should only be one VDI in this SR
        final VDI srcVdi = srcSr.getVDIs(conn).iterator().next();
        final VDI vdiCopy = srcVdi.copy(conn, destSr);
        final VolumeObjectTO newVol = new VolumeObjectTO();
        newVol.setSize(vdiCopy.getVirtualSize(conn));
        newVol.setPath(vdiCopy.getUuid(conn));
        newVol.setFormat(ImageFormat.VHD);
        hypervisorResource.removeSR(conn, srcSr);
        hypervisorResource.removeSR(conn, destSr);
        return new CopyCmdAnswer(newVol);
    } catch (final Exception ex) {
        s_logger.warn("Failed to copy snapshot to volume: " + ex.toString(), ex);
        return new CopyCmdAnswer(ex.getMessage());
    }
}
Also used : Connection(com.xensource.xenapi.Connection) VolumeObjectTO(com.cloud.storage.to.VolumeObjectTO) VDI(com.xensource.xenapi.VDI) CopyCmdAnswer(com.cloud.storage.command.CopyCmdAnswer) 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 58 with SR

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

the class XenServerStorageProcessor method dettachIso.

@Override
public Answer dettachIso(final DettachCommand cmd) {
    final DiskTO disk = cmd.getDisk();
    final DataTO data = disk.getData();
    final DataStoreTO store = data.getDataStore();
    String isoURL = null;
    if (store == null) {
        final TemplateObjectTO iso = (TemplateObjectTO) disk.getData();
        isoURL = iso.getName();
    } else {
        if (!(store instanceof NfsTO)) {
            s_logger.debug("Can't attach a iso which is not created on nfs: ");
            return new AttachAnswer("Can't attach a iso which is not created on nfs: ");
        }
        final NfsTO nfsStore = (NfsTO) store;
        isoURL = nfsStore.getUrl() + nfsStore.getPathSeparator() + data.getPath();
    }
    try {
        final Connection conn = hypervisorResource.getConnection();
        // Find the VM
        final VM vm = hypervisorResource.getVM(conn, cmd.getVmName());
        final String vmUUID = vm.getUuid(conn);
        // Find the ISO VDI
        final VDI isoVDI = hypervisorResource.getIsoVDIByURL(conn, cmd.getVmName(), isoURL);
        final SR sr = isoVDI.getSR(conn);
        // Look up all VBDs for this VDI
        final Set<VBD> vbds = isoVDI.getVBDs(conn);
        // the ISO from it
        for (final VBD vbd : vbds) {
            final VM vbdVM = vbd.getVM(conn);
            final String vbdVmUUID = vbdVM.getUuid(conn);
            if (vbdVmUUID.equals(vmUUID)) {
                // If an ISO is already inserted, eject it
                if (!vbd.getEmpty(conn)) {
                    vbd.eject(conn);
                }
                break;
            }
        }
        if (!sr.getNameLabel(conn).startsWith("XenServer Tools")) {
            hypervisorResource.removeSR(conn, sr);
        }
        return new DettachAnswer(disk);
    } catch (final XenAPIException e) {
        final String msg = "Failed to dettach volume" + " for uuid: " + data.getPath() + "  due to " + e.toString();
        s_logger.warn(msg, e);
        return new DettachAnswer(msg);
    } catch (final Exception e) {
        final String msg = "Failed to dettach volume" + " for uuid: " + data.getPath() + "  due to " + e.getMessage();
        s_logger.warn(msg, e);
        return new DettachAnswer(msg);
    }
}
Also used : PrimaryDataStoreTO(com.cloud.storage.to.PrimaryDataStoreTO) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) DettachAnswer(com.cloud.storage.command.DettachAnswer) Connection(com.xensource.xenapi.Connection) XenAPIException(com.xensource.xenapi.Types.XenAPIException) NfsTO(com.cloud.agent.api.to.NfsTO) 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) VM(com.xensource.xenapi.VM) VBD(com.xensource.xenapi.VBD) VDI(com.xensource.xenapi.VDI) TemplateObjectTO(com.cloud.storage.to.TemplateObjectTO) DiskTO(com.cloud.agent.api.to.DiskTO) AttachAnswer(com.cloud.storage.command.AttachAnswer) SR(com.xensource.xenapi.SR)

Example 59 with SR

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

the class XenServerStorageProcessor method execute.

protected Answer execute(final AttachPrimaryDataStoreCmd cmd) {
    final String dataStoreUri = cmd.getDataStore();
    final Connection conn = hypervisorResource.getConnection();
    try {
        final DecodedDataObject obj = Decoder.decode(dataStoreUri);
        final DecodedDataStore store = obj.getStore();
        final SR sr = hypervisorResource.getStorageRepository(conn, store.getUuid());
        hypervisorResource.setupHeartbeatSr(conn, sr, false);
        final long capacity = sr.getPhysicalSize(conn);
        final long available = capacity - sr.getPhysicalUtilisation(conn);
        if (capacity == -1) {
            final String msg = "Pool capacity is -1! pool: ";
            s_logger.warn(msg);
            return new Answer(cmd, false, msg);
        }
        final AttachPrimaryDataStoreAnswer answer = new AttachPrimaryDataStoreAnswer(cmd);
        answer.setCapacity(capacity);
        answer.setUuid(sr.getUuid(conn));
        answer.setAvailable(available);
        return answer;
    } catch (final XenAPIException e) {
        final String msg = "AttachPrimaryDataStoreCmd add XenAPIException:" + e.toString();
        s_logger.warn(msg, e);
        return new Answer(cmd, false, msg);
    } catch (final Exception e) {
        final String msg = "AttachPrimaryDataStoreCmd failed:" + e.getMessage();
        s_logger.warn(msg, e);
        return new Answer(cmd, false, msg);
    }
}
Also used : CreateObjectAnswer(com.cloud.storage.command.CreateObjectAnswer) IntroduceObjectAnswer(com.cloud.storage.command.IntroduceObjectAnswer) Answer(com.cloud.agent.api.Answer) SnapshotAndCopyAnswer(com.cloud.storage.command.SnapshotAndCopyAnswer) AttachPrimaryDataStoreAnswer(com.cloud.storage.command.AttachPrimaryDataStoreAnswer) DettachAnswer(com.cloud.storage.command.DettachAnswer) CopyCmdAnswer(com.cloud.storage.command.CopyCmdAnswer) AttachAnswer(com.cloud.storage.command.AttachAnswer) AttachPrimaryDataStoreAnswer(com.cloud.storage.command.AttachPrimaryDataStoreAnswer) DecodedDataStore(com.cloud.utils.storage.encoding.DecodedDataStore) Connection(com.xensource.xenapi.Connection) XenAPIException(com.xensource.xenapi.Types.XenAPIException) DecodedDataObject(com.cloud.utils.storage.encoding.DecodedDataObject) 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 60 with SR

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

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)

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