Search in sources :

Example 1 with LibvirtDomainXmlParser

use of com.cloud.agent.resource.kvm.xml.LibvirtDomainXmlParser in project cosmic by MissionCriticalCloud.

the class LibvirtDomainXMLParserTest method testDomainXMLParser.

public void testDomainXMLParser() {
    final int vncPort = 5900;
    final DiskControllerType diskBus = DiskControllerType.SCSI;
    final LibvirtDiskDef.DiskType diskType = LibvirtDiskDef.DiskType.FILE;
    final LibvirtDiskDef.DeviceType deviceType = LibvirtDiskDef.DeviceType.DISK;
    final ImageFormat imageFormat = ImageFormat.QCOW2;
    final LibvirtDiskDef.DiskCacheMode diskCache = LibvirtDiskDef.DiskCacheMode.NONE;
    final NicModel ifModel = NicModel.VIRTIO;
    final GuestNetType ifType = GuestNetType.BRIDGE;
    final String diskLabel = "vda";
    final String diskPath = "/var/lib/libvirt/images/my-test-image.qcow2";
    final String xml = "<domain type='kvm' id='10'>" + "<name>s-2970-VM</name>" + "<uuid>4d2c1526-865d-4fc9-a1ac-dbd1801a22d0</uuid>" + "<description>Debian GNU/Linux 6(64-bit)</description>" + "<memory unit='KiB'>262144</memory>" + "<currentMemory unit='KiB'>262144</currentMemory>" + "<vcpu placement='static'>1</vcpu>" + "<cputune>" + "<shares>250</shares>" + "</cputune>" + "<resource>" + "<partition>/machine</partition>" + "</resource>" + "<os>" + "<type arch='x86_64' machine='pc-i440fx-1.5'>hvm</type>" + "<boot dev='cdrom'/>" + "<boot dev='hd'/>" + "</os>" + "<features>" + "<acpi/>" + "<apic/>" + "<pae/>" + "</features>" + "<clock offset='utc'/>" + "<on_poweroff>destroy</on_poweroff>" + "<on_reboot>restart</on_reboot>" + "<on_crash>destroy</on_crash>" + "<devices>" + "<emulator>/usr/bin/kvm-spice</emulator>" + "<disk type='" + diskType.toString() + "' device='" + deviceType.toString() + "'>" + "<driver name='qemu' type='" + imageFormat.toString() + "' cache='" + diskCache.toString() + "'/>" + "<source file='" + diskPath + "'/>" + "<target dev='" + diskLabel + "' bus='" + diskBus.toString() + "'/>" + "<alias name='virtio-disk0'/>" + "<address type='pci' domain='0x0000' bus='0x00' slot='0x08' function='0x0'/>" + "</disk>" + "<disk type='file' device='cdrom'>" + "<driver name='qemu' type='raw' cache='none'/>" + "<source file='/opt/cosmic/agent/vms/systemvm.iso'/>" + "<target dev='hdc' bus='ide'/>" + "<readonly/>" + "<alias name='ide0-1-0'/>" + "<address type='drive' controller='0' bus='1' target='0' unit='0'/>" + "</disk>" + "<controller type='usb' index='0'>" + "<alias name='usb0'/>" + "<address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x2'/>" + "</controller>" + "<controller type='pci' index='0' model='pci-root'>" + "<alias name='pci0'/>" + "</controller>" + "<controller type='ide' index='0'>" + "<alias name='ide0'/>" + "<address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>" + "</controller>" + "<controller type='virtio-serial' index='0'>" + "<alias name='virtio-serial0'/>" + "<address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/>" + "</controller>" + "<interface type='" + ifType.toString() + "'>" + "<mac address='0e:00:a9:fe:02:00'/>" + "<source bridge='cloud0'/>" + "<target dev='vnet0'/>" + "<model type='" + ifModel.toString() + "'/>" + "<alias name='net0'/>" + "<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>" + "</interface>" + "<interface type='" + ifType.toString() + "'>" + "<mac address='06:c5:94:00:05:65'/>" + "<source bridge='cloudbr1'/>" + "<target dev='vnet1'/>" + "<model type='" + ifModel.toString() + "'/>" + "<alias name='net1'/>" + "<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>" + "</interface>" + "<interface type='" + ifType.toString() + "'>" + "<mac address='06:c9:f4:00:04:40'/>" + "<source bridge='cloudbr0'/>" + "<target dev='vnet2'/>" + "<model type='" + ifModel.toString() + "'/>" + "<alias name='net2'/>" + "<address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>" + "</interface>" + "<interface type='" + ifType.toString() + "'>" + "<mac address='06:7e:c6:00:05:68'/>" + "<source bridge='cloudbr1'/>" + "<target dev='vnet3'/>" + "<model type='" + ifModel.toString() + "'/>" + "<alias name='net3'/>" + "<address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>" + "</interface>" + "<serial type='pty'>" + "<source path='/dev/pts/3'/>" + "<target port='0'/>" + "<alias name='serial0'/>" + "</serial>" + "<console type='pty' tty='/dev/pts/3'>" + "<source path='/dev/pts/3'/>" + "<target type='serial' port='0'/>" + "<alias name='serial0'/>" + "</console>" + "<channel type='unix'>" + "<source mode='bind' path='/var/lib/libvirt/qemu/s-2970-VM.agent'/>" + "<target type='virtio' name='s-2970-VM.vport'/>" + "<alias name='channel0'/>" + "<address type='virtio-serial' controller='0' bus='0' port='1'/>" + "</channel>" + "<input type='tablet' bus='usb'>" + "<alias name='input0'/>" + "</input>" + "<input type='mouse' bus='ps2'/>" + "<graphics type='vnc' port='" + vncPort + "' autoport='yes' listen='0.0.0.0'>" + "<listen type='address' address='0.0.0.0'/>" + "</graphics>" + "<video>" + "<model type='cirrus' vram='9216' heads='1'/>" + "<alias name='video0'/>" + "<address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>" + "</video>" + "<memballoon model='virtio'>" + "<alias name='balloon0'/>" + "<address type='pci' domain='0x0000' bus='0x00' slot='0x09' function='0x0'/>" + "</memballoon>" + "<rng model='virtio'>" + "<backend model='random'>/dev/random</backend>" + "</rng>" + "<watchdog model='i6300esb' action='reset'/>" + "<watchdog model='ib700' action='poweroff'/>" + "</devices>" + "<seclabel type='none'/>" + "</domain>";
    final LibvirtDomainXmlParser parser = new LibvirtDomainXmlParser();
    parser.parseDomainXml(xml);
    assertEquals(vncPort - 5900, (int) parser.getVncPort());
    final List<LibvirtDiskDef> disks = parser.getDisks();
    /* Disk 0 is the first disk, the QCOW2 file backed virto disk */
    final int diskId = 0;
    assertEquals(diskLabel, disks.get(diskId).getDiskLabel());
    assertEquals(diskPath, disks.get(diskId).getDiskPath());
    assertEquals(diskCache, disks.get(diskId).getCacheMode());
    assertEquals(diskBus, disks.get(diskId).getBusType());
    assertEquals(diskType, disks.get(diskId).getDiskType());
    assertEquals(deviceType, disks.get(diskId).getDeviceType());
    assertEquals(imageFormat, disks.get(diskId).getDiskFormatType());
    final List<InterfaceDef> ifs = parser.getInterfaces();
    for (int i = 0; i < ifs.size(); i++) {
        assertEquals(ifModel, ifs.get(i).getModel());
        assertEquals(ifType, ifs.get(i).getNetType());
    }
    final List<RngDef> rngs = parser.getRngs();
    assertEquals("/dev/random", rngs.get(0).getPath());
    assertEquals(RngBackendModel.RANDOM, rngs.get(0).getRngBackendModel());
    final List<WatchDogDef> watchDogs = parser.getWatchDogs();
    assertEquals(WatchDogModel.I6300ESB, watchDogs.get(0).getModel());
    assertEquals(WatchDogAction.RESET, watchDogs.get(0).getAction());
    assertEquals(WatchDogModel.IB700, watchDogs.get(1).getModel());
    assertEquals(WatchDogAction.POWEROFF, watchDogs.get(1).getAction());
}
Also used : LibvirtDomainXmlParser(com.cloud.agent.resource.kvm.xml.LibvirtDomainXmlParser) NicModel(com.cloud.model.enumeration.NicModel) GuestNetType(com.cloud.model.enumeration.GuestNetType) ImageFormat(com.cloud.model.enumeration.ImageFormat) DiskControllerType(com.cloud.model.enumeration.DiskControllerType) InterfaceDef(com.cloud.agent.resource.kvm.xml.LibvirtVmDef.InterfaceDef) RngDef(com.cloud.agent.resource.kvm.xml.LibvirtVmDef.RngDef) LibvirtDiskDef(com.cloud.agent.resource.kvm.xml.LibvirtDiskDef) WatchDogDef(com.cloud.agent.resource.kvm.xml.LibvirtVmDef.WatchDogDef)

