Search in sources :

Example 71 with KvmStoragePool

use of com.cloud.hypervisor.kvm.storage.KvmStoragePool in project cosmic by MissionCriticalCloud.

the class LibvirtComputingResourceTest method testCreatePrivateTemplateFromSnapshotCommand.

@Test
public void testCreatePrivateTemplateFromSnapshotCommand() {
    final StoragePool pool = Mockito.mock(StoragePool.class);
    final String secondaryStoragePoolURL = "nfs:/127.0.0.1/storage/secondary";
    final Long dcId = 1l;
    final Long accountId = 1l;
    final Long volumeId = 1l;
    final String backedUpSnapshotUuid = "/run/9a0afe7c-26a7-4585-bf87-abf82ae106d9/";
    final String backedUpSnapshotName = "snap";
    final String origTemplateInstallPath = "/install/path/";
    final Long newTemplateId = 2l;
    final String templateName = "templ";
    final int wait = 0;
    final CreatePrivateTemplateFromSnapshotCommand command = new CreatePrivateTemplateFromSnapshotCommand(pool, secondaryStoragePoolURL, dcId, accountId, volumeId, backedUpSnapshotUuid, backedUpSnapshotName, origTemplateInstallPath, newTemplateId, templateName, wait);
    final String templatePath = "/template/path";
    final String localPath = "/mnt/local";
    final String tmplName = "ce97bbc1-34fe-4259-9202-74bbce2562ab";
    final KvmStoragePoolManager storagePoolMgr = Mockito.mock(KvmStoragePoolManager.class);
    final KvmStoragePool secondaryPool = Mockito.mock(KvmStoragePool.class);
    final KvmStoragePool snapshotPool = Mockito.mock(KvmStoragePool.class);
    final KvmPhysicalDisk snapshot = Mockito.mock(KvmPhysicalDisk.class);
    final StorageLayer storage = Mockito.mock(StorageLayer.class);
    final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
    final TemplateLocation location = Mockito.mock(TemplateLocation.class);
    final Processor qcow2Processor = Mockito.mock(Processor.class);
    final FormatInfo info = Mockito.mock(FormatInfo.class);
    when(libvirtComputingResource.getStoragePoolMgr()).thenReturn(storagePoolMgr);
    String snapshotPath = command.getSnapshotUuid();
    final int index = snapshotPath.lastIndexOf("/");
    snapshotPath = snapshotPath.substring(0, index);
    when(storagePoolMgr.getStoragePoolByUri(command.getSecondaryStorageUrl() + snapshotPath)).thenReturn(snapshotPool);
    when(storagePoolMgr.getStoragePoolByUri(command.getSecondaryStorageUrl())).thenReturn(secondaryPool);
    when(snapshotPool.getPhysicalDisk(command.getSnapshotName())).thenReturn(snapshot);
    when(secondaryPool.getLocalPath()).thenReturn(localPath);
    when(libvirtComputingResource.getStorage()).thenReturn(storage);
    when(libvirtComputingResource.createTmplPath()).thenReturn(templatePath);
    when(libvirtComputingResource.getCmdsTimeout()).thenReturn(1);
    final String templateFolder = command.getAccountId() + File.separator + command.getNewTemplateId();
    final String templateInstallFolder = "template/tmpl/" + templateFolder;
    final String tmplPath = secondaryPool.getLocalPath() + File.separator + templateInstallFolder;
    when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
    when(libvirtUtilitiesHelper.buildTemplateLocation(storage, tmplPath)).thenReturn(location);
    when(libvirtUtilitiesHelper.generateUuidName()).thenReturn(tmplName);
    try {
        when(libvirtUtilitiesHelper.buildQcow2Processor(storage)).thenReturn(qcow2Processor);
        when(qcow2Processor.process(tmplPath, null, tmplName)).thenReturn(info);
    } catch (final ConfigurationException e) {
        fail(e.getMessage());
    } catch (final InternalErrorException e) {
        fail(e.getMessage());
    }
    final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
    assertNotNull(wrapper);
    final Answer answer = wrapper.execute(command, libvirtComputingResource);
    assertTrue(answer.getResult());
    verify(libvirtComputingResource, times(1)).getStoragePoolMgr();
    verify(storagePoolMgr, times(1)).getStoragePoolByUri(command.getSecondaryStorageUrl() + snapshotPath);
    verify(storagePoolMgr, times(1)).getStoragePoolByUri(command.getSecondaryStorageUrl());
}
Also used : NfsStoragePool(com.cloud.hypervisor.kvm.resource.KvmHaBase.NfsStoragePool) StoragePool(com.cloud.storage.StoragePool) KvmStoragePool(com.cloud.hypervisor.kvm.storage.KvmStoragePool) KvmStoragePool(com.cloud.hypervisor.kvm.storage.KvmStoragePool) StorageLayer(com.cloud.storage.StorageLayer) Processor(com.cloud.storage.template.Processor) InternalErrorException(com.cloud.exception.InternalErrorException) LibvirtUtilitiesHelper(com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper) Answer(com.cloud.agent.api.Answer) CheckRouterAnswer(com.cloud.agent.api.CheckRouterAnswer) AttachAnswer(com.cloud.storage.command.AttachAnswer) LibvirtRequestWrapper(com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper) ConfigurationException(javax.naming.ConfigurationException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) TemplateLocation(com.cloud.storage.template.TemplateLocation) CreatePrivateTemplateFromSnapshotCommand(com.cloud.agent.api.CreatePrivateTemplateFromSnapshotCommand) KvmStoragePoolManager(com.cloud.hypervisor.kvm.storage.KvmStoragePoolManager) KvmPhysicalDisk(com.cloud.hypervisor.kvm.storage.KvmPhysicalDisk) FormatInfo(com.cloud.storage.template.Processor.FormatInfo) Test(org.junit.Test)

