Search in sources :

Example 1 with ManageSnapshotAnswer

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

the class MockStorageManagerImpl method ManageSnapshot.

@Override
public ManageSnapshotAnswer ManageSnapshot(ManageSnapshotCommand cmd) {
    String volPath = cmd.getVolumePath();
    MockVolumeVO volume = _mockVolumeDao.findByStoragePathAndType(volPath);
    if (volume == null) {
        return new ManageSnapshotAnswer(cmd, false, "Can't find the volume");
    }
    MockStoragePoolVO storagePool = _mockStoragePoolDao.findById(volume.getPoolId());
    if (storagePool == null) {
        return new ManageSnapshotAnswer(cmd, false, "Can't find the storage pooll");
    }
    String mountPoint = storagePool.getMountPoint();
    MockVolumeVO snapshot = new MockVolumeVO();
    snapshot.setName(cmd.getSnapshotName());
    snapshot.setPath(mountPoint + cmd.getSnapshotName());
    snapshot.setSize(volume.getSize());
    snapshot.setPoolId(storagePool.getId());
    snapshot.setType(MockVolumeType.SNAPSHOT);
    snapshot.setStatus(Status.DOWNLOADED);
    snapshot = _mockVolumeDao.persist(snapshot);
    return new ManageSnapshotAnswer(cmd, snapshot.getId(), snapshot.getPath(), true, "");
}
Also used : MockStoragePoolVO(com.cloud.simulator.MockStoragePoolVO) ManageSnapshotAnswer(com.cloud.agent.api.ManageSnapshotAnswer) MockVolumeVO(com.cloud.simulator.MockVolumeVO)

Example 2 with ManageSnapshotAnswer

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

the class MockStorageManagerImpl method ManageSnapshot.

@Override
public ManageSnapshotAnswer ManageSnapshot(ManageSnapshotCommand cmd) {
    String volPath = cmd.getVolumePath();
    MockVolumeVO volume = null;
    MockStoragePoolVO storagePool = null;
    TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB);
    try {
        txn.start();
        volume = _mockVolumeDao.findByStoragePathAndType(volPath);
        if (volume == null) {
            return new ManageSnapshotAnswer(cmd, false, "Can't find the volume");
        }
        storagePool = _mockStoragePoolDao.findById(volume.getPoolId());
        if (storagePool == null) {
            return new ManageSnapshotAnswer(cmd, false, "Can't find the storage pooll");
        }
        txn.commit();
    } catch (Exception ex) {
        txn.rollback();
        throw new CloudRuntimeException("Unable to perform snapshot", ex);
    } finally {
        txn.close();
        txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB);
        txn.close();
    }
    String mountPoint = storagePool.getMountPoint();
    MockVolumeVO snapshot = new MockVolumeVO();
    snapshot.setName(cmd.getSnapshotName());
    snapshot.setPath(mountPoint + cmd.getSnapshotName());
    snapshot.setSize(volume.getSize());
    snapshot.setPoolId(storagePool.getId());
    snapshot.setType(MockVolumeType.SNAPSHOT);
    snapshot.setStatus(Status.DOWNLOADED);
    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 saving snapshot " + snapshot, ex);
    } finally {
        txn.close();
        txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB);
        txn.close();
    }
    return new ManageSnapshotAnswer(cmd, snapshot.getId(), snapshot.getPath(), true, "");
}
Also used : TransactionLegacy(com.cloud.utils.db.TransactionLegacy) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) MockStoragePoolVO(com.cloud.simulator.MockStoragePoolVO) ManageSnapshotAnswer(com.cloud.agent.api.ManageSnapshotAnswer) MockVolumeVO(com.cloud.simulator.MockVolumeVO) URISyntaxException(java.net.URISyntaxException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException)

Example 3 with ManageSnapshotAnswer

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

the class LibvirtComputingResource method execute.

