Search in sources :

Example 6 with BackupSnapshotAnswer

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

the class MockStorageManagerImpl method BackupSnapshot.

@Override
public BackupSnapshotAnswer BackupSnapshot(BackupSnapshotCommand cmd, SimulatorInfo info) {
    // emulate xenserver backupsnapshot, if the base volume is deleted, then
    // backupsnapshot failed
    MockVolumeVO volume = null;
    MockVolumeVO snapshot = null;
    MockSecStorageVO secStorage = null;
    TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB);
    try {
        txn.start();
        volume = _mockVolumeDao.findByStoragePathAndType(cmd.getVolumePath());
        if (volume == null) {
            return new BackupSnapshotAnswer(cmd, false, "Can't find base volume: " + cmd.getVolumePath(), null, true);
        }
        String snapshotPath = cmd.getSnapshotUuid();
        snapshot = _mockVolumeDao.findByStoragePathAndType(snapshotPath);
        if (snapshot == null) {
            return new BackupSnapshotAnswer(cmd, false, "can't find snapshot" + snapshotPath, null, true);
        }
        String secStorageUrl = cmd.getSecondaryStorageUrl();
        secStorage = _mockSecStorageDao.findByUrl(secStorageUrl);
        if (secStorage == null) {
            return new BackupSnapshotAnswer(cmd, false, "can't find sec storage" + snapshotPath, null, true);
        }
        txn.commit();
    } catch (Exception ex) {
        txn.rollback();
        throw new CloudRuntimeException("Error when backing up snapshot");
    } finally {
        txn.close();
        txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB);
        txn.close();
    }
    MockVolumeVO newsnapshot = new MockVolumeVO();
    String name = UUID.randomUUID().toString();
    newsnapshot.setName(name);
    newsnapshot.setPath(secStorage.getMountPoint() + name);
    newsnapshot.setPoolId(secStorage.getId());
    newsnapshot.setSize(snapshot.getSize());
    newsnapshot.setStatus(Status.DOWNLOADED);
    newsnapshot.setType(MockVolumeType.SNAPSHOT);
    txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB);
    try {
        txn.start();
        snapshot = _mockVolumeDao.persist(snapshot);
        txn.commit();
    } catch (Exception ex) {
        txn.rollback();
        throw new CloudRuntimeException("Error when backing up snapshot " + newsnapshot, ex);
    } finally {
        txn.close();
        txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB);
        txn.close();
    }
    return new BackupSnapshotAnswer(cmd, true, null, newsnapshot.getName(), true);
}
Also used : TransactionLegacy(com.cloud.utils.db.TransactionLegacy) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) BackupSnapshotAnswer(com.cloud.agent.api.BackupSnapshotAnswer) MockVolumeVO(com.cloud.simulator.MockVolumeVO) MockSecStorageVO(com.cloud.simulator.MockSecStorageVO) URISyntaxException(java.net.URISyntaxException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException)

Example 7 with BackupSnapshotAnswer

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

the class VmwareStorageManagerImpl method execute.