Example 72 with KvmStoragePool

use of com.cloud.hypervisor.kvm.storage.KvmStoragePool in project cosmic by MissionCriticalCloud.

the class LibvirtComputingResourceTest method testCreateCommandCLVM.

@Test
public void testCreateCommandCLVM() {
    final DiskProfile diskCharacteristics = Mockito.mock(DiskProfile.class);
    final StorageFilerTO pool = Mockito.mock(StorageFilerTO.class);
    final String templateUrl = "http://template";
    final boolean executeInSequence = false;
    final CreateCommand command = new CreateCommand(diskCharacteristics, templateUrl, pool, executeInSequence);
    final KvmStoragePoolManager poolManager = Mockito.mock(KvmStoragePoolManager.class);
    final KvmStoragePool primary = Mockito.mock(KvmStoragePool.class);
    final KvmPhysicalDisk vol = Mockito.mock(KvmPhysicalDisk.class);
    final KvmPhysicalDisk baseVol = Mockito.mock(KvmPhysicalDisk.class);
    when(libvirtComputingResource.getStoragePoolMgr()).thenReturn(poolManager);
    when(poolManager.getStoragePool(pool.getType(), pool.getUuid())).thenReturn(primary);
    when(primary.getPhysicalDisk(command.getTemplateUrl())).thenReturn(baseVol);
    when(poolManager.createDiskFromTemplate(baseVol, diskCharacteristics.getPath(), diskCharacteristics.getProvisioningType(), primary, 0)).thenReturn(vol);
    final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
    assertNotNull(wrapper);
    final Answer answer = wrapper.execute(command, libvirtComputingResource);
    assertTrue(answer.getResult());
    verify(libvirtComputingResource, times(1)).getStoragePoolMgr();
    verify(poolManager, times(1)).getStoragePool(pool.getType(), pool.getUuid());
}
Also used : Answer(com.cloud.agent.api.Answer) CheckRouterAnswer(com.cloud.agent.api.CheckRouterAnswer) AttachAnswer(com.cloud.storage.command.AttachAnswer) LibvirtRequestWrapper(com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper) CreateCommand(com.cloud.agent.api.storage.CreateCommand) KvmStoragePool(com.cloud.hypervisor.kvm.storage.KvmStoragePool) KvmStoragePoolManager(com.cloud.hypervisor.kvm.storage.KvmStoragePoolManager) KvmPhysicalDisk(com.cloud.hypervisor.kvm.storage.KvmPhysicalDisk) DiskProfile(com.cloud.vm.DiskProfile) StorageFilerTO(com.cloud.agent.api.to.StorageFilerTO) Test(org.junit.Test)

Example 73 with KvmStoragePool

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

the class LibvirtComputingResource method templateToPrimaryDownload.

