From 8416431dd4aaa1451a998c4d44bc78b7b4140be5 Mon Sep 17 00:00:00 2001 From: Manuel Bentele Date: Tue, 9 Nov 2021 14:48:25 +0100 Subject: Remove specified Libvirt OS Nvram file during QEMU VM upload --- .../java/org/openslx/libvirt/domain/Domain.java | 32 ++++ .../VirtualizationConfigurationQemu.java | 3 + .../org/openslx/libvirt/domain/DomainTest.java | 34 +++++ .../xml/qemu-kvm_default-ubuntu-20-04-vm_uefi.xml | 166 +++++++++++++++++++++ 4 files changed, 235 insertions(+) create mode 100644 src/test/resources/libvirt/xml/qemu-kvm_default-ubuntu-20-04-vm_uefi.xml diff --git a/src/main/java/org/openslx/libvirt/domain/Domain.java b/src/main/java/org/openslx/libvirt/domain/Domain.java index cbbf827..e6049fa 100644 --- a/src/main/java/org/openslx/libvirt/domain/Domain.java +++ b/src/main/java/org/openslx/libvirt/domain/Domain.java @@ -524,6 +524,26 @@ public class Domain extends LibvirtXmlDocument 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. * @@ -1476,6 +1496,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. */ diff --git a/src/main/java/org/openslx/virtualization/configuration/VirtualizationConfigurationQemu.java b/src/main/java/org/openslx/virtualization/configuration/VirtualizationConfigurationQemu.java index 60122fd..8c3a95b 100644 --- a/src/main/java/org/openslx/virtualization/configuration/VirtualizationConfigurationQemu.java +++ b/src/main/java/org/openslx/virtualization/configuration/VirtualizationConfigurationQemu.java @@ -794,6 +794,9 @@ public class VirtualizationConfigurationQemu extends VirtualizationConfiguration { // removes all referenced storage files of all specified CDROMs, Floppy drives and HDDs this.vmConfig.removeDiskDevicesStorage(); + + // remove specified NVRAM file of OS loader (firmware) + this.vmConfig.removeOsNvram(); } @Override diff --git a/src/test/java/org/openslx/libvirt/domain/DomainTest.java b/src/test/java/org/openslx/libvirt/domain/DomainTest.java index c56759d..6b53125 100644 --- a/src/test/java/org/openslx/libvirt/domain/DomainTest.java +++ b/src/test/java/org/openslx/libvirt/domain/DomainTest.java @@ -237,6 +237,40 @@ public class DomainTest assertEquals( "pc", vm.getOsMachine() ); } + @Test + @DisplayName( "Get VM's OS loader from libvirt XML file" ) + public void testGetOsLoader() + { + Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm_uefi.xml" ); + assertEquals( "/usr/share/edk2-ovmf/x64/OVMF_CODE.fd", vm.getOsLoader() ); + } + + @Test + @DisplayName( "Set VM's OS loader in libvirt XML file" ) + public void testSetOsLoader() + { + Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm_uefi.xml" ); + vm.setOsLoader( "/usr/share/qemu/edk2-x86_64-code.fd" ); + assertEquals( "/usr/share/qemu/edk2-x86_64-code.fd", vm.getOsLoader() ); + } + + @Test + @DisplayName( "Get VM's OS Nvram from libvirt XML file" ) + public void testGetOsNvram() + { + Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm_uefi.xml" ); + assertEquals( "/var/lib/libvirt/nvram/guest_VARS.fd", vm.getOsNvram() ); + } + + @Test + @DisplayName( "Set VM's OS Nvram in libvirt XML file" ) + public void testSetOsNvram() + { + Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm_uefi.xml" ); + vm.setOsNvram( "/tmp/nvram-tmp/tmp_VARS.fd" ); + assertEquals( "/tmp/nvram-tmp/tmp_VARS.fd", vm.getOsNvram() ); + } + @Test @DisplayName( "Get VM CPU model from libvirt XML file" ) public void testGetCpuModel() diff --git a/src/test/resources/libvirt/xml/qemu-kvm_default-ubuntu-20-04-vm_uefi.xml b/src/test/resources/libvirt/xml/qemu-kvm_default-ubuntu-20-04-vm_uefi.xml new file mode 100644 index 0000000..c4e2788 --- /dev/null +++ b/src/test/resources/libvirt/xml/qemu-kvm_default-ubuntu-20-04-vm_uefi.xml @@ -0,0 +1,166 @@ + + ubuntu-20-04 + 8dc5433c-0228-49e4-b019-fa2b606aa544 + Ubuntu 20.04 + Ubuntu 20.04 desktop installation + + + + + + 4194304 + 4194304 + 2 + + hvm + /usr/share/edk2-ovmf/x64/OVMF_CODE.fd + /var/lib/libvirt/nvram/guest_VARS.fd + + + + + + + + + + + + + + destroy + restart + destroy + + + + + + /usr/bin/qemu-system-x86_64 + + + + +
+ + + + + +
+ + + + +
+ + +
+ + + +
+ + + +
+ + + +
+ + +
+ + + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + +
+ + +
+ + + + + + +
+ + + + + + + + + + + +
+ + + +
+ + +
+ + + + + + + + +
+ +