Search in sources :

Example 86 with KvmStoragePool

use of com.cloud.hypervisor.kvm.storage.KvmStoragePool in project cloudstack by apache.

the class LibvirtModifyStoragePoolCommandWrapper method execute.

@Override
public Answer execute(final ModifyStoragePoolCommand command, final LibvirtComputingResource libvirtComputingResource) {
    final KVMStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr();
    final KVMStoragePool storagepool = storagePoolMgr.createStoragePool(command.getPool().getUuid(), command.getPool().getHost(), command.getPool().getPort(), command.getPool().getPath(), command.getPool().getUserInfo(), command.getPool().getType());
    if (storagepool == null) {
        return new Answer(command, false, " Failed to create storage pool");
    }
    final Map<String, TemplateProp> tInfo = new HashMap<String, TemplateProp>();
    final ModifyStoragePoolAnswer answer = new ModifyStoragePoolAnswer(command, storagepool.getCapacity(), storagepool.getAvailable(), tInfo);
    return answer;
}
Also used : TemplateProp(com.cloud.storage.template.TemplateProp) ModifyStoragePoolAnswer(com.cloud.agent.api.ModifyStoragePoolAnswer) Answer(com.cloud.agent.api.Answer) KVMStoragePoolManager(com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager) KVMStoragePool(com.cloud.hypervisor.kvm.storage.KVMStoragePool) HashMap(java.util.HashMap) ModifyStoragePoolAnswer(com.cloud.agent.api.ModifyStoragePoolAnswer)

Example 87 with KvmStoragePool

use of com.cloud.hypervisor.kvm.storage.KvmStoragePool in project cloudstack by apache.

the class LibvirtDestroyCommandWrapper method execute.

@Override
public Answer execute(final DestroyCommand command, final LibvirtComputingResource libvirtComputingResource) {
    final VolumeTO vol = command.getVolume();
    try {
        final KVMStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr();
        final KVMStoragePool pool = storagePoolMgr.getStoragePool(vol.getPoolType(), vol.getPoolUuid());
        pool.deletePhysicalDisk(vol.getPath(), null);
        return new Answer(command, true, "Success");
    } catch (final CloudRuntimeException e) {
        s_logger.debug("Failed to delete volume: " + e.toString());
        return new Answer(command, false, e.toString());
    }
}
Also used : Answer(com.cloud.agent.api.Answer) VolumeTO(com.cloud.agent.api.to.VolumeTO) KVMStoragePoolManager(com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager) KVMStoragePool(com.cloud.hypervisor.kvm.storage.KVMStoragePool) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException)

Example 88 with KvmStoragePool

use of com.cloud.hypervisor.kvm.storage.KvmStoragePool 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) && primaryStore.getPoolType() != StoragePoolType.RBD) {
        return new Answer(command, false, String.format("Revert snapshot does not support storage pool of type [%s]. Revert snapshot is supported by storage pools of type 'NFS' or 'RBD'", primaryStore.getPoolType()));
    }
    String volumePath = volume.getPath();
    String snapshotPath = null;
    String snapshotRelPath = null;
    KVMStoragePool secondaryStoragePool = null;
    try {
        snapshotRelPath = snapshot.getPath();
        KVMStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr();
        KVMPhysicalDisk snapshotDisk = storagePoolMgr.getPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), volumePath);
        KVMStoragePool primaryPool = snapshotDisk.getPool();
        if (primaryPool.getType() == StoragePoolType.RBD) {
            Rados rados = new Rados(primaryPool.getAuthUserName());
            rados.confSet(MON_HOST, primaryPool.getSourceHost() + ":" + primaryPool.getSourcePort());
            rados.confSet(KEY, primaryPool.getAuthSecret());
            rados.confSet(CLIENT_MOUNT_TIMEOUT, RADOS_CONNECTION_TIMEOUT);
            rados.connect();
            String[] rbdPoolAndVolumeAndSnapshot = snapshotRelPath.split("/");
            int snapshotIndex = rbdPoolAndVolumeAndSnapshot.length - 1;
            String rbdSnapshotId = rbdPoolAndVolumeAndSnapshot[snapshotIndex];
            IoCTX io = rados.ioCtxCreate(primaryPool.getSourceDir());
            Rbd rbd = new Rbd(io);
            s_logger.debug(String.format("Attempting to rollback RBD snapshot [name:%s], [volumeid:%s], [snapshotid:%s]", snapshot.getName(), volumePath, rbdSnapshotId));
            RbdImage image = rbd.open(volumePath);
            image.snapRollBack(rbdSnapshotId);
            rbd.close(image);
            rados.ioCtxDestroy(io);
        } else {
            NfsTO nfsImageStore = (NfsTO) snapshotImageStore;
            String secondaryStoragePoolUrl = nfsImageStore.getUrl();
            secondaryStoragePool = storagePoolMgr.getStoragePoolByURI(secondaryStoragePoolUrl);
            String ssPmountPath = secondaryStoragePool.getLocalPath();
            snapshotPath = ssPmountPath + File.separator + snapshotRelPath;
            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());
    } catch (RadosException e) {
        s_logger.error("Failed to connect to Rados pool while trying to revert snapshot. Exception: ", e);
        return new Answer(command, false, e.toString());
    } catch (RbdException e) {
        s_logger.error("Failed to connect to revert snapshot due to RBD exception: ", e);
        return new Answer(command, false, e.toString());
    }
}
Also used : SnapshotObjectTO(org.apache.cloudstack.storage.to.SnapshotObjectTO) 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) KVMPhysicalDisk(com.cloud.hypervisor.kvm.storage.KVMPhysicalDisk) Rados(com.ceph.rados.Rados) RadosException(com.ceph.rados.exceptions.RadosException) NfsTO(com.cloud.agent.api.to.NfsTO) Answer(com.cloud.agent.api.Answer) KVMStoragePool(com.cloud.hypervisor.kvm.storage.KVMStoragePool) PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) Rbd(com.ceph.rbd.Rbd) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) RbdImage(com.ceph.rbd.RbdImage) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) IoCTX(com.ceph.rados.IoCTX) RbdException(com.ceph.rbd.RbdException)

