diff options
author | Manuel Bentele | 2021-06-25 14:23:19 +0200 |
---|---|---|
committer | Manuel Bentele | 2021-06-25 14:23:19 +0200 |
commit | c820bd818c488fb2ab14d51afa4d241b762d2fc6 (patch) | |
tree | 5f0c364c10820219ca418e6436c425074afc35e0 /core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/virtualization | |
parent | [debug-report-bwlp] add blkid output (diff) | |
parent | [libvirt] Enforce libvirt UIDs/GIDs to not collide with LDAP UIDs/GIDs (diff) | |
download | mltk-c820bd818c488fb2ab14d51afa4d241b762d2fc6.tar.gz mltk-c820bd818c488fb2ab14d51afa4d241b762d2fc6.tar.xz mltk-c820bd818c488fb2ab14d51afa4d241b762d2fc6.zip |
Merge branch 'feature/qemu-integration'
Diffstat (limited to 'core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/virtualization')
4 files changed, 409 insertions, 0 deletions
diff --git a/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/virtualization/LibvirtHypervisor.java b/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/virtualization/LibvirtHypervisor.java new file mode 100644 index 00000000..757fc706 --- /dev/null +++ b/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/virtualization/LibvirtHypervisor.java @@ -0,0 +1,209 @@ +package org.openslx.runvirt.virtualization; + +import java.io.Closeable; +import java.util.ArrayList; +import java.util.List; + +import org.libvirt.Connect; +import org.libvirt.LibvirtException; +import org.openslx.libvirt.capabilities.Capabilities; +import org.openslx.libvirt.xml.LibvirtXmlDocumentException; +import org.openslx.libvirt.xml.LibvirtXmlSerializationException; +import org.openslx.libvirt.xml.LibvirtXmlValidationException; +import org.openslx.virtualization.Version; + +/** + * Representation of a Libvirt hypervisor backend (e.g. QEMU or VMware). + * <p> + * The representation allows to connect to a running Libvirt service and query the host system's + * capabilities or manage virtual machines. + * + * @implNote This class is the abstract representation to implement various Libvirt hypervisor + * backends using inheritance. + * + * @author Manuel Bentele + * @version 1.0 + */ +public abstract class LibvirtHypervisor implements Closeable +{ + /** + * Connection to a Libvirt hypervisor backend. + */ + protected Connect hypervisor = null; + + /** + * List of registered machines on the Libvirt hypervisor backend. + */ + private List<LibvirtVirtualMachine> machines; + + /** + * Creates a new Libvirt hypervisor backend specified by an URI and connects to the specified + * backend. + * + * @param connectionUri URI of a specific Libvirt hypervisor backend. + * @throws LibvirtHypervisorException failed to connect to the specified Libvirt hypervisor + * backend. + */ + public LibvirtHypervisor( String connectionUri ) throws LibvirtHypervisorException + { + this.connect( connectionUri ); + this.machines = new ArrayList<LibvirtVirtualMachine>(); + } + + /** + * Connects to the Libvirt hypervisor backend specified by an URI. + * + * @param connectionUri URI of a specific Libvirt hypervisor backend. + * @throws LibvirtHypervisorException failed to connect to the specified Libvirt hypervisor + * backend. + */ + protected void connect( String connectionUri ) throws LibvirtHypervisorException + { + try { + this.hypervisor = new Connect( connectionUri ); + } catch ( LibvirtException e ) { + throw new LibvirtHypervisorException( e.getLocalizedMessage() ); + } + } + + /** + * Returns the URI of the connection to the Libvirt hypervisor backend. + * + * @return URI of the connection to the hypervisor. + * @throws LibvirtHypervisorException failed to return the connection URI of the Libvirt + * hypervisor backend. + */ + public String getConnectionUri() throws LibvirtHypervisorException + { + String connectionUri = null; + + try { + connectionUri = this.hypervisor.getURI(); + } catch ( LibvirtException e ) { + throw new LibvirtHypervisorException( e.getLocalizedMessage() ); + } + + return connectionUri; + } + + /** + * Returns the queried Libvirt hypervisor's host system capabilities. + * + * @return queried Libvirt hypervisor's host system capabilities. + * @throws LibvirtHypervisorException failed to query and return the Libvirt hypervisor's host + * system capabilities. + */ + public Capabilities getCapabilites() throws LibvirtHypervisorException + { + Capabilities hypervisorCapabilities = null; + + try { + final String hypervisorCapabilitiesString = this.hypervisor.getCapabilities(); + hypervisorCapabilities = new Capabilities( hypervisorCapabilitiesString ); + } catch ( LibvirtException | LibvirtXmlDocumentException | LibvirtXmlSerializationException + | LibvirtXmlValidationException e ) { + throw new LibvirtHypervisorException( e.getLocalizedMessage() ); + } + + return hypervisorCapabilities; + } + + /** + * Returns the version of the Libvirt hypervisor backend. + * + * @return version of the Libvirt hypervisor backend. + * @throws LibvirtHypervisorException failed to get the version of the Libvirt hypervisor + * backend. + */ + public Version getVersion() throws LibvirtHypervisorException + { + long hypervisorVersionRaw = 0; + Version hypervisorVersion = null; + + try { + hypervisorVersionRaw = this.hypervisor.getVersion(); + } catch ( LibvirtException e ) { + throw new LibvirtHypervisorException( e.getLocalizedMessage() ); + } + + if ( hypervisorVersionRaw > 0 ) { + final short major = Long.valueOf( hypervisorVersionRaw / Long.valueOf( 1000000 ) ).shortValue(); + hypervisorVersionRaw %= Long.valueOf( 1000000 ); + final short minor = Long.valueOf( hypervisorVersionRaw / Long.valueOf( 1000 ) ).shortValue(); + hypervisorVersion = new Version( major, minor ); + } + + return hypervisorVersion; + } + + /** + * Register a virtual machine by the Libvirt hypervisor based on a virtualization configuration. + * + * @param vmConfiguration virtualization configuration for the virtual machine. + * @return instance of the registered and defined virtual machine. + * @throws LibvirtHypervisorException failed to register and define virtual machine. + */ + public LibvirtVirtualMachine registerVm( org.openslx.libvirt.domain.Domain vmConfiguration ) + throws LibvirtHypervisorException + { + final String xmlVmConfiguration = vmConfiguration.toString(); + org.libvirt.Domain internalConfiguration = null; + + try { + internalConfiguration = this.hypervisor.domainDefineXML( xmlVmConfiguration ); + } catch ( LibvirtException e ) { + throw new LibvirtHypervisorException( e.getLocalizedMessage() ); + } + + final LibvirtVirtualMachine vm = new LibvirtVirtualMachine( internalConfiguration, vmConfiguration ); + this.machines.add( vm ); + + return vm; + } + + /** + * Deregisters an already registered virtual machine by the Libvirt hypervisor. + * + * @param vm virtual machine that should be deregistered + * @throws LibvirtHypervisorException failed to deregister virtual machine by the Libvirt + * hypervisor. + * @throws LibvirtVirtualMachineException failed to check and stop the virtual machine. + */ + public void deregisterVm( LibvirtVirtualMachine vm ) + throws LibvirtHypervisorException, LibvirtVirtualMachineException + { + // stop virtual machine if machine is running + if ( vm.isRunning() ) { + vm.stop(); + } + + // deregister and remove virtual machine from hypervisor + try { + vm.getLibvirtDomain().undefine(); + } catch ( LibvirtException e ) { + throw new LibvirtHypervisorException( e.getLocalizedMessage() ); + } + + this.machines.remove( vm ); + } + + @Override + public void close() + { + // deregister all VMs defined on the hypervisor + for ( LibvirtVirtualMachine vm : this.machines ) { + try { + this.deregisterVm( vm ); + } catch ( LibvirtHypervisorException | LibvirtVirtualMachineException e ) { + e.printStackTrace(); + } + } + + // close connection to the hypervisor + try { + this.hypervisor.close(); + } catch ( LibvirtException e ) { + e.printStackTrace(); + } + } +} diff --git a/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/virtualization/LibvirtHypervisorException.java b/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/virtualization/LibvirtHypervisorException.java new file mode 100644 index 00000000..64ae6b20 --- /dev/null +++ b/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/virtualization/LibvirtHypervisorException.java @@ -0,0 +1,25 @@ +package org.openslx.runvirt.virtualization; + +/** + * An exception of a Libvirt hypervisor error during acquiring the hypervisor's functionality. + * + * @author Manuel Bentele + * @version 1.0 + */ +public class LibvirtHypervisorException extends Exception +{ + /** + * Version for serialization. + */ + private static final long serialVersionUID = -3631452625806770209L; + + /** + * Creates a Libvirt hypervisor exception including an error message. + * + * @param errorMsg message to describe a specific Libvirt hypervisor error. + */ + LibvirtHypervisorException( String errorMsg ) + { + super( errorMsg ); + } +} diff --git a/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/virtualization/LibvirtVirtualMachine.java b/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/virtualization/LibvirtVirtualMachine.java new file mode 100644 index 00000000..3bcec2f8 --- /dev/null +++ b/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/virtualization/LibvirtVirtualMachine.java @@ -0,0 +1,150 @@ +package org.openslx.runvirt.virtualization; + +import org.libvirt.Domain; +import org.libvirt.LibvirtException; + +/** + * Representation of a Libvirt virtual machine. + * + * @author Manuel Bentele + * @version 1.0 + */ +public class LibvirtVirtualMachine +{ + /** + * Internal Libvirt virtualization configuration of the virtual machine. + */ + private Domain domain; + + /** + * Libvirt virtualization configuration of the virtual machine. + */ + private org.openslx.libvirt.domain.Domain configuration; + + /** + * Creates a new Libvirt virtual machine specified by a virtualization configuration. + * + * @param internalConfiguration internal Libvirt virtualization configuration to specify the + * Libvirt virtual machine. + * @param configuration Libvirt virtualization configuration to specify the Libvirt virtual + * machine. + */ + LibvirtVirtualMachine( Domain internalConfiguration, org.openslx.libvirt.domain.Domain configuration ) + { + this.domain = internalConfiguration; + this.configuration = configuration; + } + + /** + * Returns the internal Libvirt virtualization configuration of the Libvirt virtual machine. + * + * @return internal Libvirt virtualization configuration of the Libvirt virtual machine. + */ + Domain getLibvirtDomain() + { + return this.domain; + } + + /** + * Returns the Libvirt virtualization configuration of the Libvirt virtual machine. + * + * @return Libvirt virtualization configuration of the Libvirt virtual machine. + */ + public org.openslx.libvirt.domain.Domain getConfiguration() + { + return this.configuration; + } + + /** + * Checks if the Libvirt virtual machine is running. + * + * @return state of the Libvirt virtual machine whether it is running or not. + * @throws LibvirtVirtualMachineException failed to check if Libvirt machine is running or not. + */ + public boolean isRunning() throws LibvirtVirtualMachineException + { + int state = 0; + + try { + state = this.domain.isActive(); + } catch ( LibvirtException e ) { + throw new LibvirtVirtualMachineException( e.getLocalizedMessage() ); + } + + return ( state == 0 ) ? false : true; + } + + /** + * Starts the Libvirt virtual machine. + * + * @throws LibvirtVirtualMachineException failed to start the Libvirt virtual machine. + */ + public void start() throws LibvirtVirtualMachineException + { + if ( !this.isRunning() ) { + try { + this.domain.create(); + } catch ( LibvirtException e ) { + throw new LibvirtVirtualMachineException( e.getLocalizedMessage() ); + } + } + } + + /** + * Stops the Libvirt virtual machine. + * + * @throws LibvirtVirtualMachineException failed to stop the Libvirt virtual machine. + */ + public void stop() throws LibvirtVirtualMachineException + { + if ( this.isRunning() ) { + try { + this.domain.shutdown(); + } catch ( LibvirtException e ) { + throw new LibvirtVirtualMachineException( e.getLocalizedMessage() ); + } + } + } + + /** + * Suspends the Libvirt virtual machine. + * + * @throws LibvirtVirtualMachineException failed to suspend the Libvirt virtual machine. + */ + public void suspend() throws LibvirtVirtualMachineException + { + try { + this.domain.suspend(); + } catch ( LibvirtException e ) { + throw new LibvirtVirtualMachineException( e.getLocalizedMessage() ); + } + } + + /** + * Resumes the Libvirt virtual machine. + * + * @throws LibvirtVirtualMachineException faild to resume the Libvirt virtual machine. + */ + public void resume() throws LibvirtVirtualMachineException + { + try { + this.domain.resume(); + } catch ( LibvirtException e ) { + throw new LibvirtVirtualMachineException( e.getLocalizedMessage() ); + } + } + + /** + * Reboot the Libvirt virtual machine. + * + * @throws LibvirtVirtualMachineException failed to reboot the Libvirt virtual machine. + */ + public void reboot() throws LibvirtVirtualMachineException + { + try { + this.domain.reboot( 0 ); + } catch ( LibvirtException e ) { + throw new LibvirtVirtualMachineException( e.getLocalizedMessage() ); + } + } +} diff --git a/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/virtualization/LibvirtVirtualMachineException.java b/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/virtualization/LibvirtVirtualMachineException.java new file mode 100644 index 00000000..dd490be3 --- /dev/null +++ b/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/virtualization/LibvirtVirtualMachineException.java @@ -0,0 +1,25 @@ +package org.openslx.runvirt.virtualization; + +/** + * An exception of a Libvirt virtual machine error during controlling the virtual machine. + * + * @author Manuel Bentele + * @version 1.0 + */ +public class LibvirtVirtualMachineException extends Exception +{ + /** + * Version for serialization. + */ + private static final long serialVersionUID = -5371327391243047616L; + + /** + * Creates a Libvirt virtual machine exception including an error message. + * + * @param errorMsg message to describe a specific Libvirt virtual machine error. + */ + public LibvirtVirtualMachineException( String errorMsg ) + { + super( errorMsg ); + } +} |