diff options
Diffstat (limited to 'src/main/java/org/openslx/libvirt/domain')
5 files changed, 230 insertions, 7 deletions
diff --git a/src/main/java/org/openslx/libvirt/domain/Domain.java b/src/main/java/org/openslx/libvirt/domain/Domain.java index 7d49f14..e6049fa 100644 --- a/src/main/java/org/openslx/libvirt/domain/Domain.java +++ b/src/main/java/org/openslx/libvirt/domain/Domain.java @@ -8,6 +8,8 @@ import java.util.function.Function; import java.util.function.Predicate; import java.util.stream.Collectors; +import javax.xml.XMLConstants; + import org.openslx.libvirt.domain.device.Device; import org.openslx.libvirt.domain.device.Controller; import org.openslx.libvirt.domain.device.ControllerFloppy; @@ -42,6 +44,8 @@ import org.openslx.libvirt.xml.LibvirtXmlNode; import org.openslx.libvirt.xml.LibvirtXmlResources; import org.openslx.libvirt.xml.LibvirtXmlSerializationException; import org.openslx.libvirt.xml.LibvirtXmlValidationException; +import org.w3c.dom.Document; +import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.InputSource; @@ -57,6 +61,16 @@ import org.xml.sax.InputSource; public class Domain extends LibvirtXmlDocument { /** + * XML namespace URI for QEMU command line elements in the Libvirt domain XML document. + */ + private static final String XMLNS_QEMU_NS_URI = "http://libvirt.org/schemas/domain/qemu/1.0"; + + /** + * XML namespace prefix for QEMU command line elements in the Libvirt domain XML document. + */ + private static final String XMLNS_QEMU_NS_PREFIX = "qemu"; + + /** * Creates Libvirt domain XML document from {@link String} providing Libvirt domain XML content. * * @param xml {@link String} with Libvirt domain XML content. @@ -491,6 +505,46 @@ public class Domain extends LibvirtXmlDocument } /** + * Returns OS loader defined in the Libvirt domain XML document. + * + * @return OS loader of the virtual machine. + */ + public String getOsLoader() + { + return this.getRootXmlNode().getXmlElementValue( "os/loader" ); + } + + /** + * Set OS loader in the Libvirt domain XML document. + * + * @param loader OS loader for the virtual machine. + */ + public void setOsLoader( String loader ) + { + this.getRootXmlNode().setXmlElementValue( "os/loader", loader ); + } + + /** + * Returns OS Nvram defined in the Libvirt domain XML document. + * + * @return OS Nvram of the virtual machine. + */ + public String getOsNvram() + { + return this.getRootXmlNode().getXmlElementValue( "os/nvram" ); + } + + /** + * Set OS Nvram in the Libvirt domain XML document. + * + * @param nvram OS Nvram for the virtual machine. + */ + public void setOsNvram( String nvram ) + { + this.getRootXmlNode().setXmlElementValue( "os/nvram", nvram ); + } + + /** * Operating system types specifiable for a virtual machine in the Libvirt domain XML document. * * @author Manuel Bentele @@ -1052,6 +1106,35 @@ public class Domain extends LibvirtXmlDocument } /** + * Returns the values of QEMU command line arguments from the Libvirt domain XML document. + * + * @return values of QEMU command line arguments from the Libvirt domain XML document. + */ + public ArrayList<String> getQemuCmdlnArguments() + { + final Document xmlDocument = this.getRootXmlNode().getXmlDocument(); + final ArrayList<String> qemuCmdlnArgs = new ArrayList<String>(); + + final NodeList qemuCmdlnNodes = xmlDocument.getElementsByTagNameNS( XMLNS_QEMU_NS_URI, "commandline" ); + if ( qemuCmdlnNodes.getLength() > 0 ) { + final Node qemuCmdlnNode = qemuCmdlnNodes.item( 0 ); + final NodeList qemuCmdlnArgNodes = qemuCmdlnNode.getChildNodes(); + for ( int i = 0; i < qemuCmdlnArgNodes.getLength(); i++ ) { + final Node qemuCmdlnArgNode = qemuCmdlnArgNodes.item( i ); + if ( qemuCmdlnArgNode.getNodeType() == Node.ELEMENT_NODE ) { + final Element qemuCmdlnArgElement = Element.class.cast( qemuCmdlnArgNode ); + final String value = qemuCmdlnArgElement.getAttribute( "value" ); + if ( value != null && !value.isEmpty() ) { + qemuCmdlnArgs.add( value ); + } + } + } + } + + return qemuCmdlnArgs; + } + + /** * Adds a virtual machine device to the Libvirt domain XML document. * * @param device virtual machine device that is added to the Libvirt domain XML document. @@ -1345,6 +1428,39 @@ public class Domain extends LibvirtXmlDocument } /** + * Adds an given value as QEMU command line argument to the Libvirt domain XML document. + * + * @param value QEMU command line argument value. + */ + public void addQemuCmdlnArgument( final String value ) + { + final Element rootElement = Element.class.cast( this.getRootXmlNode().getXmlBaseNode() ); + final Document xmlDocument = this.getRootXmlNode().getXmlDocument(); + final Element qemuCmdlnElement; + + final NodeList qemuCmdlnNodes = rootElement.getElementsByTagNameNS( XMLNS_QEMU_NS_URI, "commandline" ); + if ( qemuCmdlnNodes.getLength() < 1 ) { + // add missing <domain xmlns:qemu="..."> namespace attribute + rootElement.setAttributeNS( XMLConstants.XMLNS_ATTRIBUTE_NS_URI, + XMLConstants.XMLNS_ATTRIBUTE + ":" + XMLNS_QEMU_NS_PREFIX, XMLNS_QEMU_NS_URI ); + // add missing <qemu:commandline> element + qemuCmdlnElement = xmlDocument.createElementNS( XMLNS_QEMU_NS_URI, "commandline" ); + qemuCmdlnElement.setPrefix( XMLNS_QEMU_NS_PREFIX ); + rootElement.appendChild( qemuCmdlnElement ); + } else { + // use available <qemu:commandline> element + final Node qemuCmdlnNode = qemuCmdlnNodes.item( 0 ); + assert ( qemuCmdlnNode.getNodeType() == Node.ELEMENT_NODE ); + qemuCmdlnElement = Element.class.cast( qemuCmdlnNode ); + } + + // append <qemu:arg value='...'> element with attribute + final Element qemuCmdlnArgElement = xmlDocument.createElementNS( XMLNS_QEMU_NS_URI, "arg" ); + qemuCmdlnArgElement.setAttribute( "value", value ); + qemuCmdlnElement.appendChild( qemuCmdlnArgElement ); + } + + /** * Removes boot oder entries in the Libvirt domain XML document. */ public void removeBootOrder() @@ -1381,6 +1497,18 @@ public class Domain extends LibvirtXmlDocument } /** + * Removes specified Nvram file in the Libvirt domain XML document. + */ + public void removeOsNvram() + { + final Node nvramElement = this.getRootXmlNode().getXmlElement( "os/nvram" ); + + if ( nvramElement != null ) { + nvramElement.getParentNode().removeChild( nvramElement ); + } + } + + /** * Removes network source for all interface devices in the Libvirt domain XML document. */ public void removeInterfaceDevicesSource() diff --git a/src/main/java/org/openslx/libvirt/domain/device/Graphics.java b/src/main/java/org/openslx/libvirt/domain/device/Graphics.java index d10eb3f..3fd2f81 100644 --- a/src/main/java/org/openslx/libvirt/domain/device/Graphics.java +++ b/src/main/java/org/openslx/libvirt/domain/device/Graphics.java @@ -1,5 +1,8 @@ package org.openslx.libvirt.domain.device; +import java.net.InetAddress; +import java.net.UnknownHostException; + import org.openslx.libvirt.xml.LibvirtXmlNode; /** @@ -50,6 +53,66 @@ public class Graphics extends Device } /** + * Returns the listen address of the graphics device. + * + * @return listen address of the graphics device. + */ + public InetAddress getListenAddress() + { + InetAddress parsedListenAddress = null; + + if ( this.getListenType() == ListenType.ADDRESS ) { + // only read listen address, if address listen type is set + final String rawListenAddress = this.getXmlElementAttributeValue( "listen", "address" ); + + try { + parsedListenAddress = InetAddress.getByName( rawListenAddress ); + } catch ( UnknownHostException e ) { + parsedListenAddress = null; + } + } + + return parsedListenAddress; + } + + /** + * Sets the listen address for the graphics device. + * + * @param listenAddress listen address for the graphics device. + */ + public void setListenAddress( InetAddress listenAddress ) + { + if ( this.getListenType() == ListenType.ADDRESS && listenAddress != null ) { + // only set listen address, if address listen type is set + this.setXmlElementAttributeValue( "listen", "address", listenAddress.getHostAddress() ); + } + } + + /** + * Returns the listen port of the graphics device. + * + * @return listen port of the graphics device. + */ + public int getListenPort() + { + final String listenPort = this.getXmlElementAttributeValue( "port" ); + return Integer.valueOf( listenPort ); + } + + /** + * Sets the listen port for the graphics device. + * + * @param listenPort listen port for the graphics device. + */ + public void setListenPort( int listenPort ) + { + if ( this.getListenType() == ListenType.ADDRESS ) { + // only set listen port, if address listen type is set + this.setXmlElementAttributeValue( "port", Integer.toString( listenPort ) ); + } + } + + /** * Creates a non-existent graphics device as Libvirt XML device element. * * @param graphics graphics device that is created. diff --git a/src/main/java/org/openslx/libvirt/domain/device/GraphicsSpice.java b/src/main/java/org/openslx/libvirt/domain/device/GraphicsSpice.java index fbd115b..2c6068a 100644 --- a/src/main/java/org/openslx/libvirt/domain/device/GraphicsSpice.java +++ b/src/main/java/org/openslx/libvirt/domain/device/GraphicsSpice.java @@ -1,5 +1,7 @@ package org.openslx.libvirt.domain.device; +import java.net.InetAddress; + import org.openslx.libvirt.xml.LibvirtXmlNode; /** @@ -11,6 +13,16 @@ import org.openslx.libvirt.xml.LibvirtXmlNode; public class GraphicsSpice extends Graphics { /** + * Default address of a SPICE graphics listener. + */ + public static final InetAddress DEFAULT_ADDRESS = InetAddress.getLoopbackAddress(); + + /** + * Default port of a SPICE graphics listener. + */ + public static final int DEFAULT_PORT = 5900; + + /** * Creates an empty graphics SPICE device. */ public GraphicsSpice() diff --git a/src/main/java/org/openslx/libvirt/domain/device/Hostdev.java b/src/main/java/org/openslx/libvirt/domain/device/Hostdev.java index 11e74c3..dc9cf5e 100644 --- a/src/main/java/org/openslx/libvirt/domain/device/Hostdev.java +++ b/src/main/java/org/openslx/libvirt/domain/device/Hostdev.java @@ -78,7 +78,7 @@ public class Hostdev extends Device if ( hostdev instanceof HostdevMdev ) { xmlNode.setXmlElementAttributeValue( "type", Type.MDEV.toString() ); - addedHostdev = HostdevPci.createInstance( xmlNode ); + addedHostdev = HostdevMdev.createInstance( xmlNode ); } else if ( hostdev instanceof HostdevPci ) { xmlNode.setXmlElementAttributeValue( "type", Type.PCI.toString() ); addedHostdev = HostdevPci.createInstance( xmlNode ); diff --git a/src/main/java/org/openslx/libvirt/domain/device/HostdevMdev.java b/src/main/java/org/openslx/libvirt/domain/device/HostdevMdev.java index d25a6eb..082ea5b 100644 --- a/src/main/java/org/openslx/libvirt/domain/device/HostdevMdev.java +++ b/src/main/java/org/openslx/libvirt/domain/device/HostdevMdev.java @@ -50,6 +50,26 @@ public class HostdevMdev extends Hostdev implements HostdevAddressableSource<Hos } /** + * Checks whether the hostdev mediated device memory framebuffer is on or off. + * + * @return state whether the hostdev mediated device memory framebuffer is on or off. + */ + public boolean isMemoryFramebufferOn() + { + return this.getXmlElementAttributeValueAsBool( "ramfb" ); + } + + /** + * Sets the state of the hostdev mediated device memory framebuffer. + * + * @param on state whether the hostdev mediated device memory framebuffer is on or off. + */ + public void setMemoryFramebufferOn( boolean on ) + { + this.setXmlElementAttributeValueOnOff( "ramfb", on ); + } + + /** * Returns the hostdev mediated device model. * * @return hostdev mediated device model. @@ -80,7 +100,7 @@ public class HostdevMdev extends Hostdev implements HostdevAddressableSource<Hos @Override public void setSource( HostdevMdevDeviceAddress source ) { - this.setXmlElementAttributeValue( "source/address", "domain", source.getDeviceAddressAsString() ); + this.setXmlElementAttributeValue( "source/address", "uuid", source.getDeviceAddressAsString() ); } /** @@ -89,9 +109,9 @@ public class HostdevMdev extends Hostdev implements HostdevAddressableSource<Hos * @param xmlNode Libvirt XML node of the Libvirt XML device that is created. * @return created hostdev mediated device instance. */ - public static HostdevPci createInstance( LibvirtXmlNode xmlNode ) + public static HostdevMdev createInstance( LibvirtXmlNode xmlNode ) { - return HostdevPci.newInstance( xmlNode ); + return HostdevMdev.newInstance( xmlNode ); } /** @@ -101,9 +121,9 @@ public class HostdevMdev extends Hostdev implements HostdevAddressableSource<Hos * @param xmlNode existing Libvirt XML hostdev mediated device element. * @return hostdev mediated device instance. */ - public static HostdevPci newInstance( LibvirtXmlNode xmlNode ) + public static HostdevMdev newInstance( LibvirtXmlNode xmlNode ) { - return new HostdevPci( xmlNode ); + return new HostdevMdev( xmlNode ); } /** @@ -112,7 +132,7 @@ public class HostdevMdev extends Hostdev implements HostdevAddressableSource<Hos * @author Manuel Bentele * @version 1.0 */ - enum Model + public enum Model { // @formatter:off VFIO_PCI( "vfio-pci" ), |