Example 89 with KvmStoragePool

use of com.cloud.hypervisor.kvm.storage.KvmStoragePool in project cloudstack by apache.

the class LibvirtPrimaryStorageDownloadCommandWrapper method execute.

@Override
public Answer execute(final PrimaryStorageDownloadCommand command, final LibvirtComputingResource libvirtComputingResource) {
    final String tmplturl = command.getUrl();
    final int index = tmplturl.lastIndexOf("/");
    final String mountpoint = tmplturl.substring(0, index);
    String tmpltname = null;
    if (index < tmplturl.length() - 1) {
        tmpltname = tmplturl.substring(index + 1);
    }
    KVMPhysicalDisk tmplVol = null;
    KVMStoragePool secondaryPool = null;
    final KVMStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr();
    try {
        secondaryPool = storagePoolMgr.getStoragePoolByURI(mountpoint);
        /* Get template vol */
        if (tmpltname == null) {
            secondaryPool.refresh();
            final List<KVMPhysicalDisk> disks = secondaryPool.listPhysicalDisks();
            if (disks == null || disks.isEmpty()) {
                return new PrimaryStorageDownloadAnswer("Failed to get volumes from pool: " + secondaryPool.getUuid());
            }
            for (final KVMPhysicalDisk disk : disks) {
                if (disk.getName().endsWith("qcow2")) {
                    tmplVol = disk;
                    break;
                }
            }
            if (tmplVol == null) {
                return new PrimaryStorageDownloadAnswer("Failed to get template from pool: " + secondaryPool.getUuid());
            }
        } else {
            tmplVol = secondaryPool.getPhysicalDisk(tmpltname);
        }
        /* Copy volume to primary storage */
        final KVMStoragePool primaryPool = storagePoolMgr.getStoragePool(command.getPool().getType(), command.getPoolUuid());
        final KVMPhysicalDisk primaryVol = storagePoolMgr.copyPhysicalDisk(tmplVol, UUID.randomUUID().toString(), primaryPool, 0);
        return new PrimaryStorageDownloadAnswer(primaryVol.getName(), primaryVol.getSize());
    } catch (final CloudRuntimeException e) {
        return new PrimaryStorageDownloadAnswer(e.toString());
    } finally {
        if (secondaryPool != null) {
            storagePoolMgr.deleteStoragePool(secondaryPool.getType(), secondaryPool.getUuid());
        }
    }
}
Also used : PrimaryStorageDownloadAnswer(com.cloud.agent.api.storage.PrimaryStorageDownloadAnswer) 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)

Example 90 with KvmStoragePool

use of com.cloud.hypervisor.kvm.storage.KvmStoragePool in project cloudstack by apache.

the class LibvirtComputingResourceTest method testPrimaryStorageDownloadCommandNOTemplateNODisk.

