Search in sources :

Example 36 with KvmStoragePool

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

the class LibvirtComputingResourceTest method testCreateCommand.

@Test
public void testCreateCommand() {
    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);
    when(this.libvirtComputingResource.getStoragePoolMgr()).thenReturn(poolManager);
    when(poolManager.getStoragePool(pool.getType(), pool.getUuid())).thenReturn(primary);
    when(primary.getType()).thenReturn(StoragePoolType.CLVM);
    when(this.libvirtComputingResource.templateToPrimaryDownload(command.getTemplateUrl(), primary, diskCharacteristics.getPath())).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 37 with KvmStoragePool

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

the class LibvirtComputingResourceTest method testCopyVolumeCommandToSecFalse.

@Test
public void testCopyVolumeCommandToSecFalse() {
    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())).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) KvmStoragePoolManager(com.cloud.agent.resource.kvm.storage.KvmStoragePoolManager) KvmPhysicalDisk(com.cloud.agent.resource.kvm.storage.KvmPhysicalDisk) Test(org.junit.Test)

Example 38 with KvmStoragePool

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

the class LibvirtComputingResource method attachOrDetachIso.

public synchronized String attachOrDetachIso(final Connect conn, final String vmName, String isoPath, final boolean isAttach) throws LibvirtException, URISyntaxException, InternalErrorException {
    String isoXml = null;
    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 = this.storagePoolMgr.getStoragePoolByUri(path);
        final KvmPhysicalDisk isoVol = secondaryPool.getPhysicalDisk(name);
        isoPath = isoVol.getPath();
        final LibvirtDiskDef iso = new LibvirtDiskDef();
        iso.defIsoDisk(isoPath);
        isoXml = iso.toString();
    } else {
        final LibvirtDiskDef iso = new LibvirtDiskDef();
        iso.defIsoDisk(null);
        isoXml = iso.toString();
    }
    final List<LibvirtDiskDef> disks = getDisks(conn, vmName);
    final String result = attachOrDetachDevice(conn, true, vmName, isoXml);
    if (result == null && !isAttach) {
        for (final LibvirtDiskDef disk : disks) {
            if (disk.getDeviceType() == LibvirtDiskDef.DeviceType.CDROM) {
                cleanupDisk(disk);
            }
        }
    }
    return result;
}
Also used : KvmStoragePool(com.cloud.agent.resource.kvm.storage.KvmStoragePool) LibvirtDiskDef(com.cloud.agent.resource.kvm.xml.LibvirtDiskDef) KvmPhysicalDisk(com.cloud.agent.resource.kvm.storage.KvmPhysicalDisk)

Example 39 with KvmStoragePool

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

the class LibvirtComputingResource method initializeLocalstorage.

private List<StartupLocalstorageCommand> initializeLocalstorage() {
    final List<StartupLocalstorageCommand> startupLocalstorageCommandList = new ArrayList<>();
    for (final AgentConfiguration.Localstorage localstorage : this.agentConfiguration.getLocalstorages()) {
        if (localstorage.getType() == StoragePoolType.LVM) {
            try {
                logger.debug("Found local LVM storage pool: " + localstorage.getPath() + ", with uuid: " + localstorage.getUuid() + ", in the agent configuration");
                final KvmStoragePool localStoragePool = this.storagePoolMgr.createStoragePool(localstorage.getUuid(), "localhost", -1, localstorage.getPath(), "", StoragePoolType.LVM);
                final StoragePoolInfo storagePoolInfo = new StoragePoolInfo();
                storagePoolInfo.setUuid(localstorage.getUuid());
                storagePoolInfo.setHost(getName());
                storagePoolInfo.setLocalPath(localstorage.getPath());
                storagePoolInfo.setPoolType(StoragePoolType.LVM);
                storagePoolInfo.setCapacityBytes(localStoragePool.getCapacity());
                storagePoolInfo.setAvailableBytes(localStoragePool.getAvailable());
                final StartupLocalstorageCommand startupLocalstorageCommand = new StartupLocalstorageCommand();
                startupLocalstorageCommand.setPoolInfo(storagePoolInfo);
                startupLocalstorageCommandList.add(startupLocalstorageCommand);
            } catch (final CloudRuntimeException e) {
                logger.debug("Unable to initialize local storage pool: " + e);
            }
        }
    }
    return startupLocalstorageCommandList;
}
Also used : KvmStoragePool(com.cloud.agent.resource.kvm.storage.KvmStoragePool) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) StoragePoolInfo(com.cloud.legacymodel.storage.StoragePoolInfo) ArrayList(java.util.ArrayList) AgentConfiguration(com.cloud.agent.service.AgentConfiguration) StartupLocalstorageCommand(com.cloud.legacymodel.communication.command.startup.StartupLocalstorageCommand)