@Override
@Deprecated
public Answer execute(VmwareHostService hostService, BackupSnapshotCommand cmd) {
    Long accountId = cmd.getAccountId();
    Long volumeId = cmd.getVolumeId();
    String secondaryStorageUrl = cmd.getSecondaryStorageUrl();
    // not null: Precondition.
    String snapshotUuid = cmd.getSnapshotUuid();
    String prevSnapshotUuid = cmd.getPrevSnapshotUuid();
    String prevBackupUuid = cmd.getPrevBackupUuid();
    String searchExcludedFolders = cmd.getContextParam("searchexludefolders");
    VirtualMachineMO workerVm = null;
    String workerVMName = null;
    String volumePath = cmd.getVolumePath();
    ManagedObjectReference morDs = null;
    DatastoreMO dsMo = null;
    // By default assume failure
    String details = null;
    boolean success = false;
    String snapshotBackupUuid = null;
    VmwareContext context = hostService.getServiceContext(cmd);
    VirtualMachineMO vmMo = null;
    try {
        VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, cmd);
        morDs = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, cmd.getPool().getUuid());
        try {
            vmMo = hyperHost.findVmOnHyperHost(cmd.getVmName());
            if (vmMo == null) {
                if (s_logger.isDebugEnabled()) {
                    s_logger.debug("Unable to find owner VM for BackupSnapshotCommand on host " + hyperHost.getHyperHostName() + ", will try within datacenter");
                }
                vmMo = hyperHost.findVmOnPeerHyperHost(cmd.getVmName());
                if (vmMo == null) {
                    dsMo = new DatastoreMO(hyperHost.getContext(), morDs);
                    workerVMName = hostService.getWorkerName(context, cmd, 0, dsMo);
                    vmMo = HypervisorHostHelper.createWorkerVM(hyperHost, dsMo, workerVMName, null);
                    if (vmMo == null) {
                        throw new Exception("Failed to find the newly create or relocated VM. vmName: " + workerVMName);
                    }
                    workerVm = vmMo;
                    // attach volume to worker VM
                    String datastoreVolumePath = getVolumePathInDatastore(dsMo, volumePath + ".vmdk", searchExcludedFolders);
                    vmMo.attachDisk(new String[] { datastoreVolumePath }, morDs);
                }
            }
            if (!vmMo.createSnapshot(snapshotUuid, "Snapshot taken for " + cmd.getSnapshotName(), false, false)) {
                throw new Exception("Failed to take snapshot " + cmd.getSnapshotName() + " on vm: " + cmd.getVmName());
            }
            snapshotBackupUuid = backupSnapshotToSecondaryStorage(vmMo, accountId, volumeId, cmd.getVolumePath(), snapshotUuid, secondaryStorageUrl, prevSnapshotUuid, prevBackupUuid, hostService.getWorkerName(context, cmd, 1, dsMo), cmd.getNfsVersion());
            success = (snapshotBackupUuid != null);
            if (success) {
                details = "Successfully backedUp the snapshotUuid: " + snapshotUuid + " to secondary storage.";
            }
        } finally {
            if (vmMo != null) {
                ManagedObjectReference snapshotMor = vmMo.getSnapshotMor(snapshotUuid);
                if (snapshotMor != null) {
                    vmMo.removeSnapshot(snapshotUuid, false);
                }
            }
            try {
                if (workerVm != null) {
                    workerVm.detachAllDisksAndDestroy();
                }
            } catch (Throwable e) {
                s_logger.warn(String.format("Failed to destroy worker VM [%s] due to: [%s].", workerVMName, e.getMessage()), e);
            }
        }
    } catch (Throwable e) {
        return new BackupSnapshotAnswer(cmd, false, hostService.createLogMessageException(e, cmd), snapshotBackupUuid, true);
    }
    return new BackupSnapshotAnswer(cmd, success, details, snapshotBackupUuid, true);
}
Also used : VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) VirtualMachineMO(com.cloud.hypervisor.vmware.mo.VirtualMachineMO) BackupSnapshotAnswer(com.cloud.agent.api.BackupSnapshotAnswer) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) DatastoreMO(com.cloud.hypervisor.vmware.mo.DatastoreMO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 8 with BackupSnapshotAnswer

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

the class VmwareResource method execute.

protected Answer execute(BackupSnapshotCommand cmd) {
    try {
        VmwareContext context = getServiceContext();
        VmwareManager mgr = context.getStockObject(VmwareManager.CONTEXT_STOCK_NAME);
        return mgr.getStorageManager().execute(this, cmd);
    } catch (Throwable e) {
        return new BackupSnapshotAnswer(cmd, false, createLogMessageException(e, cmd), null, true);
    }
}
Also used : VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) VmwareManager(com.cloud.hypervisor.vmware.manager.VmwareManager) BackupSnapshotAnswer(com.cloud.agent.api.BackupSnapshotAnswer)

Example 9 with BackupSnapshotAnswer

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

the class LibvirtBackupSnapshotCommandWrapper method execute.