protected ManageSnapshotAnswer execute(final ManageSnapshotCommand cmd) {
    String snapshotName = cmd.getSnapshotName();
    String snapshotPath = cmd.getSnapshotPath();
    String vmName = cmd.getVmName();
    try {
        Connect conn = LibvirtConnection.getConnection();
        DomainInfo.DomainState state = null;
        Domain vm = null;
        if (vmName != null) {
            try {
                vm = getDomain(conn, cmd.getVmName());
                state = vm.getInfo().state;
            } catch (LibvirtException e) {
            }
        }
        KVMStoragePool primaryPool = _storagePoolMgr.getStoragePool(cmd.getPool().getUuid());
        KVMPhysicalDisk disk = primaryPool.getPhysicalDisk(cmd.getVolumePath());
        if (state == DomainInfo.DomainState.VIR_DOMAIN_RUNNING && !primaryPool.isExternalSnapshot()) {
            String vmUuid = vm.getUUIDString();
            Object[] args = new Object[] { snapshotName, vmUuid };
            String snapshot = SnapshotXML.format(args);
            s_logger.debug(snapshot);
            if (cmd.getCommandSwitch().equalsIgnoreCase(ManageSnapshotCommand.CREATE_SNAPSHOT)) {
                vm.snapshotCreateXML(snapshot);
            } else {
                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 {
            /* VM is not running, create a snapshot by ourself */
            final Script command = new Script(_manageSnapshotPath, _cmdsTimeout, s_logger);
            if (cmd.getCommandSwitch().equalsIgnoreCase(ManageSnapshotCommand.CREATE_SNAPSHOT)) {
                command.add("-c", disk.getPath());
            } else {
                command.add("-d", snapshotPath);
            }
            command.add("-n", snapshotName);
            String result = command.execute();
            if (result != null) {
                s_logger.debug("Failed to manage snapshot: " + result);
                return new ManageSnapshotAnswer(cmd, false, "Failed to manage snapshot: " + result);
            }
        }
        return new ManageSnapshotAnswer(cmd, cmd.getSnapshotId(), disk.getPath() + File.separator + snapshotName, true, null);
    } catch (LibvirtException e) {
        s_logger.debug("Failed to manage snapshot: " + e.toString());
        return new ManageSnapshotAnswer(cmd, false, "Failed to manage snapshot: " + e.toString());
    }
}
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) ManageSnapshotAnswer(com.cloud.agent.api.ManageSnapshotAnswer) KVMStoragePool(com.cloud.agent.storage.KVMStoragePool) DomainInfo(org.libvirt.DomainInfo) Domain(org.libvirt.Domain)

Example 4 with ManageSnapshotAnswer

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

the class LibvirtManageSnapshotCommandWrapper method execute.

@Override
public Answer execute(final ManageSnapshotCommand command, final LibvirtComputingResource libvirtComputingResource) {
    final String snapshotName = command.getSnapshotName();
    final String snapshotPath = command.getSnapshotPath();
    final String vmName = command.getVmName();
    try {
        final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
        final Connect conn = libvirtUtilitiesHelper.getConnectionByVmName(vmName);
        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 KVMStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr();
        final StorageFilerTO pool = command.getPool();
        final KVMStoragePool primaryPool = storagePoolMgr.getStoragePool(pool.getType(), pool.getUuid());
        final KVMPhysicalDisk disk = primaryPool.getPhysicalDisk(command.getVolumePath());
        if (state == DomainState.VIR_DOMAIN_RUNNING && !primaryPool.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);
            if (command.getCommandSwitch().equalsIgnoreCase(ManageSnapshotCommand.CREATE_SNAPSHOT)) {
                vm.snapshotCreateXML(snapshot);
            } else {
                final DomainSnapshot snap = vm.snapshotLookupByName(snapshotName);
                snap.delete(0);
            }
            /*
                 * 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 {
            /**
                 * For RBD we can't use libvirt to do our snapshotting or any Bash scripts.
                 * libvirt also wants to store the memory contents of the Virtual Machine,
                 * but that's not possible with RBD since there is no way to store the memory
                 * contents in RBD.
                 *
                 * So we rely on the Java bindings for RBD to create our snapshot
                 *
                 * This snapshot might not be 100% consistent due to writes still being in the
                 * memory of the Virtual Machine, but if the VM runs a kernel which supports
                 * barriers properly (>2.6.32) this won't be any different then pulling the power
                 * cord out of a running machine.
                 */
            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(disk.getName());
                    if (command.getCommandSwitch().equalsIgnoreCase(ManageSnapshotCommand.CREATE_SNAPSHOT)) {
                        s_logger.debug("Attempting to create RBD snapshot " + disk.getName() + "@" + snapshotName);
                        image.snapCreate(snapshotName);
                    } else {
                        s_logger.debug("Attempting to remove RBD snapshot " + disk.getName() + "@" + snapshotName);
                        image.snapRemove(snapshotName);
                    }
                    rbd.close(image);
                    r.ioCtxDestroy(io);
                } catch (final Exception e) {
                    s_logger.error("A RBD snapshot operation on " + disk.getName() + " failed. The error was: " + e.getMessage());
                }
            } else {
                /* VM is not running, create a snapshot by ourself */
                final int cmdsTimeout = libvirtComputingResource.getCmdsTimeout();
                final String manageSnapshotPath = libvirtComputingResource.manageSnapshotPath();
                final Script scriptCommand = new Script(manageSnapshotPath, cmdsTimeout, s_logger);
                if (command.getCommandSwitch().equalsIgnoreCase(ManageSnapshotCommand.CREATE_SNAPSHOT)) {
                    scriptCommand.add("-c", disk.getPath());
                } else {
                    scriptCommand.add("-d", snapshotPath);
                }
                scriptCommand.add("-n", snapshotName);
                final String result = scriptCommand.execute();
                if (result != null) {
                    s_logger.debug("Failed to manage snapshot: " + result);
                    return new ManageSnapshotAnswer(command, false, "Failed to manage snapshot: " + result);
                }
            }
        }
        return new ManageSnapshotAnswer(command, command.getSnapshotId(), disk.getPath() + File.separator + snapshotName, true, null);
    } catch (final LibvirtException e) {
        s_logger.debug("Failed to manage snapshot: " + e.toString());
        return new ManageSnapshotAnswer(command, false, "Failed to manage snapshot: " + e.toString());
    }
}
Also used : Script(com.cloud.utils.script.Script) KVMStoragePoolManager(com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager) LibvirtException(org.libvirt.LibvirtException) MessageFormat(java.text.MessageFormat) KVMPhysicalDisk(com.cloud.hypervisor.kvm.storage.KVMPhysicalDisk) Connect(org.libvirt.Connect) DomainSnapshot(org.libvirt.DomainSnapshot) Rados(com.ceph.rados.Rados) ManageSnapshotAnswer(com.cloud.agent.api.ManageSnapshotAnswer) StorageFilerTO(com.cloud.agent.api.to.StorageFilerTO) LibvirtException(org.libvirt.LibvirtException) KVMStoragePool(com.cloud.hypervisor.kvm.storage.KVMStoragePool) DomainState(org.libvirt.DomainInfo.DomainState) Rbd(com.ceph.rbd.Rbd) RbdImage(com.ceph.rbd.RbdImage) IoCTX(com.ceph.rados.IoCTX) Domain(org.libvirt.Domain)