Example 40 with KvmStoragePool

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

the class LibvirtCreatePrivateTemplateFromVolumeCommandWrapper method execute.

@Override
public Answer execute(final CreatePrivateTemplateFromVolumeCommand command, final LibvirtComputingResource libvirtComputingResource) {
    final String secondaryStorageUrl = command.getSecondaryStorageUrl();
    KvmStoragePool secondaryStorage = null;
    KvmStoragePool primary = null;
    final KvmStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr();
    try {
        final String templateFolder = command.getAccountId() + File.separator + command.getTemplateId() + File.separator;
        final String templateInstallFolder = "/template/tmpl/" + templateFolder;
        secondaryStorage = storagePoolMgr.getStoragePoolByUri(secondaryStorageUrl);
        try {
            primary = storagePoolMgr.getStoragePool(command.getPool().getType(), command.getPrimaryStoragePoolNameLabel());
        } catch (final CloudRuntimeException e) {
            if (e.getMessage().contains("not found")) {
                primary = storagePoolMgr.createStoragePool(command.getPool().getUuid(), command.getPool().getHost(), command.getPool().getPort(), command.getPool().getPath(), command.getPool().getUserInfo(), command.getPool().getType());
            } else {
                return new CreatePrivateTemplateAnswer(command, false, e.getMessage());
            }
        }
        final KvmPhysicalDisk disk = primary.getPhysicalDisk(command.getVolumePath());
        final String tmpltPath = secondaryStorage.getLocalPath() + File.separator + templateInstallFolder;
        final StorageLayer storage = libvirtComputingResource.getStorage();
        storage.mkdirs(tmpltPath);
        if (primary.getType() != StoragePoolType.RBD) {
            final String createTmplPath = libvirtComputingResource.createTmplPath();
            final int cmdsTimeout = libvirtComputingResource.getCmdsTimeout();
            final Script scriptCommand = new Script(createTmplPath, cmdsTimeout, s_logger);
            scriptCommand.add("-f", disk.getPath());
            scriptCommand.add("-t", tmpltPath);
            scriptCommand.add("-n", command.getUniqueName() + ".qcow2");
            final String result = scriptCommand.execute();
            if (result != null) {
                s_logger.debug("failed to create template: " + result);
                return new CreatePrivateTemplateAnswer(command, false, result);
            }
        } else {
            s_logger.debug("Converting RBD disk " + disk.getPath() + " into template " + command.getUniqueName());
            final QemuImgFile srcFile = new QemuImgFile(KvmPhysicalDisk.rbdStringBuilder(primary.getSourceHost(), primary.getSourcePort(), primary.getAuthUserName(), primary.getAuthSecret(), disk.getPath()));
            srcFile.setFormat(PhysicalDiskFormat.RAW);
            final QemuImgFile destFile = new QemuImgFile(tmpltPath + "/" + command.getUniqueName() + ".qcow2");
            destFile.setFormat(PhysicalDiskFormat.QCOW2);
            final QemuImg q = new QemuImg(0);
            try {
                q.convert(srcFile, destFile);
            } catch (final QemuImgException e) {
                s_logger.error("Failed to create new template while converting " + srcFile.getFileName() + " to " + destFile.getFileName() + " the error was: " + e.getMessage());
            }
            final File templateProp = new File(tmpltPath + "/template.properties");
            if (!templateProp.exists()) {
                templateProp.createNewFile();
            }
            String templateContent = "filename=" + command.getUniqueName() + ".qcow2" + System.getProperty("line.separator");
            final DateFormat dateFormat = new SimpleDateFormat("MM_dd_yyyy");
            final Date date = new Date();
            templateContent += "snapshot.name=" + dateFormat.format(date) + System.getProperty("line.separator");
            try (final FileOutputStream templFo = new FileOutputStream(templateProp)) {
                templFo.write(templateContent.getBytes("UTF-8"));
                templFo.flush();
            } catch (final IOException ex) {
                s_logger.error("CreatePrivateTemplateAnswer:Exception:" + ex.getMessage());
            }
        }
        final Map<String, Object> params = new HashMap<>();
        params.put(StorageLayer.InstanceConfigKey, storage);
        final Processor qcow2Processor = new QCOW2Processor();
        qcow2Processor.configure("QCOW2 Processor", params);
        final TemplateFormatInfo info = qcow2Processor.process(tmpltPath, null, command.getUniqueName());
        final TemplateLocation loc = new TemplateLocation(storage, tmpltPath);
        loc.create(1, true, command.getUniqueName());
        loc.addFormat(info);
        loc.save();
        return new CreatePrivateTemplateAnswer(command, true, null, templateInstallFolder + command.getUniqueName() + ".qcow2", info.virtualSize, info.size, command.getUniqueName(), ImageFormat.QCOW2);
    } catch (final InternalErrorException e) {
        return new CreatePrivateTemplateAnswer(command, false, e.toString());
    } catch (final IOException e) {
        return new CreatePrivateTemplateAnswer(command, false, e.toString());
    } catch (final ConfigurationException e) {
        return new CreatePrivateTemplateAnswer(command, false, e.toString());
    } catch (final CloudRuntimeException e) {
        return new CreatePrivateTemplateAnswer(command, false, e.toString());
    } finally {
        if (secondaryStorage != null) {
            storagePoolMgr.deleteStoragePool(secondaryStorage.getType(), secondaryStorage.getUuid());
        }
    }
}
Also used : KvmStoragePool(com.cloud.agent.resource.kvm.storage.KvmStoragePool) StorageLayer(com.cloud.utils.storage.StorageLayer) QCOW2Processor(com.cloud.common.storageprocessor.QCOW2Processor) Processor(com.cloud.common.storageprocessor.Processor) HashMap(java.util.HashMap) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) TemplateLocation(com.cloud.common.storageprocessor.TemplateLocation) QemuImgException(com.cloud.agent.resource.kvm.storage.utils.QemuImgException) KvmStoragePoolManager(com.cloud.agent.resource.kvm.storage.KvmStoragePoolManager) KvmPhysicalDisk(com.cloud.agent.resource.kvm.storage.KvmPhysicalDisk) Script(com.cloud.utils.script.Script) IOException(java.io.IOException) InternalErrorException(com.cloud.legacymodel.exceptions.InternalErrorException) Date(java.util.Date) QemuImg(com.cloud.agent.resource.kvm.storage.utils.QemuImg) QCOW2Processor(com.cloud.common.storageprocessor.QCOW2Processor) QemuImgFile(com.cloud.agent.resource.kvm.storage.utils.QemuImgFile) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) FileOutputStream(java.io.FileOutputStream) TemplateFormatInfo(com.cloud.legacymodel.storage.TemplateFormatInfo) CreatePrivateTemplateAnswer(com.cloud.legacymodel.communication.answer.CreatePrivateTemplateAnswer) File(java.io.File) QemuImgFile(com.cloud.agent.resource.kvm.storage.utils.QemuImgFile) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

