Search in sources :

Example 1 with DiskDef

use of com.cloud.hypervisor.kvm.resource.LibvirtVMDef.DiskDef 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) 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 = _storagePoolMgr.getStoragePoolByURI(path);
        final KVMPhysicalDisk isoVol = secondaryPool.getPhysicalDisk(name);
        isoPath = isoVol.getPath();
        final DiskDef iso = new DiskDef();
        iso.defISODisk(isoPath);
        isoXml = iso.toString();
    } else {
        final DiskDef iso = new DiskDef();
        iso.defISODisk(null);
        isoXml = iso.toString();
    }
    final List<DiskDef> disks = getDisks(conn, vmName);
    final String result = attachOrDetachDevice(conn, true, vmName, isoXml);
    if (result == null && !isAttach) {
        for (final DiskDef disk : disks) {
            if (disk.getDeviceType() == DiskDef.DeviceType.CDROM) {
                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)

Example 2 with DiskDef

use of com.cloud.hypervisor.kvm.resource.LibvirtVMDef.DiskDef in project cloudstack by apache.

the class KVMStorageProcessor method attachOrDetachISO.

protected 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 = storagePoolMgr.getStoragePoolByURI(path);
        final KVMPhysicalDisk isoVol = secondaryPool.getPhysicalDisk(name);
        isoPath = isoVol.getPath();
        final DiskDef iso = new DiskDef();
        iso.defISODisk(isoPath);
        isoXml = iso.toString();
    } else {
        final DiskDef iso = new DiskDef();
        iso.defISODisk(null);
        isoXml = iso.toString();
    }
    final List<DiskDef> disks = resource.getDisks(conn, vmName);
    final String result = attachOrDetachDevice(conn, true, vmName, isoXml);
    if (result == null && !isAttach) {
        for (final DiskDef disk : disks) {
            if (disk.getDeviceType() == DiskDef.DeviceType.CDROM) {
                resource.cleanupDisk(disk);
            }
        }
    }
    return result;
}
Also used : DiskDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.DiskDef)

Example 3 with DiskDef

use of com.cloud.hypervisor.kvm.resource.LibvirtVMDef.DiskDef in project cloudstack by apache.

the class KVMStorageProcessor method attachOrDetachISO.

protected synchronized String attachOrDetachISO(final Connect conn, final String vmName, String isoPath, final boolean isAttach, Map<String, String> params) throws LibvirtException, URISyntaxException, InternalErrorException {
    String isoXml = null;
    boolean isUefiEnabled = MapUtils.isNotEmpty(params) && params.containsKey("UEFI");
    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();
        final DiskDef iso = new DiskDef();
        iso.defISODisk(isoPath, isUefiEnabled);
        isoXml = iso.toString();
    } else {
        final DiskDef iso = new DiskDef();
        iso.defISODisk(null, isUefiEnabled);
        isoXml = iso.toString();
    }
    final List<DiskDef> disks = resource.getDisks(conn, vmName);
    final String result = attachOrDetachDevice(conn, true, vmName, isoXml);
    if (result == null && !isAttach) {
        for (final DiskDef disk : disks) {
            if (disk.getDeviceType() == DiskDef.DeviceType.CDROM) {
                resource.cleanupDisk(disk);
            }
        }
    }
    return result;
}
Also used : DiskDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.DiskDef)

Example 4 with DiskDef

use of com.cloud.hypervisor.kvm.resource.LibvirtVMDef.DiskDef in project cloudstack by apache.

the class KVMStorageProcessor method attachOrDetachDisk.

