Search in sources :

Example 1 with BackupSnapshotAnswer

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

the class BackupSnapshotAnswerTest method setUp.

@Before
public void setUp() {
    StoragePool pool = Mockito.mock(StoragePool.class);
    bsc = new BackupSnapshotCommand("secondaryStoragePoolURL", 101L, 102L, 103L, 104L, 105L, "volumePath", pool, "snapshotUuid", "snapshotName", "prevSnapshotUuid", "prevBackupUuid", false, "vmName", 5);
    bsa = new BackupSnapshotAnswer(bsc, true, "results", "bussname", false);
}
Also used : BackupSnapshotCommand(com.cloud.agent.api.BackupSnapshotCommand) StoragePool(com.cloud.storage.StoragePool) BackupSnapshotAnswer(com.cloud.agent.api.BackupSnapshotAnswer) Before(org.junit.Before)

Example 2 with BackupSnapshotAnswer

use of com.cloud.agent.api.BackupSnapshotAnswer in project CloudStack-archive by CloudStack-extras.

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 = _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();
    MockVolumeVO snapshot = _mockVolumeDao.findByStoragePathAndType(snapshotPath);
    if (snapshot == null) {
        return new BackupSnapshotAnswer(cmd, false, "can't find snapshot" + snapshotPath, null, true);
    }
    String secStorageUrl = cmd.getSecondaryStorageUrl();
    MockSecStorageVO secStorage = _mockSecStorageDao.findByUrl(secStorageUrl);
    if (secStorage == null) {
        return new BackupSnapshotAnswer(cmd, false, "can't find sec storage" + snapshotPath, null, true);
    }
    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);
    newsnapshot = _mockVolumeDao.persist(newsnapshot);
    return new BackupSnapshotAnswer(cmd, true, null, newsnapshot.getName(), true);
}
Also used : BackupSnapshotAnswer(com.cloud.agent.api.BackupSnapshotAnswer) MockVolumeVO(com.cloud.simulator.MockVolumeVO) MockSecStorageVO(com.cloud.simulator.MockSecStorageVO)

Example 3 with BackupSnapshotAnswer

use of com.cloud.agent.api.BackupSnapshotAnswer in project CloudStack-archive by CloudStack-extras.

the class LibvirtComputingResource method execute.

