summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorManuel Bentele2021-05-17 14:17:53 +0200
committerManuel Bentele2021-05-17 14:17:53 +0200
commitcac9562dd5f4cb106fb068a26af6e22dd3c6a463 (patch)
treef1b545aa2e6dcfc61ae718623e28113066aa15c2
parent[qemu] Add viewer representation to render displays of the virtual machine (diff)
downloadmltk-cac9562dd5f4cb106fb068a26af6e22dd3c6a463.tar.gz
mltk-cac9562dd5f4cb106fb068a26af6e22dd3c6a463.tar.xz
mltk-cac9562dd5f4cb106fb068a26af6e22dd3c6a463.zip
[qemu] Keep track of defined VMs to automatically close them before program exit
-rw-r--r--core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/plugin/qemu/App.java11
-rw-r--r--core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/virtualization/LibvirtHypervisor.java25
2 files changed, 25 insertions, 11 deletions
diff --git a/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/plugin/qemu/App.java b/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/plugin/qemu/App.java
index ecc97b12..e5bc4780 100644
--- a/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/plugin/qemu/App.java
+++ b/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/plugin/qemu/App.java
@@ -178,11 +178,6 @@ public class App
vm.start();
} catch ( LibvirtVirtualMachineException e ) {
LOGGER.error( "Failed to start defined VM: " + e.getLocalizedMessage() );
- try {
- hypervisor.deregisterVm( vm );
- } catch ( LibvirtHypervisorException | LibvirtVirtualMachineException e1 ) {
- LOGGER.error( "Failed to undefine VM in error state after failed start of VM: " + e.getLocalizedMessage() );
- }
hypervisor.close();
System.exit( 7 );
}
@@ -193,11 +188,6 @@ public class App
vmViewer.display();
} catch ( ViewerException e ) {
LOGGER.error( "Failed to display VM: " + e.getLocalizedMessage() );
- try {
- hypervisor.deregisterVm( vm );
- } catch ( LibvirtHypervisorException | LibvirtVirtualMachineException e1 ) {
- LOGGER.error( "Failed to undefine VM in error state after failed display: " + e.getLocalizedMessage() );
- }
hypervisor.close();
System.exit( 8 );
}
@@ -208,6 +198,7 @@ public class App
} catch ( LibvirtHypervisorException | LibvirtVirtualMachineException e ) {
LOGGER.error( "Failed to undefine VM: " + e.getLocalizedMessage() );
hypervisor.close();
+ System.exit( 9 );
}
// close connection to hypervisor
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
index 42e2d00f..757fc706 100644
--- 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
@@ -1,6 +1,8 @@
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;
@@ -30,6 +32,11 @@ public abstract class LibvirtHypervisor implements Closeable
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.
*
@@ -40,6 +47,7 @@ public abstract class LibvirtHypervisor implements Closeable
public LibvirtHypervisor( String connectionUri ) throws LibvirtHypervisorException
{
this.connect( connectionUri );
+ this.machines = new ArrayList<LibvirtVirtualMachine>();
}
/**
@@ -147,7 +155,10 @@ public abstract class LibvirtHypervisor implements Closeable
throw new LibvirtHypervisorException( e.getLocalizedMessage() );
}
- return new LibvirtVirtualMachine( internalConfiguration, vmConfiguration );
+ final LibvirtVirtualMachine vm = new LibvirtVirtualMachine( internalConfiguration, vmConfiguration );
+ this.machines.add( vm );
+
+ return vm;
}
/**
@@ -172,11 +183,23 @@ public abstract class LibvirtHypervisor implements Closeable
} 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 ) {