Search in sources :

Example 61 with NfsTO

use of com.cloud.agent.api.to.NfsTO in project cosmic by MissionCriticalCloud.

the class LibvirtRevertSnapshotCommandWrapper method execute.

@Override
public Answer execute(final RevertSnapshotCommand command, final LibvirtComputingResource libvirtComputingResource) {
    final SnapshotObjectTO snapshot = command.getData();
    final VolumeObjectTO volume = snapshot.getVolume();
    final PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO) volume.getDataStore();
    final DataStoreTO snapshotImageStore = snapshot.getDataStore();
    if (!(snapshotImageStore instanceof NfsTO)) {
        return new Answer(command, false, "revert snapshot on object storage is not implemented yet");
    }
    final NfsTO nfsImageStore = (NfsTO) snapshotImageStore;
    final String secondaryStoragePoolUrl = nfsImageStore.getUrl();
    final String volumePath = volume.getPath();
    String snapshotPath = null;
    String snapshotRelPath = null;
    KvmStoragePool secondaryStoragePool = null;
    try {
        final KvmStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr();
        secondaryStoragePool = storagePoolMgr.getStoragePoolByUri(secondaryStoragePoolUrl);
        final String ssPmountPath = secondaryStoragePool.getLocalPath();
        snapshotRelPath = snapshot.getPath();
        snapshotPath = ssPmountPath + File.separator + snapshotRelPath;
        final KvmPhysicalDisk snapshotDisk = storagePoolMgr.getPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), volumePath);
        final KvmStoragePool primaryPool = snapshotDisk.getPool();
        if (primaryPool.getType() == StoragePoolType.RBD) {
            return new Answer(command, false, "revert snapshot to RBD is not implemented yet");
        } else {
            final Script cmd = new Script(libvirtComputingResource.manageSnapshotPath(), libvirtComputingResource.getCmdsTimeout(), s_logger);
            cmd.add("-v", snapshotPath);
            cmd.add("-n", snapshotDisk.getName());
            cmd.add("-p", snapshotDisk.getPath());
            final String result = cmd.execute();
            if (result != null) {
                s_logger.debug("Failed to revert snaptshot: " + result);
                return new Answer(command, false, result);
            }
        }
        return new Answer(command, true, "RevertSnapshotCommand executes successfully");
    } catch (final CloudRuntimeException e) {
        return new Answer(command, false, e.toString());
    }
}
Also used : SnapshotObjectTO(com.cloud.storage.to.SnapshotObjectTO) Answer(com.cloud.agent.api.Answer) Script(com.cloud.utils.script.Script) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) PrimaryDataStoreTO(com.cloud.storage.to.PrimaryDataStoreTO) KvmStoragePool(com.cloud.hypervisor.kvm.storage.KvmStoragePool) PrimaryDataStoreTO(com.cloud.storage.to.PrimaryDataStoreTO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VolumeObjectTO(com.cloud.storage.to.VolumeObjectTO) KvmStoragePoolManager(com.cloud.hypervisor.kvm.storage.KvmStoragePoolManager) KvmPhysicalDisk(com.cloud.hypervisor.kvm.storage.KvmPhysicalDisk) NfsTO(com.cloud.agent.api.to.NfsTO)

Example 62 with NfsTO

use of com.cloud.agent.api.to.NfsTO 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 Volume.Type type = volume.getType();
    if (type == Volume.Type.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.agent.api.to.DataStoreTO) XenAPIException(com.xensource.xenapi.Types.XenAPIException) URISyntaxException(java.net.URISyntaxException) NfsTO(com.cloud.agent.api.to.NfsTO) URI(java.net.URI) XenAPIException(com.xensource.xenapi.Types.XenAPIException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) 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) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) DataTO(com.cloud.agent.api.to.DataTO) Volume(com.cloud.storage.Volume) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VolumeObjectTO(com.cloud.storage.to.VolumeObjectTO) VDI(com.xensource.xenapi.VDI) TemplateObjectTO(com.cloud.storage.to.TemplateObjectTO) SR(com.xensource.xenapi.SR)

Example 63 with NfsTO

use of com.cloud.agent.api.to.NfsTO in project cosmic by MissionCriticalCloud.