protected synchronized String attachOrDetachDisk(final Connect conn, final boolean attach, final String vmName, final KVMPhysicalDisk attachingDisk, final int devId, final String serial, final Long bytesReadRate, final Long bytesReadRateMax, final Long bytesReadRateMaxLength, final Long bytesWriteRate, final Long bytesWriteRateMax, final Long bytesWriteRateMaxLength, final Long iopsReadRate, final Long iopsReadRateMax, final Long iopsReadRateMaxLength, final Long iopsWriteRate, final Long iopsWriteRateMax, final Long iopsWriteRateMaxLength, final String cacheMode) throws LibvirtException, InternalErrorException {
    List<DiskDef> disks = null;
    Domain dm = null;
    DiskDef diskdef = null;
    final KVMStoragePool attachingPool = attachingDisk.getPool();
    try {
        dm = conn.domainLookupByName(vmName);
        final LibvirtDomainXMLParser parser = new LibvirtDomainXMLParser();
        final String domXml = dm.getXMLDesc(0);
        parser.parseDomainXML(domXml);
        disks = parser.getDisks();
        if (!attach) {
            if (attachingPool.getType() == StoragePoolType.RBD) {
                if (resource.getHypervisorType() == Hypervisor.HypervisorType.LXC) {
                    final String device = resource.mapRbdDevice(attachingDisk);
                    if (device != null) {
                        s_logger.debug("RBD device on host is: " + device);
                        attachingDisk.setPath(device);
                    }
                }
            }
            for (final DiskDef disk : disks) {
                final String file = disk.getDiskPath();
                if (file != null && file.equalsIgnoreCase(attachingDisk.getPath())) {
                    diskdef = disk;
                    break;
                }
            }
            if (diskdef == null) {
                throw new InternalErrorException("disk: " + attachingDisk.getPath() + " is not attached before");
            }
        } else {
            DiskDef.DiskBus busT = DiskDef.DiskBus.VIRTIO;
            for (final DiskDef disk : disks) {
                if (disk.getDeviceType() == DeviceType.DISK) {
                    if (disk.getBusType() == DiskDef.DiskBus.SCSI) {
                        busT = DiskDef.DiskBus.SCSI;
                    }
                    break;
                }
            }
            diskdef = new DiskDef();
            if (busT == DiskDef.DiskBus.SCSI) {
                diskdef.setQemuDriver(true);
                diskdef.setDiscard(DiscardType.UNMAP);
            }
            diskdef.setSerial(serial);
            if (attachingPool.getType() == StoragePoolType.RBD) {
                if (resource.getHypervisorType() == Hypervisor.HypervisorType.LXC) {
                    // For LXC, map image to host and then attach to Vm
                    final String device = resource.mapRbdDevice(attachingDisk);
                    if (device != null) {
                        s_logger.debug("RBD device on host is: " + device);
                        diskdef.defBlockBasedDisk(device, devId, busT);
                    } else {
                        throw new InternalErrorException("Error while mapping disk " + attachingDisk.getPath() + " on host");
                    }
                } else {
                    diskdef.defNetworkBasedDisk(attachingDisk.getPath(), attachingPool.getSourceHost(), attachingPool.getSourcePort(), attachingPool.getAuthUserName(), attachingPool.getUuid(), devId, busT, DiskProtocol.RBD, DiskDef.DiskFmtType.RAW);
                }
            } else if (attachingPool.getType() == StoragePoolType.Gluster) {
                final String mountpoint = attachingPool.getLocalPath();
                final String path = attachingDisk.getPath();
                final String glusterVolume = attachingPool.getSourceDir().replace("/", "");
                diskdef.defNetworkBasedDisk(glusterVolume + path.replace(mountpoint, ""), attachingPool.getSourceHost(), attachingPool.getSourcePort(), null, null, devId, busT, DiskProtocol.GLUSTER, DiskDef.DiskFmtType.QCOW2);
            } else if (attachingDisk.getFormat() == PhysicalDiskFormat.QCOW2) {
                diskdef.defFileBasedDisk(attachingDisk.getPath(), devId, busT, DiskDef.DiskFmtType.QCOW2);
            } else if (attachingDisk.getFormat() == PhysicalDiskFormat.RAW) {
                diskdef.defBlockBasedDisk(attachingDisk.getPath(), devId, busT);
            }
            if ((bytesReadRate != null) && (bytesReadRate > 0)) {
                diskdef.setBytesReadRate(bytesReadRate);
            }
            if ((bytesReadRateMax != null) && (bytesReadRateMax > 0)) {
                diskdef.setBytesReadRateMax(bytesReadRateMax);
            }
            if ((bytesReadRateMaxLength != null) && (bytesReadRateMaxLength > 0)) {
                diskdef.setBytesReadRateMaxLength(bytesReadRateMaxLength);
            }
            if ((bytesWriteRate != null) && (bytesWriteRate > 0)) {
                diskdef.setBytesWriteRate(bytesWriteRate);
            }
            if ((bytesWriteRateMax != null) && (bytesWriteRateMax > 0)) {
                diskdef.setBytesWriteRateMax(bytesWriteRateMax);
            }
            if ((bytesWriteRateMaxLength != null) && (bytesWriteRateMaxLength > 0)) {
                diskdef.setBytesWriteRateMaxLength(bytesWriteRateMaxLength);
            }
            if ((iopsReadRate != null) && (iopsReadRate > 0)) {
                diskdef.setIopsReadRate(iopsReadRate);
            }
            if ((iopsReadRateMax != null) && (iopsReadRateMax > 0)) {
                diskdef.setIopsReadRateMax(iopsReadRateMax);
            }
            if ((iopsReadRateMaxLength != null) && (iopsReadRateMaxLength > 0)) {
                diskdef.setIopsReadRateMaxLength(iopsReadRateMaxLength);
            }
            if ((iopsWriteRate != null) && (iopsWriteRate > 0)) {
                diskdef.setIopsWriteRate(iopsWriteRate);
            }
            if ((iopsWriteRateMax != null) && (iopsWriteRateMax > 0)) {
                diskdef.setIopsWriteRateMax(iopsWriteRateMax);
            }
            if ((iopsWriteRateMaxLength != null) && (iopsWriteRateMaxLength > 0)) {
                diskdef.setIopsWriteRateMaxLength(iopsWriteRateMaxLength);
            }
            if (cacheMode != null) {
                diskdef.setCacheMode(DiskDef.DiskCacheMode.valueOf(cacheMode.toUpperCase()));
            }
        }
        final String xml = diskdef.toString();
        return attachOrDetachDevice(conn, attach, vmName, xml);
    } finally {
        if (dm != null) {
            dm.free();
        }
    }
}
Also used : DiskDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.DiskDef) LibvirtDomainXMLParser(com.cloud.hypervisor.kvm.resource.LibvirtDomainXMLParser) InternalErrorException(com.cloud.exception.InternalErrorException) Domain(org.libvirt.Domain)