// this is much like PrimaryStorageDownloadCommand, but keeping it separate
public KVMPhysicalDisk templateToPrimaryDownload(final String templateUrl, final KVMStoragePool primaryPool, final String volUuid) {
    final int index = templateUrl.lastIndexOf("/");
    final String mountpoint = templateUrl.substring(0, index);
    String templateName = null;
    if (index < templateUrl.length() - 1) {
        templateName = templateUrl.substring(index + 1);
    }
    KVMPhysicalDisk templateVol = null;
    KVMStoragePool secondaryPool = null;
    try {
        secondaryPool = _storagePoolMgr.getStoragePoolByURI(mountpoint);
        /* Get template vol */
        if (templateName == null) {
            secondaryPool.refresh();
            final List<KVMPhysicalDisk> disks = secondaryPool.listPhysicalDisks();
            if (disks == null || disks.isEmpty()) {
                s_logger.error("Failed to get volumes from pool: " + secondaryPool.getUuid());
                return null;
            }
            for (final KVMPhysicalDisk disk : disks) {
                if (disk.getName().endsWith("qcow2")) {
                    templateVol = disk;
                    break;
                }
            }
            if (templateVol == null) {
                s_logger.error("Failed to get template from pool: " + secondaryPool.getUuid());
                return null;
            }
        } else {
            templateVol = secondaryPool.getPhysicalDisk(templateName);
        }
        /* Copy volume to primary storage */
        final KVMPhysicalDisk primaryVol = _storagePoolMgr.copyPhysicalDisk(templateVol, volUuid, primaryPool, 0);
        return primaryVol;
    } catch (final CloudRuntimeException e) {
        s_logger.error("Failed to download template to primary storage", e);
        return null;
    } finally {
        if (secondaryPool != null) {
            _storagePoolMgr.deleteStoragePool(secondaryPool.getType(), secondaryPool.getUuid());
        }
    }
}
Also used : KVMStoragePool(com.cloud.hypervisor.kvm.storage.KVMStoragePool) KVMPhysicalDisk(com.cloud.hypervisor.kvm.storage.KVMPhysicalDisk) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException)

Example 74 with KvmStoragePool

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

the class LibvirtComputingResource method getVolumePath.

public String getVolumePath(final Connect conn, final DiskTO volume, boolean diskOnHostCache) throws LibvirtException, URISyntaxException {
    final DataTO data = volume.getData();
    final DataStoreTO store = data.getDataStore();
    if (volume.getType() == Volume.Type.ISO && data.getPath() != null && (store instanceof NfsTO || store instanceof PrimaryDataStoreTO && data instanceof TemplateObjectTO && !((TemplateObjectTO) data).isDirectDownload())) {
        if (data.getPath().startsWith(ConfigDrive.CONFIGDRIVEDIR) && diskOnHostCache) {
            String configDrivePath = getConfigPath() + "/" + data.getPath();
            return configDrivePath;
        }
        final String isoPath = store.getUrl().split("\\?")[0] + File.separator + data.getPath();
        final int index = isoPath.lastIndexOf("/");
        final String path = isoPath.substring(0, index);
        final String name = isoPath.substring(index + 1);
        final KVMStoragePool secondaryPool = _storagePoolMgr.getStoragePoolByURI(path);
        final KVMPhysicalDisk isoVol = secondaryPool.getPhysicalDisk(name);
        return isoVol.getPath();
    } else {
        return data.getPath();
    }
}
Also used : PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) DataTO(com.cloud.agent.api.to.DataTO) KVMStoragePool(com.cloud.hypervisor.kvm.storage.KVMStoragePool) PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) KVMPhysicalDisk(com.cloud.hypervisor.kvm.storage.KVMPhysicalDisk) TemplateObjectTO(org.apache.cloudstack.storage.to.TemplateObjectTO) NfsTO(com.cloud.agent.api.to.NfsTO)

Example 75 with KvmStoragePool

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

the class LibvirtComputingResource method attachOrDetachISO.

public synchronized String attachOrDetachISO(final Connect conn, final String vmName, String isoPath, final boolean isAttach, final Integer diskSeq) throws LibvirtException, URISyntaxException, InternalErrorException {
    final DiskDef iso = new DiskDef();
    if (isoPath != null && isAttach) {
        final int index = isoPath.lastIndexOf("/");
        final String path = isoPath.substring(0, index);
        final String name = isoPath.substring(index + 1);
        final KVMStoragePool secondaryPool = _storagePoolMgr.getStoragePoolByURI(path);
        final KVMPhysicalDisk isoVol = secondaryPool.getPhysicalDisk(name);
        isoPath = isoVol.getPath();
        iso.defISODisk(isoPath, diskSeq);
    } else {
        iso.defISODisk(null, diskSeq);
    }
    final String result = attachOrDetachDevice(conn, true, vmName, iso.toString());
    if (result == null && !isAttach) {
        final List<DiskDef> disks = getDisks(conn, vmName);
        for (final DiskDef disk : disks) {
            if (disk.getDeviceType() == DiskDef.DeviceType.CDROM && (diskSeq == null || disk.getDiskLabel() == iso.getDiskLabel())) {
                cleanupDisk(disk);
            }
        }
    }
    return result;
}
Also used : DiskDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.DiskDef) KVMStoragePool(com.cloud.hypervisor.kvm.storage.KVMStoragePool) KVMPhysicalDisk(com.cloud.hypervisor.kvm.storage.KVMPhysicalDisk)

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