Search in sources :

Example 11 with KvmPhysicalDisk

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

the class LibvirtComputingResourceTest method testPrimaryStorageDownloadCommandNOTemplateNOQcow2.

@Test
public void testPrimaryStorageDownloadCommandNOTemplateNOQcow2() {
    final StoragePool pool = Mockito.mock(StoragePool.class);
    final List<KvmPhysicalDisk> disks = new ArrayList<>();
    final List<KvmPhysicalDisk> spiedDisks = Mockito.spy(disks);
    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(this.libvirtComputingResource.getStoragePoolMgr()).thenReturn(storagePoolMgr);
    when(storagePoolMgr.getStoragePoolByUri(mountpoint)).thenReturn(secondaryPool);
    when(secondaryPool.listPhysicalDisks()).thenReturn(spiedDisks);
    when(spiedDisks.isEmpty()).thenReturn(false);
    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)

Example 12 with KvmPhysicalDisk

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

the class LibvirtComputingResourceTest method testResizeVolumeCommandShrink.

@Test
@Ignore
public void testResizeVolumeCommandShrink() {
    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 = true;
    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);
    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);
    final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
    assertNotNull(wrapper);
    final Answer answer = wrapper.execute(command, this.libvirtComputingResource);
    assertFalse(answer.getResult());
}
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) 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) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 13 with KvmPhysicalDisk

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

the class LibvirtComputingResourceTest method testCreatePrivateTemplateFromSnapshotCommandConfigurationException.

