Search in sources :

Example 41 with StorageFilerTO

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

the class VmwareStorageManagerImpl method execute.

@Override
public Answer execute(VmwareHostService hostService, CopyVolumeCommand cmd) {
    Long volumeId = cmd.getVolumeId();
    String volumePath = cmd.getVolumePath();
    String secondaryStorageURL = cmd.getSecondaryStorageURL();
    String vmName = cmd.getVmName();
    VmwareContext context = hostService.getServiceContext(cmd);
    try {
        VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, cmd);
        Pair<String, String> result;
        if (cmd.toSecondaryStorage()) {
            result = copyVolumeToSecStorage(hostService, hyperHost, cmd, vmName, volumeId, cmd.getPool().getUuid(), volumePath, secondaryStorageURL, hostService.getWorkerName(context, cmd, 0), cmd.getNfsVersion());
        } else {
            StorageFilerTO poolTO = cmd.getPool();
            ManagedObjectReference morDatastore = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, poolTO.getUuid());
            if (morDatastore == null) {
                morDatastore = hyperHost.mountDatastore(false, poolTO.getHost(), 0, poolTO.getPath(), poolTO.getUuid().replace("-", ""));
                if (morDatastore == null) {
                    throw new Exception("Unable to mount storage pool on host. storeUrl: " + poolTO.getHost() + ":/" + poolTO.getPath());
                }
            }
            result = copyVolumeFromSecStorage(hyperHost, volumeId, new DatastoreMO(context, morDatastore), secondaryStorageURL, volumePath, cmd.getNfsVersion());
            deleteVolumeDirOnSecondaryStorage(volumeId, secondaryStorageURL, cmd.getNfsVersion());
        }
        return new CopyVolumeAnswer(cmd, true, null, result.first(), result.second());
    } catch (Throwable e) {
        if (e instanceof RemoteException) {
            hostService.invalidateServiceContext(context);
        }
        String msg = "Unable to execute CopyVolumeCommand due to exception";
        s_logger.error(msg, e);
        return new CopyVolumeAnswer(cmd, false, "CopyVolumeCommand failed due to exception: " + StringUtils.getExceptionStackInfo(e), null, null);
    }
}
Also used : VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) CopyVolumeAnswer(com.cloud.agent.api.storage.CopyVolumeAnswer) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) RemoteException(java.rmi.RemoteException) StorageFilerTO(com.cloud.agent.api.to.StorageFilerTO) RemoteException(java.rmi.RemoteException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DatastoreMO(com.cloud.hypervisor.vmware.mo.DatastoreMO) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 42 with StorageFilerTO

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

the class MockStorageManagerImpl method ModifyStoragePool.

@Override
public ModifyStoragePoolAnswer ModifyStoragePool(ModifyStoragePoolCommand cmd) {
    StorageFilerTO sf = cmd.getPool();
    TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB);
    MockStoragePoolVO storagePool = null;
    try {
        txn.start();
        storagePool = _mockStoragePoolDao.findByUuid(sf.getUuid());
        if (storagePool == null) {
            storagePool = new MockStoragePoolVO();
            storagePool.setUuid(sf.getUuid());
            storagePool.setMountPoint("/mnt/" + sf.getUuid());
            Long size = DEFAULT_HOST_STORAGE_SIZE;
            String path = sf.getPath();
            int index = path.lastIndexOf("/");
            if (index != -1) {
                path = path.substring(index + 1);
                if (path != null) {
                    String[] values = path.split("=");
                    if (values.length > 1 && values[0].equalsIgnoreCase("size")) {
                        size = Long.parseLong(values[1]);
                    }
                }
            }
            storagePool.setCapacity(size);
            storagePool.setStorageType(sf.getType());
            storagePool = _mockStoragePoolDao.persist(storagePool);
        }
        txn.commit();
    } catch (Exception ex) {
        txn.rollback();
        throw new CloudRuntimeException("Error when modifying storage pool " + cmd.getPool().getPath(), ex);
    } finally {
        txn.close();
        txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB);
        txn.close();
    }
    return new ModifyStoragePoolAnswer(cmd, storagePool.getCapacity(), storagePool.getCapacity(), new HashMap<String, TemplateProp>());
}
Also used : TemplateProp(com.cloud.storage.template.TemplateProp) TransactionLegacy(com.cloud.utils.db.TransactionLegacy) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ModifyStoragePoolAnswer(com.cloud.agent.api.ModifyStoragePoolAnswer) MockStoragePoolVO(com.cloud.simulator.MockStoragePoolVO) StorageFilerTO(com.cloud.agent.api.to.StorageFilerTO) URISyntaxException(java.net.URISyntaxException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException)