@Override
public Answer execute(final BackupSnapshotCommand command, final LibvirtComputingResource libvirtComputingResource) {
    final Long dcId = command.getDataCenterId();
    final Long accountId = command.getAccountId();
    final Long volumeId = command.getVolumeId();
    final String secondaryStoragePoolUrl = command.getSecondaryStorageUrl();
    final String snapshotName = command.getSnapshotName();
    String snapshotDestPath = null;
    String snapshotRelPath = null;
    final String vmName = command.getVmName();
    KVMStoragePool secondaryStoragePool = null;
    final KVMStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr();
    try {
        final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
        final Connect conn = libvirtUtilitiesHelper.getConnectionByVmName(vmName);
        secondaryStoragePool = storagePoolMgr.getStoragePoolByURI(secondaryStoragePoolUrl);
        final String ssPmountPath = secondaryStoragePool.getLocalPath();
        snapshotRelPath = File.separator + "snapshots" + File.separator + dcId + File.separator + accountId + File.separator + volumeId;
        snapshotDestPath = ssPmountPath + File.separator + "snapshots" + File.separator + dcId + File.separator + accountId + File.separator + volumeId;
        final KVMStoragePool primaryPool = storagePoolMgr.getStoragePool(command.getPool().getType(), command.getPrimaryStoragePoolNameLabel());
        final KVMPhysicalDisk snapshotDisk = primaryPool.getPhysicalDisk(command.getVolumePath());
        final String manageSnapshotPath = libvirtComputingResource.manageSnapshotPath();
        final int cmdsTimeout = libvirtComputingResource.getCmdsTimeout();
        /**
         * RBD snapshots can't be copied using qemu-img, so we have to use
         * the Java bindings for librbd here.
         *
         * These bindings will read the snapshot and write the contents to
         * the secondary storage directly
         *
         * It will stop doing so if the amount of time spend is longer then
         * cmds.timeout
         */
        if (primaryPool.getType() == StoragePoolType.RBD) {
            try {
                final Rados r = new Rados(primaryPool.getAuthUserName());
                r.confSet("mon_host", primaryPool.getSourceHost() + ":" + primaryPool.getSourcePort());
                r.confSet("key", primaryPool.getAuthSecret());
                r.confSet("client_mount_timeout", "30");
                r.connect();
                s_logger.debug("Succesfully connected to Ceph cluster at " + r.confGet("mon_host"));
                final IoCTX io = r.ioCtxCreate(primaryPool.getSourceDir());
                final Rbd rbd = new Rbd(io);
                final RbdImage image = rbd.open(snapshotDisk.getName(), snapshotName);
                final File fh = new File(snapshotDestPath);
                try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(fh))) {
                    final int chunkSize = 4194304;
                    long offset = 0;
                    s_logger.debug("Backuping up RBD snapshot " + snapshotName + " to  " + snapshotDestPath);
                    while (true) {
                        final byte[] buf = new byte[chunkSize];
                        final int bytes = image.read(offset, buf, chunkSize);
                        if (bytes <= 0) {
                            break;
                        }
                        bos.write(buf, 0, bytes);
                        offset += bytes;
                    }
                    s_logger.debug("Completed backing up RBD snapshot " + snapshotName + " to  " + snapshotDestPath + ". Bytes written: " + toHumanReadableSize(offset));
                } catch (final IOException ex) {
                    s_logger.error("BackupSnapshotAnswer:Exception:" + ex.getMessage());
                }
                r.ioCtxDestroy(io);
            } catch (final RadosException e) {
                s_logger.error("A RADOS operation failed. The error was: " + e.getMessage());
                return new BackupSnapshotAnswer(command, false, e.toString(), null, true);
            } catch (final RbdException e) {
                s_logger.error("A RBD operation on " + snapshotDisk.getName() + " failed. The error was: " + e.getMessage());
                return new BackupSnapshotAnswer(command, false, e.toString(), null, true);
            }
        } else {
            final Script scriptCommand = new Script(manageSnapshotPath, cmdsTimeout, s_logger);
            scriptCommand.add("-b", snapshotDisk.getPath());
            scriptCommand.add("-n", snapshotName);
            scriptCommand.add("-p", snapshotDestPath);
            scriptCommand.add("-t", snapshotName);
            final String result = scriptCommand.execute();
            if (result != null) {
                s_logger.debug("Failed to backup snaptshot: " + result);
                return new BackupSnapshotAnswer(command, false, result, null, true);
            }
        }
        /* Delete the snapshot on primary */
        DomainState state = null;
        Domain vm = null;
        if (vmName != null) {
            try {
                vm = libvirtComputingResource.getDomain(conn, command.getVmName());
                state = vm.getInfo().state;
            } catch (final LibvirtException e) {
                s_logger.trace("Ignoring libvirt error.", e);
            }
        }
        final KVMStoragePool primaryStorage = storagePoolMgr.getStoragePool(command.getPool().getType(), command.getPool().getUuid());
        if (state == DomainState.VIR_DOMAIN_RUNNING && !primaryStorage.isExternalSnapshot()) {
            final MessageFormat snapshotXML = new MessageFormat("   <domainsnapshot>" + "       <name>{0}</name>" + "          <domain>" + "            <uuid>{1}</uuid>" + "        </domain>" + "    </domainsnapshot>");
            final String vmUuid = vm.getUUIDString();
            final Object[] args = new Object[] { snapshotName, vmUuid };
            final String snapshot = snapshotXML.format(args);
            s_logger.debug(snapshot);
            final DomainSnapshot snap = vm.snapshotLookupByName(snapshotName);
            if (snap != null) {
                snap.delete(0);
            } else {
                throw new CloudRuntimeException("Unable to find vm snapshot with name -" + snapshotName);
            }
            /*
                 * libvirt on RHEL6 doesn't handle resume event emitted from
                 * qemu
                 */
            vm = libvirtComputingResource.getDomain(conn, command.getVmName());
            state = vm.getInfo().state;
            if (state == DomainState.VIR_DOMAIN_PAUSED) {
                vm.resume();
            }
        } else {
            final Script scriptCommand = new Script(manageSnapshotPath, cmdsTimeout, s_logger);
            scriptCommand.add("-d", snapshotDisk.getPath());
            scriptCommand.add("-n", snapshotName);
            final String result = scriptCommand.execute();
            if (result != null) {
                s_logger.debug("Failed to backup snapshot: " + result);
                return new BackupSnapshotAnswer(command, false, "Failed to backup snapshot: " + result, null, true);
            }
        }
    } catch (final LibvirtException e) {
        return new BackupSnapshotAnswer(command, false, e.toString(), null, true);
    } catch (final CloudRuntimeException e) {
        return new BackupSnapshotAnswer(command, false, e.toString(), null, true);
    } finally {
        if (secondaryStoragePool != null) {
            storagePoolMgr.deleteStoragePool(secondaryStoragePool.getType(), secondaryStoragePool.getUuid());
        }
    }
    return new BackupSnapshotAnswer(command, true, null, snapshotRelPath + File.separator + snapshotName, true);
}
Also used : LibvirtException(org.libvirt.LibvirtException) RadosException(com.ceph.rados.exceptions.RadosException) KVMStoragePool(com.cloud.hypervisor.kvm.storage.KVMStoragePool) Rbd(com.ceph.rbd.Rbd) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) BufferedOutputStream(java.io.BufferedOutputStream) Script(com.cloud.utils.script.Script) KVMStoragePoolManager(com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager) MessageFormat(java.text.MessageFormat) KVMPhysicalDisk(com.cloud.hypervisor.kvm.storage.KVMPhysicalDisk) Connect(org.libvirt.Connect) Rados(com.ceph.rados.Rados) DomainSnapshot(org.libvirt.DomainSnapshot) IOException(java.io.IOException) DomainState(org.libvirt.DomainInfo.DomainState) FileOutputStream(java.io.FileOutputStream) RbdImage(com.ceph.rbd.RbdImage) IoCTX(com.ceph.rados.IoCTX) BackupSnapshotAnswer(com.cloud.agent.api.BackupSnapshotAnswer) Domain(org.libvirt.Domain) File(java.io.File) RbdException(com.ceph.rbd.RbdException)

Aggregations

BackupSnapshotAnswer (com.cloud.agent.api.BackupSnapshotAnswer)9 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)5 Script (com.cloud.utils.script.Script)3 Connect (org.libvirt.Connect)3 Domain (org.libvirt.Domain)3 DomainSnapshot (org.libvirt.DomainSnapshot)3 LibvirtException (org.libvirt.LibvirtException)3 IoCTX (com.ceph.rados.IoCTX)2 Rados (com.ceph.rados.Rados)2 RadosException (com.ceph.rados.exceptions.RadosException)2 Rbd (com.ceph.rbd.Rbd)2 RbdException (com.ceph.rbd.RbdException)2 RbdImage (com.ceph.rbd.RbdImage)2 BackupSnapshotCommand (com.cloud.agent.api.BackupSnapshotCommand)2 VmwareContext (com.cloud.hypervisor.vmware.util.VmwareContext)2 MockSecStorageVO (com.cloud.simulator.MockSecStorageVO)2 MockVolumeVO (com.cloud.simulator.MockVolumeVO)2 StoragePool (com.cloud.storage.StoragePool)2 BufferedOutputStream (java.io.BufferedOutputStream)2 File (java.io.File)2