KvmStoragePool (com.cloud.agent.resource.kvm.storage.KvmStoragePool)46 KvmStoragePoolManager (com.cloud.agent.resource.kvm.storage.KvmStoragePoolManager)41 KvmPhysicalDisk (com.cloud.agent.resource.kvm.storage.KvmPhysicalDisk)34 Answer (com.cloud.legacymodel.communication.answer.Answer)31 LibvirtRequestWrapper (com.cloud.agent.resource.kvm.wrapper.LibvirtRequestWrapper)28 Test (org.junit.Test)28 AttachAnswer (com.cloud.legacymodel.communication.answer.AttachAnswer)27 CheckRouterAnswer (com.cloud.legacymodel.communication.answer.CheckRouterAnswer)27 NfsStoragePool (com.cloud.agent.resource.kvm.ha.KvmHaBase.NfsStoragePool)20 StoragePool (com.cloud.legacymodel.storage.StoragePool)20 StorageFilerTO (com.cloud.legacymodel.to.StorageFilerTO)17 CloudRuntimeException (com.cloud.legacymodel.exceptions.CloudRuntimeException)15 LibvirtUtilitiesHelper (com.cloud.agent.resource.kvm.wrapper.LibvirtUtilitiesHelper)12 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 LibvirtException (org.libvirt.LibvirtException)6 TemplateFormatInfo (com.cloud.legacymodel.storage.TemplateFormatInfo)5