Search in sources :

Example 36 with SnapshotObjectTO

use of org.apache.cloudstack.storage.to.SnapshotObjectTO in project cloudstack by apache.

the class Ovm3StorageProcessorTest method createSnapshotObjectCommandTest.

@Test
public void createSnapshotObjectCommandTest() throws ConfigurationException {
    con = prepare();
    String snapuuid = ovmObject.newUuid();
    SnapshotObjectTO snap = snapshot(snapuuid, linux.getRepoId(), "", linux.getVirtualDisksDir());
    String response = storageplugin.getFileCreateXml().replace(storageplugin.getFileName(), snapuuid + ".raw");
    response = response.replace(storageplugin.getPoolUuid(), ovmObject.deDash(linux.getRepoId()));
    con.setMethodResponse("storage_plugin_create", results.simpleResponseWrapWrapper(response));
    CreateObjectCommand create = new CreateObjectCommand(snap);
    Answer ra = hypervisor.executeRequest(create);
    results.basicBooleanTest(ra.getResult(), false);
}
Also used : SnapshotObjectTO(org.apache.cloudstack.storage.to.SnapshotObjectTO) Answer(com.cloud.agent.api.Answer) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer) CreateObjectCommand(org.apache.cloudstack.storage.command.CreateObjectCommand) ConnectionTest(com.cloud.hypervisor.ovm3.objects.ConnectionTest) LinuxTest(com.cloud.hypervisor.ovm3.objects.LinuxTest) Test(org.junit.Test) XenTest(com.cloud.hypervisor.ovm3.objects.XenTest) StoragePluginTest(com.cloud.hypervisor.ovm3.objects.StoragePluginTest) Ovm3SupportTest(com.cloud.hypervisor.ovm3.support.Ovm3SupportTest) XmlTestResultTest(com.cloud.hypervisor.ovm3.objects.XmlTestResultTest) Ovm3ConfigurationTest(com.cloud.hypervisor.ovm3.resources.helpers.Ovm3ConfigurationTest)

Example 37 with SnapshotObjectTO

use of org.apache.cloudstack.storage.to.SnapshotObjectTO in project cloudstack by apache.

the class LibvirtRevertSnapshotCommandWrapper method execute.

@Override
public Answer execute(final RevertSnapshotCommand command, final LibvirtComputingResource libvirtComputingResource) {
    SnapshotObjectTO snapshot = command.getData();
    VolumeObjectTO volume = snapshot.getVolume();
    PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO) volume.getDataStore();
    DataStoreTO snapshotImageStore = snapshot.getDataStore();
    if (!(snapshotImageStore instanceof NfsTO)) {
        return new Answer(command, false, "revert snapshot on object storage is not implemented yet");
    }
    NfsTO nfsImageStore = (NfsTO) snapshotImageStore;
    String secondaryStoragePoolUrl = nfsImageStore.getUrl();
    String volumePath = volume.getPath();
    String snapshotPath = null;
    String snapshotRelPath = null;
    KVMStoragePool secondaryStoragePool = null;
    try {
        final KVMStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr();
        secondaryStoragePool = storagePoolMgr.getStoragePoolByURI(secondaryStoragePoolUrl);
        String ssPmountPath = secondaryStoragePool.getLocalPath();
        snapshotRelPath = snapshot.getPath();
        snapshotPath = ssPmountPath + File.separator + snapshotRelPath;
        KVMPhysicalDisk snapshotDisk = storagePoolMgr.getPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), volumePath);
        KVMStoragePool primaryPool = snapshotDisk.getPool();
        if (primaryPool.getType() == StoragePoolType.RBD) {
            return new Answer(command, false, "revert snapshot to RBD is not implemented yet");
        } else {
            Script cmd = new Script(libvirtComputingResource.manageSnapshotPath(), libvirtComputingResource.getCmdsTimeout(), s_logger);
            cmd.add("-v", snapshotPath);
            cmd.add("-n", snapshotDisk.getName());
            cmd.add("-p", snapshotDisk.getPath());
            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 (CloudRuntimeException e) {
        return new Answer(command, false, e.toString());
    }
}
Also used : SnapshotObjectTO(org.apache.cloudstack.storage.to.SnapshotObjectTO) Answer(com.cloud.agent.api.Answer) Script(com.cloud.utils.script.Script) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) KVMStoragePoolManager(com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager) KVMStoragePool(com.cloud.hypervisor.kvm.storage.KVMStoragePool) PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) KVMPhysicalDisk(com.cloud.hypervisor.kvm.storage.KVMPhysicalDisk) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) NfsTO(com.cloud.agent.api.to.NfsTO)