Example 2 with LibvirtDomainXmlParser

use of com.cloud.agent.resource.kvm.xml.LibvirtDomainXmlParser in project cosmic by MissionCriticalCloud.

the class KvmStorageProcessor method attachOrDetachDisk.

private synchronized String attachOrDetachDisk(final Connect conn, final boolean attach, final String vmName, final KvmPhysicalDisk attachingDisk, final int devId, final DiskControllerType diskControllerType, final ImageFormat diskFormat, final String serial, final VolumeObjectTO volumeObjectTO) throws LibvirtException, InternalErrorException {
    final List<LibvirtDiskDef> disks;
    Domain dm = null;
    LibvirtDiskDef 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) {
            for (final LibvirtDiskDef 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 = new LibvirtDiskDef();
            if (diskControllerType == DiskControllerType.SCSI) {
                diskdef.setQemuDriver(true);
                diskdef.setDiscard(LibvirtDiskDef.DiscardType.UNMAP);
            }
            diskdef.setSerial(serial);
            diskdef.setDeviceId(devId);
            Optional.ofNullable(volumeObjectTO.getBytesReadRate()).filter(bps -> bps > 0).ifPresent(diskdef::setBytesReadRate);
            Optional.ofNullable(volumeObjectTO.getBytesWriteRate()).filter(bps -> bps > 0).ifPresent(diskdef::setBytesWriteRate);
            if (volumeObjectTO.getIopsTotalRate() == null) {
                Optional.ofNullable(volumeObjectTO.getIopsReadRate()).filter(iops -> iops > 0).ifPresent(diskdef::setIopsReadRate);
                Optional.ofNullable(volumeObjectTO.getIopsWriteRate()).filter(iops -> iops > 0).ifPresent(diskdef::setIopsWriteRate);
            } else {
                Optional.ofNullable(volumeObjectTO.getIopsTotalRate()).filter(iops -> iops > 0).ifPresent(diskdef::setIopsTotalRate);
            }
            if (attachingPool.getType() == StoragePoolType.RBD) {
                diskdef.defNetworkBasedDisk(attachingDisk.getPath(), attachingPool.getSourceHost(), attachingPool.getSourcePort(), attachingPool.getAuthUserName(), attachingPool.getUuid(), devId, diskControllerType, LibvirtDiskDef.DiskProtocol.RBD, ImageFormat.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, diskControllerType, LibvirtDiskDef.DiskProtocol.GLUSTER, ImageFormat.QCOW2);
            } else if (attachingPool.getType() == StoragePoolType.NetworkFilesystem) {
                diskdef.defFileBasedDisk(attachingDisk.getPath(), devId, diskControllerType, diskFormat);
            } else if (attachingPool.getType() == StoragePoolType.LVM) {
                diskdef.defBlockBasedDisk(attachingDisk.getPath(), devId, diskControllerType);
            }
        }
        final String xml = diskdef.toString();
        return attachOrDetachDevice(conn, attach, vmName, xml);
    } finally {
        if (dm != null) {
            dm.free();
        }
    }
}
Also used : TemplateFormatInfo(com.cloud.legacymodel.storage.TemplateFormatInfo) DettachCommand(com.cloud.legacymodel.communication.command.DettachCommand) CreateObjectCommand(com.cloud.legacymodel.communication.command.CreateObjectCommand) CreateObjectAnswer(com.cloud.legacymodel.communication.answer.CreateObjectAnswer) Date(java.util.Date) SnapshotAndCopyCommand(com.cloud.legacymodel.communication.command.SnapshotAndCopyCommand) PrimaryDataStoreTO(com.cloud.legacymodel.to.PrimaryDataStoreTO) LoggerFactory(org.slf4j.LoggerFactory) DomainSnapshot(org.libvirt.DomainSnapshot) DiskTO(com.cloud.legacymodel.to.DiskTO) LibvirtConnection(com.cloud.agent.resource.kvm.LibvirtConnection) LibvirtException(org.libvirt.LibvirtException) StorageLayer(com.cloud.utils.storage.StorageLayer) PrimaryStorageDownloadAnswer(com.cloud.legacymodel.communication.answer.PrimaryStorageDownloadAnswer) Map(java.util.Map) SnapshotObjectTO(com.cloud.legacymodel.to.SnapshotObjectTO) DataObjectType(com.cloud.model.enumeration.DataObjectType) DateFormat(java.text.DateFormat) QemuImg(com.cloud.agent.resource.kvm.storage.utils.QemuImg) QemuImgException(com.cloud.agent.resource.kvm.storage.utils.QemuImgException) DomainState(org.libvirt.DomainInfo.DomainState) Rbd(com.ceph.rbd.Rbd) DomainDeviceModifyFlags(org.libvirt.flags.DomainDeviceModifyFlags) QCOW2Processor(com.cloud.common.storageprocessor.QCOW2Processor) DomainInfo(org.libvirt.DomainInfo) LibvirtComputingResource(com.cloud.agent.resource.kvm.LibvirtComputingResource) UUID(java.util.UUID) IoCTX(com.ceph.rados.IoCTX) FileNotFoundException(java.io.FileNotFoundException) AttachCommand(com.cloud.legacymodel.communication.command.AttachCommand) Script(com.cloud.utils.script.Script) PhysicalDiskFormat(com.cloud.model.enumeration.PhysicalDiskFormat) DeleteCommand(com.cloud.legacymodel.communication.command.DeleteCommand) RbdImage(com.ceph.rbd.RbdImage) List(java.util.List) CopyCommand(com.cloud.legacymodel.communication.command.CopyCommand) Answer(com.cloud.legacymodel.communication.answer.Answer) Optional(java.util.Optional) Rados(com.ceph.rados.Rados) TemplateLocation(com.cloud.common.storageprocessor.TemplateLocation) ImageFormat(com.cloud.model.enumeration.ImageFormat) VolumeObjectTO(com.cloud.legacymodel.to.VolumeObjectTO) InternalErrorException(com.cloud.legacymodel.exceptions.InternalErrorException) CopyCmdAnswer(com.cloud.legacymodel.communication.answer.CopyCmdAnswer) TemplateObjectTO(com.cloud.legacymodel.to.TemplateObjectTO) SimpleDateFormat(java.text.SimpleDateFormat) HashMap(java.util.HashMap) ConfigurationException(javax.naming.ConfigurationException) ForgetObjectCommand(com.cloud.legacymodel.communication.command.ForgetObjectCommand) DataTO(com.cloud.legacymodel.to.DataTO) DiskControllerType(com.cloud.model.enumeration.DiskControllerType) MessageFormat(java.text.MessageFormat) Connect(org.libvirt.Connect) NfsTO(com.cloud.legacymodel.to.NfsTO) DettachAnswer(com.cloud.legacymodel.communication.answer.DettachAnswer) Logger(org.slf4j.Logger) LibvirtDiskDef(com.cloud.agent.resource.kvm.xml.LibvirtDiskDef) SnapshotAndCopyAnswer(com.cloud.legacymodel.communication.answer.SnapshotAndCopyAnswer) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) StorageProcessor(com.cloud.common.storageprocessor.resource.StorageProcessor) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) File(java.io.File) StoragePoolType(com.cloud.model.enumeration.StoragePoolType) LibvirtDomainXmlParser(com.cloud.agent.resource.kvm.xml.LibvirtDomainXmlParser) DataStoreTO(com.cloud.legacymodel.to.DataStoreTO) Domain(org.libvirt.Domain) JavaStorageLayer(com.cloud.utils.storage.JavaStorageLayer) QemuImgFile(com.cloud.agent.resource.kvm.storage.utils.QemuImgFile) Processor(com.cloud.common.storageprocessor.Processor) IntroduceObjectCommand(com.cloud.legacymodel.communication.command.IntroduceObjectCommand) AttachAnswer(com.cloud.legacymodel.communication.answer.AttachAnswer) LibvirtDiskDef(com.cloud.agent.resource.kvm.xml.LibvirtDiskDef) LibvirtDomainXmlParser(com.cloud.agent.resource.kvm.xml.LibvirtDomainXmlParser) InternalErrorException(com.cloud.legacymodel.exceptions.InternalErrorException) Domain(org.libvirt.Domain)