@Test
public void testCreatePrivateTemplateFromSnapshotCommandConfigurationException() {
    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 TemplateFormatInfo info = Mockito.mock(TemplateFormatInfo.class);
    when(this.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(this.libvirtComputingResource.getStorage()).thenReturn(storage);
    when(this.libvirtComputingResource.createTmplPath()).thenReturn(templatePath);
    when(this.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(this.libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
    when(libvirtUtilitiesHelper.buildTemplateLocation(storage, tmplPath)).thenReturn(location);
    when(libvirtUtilitiesHelper.generateUuidName()).thenReturn(tmplName);
    try {
        when(libvirtUtilitiesHelper.buildQcow2Processor(storage)).thenThrow(ConfigurationException.class);
        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, this.libvirtComputingResource);
    assertFalse(answer.getResult());
    verify(this.libvirtComputingResource, times(1)).getStoragePoolMgr();
    verify(storagePoolMgr, times(1)).getStoragePoolByUri(command.getSecondaryStorageUrl() + snapshotPath);
    verify(storagePoolMgr, times(1)).getStoragePoolByUri(command.getSecondaryStorageUrl());
}
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) StorageLayer(com.cloud.utils.storage.StorageLayer) Processor(com.cloud.common.storageprocessor.Processor) InternalErrorException(com.cloud.legacymodel.exceptions.InternalErrorException) 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) ConfigurationException(javax.naming.ConfigurationException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) TemplateLocation(com.cloud.common.storageprocessor.TemplateLocation) CreatePrivateTemplateFromSnapshotCommand(com.cloud.legacymodel.communication.command.CreatePrivateTemplateFromSnapshotCommand) KvmStoragePoolManager(com.cloud.agent.resource.kvm.storage.KvmStoragePoolManager) TemplateFormatInfo(com.cloud.legacymodel.storage.TemplateFormatInfo) KvmPhysicalDisk(com.cloud.agent.resource.kvm.storage.KvmPhysicalDisk) Test(org.junit.Test)

Example 14 with KvmPhysicalDisk

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

the class LibvirtComputingResource method createVbd.

public void createVbd(final Connect conn, final VirtualMachineTO vmSpec, final String vmName, final LibvirtVmDef vm) throws InternalErrorException, LibvirtException, URISyntaxException {
    final List<DiskTO> disks = Arrays.asList(vmSpec.getDisks());
    Collections.sort(disks, new Comparator<DiskTO>() {

        @Override
        public int compare(final DiskTO arg0, final DiskTO arg1) {
            return arg0.getDiskSeq() > arg1.getDiskSeq() ? 1 : -1;
        }
    });
    for (final DiskTO volume : disks) {
        KvmPhysicalDisk physicalDisk = null;
        KvmStoragePool pool = null;
        final DataTO data = volume.getData();
        if (volume.getType() == VolumeType.ISO && data.getPath() != null) {
            final NfsTO nfsStore = (NfsTO) data.getDataStore();
            final String volPath = nfsStore.getUrl() + File.separator + data.getPath();
            final int index = volPath.lastIndexOf("/");
            final String volDir = volPath.substring(0, index);
            final String volName = volPath.substring(index + 1);
            final KvmStoragePool secondaryStorage = this.storagePoolMgr.getStoragePoolByUri(volDir);
            physicalDisk = secondaryStorage.getPhysicalDisk(volName);
        } else if (volume.getType() != VolumeType.ISO) {
            final PrimaryDataStoreTO store = (PrimaryDataStoreTO) data.getDataStore();
            physicalDisk = this.storagePoolMgr.getPhysicalDisk(store.getPoolType(), store.getUuid(), data.getPath());
            pool = physicalDisk.getPool();
        }
        String volPath = null;
        if (physicalDisk != null) {
            volPath = physicalDisk.getPath();
        }
        // check for disk activity, if detected we should exit because vm is running elsewhere
        if (this.diskActivityCheckEnabled && physicalDisk != null && physicalDisk.getFormat() == PhysicalDiskFormat.QCOW2) {
            logger.debug("Checking physical disk file at path " + volPath + " for disk activity to ensure vm is not running elsewhere");
            try {
                HypervisorUtils.checkVolumeFileForActivity(volPath, this.diskActivityCheckTimeoutSeconds, this.diskActivityInactiveThresholdMilliseconds, this.diskActivityCheckFileSizeMin);
            } catch (final IOException ex) {
                throw new CloudRuntimeException("Unable to check physical disk file for activity", ex);
            }
            logger.debug("Disk activity check cleared");
        }
        final LibvirtDiskDef disk = new LibvirtDiskDef();
        if (volume.getType() == VolumeType.ISO) {
            if (volPath == null) {
                /* Add iso as placeholder */
                disk.defIsoDisk(null);
            } else {
                disk.defIsoDisk(volPath);
            }
        } else {
            final int devId = volume.getDiskSeq().intValue();
            if (volume.getDiskController() == DiskControllerType.SCSI) {
                disk.setQemuDriver(true);
                disk.setDiscard(DiscardType.UNMAP);
            }
            disk.setImageFormat(volume.getDiskFormat());
            if (pool.getType() == StoragePoolType.RBD) {
                /*
                     * For RBD pools we use the secret mechanism in libvirt. We store the secret under the UUID of the pool,
                     * that's why we pass the pool's UUID as the authSecret
                     */
                disk.defNetworkBasedDisk(physicalDisk.getPath().replace("rbd:", ""), pool.getSourceHost(), pool.getSourcePort(), pool.getAuthUserName(), pool.getUuid(), devId, volume.getDiskController(), DiskProtocol.RBD, ImageFormat.RAW);
            } else if (pool.getType() == StoragePoolType.Gluster) {
                final String mountpoint = pool.getLocalPath();
                final String path = physicalDisk.getPath();
                final String glusterVolume = pool.getSourceDir().replace("/", "");
                disk.defNetworkBasedDisk(glusterVolume + path.replace(mountpoint, ""), pool.getSourceHost(), pool.getSourcePort(), null, null, devId, volume.getDiskController(), DiskProtocol.GLUSTER, ImageFormat.QCOW2);
            } else if (pool.getType() == StoragePoolType.CLVM || pool.getType() == StoragePoolType.LVM) {
                disk.defBlockBasedDisk(physicalDisk.getPath(), devId, volume.getDiskController());
            } else if (pool.getType() == StoragePoolType.NetworkFilesystem) {
                disk.defFileBasedDisk(physicalDisk.getPath(), devId, volume.getDiskController(), volume.getDiskFormat());
            } else {
                disk.defFileBasedDisk(physicalDisk.getPath(), devId, volume.getDiskController(), volume.getDiskFormat());
            }
        }
        if (data instanceof VolumeObjectTO) {
            final VolumeObjectTO volumeObjectTo = (VolumeObjectTO) data;
            disk.setSerial(volumeObjectTo.getDeviceId() + "-" + diskUuidToSerial(volumeObjectTo.getUuid()));
            disk.setDeviceId(volumeObjectTo.getDeviceId().intValue());
            if (volumeObjectTo.getBytesReadRate() != null && volumeObjectTo.getBytesReadRate() > 0) {
                disk.setBytesReadRate(volumeObjectTo.getBytesReadRate());
            }
            if (volumeObjectTo.getBytesWriteRate() != null && volumeObjectTo.getBytesWriteRate() > 0) {
                disk.setBytesWriteRate(volumeObjectTo.getBytesWriteRate());
            }
            if (volumeObjectTo.getIopsReadRate() != null && volumeObjectTo.getIopsReadRate() > 0) {
                disk.setIopsReadRate(volumeObjectTo.getIopsReadRate());
            }
            if (volumeObjectTo.getIopsWriteRate() != null && volumeObjectTo.getIopsWriteRate() > 0) {
                disk.setIopsWriteRate(volumeObjectTo.getIopsWriteRate());
            }
            if (volumeObjectTo.getIopsTotalRate() != null && volumeObjectTo.getIopsTotalRate() > 0) {
                disk.setIopsTotalRate(volumeObjectTo.getIopsTotalRate());
            }
            if (volumeObjectTo.getCacheMode() != null) {
                disk.setCacheMode(LibvirtDiskDef.DiskCacheMode.valueOf(volumeObjectTo.getCacheMode().toString().toUpperCase()));
            }
            if (volumeObjectTo.getFormat() != null) {
                physicalDisk.setFormat(physicalDisk.getPhysicalDiskFormatFromImageFormat(volumeObjectTo.getFormat()));
            }
        }
        logger.debug("Adding disk: " + disk.toString());
        vm.getDevices().addDevice(disk);
    }
    if (vmSpec.getType() != VirtualMachineType.User) {
        final String sysvmIsoPath = getSysvmIsoPath();
        if (sysvmIsoPath != null) {
            final LibvirtDiskDef iso = new LibvirtDiskDef();
            iso.defIsoDisk(sysvmIsoPath);
            vm.getDevices().addDevice(iso);
        }
    }
}
Also used : KvmStoragePool(com.cloud.agent.resource.kvm.storage.KvmStoragePool) IOException(java.io.IOException) NfsTO(com.cloud.legacymodel.to.NfsTO) DataTO(com.cloud.legacymodel.to.DataTO) LibvirtDiskDef(com.cloud.agent.resource.kvm.xml.LibvirtDiskDef) PrimaryDataStoreTO(com.cloud.legacymodel.to.PrimaryDataStoreTO) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) VolumeObjectTO(com.cloud.legacymodel.to.VolumeObjectTO) KvmPhysicalDisk(com.cloud.agent.resource.kvm.storage.KvmPhysicalDisk) DiskTO(com.cloud.legacymodel.to.DiskTO)

Example 15 with KvmPhysicalDisk

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

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 = this.storagePoolMgr.getStoragePoolByUri(mountpoint);
        /* Get template vol */
        if (templateName == null) {
            secondaryPool.refresh();
            final List<KvmPhysicalDisk> disks = secondaryPool.listPhysicalDisks();
            if (disks == null || disks.isEmpty()) {
                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) {
                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 = this.storagePoolMgr.copyPhysicalDisk(templateVol, volUuid, primaryPool, 0);
        return primaryVol;
    } catch (final CloudRuntimeException e) {
        logger.error("Failed to download template to primary storage", e);
        return null;
    } finally {
        if (secondaryPool != null) {
            this.storagePoolMgr.deleteStoragePool(secondaryPool.getType(), secondaryPool.getUuid());
        }
    }
}
Also used : KvmStoragePool(com.cloud.agent.resource.kvm.storage.KvmStoragePool) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) KvmPhysicalDisk(com.cloud.agent.resource.kvm.storage.KvmPhysicalDisk)

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