Example 5 with DiskDef

use of com.cloud.hypervisor.kvm.resource.LibvirtVMDef.DiskDef in project cloudstack by apache.

the class LibvirtComputingResourceTest method testMigrateCommand.

@Test
public void testMigrateCommand() {
    final Connect conn = Mockito.mock(Connect.class);
    final Connect dconn = Mockito.mock(Connect.class);
    final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
    final String vmName = "Test";
    final String destIp = "127.0.0.100";
    final boolean isWindows = false;
    final VirtualMachineTO vmTO = Mockito.mock(VirtualMachineTO.class);
    final boolean executeInSequence = false;
    DiskTO[] diskTOS = new DiskTO[] {};
    final MigrateCommand command = new MigrateCommand(vmName, destIp, isWindows, vmTO, executeInSequence);
    when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
    when(vmTO.getDisks()).thenReturn(diskTOS);
    try {
        when(libvirtUtilitiesHelper.getConnectionByVmName(vmName)).thenReturn(conn);
        when(libvirtUtilitiesHelper.retrieveQemuConnection("qemu+tcp://" + command.getDestinationIp() + "/system")).thenReturn(dconn);
    } catch (final LibvirtException e) {
        fail(e.getMessage());
    }
    final InterfaceDef interfaceDef = Mockito.mock(InterfaceDef.class);
    final List<InterfaceDef> ifaces = new ArrayList<InterfaceDef>();
    ifaces.add(interfaceDef);
    when(libvirtComputingResource.getInterfaces(conn, vmName)).thenReturn(ifaces);
    final DiskDef diskDef = Mockito.mock(DiskDef.class);
    final List<DiskDef> disks = new ArrayList<DiskDef>();
    disks.add(diskDef);
    when(libvirtComputingResource.getDisks(conn, vmName)).thenReturn(disks);
    final Domain dm = Mockito.mock(Domain.class);
    try {
        when(conn.domainLookupByName(vmName)).thenReturn(dm);
        when(libvirtComputingResource.getPrivateIp()).thenReturn("127.0.0.1");
        when(dm.getXMLDesc(8)).thenReturn("<domain type='kvm' id='3'>" + "  <devices>" + "    <graphics type='vnc' port='5900' autoport='yes' listen='10.10.10.1'>" + "      <listen type='address' address='10.10.10.1'/>" + "    </graphics>" + "  </devices>" + "</domain>");
        when(dm.getXMLDesc(1)).thenReturn("<domain type='kvm' id='3'>" + "  <devices>" + "    <graphics type='vnc' port='5900' autoport='yes' listen='10.10.10.1'>" + "      <listen type='address' address='10.10.10.1'/>" + "    </graphics>" + "  </devices>" + "</domain>");
        when(dm.isPersistent()).thenReturn(1);
        doNothing().when(dm).undefine();
    } catch (final LibvirtException e) {
        fail(e.getMessage());
    } catch (final Exception 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)).getLibvirtUtilitiesHelper();
    try {
        verify(libvirtUtilitiesHelper, times(1)).getConnectionByVmName(vmName);
        verify(libvirtUtilitiesHelper, times(1)).retrieveQemuConnection("qemu+tcp://" + command.getDestinationIp() + "/system");
    } catch (final LibvirtException e) {
        fail(e.getMessage());
    }
    verify(libvirtComputingResource, times(1)).getInterfaces(conn, vmName);
    verify(libvirtComputingResource, times(1)).getDisks(conn, vmName);
    try {
        verify(conn, times(1)).domainLookupByName(vmName);
    } catch (final LibvirtException e) {
        fail(e.getMessage());
    }
    try {
        verify(dm, times(1)).getXMLDesc(8);
    } catch (final Throwable t) {
        try {
            verify(dm, times(1)).getXMLDesc(1);
        } catch (final LibvirtException e) {
            fail(e.getMessage());
        }
    }
}
Also used : LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) ArrayList(java.util.ArrayList) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) XPathExpressionException(javax.xml.xpath.XPathExpressionException) URISyntaxException(java.net.URISyntaxException) LibvirtException(org.libvirt.LibvirtException) SAXException(org.xml.sax.SAXException) InternalErrorException(com.cloud.exception.InternalErrorException) ConfigurationException(javax.naming.ConfigurationException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) LibvirtUtilitiesHelper(com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper) MigrateCommand(com.cloud.agent.api.MigrateCommand) InterfaceDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef) 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) DiskDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.DiskDef) LibvirtRequestWrapper(com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper) Domain(org.libvirt.Domain) DiskTO(com.cloud.agent.api.to.DiskTO) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

DiskDef (com.cloud.hypervisor.kvm.resource.LibvirtVMDef.DiskDef)27 Domain (org.libvirt.Domain)10 Test (org.junit.Test)8 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)8 InterfaceDef (com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef)7 InternalErrorException (com.cloud.exception.InternalErrorException)6 LibvirtException (org.libvirt.LibvirtException)6 KVMStoragePool (com.cloud.hypervisor.kvm.storage.KVMStoragePool)4 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 Connect (org.libvirt.Connect)4 MigrateDiskInfo (com.cloud.agent.api.MigrateCommand.MigrateDiskInfo)3 KVMPhysicalDisk (com.cloud.hypervisor.kvm.storage.KVMPhysicalDisk)3 File (java.io.File)3 URISyntaxException (java.net.URISyntaxException)3 DomainBlockStats (org.libvirt.DomainBlockStats)3 VmStatsEntry (com.cloud.agent.api.VmStatsEntry)2 DiskTO (com.cloud.agent.api.to.DiskTO)2 DpdkTO (com.cloud.agent.api.to.DpdkTO)2 VirtualMachineTO (com.cloud.agent.api.to.VirtualMachineTO)2