Example 38 with SnapshotObjectTO

use of org.apache.cloudstack.storage.to.SnapshotObjectTO in project cloudstack by apache.

the class Ovm3StorageProcessor method createSnapshot.

/**
     * Creates a snapshot from a volume, but only if the VM is stopped.
     * This due qemu not being able to snap raw volumes.
     *
     * if stopped yes, if running ... no, unless we have ocfs2 when
     * using raw partitions (file:) if using tap:aio we cloud...
     * The "ancient" way:
     * We do however follow the "two stage" approach, of "snap"
     * on primary first, with the create object... and then
     * backup the snapshot with the copycmd....
     * (should transfer to createSnapshot, backupSnapshot)
     */
@Override
public Answer createSnapshot(CreateObjectCommand cmd) {
    LOGGER.debug("execute createSnapshot: " + cmd.getClass());
    DataTO data = cmd.getData();
    Xen xen = new Xen(c);
    SnapshotObjectTO snap = (SnapshotObjectTO) data;
    VolumeObjectTO vol = snap.getVolume();
    try {
        Xen.Vm vm = xen.getVmConfig(snap.getVmName());
        if (vm != null) {
            return new CreateObjectAnswer("Snapshot object creation not supported for running VMs." + snap.getVmName());
        }
        Linux host = new Linux(c);
        String uuid = host.newUuid();
        /* for root volumes this works... */
        String src = vol.getPath() + "/" + vol.getUuid() + ".raw";
        String dest = vol.getPath() + "/" + uuid + ".raw";
        /* seems that sometimes the path is already contains a file
             * in case, we just replace it.... (Seems to happen if not ROOT)
             */
        if (vol.getPath().contains(vol.getUuid())) {
            src = getVirtualDiskPath(vol.getUuid(), data.getDataStore().getUuid());
            dest = src.replace(vol.getUuid(), uuid);
        }
        LOGGER.debug("Snapshot " + src + " to " + dest);
        host.copyFile(src, dest);
        SnapshotObjectTO nsnap = new SnapshotObjectTO();
        // nsnap.setPath(dest);
        // move to something that looks the same as xenserver.
        nsnap.setPath(uuid);
        return new CreateObjectAnswer(nsnap);
    } catch (Ovm3ResourceException e) {
        return new CreateObjectAnswer("Snapshot object creation failed. " + e.getMessage());
    }
}
Also used : Xen(com.cloud.hypervisor.ovm3.objects.Xen) SnapshotObjectTO(org.apache.cloudstack.storage.to.SnapshotObjectTO) DataTO(com.cloud.agent.api.to.DataTO) Linux(com.cloud.hypervisor.ovm3.objects.Linux) Ovm3ResourceException(com.cloud.hypervisor.ovm3.objects.Ovm3ResourceException) CreateObjectAnswer(org.apache.cloudstack.storage.command.CreateObjectAnswer) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO)

Example 39 with SnapshotObjectTO

use of org.apache.cloudstack.storage.to.SnapshotObjectTO in project cloudstack by apache.

the class SimulatorStorageProcessor method createVolumeFromSnapshot.

@Override
public Answer createVolumeFromSnapshot(CopyCommand cmd) {
    DataTO srcData = cmd.getSrcTO();
    SnapshotObjectTO snapshot = (SnapshotObjectTO) srcData;
    String snapshotPath = snapshot.getPath();
    int index = snapshotPath.lastIndexOf("/");
    String snapshotName = snapshotPath.substring(index + 1);
    VolumeObjectTO newVol = new VolumeObjectTO();
    newVol.setPath(snapshotName);
    return new CopyCmdAnswer(newVol);
}
Also used : SnapshotObjectTO(org.apache.cloudstack.storage.to.SnapshotObjectTO) DataTO(com.cloud.agent.api.to.DataTO) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer)

Example 40 with SnapshotObjectTO

use of org.apache.cloudstack.storage.to.SnapshotObjectTO in project cloudstack by apache.

the class KVMStorageProcessor method createSnapshot.

