From cac9562dd5f4cb106fb068a26af6e22dd3c6a463 Mon Sep 17 00:00:00 2001 From: Manuel Bentele Date: Mon, 17 May 2021 14:17:53 +0200 Subject: [qemu] Keep track of defined VMs to automatically close them before program exit --- .../java/org/openslx/runvirt/plugin/qemu/App.java | 11 +--------- .../runvirt/virtualization/LibvirtHypervisor.java | 25 +++++++++++++++++++++- 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; @@ -29,6 +31,11 @@ public abstract class LibvirtHypervisor implements Closeable */ protected Connect hypervisor = null; + /** + * List of registered machines on the Libvirt hypervisor backend. + */ + private List 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(); } /** @@ -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 ) { -- cgit v1.2.3-55-g7522