Example 3 with LibvirtDomainXmlParser

use of com.cloud.agent.resource.kvm.xml.LibvirtDomainXmlParser in project cosmic by MissionCriticalCloud.

the class LibvirtComputingResource method getVncPort.

public Integer getVncPort(final Connect conn, final String vmName) throws LibvirtException {
    final LibvirtDomainXmlParser parser = new LibvirtDomainXmlParser();
    Domain dm = null;
    try {
        dm = conn.domainLookupByName(vmName);
        final String xmlDesc = dm.getXMLDesc(0);
        parser.parseDomainXml(xmlDesc);
        return parser.getVncPort();
    } finally {
        try {
            if (dm != null) {
                dm.free();
            }
        } catch (final LibvirtException l) {
            logger.trace("Ignoring libvirt error.", l);
        }
    }
}
Also used : LibvirtException(org.libvirt.LibvirtException) LibvirtDomainXmlParser(com.cloud.agent.resource.kvm.xml.LibvirtDomainXmlParser) Domain(org.libvirt.Domain)

Example 4 with LibvirtDomainXmlParser

use of com.cloud.agent.resource.kvm.xml.LibvirtDomainXmlParser in project cosmic by MissionCriticalCloud.

the class LibvirtComputingResource method getDisks.

public List<LibvirtDiskDef> getDisks(final Connect conn, final String vmName) {
    final LibvirtDomainXmlParser parser = new LibvirtDomainXmlParser();
    Domain dm = null;
    try {
        dm = conn.domainLookupByName(vmName);
        parser.parseDomainXml(dm.getXMLDesc(0));
        return parser.getDisks();
    } catch (final LibvirtException e) {
        logger.debug("Failed to get dom xml: " + e.toString());
        return new ArrayList<>();
    } finally {
        try {
            if (dm != null) {
                dm.free();
            }
        } catch (final LibvirtException e) {
            logger.trace("Ignoring libvirt error.", e);
        }
    }
}
Also used : LibvirtException(org.libvirt.LibvirtException) LibvirtDomainXmlParser(com.cloud.agent.resource.kvm.xml.LibvirtDomainXmlParser) Domain(org.libvirt.Domain)