@Override
public Answer createSnapshot(final CreateObjectCommand cmd) {
    final SnapshotObjectTO snapshotTO = (SnapshotObjectTO) cmd.getData();
    final PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO) snapshotTO.getDataStore();
    final VolumeObjectTO volume = snapshotTO.getVolume();
    final String snapshotName = UUID.randomUUID().toString();
    final String vmName = volume.getVmName();
    try {
        final Connect conn = LibvirtConnection.getConnectionByVmName(vmName);
        DomainInfo.DomainState state = null;
        Domain vm = null;
        if (vmName != null) {
            try {
                vm = resource.getDomain(conn, vmName);
                state = vm.getInfo().state;
            } catch (final LibvirtException e) {
                s_logger.trace("Ignoring libvirt error.", e);
            }
        }
        final KVMStoragePool primaryPool = storagePoolMgr.getStoragePool(primaryStore.getPoolType(), primaryStore.getUuid());
        final KVMPhysicalDisk disk = storagePoolMgr.getPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), volume.getPath());
        if (state == DomainInfo.DomainState.VIR_DOMAIN_RUNNING && !primaryPool.isExternalSnapshot()) {
            final String vmUuid = vm.getUUIDString();
            final Object[] args = new Object[] { snapshotName, vmUuid };
            final String snapshot = SnapshotXML.format(args);
            final long start = System.currentTimeMillis();
            vm.snapshotCreateXML(snapshot);
            final long total = (System.currentTimeMillis() - start) / 1000;
            s_logger.debug("snapshot takes " + total + " seconds to finish");
            /*
                 * libvirt on RHEL6 doesn't handle resume event emitted from
                 * qemu
                 */
            vm = resource.getDomain(conn, vmName);
            state = vm.getInfo().state;
            if (state == DomainInfo.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());
                    s_logger.debug("Attempting to create RBD snapshot " + disk.getName() + "@" + snapshotName);
                    image.snapCreate(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 Script command = new Script(_manageSnapshotPath, _cmdsTimeout, s_logger);
                command.add("-c", disk.getPath());
                command.add("-n", snapshotName);
                final String result = command.execute();
                if (result != null) {
                    s_logger.debug("Failed to manage snapshot: " + result);
                    return new CreateObjectAnswer("Failed to manage snapshot: " + result);
                }
            }
        }
        final SnapshotObjectTO newSnapshot = new SnapshotObjectTO();
        // NOTE: sort of hack, we'd better just put snapshtoName
        newSnapshot.setPath(disk.getPath() + File.separator + snapshotName);
        return new CreateObjectAnswer(newSnapshot);
    } catch (final LibvirtException e) {
        s_logger.debug("Failed to manage snapshot: ", e);
        return new CreateObjectAnswer("Failed to manage snapshot: " + e.toString());
    }
}
Also used : SnapshotObjectTO(org.apache.cloudstack.storage.to.SnapshotObjectTO) Script(com.cloud.utils.script.Script) LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) Rados(com.ceph.rados.Rados) CreateObjectAnswer(org.apache.cloudstack.storage.command.CreateObjectAnswer) URISyntaxException(java.net.URISyntaxException) LibvirtException(org.libvirt.LibvirtException) RbdException(com.ceph.rbd.RbdException) QemuImgException(org.apache.cloudstack.utils.qemu.QemuImgException) FileNotFoundException(java.io.FileNotFoundException) InternalErrorException(com.cloud.exception.InternalErrorException) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) RadosException(com.ceph.rados.exceptions.RadosException) IOException(java.io.IOException) PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) Rbd(com.ceph.rbd.Rbd) RbdImage(com.ceph.rbd.RbdImage) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) IoCTX(com.ceph.rados.IoCTX) DomainInfo(org.libvirt.DomainInfo) Domain(org.libvirt.Domain)

Aggregations

SnapshotObjectTO (org.apache.cloudstack.storage.to.SnapshotObjectTO)44 CopyCmdAnswer (org.apache.cloudstack.storage.command.CopyCmdAnswer)33 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)23 DataTO (com.cloud.agent.api.to.DataTO)22 NfsTO (com.cloud.agent.api.to.NfsTO)21 DataStoreTO (com.cloud.agent.api.to.DataStoreTO)20 PrimaryDataStoreTO (org.apache.cloudstack.storage.to.PrimaryDataStoreTO)19 VolumeObjectTO (org.apache.cloudstack.storage.to.VolumeObjectTO)16 InternalErrorException (com.cloud.exception.InternalErrorException)14 CreateObjectAnswer (org.apache.cloudstack.storage.command.CreateObjectAnswer)14 Answer (com.cloud.agent.api.Answer)12 TemplateObjectTO (org.apache.cloudstack.storage.to.TemplateObjectTO)11 Connection (com.xensource.xenapi.Connection)9 XenAPIException (com.xensource.xenapi.Types.XenAPIException)9 VDI (com.xensource.xenapi.VDI)9 XmlRpcException (org.apache.xmlrpc.XmlRpcException)9 SR (com.xensource.xenapi.SR)8 URI (java.net.URI)7 ConfigurationException (javax.naming.ConfigurationException)7 S3TO (com.cloud.agent.api.to.S3TO)6