Search in sources :

Example 21 with KvmPhysicalDisk

use of com.cloud.agent.resource.kvm.storage.KvmPhysicalDisk in project cosmic by MissionCriticalCloud.

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.legacymodel.communication.answer.PrimaryStorageDownloadAnswer) KvmStoragePool(com.cloud.agent.resource.kvm.storage.KvmStoragePool) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) KvmStoragePoolManager(com.cloud.agent.resource.kvm.storage.KvmStoragePoolManager) KvmPhysicalDisk(com.cloud.agent.resource.kvm.storage.KvmPhysicalDisk)

Example 22 with KvmPhysicalDisk

use of com.cloud.agent.resource.kvm.storage.KvmPhysicalDisk in project cosmic by MissionCriticalCloud.

the class LibvirtComputingResourceTest method testResizeVolumeCommandException.

@Test
@Ignore
public void testResizeVolumeCommandException() {
    final String path = "nfs:/127.0.0.1/storage/secondary";
    final StorageFilerTO pool = Mockito.mock(StorageFilerTO.class);
    final Long currentSize = 100l;
    final Long newSize = 200l;
    final boolean shrinkOk = false;
    final String vmInstance = "Test";
    final ResizeVolumeCommand command = new ResizeVolumeCommand(path, pool, currentSize, newSize, shrinkOk, vmInstance);
    final KvmStoragePoolManager storagePoolMgr = Mockito.mock(KvmStoragePoolManager.class);
    final KvmStoragePool storagePool = Mockito.mock(KvmStoragePool.class);
    final KvmPhysicalDisk vol = Mockito.mock(KvmPhysicalDisk.class);
    final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
    when(this.libvirtComputingResource.getStoragePoolMgr()).thenReturn(storagePoolMgr);
    when(storagePoolMgr.getStoragePool(pool.getType(), pool.getUuid())).thenReturn(storagePool);
    when(storagePool.getPhysicalDisk(path)).thenReturn(vol);
    when(vol.getPath()).thenReturn(path);
    when(storagePool.getType()).thenReturn(StoragePoolType.RBD);
    when(vol.getFormat()).thenReturn(PhysicalDiskFormat.FILE);
    when(this.libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
    try {
        when(libvirtUtilitiesHelper.getConnection()).thenThrow(LibvirtException.class);
    } catch (final LibvirtException e) {
        fail(e.getMessage());
    }
    final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
    assertNotNull(wrapper);
    final Answer answer = wrapper.execute(command, this.libvirtComputingResource);
    assertFalse(answer.getResult());
    verify(this.libvirtComputingResource, times(1)).getStoragePoolMgr();
    verify(this.libvirtComputingResource, times(1)).getLibvirtUtilitiesHelper();
    try {
        verify(libvirtUtilitiesHelper, times(1)).getConnection();
    } catch (final LibvirtException e) {
        fail(e.getMessage());
    }
}
Also used : Answer(com.cloud.legacymodel.communication.answer.Answer) CheckRouterAnswer(com.cloud.legacymodel.communication.answer.CheckRouterAnswer) AttachAnswer(com.cloud.legacymodel.communication.answer.AttachAnswer) LibvirtRequestWrapper(com.cloud.agent.resource.kvm.wrapper.LibvirtRequestWrapper) KvmStoragePool(com.cloud.agent.resource.kvm.storage.KvmStoragePool) LibvirtException(org.libvirt.LibvirtException) ResizeVolumeCommand(com.cloud.legacymodel.communication.command.ResizeVolumeCommand) KvmStoragePoolManager(com.cloud.agent.resource.kvm.storage.KvmStoragePoolManager) KvmPhysicalDisk(com.cloud.agent.resource.kvm.storage.KvmPhysicalDisk) StorageFilerTO(com.cloud.legacymodel.to.StorageFilerTO) LibvirtUtilitiesHelper(com.cloud.agent.resource.kvm.wrapper.LibvirtUtilitiesHelper) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 23 with KvmPhysicalDisk

use of com.cloud.agent.resource.kvm.storage.KvmPhysicalDisk in project cosmic by MissionCriticalCloud.

the class LibvirtComputingResourceTest method testCreateCommandNoTemplate.

@Test
public void testCreateCommandNoTemplate() {
    final DiskProfile diskCharacteristics = Mockito.mock(DiskProfile.class);
    final StorageFilerTO pool = Mockito.mock(StorageFilerTO.class);
    final boolean executeInSequence = false;
    final CreateCommand command = new CreateCommand(diskCharacteristics, pool, executeInSequence);
    final KvmStoragePoolManager poolManager = Mockito.mock(KvmStoragePoolManager.class);
    final KvmStoragePool primary = Mockito.mock(KvmStoragePool.class);
    final KvmPhysicalDisk vol = Mockito.mock(KvmPhysicalDisk.class);
    when(this.libvirtComputingResource.getStoragePoolMgr()).thenReturn(poolManager);
    when(poolManager.getStoragePool(pool.getType(), pool.getUuid())).thenReturn(primary);
    when(primary.createPhysicalDisk(diskCharacteristics.getPath(), diskCharacteristics.getProvisioningType(), diskCharacteristics.getSize())).thenReturn(vol);
    final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
    assertNotNull(wrapper);
    final Answer answer = wrapper.execute(command, this.libvirtComputingResource);
    assertTrue(answer.getResult());
    verify(this.libvirtComputingResource, times(1)).getStoragePoolMgr();
    verify(poolManager, times(1)).getStoragePool(pool.getType(), pool.getUuid());
}
Also used : Answer(com.cloud.legacymodel.communication.answer.Answer) CheckRouterAnswer(com.cloud.legacymodel.communication.answer.CheckRouterAnswer) AttachAnswer(com.cloud.legacymodel.communication.answer.AttachAnswer) LibvirtRequestWrapper(com.cloud.agent.resource.kvm.wrapper.LibvirtRequestWrapper) CreateCommand(com.cloud.legacymodel.communication.command.CreateCommand) KvmStoragePool(com.cloud.agent.resource.kvm.storage.KvmStoragePool) KvmStoragePoolManager(com.cloud.agent.resource.kvm.storage.KvmStoragePoolManager) KvmPhysicalDisk(com.cloud.agent.resource.kvm.storage.KvmPhysicalDisk) DiskProfile(com.cloud.legacymodel.storage.DiskProfile) StorageFilerTO(com.cloud.legacymodel.to.StorageFilerTO) Test(org.junit.Test)

Example 24 with KvmPhysicalDisk

use of com.cloud.agent.resource.kvm.storage.KvmPhysicalDisk in project cosmic by MissionCriticalCloud.

the class LibvirtComputingResourceTest method testCopyVolumeCommandPrimaryNotFound.

@Test
public void testCopyVolumeCommandPrimaryNotFound() {
    final StoragePool storagePool = Mockito.mock(StoragePool.class);
    final String secondaryStoragePoolURL = "nfs:/127.0.0.1/storage/secondary";
    final Long volumeId = 1l;
    final int wait = 0;
    final String volumePath = "/vol/path";
    final boolean toSecondaryStorage = false;
    final boolean executeInSequence = false;
    final CopyVolumeCommand command = new CopyVolumeCommand(volumeId, volumePath, storagePool, secondaryStoragePoolURL, toSecondaryStorage, wait, executeInSequence);
    final String destVolumeName = "ce97bbc1-34fe-4259-9202-74bbce2562ab";
    final String volumeDestPath = "/volumes/" + command.getVolumeId() + File.separator;
    final KvmStoragePoolManager storagePoolMgr = Mockito.mock(KvmStoragePoolManager.class);
    final KvmStoragePool secondary = Mockito.mock(KvmStoragePool.class);
    final KvmStoragePool primary = Mockito.mock(KvmStoragePool.class);
    final KvmPhysicalDisk disk = Mockito.mock(KvmPhysicalDisk.class);
    final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
    final StorageFilerTO pool = command.getPool();
    when(this.libvirtComputingResource.getStoragePoolMgr()).thenReturn(storagePoolMgr);
    when(storagePoolMgr.getStoragePool(pool.getType(), pool.getUuid())).thenThrow(new CloudRuntimeException("not found"));
    when(storagePoolMgr.createStoragePool(pool.getUuid(), pool.getHost(), pool.getPort(), pool.getPath(), pool.getUserInfo(), pool.getType())).thenReturn(primary);
    when(this.libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
    when(libvirtUtilitiesHelper.generateUuidName()).thenReturn(destVolumeName);
    when(secondary.getType()).thenReturn(StoragePoolType.ManagedNFS);
    when(secondary.getUuid()).thenReturn("60d979d8-d132-4181-8eca-8dfde50d7df6");
    when(storagePoolMgr.getStoragePoolByUri(secondaryStoragePoolURL + volumeDestPath)).thenReturn(secondary);
    when(primary.getPhysicalDisk(command.getVolumePath() + ".qcow2")).thenReturn(disk);
    when(storagePoolMgr.copyPhysicalDisk(disk, destVolumeName, primary, 0)).thenReturn(disk);
    final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
    assertNotNull(wrapper);
    final Answer answer = wrapper.execute(command, this.libvirtComputingResource);
    assertTrue(answer.getResult());
    verify(this.libvirtComputingResource, times(1)).getStoragePoolMgr();
}
Also used : NfsStoragePool(com.cloud.agent.resource.kvm.ha.KvmHaBase.NfsStoragePool) KvmStoragePool(com.cloud.agent.resource.kvm.storage.KvmStoragePool) StoragePool(com.cloud.legacymodel.storage.StoragePool) KvmStoragePool(com.cloud.agent.resource.kvm.storage.KvmStoragePool) CopyVolumeCommand(com.cloud.legacymodel.communication.command.CopyVolumeCommand) StorageFilerTO(com.cloud.legacymodel.to.StorageFilerTO) LibvirtUtilitiesHelper(com.cloud.agent.resource.kvm.wrapper.LibvirtUtilitiesHelper) Answer(com.cloud.legacymodel.communication.answer.Answer) CheckRouterAnswer(com.cloud.legacymodel.communication.answer.CheckRouterAnswer) AttachAnswer(com.cloud.legacymodel.communication.answer.AttachAnswer) LibvirtRequestWrapper(com.cloud.agent.resource.kvm.wrapper.LibvirtRequestWrapper) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) KvmStoragePoolManager(com.cloud.agent.resource.kvm.storage.KvmStoragePoolManager) KvmPhysicalDisk(com.cloud.agent.resource.kvm.storage.KvmPhysicalDisk) Test(org.junit.Test)

Example 25 with KvmPhysicalDisk

use of com.cloud.agent.resource.kvm.storage.KvmPhysicalDisk in project cosmic by MissionCriticalCloud.

the class LibvirtComputingResourceTest method testPrimaryStorageDownloadCommandNOTemplateDisk.

@Test(expected = NullPointerException.class)
public void testPrimaryStorageDownloadCommandNOTemplateDisk() {
    final StoragePool pool = Mockito.mock(StoragePool.class);
    final List<KvmPhysicalDisk> disks = new ArrayList<>();
    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 KvmPhysicalDisk disk = new KvmPhysicalDisk("/path", "disk.qcow2", primaryPool);
    disks.add(disk);
    final int index = url.lastIndexOf("/");
    final String mountpoint = url.substring(0, index);
    when(this.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, this.libvirtComputingResource);
    assertFalse(answer.getResult());
    verify(this.libvirtComputingResource, times(1)).getStoragePoolMgr();
}
Also used : NfsStoragePool(com.cloud.agent.resource.kvm.ha.KvmHaBase.NfsStoragePool) KvmStoragePool(com.cloud.agent.resource.kvm.storage.KvmStoragePool) StoragePool(com.cloud.legacymodel.storage.StoragePool) KvmStoragePool(com.cloud.agent.resource.kvm.storage.KvmStoragePool) PrimaryStorageDownloadCommand(com.cloud.legacymodel.communication.command.PrimaryStorageDownloadCommand) ArrayList(java.util.ArrayList) ImageFormat(com.cloud.model.enumeration.ImageFormat) Answer(com.cloud.legacymodel.communication.answer.Answer) CheckRouterAnswer(com.cloud.legacymodel.communication.answer.CheckRouterAnswer) AttachAnswer(com.cloud.legacymodel.communication.answer.AttachAnswer) LibvirtRequestWrapper(com.cloud.agent.resource.kvm.wrapper.LibvirtRequestWrapper) KvmStoragePoolManager(com.cloud.agent.resource.kvm.storage.KvmStoragePoolManager) KvmPhysicalDisk(com.cloud.agent.resource.kvm.storage.KvmPhysicalDisk) Test(org.junit.Test)

Aggregations

KvmPhysicalDisk (com.cloud.agent.resource.kvm.storage.KvmPhysicalDisk)35 KvmStoragePool (com.cloud.agent.resource.kvm.storage.KvmStoragePool)34 KvmStoragePoolManager (com.cloud.agent.resource.kvm.storage.KvmStoragePoolManager)31 Answer (com.cloud.legacymodel.communication.answer.Answer)22 LibvirtRequestWrapper (com.cloud.agent.resource.kvm.wrapper.LibvirtRequestWrapper)20 AttachAnswer (com.cloud.legacymodel.communication.answer.AttachAnswer)20 CheckRouterAnswer (com.cloud.legacymodel.communication.answer.CheckRouterAnswer)20 Test (org.junit.Test)20 StorageFilerTO (com.cloud.legacymodel.to.StorageFilerTO)15 NfsStoragePool (com.cloud.agent.resource.kvm.ha.KvmHaBase.NfsStoragePool)14 StoragePool (com.cloud.legacymodel.storage.StoragePool)14 CloudRuntimeException (com.cloud.legacymodel.exceptions.CloudRuntimeException)11 LibvirtUtilitiesHelper (com.cloud.agent.resource.kvm.wrapper.LibvirtUtilitiesHelper)10 LibvirtException (org.libvirt.LibvirtException)7 Processor (com.cloud.common.storageprocessor.Processor)6 TemplateLocation (com.cloud.common.storageprocessor.TemplateLocation)6 InternalErrorException (com.cloud.legacymodel.exceptions.InternalErrorException)6 StorageLayer (com.cloud.utils.storage.StorageLayer)6 ConfigurationException (javax.naming.ConfigurationException)6 Connect (org.libvirt.Connect)6