Example 43 with StorageFilerTO

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

Example 44 with StorageFilerTO

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

the class LibvirtCreateVolumeFromSnapshotCommandWrapper method execute.

@Override
public Answer execute(final CreateVolumeFromSnapshotCommand command, final LibvirtComputingResource libvirtComputingResource) {
    try {
        String snapshotPath = command.getSnapshotUuid();
        final int index = snapshotPath.lastIndexOf("/");
        snapshotPath = snapshotPath.substring(0, index);
        final KVMStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr();
        final KVMStoragePool secondaryPool = storagePoolMgr.getStoragePoolByURI(command.getSecondaryStorageUrl() + snapshotPath);
        final KVMPhysicalDisk snapshot = secondaryPool.getPhysicalDisk(command.getSnapshotName());
        final String primaryUuid = command.getPrimaryStoragePoolNameLabel();
        final StorageFilerTO pool = command.getPool();
        final KVMStoragePool primaryPool = storagePoolMgr.getStoragePool(pool.getType(), primaryUuid);
        final String volUuid = UUID.randomUUID().toString();
        final KVMPhysicalDisk disk = storagePoolMgr.copyPhysicalDisk(snapshot, volUuid, primaryPool, 0);
        if (disk == null) {
            throw new NullPointerException("Disk was not successfully copied to the new storage.");
        }
        return new CreateVolumeFromSnapshotAnswer(command, true, "", disk.getName());
    } catch (final CloudRuntimeException e) {
        return new CreateVolumeFromSnapshotAnswer(command, false, e.toString(), null);
    } catch (final Exception e) {
        return new CreateVolumeFromSnapshotAnswer(command, false, e.toString(), null);
    }
}
Also used : KVMStoragePoolManager(com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager) KVMStoragePool(com.cloud.hypervisor.kvm.storage.KVMStoragePool) KVMPhysicalDisk(com.cloud.hypervisor.kvm.storage.KVMPhysicalDisk) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) StorageFilerTO(com.cloud.agent.api.to.StorageFilerTO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) CreateVolumeFromSnapshotAnswer(com.cloud.agent.api.CreateVolumeFromSnapshotAnswer)

Example 45 with StorageFilerTO

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

the class LibvirtDeleteStoragePoolCommandWrapper method execute.

@Override
public Answer execute(final DeleteStoragePoolCommand command, final LibvirtComputingResource libvirtComputingResource) {
    try {
        final StorageFilerTO pool = command.getPool();
        final KVMStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr();
        storagePoolMgr.deleteStoragePool(pool.getType(), pool.getUuid());
        return new Answer(command);
    } catch (final CloudRuntimeException e) {
        return new Answer(command, false, e.toString());
    }
}
Also used : Answer(com.cloud.agent.api.Answer) KVMStoragePoolManager(com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) StorageFilerTO(com.cloud.agent.api.to.StorageFilerTO)

Aggregations

StorageFilerTO (com.cloud.agent.api.to.StorageFilerTO)54 Answer (com.cloud.agent.api.Answer)30 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)27 KVMStoragePoolManager (com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager)21 KVMStoragePool (com.cloud.hypervisor.kvm.storage.KVMStoragePool)20 Test (org.junit.Test)19 AttachAnswer (org.apache.cloudstack.storage.command.AttachAnswer)17 CheckRouterAnswer (com.cloud.agent.api.CheckRouterAnswer)16 LibvirtRequestWrapper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper)16 VolumeTO (com.cloud.agent.api.to.VolumeTO)15 KVMPhysicalDisk (com.cloud.hypervisor.kvm.storage.KVMPhysicalDisk)15 ModifyStoragePoolAnswer (com.cloud.agent.api.ModifyStoragePoolAnswer)10 DiskProfile (com.cloud.vm.DiskProfile)10 ConfigurationException (javax.naming.ConfigurationException)10 CreateAnswer (com.cloud.agent.api.storage.CreateAnswer)9 ResizeVolumeCommand (com.cloud.agent.api.storage.ResizeVolumeCommand)8 NfsStoragePool (com.cloud.hypervisor.kvm.resource.KVMHABase.NfsStoragePool)8 Pair (com.cloud.utils.Pair)8 Connection (com.xensource.xenapi.Connection)8 HashMap (java.util.HashMap)8