protected BackupSnapshotAnswer execute(final BackupSnapshotCommand cmd) {
    Long dcId = cmd.getDataCenterId();
    Long accountId = cmd.getAccountId();
    Long volumeId = cmd.getVolumeId();
    String secondaryStoragePoolUrl = cmd.getSecondaryStorageUrl();
    String snapshotName = cmd.getSnapshotName();
    String snapshotPath = cmd.getVolumePath();
    String snapshotDestPath = null;
    String snapshotRelPath = null;
    String vmName = cmd.getVmName();
    KVMStoragePool secondaryStoragePool = null;
    try {
        Connect conn = LibvirtConnection.getConnection();
        secondaryStoragePool = _storagePoolMgr.getStoragePoolByURI(secondaryStoragePoolUrl);
        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;
        KVMStoragePool primaryPool = _storagePoolMgr.getStoragePool(cmd.getPrimaryStoragePoolNameLabel());
        KVMPhysicalDisk snapshotDisk = primaryPool.getPhysicalDisk(cmd.getVolumePath());
        Script command = new Script(_manageSnapshotPath, _cmdsTimeout, s_logger);
        command.add("-b", snapshotDisk.getPath());
        command.add("-n", snapshotName);
        command.add("-p", snapshotDestPath);
        command.add("-t", snapshotName);
        String result = command.execute();
        if (result != null) {
            s_logger.debug("Failed to backup snaptshot: " + result);
            return new BackupSnapshotAnswer(cmd, false, result, null, true);
        }
        /* Delete the snapshot on primary */
        DomainInfo.DomainState state = null;
        Domain vm = null;
        if (vmName != null) {
            try {
                vm = getDomain(conn, cmd.getVmName());
                state = vm.getInfo().state;
            } catch (LibvirtException e) {
            }
        }
        KVMStoragePool primaryStorage = _storagePoolMgr.getStoragePool(cmd.getPool().getUuid());
        if (state == DomainInfo.DomainState.VIR_DOMAIN_RUNNING && !primaryStorage.isExternalSnapshot()) {
            String vmUuid = vm.getUUIDString();
            Object[] args = new Object[] { snapshotName, vmUuid };
            String snapshot = SnapshotXML.format(args);
            s_logger.debug(snapshot);
            DomainSnapshot snap = vm.snapshotLookupByName(snapshotName);
            snap.delete(0);
            /*
				 * libvirt on RHEL6 doesn't handle resume event emitted from
				 * qemu
				 */
            vm = getDomain(conn, cmd.getVmName());
            state = vm.getInfo().state;
            if (state == DomainInfo.DomainState.VIR_DOMAIN_PAUSED) {
                vm.resume();
            }
        } else {
            command = new Script(_manageSnapshotPath, _cmdsTimeout, s_logger);
            command.add("-d", snapshotDisk.getPath());
            command.add("-n", snapshotName);
            result = command.execute();
            if (result != null) {
                s_logger.debug("Failed to backup snapshot: " + result);
                return new BackupSnapshotAnswer(cmd, false, "Failed to backup snapshot: " + result, null, true);
            }
        }
    } catch (LibvirtException e) {
        return new BackupSnapshotAnswer(cmd, false, e.toString(), null, true);
    } catch (CloudRuntimeException e) {
        return new BackupSnapshotAnswer(cmd, false, e.toString(), null, true);
    } finally {
        if (secondaryStoragePool != null) {
            secondaryStoragePool.delete();
        }
    }
    return new BackupSnapshotAnswer(cmd, true, null, snapshotRelPath + File.separator + snapshotName, true);
}
Also used : Script(com.cloud.utils.script.Script) LibvirtException(org.libvirt.LibvirtException) KVMPhysicalDisk(com.cloud.agent.storage.KVMPhysicalDisk) Connect(org.libvirt.Connect) DomainSnapshot(org.libvirt.DomainSnapshot) KVMStoragePool(com.cloud.agent.storage.KVMStoragePool) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) BackupSnapshotAnswer(com.cloud.agent.api.BackupSnapshotAnswer) DomainInfo(org.libvirt.DomainInfo) Domain(org.libvirt.Domain)

Example 4 with BackupSnapshotAnswer

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

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();
        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: " + 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 : KvmStoragePool(com.cloud.hypervisor.kvm.storage.KvmStoragePool) LibvirtException(org.libvirt.LibvirtException) RadosException(com.ceph.rados.exceptions.RadosException) Rbd(com.ceph.rbd.Rbd) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) KvmStoragePoolManager(com.cloud.hypervisor.kvm.storage.KvmStoragePoolManager) KvmPhysicalDisk(com.cloud.hypervisor.kvm.storage.KvmPhysicalDisk) BufferedOutputStream(java.io.BufferedOutputStream) Script(com.cloud.utils.script.Script) MessageFormat(java.text.MessageFormat) 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)

Example 5 with BackupSnapshotAnswer

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

the class BackupSnapshotAnswerTest method setUp.

@Before
public void setUp() {
    final StoragePool pool = Mockito.mock(StoragePool.class);
    bsc = new BackupSnapshotCommand("secondaryStoragePoolURL", 101L, 102L, 103L, 104L, 105L, "volumePath", pool, "snapshotUuid", "snapshotName", "prevSnapshotUuid", "prevBackupUuid", false, "vmName", 5);
    bsa = new BackupSnapshotAnswer(bsc, true, "results", "bussname", false);
}
Also used : BackupSnapshotCommand(com.cloud.agent.api.BackupSnapshotCommand) StoragePool(com.cloud.storage.StoragePool) BackupSnapshotAnswer(com.cloud.agent.api.BackupSnapshotAnswer) Before(org.junit.Before)

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