the class XenServerStorageProcessor method attachIso.

@Override
public AttachAnswer attachIso(final AttachCommand 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();
    }
    final String vmName = cmd.getVmName();
    try {
        final Connection conn = hypervisorResource.getConnection();
        VBD isoVBD = null;
        // Find the VM
        final VM vm = hypervisorResource.getVM(conn, vmName);
        // Find the ISO VDI
        final VDI isoVDI = hypervisorResource.getIsoVDIByURL(conn, vmName, isoURL);
        // Find the VM's CD-ROM VBD
        final Set<VBD> vbds = vm.getVBDs(conn);
        for (final VBD vbd : vbds) {
            final String userDevice = vbd.getUserdevice(conn);
            final Types.VbdType type = vbd.getType(conn);
            if (userDevice.equals("3") && type == Types.VbdType.CD) {
                isoVBD = vbd;
                break;
            }
        }
        if (isoVBD == null) {
            throw new CloudRuntimeException("Unable to find CD-ROM VBD for VM: " + vmName);
        } else {
            // If an ISO is already inserted, eject it
            if (!isoVBD.getEmpty(conn)) {
                isoVBD.eject(conn);
            }
            // Insert the new ISO
            isoVBD.insert(conn, isoVDI);
        }
        return new AttachAnswer(disk);
    } catch (final XenAPIException e) {
        s_logger.warn("Failed to attach iso" + ": " + e.toString(), e);
        return new AttachAnswer(e.toString());
    } catch (final Exception e) {
        s_logger.warn("Failed to attach iso" + ": " + e.toString(), e);
        return new AttachAnswer(e.toString());
    }
}
Also used : Types(com.xensource.xenapi.Types) PrimaryDataStoreTO(com.cloud.storage.to.PrimaryDataStoreTO) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) 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) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) 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)

Example 64 with NfsTO

use of com.cloud.agent.api.to.NfsTO in project cosmic by MissionCriticalCloud.

the class XenServerStorageProcessor method createTemplateFromSnapshot.

@Override
public Answer createTemplateFromSnapshot(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;
    }
    final String userSpecifiedTemplateName = templateObjTO.getName();
    NfsTO destStore = null;
    URI destUri = null;
    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);
        srcSr = hypervisorResource.getIscsiSR(conn, iScsiName, storageHost, iScsiName, chapInitiatorUsername, chapInitiatorSecret, true);
        final String destNfsPath = destUri.getHost() + ":" + destUri.getPath();
        if (!hypervisorResource.createSecondaryStorageFolder(conn, destNfsPath, destDir)) {
            final String details = " Failed to create folder " + destDir + " in secondary storage";
            s_logger.warn(details);
            return new CopyCmdAnswer(details);
        }
        final URI templateUri = new URI(destStore.getUrl() + "/" + destDir);
        destSr = hypervisorResource.createNfsSRbyURI(conn, templateUri, false);
        // there should only be one VDI in this SR
        final VDI srcVdi = srcSr.getVDIs(conn).iterator().next();
        destVdi = srcVdi.copy(conn, destSr);
        // scan makes XenServer pick up VDI physicalSize
        destSr.scan(conn);
        if (userSpecifiedTemplateName != null) {
            destVdi.setNameLabel(conn, userSpecifiedTemplateName);
        }
        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, userSpecifiedTemplateName, null, physicalSize, virtualSize, templateObjTO.getId());
        if (!result) {
            throw new CloudRuntimeException("Could not create the template.properties file on secondary storage dir: " + templateUri);
        }
        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 Exception ex) {
        s_logger.error("Failed to create a template from a snapshot", ex);
        return new CopyCmdAnswer("Failed to create a template from a snapshot: " + ex.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(com.cloud.storage.to.SnapshotObjectTO) 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) PrimaryDataStoreTO(com.cloud.storage.to.PrimaryDataStoreTO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VDI(com.xensource.xenapi.VDI) TemplateObjectTO(com.cloud.storage.to.TemplateObjectTO) CopyCmdAnswer(com.cloud.storage.command.CopyCmdAnswer) SR(com.xensource.xenapi.SR)

Example 65 with NfsTO

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

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