Example 5 with LibvirtDomainXmlParser

use of com.cloud.agent.resource.kvm.xml.LibvirtDomainXmlParser in project cosmic by MissionCriticalCloud.

the class LibvirtComputingResource method getInterfaces.

public List<InterfaceDef> getInterfaces(final Connect conn, final String vmName) {
    final LibvirtDomainXmlParser parser = new LibvirtDomainXmlParser();
    Domain dm = null;
    try {
        dm = conn.domainLookupByName(vmName);
        parser.parseDomainXml(dm.getXMLDesc(0));
        return parser.getInterfaces();
    } catch (final LibvirtException e) {
        logger.debug("Failed to get dom xml: " + e.toString());
        return new ArrayList<>();
    } finally {
        try {
            if (dm != null) {
                dm.free();
            }
        } catch (final LibvirtException e) {
            logger.trace("Ignoring libvirt error.", e);
        }
    }
}
Also used : LibvirtException(org.libvirt.LibvirtException) LibvirtDomainXmlParser(com.cloud.agent.resource.kvm.xml.LibvirtDomainXmlParser) Domain(org.libvirt.Domain)

Aggregations

LibvirtDomainXmlParser (com.cloud.agent.resource.kvm.xml.LibvirtDomainXmlParser)5 Domain (org.libvirt.Domain)4 LibvirtException (org.libvirt.LibvirtException)4 LibvirtDiskDef (com.cloud.agent.resource.kvm.xml.LibvirtDiskDef)2 DiskControllerType (com.cloud.model.enumeration.DiskControllerType)2 ImageFormat (com.cloud.model.enumeration.ImageFormat)2 IoCTX (com.ceph.rados.IoCTX)1 Rados (com.ceph.rados.Rados)1 Rbd (com.ceph.rbd.Rbd)1 RbdImage (com.ceph.rbd.RbdImage)1 LibvirtComputingResource (com.cloud.agent.resource.kvm.LibvirtComputingResource)1 LibvirtConnection (com.cloud.agent.resource.kvm.LibvirtConnection)1 QemuImg (com.cloud.agent.resource.kvm.storage.utils.QemuImg)1 QemuImgException (com.cloud.agent.resource.kvm.storage.utils.QemuImgException)1 QemuImgFile (com.cloud.agent.resource.kvm.storage.utils.QemuImgFile)1 InterfaceDef (com.cloud.agent.resource.kvm.xml.LibvirtVmDef.InterfaceDef)1 RngDef (com.cloud.agent.resource.kvm.xml.LibvirtVmDef.RngDef)1 WatchDogDef (com.cloud.agent.resource.kvm.xml.LibvirtVmDef.WatchDogDef)1 Processor (com.cloud.common.storageprocessor.Processor)1 QCOW2Processor (com.cloud.common.storageprocessor.QCOW2Processor)1