@Test
public void testPrimaryStorageDownloadCommandNOTemplateNODisk() {
    final StoragePool pool = Mockito.mock(StoragePool.class);
    final List<KVMPhysicalDisk> disks = new ArrayList<KVMPhysicalDisk>();
    final String name = "Test";
    final String url = "http://template/";
    final ImageFormat format = ImageFormat.QCOW2;
    final long accountId = 1l;
    final int wait = 0;
    final PrimaryStorageDownloadCommand command = new PrimaryStorageDownloadCommand(name, url, format, accountId, pool, wait);
    final KVMStoragePoolManager storagePoolMgr = Mockito.mock(KVMStoragePoolManager.class);
    final KVMStoragePool primaryPool = Mockito.mock(KVMStoragePool.class);
    final KVMStoragePool secondaryPool = Mockito.mock(KVMStoragePool.class);
    final KVMPhysicalDisk tmplVol = Mockito.mock(KVMPhysicalDisk.class);
    final KVMPhysicalDisk primaryVol = Mockito.mock(KVMPhysicalDisk.class);
    final int index = url.lastIndexOf("/");
    final String mountpoint = url.substring(0, index);
    when(libvirtComputingResource.getStoragePoolMgr()).thenReturn(storagePoolMgr);
    when(storagePoolMgr.getStoragePoolByURI(mountpoint)).thenReturn(secondaryPool);
    when(secondaryPool.listPhysicalDisks()).thenReturn(disks);
    when(storagePoolMgr.getStoragePool(command.getPool().getType(), command.getPoolUuid())).thenReturn(primaryPool);
    when(storagePoolMgr.copyPhysicalDisk(tmplVol, UUID.randomUUID().toString(), primaryPool, 0)).thenReturn(primaryVol);
    final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
    assertNotNull(wrapper);
    final Answer answer = wrapper.execute(command, libvirtComputingResource);
    assertFalse(answer.getResult());
    verify(libvirtComputingResource, times(1)).getStoragePoolMgr();
}
Also used : KVMStoragePoolManager(com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager) KVMStoragePool(com.cloud.hypervisor.kvm.storage.KVMStoragePool) StoragePool(com.cloud.storage.StoragePool) NfsStoragePool(com.cloud.hypervisor.kvm.resource.KVMHABase.NfsStoragePool) KVMPhysicalDisk(com.cloud.hypervisor.kvm.storage.KVMPhysicalDisk) PrimaryStorageDownloadCommand(com.cloud.agent.api.storage.PrimaryStorageDownloadCommand) ArrayList(java.util.ArrayList) ImageFormat(com.cloud.storage.Storage.ImageFormat) UnsupportedAnswer(com.cloud.agent.api.UnsupportedAnswer) AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer) Answer(com.cloud.agent.api.Answer) CheckRouterAnswer(com.cloud.agent.api.CheckRouterAnswer) LibvirtRequestWrapper(com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper) KVMStoragePool(com.cloud.hypervisor.kvm.storage.KVMStoragePool) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

Answer (com.cloud.agent.api.Answer)66 KVMStoragePool (com.cloud.hypervisor.kvm.storage.KVMStoragePool)58 CheckRouterAnswer (com.cloud.agent.api.CheckRouterAnswer)57 LibvirtRequestWrapper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper)57 Test (org.junit.Test)57 KVMStoragePoolManager (com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager)47 KvmStoragePool (com.cloud.hypervisor.kvm.storage.KvmStoragePool)46 KvmStoragePoolManager (com.cloud.hypervisor.kvm.storage.KvmStoragePoolManager)41 KVMPhysicalDisk (com.cloud.hypervisor.kvm.storage.KVMPhysicalDisk)40 StoragePool (com.cloud.storage.StoragePool)40 StorageFilerTO (com.cloud.agent.api.to.StorageFilerTO)35 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)35 KvmPhysicalDisk (com.cloud.hypervisor.kvm.storage.KvmPhysicalDisk)34 UnsupportedAnswer (com.cloud.agent.api.UnsupportedAnswer)29 AttachAnswer (org.apache.cloudstack.storage.command.AttachAnswer)29 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)29 AttachAnswer (com.cloud.storage.command.AttachAnswer)28 LibvirtUtilitiesHelper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper)25 NfsStoragePool (com.cloud.hypervisor.kvm.resource.KVMHABase.NfsStoragePool)20 NfsStoragePool (com.cloud.hypervisor.kvm.resource.KvmHaBase.NfsStoragePool)20