Aggregations

ManageSnapshotAnswer (com.cloud.agent.api.ManageSnapshotAnswer)4 MockStoragePoolVO (com.cloud.simulator.MockStoragePoolVO)2 MockVolumeVO (com.cloud.simulator.MockVolumeVO)2 Script (com.cloud.utils.script.Script)2 Connect (org.libvirt.Connect)2 Domain (org.libvirt.Domain)2 DomainSnapshot (org.libvirt.DomainSnapshot)2 LibvirtException (org.libvirt.LibvirtException)2 IoCTX (com.ceph.rados.IoCTX)1 Rados (com.ceph.rados.Rados)1 Rbd (com.ceph.rbd.Rbd)1 RbdImage (com.ceph.rbd.RbdImage)1 StorageFilerTO (com.cloud.agent.api.to.StorageFilerTO)1 KVMPhysicalDisk (com.cloud.agent.storage.KVMPhysicalDisk)1 KVMStoragePool (com.cloud.agent.storage.KVMStoragePool)1 KVMPhysicalDisk (com.cloud.hypervisor.kvm.storage.KVMPhysicalDisk)1 KVMStoragePool (com.cloud.hypervisor.kvm.storage.KVMStoragePool)1 KVMStoragePoolManager (com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager)1 TransactionLegacy (com.cloud.utils.db.TransactionLegacy)1 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)1