From bf15b73d90a18e52f8764058d6fe80037d8a2307 Mon Sep 17 00:00:00 2001 From: Manuel Bentele Date: Fri, 19 Mar 2021 13:42:29 +0100 Subject: Add implementation of Libvirt XML capabilities documents --- .../libvirt/capabilities/CapabilitiesTest.java | 305 +++++++++++++++++++++ .../org/openslx/libvirt/domain/DomainTest.java | 52 ++++ 2 files changed, 357 insertions(+) create mode 100644 src/test/java/org/openslx/libvirt/capabilities/CapabilitiesTest.java (limited to 'src/test/java/org/openslx') diff --git a/src/test/java/org/openslx/libvirt/capabilities/CapabilitiesTest.java b/src/test/java/org/openslx/libvirt/capabilities/CapabilitiesTest.java new file mode 100644 index 0000000..e7d7651 --- /dev/null +++ b/src/test/java/org/openslx/libvirt/capabilities/CapabilitiesTest.java @@ -0,0 +1,305 @@ +package org.openslx.libvirt.capabilities; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.fail; + +import java.math.BigInteger; +import java.util.List; + +import org.apache.log4j.Level; +import org.apache.log4j.LogManager; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.openslx.libvirt.capabilities.cpu.Cpu; +import org.openslx.libvirt.capabilities.cpu.Feature; +import org.openslx.libvirt.capabilities.cpu.Pages; +import org.openslx.libvirt.capabilities.guest.Domain; +import org.openslx.libvirt.capabilities.guest.Guest; +import org.openslx.libvirt.capabilities.guest.Machine; +import org.openslx.libvirt.domain.Domain.OsType; +import org.openslx.libvirt.domain.Domain.Type; +import org.openslx.libvirt.xml.LibvirtXmlDocumentException; +import org.openslx.libvirt.xml.LibvirtXmlSerializationException; +import org.openslx.libvirt.xml.LibvirtXmlTestResources; +import org.openslx.libvirt.xml.LibvirtXmlValidationException; + +public class CapabilitiesTest +{ + @BeforeAll + public static void setUp() + { + // disable logging with log4j + LogManager.getRootLogger().setLevel( Level.OFF ); + } + + private Capabilities newCapabilitiesInstance( String xmlFileName ) + { + Capabilities caps = null; + + try { + caps = new Capabilities( LibvirtXmlTestResources.getLibvirtXmlFile( xmlFileName ) ); + } catch ( LibvirtXmlDocumentException | LibvirtXmlSerializationException | LibvirtXmlValidationException e ) { + final String errorMsg = new String( + "Cannot prepare requested Libvirt capabilities XML file from the resources folder" ); + fail( errorMsg ); + } + + return caps; + } + + @Test + @DisplayName( "Get host UUID from libvirt XML capabilities file" ) + public void testGetHostUuid() + { + final Capabilities caps = this.newCapabilitiesInstance( "qemu-kvm_capabilities_default.xml" ); + + assertEquals( "9b2f12af-1fba-444c-b72b-9cbc43fb3ca5", caps.getHostUuid() ); + } + + @Test + @DisplayName( "Get host CPU from libvirt XML capabilities file" ) + public void testGetHostCpu() + { + final Capabilities caps = this.newCapabilitiesInstance( "qemu-kvm_capabilities_default.xml" ); + final Cpu hostCpu = caps.getHostCpu(); + + assertNotNull( hostCpu ); + assertEquals( "x86_64", hostCpu.getArch() ); + assertEquals( "Skylake-Client-IBRS", hostCpu.getModel() ); + assertEquals( "Intel", hostCpu.getVendor() ); + assertEquals( 1, hostCpu.getTopologySockets() ); + assertEquals( 1, hostCpu.getTopologyDies() ); + assertEquals( 4, hostCpu.getTopologyCores() ); + assertEquals( 1, hostCpu.getTopologyThreads() ); + } + + @Test + @DisplayName( "Get non-existent host CPU from libvirt XML capabilities file" ) + public void testGetHostCpuNonExistent() + { + final Capabilities caps = this.newCapabilitiesInstance( "qemu-kvm_capabilities_no-cpu.xml" ); + final Cpu hostCpu = caps.getHostCpu(); + + assertNull( hostCpu ); + } + + @Test + @DisplayName( "Get host CPU features from libvirt XML capabilities file" ) + public void testGetHostCpuFeatures() + { + final Capabilities caps = this.newCapabilitiesInstance( "qemu-kvm_capabilities_default.xml" ); + final Cpu hostCpu = caps.getHostCpu(); + + assertNotNull( hostCpu ); + + final List hostCpuFeatures = hostCpu.getFeatures(); + assertNotNull( hostCpuFeatures ); + assertEquals( 25, hostCpuFeatures.size() ); + + final Feature hostCpuFeature = hostCpuFeatures.get( 9 ); + assertNotNull( hostCpuFeature ); + assertEquals( "vmx", hostCpuFeature.getName() ); + } + + @Test + @DisplayName( "Get empty host CPU features from libvirt XML capabilities file" ) + public void testGetHostCpuFeaturesEmpty() + { + final Capabilities caps = this.newCapabilitiesInstance( "qemu-kvm_capabilities_no-cpu-features.xml" ); + final Cpu hostCpu = caps.getHostCpu(); + + assertNotNull( hostCpu ); + + final List hostCpuFeatures = hostCpu.getFeatures(); + assertNotNull( hostCpuFeatures ); + assertEquals( 0, hostCpuFeatures.size() ); + } + + @Test + @DisplayName( "Get host CPU pages from libvirt XML capabilities file" ) + public void testGetHostCpuPages() + { + final Capabilities caps = this.newCapabilitiesInstance( "qemu-kvm_capabilities_default.xml" ); + final Cpu hostCpu = caps.getHostCpu(); + + assertNotNull( hostCpu ); + + final List hostCpuPages = hostCpu.getPages(); + assertNotNull( hostCpuPages ); + assertEquals( 3, hostCpuPages.size() ); + + final Pages hostCpuPage = hostCpuPages.get( 2 ); + assertNotNull( hostCpuPage ); + assertEquals( new BigInteger( "1073741824" ).toString(), hostCpuPage.getSize().toString() ); + } + + @Test + @DisplayName( "Get empty host CPU pages from libvirt XML capabilities file" ) + public void testGetHostCpuPagesEmpty() + { + final Capabilities caps = this.newCapabilitiesInstance( "qemu-kvm_capabilities_no-cpu-pages.xml" ); + final Cpu hostCpu = caps.getHostCpu(); + + assertNotNull( hostCpu ); + + final List hostCpuPages = hostCpu.getPages(); + assertNotNull( hostCpuPages ); + assertEquals( 0, hostCpuPages.size() ); + } + + @Test + @DisplayName( "Get host IOMMU support from libvirt XML capabilities file" ) + public void testGetHostIommuSupport() + { + final Capabilities caps = this.newCapabilitiesInstance( "qemu-kvm_capabilities_default.xml" ); + + assertEquals( true, caps.hasHostIommuSupport() ); + } + + @Test + @DisplayName( "Get non-existent host IOMMU support from libvirt XML capabilities file" ) + public void testGetHostIommuSupportNonExistent() + { + final Capabilities caps = this.newCapabilitiesInstance( "qemu-kvm_capabilities_no-iommu.xml" ); + + assertEquals( false, caps.hasHostIommuSupport() ); + } + + @Test + @DisplayName( "Get guests from libvirt XML capabilities file" ) + public void testGetGuests() + { + final Capabilities caps = this.newCapabilitiesInstance( "qemu-kvm_capabilities_default.xml" ); + + final List guests = caps.getGuests(); + assertNotNull( guests ); + assertEquals( 26, guests.size() ); + + final Guest guest = guests.get( 3 ); + assertNotNull( guest ); + assertEquals( OsType.HVM.toString(), guest.getOsType().toString() ); + assertEquals( "aarch64", guest.getArchName() ); + assertEquals( 64, guest.getArchWordSize() ); + assertEquals( "/usr/bin/qemu-system-aarch64", guest.getArchEmulator() ); + } + + @Test + @DisplayName( "Get empty guests from libvirt XML capabilities file" ) + public void testGetGuestsEmpty() + { + final Capabilities caps = this.newCapabilitiesInstance( "qemu-kvm_capabilities_no-guests.xml" ); + + final List guests = caps.getGuests(); + assertNotNull( guests ); + assertEquals( 0, guests.size() ); + } + + @Test + @DisplayName( "Get guest machines from libvirt XML capabilities file" ) + public void testGetGuestMachines() + { + final Capabilities caps = this.newCapabilitiesInstance( "qemu-kvm_capabilities_default.xml" ); + + final List guests = caps.getGuests(); + assertNotNull( guests ); + assertEquals( 26, guests.size() ); + + final Guest guest = guests.get( 3 ); + assertNotNull( guest ); + + final List guestMachines = guest.getArchMachines(); + assertNotNull( guestMachines ); + assertEquals( 89, guestMachines.size() ); + + final Machine guestMachine = guestMachines.get( 5 ); + assertNotNull( guestMachine ); + assertNull( guestMachine.getCanonicalMachine() ); + assertEquals( 2, guestMachine.getMaxCpus() ); + assertEquals( "nuri", guestMachine.getName() ); + } + + @Test + @DisplayName( "Get empty guest machines from libvirt XML capabilities file" ) + public void testGetGuestMachinesEmpty() + { + final Capabilities caps = this.newCapabilitiesInstance( "qemu-kvm_capabilities_no-guest-machines.xml" ); + + final List guests = caps.getGuests(); + assertNotNull( guests ); + assertEquals( 26, guests.size() ); + + final Guest guest = guests.get( 3 ); + assertNotNull( guest ); + + final List guestMachines = guest.getArchMachines(); + assertNotNull( guestMachines ); + assertEquals( 0, guestMachines.size() ); + } + + @Test + @DisplayName( "Get canonical guest machine from libvirt XML capabilities file" ) + public void testGetGuestMachineCanonical() + { + final Capabilities caps = this.newCapabilitiesInstance( "qemu-kvm_capabilities_default.xml" ); + + final List guests = caps.getGuests(); + assertNotNull( guests ); + assertEquals( 26, guests.size() ); + + final Guest guest = guests.get( 3 ); + assertNotNull( guest ); + + final List guestMachines = guest.getArchMachines(); + assertNotNull( guestMachines ); + assertEquals( 89, guestMachines.size() ); + + final Machine guestMachine = guestMachines.get( 29 ); + assertNotNull( guestMachine ); + assertEquals( "virt-5.2", guestMachine.getCanonicalMachine() ); + assertEquals( 512, guestMachine.getMaxCpus() ); + assertEquals( "virt", guestMachine.getName() ); + } + + @Test + @DisplayName( "Get guest machine domains from libvirt XML capabilities file" ) + public void testGetGuestMachineDomains() + { + final Capabilities caps = this.newCapabilitiesInstance( "qemu-kvm_capabilities_default.xml" ); + + final List guests = caps.getGuests(); + assertNotNull( guests ); + assertEquals( 26, guests.size() ); + + final Guest guest = guests.get( 5 ); + assertNotNull( guest ); + + final List guestDomains = guest.getArchDomains(); + assertNotNull( guestDomains ); + assertEquals( 2, guestDomains.size() ); + + final Domain guestDomain = guestDomains.get( 1 ); + assertNotNull( guestDomain ); + assertEquals( Type.KVM, guestDomain.getType() ); + } + + @Test + @DisplayName( "Get empty guest machine domains from libvirt XML capabilities file" ) + public void testGetGuestMachineDomainsEmpty() + { + final Capabilities caps = this.newCapabilitiesInstance( "qemu-kvm_capabilities_no-guest-machines.xml" ); + + final List guests = caps.getGuests(); + assertNotNull( guests ); + assertEquals( 26, guests.size() ); + + final Guest guest = guests.get( 3 ); + assertNotNull( guest ); + + final List guestDomains = guest.getArchDomains(); + assertNotNull( guestDomains ); + assertEquals( 0, guestDomains.size() ); + } +} diff --git a/src/test/java/org/openslx/libvirt/domain/DomainTest.java b/src/test/java/org/openslx/libvirt/domain/DomainTest.java index a604b21..84f6a32 100644 --- a/src/test/java/org/openslx/libvirt/domain/DomainTest.java +++ b/src/test/java/org/openslx/libvirt/domain/DomainTest.java @@ -13,6 +13,7 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.openslx.libvirt.domain.Domain.CpuCheck; import org.openslx.libvirt.domain.Domain.CpuMode; +import org.openslx.libvirt.domain.Domain.OsType; import org.openslx.libvirt.xml.LibvirtXmlDocumentException; import org.openslx.libvirt.xml.LibvirtXmlSerializationException; import org.openslx.libvirt.xml.LibvirtXmlTestResources; @@ -177,6 +178,57 @@ public class DomainTest assertEquals( 4, vm.getVCpu() ); } + @Test + @DisplayName( "Get VM's OS type from libvirt XML file" ) + public void testGetOsType() + { + Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + assertEquals( OsType.HVM.toString(), vm.getOsType().toString() ); + } + + @Test + @DisplayName( "Set VM's OS type in libvirt XML file" ) + public void testSetOsType() + { + Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + vm.setOsType( OsType.XEN ); + assertEquals( OsType.XEN.toString(), vm.getOsType().toString() ); + } + + @Test + @DisplayName( "Get VM's OS architecture from libvirt XML file" ) + public void testGetOsArch() + { + Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + assertEquals( "x86_64", vm.getOsArch() ); + } + + @Test + @DisplayName( "Set VM's OS architecture in libvirt XML file" ) + public void testSetOsArch() + { + Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + vm.setOsArch( "aarch" ); + assertEquals( "aarch", vm.getOsArch() ); + } + + @Test + @DisplayName( "Get VM's OS machine from libvirt XML file" ) + public void testGetOsMachine() + { + Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + assertEquals( "pc-q35-5.1", vm.getOsMachine() ); + } + + @Test + @DisplayName( "Set VM's OS machine in libvirt XML file" ) + public void testSetOsMachine() + { + Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + vm.setOsMachine( "pc" ); + assertEquals( "pc", vm.getOsMachine() ); + } + @Test @DisplayName( "Get VM CPU model from libvirt XML file" ) public void testGetCpuModel() -- cgit v1.2.3-55-g7522 From 696fd71f026994e71a70097dba6dc0e37e996f32 Mon Sep 17 00:00:00 2001 From: Manuel Bentele Date: Tue, 23 Mar 2021 14:53:54 +0100 Subject: Add support for emulator binary in Libvirt domain XML documents --- src/main/java/org/openslx/libvirt/domain/Domain.java | 20 ++++++++++++++++++++ .../java/org/openslx/libvirt/domain/DomainTest.java | 17 +++++++++++++++++ 2 files changed, 37 insertions(+) (limited to 'src/test/java/org/openslx') diff --git a/src/main/java/org/openslx/libvirt/domain/Domain.java b/src/main/java/org/openslx/libvirt/domain/Domain.java index 9611c59..ca3df77 100644 --- a/src/main/java/org/openslx/libvirt/domain/Domain.java +++ b/src/main/java/org/openslx/libvirt/domain/Domain.java @@ -636,6 +636,26 @@ public class Domain extends LibvirtXmlDocument this.getRootXmlNode().setXmlElementAttributeValue( "cpu", "check", check.toString() ); } + /** + * Returns the file name of the emulator binary defined in the Libvirt domain XML document. + * + * @return file name of the emulator binary. + */ + public String getDevicesEmulator() + { + return this.getRootXmlNode().getXmlElementValue( "devices/emulator" ); + } + + /** + * Sets the file name of the emulator binary in the Libvirt domain XML document. + * + * @param emulator file name of the emulator binary. + */ + public void setDevicesEmulator( String emulator ) + { + this.getRootXmlNode().setXmlElementValue( "devices/emulator", emulator ); + } + /** * Returns virtual machine devices defined in the Libvirt domain XML document. * diff --git a/src/test/java/org/openslx/libvirt/domain/DomainTest.java b/src/test/java/org/openslx/libvirt/domain/DomainTest.java index 84f6a32..1712b68 100644 --- a/src/test/java/org/openslx/libvirt/domain/DomainTest.java +++ b/src/test/java/org/openslx/libvirt/domain/DomainTest.java @@ -280,6 +280,23 @@ public class DomainTest assertEquals( CpuCheck.NONE.toString(), vm.getCpuCheck().toString() ); } + @Test + @DisplayName( "Get VM emulator binary from libvirt XML file" ) + public void testGetDevicesEmulator() + { + Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + assertEquals( "/usr/bin/qemu-system-x86_64", vm.getDevicesEmulator() ); + } + + @Test + @DisplayName( "Set VM emulator binary in libvirt XML file" ) + public void testSetDevicesEmulator() + { + Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + vm.setDevicesEmulator( "/usr/bin/qemu-system-i386" ); + assertEquals( "/usr/bin/qemu-system-i386", vm.getDevicesEmulator() ); + } + @Test @DisplayName( "Get all VM devices from libvirt XML file" ) public void testGetDevices() -- cgit v1.2.3-55-g7522 From 936887890b6517d844331c7a37a1dc56c6a47da1 Mon Sep 17 00:00:00 2001 From: Manuel Bentele Date: Wed, 24 Mar 2021 12:35:37 +0100 Subject: Add parallel and serial devices to Libvirt domain XML documents --- .../java/org/openslx/libvirt/domain/Domain.java | 44 ++++++ .../org/openslx/libvirt/domain/device/Device.java | 14 ++ .../openslx/libvirt/domain/device/Parallel.java | 158 +++++++++++++++++++++ .../org/openslx/libvirt/domain/device/Serial.java | 156 ++++++++++++++++++++ .../org/openslx/libvirt/domain/DomainTest.java | 18 ++- 5 files changed, 389 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/openslx/libvirt/domain/device/Parallel.java create mode 100644 src/main/java/org/openslx/libvirt/domain/device/Serial.java (limited to 'src/test/java/org/openslx') diff --git a/src/main/java/org/openslx/libvirt/domain/Domain.java b/src/main/java/org/openslx/libvirt/domain/Domain.java index ca3df77..e399340 100644 --- a/src/main/java/org/openslx/libvirt/domain/Domain.java +++ b/src/main/java/org/openslx/libvirt/domain/Domain.java @@ -28,6 +28,8 @@ import org.openslx.libvirt.domain.device.Hostdev; import org.openslx.libvirt.domain.device.Interface; import org.openslx.libvirt.domain.device.InterfaceBridge; import org.openslx.libvirt.domain.device.InterfaceNetwork; +import org.openslx.libvirt.domain.device.Parallel; +import org.openslx.libvirt.domain.device.Serial; import org.openslx.libvirt.domain.device.Sound; import org.openslx.libvirt.domain.device.Video; import org.openslx.libvirt.xml.LibvirtXmlDocument; @@ -853,6 +855,28 @@ public class Domain extends LibvirtXmlDocument return Domain.filterDevices( Graphics.class, this.getDevices() ); } + /** + * Returns list of virtual machine parallel port devices specified in the Libvirt domain XML + * document. + * + * @return list of virtual machine parallel port devices. + */ + public ArrayList getParallelDevices() + { + return Domain.filterDevices( Parallel.class, this.getDevices() ); + } + + /** + * Returns list of virtual machine serial port devices specified in the Libvirt domain XML + * document. + * + * @return list of virtual machine serial port devices. + */ + public ArrayList getSerialDevices() + { + return Domain.filterDevices( Serial.class, this.getDevices() ); + } + /** * Returns list of virtual machine sound devices specified in the Libvirt domain XML document. * @@ -1086,6 +1110,26 @@ public class Domain extends LibvirtXmlDocument return GraphicsVnc.class.cast( this.addDevice( new GraphicsVnc() ) ); } + /** + * Adds a virtual machine parallel port device to the Libvirt domain XML document. + * + * @return reference to the added parallel port device if creation was successful. + */ + public Parallel addParallelDevice() + { + return Parallel.class.cast( this.addDevice( new Parallel() ) ); + } + + /** + * Adds a virtual machine serial port device to the Libvirt domain XML document. + * + * @return reference to the added serial port device if creation was successful. + */ + public Serial addSerialDevice() + { + return Serial.class.cast( this.addDevice( new Serial() ) ); + } + /** * Adds a virtual machine sound device to the Libvirt domain XML document. * diff --git a/src/main/java/org/openslx/libvirt/domain/device/Device.java b/src/main/java/org/openslx/libvirt/domain/device/Device.java index 5c26c55..b7403cd 100644 --- a/src/main/java/org/openslx/libvirt/domain/device/Device.java +++ b/src/main/java/org/openslx/libvirt/domain/device/Device.java @@ -86,6 +86,12 @@ public class Device extends LibvirtXmlNode } else if ( device instanceof Graphics ) { LibvirtXmlNode xmlNode = Device.createDeviceElement( xmlParentNode, Type.GRAPHICS ); createdDevice = Graphics.createInstance( Graphics.class.cast( device ), xmlNode ); + } else if ( device instanceof Parallel ) { + LibvirtXmlNode xmlNode = Device.createDeviceElement( xmlParentNode, Type.PARALLEL ); + createdDevice = Parallel.createInstance( xmlNode ); + } else if ( device instanceof Serial ) { + LibvirtXmlNode xmlNode = Device.createDeviceElement( xmlParentNode, Type.SERIAL ); + createdDevice = Serial.createInstance( xmlNode ); } else if ( device instanceof Sound ) { LibvirtXmlNode xmlNode = Device.createDeviceElement( xmlParentNode, Type.SOUND ); createdDevice = Sound.createInstance( xmlNode ); @@ -134,6 +140,12 @@ public class Device extends LibvirtXmlNode case GRAPHICS: device = Graphics.newInstance( xmlNode ); break; + case PARALLEL: + device = Parallel.newInstance( xmlNode ); + break; + case SERIAL: + device = Serial.newInstance( xmlNode ); + break; case SOUND: device = Sound.newInstance( xmlNode ); break; @@ -160,6 +172,8 @@ public class Device extends LibvirtXmlNode HOSTDEV ( "hostdev" ), INTERFACE ( "interface" ), GRAPHICS ( "graphics" ), + PARALLEL ( "parallel" ), + SERIAL ( "serial" ), SOUND ( "sound" ), VIDEO ( "video" ); // @formatter:on diff --git a/src/main/java/org/openslx/libvirt/domain/device/Parallel.java b/src/main/java/org/openslx/libvirt/domain/device/Parallel.java new file mode 100644 index 0000000..7db60ca --- /dev/null +++ b/src/main/java/org/openslx/libvirt/domain/device/Parallel.java @@ -0,0 +1,158 @@ +package org.openslx.libvirt.domain.device; + +import org.openslx.libvirt.xml.LibvirtXmlNode; + +/** + * A parallel port device node in a Libvirt domain XML document. + * + * @author Manuel Bentele + * @version 1.0 + */ +public class Parallel extends Device +{ + /** + * Creates an empty parallel port device. + */ + public Parallel() + { + super(); + } + + /** + * Creates a parallel port device representing an existing Libvirt XML parallel port device + * element. + * + * @param xmlNode existing Libvirt XML parallel port device element. + */ + public Parallel( LibvirtXmlNode xmlNode ) + { + super( xmlNode ); + } + + /** + * Returns the type of the parallel port device. + * + * @return type of the parallel port device. + */ + public Type getType() + { + final String type = this.getXmlElementAttributeValue( "type" ); + return Type.fromString( type ); + } + + /** + * Sets the type for the parallel port device. + * + * @param type type for the parallel port device. + */ + public void setType( Type type ) + { + this.setXmlElementAttributeValue( "type", type.toString() ); + } + + /** + * Returns the source of the parallel port device. + * + * @return source of the parallel port device. + */ + public String getSource() + { + return this.getXmlElementAttributeValue( "source", "path" ); + } + + /** + * Sets the source for the parallel port device. + * + * @param source source for the parallel port device. + */ + public void setSource( String source ) + { + this.setXmlElementAttributeValue( "source", "path", source ); + } + + /** + * Creates a non-existent parallel port device as Libvirt XML parallel port device element. + * + * @param xmlNode Libvirt XML node of the Libvirt XML parallel port device that is created. + * @return created parallel port device instance. + */ + public static Parallel createInstance( LibvirtXmlNode xmlNode ) + { + return Parallel.newInstance( xmlNode ); + } + + /** + * Creates a parallel port device representing an existing Libvirt XML parallel port device + * element. + * + * @param xmlNode existing Libvirt XML parallel port device element. + * @return parallel port device instance. + */ + public static Parallel newInstance( LibvirtXmlNode xmlNode ) + { + return new Parallel( xmlNode ); + } + + /** + * Type of parallel port device. + * + * @author Manuel Bentele + * @version 1.0 + */ + public enum Type + { + // @formatter:off + DEV ( "dev" ), + FILE ( "file" ), + PIPE ( "pipe" ), + UNIX ( "unix" ), + TCP ( "tcp" ), + UDP ( "udp" ), + NULL ( "null" ), + STDIO ( "stdio" ), + VC ( "vc" ), + PTY ( "pty" ), + SPICEVMC ( "spicevmc" ), + SPICEPORT( "spiceport" ), + NMDM ( "nmdm" ); + // @formatter:on + + /** + * Name of the parallel port device type. + */ + private String type; + + /** + * Creates parallel port device type. + * + * @param type valid name of the parallel port device type in a Libvirt domain XML document. + */ + Type( String type ) + { + this.type = type; + } + + @Override + public String toString() + { + return this.type; + } + + /** + * Creates parallel port device type from its name with error check. + * + * @param type name of the parallel port device type in a Libvirt domain XML document. + * @return valid parallel port device type. + */ + public static Type fromString( String type ) + { + for ( Type t : Type.values() ) { + if ( t.type.equalsIgnoreCase( type ) ) { + return t; + } + } + + return null; + } + } +} diff --git a/src/main/java/org/openslx/libvirt/domain/device/Serial.java b/src/main/java/org/openslx/libvirt/domain/device/Serial.java new file mode 100644 index 0000000..54be26e --- /dev/null +++ b/src/main/java/org/openslx/libvirt/domain/device/Serial.java @@ -0,0 +1,156 @@ +package org.openslx.libvirt.domain.device; + +import org.openslx.libvirt.xml.LibvirtXmlNode; + +/** + * A serial port device node in a Libvirt domain XML document. + * + * @author Manuel Bentele + * @version 1.0 + */ +public class Serial extends Device +{ + /** + * Creates an empty serial port device. + */ + public Serial() + { + super(); + } + + /** + * Creates a serial port device representing an existing Libvirt XML serial port device element. + * + * @param xmlNode existing Libvirt XML serial port device element. + */ + public Serial( LibvirtXmlNode xmlNode ) + { + super( xmlNode ); + } + + /** + * Returns the type of the serial port device. + * + * @return type of the serial port device. + */ + public Type getType() + { + final String type = this.getXmlElementAttributeValue( "type" ); + return Type.fromString( type ); + } + + /** + * Sets the type for the serial port device. + * + * @param type type for the serial port device. + */ + public void setType( Type type ) + { + this.setXmlElementAttributeValue( "type", type.toString() ); + } + + /** + * Returns the source of the serial port device. + * + * @return source of the serial port device. + */ + public String getSource() + { + return this.getXmlElementAttributeValue( "source", "path" ); + } + + /** + * Sets the source for the serial port device. + * + * @param source source for the serial port device. + */ + public void setSource( String source ) + { + this.setXmlElementAttributeValue( "source", "path", source ); + } + + /** + * Creates a non-existent serial port device as Libvirt XML serial port device element. + * + * @param xmlNode Libvirt XML node of the Libvirt XML serial port device that is created. + * @return created serial port device instance. + */ + public static Serial createInstance( LibvirtXmlNode xmlNode ) + { + return Serial.newInstance( xmlNode ); + } + + /** + * Creates a serial port device representing an existing Libvirt XML serial port device element. + * + * @param xmlNode existing Libvirt XML serial port device element. + * @return serial port device instance. + */ + public static Serial newInstance( LibvirtXmlNode xmlNode ) + { + return new Serial( xmlNode ); + } + + /** + * Type of serial port device. + * + * @author Manuel Bentele + * @version 1.0 + */ + public enum Type + { + // @formatter:off + DEV ( "dev" ), + FILE ( "file" ), + PIPE ( "pipe" ), + UNIX ( "unix" ), + TCP ( "tcp" ), + UDP ( "udp" ), + NULL ( "null" ), + STDIO ( "stdio" ), + VC ( "vc" ), + PTY ( "pty" ), + SPICEVMC ( "spicevmc" ), + SPICEPORT( "spiceport" ), + NMDM ( "nmdm" ); + // @formatter:on + + /** + * Name of the serial port device type. + */ + private String type; + + /** + * Creates serial port device type. + * + * @param type valid name of the serial port device type in a Libvirt domain XML document. + */ + Type( String type ) + { + this.type = type; + } + + @Override + public String toString() + { + return this.type; + } + + /** + * Creates serial port device type from its name with error check. + * + * @param type name of the serial port device type in a Libvirt domain XML document. + * @return valid serial port device type. + */ + public static Type fromString( String type ) + { + for ( Type t : Type.values() ) { + if ( t.type.equalsIgnoreCase( type ) ) { + return t; + } + } + + return null; + } + } +} diff --git a/src/test/java/org/openslx/libvirt/domain/DomainTest.java b/src/test/java/org/openslx/libvirt/domain/DomainTest.java index 1712b68..b4e0187 100644 --- a/src/test/java/org/openslx/libvirt/domain/DomainTest.java +++ b/src/test/java/org/openslx/libvirt/domain/DomainTest.java @@ -302,7 +302,7 @@ public class DomainTest public void testGetDevices() { Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); - assertEquals( 21, vm.getDevices().size() ); + assertEquals( 22, vm.getDevices().size() ); } @Test @@ -345,6 +345,22 @@ public class DomainTest assertEquals( 1, vm.getGraphicDevices().size() ); } + @Test + @DisplayName( "Get all VM parallel port devices from libvirt XML file" ) + public void testGetParallelDevices() + { + Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + assertEquals( 0, vm.getParallelDevices().size() ); + } + + @Test + @DisplayName( "Get all VM serial port devices from libvirt XML file" ) + public void testGetSerialDevices() + { + Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + assertEquals( 1, vm.getSerialDevices().size() ); + } + @Test @DisplayName( "Get all VM sound devices from libvirt XML file" ) public void testGetSoundDevices() -- cgit v1.2.3-55-g7522 From 2d012afbaf72794ad8ff16cc7ac50bc2e05b0c74 Mon Sep 17 00:00:00 2001 From: Manuel Bentele Date: Wed, 24 Mar 2021 15:20:33 +0100 Subject: Add shared folder support to Libvirt domain XML documents --- .../java/org/openslx/libvirt/domain/Domain.java | 22 ++ .../org/openslx/libvirt/domain/device/Device.java | 7 + .../openslx/libvirt/domain/device/FileSystem.java | 292 +++++++++++++++++++++ .../org/openslx/libvirt/domain/DomainTest.java | 8 + 4 files changed, 329 insertions(+) create mode 100644 src/main/java/org/openslx/libvirt/domain/device/FileSystem.java (limited to 'src/test/java/org/openslx') diff --git a/src/main/java/org/openslx/libvirt/domain/Domain.java b/src/main/java/org/openslx/libvirt/domain/Domain.java index e399340..f29fcd0 100644 --- a/src/main/java/org/openslx/libvirt/domain/Domain.java +++ b/src/main/java/org/openslx/libvirt/domain/Domain.java @@ -20,6 +20,7 @@ import org.openslx.libvirt.domain.device.Disk; import org.openslx.libvirt.domain.device.DiskCdrom; import org.openslx.libvirt.domain.device.DiskFloppy; import org.openslx.libvirt.domain.device.DiskStorage; +import org.openslx.libvirt.domain.device.FileSystem; import org.openslx.libvirt.domain.device.Graphics; import org.openslx.libvirt.domain.device.GraphicsSdl; import org.openslx.libvirt.domain.device.GraphicsSpice; @@ -824,6 +825,17 @@ public class Domain extends LibvirtXmlDocument return Domain.filterDevices( DiskStorage.class, this.getDevices() ); } + /** + * Returns list of virtual machine file system devices specified in the Libvirt domain XML + * document. + * + * @return list of virtual machine file system devices. + */ + public ArrayList getFileSystemDevices() + { + return Domain.filterDevices( FileSystem.class, this.getDevices() ); + } + /** * Returns list of virtual machine hostdev devices specified in the Libvirt domain XML document. * @@ -1030,6 +1042,16 @@ public class Domain extends LibvirtXmlDocument return DiskStorage.class.cast( this.addDevice( new DiskStorage() ) ); } + /** + * Adds a virtual machine file system device to the Libvirt domain XML document. + * + * @return reference to the added file system device if creation was successful. + */ + public FileSystem addFileSystemDevice() + { + return FileSystem.class.cast( this.addDevice( new FileSystem() ) ); + } + /** * Adds a virtual machine disk device to the Libvirt domain XML document. * diff --git a/src/main/java/org/openslx/libvirt/domain/device/Device.java b/src/main/java/org/openslx/libvirt/domain/device/Device.java index b7403cd..151592a 100644 --- a/src/main/java/org/openslx/libvirt/domain/device/Device.java +++ b/src/main/java/org/openslx/libvirt/domain/device/Device.java @@ -77,6 +77,9 @@ public class Device extends LibvirtXmlNode } else if ( device instanceof Disk ) { LibvirtXmlNode xmlNode = Device.createDeviceElement( xmlParentNode, Type.DISK ); createdDevice = Disk.createInstance( Disk.class.cast( device ), xmlNode ); + } else if ( device instanceof FileSystem ) { + LibvirtXmlNode xmlNode = Device.createDeviceElement( xmlParentNode, Type.FILESYSTEM ); + createdDevice = FileSystem.createInstance( FileSystem.class.cast( device ), xmlNode ); } else if ( device instanceof Hostdev ) { LibvirtXmlNode xmlNode = Device.createDeviceElement( xmlParentNode, Type.HOSTDEV ); createdDevice = Hostdev.createInstance( Hostdev.class.cast( device ), xmlNode ); @@ -131,6 +134,9 @@ public class Device extends LibvirtXmlNode case DISK: device = Disk.newInstance( xmlNode ); break; + case FILESYSTEM: + device = FileSystem.newInstance( xmlNode ); + break; case HOSTDEV: device = Hostdev.newInstance( xmlNode ); break; @@ -169,6 +175,7 @@ public class Device extends LibvirtXmlNode // @formatter:off CONTROLLER( "controller" ), DISK ( "disk" ), + FILESYSTEM( "filesystem" ), HOSTDEV ( "hostdev" ), INTERFACE ( "interface" ), GRAPHICS ( "graphics" ), diff --git a/src/main/java/org/openslx/libvirt/domain/device/FileSystem.java b/src/main/java/org/openslx/libvirt/domain/device/FileSystem.java new file mode 100644 index 0000000..9ec8caf --- /dev/null +++ b/src/main/java/org/openslx/libvirt/domain/device/FileSystem.java @@ -0,0 +1,292 @@ +package org.openslx.libvirt.domain.device; + +import org.openslx.libvirt.xml.LibvirtXmlNode; + +/** + * A file system device node in a Libvirt domain XML document. + * + * @author Manuel Bentele + * @version 1.0 + */ +public class FileSystem extends Device +{ + /** + * Creates an empty file system device. + */ + public FileSystem() + { + super(); + } + + /** + * Creates a file system device representing an existing Libvirt XML file system device element. + * + * @param xmlNode existing Libvirt XML file system device element. + */ + public FileSystem( LibvirtXmlNode xmlNode ) + { + super( xmlNode ); + } + + /** + * Returns access mode of the file system device. + * + * @return access mode of the file system device. + */ + public AccessMode getAccessMode() + { + final String mode = this.getXmlElementAttributeValue( "accessmode" ); + return AccessMode.fromString( mode ); + } + + /** + * Sets access mode for the file system device. + * + * @param mode access mode for the file system device. + */ + public void setAccessMode( AccessMode mode ) + { + this.setXmlElementAttributeValue( "accessmode", mode.toString() ); + } + + /** + * Returns type of the file system device. + * + * @return type of the file system device. + */ + public Type getType() + { + final String type = this.getXmlElementAttributeValue( "type" ); + return Type.fromString( type ); + } + + /** + * Sets type for the file system device. + * + * @param type type for the file system device. + */ + public void setType( Type type ) + { + this.setXmlElementAttributeValue( "type", type.toString() ); + } + + /** + * Returns source of the file system device. + * + * @return source of the file system device. + */ + public String getSource() + { + final Type type = this.getType(); + String source = null; + + switch ( type ) { + case BIND: + source = this.getXmlElementAttributeValue( "source", "dir" ); + break; + case BLOCK: + source = this.getXmlElementAttributeValue( "source", "dev" ); + break; + case FILE: + source = this.getXmlElementAttributeValue( "source", "file" ); + break; + case MOUNT: + source = this.getXmlElementAttributeValue( "source", "dir" ); + break; + case RAM: + source = this.getXmlElementAttributeValue( "source", "usage" ); + break; + case TEMPLATE: + source = this.getXmlElementAttributeValue( "source", "name" ); + break; + } + + return source; + } + + /** + * Sets source for the file system device. + * + * @param source source for the file system device. + */ + public void setSource( String source ) + { + Type type = this.getType(); + + // remove all attributes from sub-element 'source' + this.removeXmlElementAttributes( "source" ); + + switch ( type ) { + case BIND: + this.setXmlElementAttributeValue( "source", "dir", source ); + break; + case BLOCK: + this.setXmlElementAttributeValue( "source", "dev", source ); + break; + case FILE: + this.setXmlElementAttributeValue( "source", "file", source ); + break; + case MOUNT: + this.setXmlElementAttributeValue( "source", "dir", source ); + break; + case RAM: + this.setXmlElementAttributeValue( "source", "usage", source ); + break; + case TEMPLATE: + this.setXmlElementAttributeValue( "source", "name", source ); + break; + } + } + + /** + * Returns target of the file system device. + * + * @return target of the file system device. + */ + public String getTarget() + { + return this.getXmlElementAttributeValue( "target", "dir" ); + } + + /** + * Sets target for the file system device. + * + * @param target target for the file system device. + */ + public void setTarget( String target ) + { + this.setXmlElementAttributeValue( "target", "dir", target ); + } + + /** + * Creates a non-existent file system device as Libvirt XML device element. + * + * @param xmlNode Libvirt XML node of the Libvirt XML device that is created. + * @return created file system device instance. + */ + public static FileSystem createInstance( LibvirtXmlNode xmlNode ) + { + return FileSystem.newInstance( xmlNode ); + } + + /** + * Creates a file system device representing an existing Libvirt XML file system device element. + * + * @param xmlNode existing Libvirt XML file system device element. + * @return file system device instance. + */ + public static FileSystem newInstance( LibvirtXmlNode xmlNode ) + { + return new FileSystem( xmlNode ); + } + + /** + * Access mode for the file system device. + * + * @author Manuel Bentele + * @version 1.0 + */ + public enum AccessMode + { + // @formatter:off + PASSTHROUGH( "passthrough" ), + MAPPED ( "mapped" ), + SQUASH ( "squash" ); + // @formatter:on + + /** + * Name of the file system device access mode. + */ + private String mode; + + /** + * Creates file system device access mode. + * + * @param mode valid name of the file system device access mode in a Libvirt domain XML + * document. + */ + AccessMode( String mode ) + { + this.mode = mode; + } + + @Override + public String toString() + { + return this.mode; + } + + /** + * Creates file system device access mode from its name with error check. + * + * @param mode name of the file system device access mode in a Libvirt domain XML document. + * @return valid file system device access mode. + */ + public static AccessMode fromString( String mode ) + { + for ( AccessMode a : AccessMode.values() ) { + if ( a.mode.equalsIgnoreCase( mode ) ) { + return a; + } + } + + return null; + } + } + + /** + * Type of file system device. + * + * @author Manuel Bentele + * @version 1.0 + */ + public enum Type + { + // @formatter:off + MOUNT ( "mount" ), + TEMPLATE( "template" ), + FILE ( "file" ), + BLOCK ( "block" ), + RAM ( "ram" ), + BIND ( "bind" ); + // @formatter:on + + /** + * Name of the file system device type. + */ + private String type; + + /** + * Creates file system device type. + * + * @param type valid name of the file system device type in a Libvirt domain XML document. + */ + Type( String type ) + { + this.type = type; + } + + @Override + public String toString() + { + return this.type; + } + + /** + * Creates file system device type from its name with error check. + * + * @param type name of the file system device type in a Libvirt domain XML document. + * @return valid file system device type. + */ + public static Type fromString( String type ) + { + for ( Type t : Type.values() ) { + if ( t.type.equalsIgnoreCase( type ) ) { + return t; + } + } + + return null; + } + } +} diff --git a/src/test/java/org/openslx/libvirt/domain/DomainTest.java b/src/test/java/org/openslx/libvirt/domain/DomainTest.java index b4e0187..aa556f9 100644 --- a/src/test/java/org/openslx/libvirt/domain/DomainTest.java +++ b/src/test/java/org/openslx/libvirt/domain/DomainTest.java @@ -321,6 +321,14 @@ public class DomainTest assertEquals( 3, vm.getDiskDevices().size() ); } + @Test + @DisplayName( "Get all VM file system devices from libvirt XML file" ) + public void testGetFileSystemDevices() + { + Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + assertEquals( 0, vm.getFileSystemDevices().size() ); + } + @Test @DisplayName( "Get all VM hostdev devices from libvirt XML file" ) public void testGetHostdevDevices() -- cgit v1.2.3-55-g7522 From 7aa2380b3bc33ad6b1a892b1b3491c93eee8edbb Mon Sep 17 00:00:00 2001 From: Manuel Bentele Date: Fri, 26 Mar 2021 16:22:54 +0100 Subject: Fix issues in filtering sources of Libvirt network interfaces --- .../java/org/openslx/libvirt/domain/Domain.java | 3 +- .../org/openslx/libvirt/domain/device/Disk.java | 4 +- src/test/java/org/openslx/vm/QemuMetaDataTest.java | 104 +++++++++++++++++---- 3 files changed, 88 insertions(+), 23 deletions(-) (limited to 'src/test/java/org/openslx') diff --git a/src/main/java/org/openslx/libvirt/domain/Domain.java b/src/main/java/org/openslx/libvirt/domain/Domain.java index 727da13..dbc60e6 100644 --- a/src/main/java/org/openslx/libvirt/domain/Domain.java +++ b/src/main/java/org/openslx/libvirt/domain/Domain.java @@ -1216,7 +1216,8 @@ public class Domain extends LibvirtXmlDocument public void removeInterfaceDevicesSource() { for ( Interface interfaceDevice : this.getInterfaceDevices() ) { - interfaceDevice.removeSource(); + // set empty source to preserve the XML attribute (to prevent XML validation errors) + interfaceDevice.setSource( "" ); } } } diff --git a/src/main/java/org/openslx/libvirt/domain/device/Disk.java b/src/main/java/org/openslx/libvirt/domain/device/Disk.java index 464e7b6..d9007f5 100644 --- a/src/main/java/org/openslx/libvirt/domain/device/Disk.java +++ b/src/main/java/org/openslx/libvirt/domain/device/Disk.java @@ -68,7 +68,7 @@ public class Disk extends Device storageSource = this.getXmlElementAttributeValue( "source", "file" ); break; case BLOCK: - storageSource = this.getXmlElementAttributeValue( "source", "bdev" ); + storageSource = this.getXmlElementAttributeValue( "source", "dev" ); break; } @@ -96,7 +96,7 @@ public class Disk extends Device this.setXmlElementAttributeValue( "source", "file", source ); break; case BLOCK: - this.setXmlElementAttributeValue( "source", "bdev", source ); + this.setXmlElementAttributeValue( "source", "dev", source ); break; } } diff --git a/src/test/java/org/openslx/vm/QemuMetaDataTest.java b/src/test/java/org/openslx/vm/QemuMetaDataTest.java index 3217fda..a3053a1 100644 --- a/src/test/java/org/openslx/vm/QemuMetaDataTest.java +++ b/src/test/java/org/openslx/vm/QemuMetaDataTest.java @@ -1,5 +1,6 @@ package org.openslx.vm; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -19,6 +20,7 @@ import org.apache.log4j.LogManager; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.function.Executable; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; @@ -58,64 +60,94 @@ public class QemuMetaDataTest @Test @DisplayName( "Test display name from VM configuration" ) - public void testQemuMetaDataGetDisplayName() throws UnsupportedVirtualizerFormatException, IOException + public void testQemuMetaDataGetDisplayName() + throws UnsupportedVirtualizerFormatException, IOException, NoSuchFieldException, SecurityException, + IllegalArgumentException, IllegalAccessException { File file = LibvirtXmlTestResources.getLibvirtXmlFile( "qemu-kvm_default-archlinux-vm.xml" ); QemuMetaData vmConfig = new QemuMetaData( null, file ); + final Domain vmLibvirtDomainConfig = QemuMetaDataTest.getPrivateDomainFromQemuMetaData( vmConfig ); + final String displayName = vmConfig.getDisplayName(); assertEquals( "archlinux", displayName ); + + assertDoesNotThrow​( () -> vmLibvirtDomainConfig.validateXml() ); } @Test @DisplayName( "Test machine snapshot state from VM configuration" ) - public void testQemuMetaDataIsMachineSnapshot() throws UnsupportedVirtualizerFormatException, IOException + public void testQemuMetaDataIsMachineSnapshot() + throws UnsupportedVirtualizerFormatException, IOException, NoSuchFieldException, SecurityException, + IllegalArgumentException, IllegalAccessException { File file = LibvirtXmlTestResources.getLibvirtXmlFile( "qemu-kvm_default-archlinux-vm.xml" ); QemuMetaData vmConfig = new QemuMetaData( null, file ); + final Domain vmLibvirtDomainConfig = QemuMetaDataTest.getPrivateDomainFromQemuMetaData( vmConfig ); + final boolean isVmSnapshot = vmConfig.isMachineSnapshot(); assertEquals( false, isVmSnapshot ); + + assertDoesNotThrow​( () -> vmLibvirtDomainConfig.validateXml() ); } @Test @DisplayName( "Test supported image formats from VM configuration" ) - public void testQemuMetaDataGetSupportedImageFormats() throws UnsupportedVirtualizerFormatException, IOException + public void testQemuMetaDataGetSupportedImageFormats() + throws UnsupportedVirtualizerFormatException, IOException, NoSuchFieldException, SecurityException, + IllegalArgumentException, IllegalAccessException { File file = LibvirtXmlTestResources.getLibvirtXmlFile( "qemu-kvm_default-archlinux-vm.xml" ); QemuMetaData vmConfig = new QemuMetaData( null, file ); + final Domain vmLibvirtDomainConfig = QemuMetaDataTest.getPrivateDomainFromQemuMetaData( vmConfig ); + final List supportedImageFormats = vmConfig.getSupportedImageFormats(); assertNotNull( supportedImageFormats ); assertEquals( 3, supportedImageFormats.size() ); assertEquals( true, supportedImageFormats .containsAll( Arrays.asList( ImageFormat.QCOW2, ImageFormat.VMDK, ImageFormat.VDI ) ) ); + + assertDoesNotThrow​( () -> vmLibvirtDomainConfig.validateXml() ); } @Test @DisplayName( "Test output of HDDs from VM configuration" ) - public void testQemuMetaDataGetHdds() throws UnsupportedVirtualizerFormatException, IOException + public void testQemuMetaDataGetHdds() + throws UnsupportedVirtualizerFormatException, IOException, NoSuchFieldException, SecurityException, + IllegalArgumentException, IllegalAccessException { File file = LibvirtXmlTestResources.getLibvirtXmlFile( "qemu-kvm_default-archlinux-vm.xml" ); QemuMetaData vmConfig = new QemuMetaData( null, file ); + final Domain vmLibvirtDomainConfig = QemuMetaDataTest.getPrivateDomainFromQemuMetaData( vmConfig ); + final List hdds = vmConfig.getHdds(); assertNotNull( hdds ); assertEquals( 1, hdds.size() ); assertEquals( "/var/lib/libvirt/images/archlinux.qcow2", hdds.get( 0 ).diskImage ); + + assertDoesNotThrow​( () -> vmLibvirtDomainConfig.validateXml() ); } @Test @DisplayName( "Test output of unfiltered VM configuration" ) - public void testQemuMetaDataGetDefinitionArray() throws UnsupportedVirtualizerFormatException, IOException + public void testQemuMetaDataGetDefinitionArray() + throws UnsupportedVirtualizerFormatException, IOException, NoSuchFieldException, SecurityException, + IllegalArgumentException, IllegalAccessException { File file = LibvirtXmlTestResources.getLibvirtXmlFile( "qemu-kvm_default-archlinux-vm.xml" ); QemuMetaData vmConfig = new QemuMetaData( null, file ); + final Domain vmLibvirtDomainConfig = QemuMetaDataTest.getPrivateDomainFromQemuMetaData( vmConfig ); + + final int numberOfDeletedElements = 1; + final String unfilteredXmlConfig = new String( vmConfig.getDefinitionArray(), StandardCharsets.UTF_8 ); final String originalXmlConfig = FileUtils.readFileToString( file, StandardCharsets.UTF_8 ); @@ -124,17 +156,23 @@ public class QemuMetaDataTest final int lengthUnfilteredXmlConfig = unfilteredXmlConfig.split( System.lineSeparator() ).length; final int lengthOriginalXmlConfig = originalXmlConfig.split( System.lineSeparator() ).length; - assertEquals( lengthOriginalXmlConfig, lengthUnfilteredXmlConfig ); + assertEquals( lengthOriginalXmlConfig, lengthUnfilteredXmlConfig + numberOfDeletedElements ); + + assertDoesNotThrow​( () -> vmLibvirtDomainConfig.validateXml() ); } @Test @DisplayName( "Test output of filtered VM configuration" ) - public void testQemuMetaDataGetFilteredDefinitionArray() throws UnsupportedVirtualizerFormatException, IOException + public void testQemuMetaDataGetFilteredDefinitionArray() + throws UnsupportedVirtualizerFormatException, IOException, NoSuchFieldException, SecurityException, + IllegalArgumentException, IllegalAccessException { File file = LibvirtXmlTestResources.getLibvirtXmlFile( "qemu-kvm_default-archlinux-vm.xml" ); QemuMetaData vmConfig = new QemuMetaData( null, file ); - final int numberOfDeletedElements = 4; + final Domain vmLibvirtDomainConfig = QemuMetaDataTest.getPrivateDomainFromQemuMetaData( vmConfig ); + + final int numberOfDeletedElements = 2; final String filteredXmlConfig = new String( vmConfig.getFilteredDefinitionArray(), StandardCharsets.UTF_8 ); final String originalXmlConfig = FileUtils.readFileToString( file, StandardCharsets.UTF_8 ); @@ -145,6 +183,8 @@ public class QemuMetaDataTest final int lengthOriginalXmlConfig = originalXmlConfig.split( System.lineSeparator() ).length; assertEquals( lengthOriginalXmlConfig, lengthFilteredXmlConfig + numberOfDeletedElements ); + + assertDoesNotThrow​( () -> vmLibvirtDomainConfig.validateXml() ); } @ParameterizedTest @@ -158,7 +198,7 @@ public class QemuMetaDataTest File file = LibvirtXmlTestResources.getLibvirtXmlFile( xmlFileName ); QemuMetaData vmConfig = new QemuMetaData( null, file ); - Domain vmLibvirtDomainConfig = QemuMetaDataTest.getPrivateDomainFromQemuMetaData( vmConfig ); + final Domain vmLibvirtDomainConfig = QemuMetaDataTest.getPrivateDomainFromQemuMetaData( vmConfig ); final int numHddsLibvirtDomainXmlBeforeAdd = vmLibvirtDomainConfig.getDiskStorageDevices().size(); final int numHddsQemuMetaDataBeforeAdd = vmConfig.hdds.size(); @@ -184,6 +224,8 @@ public class QemuMetaDataTest DiskStorage addedStorageDevice = vmLibvirtDomainConfig.getDiskStorageDevices().get( 0 ); assertEquals( diskFile.getAbsolutePath(), addedStorageDevice.getStorageSource() ); + + assertDoesNotThrow​( () -> vmLibvirtDomainConfig.validateXml() ); } @ParameterizedTest @@ -197,7 +239,7 @@ public class QemuMetaDataTest File file = LibvirtXmlTestResources.getLibvirtXmlFile( xmlFileName ); QemuMetaData vmConfig = new QemuMetaData( null, file ); - Domain vmLibvirtDomainConfig = QemuMetaDataTest.getPrivateDomainFromQemuMetaData( vmConfig ); + final Domain vmLibvirtDomainConfig = QemuMetaDataTest.getPrivateDomainFromQemuMetaData( vmConfig ); final int numCdromsLibvirtDomainXmlBeforeAdd = vmLibvirtDomainConfig.getDiskCdromDevices().size(); @@ -210,6 +252,8 @@ public class QemuMetaDataTest DiskCdrom addedCdromDevice = vmLibvirtDomainConfig.getDiskCdromDevices().get( 0 ); assertEquals( diskFile.getAbsolutePath(), addedCdromDevice.getStorageSource() ); + + assertDoesNotThrow​( () -> vmLibvirtDomainConfig.validateXml() ); } @ParameterizedTest @@ -222,7 +266,7 @@ public class QemuMetaDataTest File file = LibvirtXmlTestResources.getLibvirtXmlFile( xmlFileName ); QemuMetaData vmConfig = new QemuMetaData( null, file ); - Domain vmLibvirtDomainConfig = QemuMetaDataTest.getPrivateDomainFromQemuMetaData( vmConfig ); + final Domain vmLibvirtDomainConfig = QemuMetaDataTest.getPrivateDomainFromQemuMetaData( vmConfig ); final int numCdromsLibvirtDomainXmlBeforeAdd = vmLibvirtDomainConfig.getDiskCdromDevices().size(); @@ -235,6 +279,8 @@ public class QemuMetaDataTest DiskCdrom addedCdromDevice = vmLibvirtDomainConfig.getDiskCdromDevices().get( 0 ); assertEquals( QemuMetaData.CDROM_DEFAULT_PHYSICAL_DRIVE, addedCdromDevice.getStorageSource() ); + + assertDoesNotThrow​( () -> vmLibvirtDomainConfig.validateXml() ); } @ParameterizedTest @@ -248,7 +294,7 @@ public class QemuMetaDataTest File file = LibvirtXmlTestResources.getLibvirtXmlFile( xmlFileName ); QemuMetaData vmConfig = new QemuMetaData( null, file ); - Domain vmLibvirtDomainConfig = QemuMetaDataTest.getPrivateDomainFromQemuMetaData( vmConfig ); + final Domain vmLibvirtDomainConfig = QemuMetaDataTest.getPrivateDomainFromQemuMetaData( vmConfig ); final int numFloppiesLibvirtDomainXmlBeforeAdd = vmLibvirtDomainConfig.getDiskFloppyDevices().size(); @@ -262,6 +308,8 @@ public class QemuMetaDataTest DiskFloppy addedFloppyDevice = vmLibvirtDomainConfig.getDiskFloppyDevices().get( 0 ); assertTrue( addedFloppyDevice.isReadOnly() ); assertEquals( diskFile.getAbsolutePath(), addedFloppyDevice.getStorageSource() ); + + assertDoesNotThrow​( () -> vmLibvirtDomainConfig.validateXml() ); } @ParameterizedTest @@ -274,11 +322,13 @@ public class QemuMetaDataTest File file = LibvirtXmlTestResources.getLibvirtXmlFile( "qemu-kvm_default-archlinux-vm.xml" ); QemuMetaData vmConfig = new QemuMetaData( null, file ); - Domain vmLibvirtDomainConfig = QemuMetaDataTest.getPrivateDomainFromQemuMetaData( vmConfig ); + final Domain vmLibvirtDomainConfig = QemuMetaDataTest.getPrivateDomainFromQemuMetaData( vmConfig ); vmConfig.addCpuCoreCount( coreCount ); assertEquals( coreCount, vmLibvirtDomainConfig.getVCpu() ); + + assertDoesNotThrow​( () -> vmLibvirtDomainConfig.validateXml() ); } @ParameterizedTest @@ -291,7 +341,7 @@ public class QemuMetaDataTest File file = LibvirtXmlTestResources.getLibvirtXmlFile( xmlFileName ); QemuMetaData vmConfig = new QemuMetaData( null, file ); - Domain vmLibvirtDomainConfig = QemuMetaDataTest.getPrivateDomainFromQemuMetaData( vmConfig ); + final Domain vmLibvirtDomainConfig = QemuMetaDataTest.getPrivateDomainFromQemuMetaData( vmConfig ); SoundCardType soundCardType = vmConfig.getSoundCard(); @@ -300,6 +350,8 @@ public class QemuMetaDataTest } else { assertEquals( SoundCardType.HD_AUDIO, soundCardType ); } + + assertDoesNotThrow​( () -> vmLibvirtDomainConfig.validateXml() ); } @ParameterizedTest @@ -312,7 +364,7 @@ public class QemuMetaDataTest File file = LibvirtXmlTestResources.getLibvirtXmlFile( xmlFileName ); QemuMetaData vmConfig = new QemuMetaData( null, file ); - Domain vmLibvirtDomainConfig = QemuMetaDataTest.getPrivateDomainFromQemuMetaData( vmConfig ); + final Domain vmLibvirtDomainConfig = QemuMetaDataTest.getPrivateDomainFromQemuMetaData( vmConfig ); final int numSoundDevsLibvirtDomainXmlBeforeAdd = vmLibvirtDomainConfig.getSoundDevices().size(); @@ -325,6 +377,8 @@ public class QemuMetaDataTest Sound addedSoundDevice = vmLibvirtDomainConfig.getSoundDevices().get( 0 ); assertEquals( Sound.Model.SB16, addedSoundDevice.getModel() ); + + assertDoesNotThrow​( () -> vmLibvirtDomainConfig.validateXml() ); } @ParameterizedTest @@ -337,7 +391,7 @@ public class QemuMetaDataTest File file = LibvirtXmlTestResources.getLibvirtXmlFile( xmlFileName ); QemuMetaData vmConfig = new QemuMetaData( null, file ); - Domain vmLibvirtDomainConfig = QemuMetaDataTest.getPrivateDomainFromQemuMetaData( vmConfig ); + final Domain vmLibvirtDomainConfig = QemuMetaDataTest.getPrivateDomainFromQemuMetaData( vmConfig ); EthernetDevType ethernetDeviceType = vmConfig.getEthernetDevType( 0 ); @@ -346,6 +400,8 @@ public class QemuMetaDataTest } else { assertEquals( EthernetDevType.PARAVIRT, ethernetDeviceType ); } + + assertDoesNotThrow​( () -> vmLibvirtDomainConfig.validateXml() ); } @ParameterizedTest @@ -358,7 +414,7 @@ public class QemuMetaDataTest File file = LibvirtXmlTestResources.getLibvirtXmlFile( xmlFileName ); QemuMetaData vmConfig = new QemuMetaData( null, file ); - Domain vmLibvirtDomainConfig = QemuMetaDataTest.getPrivateDomainFromQemuMetaData( vmConfig ); + final Domain vmLibvirtDomainConfig = QemuMetaDataTest.getPrivateDomainFromQemuMetaData( vmConfig ); vmConfig.setEthernetDevType( 0, EthernetDevType.E1000E ); @@ -366,6 +422,8 @@ public class QemuMetaDataTest Interface addedEthernetDevice = vmLibvirtDomainConfig.getInterfaceDevices().get( 0 ); assertEquals( Interface.Model.E1000E, addedEthernetDevice.getModel() ); } + + assertDoesNotThrow​( () -> vmLibvirtDomainConfig.validateXml() ); } @ParameterizedTest @@ -378,7 +436,7 @@ public class QemuMetaDataTest File file = LibvirtXmlTestResources.getLibvirtXmlFile( xmlFileName ); QemuMetaData vmConfig = new QemuMetaData( null, file ); - Domain vmLibvirtDomainConfig = QemuMetaDataTest.getPrivateDomainFromQemuMetaData( vmConfig ); + final Domain vmLibvirtDomainConfig = QemuMetaDataTest.getPrivateDomainFromQemuMetaData( vmConfig ); UsbSpeed maxUsbSpeed = vmConfig.getMaxUsbSpeed(); @@ -387,6 +445,8 @@ public class QemuMetaDataTest } else { assertEquals( UsbSpeed.USB3_0, maxUsbSpeed ); } + + assertDoesNotThrow​( () -> vmLibvirtDomainConfig.validateXml() ); } @ParameterizedTest @@ -399,7 +459,7 @@ public class QemuMetaDataTest File file = LibvirtXmlTestResources.getLibvirtXmlFile( xmlFileName ); QemuMetaData vmConfig = new QemuMetaData( null, file ); - Domain vmLibvirtDomainConfig = QemuMetaDataTest.getPrivateDomainFromQemuMetaData( vmConfig ); + final Domain vmLibvirtDomainConfig = QemuMetaDataTest.getPrivateDomainFromQemuMetaData( vmConfig ); final int numUsbControllersLibvirtDomainXmlBeforeAdd = vmLibvirtDomainConfig.getUsbControllerDevices().size(); @@ -412,6 +472,8 @@ public class QemuMetaDataTest ControllerUsb addedUsbControllerDevice = vmLibvirtDomainConfig.getUsbControllerDevices().get( 0 ); assertEquals( ControllerUsb.Model.ICH9_EHCI1, addedUsbControllerDevice.getModel() ); + + assertDoesNotThrow​( () -> vmLibvirtDomainConfig.validateXml() ); } static Stream configAndEthernetTypeProvider() @@ -435,7 +497,7 @@ public class QemuMetaDataTest File file = LibvirtXmlTestResources.getLibvirtXmlFile( xmlFileName ); QemuMetaData vmConfig = new QemuMetaData( null, file ); - Domain vmLibvirtDomainConfig = QemuMetaDataTest.getPrivateDomainFromQemuMetaData( vmConfig ); + final Domain vmLibvirtDomainConfig = QemuMetaDataTest.getPrivateDomainFromQemuMetaData( vmConfig ); final int numEthernetDevsLibvirtDomainXmlBeforeAdd = vmLibvirtDomainConfig.getInterfaceDevices().size(); @@ -464,5 +526,7 @@ public class QemuMetaDataTest assertEquals( QemuMetaData.NETWORK_DEFAULT_NAT, addedEthernetDevice.getSource() ); break; } + + assertDoesNotThrow​( () -> vmLibvirtDomainConfig.validateXml() ); } } -- cgit v1.2.3-55-g7522 From be9fcc35d87eb5455937799248473c30a1d6afa8 Mon Sep 17 00:00:00 2001 From: Manuel Bentele Date: Mon, 12 Apr 2021 07:58:12 +0200 Subject: Removed invalid symbol in function call syntax --- src/test/java/org/openslx/vm/QemuMetaDataTest.java | 37 +++++++++++----------- 1 file changed, 18 insertions(+), 19 deletions(-) (limited to 'src/test/java/org/openslx') diff --git a/src/test/java/org/openslx/vm/QemuMetaDataTest.java b/src/test/java/org/openslx/vm/QemuMetaDataTest.java index a3053a1..869d0f5 100644 --- a/src/test/java/org/openslx/vm/QemuMetaDataTest.java +++ b/src/test/java/org/openslx/vm/QemuMetaDataTest.java @@ -20,7 +20,6 @@ import org.apache.log4j.LogManager; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.function.Executable; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; @@ -73,7 +72,7 @@ public class QemuMetaDataTest assertEquals( "archlinux", displayName ); - assertDoesNotThrow​( () -> vmLibvirtDomainConfig.validateXml() ); + assertDoesNotThrow( () -> vmLibvirtDomainConfig.validateXml() ); } @Test @@ -91,7 +90,7 @@ public class QemuMetaDataTest assertEquals( false, isVmSnapshot ); - assertDoesNotThrow​( () -> vmLibvirtDomainConfig.validateXml() ); + assertDoesNotThrow( () -> vmLibvirtDomainConfig.validateXml() ); } @Test @@ -112,7 +111,7 @@ public class QemuMetaDataTest assertEquals( true, supportedImageFormats .containsAll( Arrays.asList( ImageFormat.QCOW2, ImageFormat.VMDK, ImageFormat.VDI ) ) ); - assertDoesNotThrow​( () -> vmLibvirtDomainConfig.validateXml() ); + assertDoesNotThrow( () -> vmLibvirtDomainConfig.validateXml() ); } @Test @@ -132,7 +131,7 @@ public class QemuMetaDataTest assertEquals( 1, hdds.size() ); assertEquals( "/var/lib/libvirt/images/archlinux.qcow2", hdds.get( 0 ).diskImage ); - assertDoesNotThrow​( () -> vmLibvirtDomainConfig.validateXml() ); + assertDoesNotThrow( () -> vmLibvirtDomainConfig.validateXml() ); } @Test @@ -158,7 +157,7 @@ public class QemuMetaDataTest assertEquals( lengthOriginalXmlConfig, lengthUnfilteredXmlConfig + numberOfDeletedElements ); - assertDoesNotThrow​( () -> vmLibvirtDomainConfig.validateXml() ); + assertDoesNotThrow( () -> vmLibvirtDomainConfig.validateXml() ); } @Test @@ -184,7 +183,7 @@ public class QemuMetaDataTest assertEquals( lengthOriginalXmlConfig, lengthFilteredXmlConfig + numberOfDeletedElements ); - assertDoesNotThrow​( () -> vmLibvirtDomainConfig.validateXml() ); + assertDoesNotThrow( () -> vmLibvirtDomainConfig.validateXml() ); } @ParameterizedTest @@ -225,7 +224,7 @@ public class QemuMetaDataTest DiskStorage addedStorageDevice = vmLibvirtDomainConfig.getDiskStorageDevices().get( 0 ); assertEquals( diskFile.getAbsolutePath(), addedStorageDevice.getStorageSource() ); - assertDoesNotThrow​( () -> vmLibvirtDomainConfig.validateXml() ); + assertDoesNotThrow( () -> vmLibvirtDomainConfig.validateXml() ); } @ParameterizedTest @@ -253,7 +252,7 @@ public class QemuMetaDataTest DiskCdrom addedCdromDevice = vmLibvirtDomainConfig.getDiskCdromDevices().get( 0 ); assertEquals( diskFile.getAbsolutePath(), addedCdromDevice.getStorageSource() ); - assertDoesNotThrow​( () -> vmLibvirtDomainConfig.validateXml() ); + assertDoesNotThrow( () -> vmLibvirtDomainConfig.validateXml() ); } @ParameterizedTest @@ -280,7 +279,7 @@ public class QemuMetaDataTest DiskCdrom addedCdromDevice = vmLibvirtDomainConfig.getDiskCdromDevices().get( 0 ); assertEquals( QemuMetaData.CDROM_DEFAULT_PHYSICAL_DRIVE, addedCdromDevice.getStorageSource() ); - assertDoesNotThrow​( () -> vmLibvirtDomainConfig.validateXml() ); + assertDoesNotThrow( () -> vmLibvirtDomainConfig.validateXml() ); } @ParameterizedTest @@ -309,7 +308,7 @@ public class QemuMetaDataTest assertTrue( addedFloppyDevice.isReadOnly() ); assertEquals( diskFile.getAbsolutePath(), addedFloppyDevice.getStorageSource() ); - assertDoesNotThrow​( () -> vmLibvirtDomainConfig.validateXml() ); + assertDoesNotThrow( () -> vmLibvirtDomainConfig.validateXml() ); } @ParameterizedTest @@ -328,7 +327,7 @@ public class QemuMetaDataTest assertEquals( coreCount, vmLibvirtDomainConfig.getVCpu() ); - assertDoesNotThrow​( () -> vmLibvirtDomainConfig.validateXml() ); + assertDoesNotThrow( () -> vmLibvirtDomainConfig.validateXml() ); } @ParameterizedTest @@ -351,7 +350,7 @@ public class QemuMetaDataTest assertEquals( SoundCardType.HD_AUDIO, soundCardType ); } - assertDoesNotThrow​( () -> vmLibvirtDomainConfig.validateXml() ); + assertDoesNotThrow( () -> vmLibvirtDomainConfig.validateXml() ); } @ParameterizedTest @@ -378,7 +377,7 @@ public class QemuMetaDataTest Sound addedSoundDevice = vmLibvirtDomainConfig.getSoundDevices().get( 0 ); assertEquals( Sound.Model.SB16, addedSoundDevice.getModel() ); - assertDoesNotThrow​( () -> vmLibvirtDomainConfig.validateXml() ); + assertDoesNotThrow( () -> vmLibvirtDomainConfig.validateXml() ); } @ParameterizedTest @@ -401,7 +400,7 @@ public class QemuMetaDataTest assertEquals( EthernetDevType.PARAVIRT, ethernetDeviceType ); } - assertDoesNotThrow​( () -> vmLibvirtDomainConfig.validateXml() ); + assertDoesNotThrow( () -> vmLibvirtDomainConfig.validateXml() ); } @ParameterizedTest @@ -423,7 +422,7 @@ public class QemuMetaDataTest assertEquals( Interface.Model.E1000E, addedEthernetDevice.getModel() ); } - assertDoesNotThrow​( () -> vmLibvirtDomainConfig.validateXml() ); + assertDoesNotThrow( () -> vmLibvirtDomainConfig.validateXml() ); } @ParameterizedTest @@ -446,7 +445,7 @@ public class QemuMetaDataTest assertEquals( UsbSpeed.USB3_0, maxUsbSpeed ); } - assertDoesNotThrow​( () -> vmLibvirtDomainConfig.validateXml() ); + assertDoesNotThrow( () -> vmLibvirtDomainConfig.validateXml() ); } @ParameterizedTest @@ -473,7 +472,7 @@ public class QemuMetaDataTest ControllerUsb addedUsbControllerDevice = vmLibvirtDomainConfig.getUsbControllerDevices().get( 0 ); assertEquals( ControllerUsb.Model.ICH9_EHCI1, addedUsbControllerDevice.getModel() ); - assertDoesNotThrow​( () -> vmLibvirtDomainConfig.validateXml() ); + assertDoesNotThrow( () -> vmLibvirtDomainConfig.validateXml() ); } static Stream configAndEthernetTypeProvider() @@ -527,6 +526,6 @@ public class QemuMetaDataTest break; } - assertDoesNotThrow​( () -> vmLibvirtDomainConfig.validateXml() ); + assertDoesNotThrow( () -> vmLibvirtDomainConfig.validateXml() ); } } -- cgit v1.2.3-55-g7522 From f2b0667b55afbc51bc8de4652e7ce4403fbb9bed Mon Sep 17 00:00:00 2001 From: Manuel Bentele Date: Mon, 12 Apr 2021 11:43:29 +0200 Subject: Adjusts network bridge names for Libvirt networking --- src/main/java/org/openslx/vm/QemuMetaData.java | 32 +++++++++++----------- src/test/java/org/openslx/vm/QemuMetaDataTest.java | 10 +++---- 2 files changed, 21 insertions(+), 21 deletions(-) (limited to 'src/test/java/org/openslx') diff --git a/src/main/java/org/openslx/vm/QemuMetaData.java b/src/main/java/org/openslx/vm/QemuMetaData.java index 3209925..e758d64 100644 --- a/src/main/java/org/openslx/vm/QemuMetaData.java +++ b/src/main/java/org/openslx/vm/QemuMetaData.java @@ -227,19 +227,19 @@ public class QemuMetaData extends VmMetaData { /** - * Default bridge name of the network bridge connected to the LAN. + * Name of the network bridge for the LAN. */ - public static final String NETWORK_DEFAULT_BRIDGE = "brBwLehrpool"; + public static final String NETWORK_BRIDGE_LAN_DEFAULT = "br0"; /** - * Default network name of the isolated host network (host only). + * Name of the network bridge for the default NAT network. */ - public static final String NETWORK_DEFAULT_HOST_ONLY = "host"; + public static final String NETWORK_BRIDGE_NAT_DEFAULT = "nat1"; /** - * Default network name of the NAT network. + * Name of the network for the isolated host network (host only). */ - public static final String NETWORK_DEFAULT_NAT = "nat"; + public static final String NETWORK_BRIDGE_HOST_ONLY_DEFAULT = "vsw2"; /** * Default physical CDROM drive of the hypervisor host. @@ -799,19 +799,19 @@ public class QemuMetaData extends // add network bridge interface device interfaceDevice = this.vmConfig.addInterfaceBridgeDevice(); interfaceDevice.setModel( defaultNetworkDeviceModel ); - interfaceDevice.setSource( QemuMetaData.NETWORK_DEFAULT_BRIDGE ); + interfaceDevice.setSource( QemuMetaData.NETWORK_BRIDGE_LAN_DEFAULT ); break; case HOST_ONLY: // add network interface device with link to the isolated host network - interfaceDevice = this.vmConfig.addInterfaceNetworkDevice(); + interfaceDevice = this.vmConfig.addInterfaceBridgeDevice(); interfaceDevice.setModel( defaultNetworkDeviceModel ); - interfaceDevice.setSource( QemuMetaData.NETWORK_DEFAULT_HOST_ONLY ); + interfaceDevice.setSource( QemuMetaData.NETWORK_BRIDGE_HOST_ONLY_DEFAULT ); break; case NAT: // add network interface device with link to the NAT network - interfaceDevice = this.vmConfig.addInterfaceNetworkDevice(); + interfaceDevice = this.vmConfig.addInterfaceBridgeDevice(); interfaceDevice.setModel( defaultNetworkDeviceModel ); - interfaceDevice.setSource( QemuMetaData.NETWORK_DEFAULT_NAT ); + interfaceDevice.setSource( QemuMetaData.NETWORK_BRIDGE_NAT_DEFAULT ); break; } } else { @@ -819,15 +819,15 @@ public class QemuMetaData extends switch ( type ) { case BRIDGED: interfaceDevice.setType( Interface.Type.BRIDGE ); - interfaceDevice.setSource( QemuMetaData.NETWORK_DEFAULT_BRIDGE ); + interfaceDevice.setSource( QemuMetaData.NETWORK_BRIDGE_LAN_DEFAULT ); break; case HOST_ONLY: - interfaceDevice.setType( Interface.Type.NETWORK ); - interfaceDevice.setSource( QemuMetaData.NETWORK_DEFAULT_HOST_ONLY ); + interfaceDevice.setType( Interface.Type.BRIDGE ); + interfaceDevice.setSource( QemuMetaData.NETWORK_BRIDGE_HOST_ONLY_DEFAULT ); break; case NAT: - interfaceDevice.setType( Interface.Type.NETWORK ); - interfaceDevice.setSource( QemuMetaData.NETWORK_DEFAULT_NAT ); + interfaceDevice.setType( Interface.Type.BRIDGE ); + interfaceDevice.setSource( QemuMetaData.NETWORK_BRIDGE_NAT_DEFAULT ); break; } } diff --git a/src/test/java/org/openslx/vm/QemuMetaDataTest.java b/src/test/java/org/openslx/vm/QemuMetaDataTest.java index 869d0f5..30607db 100644 --- a/src/test/java/org/openslx/vm/QemuMetaDataTest.java +++ b/src/test/java/org/openslx/vm/QemuMetaDataTest.java @@ -512,17 +512,17 @@ public class QemuMetaDataTest case BRIDGED: assertEquals( Interface.Type.BRIDGE, addedEthernetDevice.getType() ); assertEquals( Interface.Model.VIRTIO, addedEthernetDevice.getModel() ); - assertEquals( QemuMetaData.NETWORK_DEFAULT_BRIDGE, addedEthernetDevice.getSource() ); + assertEquals( QemuMetaData.NETWORK_BRIDGE_LAN_DEFAULT, addedEthernetDevice.getSource() ); break; case HOST_ONLY: - assertEquals( Interface.Type.NETWORK, addedEthernetDevice.getType() ); + assertEquals( Interface.Type.BRIDGE, addedEthernetDevice.getType() ); assertEquals( Interface.Model.VIRTIO, addedEthernetDevice.getModel() ); - assertEquals( QemuMetaData.NETWORK_DEFAULT_HOST_ONLY, addedEthernetDevice.getSource() ); + assertEquals( QemuMetaData.NETWORK_BRIDGE_HOST_ONLY_DEFAULT, addedEthernetDevice.getSource() ); break; case NAT: - assertEquals( Interface.Type.NETWORK, addedEthernetDevice.getType() ); + assertEquals( Interface.Type.BRIDGE, addedEthernetDevice.getType() ); assertEquals( Interface.Model.VIRTIO, addedEthernetDevice.getModel() ); - assertEquals( QemuMetaData.NETWORK_DEFAULT_NAT, addedEthernetDevice.getSource() ); + assertEquals( QemuMetaData.NETWORK_BRIDGE_NAT_DEFAULT, addedEthernetDevice.getSource() ); break; } -- cgit v1.2.3-55-g7522 From 07661b83665c2db556d26dcc11c9ad380878b0ff Mon Sep 17 00:00:00 2001 From: Manuel Bentele Date: Thu, 15 Apr 2021 15:05:15 +0200 Subject: Move VmMetaData classes to new virtualization config directories --- src/main/java/org/openslx/util/ThriftUtil.java | 2 +- .../UnsupportedVirtualizerFormatException.java | 13 + .../virtualization/configuration/VmMetaData.java | 429 ++++++++++ .../container/DockerMetaDataDummy.java | 221 ++++++ .../configuration/machine/KeyValuePair.java | 13 + .../configuration/machine/QemuMetaData.java | 883 +++++++++++++++++++++ .../configuration/machine/QemuMetaDataUtils.java | 191 +++++ .../configuration/machine/VboxConfig.java | 632 +++++++++++++++ .../configuration/machine/VboxMetaData.java | 537 +++++++++++++ .../configuration/machine/VmwareConfig.java | 277 +++++++ .../configuration/machine/VmwareMetaData.java | 684 ++++++++++++++++ .../java/org/openslx/vm/DockerMetaDataDummy.java | 219 ----- src/main/java/org/openslx/vm/KeyValuePair.java | 13 - src/main/java/org/openslx/vm/QemuMetaData.java | 881 -------------------- .../java/org/openslx/vm/QemuMetaDataUtils.java | 191 ----- .../vm/UnsupportedVirtualizerFormatException.java | 13 - src/main/java/org/openslx/vm/VboxConfig.java | 631 --------------- src/main/java/org/openslx/vm/VboxMetaData.java | 535 ------------- src/main/java/org/openslx/vm/VmMetaData.java | 425 ---------- src/main/java/org/openslx/vm/VmwareConfig.java | 276 ------- src/main/java/org/openslx/vm/VmwareMetaData.java | 682 ---------------- .../java/org/openslx/vm/disk/DiskImageVmdk.java | 4 +- .../configuration/machine/QemuMetaDataTest.java | 533 +++++++++++++ src/test/java/org/openslx/vm/QemuMetaDataTest.java | 531 ------------- 24 files changed, 4416 insertions(+), 4400 deletions(-) create mode 100644 src/main/java/org/openslx/virtualization/configuration/UnsupportedVirtualizerFormatException.java create mode 100644 src/main/java/org/openslx/virtualization/configuration/VmMetaData.java create mode 100644 src/main/java/org/openslx/virtualization/configuration/container/DockerMetaDataDummy.java create mode 100644 src/main/java/org/openslx/virtualization/configuration/machine/KeyValuePair.java create mode 100644 src/main/java/org/openslx/virtualization/configuration/machine/QemuMetaData.java create mode 100644 src/main/java/org/openslx/virtualization/configuration/machine/QemuMetaDataUtils.java create mode 100644 src/main/java/org/openslx/virtualization/configuration/machine/VboxConfig.java create mode 100644 src/main/java/org/openslx/virtualization/configuration/machine/VboxMetaData.java create mode 100644 src/main/java/org/openslx/virtualization/configuration/machine/VmwareConfig.java create mode 100644 src/main/java/org/openslx/virtualization/configuration/machine/VmwareMetaData.java delete mode 100644 src/main/java/org/openslx/vm/DockerMetaDataDummy.java delete mode 100644 src/main/java/org/openslx/vm/KeyValuePair.java delete mode 100644 src/main/java/org/openslx/vm/QemuMetaData.java delete mode 100644 src/main/java/org/openslx/vm/QemuMetaDataUtils.java delete mode 100644 src/main/java/org/openslx/vm/UnsupportedVirtualizerFormatException.java delete mode 100644 src/main/java/org/openslx/vm/VboxConfig.java delete mode 100644 src/main/java/org/openslx/vm/VboxMetaData.java delete mode 100644 src/main/java/org/openslx/vm/VmMetaData.java delete mode 100644 src/main/java/org/openslx/vm/VmwareConfig.java delete mode 100644 src/main/java/org/openslx/vm/VmwareMetaData.java create mode 100644 src/test/java/org/openslx/virtualization/configuration/machine/QemuMetaDataTest.java delete mode 100644 src/test/java/org/openslx/vm/QemuMetaDataTest.java (limited to 'src/test/java/org/openslx') diff --git a/src/main/java/org/openslx/util/ThriftUtil.java b/src/main/java/org/openslx/util/ThriftUtil.java index 3c2c9ea..2cfc770 100644 --- a/src/main/java/org/openslx/util/ThriftUtil.java +++ b/src/main/java/org/openslx/util/ThriftUtil.java @@ -7,7 +7,7 @@ import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.List; -import org.openslx.vm.VmwareConfig; +import org.openslx.virtualization.configuration.machine.VmwareConfig; public class ThriftUtil { diff --git a/src/main/java/org/openslx/virtualization/configuration/UnsupportedVirtualizerFormatException.java b/src/main/java/org/openslx/virtualization/configuration/UnsupportedVirtualizerFormatException.java new file mode 100644 index 0000000..3040669 --- /dev/null +++ b/src/main/java/org/openslx/virtualization/configuration/UnsupportedVirtualizerFormatException.java @@ -0,0 +1,13 @@ +package org.openslx.virtualization.configuration; + +public class UnsupportedVirtualizerFormatException extends Exception +{ + /** + * Version for serialization. + */ + private static final long serialVersionUID = 5794121065945636839L; + + public UnsupportedVirtualizerFormatException(String message) { + super(message); + } +} \ No newline at end of file diff --git a/src/main/java/org/openslx/virtualization/configuration/VmMetaData.java b/src/main/java/org/openslx/virtualization/configuration/VmMetaData.java new file mode 100644 index 0000000..ae94fe0 --- /dev/null +++ b/src/main/java/org/openslx/virtualization/configuration/VmMetaData.java @@ -0,0 +1,429 @@ +package org.openslx.virtualization.configuration; + +import java.io.File; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + +import org.apache.log4j.Logger; +import org.openslx.bwlp.thrift.iface.OperatingSystem; +import org.openslx.bwlp.thrift.iface.Virtualizer; +import org.openslx.virtualization.configuration.container.DockerMetaDataDummy; +import org.openslx.virtualization.configuration.machine.QemuMetaData; +import org.openslx.virtualization.configuration.machine.VboxMetaData; +import org.openslx.virtualization.configuration.machine.VmwareMetaData; +import org.openslx.vm.disk.DiskImage; + +/** + * Describes a configured virtual machine. This class is parsed from a machine + * description, like a *.vmx for VMware machines. + */ +public abstract class VmMetaData +{ + private static final Logger LOGGER = Logger.getLogger( VmMetaData.class ); + + /* + * Helper types + */ + protected Map soundCards = new HashMap<>(); + protected Map ddacc = new HashMap<>(); + protected Map hwversion = new HashMap<>(); + protected Map networkCards = new HashMap<>(); + protected Map usbSpeeds = new HashMap<>(); + + /** + * Virtual sound cards types + */ + public static enum SoundCardType + { + NONE( "None" ), DEFAULT( "(default)" ), SOUND_BLASTER( "Sound Blaster 16" ), ES( "ES 1371" ), HD_AUDIO( "Intel Integrated HD Audio" ), AC( "Intel ICH Audio Codec 97" ); + + public final String displayName; + + private SoundCardType( String dName ) + { + this.displayName = dName; + } + } + + /** + * 3D acceleration types + */ + public static enum DDAcceleration + { + OFF( "Off" ), ON( "On" ); + + public final String displayName; + + private DDAcceleration( String dName ) + { + this.displayName = dName; + } + } + + /** + * Virtual hardware version - currently only in use for VMPlayer + */ + public static enum HWVersion + { + NONE( "(invalid)" ), + THREE( " 3 (Workstation 4/5, Player 1)" ), + FOUR( " 4 (Workstation 4/5, Player 1/2, Fusion 1)" ), + SIX( " 6 (Workstation 6)" ), + SEVEN( " 7 (Workstation 6.5/7, Player 3, Fusion 2/3)" ), + EIGHT( " 8 (Workstation 8, Player/Fusion 4)" ), + NINE( " 9 (Workstation 9, Player/Fusion 5)" ), + TEN( "10 (Workstation 10, Player/Fusion 6)" ), + ELEVEN( "11 (Workstation 11, Player/Fusion 7)" ), + TWELVE( "12 (Workstation/Player 12, Fusion 8)" ), + FOURTEEN( "14 (Workstation/Player 14, Fusion 10)"), + FIFTEEN( "15 (Workstation/Player 15, Fusion 11)"), + FIFTEEN_ONE( "16 (Workstation/Player 15.1, Fusion 11.1)"), + SIXTEEN( "17 (Workstation/Player 16, Fusion 12)"), + SIXTEEN_ONE( "18 (Workstation/Player 16.1, Fusion 12.1)"), + DEFAULT( "default" ); + + public final String displayName; + + private HWVersion( String dName ) + { + this.displayName = dName; + } + } + + /** + * Virtual network cards + */ + public static enum EthernetDevType + { + AUTO( "(default)" ), PCNET32( "AMD PCnet32" ), E1000( "Intel E1000 (PCI)" ), E1000E( "Intel E1000e (PCI-Express)" ), VMXNET( "VMXnet" ), VMXNET3( "VMXnet 3" ), PCNETPCI2( + "PCnet-PCI II" ), PCNETFAST3( "PCnet-FAST III" ), PRO1000MTD( "Intel PRO/1000 MT Desktop" ), PRO1000TS( + "Intel PRO/1000 T Server" ), PRO1000MTS( "Intel PRO/1000 MT Server" ), PARAVIRT( "Paravirtualized Network" ), NONE( "No Network Card" ); + + public final String displayName; + + private EthernetDevType( String dName ) + { + this.displayName = dName; + } + } + + public static enum UsbSpeed + { + NONE( "None" ), + USB1_1( "USB 1.1" ), + USB2_0( "USB 2.0" ), + USB3_0( "USB 3.0" ); + + public final String displayName; + + private UsbSpeed( String dName ) + { + this.displayName = dName; + } + } + + public static enum DriveBusType + { + SCSI, IDE, SATA, NVME; + } + + public static class HardDisk + { + public final String chipsetDriver; + public final DriveBusType bus; + public final String diskImage; + + public HardDisk( String chipsetDriver, DriveBusType bus, String diskImage ) + { + this.chipsetDriver = chipsetDriver; + this.bus = bus; + this.diskImage = diskImage; + } + } + + public static enum EtherType + { + NAT, BRIDGED, HOST_ONLY; + } + /* + * Members + */ + + protected final List hdds = new ArrayList(); + + private final List osList; + + private OperatingSystem os = null; + + protected String displayName = null; + + protected boolean isMachineSnapshot; + + /* + * Getters for virtual hardware + */ + public List getSupportedSoundCards() + { + ArrayList availables = new ArrayList( soundCards.keySet() ); + Collections.sort( availables ); + return availables; + } + + public List getSupportedDDAccs() + { + ArrayList availables = new ArrayList( ddacc.keySet() ); + Collections.sort( availables ); + return availables; + } + + public List getSupportedHWVersions() + { + ArrayList availables = new ArrayList( hwversion.keySet() ); + Collections.sort( availables ); + return availables; + } + + public List getSupportedEthernetDevices() + { + ArrayList availables = new ArrayList( networkCards.keySet() ); + Collections.sort( availables ); + return availables; + } + + public List getSupportedUsbSpeeds() + { + ArrayList availables = new ArrayList<>( usbSpeeds.keySet() ); + Collections.sort( availables ); + return availables; + } + + /** + * Get operating system of this VM. + */ + public OperatingSystem getOs() + { + return os; + } + + /** + * Get all hard disks of this VM. + */ + public List getHdds() + { + return Collections.unmodifiableList( hdds ); + } + + /** + * Get display name of VM. + */ + public String getDisplayName() + { + return displayName; + } + + /* + * Getter for isMachineSnapshot + */ + public boolean isMachineSnapshot() + { + return isMachineSnapshot; + } + + /** + * This method should return a minimal representation of the input meta data. + * The representation is platform dependent, and should be stripped of all + * non-essential configuration, such as CD/DVD/FLoppy drives, serial or parallel + * ports, shared folders, or anything else that could be considered sensible + * information (absolute paths containing the local user's name). + */ + public abstract byte[] getFilteredDefinitionArray(); + + public final ByteBuffer getFilteredDefinition() + { + return ByteBuffer.wrap( getFilteredDefinitionArray() ); + } + + /* + * Methods + */ + + public VmMetaData( List osList ) + { + this.osList = osList; + + // register virtual hardware models for graphical editing of virtual devices (GPU, sound, USB, ...) + this.registerVirtualHW(); + } + + /** + * Called from subclass to set the OS. If the OS cannot be determined from the + * given parameters, it will not be set. + * + * @param virtId + * virtualizer, eg "vmware" for VMware + * @param virtOsId + * the os identifier used by the virtualizer, eg. windows7-64 for + * 64bit Windows 7 on VMware + */ + protected final void setOs( String virtId, String virtOsId ) + { + OperatingSystem lazyMatch = null; + for ( OperatingSystem os : osList ) { + if ( os.getVirtualizerOsId() == null ) + continue; + for ( Entry entry : os.getVirtualizerOsId().entrySet() ) { + if ( !entry.getValue().equals( virtOsId ) ) + continue; + if ( entry.getKey().equals( virtId ) ) { + this.os = os; + return; + } else { + lazyMatch = os; + } + } + } + this.os = lazyMatch; + } + + /** + * Returns list of image formats supported by the VM's hypervisor. + * + * @return list of image formats. + */ + public abstract List getSupportedImageFormats(); + + /** + * Apply config options that are desired when locally editing a VM. for vmware, + * this disables automatic DPI scaling of the guest. + */ + public abstract void applySettingsForLocalEdit(); + + /** + * Returns a VmMetaData instance of the given machine description given as file + * + * @param osList List of supported operating systems + * @param file VM's machine description file to get the metadata instance from + * @return VmMetaData object representing the relevant parts of the given machine description + */ + public static VmMetaData getInstance( List osList, File file ) + throws IOException + { + try { + return new VmwareMetaData( osList, file ); + } catch ( UnsupportedVirtualizerFormatException e ) { + LOGGER.info( "Not a VMware file", e ); + } + try { + return new VboxMetaData( osList, file ); + } catch ( UnsupportedVirtualizerFormatException e ) { + LOGGER.info( "Not a VirtualBox file", e ); + } + try { + return new QemuMetaData( osList, file ); + } catch ( UnsupportedVirtualizerFormatException e ) { + LOGGER.info( "Not a Libvirt file", e ); + } + try { + return new DockerMetaDataDummy(osList, file); + } catch ( Exception e ) { + LOGGER.info( "Not a tar.gz file, for docker container", e ); + } + + LOGGER.error( "Could not detect any known virtualizer format" ); + return null; + } + + /** + * Returns a VmMetaData instance of the given machine description given as a byte array + * + * @param osList List of supported operating systems + * @param vmContent VM's machine description as byte array (e.g. stored in DB) + * @param length length of the byte array given as vmContent + * @return VmMetaData object representing the relevant parts of the given machine description + * @throws IOException + */ + public static VmMetaData getInstance( List osList, byte[] vmContent, int length ) + throws IOException + { + try { + return new VmwareMetaData( osList, vmContent, length ); + } catch ( UnsupportedVirtualizerFormatException e ) { + LOGGER.info( "Not a VMware file", e ); + } + try { + return new VboxMetaData( osList, vmContent, length ); + } catch ( UnsupportedVirtualizerFormatException e ) { + LOGGER.info( "Not a VirtualBox file", e ); + } + try { + return new QemuMetaData( osList, vmContent, length ); + } catch ( UnsupportedVirtualizerFormatException e ) { + LOGGER.info( "Not a Libvirt file", e ); + } + try { + return new DockerMetaDataDummy( osList, vmContent, length ); + } catch ( UnsupportedVirtualizerFormatException e ) { + LOGGER.info( "Not a tar.gz file, for docker container", e ); + } + + LOGGER.error( "Could not detect any known virtualizer format" ); + return null; + } + + public abstract boolean addHddTemplate( File diskImage, String hddMode, String redoDir ); + + public abstract boolean addHddTemplate( String diskImagePath, String hddMode, String redoDir ); + + public abstract boolean addDefaultNat(); + + public abstract void setOs( String vendorOsId ); + + public abstract boolean addDisplayName( String name ); + + public abstract boolean addRam( int mem ); + + public abstract void addFloppy( int index, String image, boolean readOnly ); + + public abstract boolean addCdrom( String image ); + + public abstract boolean addCpuCoreCount( int nrOfCores ); + + public abstract void setSoundCard( SoundCardType type ); + + public abstract SoundCardType getSoundCard(); + + public abstract void setDDAcceleration( DDAcceleration type ); + + public abstract DDAcceleration getDDAcceleration(); + + public abstract void setHWVersion( HWVersion type ); + + public abstract HWVersion getHWVersion(); + + public abstract void setEthernetDevType( int cardIndex, EthernetDevType type ); + + public abstract EthernetDevType getEthernetDevType( int cardIndex ); + + public abstract void setMaxUsbSpeed( UsbSpeed speed ); + + public abstract UsbSpeed getMaxUsbSpeed(); + + public abstract byte[] getDefinitionArray(); + + public abstract boolean addEthernet( EtherType type ); + + public abstract Virtualizer getVirtualizer(); + + public abstract boolean tweakForNonPersistent(); + + /** + * Function used to register virtual devices + */ + public abstract void registerVirtualHW(); +} diff --git a/src/main/java/org/openslx/virtualization/configuration/container/DockerMetaDataDummy.java b/src/main/java/org/openslx/virtualization/configuration/container/DockerMetaDataDummy.java new file mode 100644 index 0000000..321953e --- /dev/null +++ b/src/main/java/org/openslx/virtualization/configuration/container/DockerMetaDataDummy.java @@ -0,0 +1,221 @@ +package org.openslx.virtualization.configuration.container; + +import org.apache.log4j.Logger; +import org.openslx.bwlp.thrift.iface.OperatingSystem; +import org.openslx.bwlp.thrift.iface.Virtualizer; +import org.openslx.thrifthelper.TConst; +import org.openslx.virtualization.configuration.UnsupportedVirtualizerFormatException; +import org.openslx.virtualization.configuration.VmMetaData; +import org.openslx.vm.disk.DiskImage; +import org.openslx.vm.disk.DiskImage.ImageFormat; + +import java.io.BufferedInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +class DockerSoundCardMeta +{ +} + +class DockerDDAccelMeta +{ +} + +class DockerHWVersionMeta +{ +} + +class DockerEthernetDevTypeMeta +{ +} + +class DockerUsbSpeedMeta +{ +} + +public class DockerMetaDataDummy extends VmMetaData { + + /** + * List of supported image formats by the Docker hypervisor. + */ + private static final List SUPPORTED_IMAGE_FORMATS = Collections.unmodifiableList( + Arrays.asList( ImageFormat.NONE ) ); + + private static final Logger LOGGER = Logger.getLogger( DockerMetaDataDummy.class); + + private final Virtualizer virtualizer = new Virtualizer(TConst.VIRT_DOCKER, "Docker"); + + /** + * containerDefinition is a serialized tar.gz archive and represents a + * ContainerDefinition. This archive contains a serialized Container Recipe (e.g. Dockerfile) + * and a ContainerMeta witch is serialized as a json file. + *

+ * See ContainerDefintion in tutor-module (bwsuite). + *

+ * This field is in vm context the machine description e.g. vmware = vmx. + * This field will be stored in table imageversion.virtualizerconfig + */ + private byte[] containerDefinition; + + public DockerMetaDataDummy(List osList, File file) throws UnsupportedVirtualizerFormatException { + super(osList); + + BufferedInputStream bis = null; + + try { + bis = new BufferedInputStream(new FileInputStream(file)); + containerDefinition = new byte[(int) file.length()]; + bis.read(containerDefinition); + + checkIsTarGz(); + } catch (IOException | UnsupportedVirtualizerFormatException e) { + LOGGER.error("Couldn't read dockerfile", e); + } finally { + try { + bis.close(); + } catch ( IOException e ) { + LOGGER.warn( "Could not close the input stream!" ); + } + } + } + + public DockerMetaDataDummy(List osList, byte[] vmContent, int length) + throws UnsupportedVirtualizerFormatException { + super(osList); + + containerDefinition = vmContent; + + checkIsTarGz(); + } + + /* + TODO This is just a simple check to prevent the workflow from considering any content as acceptable. + */ + /** + * Checks if the first two bytes of the content identifies a tar.gz archive. + * The first byte is 31 == 0x1f, the second byte has to be -117 == 0x8b. + * + * @throws UnsupportedVirtualizerFormatException + */ + private void checkIsTarGz() throws UnsupportedVirtualizerFormatException { + if (!((31 == containerDefinition[0]) && (-117 == containerDefinition[1]))) { + LOGGER.warn("Not Supported Content."); + throw new UnsupportedVirtualizerFormatException( + "DockerMetaDataDummy: Not tar.gz encoded content!"); + } + } + + @Override public byte[] getFilteredDefinitionArray() { + return containerDefinition; + } + + @Override + public List getSupportedImageFormats() + { + return DockerMetaDataDummy.SUPPORTED_IMAGE_FORMATS; + } + + @Override public void applySettingsForLocalEdit() { + + } + + @Override public boolean addHddTemplate(File diskImage, String hddMode, String redoDir) { + return false; + } + + @Override public boolean addHddTemplate(String diskImagePath, String hddMode, String redoDir) { + return false; + } + + @Override public boolean addDefaultNat() { + return false; + } + + @Override public void setOs(String vendorOsId) { + + } + + @Override public boolean addDisplayName(String name) { + return false; + } + + @Override public boolean addRam(int mem) { + return false; + } + + @Override public void addFloppy(int index, String image, boolean readOnly) { + + } + + @Override public boolean addCdrom(String image) { + return false; + } + + @Override public boolean addCpuCoreCount(int nrOfCores) { + return false; + } + + @Override public void setSoundCard(SoundCardType type) { + + } + + @Override public SoundCardType getSoundCard() { + return SoundCardType.NONE; + } + + @Override public void setDDAcceleration(DDAcceleration type) { + + } + + @Override public DDAcceleration getDDAcceleration() { + return DDAcceleration.OFF; + } + + @Override public void setHWVersion(HWVersion type) { + + } + + @Override public HWVersion getHWVersion() { + return HWVersion.DEFAULT; + } + + @Override public void setEthernetDevType(int cardIndex, EthernetDevType type) { + + } + + @Override public EthernetDevType getEthernetDevType(int cardIndex) { + return EthernetDevType.NONE; + } + + @Override public void setMaxUsbSpeed(UsbSpeed speed) { + + } + + @Override public UsbSpeed getMaxUsbSpeed() { + return UsbSpeed.NONE; + } + + @Override public byte[] getDefinitionArray() { + return new byte[0]; + } + + @Override public boolean addEthernet(EtherType type) { + return false; + } + + @Override public Virtualizer getVirtualizer() { + return virtualizer; + } + + @Override public boolean tweakForNonPersistent() { + return false; + } + + @Override public void registerVirtualHW() { + + } +} diff --git a/src/main/java/org/openslx/virtualization/configuration/machine/KeyValuePair.java b/src/main/java/org/openslx/virtualization/configuration/machine/KeyValuePair.java new file mode 100644 index 0000000..ecd4f2a --- /dev/null +++ b/src/main/java/org/openslx/virtualization/configuration/machine/KeyValuePair.java @@ -0,0 +1,13 @@ +package org.openslx.virtualization.configuration.machine; + +class KeyValuePair +{ + public final String key; + public final String value; + + public KeyValuePair( String key, String value ) + { + this.key = key; + this.value = value; + } +} \ No newline at end of file diff --git a/src/main/java/org/openslx/virtualization/configuration/machine/QemuMetaData.java b/src/main/java/org/openslx/virtualization/configuration/machine/QemuMetaData.java new file mode 100644 index 0000000..5dd86be --- /dev/null +++ b/src/main/java/org/openslx/virtualization/configuration/machine/QemuMetaData.java @@ -0,0 +1,883 @@ +package org.openslx.virtualization.configuration.machine; + +import java.io.File; +import java.math.BigInteger; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Map.Entry; + +import org.openslx.bwlp.thrift.iface.OperatingSystem; +import org.openslx.bwlp.thrift.iface.Virtualizer; +import org.openslx.libvirt.domain.Domain; +import org.openslx.libvirt.domain.device.ControllerUsb; +import org.openslx.libvirt.domain.device.Disk.BusType; +import org.openslx.libvirt.domain.device.Disk.StorageType; +import org.openslx.libvirt.domain.device.DiskCdrom; +import org.openslx.libvirt.domain.device.DiskFloppy; +import org.openslx.libvirt.domain.device.DiskStorage; +import org.openslx.libvirt.domain.device.Graphics; +import org.openslx.libvirt.domain.device.GraphicsSpice; +import org.openslx.libvirt.domain.device.Interface; +import org.openslx.libvirt.domain.device.Sound; +import org.openslx.libvirt.domain.device.Video; +import org.openslx.libvirt.xml.LibvirtXmlDocumentException; +import org.openslx.libvirt.xml.LibvirtXmlSerializationException; +import org.openslx.libvirt.xml.LibvirtXmlValidationException; +import org.openslx.thrifthelper.TConst; +import org.openslx.virtualization.configuration.UnsupportedVirtualizerFormatException; +import org.openslx.virtualization.configuration.VmMetaData; +import org.openslx.vm.disk.DiskImage; +import org.openslx.vm.disk.DiskImage.ImageFormat; + +/** + * Metadata to describe the hardware type of a QEMU sound card. + * + * @author Manuel Bentele + * @version 1.0 + */ +class QemuSoundCardMeta +{ + /** + * Stores the hardware model of the QEMU sound card. + */ + private final Sound.Model model; + + /** + * Creates metadata to describe the hardware model of a QEMU sound card. + * + * @param model hardware model of the QEMU sound card. + */ + public QemuSoundCardMeta( Sound.Model model ) + { + this.model = model; + } + + /** + * Returns hardware model of the QEMU sound card. + * + * @return hardware model of the QEMU sound card. + */ + public Sound.Model getModel() + { + return this.model; + } +} + +/** + * Metadata to describe the hardware acceleration state of QEMU virtual graphics. + * + * @author Manuel Bentele + * @version 1.0 + */ +class QemuDDAccelMeta +{ + /** + * Stores state of the hardware acceleration for QEMU virtual graphics. + */ + private final boolean enabled; + + /** + * Creates metadata to describe the hardware acceleration state of QEMU virtual graphics. + * + * @param enabled state of the hardware acceleration for QEMU virtual graphics. + */ + public QemuDDAccelMeta( boolean enabled ) + { + this.enabled = enabled; + } + + /** + * Returns state of the hardware acceleration of QEMU virtual graphics. + * + * @return state of the hardware acceleration for QEMU virtual graphics. + */ + public boolean isEnabled() + { + return this.enabled; + } +} + +/** + * Metadata to describe the version of a QEMU virtual machine configuration. + * + * @author Manuel Bentele + * @version 1.0 + */ +class QemuHWVersionMeta +{ + /** + * Stores the version of a QEMU virtual machine configuration. + */ + private final int version; + + /** + * Creates metadata to describe the version of a QEMU virtual machine configuration. + * + * @param version version of the QEMU virtual machine configuration. + */ + public QemuHWVersionMeta( int version ) + { + this.version = version; + } + + /** + * Returns version of the QEMU virtual machine configuration. + * + * @return version of the QEMU virtual machine configuration. + */ + public int getVersion() + { + return this.version; + } +} + +/** + * Metadata to describe the hardware type of a QEMU ethernet device. + * + * @author Manuel Bentele + * @version 1.0 + */ +class QemuEthernetDevTypeMeta +{ + /** + * Stores the hardware model of the QEMU ethernet device. + */ + private final Interface.Model model; + + /** + * Creates metadata to describe the hardware type of a QEMU ethernet device. + * + * @param model hardware type of the QEMU ethernet device. + */ + public QemuEthernetDevTypeMeta( Interface.Model model ) + { + this.model = model; + } + + /** + * Returns the hardware type of a QEMU ethernet device. + * + * @return hardware type of the QEMU ethernet device. + */ + public Interface.Model getModel() + { + return this.model; + } +} + +/** + * Metadata to describe a QEMU USB controller. + * + * @author Manuel Bentele + * @version 1.0 + */ +class QemuUsbSpeedMeta +{ + /** + * Stores the USB speed of the QEMU USB controller. + */ + private final int speed; + + /** + * Stores the QEMU hardware model of the USB controller. + */ + private final ControllerUsb.Model model; + + /** + * Creates metadata to describe a QEMU USB controller. + * + * @param speed USB speed of the QEMU USB controller. + * @param model QEMU hardware model of the USB controller. + */ + public QemuUsbSpeedMeta( int speed, ControllerUsb.Model model ) + { + this.speed = speed; + this.model = model; + } + + /** + * Returns the speed of the QEMU USB controller. + * + * @return speed of the QEMU USB controller. + */ + public int getSpeed() + { + return this.speed; + } + + /** + * Returns QEMU hardware model of the USB controller. + * + * @return hardware model of the QEMU USB controller. + */ + public ControllerUsb.Model getModel() + { + return this.model; + } +} + +/** + * Virtual machine configuration (managed by Libvirt) for the QEMU hypervisor. + * + * @author Manuel Bentele + * @version 1.0 + */ +public class QemuMetaData extends + VmMetaData +{ + /** + * Name of the network bridge for the LAN. + */ + public static final String NETWORK_BRIDGE_LAN_DEFAULT = "br0"; + + /** + * Name of the network bridge for the default NAT network. + */ + public static final String NETWORK_BRIDGE_NAT_DEFAULT = "nat1"; + + /** + * Name of the network for the isolated host network (host only). + */ + public static final String NETWORK_BRIDGE_HOST_ONLY_DEFAULT = "vsw2"; + + /** + * Default physical CDROM drive of the hypervisor host. + */ + public static final String CDROM_DEFAULT_PHYSICAL_DRIVE = "/dev/sr0"; + + /** + * List of supported image formats by the QEMU hypervisor. + */ + private static final List SUPPORTED_IMAGE_FORMATS = Collections.unmodifiableList( + Arrays.asList( ImageFormat.QCOW2, ImageFormat.VMDK, ImageFormat.VDI ) ); + + /** + * Representation of a QEMU hypervisor (managed by Libvirt). + */ + private static final Virtualizer VIRTUALIZER = new Virtualizer( TConst.VIRT_QEMU, "QEMU" ); + + /** + * Libvirt XML configuration file to modify configuration of virtual machine for QEMU. + */ + private Domain vmConfig = null; + + /** + * Stores current index of added HDD device to the Libvirt XML configuration file. + */ + private int vmDeviceIndexHddAdd = 0; + + /** + * Stores current index of added CDROM device to the Libvirt XML configuration file. + */ + private int vmDeviceIndexCdromAdd = 0; + + /** + * Stores current index of added ethernet device to the Libvirt XML configuration file. + */ + private int vmDeviceIndexEthernetAdd = 0; + + /** + * Creates new virtual machine configuration (managed by Libvirt) for the QEMU hypervisor. + * + * @param osList list of operating systems. + * @param file image file for the QEMU hypervisor. + * + * @throws UnsupportedVirtualizerFormatException Libvirt XML configuration cannot be processed. + */ + public QemuMetaData( List osList, File file ) throws UnsupportedVirtualizerFormatException + { + super( osList ); + + try { + // read and parse Libvirt domain XML configuration document + this.vmConfig = new Domain( file ); + } catch ( LibvirtXmlDocumentException | LibvirtXmlSerializationException | LibvirtXmlValidationException e ) { + throw new UnsupportedVirtualizerFormatException( e.getLocalizedMessage() ); + } + + // parse VM config and initialize fields of QemuMetaData class + this.parseVmConfig(); + } + + /** + * Creates new virtual machine configuration (managed by Libvirt) for the QEMU hypervisor. + * + * @param osList list of operating systems. + * @param vmContent file content for the QEMU hypervisor. + * @param length number of bytes of the file content. + * + * @throws UnsupportedVirtualizerFormatException Libvirt XML configuration cannot be processed. + */ + public QemuMetaData( List osList, byte[] vmContent, int length ) + throws UnsupportedVirtualizerFormatException + { + super( osList ); + + try { + // read and parse Libvirt domain XML configuration document + this.vmConfig = new Domain( new String( vmContent ) ); + } catch ( LibvirtXmlDocumentException | LibvirtXmlSerializationException | LibvirtXmlValidationException e ) { + throw new UnsupportedVirtualizerFormatException( e.getLocalizedMessage() ); + } + + // parse VM config and initialize fields of QemuMetaData class + this.parseVmConfig(); + } + + /** + * Parses Libvirt domain XML configuration to initialize QEMU metadata. + */ + private void parseVmConfig() + { + // set display name of VM + this.displayName = vmConfig.getName(); + + // this property cannot be checked with the Libvirt domain XML configuration + // to check if machine is in a paused/suspended state, look in the QEMU qcow2 image for snapshots and machine states + this.isMachineSnapshot = false; + + // add HDDs, SSDs to QEMU metadata + for ( DiskStorage storageDiskDevice : this.vmConfig.getDiskStorageDevices() ) { + this.addHddMetaData( storageDiskDevice ); + } + + // start of privacy filters to filter out sensitive information like name of users in absolute paths, ... + // removes all referenced storage files of all specified CDROMs, Floppy drives and HDDs + this.vmConfig.removeDiskDevicesStorage(); + } + + /** + * Adds an existing and observed storage disk device to the HDD metadata. + * + * @param storageDiskDevice existing and observed storage disk that should be added to the + * metadata. + */ + private void addHddMetaData( DiskStorage storageDiskDevice ) + { + String hddChipsetModel = null; + DriveBusType hddChipsetBus = QemuMetaDataUtils.convertBusType( storageDiskDevice.getBusType() ); + String hddImagePath = storageDiskDevice.getStorageSource(); + + this.hdds.add( new HardDisk( hddChipsetModel, hddChipsetBus, hddImagePath ) ); + } + + @Override + public byte[] getFilteredDefinitionArray() + { + // removes all specified boot order entries + this.vmConfig.removeBootOrder(); + + // removes all source networks of all specified network interfaces + this.vmConfig.removeInterfaceDevicesSource(); + + // output filtered Libvirt domain XML configuration + String configuration = this.vmConfig.toString(); + return configuration.getBytes( StandardCharsets.UTF_8 ); + } + + @Override + public List getSupportedImageFormats() + { + return QemuMetaData.SUPPORTED_IMAGE_FORMATS; + } + + @Override + public void applySettingsForLocalEdit() + { + // NOT implemented yet + } + + @Override + public boolean addHddTemplate( File diskImage, String hddMode, String redoDir ) + { + return this.addHddTemplate( diskImage.getAbsolutePath(), hddMode, redoDir ); + } + + @Override + public boolean addHddTemplate( String diskImagePath, String hddMode, String redoDir ) + { + return this.addHddTemplate( this.vmDeviceIndexHddAdd++, diskImagePath, hddMode, redoDir ); + } + + /** + * Adds hard disk drive (HDD) to the QEMU virtual machine configuration. + * + * @param index current index of HDD to be added to the virtual machine configuration. + * @param diskImagePath path to the virtual disk image for the HDD. + * @param hddMode operation mode of the HDD. + * @param redoDir directory for the redo log if an independent non-persistent + * hddMode is set. + * @return result state of adding the HDD. + */ + public boolean addHddTemplate( int index, String diskImagePath, String hddMode, String redoDir ) + { + ArrayList storageDiskDevices = this.vmConfig.getDiskStorageDevices(); + DiskStorage storageDiskDevice = QemuMetaDataUtils.getArrayIndex( storageDiskDevices, index ); + + if ( storageDiskDevice == null ) { + // HDD does not exist, so create new storage (HDD) device + storageDiskDevice = this.vmConfig.addDiskStorageDevice(); + storageDiskDevice.setReadOnly( false ); + storageDiskDevice.setBusType( BusType.VIRTIO ); + String targetDevName = QemuMetaDataUtils.createAlphabeticalDeviceName( "vd", index ); + storageDiskDevice.setTargetDevice( targetDevName ); + storageDiskDevice.setStorage( StorageType.FILE, diskImagePath ); + + // add new created HDD to the metadata of the QemuMetaData object, too + this.addHddMetaData( storageDiskDevice ); + } else { + // HDD exists, so update existing storage (HDD) device + storageDiskDevice.setStorage( StorageType.FILE, diskImagePath ); + } + + return false; + } + + @Override + public boolean addDefaultNat() + { + return this.addEthernet( EtherType.NAT ); + } + + @Override + public void setOs( String vendorOsId ) + { + this.setOs( vendorOsId ); + } + + @Override + public boolean addDisplayName( String name ) + { + this.vmConfig.setName( name ); + + final boolean statusName = this.vmConfig.getName().equals( name ); + + return statusName; + } + + @Override + public boolean addRam( int mem ) + { + BigInteger memory = BigInteger.valueOf( mem ); + + this.vmConfig.setMemory( memory ); + this.vmConfig.setCurrentMemory( memory ); + + final boolean isMemorySet = this.vmConfig.getMemory().toString().equals( memory.toString() ); + final boolean isCurrentMemorySet = this.vmConfig.getCurrentMemory().toString().equals( memory.toString() ); + + return isMemorySet && isCurrentMemorySet; + } + + @Override + public void addFloppy( int index, String image, boolean readOnly ) + { + ArrayList floppyDiskDevices = this.vmConfig.getDiskFloppyDevices(); + DiskFloppy floppyDiskDevice = QemuMetaDataUtils.getArrayIndex( floppyDiskDevices, index ); + + if ( floppyDiskDevice == null ) { + // floppy device does not exist, so create new floppy device + floppyDiskDevice = this.vmConfig.addDiskFloppyDevice(); + floppyDiskDevice.setBusType( BusType.FDC ); + String targetDevName = QemuMetaDataUtils.createAlphabeticalDeviceName( "fd", index ); + floppyDiskDevice.setTargetDevice( targetDevName ); + floppyDiskDevice.setReadOnly( readOnly ); + floppyDiskDevice.setStorage( StorageType.FILE, image ); + } else { + // floppy device exists, so update existing floppy device + floppyDiskDevice.setReadOnly( readOnly ); + floppyDiskDevice.setStorage( StorageType.FILE, image ); + } + } + + @Override + public boolean addCdrom( String image ) + { + return this.addCdrom( this.vmDeviceIndexCdromAdd++, image ); + } + + /** + * Adds CDROM drive to the QEMU virtual machine configuration. + * + * @param index current index of CDROM drive to be added to the virtual machine configuration. + * @param image path to a virtual image that will be inserted as CDROM into the drive. + * @return result state of adding the CDROM drive. + */ + public boolean addCdrom( int index, String image ) + { + ArrayList cdromDiskDevices = this.vmConfig.getDiskCdromDevices(); + DiskCdrom cdromDiskDevice = QemuMetaDataUtils.getArrayIndex( cdromDiskDevices, index ); + + if ( cdromDiskDevice == null ) { + // CDROM device does not exist, so create new CDROM device + cdromDiskDevice = this.vmConfig.addDiskCdromDevice(); + cdromDiskDevice.setBusType( BusType.SATA ); + String targetDevName = QemuMetaDataUtils.createAlphabeticalDeviceName( "sd", index ); + cdromDiskDevice.setTargetDevice( targetDevName ); + cdromDiskDevice.setReadOnly( true ); + + if ( image == null ) { + cdromDiskDevice.setStorage( StorageType.BLOCK, CDROM_DEFAULT_PHYSICAL_DRIVE ); + } else { + cdromDiskDevice.setStorage( StorageType.FILE, image ); + } + } else { + // CDROM device exists, so update existing CDROM device + cdromDiskDevice.setReadOnly( true ); + + if ( image == null ) { + cdromDiskDevice.setStorage( StorageType.BLOCK, CDROM_DEFAULT_PHYSICAL_DRIVE ); + } else { + cdromDiskDevice.setStorage( StorageType.FILE, image ); + } + } + + return false; + } + + @Override + public boolean addCpuCoreCount( int nrOfCores ) + { + this.vmConfig.setVCpu( nrOfCores ); + + boolean isVCpuSet = this.vmConfig.getVCpu() == nrOfCores; + + return isVCpuSet; + } + + @Override + public void setSoundCard( SoundCardType type ) + { + QemuSoundCardMeta soundDeviceConfig = this.soundCards.get( type ); + ArrayList soundDevices = this.vmConfig.getSoundDevices(); + Sound.Model soundDeviceModel = soundDeviceConfig.getModel(); + + if ( soundDevices.isEmpty() ) { + // create new sound device with 'soundDeviceModel' hardware + Sound soundDevice = this.vmConfig.addSoundDevice(); + soundDevice.setModel( soundDeviceModel ); + } else { + // update sound device model type of existing sound devices + for ( Sound soundDevice : soundDevices ) { + soundDevice.setModel( soundDeviceModel ); + } + } + } + + @Override + public SoundCardType getSoundCard() + { + ArrayList soundDevices = this.vmConfig.getSoundDevices(); + SoundCardType soundDeviceType = SoundCardType.DEFAULT; + + if ( soundDevices.isEmpty() ) { + // the VM configuration does not contain a sound card device + soundDeviceType = SoundCardType.NONE; + } else { + // the VM configuration at least one sound card device, so return the type of the first one + Sound.Model soundDeviceModel = soundDevices.get( 0 ).getModel(); + soundDeviceType = QemuMetaDataUtils.convertSoundDeviceModel( soundDeviceModel ); + } + + return soundDeviceType; + } + + @Override + public void setDDAcceleration( DDAcceleration type ) + { + QemuDDAccelMeta accelerationConfig = this.ddacc.get( type ); + ArrayList graphicDevices = this.vmConfig.getGraphicDevices(); + ArrayList

- * See ContainerDefintion in tutor-module (bwsuite). - *

- * This field is in vm context the machine description e.g. vmware = vmx. - * This field will be stored in table imageversion.virtualizerconfig - */ - private byte[] containerDefinition; - - public DockerMetaDataDummy(List osList, File file) throws UnsupportedVirtualizerFormatException { - super(osList); - - BufferedInputStream bis = null; - - try { - bis = new BufferedInputStream(new FileInputStream(file)); - containerDefinition = new byte[(int) file.length()]; - bis.read(containerDefinition); - - checkIsTarGz(); - } catch (IOException | UnsupportedVirtualizerFormatException e) { - LOGGER.error("Couldn't read dockerfile", e); - } finally { - try { - bis.close(); - } catch ( IOException e ) { - LOGGER.warn( "Could not close the input stream!" ); - } - } - } - - public DockerMetaDataDummy(List osList, byte[] vmContent, int length) - throws UnsupportedVirtualizerFormatException { - super(osList); - - containerDefinition = vmContent; - - checkIsTarGz(); - } - - /* - TODO This is just a simple check to prevent the workflow from considering any content as acceptable. - */ - /** - * Checks if the first two bytes of the content identifies a tar.gz archive. - * The first byte is 31 == 0x1f, the second byte has to be -117 == 0x8b. - * - * @throws UnsupportedVirtualizerFormatException - */ - private void checkIsTarGz() throws UnsupportedVirtualizerFormatException { - if (!((31 == containerDefinition[0]) && (-117 == containerDefinition[1]))) { - LOGGER.warn("Not Supported Content."); - throw new UnsupportedVirtualizerFormatException( - "DockerMetaDataDummy: Not tar.gz encoded content!"); - } - } - - @Override public byte[] getFilteredDefinitionArray() { - return containerDefinition; - } - - @Override - public List getSupportedImageFormats() - { - return DockerMetaDataDummy.SUPPORTED_IMAGE_FORMATS; - } - - @Override public void applySettingsForLocalEdit() { - - } - - @Override public boolean addHddTemplate(File diskImage, String hddMode, String redoDir) { - return false; - } - - @Override public boolean addHddTemplate(String diskImagePath, String hddMode, String redoDir) { - return false; - } - - @Override public boolean addDefaultNat() { - return false; - } - - @Override public void setOs(String vendorOsId) { - - } - - @Override public boolean addDisplayName(String name) { - return false; - } - - @Override public boolean addRam(int mem) { - return false; - } - - @Override public void addFloppy(int index, String image, boolean readOnly) { - - } - - @Override public boolean addCdrom(String image) { - return false; - } - - @Override public boolean addCpuCoreCount(int nrOfCores) { - return false; - } - - @Override public void setSoundCard(SoundCardType type) { - - } - - @Override public SoundCardType getSoundCard() { - return SoundCardType.NONE; - } - - @Override public void setDDAcceleration(DDAcceleration type) { - - } - - @Override public DDAcceleration getDDAcceleration() { - return DDAcceleration.OFF; - } - - @Override public void setHWVersion(HWVersion type) { - - } - - @Override public HWVersion getHWVersion() { - return HWVersion.DEFAULT; - } - - @Override public void setEthernetDevType(int cardIndex, EthernetDevType type) { - - } - - @Override public EthernetDevType getEthernetDevType(int cardIndex) { - return EthernetDevType.NONE; - } - - @Override public void setMaxUsbSpeed(UsbSpeed speed) { - - } - - @Override public UsbSpeed getMaxUsbSpeed() { - return UsbSpeed.NONE; - } - - @Override public byte[] getDefinitionArray() { - return new byte[0]; - } - - @Override public boolean addEthernet(EtherType type) { - return false; - } - - @Override public Virtualizer getVirtualizer() { - return virtualizer; - } - - @Override public boolean tweakForNonPersistent() { - return false; - } - - @Override public void registerVirtualHW() { - - } -} diff --git a/src/main/java/org/openslx/vm/KeyValuePair.java b/src/main/java/org/openslx/vm/KeyValuePair.java deleted file mode 100644 index c5650ec..0000000 --- a/src/main/java/org/openslx/vm/KeyValuePair.java +++ /dev/null @@ -1,13 +0,0 @@ -package org.openslx.vm; - -class KeyValuePair -{ - public final String key; - public final String value; - - public KeyValuePair( String key, String value ) - { - this.key = key; - this.value = value; - } -} \ No newline at end of file diff --git a/src/main/java/org/openslx/vm/QemuMetaData.java b/src/main/java/org/openslx/vm/QemuMetaData.java deleted file mode 100644 index e758d64..0000000 --- a/src/main/java/org/openslx/vm/QemuMetaData.java +++ /dev/null @@ -1,881 +0,0 @@ -package org.openslx.vm; - -import java.io.File; -import java.math.BigInteger; -import java.nio.charset.StandardCharsets; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; -import java.util.Map.Entry; - -import org.openslx.bwlp.thrift.iface.OperatingSystem; -import org.openslx.bwlp.thrift.iface.Virtualizer; -import org.openslx.libvirt.domain.Domain; -import org.openslx.libvirt.domain.device.ControllerUsb; -import org.openslx.libvirt.domain.device.Disk.BusType; -import org.openslx.libvirt.domain.device.Disk.StorageType; -import org.openslx.libvirt.domain.device.DiskCdrom; -import org.openslx.libvirt.domain.device.DiskFloppy; -import org.openslx.libvirt.domain.device.DiskStorage; -import org.openslx.libvirt.domain.device.Graphics; -import org.openslx.libvirt.domain.device.GraphicsSpice; -import org.openslx.libvirt.domain.device.Interface; -import org.openslx.libvirt.domain.device.Sound; -import org.openslx.libvirt.domain.device.Video; -import org.openslx.libvirt.xml.LibvirtXmlDocumentException; -import org.openslx.libvirt.xml.LibvirtXmlSerializationException; -import org.openslx.libvirt.xml.LibvirtXmlValidationException; -import org.openslx.thrifthelper.TConst; -import org.openslx.vm.disk.DiskImage; -import org.openslx.vm.disk.DiskImage.ImageFormat; - -/** - * Metadata to describe the hardware type of a QEMU sound card. - * - * @author Manuel Bentele - * @version 1.0 - */ -class QemuSoundCardMeta -{ - /** - * Stores the hardware model of the QEMU sound card. - */ - private final Sound.Model model; - - /** - * Creates metadata to describe the hardware model of a QEMU sound card. - * - * @param model hardware model of the QEMU sound card. - */ - public QemuSoundCardMeta( Sound.Model model ) - { - this.model = model; - } - - /** - * Returns hardware model of the QEMU sound card. - * - * @return hardware model of the QEMU sound card. - */ - public Sound.Model getModel() - { - return this.model; - } -} - -/** - * Metadata to describe the hardware acceleration state of QEMU virtual graphics. - * - * @author Manuel Bentele - * @version 1.0 - */ -class QemuDDAccelMeta -{ - /** - * Stores state of the hardware acceleration for QEMU virtual graphics. - */ - private final boolean enabled; - - /** - * Creates metadata to describe the hardware acceleration state of QEMU virtual graphics. - * - * @param enabled state of the hardware acceleration for QEMU virtual graphics. - */ - public QemuDDAccelMeta( boolean enabled ) - { - this.enabled = enabled; - } - - /** - * Returns state of the hardware acceleration of QEMU virtual graphics. - * - * @return state of the hardware acceleration for QEMU virtual graphics. - */ - public boolean isEnabled() - { - return this.enabled; - } -} - -/** - * Metadata to describe the version of a QEMU virtual machine configuration. - * - * @author Manuel Bentele - * @version 1.0 - */ -class QemuHWVersionMeta -{ - /** - * Stores the version of a QEMU virtual machine configuration. - */ - private final int version; - - /** - * Creates metadata to describe the version of a QEMU virtual machine configuration. - * - * @param version version of the QEMU virtual machine configuration. - */ - public QemuHWVersionMeta( int version ) - { - this.version = version; - } - - /** - * Returns version of the QEMU virtual machine configuration. - * - * @return version of the QEMU virtual machine configuration. - */ - public int getVersion() - { - return this.version; - } -} - -/** - * Metadata to describe the hardware type of a QEMU ethernet device. - * - * @author Manuel Bentele - * @version 1.0 - */ -class QemuEthernetDevTypeMeta -{ - /** - * Stores the hardware model of the QEMU ethernet device. - */ - private final Interface.Model model; - - /** - * Creates metadata to describe the hardware type of a QEMU ethernet device. - * - * @param model hardware type of the QEMU ethernet device. - */ - public QemuEthernetDevTypeMeta( Interface.Model model ) - { - this.model = model; - } - - /** - * Returns the hardware type of a QEMU ethernet device. - * - * @return hardware type of the QEMU ethernet device. - */ - public Interface.Model getModel() - { - return this.model; - } -} - -/** - * Metadata to describe a QEMU USB controller. - * - * @author Manuel Bentele - * @version 1.0 - */ -class QemuUsbSpeedMeta -{ - /** - * Stores the USB speed of the QEMU USB controller. - */ - private final int speed; - - /** - * Stores the QEMU hardware model of the USB controller. - */ - private final ControllerUsb.Model model; - - /** - * Creates metadata to describe a QEMU USB controller. - * - * @param speed USB speed of the QEMU USB controller. - * @param model QEMU hardware model of the USB controller. - */ - public QemuUsbSpeedMeta( int speed, ControllerUsb.Model model ) - { - this.speed = speed; - this.model = model; - } - - /** - * Returns the speed of the QEMU USB controller. - * - * @return speed of the QEMU USB controller. - */ - public int getSpeed() - { - return this.speed; - } - - /** - * Returns QEMU hardware model of the USB controller. - * - * @return hardware model of the QEMU USB controller. - */ - public ControllerUsb.Model getModel() - { - return this.model; - } -} - -/** - * Virtual machine configuration (managed by Libvirt) for the QEMU hypervisor. - * - * @author Manuel Bentele - * @version 1.0 - */ -public class QemuMetaData extends - VmMetaData -{ - /** - * Name of the network bridge for the LAN. - */ - public static final String NETWORK_BRIDGE_LAN_DEFAULT = "br0"; - - /** - * Name of the network bridge for the default NAT network. - */ - public static final String NETWORK_BRIDGE_NAT_DEFAULT = "nat1"; - - /** - * Name of the network for the isolated host network (host only). - */ - public static final String NETWORK_BRIDGE_HOST_ONLY_DEFAULT = "vsw2"; - - /** - * Default physical CDROM drive of the hypervisor host. - */ - public static final String CDROM_DEFAULT_PHYSICAL_DRIVE = "/dev/sr0"; - - /** - * List of supported image formats by the QEMU hypervisor. - */ - private static final List SUPPORTED_IMAGE_FORMATS = Collections.unmodifiableList( - Arrays.asList( ImageFormat.QCOW2, ImageFormat.VMDK, ImageFormat.VDI ) ); - - /** - * Representation of a QEMU hypervisor (managed by Libvirt). - */ - private static final Virtualizer VIRTUALIZER = new Virtualizer( TConst.VIRT_QEMU, "QEMU" ); - - /** - * Libvirt XML configuration file to modify configuration of virtual machine for QEMU. - */ - private Domain vmConfig = null; - - /** - * Stores current index of added HDD device to the Libvirt XML configuration file. - */ - private int vmDeviceIndexHddAdd = 0; - - /** - * Stores current index of added CDROM device to the Libvirt XML configuration file. - */ - private int vmDeviceIndexCdromAdd = 0; - - /** - * Stores current index of added ethernet device to the Libvirt XML configuration file. - */ - private int vmDeviceIndexEthernetAdd = 0; - - /** - * Creates new virtual machine configuration (managed by Libvirt) for the QEMU hypervisor. - * - * @param osList list of operating systems. - * @param file image file for the QEMU hypervisor. - * - * @throws UnsupportedVirtualizerFormatException Libvirt XML configuration cannot be processed. - */ - public QemuMetaData( List osList, File file ) throws UnsupportedVirtualizerFormatException - { - super( osList ); - - try { - // read and parse Libvirt domain XML configuration document - this.vmConfig = new Domain( file ); - } catch ( LibvirtXmlDocumentException | LibvirtXmlSerializationException | LibvirtXmlValidationException e ) { - throw new UnsupportedVirtualizerFormatException( e.getLocalizedMessage() ); - } - - // parse VM config and initialize fields of QemuMetaData class - this.parseVmConfig(); - } - - /** - * Creates new virtual machine configuration (managed by Libvirt) for the QEMU hypervisor. - * - * @param osList list of operating systems. - * @param vmContent file content for the QEMU hypervisor. - * @param length number of bytes of the file content. - * - * @throws UnsupportedVirtualizerFormatException Libvirt XML configuration cannot be processed. - */ - public QemuMetaData( List osList, byte[] vmContent, int length ) - throws UnsupportedVirtualizerFormatException - { - super( osList ); - - try { - // read and parse Libvirt domain XML configuration document - this.vmConfig = new Domain( new String( vmContent ) ); - } catch ( LibvirtXmlDocumentException | LibvirtXmlSerializationException | LibvirtXmlValidationException e ) { - throw new UnsupportedVirtualizerFormatException( e.getLocalizedMessage() ); - } - - // parse VM config and initialize fields of QemuMetaData class - this.parseVmConfig(); - } - - /** - * Parses Libvirt domain XML configuration to initialize QEMU metadata. - */ - private void parseVmConfig() - { - // set display name of VM - this.displayName = vmConfig.getName(); - - // this property cannot be checked with the Libvirt domain XML configuration - // to check if machine is in a paused/suspended state, look in the QEMU qcow2 image for snapshots and machine states - this.isMachineSnapshot = false; - - // add HDDs, SSDs to QEMU metadata - for ( DiskStorage storageDiskDevice : this.vmConfig.getDiskStorageDevices() ) { - this.addHddMetaData( storageDiskDevice ); - } - - // start of privacy filters to filter out sensitive information like name of users in absolute paths, ... - // removes all referenced storage files of all specified CDROMs, Floppy drives and HDDs - this.vmConfig.removeDiskDevicesStorage(); - } - - /** - * Adds an existing and observed storage disk device to the HDD metadata. - * - * @param storageDiskDevice existing and observed storage disk that should be added to the - * metadata. - */ - private void addHddMetaData( DiskStorage storageDiskDevice ) - { - String hddChipsetModel = null; - DriveBusType hddChipsetBus = QemuMetaDataUtils.convertBusType( storageDiskDevice.getBusType() ); - String hddImagePath = storageDiskDevice.getStorageSource(); - - this.hdds.add( new HardDisk( hddChipsetModel, hddChipsetBus, hddImagePath ) ); - } - - @Override - public byte[] getFilteredDefinitionArray() - { - // removes all specified boot order entries - this.vmConfig.removeBootOrder(); - - // removes all source networks of all specified network interfaces - this.vmConfig.removeInterfaceDevicesSource(); - - // output filtered Libvirt domain XML configuration - String configuration = this.vmConfig.toString(); - return configuration.getBytes( StandardCharsets.UTF_8 ); - } - - @Override - public List getSupportedImageFormats() - { - return QemuMetaData.SUPPORTED_IMAGE_FORMATS; - } - - @Override - public void applySettingsForLocalEdit() - { - // NOT implemented yet - } - - @Override - public boolean addHddTemplate( File diskImage, String hddMode, String redoDir ) - { - return this.addHddTemplate( diskImage.getAbsolutePath(), hddMode, redoDir ); - } - - @Override - public boolean addHddTemplate( String diskImagePath, String hddMode, String redoDir ) - { - return this.addHddTemplate( this.vmDeviceIndexHddAdd++, diskImagePath, hddMode, redoDir ); - } - - /** - * Adds hard disk drive (HDD) to the QEMU virtual machine configuration. - * - * @param index current index of HDD to be added to the virtual machine configuration. - * @param diskImagePath path to the virtual disk image for the HDD. - * @param hddMode operation mode of the HDD. - * @param redoDir directory for the redo log if an independent non-persistent - * hddMode is set. - * @return result state of adding the HDD. - */ - public boolean addHddTemplate( int index, String diskImagePath, String hddMode, String redoDir ) - { - ArrayList storageDiskDevices = this.vmConfig.getDiskStorageDevices(); - DiskStorage storageDiskDevice = QemuMetaDataUtils.getArrayIndex( storageDiskDevices, index ); - - if ( storageDiskDevice == null ) { - // HDD does not exist, so create new storage (HDD) device - storageDiskDevice = this.vmConfig.addDiskStorageDevice(); - storageDiskDevice.setReadOnly( false ); - storageDiskDevice.setBusType( BusType.VIRTIO ); - String targetDevName = QemuMetaDataUtils.createAlphabeticalDeviceName( "vd", index ); - storageDiskDevice.setTargetDevice( targetDevName ); - storageDiskDevice.setStorage( StorageType.FILE, diskImagePath ); - - // add new created HDD to the metadata of the QemuMetaData object, too - this.addHddMetaData( storageDiskDevice ); - } else { - // HDD exists, so update existing storage (HDD) device - storageDiskDevice.setStorage( StorageType.FILE, diskImagePath ); - } - - return false; - } - - @Override - public boolean addDefaultNat() - { - return this.addEthernet( EtherType.NAT ); - } - - @Override - public void setOs( String vendorOsId ) - { - this.setOs( vendorOsId ); - } - - @Override - public boolean addDisplayName( String name ) - { - this.vmConfig.setName( name ); - - final boolean statusName = this.vmConfig.getName().equals( name ); - - return statusName; - } - - @Override - public boolean addRam( int mem ) - { - BigInteger memory = BigInteger.valueOf( mem ); - - this.vmConfig.setMemory( memory ); - this.vmConfig.setCurrentMemory( memory ); - - final boolean isMemorySet = this.vmConfig.getMemory().toString().equals( memory.toString() ); - final boolean isCurrentMemorySet = this.vmConfig.getCurrentMemory().toString().equals( memory.toString() ); - - return isMemorySet && isCurrentMemorySet; - } - - @Override - public void addFloppy( int index, String image, boolean readOnly ) - { - ArrayList floppyDiskDevices = this.vmConfig.getDiskFloppyDevices(); - DiskFloppy floppyDiskDevice = QemuMetaDataUtils.getArrayIndex( floppyDiskDevices, index ); - - if ( floppyDiskDevice == null ) { - // floppy device does not exist, so create new floppy device - floppyDiskDevice = this.vmConfig.addDiskFloppyDevice(); - floppyDiskDevice.setBusType( BusType.FDC ); - String targetDevName = QemuMetaDataUtils.createAlphabeticalDeviceName( "fd", index ); - floppyDiskDevice.setTargetDevice( targetDevName ); - floppyDiskDevice.setReadOnly( readOnly ); - floppyDiskDevice.setStorage( StorageType.FILE, image ); - } else { - // floppy device exists, so update existing floppy device - floppyDiskDevice.setReadOnly( readOnly ); - floppyDiskDevice.setStorage( StorageType.FILE, image ); - } - } - - @Override - public boolean addCdrom( String image ) - { - return this.addCdrom( this.vmDeviceIndexCdromAdd++, image ); - } - - /** - * Adds CDROM drive to the QEMU virtual machine configuration. - * - * @param index current index of CDROM drive to be added to the virtual machine configuration. - * @param image path to a virtual image that will be inserted as CDROM into the drive. - * @return result state of adding the CDROM drive. - */ - public boolean addCdrom( int index, String image ) - { - ArrayList cdromDiskDevices = this.vmConfig.getDiskCdromDevices(); - DiskCdrom cdromDiskDevice = QemuMetaDataUtils.getArrayIndex( cdromDiskDevices, index ); - - if ( cdromDiskDevice == null ) { - // CDROM device does not exist, so create new CDROM device - cdromDiskDevice = this.vmConfig.addDiskCdromDevice(); - cdromDiskDevice.setBusType( BusType.SATA ); - String targetDevName = QemuMetaDataUtils.createAlphabeticalDeviceName( "sd", index ); - cdromDiskDevice.setTargetDevice( targetDevName ); - cdromDiskDevice.setReadOnly( true ); - - if ( image == null ) { - cdromDiskDevice.setStorage( StorageType.BLOCK, CDROM_DEFAULT_PHYSICAL_DRIVE ); - } else { - cdromDiskDevice.setStorage( StorageType.FILE, image ); - } - } else { - // CDROM device exists, so update existing CDROM device - cdromDiskDevice.setReadOnly( true ); - - if ( image == null ) { - cdromDiskDevice.setStorage( StorageType.BLOCK, CDROM_DEFAULT_PHYSICAL_DRIVE ); - } else { - cdromDiskDevice.setStorage( StorageType.FILE, image ); - } - } - - return false; - } - - @Override - public boolean addCpuCoreCount( int nrOfCores ) - { - this.vmConfig.setVCpu( nrOfCores ); - - boolean isVCpuSet = this.vmConfig.getVCpu() == nrOfCores; - - return isVCpuSet; - } - - @Override - public void setSoundCard( SoundCardType type ) - { - QemuSoundCardMeta soundDeviceConfig = this.soundCards.get( type ); - ArrayList soundDevices = this.vmConfig.getSoundDevices(); - Sound.Model soundDeviceModel = soundDeviceConfig.getModel(); - - if ( soundDevices.isEmpty() ) { - // create new sound device with 'soundDeviceModel' hardware - Sound soundDevice = this.vmConfig.addSoundDevice(); - soundDevice.setModel( soundDeviceModel ); - } else { - // update sound device model type of existing sound devices - for ( Sound soundDevice : soundDevices ) { - soundDevice.setModel( soundDeviceModel ); - } - } - } - - @Override - public SoundCardType getSoundCard() - { - ArrayList soundDevices = this.vmConfig.getSoundDevices(); - SoundCardType soundDeviceType = SoundCardType.DEFAULT; - - if ( soundDevices.isEmpty() ) { - // the VM configuration does not contain a sound card device - soundDeviceType = SoundCardType.NONE; - } else { - // the VM configuration at least one sound card device, so return the type of the first one - Sound.Model soundDeviceModel = soundDevices.get( 0 ).getModel(); - soundDeviceType = QemuMetaDataUtils.convertSoundDeviceModel( soundDeviceModel ); - } - - return soundDeviceType; - } - - @Override - public void setDDAcceleration( DDAcceleration type ) - { - QemuDDAccelMeta accelerationConfig = this.ddacc.get( type ); - ArrayList graphicDevices = this.vmConfig.getGraphicDevices(); - ArrayList

+ * See ContainerDefintion in tutor-module (bwsuite). + *

+ * This field is in vm context the machine description e.g. vmware = vmx. + * This field will be stored in table imageversion.virtualizerconfig + */ + private byte[] containerDefinition; + + public VirtualizationConfigurationDocker(List osList, File file) throws VirtualizationConfigurationException { + super(osList); + + BufferedInputStream bis = null; + + try { + bis = new BufferedInputStream(new FileInputStream(file)); + containerDefinition = new byte[(int) file.length()]; + bis.read(containerDefinition); + + checkIsTarGz(); + } catch (IOException | VirtualizationConfigurationException e) { + LOGGER.error("Couldn't read dockerfile", e); + } finally { + try { + bis.close(); + } catch ( IOException e ) { + LOGGER.warn( "Could not close the input stream!" ); + } + } + } + + public VirtualizationConfigurationDocker(List osList, byte[] vmContent, int length) + throws VirtualizationConfigurationException { + super(osList); + + containerDefinition = vmContent; + + checkIsTarGz(); + } + + /* + TODO This is just a simple check to prevent the workflow from considering any content as acceptable. + */ + /** + * Checks if the first two bytes of the content identifies a tar.gz archive. + * The first byte is 31 == 0x1f, the second byte has to be -117 == 0x8b. + * + * @throws VirtualizationConfigurationException + */ + private void checkIsTarGz() throws VirtualizationConfigurationException { + if (!((31 == containerDefinition[0]) && (-117 == containerDefinition[1]))) { + LOGGER.warn("Not Supported Content."); + throw new VirtualizationConfigurationException( + "DockerMetaDataDummy: Not tar.gz encoded content!"); + } + } + + @Override public byte[] getFilteredDefinitionArray() { + return containerDefinition; + } + + @Override + public List getSupportedImageFormats() + { + return VirtualizationConfigurationDocker.SUPPORTED_IMAGE_FORMATS; + } + + @Override public void applySettingsForLocalEdit() { + + } + + @Override public boolean addHddTemplate(File diskImage, String hddMode, String redoDir) { + return false; + } + + @Override public boolean addHddTemplate(String diskImagePath, String hddMode, String redoDir) { + return false; + } + + @Override public boolean addDefaultNat() { + return false; + } + + @Override public void setOs(String vendorOsId) { + + } + + @Override public boolean addDisplayName(String name) { + return false; + } + + @Override public boolean addRam(int mem) { + return false; + } + + @Override public void addFloppy(int index, String image, boolean readOnly) { + + } + + @Override public boolean addCdrom(String image) { + return false; + } + + @Override public boolean addCpuCoreCount(int nrOfCores) { + return false; + } + + @Override public void setSoundCard(SoundCardType type) { + + } + + @Override public SoundCardType getSoundCard() { + return SoundCardType.NONE; + } + + @Override public void setDDAcceleration(DDAcceleration type) { + + } + + @Override public DDAcceleration getDDAcceleration() { + return DDAcceleration.OFF; + } + + @Override public void setHWVersion(HWVersion type) { + + } + + @Override public HWVersion getHWVersion() { + return HWVersion.DEFAULT; + } + + @Override public void setEthernetDevType(int cardIndex, EthernetDevType type) { + + } + + @Override public EthernetDevType getEthernetDevType(int cardIndex) { + return EthernetDevType.NONE; + } + + @Override public void setMaxUsbSpeed(UsbSpeed speed) { + + } + + @Override public UsbSpeed getMaxUsbSpeed() { + return UsbSpeed.NONE; + } + + @Override public byte[] getDefinitionArray() { + return new byte[0]; + } + + @Override public boolean addEthernet(EtherType type) { + return false; + } + + @Override public Virtualizer getVirtualizer() { + return virtualizer; + } + + @Override public boolean tweakForNonPersistent() { + return false; + } + + @Override public void registerVirtualHW() { + + } +} diff --git a/src/main/java/org/openslx/virtualization/configuration/VirtualizationConfigurationException.java b/src/main/java/org/openslx/virtualization/configuration/VirtualizationConfigurationException.java new file mode 100644 index 0000000..2d401b1 --- /dev/null +++ b/src/main/java/org/openslx/virtualization/configuration/VirtualizationConfigurationException.java @@ -0,0 +1,13 @@ +package org.openslx.virtualization.configuration; + +public class VirtualizationConfigurationException extends Exception +{ + /** + * Version for serialization. + */ + private static final long serialVersionUID = 5794121065945636839L; + + public VirtualizationConfigurationException(String message) { + super(message); + } +} \ No newline at end of file diff --git a/src/main/java/org/openslx/virtualization/configuration/VirtualizationConfigurationQemu.java b/src/main/java/org/openslx/virtualization/configuration/VirtualizationConfigurationQemu.java new file mode 100644 index 0000000..91bb9ae --- /dev/null +++ b/src/main/java/org/openslx/virtualization/configuration/VirtualizationConfigurationQemu.java @@ -0,0 +1,881 @@ +package org.openslx.virtualization.configuration; + +import java.io.File; +import java.math.BigInteger; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Map.Entry; + +import org.openslx.bwlp.thrift.iface.OperatingSystem; +import org.openslx.bwlp.thrift.iface.Virtualizer; +import org.openslx.libvirt.domain.Domain; +import org.openslx.libvirt.domain.device.ControllerUsb; +import org.openslx.libvirt.domain.device.Disk.BusType; +import org.openslx.libvirt.domain.device.Disk.StorageType; +import org.openslx.libvirt.domain.device.DiskCdrom; +import org.openslx.libvirt.domain.device.DiskFloppy; +import org.openslx.libvirt.domain.device.DiskStorage; +import org.openslx.libvirt.domain.device.Graphics; +import org.openslx.libvirt.domain.device.GraphicsSpice; +import org.openslx.libvirt.domain.device.Interface; +import org.openslx.libvirt.domain.device.Sound; +import org.openslx.libvirt.domain.device.Video; +import org.openslx.libvirt.xml.LibvirtXmlDocumentException; +import org.openslx.libvirt.xml.LibvirtXmlSerializationException; +import org.openslx.libvirt.xml.LibvirtXmlValidationException; +import org.openslx.thrifthelper.TConst; +import org.openslx.vm.disk.DiskImage; +import org.openslx.vm.disk.DiskImage.ImageFormat; + +/** + * Metadata to describe the hardware type of a QEMU sound card. + * + * @author Manuel Bentele + * @version 1.0 + */ +class QemuSoundCardMeta +{ + /** + * Stores the hardware model of the QEMU sound card. + */ + private final Sound.Model model; + + /** + * Creates metadata to describe the hardware model of a QEMU sound card. + * + * @param model hardware model of the QEMU sound card. + */ + public QemuSoundCardMeta( Sound.Model model ) + { + this.model = model; + } + + /** + * Returns hardware model of the QEMU sound card. + * + * @return hardware model of the QEMU sound card. + */ + public Sound.Model getModel() + { + return this.model; + } +} + +/** + * Metadata to describe the hardware acceleration state of QEMU virtual graphics. + * + * @author Manuel Bentele + * @version 1.0 + */ +class QemuDDAccelMeta +{ + /** + * Stores state of the hardware acceleration for QEMU virtual graphics. + */ + private final boolean enabled; + + /** + * Creates metadata to describe the hardware acceleration state of QEMU virtual graphics. + * + * @param enabled state of the hardware acceleration for QEMU virtual graphics. + */ + public QemuDDAccelMeta( boolean enabled ) + { + this.enabled = enabled; + } + + /** + * Returns state of the hardware acceleration of QEMU virtual graphics. + * + * @return state of the hardware acceleration for QEMU virtual graphics. + */ + public boolean isEnabled() + { + return this.enabled; + } +} + +/** + * Metadata to describe the version of a QEMU virtual machine configuration. + * + * @author Manuel Bentele + * @version 1.0 + */ +class QemuHWVersionMeta +{ + /** + * Stores the version of a QEMU virtual machine configuration. + */ + private final int version; + + /** + * Creates metadata to describe the version of a QEMU virtual machine configuration. + * + * @param version version of the QEMU virtual machine configuration. + */ + public QemuHWVersionMeta( int version ) + { + this.version = version; + } + + /** + * Returns version of the QEMU virtual machine configuration. + * + * @return version of the QEMU virtual machine configuration. + */ + public int getVersion() + { + return this.version; + } +} + +/** + * Metadata to describe the hardware type of a QEMU ethernet device. + * + * @author Manuel Bentele + * @version 1.0 + */ +class QemuEthernetDevTypeMeta +{ + /** + * Stores the hardware model of the QEMU ethernet device. + */ + private final Interface.Model model; + + /** + * Creates metadata to describe the hardware type of a QEMU ethernet device. + * + * @param model hardware type of the QEMU ethernet device. + */ + public QemuEthernetDevTypeMeta( Interface.Model model ) + { + this.model = model; + } + + /** + * Returns the hardware type of a QEMU ethernet device. + * + * @return hardware type of the QEMU ethernet device. + */ + public Interface.Model getModel() + { + return this.model; + } +} + +/** + * Metadata to describe a QEMU USB controller. + * + * @author Manuel Bentele + * @version 1.0 + */ +class QemuUsbSpeedMeta +{ + /** + * Stores the USB speed of the QEMU USB controller. + */ + private final int speed; + + /** + * Stores the QEMU hardware model of the USB controller. + */ + private final ControllerUsb.Model model; + + /** + * Creates metadata to describe a QEMU USB controller. + * + * @param speed USB speed of the QEMU USB controller. + * @param model QEMU hardware model of the USB controller. + */ + public QemuUsbSpeedMeta( int speed, ControllerUsb.Model model ) + { + this.speed = speed; + this.model = model; + } + + /** + * Returns the speed of the QEMU USB controller. + * + * @return speed of the QEMU USB controller. + */ + public int getSpeed() + { + return this.speed; + } + + /** + * Returns QEMU hardware model of the USB controller. + * + * @return hardware model of the QEMU USB controller. + */ + public ControllerUsb.Model getModel() + { + return this.model; + } +} + +/** + * Virtual machine configuration (managed by Libvirt) for the QEMU hypervisor. + * + * @author Manuel Bentele + * @version 1.0 + */ +public class VirtualizationConfigurationQemu extends + VirtualizationConfiguration +{ + /** + * Name of the network bridge for the LAN. + */ + public static final String NETWORK_BRIDGE_LAN_DEFAULT = "br0"; + + /** + * Name of the network bridge for the default NAT network. + */ + public static final String NETWORK_BRIDGE_NAT_DEFAULT = "nat1"; + + /** + * Name of the network for the isolated host network (host only). + */ + public static final String NETWORK_BRIDGE_HOST_ONLY_DEFAULT = "vsw2"; + + /** + * Default physical CDROM drive of the hypervisor host. + */ + public static final String CDROM_DEFAULT_PHYSICAL_DRIVE = "/dev/sr0"; + + /** + * List of supported image formats by the QEMU hypervisor. + */ + private static final List SUPPORTED_IMAGE_FORMATS = Collections.unmodifiableList( + Arrays.asList( ImageFormat.QCOW2, ImageFormat.VMDK, ImageFormat.VDI ) ); + + /** + * Representation of a QEMU hypervisor (managed by Libvirt). + */ + private static final Virtualizer VIRTUALIZER = new Virtualizer( TConst.VIRT_QEMU, "QEMU" ); + + /** + * Libvirt XML configuration file to modify configuration of virtual machine for QEMU. + */ + private Domain vmConfig = null; + + /** + * Stores current index of added HDD device to the Libvirt XML configuration file. + */ + private int vmDeviceIndexHddAdd = 0; + + /** + * Stores current index of added CDROM device to the Libvirt XML configuration file. + */ + private int vmDeviceIndexCdromAdd = 0; + + /** + * Stores current index of added ethernet device to the Libvirt XML configuration file. + */ + private int vmDeviceIndexEthernetAdd = 0; + + /** + * Creates new virtual machine configuration (managed by Libvirt) for the QEMU hypervisor. + * + * @param osList list of operating systems. + * @param file image file for the QEMU hypervisor. + * + * @throws VirtualizationConfigurationException Libvirt XML configuration cannot be processed. + */ + public VirtualizationConfigurationQemu( List osList, File file ) throws VirtualizationConfigurationException + { + super( osList ); + + try { + // read and parse Libvirt domain XML configuration document + this.vmConfig = new Domain( file ); + } catch ( LibvirtXmlDocumentException | LibvirtXmlSerializationException | LibvirtXmlValidationException e ) { + throw new VirtualizationConfigurationException( e.getLocalizedMessage() ); + } + + // parse VM config and initialize fields of QemuMetaData class + this.parseVmConfig(); + } + + /** + * Creates new virtual machine configuration (managed by Libvirt) for the QEMU hypervisor. + * + * @param osList list of operating systems. + * @param vmContent file content for the QEMU hypervisor. + * @param length number of bytes of the file content. + * + * @throws VirtualizationConfigurationException Libvirt XML configuration cannot be processed. + */ + public VirtualizationConfigurationQemu( List osList, byte[] vmContent, int length ) + throws VirtualizationConfigurationException + { + super( osList ); + + try { + // read and parse Libvirt domain XML configuration document + this.vmConfig = new Domain( new String( vmContent ) ); + } catch ( LibvirtXmlDocumentException | LibvirtXmlSerializationException | LibvirtXmlValidationException e ) { + throw new VirtualizationConfigurationException( e.getLocalizedMessage() ); + } + + // parse VM config and initialize fields of QemuMetaData class + this.parseVmConfig(); + } + + /** + * Parses Libvirt domain XML configuration to initialize QEMU metadata. + */ + private void parseVmConfig() + { + // set display name of VM + this.displayName = vmConfig.getName(); + + // this property cannot be checked with the Libvirt domain XML configuration + // to check if machine is in a paused/suspended state, look in the QEMU qcow2 image for snapshots and machine states + this.isMachineSnapshot = false; + + // add HDDs, SSDs to QEMU metadata + for ( DiskStorage storageDiskDevice : this.vmConfig.getDiskStorageDevices() ) { + this.addHddMetaData( storageDiskDevice ); + } + + // start of privacy filters to filter out sensitive information like name of users in absolute paths, ... + // removes all referenced storage files of all specified CDROMs, Floppy drives and HDDs + this.vmConfig.removeDiskDevicesStorage(); + } + + /** + * Adds an existing and observed storage disk device to the HDD metadata. + * + * @param storageDiskDevice existing and observed storage disk that should be added to the + * metadata. + */ + private void addHddMetaData( DiskStorage storageDiskDevice ) + { + String hddChipsetModel = null; + DriveBusType hddChipsetBus = VirtualizationConfigurationQemuUtils.convertBusType( storageDiskDevice.getBusType() ); + String hddImagePath = storageDiskDevice.getStorageSource(); + + this.hdds.add( new HardDisk( hddChipsetModel, hddChipsetBus, hddImagePath ) ); + } + + @Override + public byte[] getFilteredDefinitionArray() + { + // removes all specified boot order entries + this.vmConfig.removeBootOrder(); + + // removes all source networks of all specified network interfaces + this.vmConfig.removeInterfaceDevicesSource(); + + // output filtered Libvirt domain XML configuration + String configuration = this.vmConfig.toString(); + return configuration.getBytes( StandardCharsets.UTF_8 ); + } + + @Override + public List getSupportedImageFormats() + { + return VirtualizationConfigurationQemu.SUPPORTED_IMAGE_FORMATS; + } + + @Override + public void applySettingsForLocalEdit() + { + // NOT implemented yet + } + + @Override + public boolean addHddTemplate( File diskImage, String hddMode, String redoDir ) + { + return this.addHddTemplate( diskImage.getAbsolutePath(), hddMode, redoDir ); + } + + @Override + public boolean addHddTemplate( String diskImagePath, String hddMode, String redoDir ) + { + return this.addHddTemplate( this.vmDeviceIndexHddAdd++, diskImagePath, hddMode, redoDir ); + } + + /** + * Adds hard disk drive (HDD) to the QEMU virtual machine configuration. + * + * @param index current index of HDD to be added to the virtual machine configuration. + * @param diskImagePath path to the virtual disk image for the HDD. + * @param hddMode operation mode of the HDD. + * @param redoDir directory for the redo log if an independent non-persistent + * hddMode is set. + * @return result state of adding the HDD. + */ + public boolean addHddTemplate( int index, String diskImagePath, String hddMode, String redoDir ) + { + ArrayList storageDiskDevices = this.vmConfig.getDiskStorageDevices(); + DiskStorage storageDiskDevice = VirtualizationConfigurationQemuUtils.getArrayIndex( storageDiskDevices, index ); + + if ( storageDiskDevice == null ) { + // HDD does not exist, so create new storage (HDD) device + storageDiskDevice = this.vmConfig.addDiskStorageDevice(); + storageDiskDevice.setReadOnly( false ); + storageDiskDevice.setBusType( BusType.VIRTIO ); + String targetDevName = VirtualizationConfigurationQemuUtils.createAlphabeticalDeviceName( "vd", index ); + storageDiskDevice.setTargetDevice( targetDevName ); + storageDiskDevice.setStorage( StorageType.FILE, diskImagePath ); + + // add new created HDD to the metadata of the QemuMetaData object, too + this.addHddMetaData( storageDiskDevice ); + } else { + // HDD exists, so update existing storage (HDD) device + storageDiskDevice.setStorage( StorageType.FILE, diskImagePath ); + } + + return false; + } + + @Override + public boolean addDefaultNat() + { + return this.addEthernet( EtherType.NAT ); + } + + @Override + public void setOs( String vendorOsId ) + { + this.setOs( vendorOsId ); + } + + @Override + public boolean addDisplayName( String name ) + { + this.vmConfig.setName( name ); + + final boolean statusName = this.vmConfig.getName().equals( name ); + + return statusName; + } + + @Override + public boolean addRam( int mem ) + { + BigInteger memory = BigInteger.valueOf( mem ); + + this.vmConfig.setMemory( memory ); + this.vmConfig.setCurrentMemory( memory ); + + final boolean isMemorySet = this.vmConfig.getMemory().toString().equals( memory.toString() ); + final boolean isCurrentMemorySet = this.vmConfig.getCurrentMemory().toString().equals( memory.toString() ); + + return isMemorySet && isCurrentMemorySet; + } + + @Override + public void addFloppy( int index, String image, boolean readOnly ) + { + ArrayList floppyDiskDevices = this.vmConfig.getDiskFloppyDevices(); + DiskFloppy floppyDiskDevice = VirtualizationConfigurationQemuUtils.getArrayIndex( floppyDiskDevices, index ); + + if ( floppyDiskDevice == null ) { + // floppy device does not exist, so create new floppy device + floppyDiskDevice = this.vmConfig.addDiskFloppyDevice(); + floppyDiskDevice.setBusType( BusType.FDC ); + String targetDevName = VirtualizationConfigurationQemuUtils.createAlphabeticalDeviceName( "fd", index ); + floppyDiskDevice.setTargetDevice( targetDevName ); + floppyDiskDevice.setReadOnly( readOnly ); + floppyDiskDevice.setStorage( StorageType.FILE, image ); + } else { + // floppy device exists, so update existing floppy device + floppyDiskDevice.setReadOnly( readOnly ); + floppyDiskDevice.setStorage( StorageType.FILE, image ); + } + } + + @Override + public boolean addCdrom( String image ) + { + return this.addCdrom( this.vmDeviceIndexCdromAdd++, image ); + } + + /** + * Adds CDROM drive to the QEMU virtual machine configuration. + * + * @param index current index of CDROM drive to be added to the virtual machine configuration. + * @param image path to a virtual image that will be inserted as CDROM into the drive. + * @return result state of adding the CDROM drive. + */ + public boolean addCdrom( int index, String image ) + { + ArrayList cdromDiskDevices = this.vmConfig.getDiskCdromDevices(); + DiskCdrom cdromDiskDevice = VirtualizationConfigurationQemuUtils.getArrayIndex( cdromDiskDevices, index ); + + if ( cdromDiskDevice == null ) { + // CDROM device does not exist, so create new CDROM device + cdromDiskDevice = this.vmConfig.addDiskCdromDevice(); + cdromDiskDevice.setBusType( BusType.SATA ); + String targetDevName = VirtualizationConfigurationQemuUtils.createAlphabeticalDeviceName( "sd", index ); + cdromDiskDevice.setTargetDevice( targetDevName ); + cdromDiskDevice.setReadOnly( true ); + + if ( image == null ) { + cdromDiskDevice.setStorage( StorageType.BLOCK, CDROM_DEFAULT_PHYSICAL_DRIVE ); + } else { + cdromDiskDevice.setStorage( StorageType.FILE, image ); + } + } else { + // CDROM device exists, so update existing CDROM device + cdromDiskDevice.setReadOnly( true ); + + if ( image == null ) { + cdromDiskDevice.setStorage( StorageType.BLOCK, CDROM_DEFAULT_PHYSICAL_DRIVE ); + } else { + cdromDiskDevice.setStorage( StorageType.FILE, image ); + } + } + + return false; + } + + @Override + public boolean addCpuCoreCount( int nrOfCores ) + { + this.vmConfig.setVCpu( nrOfCores ); + + boolean isVCpuSet = this.vmConfig.getVCpu() == nrOfCores; + + return isVCpuSet; + } + + @Override + public void setSoundCard( SoundCardType type ) + { + QemuSoundCardMeta soundDeviceConfig = this.soundCards.get( type ); + ArrayList soundDevices = this.vmConfig.getSoundDevices(); + Sound.Model soundDeviceModel = soundDeviceConfig.getModel(); + + if ( soundDevices.isEmpty() ) { + // create new sound device with 'soundDeviceModel' hardware + Sound soundDevice = this.vmConfig.addSoundDevice(); + soundDevice.setModel( soundDeviceModel ); + } else { + // update sound device model type of existing sound devices + for ( Sound soundDevice : soundDevices ) { + soundDevice.setModel( soundDeviceModel ); + } + } + } + + @Override + public SoundCardType getSoundCard() + { + ArrayList soundDevices = this.vmConfig.getSoundDevices(); + SoundCardType soundDeviceType = SoundCardType.DEFAULT; + + if ( soundDevices.isEmpty() ) { + // the VM configuration does not contain a sound card device + soundDeviceType = SoundCardType.NONE; + } else { + // the VM configuration at least one sound card device, so return the type of the first one + Sound.Model soundDeviceModel = soundDevices.get( 0 ).getModel(); + soundDeviceType = VirtualizationConfigurationQemuUtils.convertSoundDeviceModel( soundDeviceModel ); + } + + return soundDeviceType; + } + + @Override + public void setDDAcceleration( DDAcceleration type ) + { + QemuDDAccelMeta accelerationConfig = this.ddacc.get( type ); + ArrayList graphicDevices = this.vmConfig.getGraphicDevices(); + ArrayList

- * See ContainerDefintion in tutor-module (bwsuite). - *

- * This field is in vm context the machine description e.g. vmware = vmx. - * This field will be stored in table imageversion.virtualizerconfig - */ - private byte[] containerDefinition; - - public DockerMetaDataDummy(List osList, File file) throws UnsupportedVirtualizerFormatException { - super(osList); - - BufferedInputStream bis = null; - - try { - bis = new BufferedInputStream(new FileInputStream(file)); - containerDefinition = new byte[(int) file.length()]; - bis.read(containerDefinition); - - checkIsTarGz(); - } catch (IOException | UnsupportedVirtualizerFormatException e) { - LOGGER.error("Couldn't read dockerfile", e); - } finally { - try { - bis.close(); - } catch ( IOException e ) { - LOGGER.warn( "Could not close the input stream!" ); - } - } - } - - public DockerMetaDataDummy(List osList, byte[] vmContent, int length) - throws UnsupportedVirtualizerFormatException { - super(osList); - - containerDefinition = vmContent; - - checkIsTarGz(); - } - - /* - TODO This is just a simple check to prevent the workflow from considering any content as acceptable. - */ - /** - * Checks if the first two bytes of the content identifies a tar.gz archive. - * The first byte is 31 == 0x1f, the second byte has to be -117 == 0x8b. - * - * @throws UnsupportedVirtualizerFormatException - */ - private void checkIsTarGz() throws UnsupportedVirtualizerFormatException { - if (!((31 == containerDefinition[0]) && (-117 == containerDefinition[1]))) { - LOGGER.warn("Not Supported Content."); - throw new UnsupportedVirtualizerFormatException( - "DockerMetaDataDummy: Not tar.gz encoded content!"); - } - } - - @Override public byte[] getFilteredDefinitionArray() { - return containerDefinition; - } - - @Override - public List getSupportedImageFormats() - { - return DockerMetaDataDummy.SUPPORTED_IMAGE_FORMATS; - } - - @Override public void applySettingsForLocalEdit() { - - } - - @Override public boolean addHddTemplate(File diskImage, String hddMode, String redoDir) { - return false; - } - - @Override public boolean addHddTemplate(String diskImagePath, String hddMode, String redoDir) { - return false; - } - - @Override public boolean addDefaultNat() { - return false; - } - - @Override public void setOs(String vendorOsId) { - - } - - @Override public boolean addDisplayName(String name) { - return false; - } - - @Override public boolean addRam(int mem) { - return false; - } - - @Override public void addFloppy(int index, String image, boolean readOnly) { - - } - - @Override public boolean addCdrom(String image) { - return false; - } - - @Override public boolean addCpuCoreCount(int nrOfCores) { - return false; - } - - @Override public void setSoundCard(SoundCardType type) { - - } - - @Override public SoundCardType getSoundCard() { - return SoundCardType.NONE; - } - - @Override public void setDDAcceleration(DDAcceleration type) { - - } - - @Override public DDAcceleration getDDAcceleration() { - return DDAcceleration.OFF; - } - - @Override public void setHWVersion(HWVersion type) { - - } - - @Override public HWVersion getHWVersion() { - return HWVersion.DEFAULT; - } - - @Override public void setEthernetDevType(int cardIndex, EthernetDevType type) { - - } - - @Override public EthernetDevType getEthernetDevType(int cardIndex) { - return EthernetDevType.NONE; - } - - @Override public void setMaxUsbSpeed(UsbSpeed speed) { - - } - - @Override public UsbSpeed getMaxUsbSpeed() { - return UsbSpeed.NONE; - } - - @Override public byte[] getDefinitionArray() { - return new byte[0]; - } - - @Override public boolean addEthernet(EtherType type) { - return false; - } - - @Override public Virtualizer getVirtualizer() { - return virtualizer; - } - - @Override public boolean tweakForNonPersistent() { - return false; - } - - @Override public void registerVirtualHW() { - - } -} diff --git a/src/main/java/org/openslx/virtualization/configuration/machine/KeyValuePair.java b/src/main/java/org/openslx/virtualization/configuration/machine/KeyValuePair.java deleted file mode 100644 index ecd4f2a..0000000 --- a/src/main/java/org/openslx/virtualization/configuration/machine/KeyValuePair.java +++ /dev/null @@ -1,13 +0,0 @@ -package org.openslx.virtualization.configuration.machine; - -class KeyValuePair -{ - public final String key; - public final String value; - - public KeyValuePair( String key, String value ) - { - this.key = key; - this.value = value; - } -} \ No newline at end of file diff --git a/src/main/java/org/openslx/virtualization/configuration/machine/QemuMetaData.java b/src/main/java/org/openslx/virtualization/configuration/machine/QemuMetaData.java deleted file mode 100644 index 5dd86be..0000000 --- a/src/main/java/org/openslx/virtualization/configuration/machine/QemuMetaData.java +++ /dev/null @@ -1,883 +0,0 @@ -package org.openslx.virtualization.configuration.machine; - -import java.io.File; -import java.math.BigInteger; -import java.nio.charset.StandardCharsets; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; -import java.util.Map.Entry; - -import org.openslx.bwlp.thrift.iface.OperatingSystem; -import org.openslx.bwlp.thrift.iface.Virtualizer; -import org.openslx.libvirt.domain.Domain; -import org.openslx.libvirt.domain.device.ControllerUsb; -import org.openslx.libvirt.domain.device.Disk.BusType; -import org.openslx.libvirt.domain.device.Disk.StorageType; -import org.openslx.libvirt.domain.device.DiskCdrom; -import org.openslx.libvirt.domain.device.DiskFloppy; -import org.openslx.libvirt.domain.device.DiskStorage; -import org.openslx.libvirt.domain.device.Graphics; -import org.openslx.libvirt.domain.device.GraphicsSpice; -import org.openslx.libvirt.domain.device.Interface; -import org.openslx.libvirt.domain.device.Sound; -import org.openslx.libvirt.domain.device.Video; -import org.openslx.libvirt.xml.LibvirtXmlDocumentException; -import org.openslx.libvirt.xml.LibvirtXmlSerializationException; -import org.openslx.libvirt.xml.LibvirtXmlValidationException; -import org.openslx.thrifthelper.TConst; -import org.openslx.virtualization.configuration.UnsupportedVirtualizerFormatException; -import org.openslx.virtualization.configuration.VmMetaData; -import org.openslx.vm.disk.DiskImage; -import org.openslx.vm.disk.DiskImage.ImageFormat; - -/** - * Metadata to describe the hardware type of a QEMU sound card. - * - * @author Manuel Bentele - * @version 1.0 - */ -class QemuSoundCardMeta -{ - /** - * Stores the hardware model of the QEMU sound card. - */ - private final Sound.Model model; - - /** - * Creates metadata to describe the hardware model of a QEMU sound card. - * - * @param model hardware model of the QEMU sound card. - */ - public QemuSoundCardMeta( Sound.Model model ) - { - this.model = model; - } - - /** - * Returns hardware model of the QEMU sound card. - * - * @return hardware model of the QEMU sound card. - */ - public Sound.Model getModel() - { - return this.model; - } -} - -/** - * Metadata to describe the hardware acceleration state of QEMU virtual graphics. - * - * @author Manuel Bentele - * @version 1.0 - */ -class QemuDDAccelMeta -{ - /** - * Stores state of the hardware acceleration for QEMU virtual graphics. - */ - private final boolean enabled; - - /** - * Creates metadata to describe the hardware acceleration state of QEMU virtual graphics. - * - * @param enabled state of the hardware acceleration for QEMU virtual graphics. - */ - public QemuDDAccelMeta( boolean enabled ) - { - this.enabled = enabled; - } - - /** - * Returns state of the hardware acceleration of QEMU virtual graphics. - * - * @return state of the hardware acceleration for QEMU virtual graphics. - */ - public boolean isEnabled() - { - return this.enabled; - } -} - -/** - * Metadata to describe the version of a QEMU virtual machine configuration. - * - * @author Manuel Bentele - * @version 1.0 - */ -class QemuHWVersionMeta -{ - /** - * Stores the version of a QEMU virtual machine configuration. - */ - private final int version; - - /** - * Creates metadata to describe the version of a QEMU virtual machine configuration. - * - * @param version version of the QEMU virtual machine configuration. - */ - public QemuHWVersionMeta( int version ) - { - this.version = version; - } - - /** - * Returns version of the QEMU virtual machine configuration. - * - * @return version of the QEMU virtual machine configuration. - */ - public int getVersion() - { - return this.version; - } -} - -/** - * Metadata to describe the hardware type of a QEMU ethernet device. - * - * @author Manuel Bentele - * @version 1.0 - */ -class QemuEthernetDevTypeMeta -{ - /** - * Stores the hardware model of the QEMU ethernet device. - */ - private final Interface.Model model; - - /** - * Creates metadata to describe the hardware type of a QEMU ethernet device. - * - * @param model hardware type of the QEMU ethernet device. - */ - public QemuEthernetDevTypeMeta( Interface.Model model ) - { - this.model = model; - } - - /** - * Returns the hardware type of a QEMU ethernet device. - * - * @return hardware type of the QEMU ethernet device. - */ - public Interface.Model getModel() - { - return this.model; - } -} - -/** - * Metadata to describe a QEMU USB controller. - * - * @author Manuel Bentele - * @version 1.0 - */ -class QemuUsbSpeedMeta -{ - /** - * Stores the USB speed of the QEMU USB controller. - */ - private final int speed; - - /** - * Stores the QEMU hardware model of the USB controller. - */ - private final ControllerUsb.Model model; - - /** - * Creates metadata to describe a QEMU USB controller. - * - * @param speed USB speed of the QEMU USB controller. - * @param model QEMU hardware model of the USB controller. - */ - public QemuUsbSpeedMeta( int speed, ControllerUsb.Model model ) - { - this.speed = speed; - this.model = model; - } - - /** - * Returns the speed of the QEMU USB controller. - * - * @return speed of the QEMU USB controller. - */ - public int getSpeed() - { - return this.speed; - } - - /** - * Returns QEMU hardware model of the USB controller. - * - * @return hardware model of the QEMU USB controller. - */ - public ControllerUsb.Model getModel() - { - return this.model; - } -} - -/** - * Virtual machine configuration (managed by Libvirt) for the QEMU hypervisor. - * - * @author Manuel Bentele - * @version 1.0 - */ -public class QemuMetaData extends - VmMetaData -{ - /** - * Name of the network bridge for the LAN. - */ - public static final String NETWORK_BRIDGE_LAN_DEFAULT = "br0"; - - /** - * Name of the network bridge for the default NAT network. - */ - public static final String NETWORK_BRIDGE_NAT_DEFAULT = "nat1"; - - /** - * Name of the network for the isolated host network (host only). - */ - public static final String NETWORK_BRIDGE_HOST_ONLY_DEFAULT = "vsw2"; - - /** - * Default physical CDROM drive of the hypervisor host. - */ - public static final String CDROM_DEFAULT_PHYSICAL_DRIVE = "/dev/sr0"; - - /** - * List of supported image formats by the QEMU hypervisor. - */ - private static final List SUPPORTED_IMAGE_FORMATS = Collections.unmodifiableList( - Arrays.asList( ImageFormat.QCOW2, ImageFormat.VMDK, ImageFormat.VDI ) ); - - /** - * Representation of a QEMU hypervisor (managed by Libvirt). - */ - private static final Virtualizer VIRTUALIZER = new Virtualizer( TConst.VIRT_QEMU, "QEMU" ); - - /** - * Libvirt XML configuration file to modify configuration of virtual machine for QEMU. - */ - private Domain vmConfig = null; - - /** - * Stores current index of added HDD device to the Libvirt XML configuration file. - */ - private int vmDeviceIndexHddAdd = 0; - - /** - * Stores current index of added CDROM device to the Libvirt XML configuration file. - */ - private int vmDeviceIndexCdromAdd = 0; - - /** - * Stores current index of added ethernet device to the Libvirt XML configuration file. - */ - private int vmDeviceIndexEthernetAdd = 0; - - /** - * Creates new virtual machine configuration (managed by Libvirt) for the QEMU hypervisor. - * - * @param osList list of operating systems. - * @param file image file for the QEMU hypervisor. - * - * @throws UnsupportedVirtualizerFormatException Libvirt XML configuration cannot be processed. - */ - public QemuMetaData( List osList, File file ) throws UnsupportedVirtualizerFormatException - { - super( osList ); - - try { - // read and parse Libvirt domain XML configuration document - this.vmConfig = new Domain( file ); - } catch ( LibvirtXmlDocumentException | LibvirtXmlSerializationException | LibvirtXmlValidationException e ) { - throw new UnsupportedVirtualizerFormatException( e.getLocalizedMessage() ); - } - - // parse VM config and initialize fields of QemuMetaData class - this.parseVmConfig(); - } - - /** - * Creates new virtual machine configuration (managed by Libvirt) for the QEMU hypervisor. - * - * @param osList list of operating systems. - * @param vmContent file content for the QEMU hypervisor. - * @param length number of bytes of the file content. - * - * @throws UnsupportedVirtualizerFormatException Libvirt XML configuration cannot be processed. - */ - public QemuMetaData( List osList, byte[] vmContent, int length ) - throws UnsupportedVirtualizerFormatException - { - super( osList ); - - try { - // read and parse Libvirt domain XML configuration document - this.vmConfig = new Domain( new String( vmContent ) ); - } catch ( LibvirtXmlDocumentException | LibvirtXmlSerializationException | LibvirtXmlValidationException e ) { - throw new UnsupportedVirtualizerFormatException( e.getLocalizedMessage() ); - } - - // parse VM config and initialize fields of QemuMetaData class - this.parseVmConfig(); - } - - /** - * Parses Libvirt domain XML configuration to initialize QEMU metadata. - */ - private void parseVmConfig() - { - // set display name of VM - this.displayName = vmConfig.getName(); - - // this property cannot be checked with the Libvirt domain XML configuration - // to check if machine is in a paused/suspended state, look in the QEMU qcow2 image for snapshots and machine states - this.isMachineSnapshot = false; - - // add HDDs, SSDs to QEMU metadata - for ( DiskStorage storageDiskDevice : this.vmConfig.getDiskStorageDevices() ) { - this.addHddMetaData( storageDiskDevice ); - } - - // start of privacy filters to filter out sensitive information like name of users in absolute paths, ... - // removes all referenced storage files of all specified CDROMs, Floppy drives and HDDs - this.vmConfig.removeDiskDevicesStorage(); - } - - /** - * Adds an existing and observed storage disk device to the HDD metadata. - * - * @param storageDiskDevice existing and observed storage disk that should be added to the - * metadata. - */ - private void addHddMetaData( DiskStorage storageDiskDevice ) - { - String hddChipsetModel = null; - DriveBusType hddChipsetBus = QemuMetaDataUtils.convertBusType( storageDiskDevice.getBusType() ); - String hddImagePath = storageDiskDevice.getStorageSource(); - - this.hdds.add( new HardDisk( hddChipsetModel, hddChipsetBus, hddImagePath ) ); - } - - @Override - public byte[] getFilteredDefinitionArray() - { - // removes all specified boot order entries - this.vmConfig.removeBootOrder(); - - // removes all source networks of all specified network interfaces - this.vmConfig.removeInterfaceDevicesSource(); - - // output filtered Libvirt domain XML configuration - String configuration = this.vmConfig.toString(); - return configuration.getBytes( StandardCharsets.UTF_8 ); - } - - @Override - public List getSupportedImageFormats() - { - return QemuMetaData.SUPPORTED_IMAGE_FORMATS; - } - - @Override - public void applySettingsForLocalEdit() - { - // NOT implemented yet - } - - @Override - public boolean addHddTemplate( File diskImage, String hddMode, String redoDir ) - { - return this.addHddTemplate( diskImage.getAbsolutePath(), hddMode, redoDir ); - } - - @Override - public boolean addHddTemplate( String diskImagePath, String hddMode, String redoDir ) - { - return this.addHddTemplate( this.vmDeviceIndexHddAdd++, diskImagePath, hddMode, redoDir ); - } - - /** - * Adds hard disk drive (HDD) to the QEMU virtual machine configuration. - * - * @param index current index of HDD to be added to the virtual machine configuration. - * @param diskImagePath path to the virtual disk image for the HDD. - * @param hddMode operation mode of the HDD. - * @param redoDir directory for the redo log if an independent non-persistent - * hddMode is set. - * @return result state of adding the HDD. - */ - public boolean addHddTemplate( int index, String diskImagePath, String hddMode, String redoDir ) - { - ArrayList storageDiskDevices = this.vmConfig.getDiskStorageDevices(); - DiskStorage storageDiskDevice = QemuMetaDataUtils.getArrayIndex( storageDiskDevices, index ); - - if ( storageDiskDevice == null ) { - // HDD does not exist, so create new storage (HDD) device - storageDiskDevice = this.vmConfig.addDiskStorageDevice(); - storageDiskDevice.setReadOnly( false ); - storageDiskDevice.setBusType( BusType.VIRTIO ); - String targetDevName = QemuMetaDataUtils.createAlphabeticalDeviceName( "vd", index ); - storageDiskDevice.setTargetDevice( targetDevName ); - storageDiskDevice.setStorage( StorageType.FILE, diskImagePath ); - - // add new created HDD to the metadata of the QemuMetaData object, too - this.addHddMetaData( storageDiskDevice ); - } else { - // HDD exists, so update existing storage (HDD) device - storageDiskDevice.setStorage( StorageType.FILE, diskImagePath ); - } - - return false; - } - - @Override - public boolean addDefaultNat() - { - return this.addEthernet( EtherType.NAT ); - } - - @Override - public void setOs( String vendorOsId ) - { - this.setOs( vendorOsId ); - } - - @Override - public boolean addDisplayName( String name ) - { - this.vmConfig.setName( name ); - - final boolean statusName = this.vmConfig.getName().equals( name ); - - return statusName; - } - - @Override - public boolean addRam( int mem ) - { - BigInteger memory = BigInteger.valueOf( mem ); - - this.vmConfig.setMemory( memory ); - this.vmConfig.setCurrentMemory( memory ); - - final boolean isMemorySet = this.vmConfig.getMemory().toString().equals( memory.toString() ); - final boolean isCurrentMemorySet = this.vmConfig.getCurrentMemory().toString().equals( memory.toString() ); - - return isMemorySet && isCurrentMemorySet; - } - - @Override - public void addFloppy( int index, String image, boolean readOnly ) - { - ArrayList floppyDiskDevices = this.vmConfig.getDiskFloppyDevices(); - DiskFloppy floppyDiskDevice = QemuMetaDataUtils.getArrayIndex( floppyDiskDevices, index ); - - if ( floppyDiskDevice == null ) { - // floppy device does not exist, so create new floppy device - floppyDiskDevice = this.vmConfig.addDiskFloppyDevice(); - floppyDiskDevice.setBusType( BusType.FDC ); - String targetDevName = QemuMetaDataUtils.createAlphabeticalDeviceName( "fd", index ); - floppyDiskDevice.setTargetDevice( targetDevName ); - floppyDiskDevice.setReadOnly( readOnly ); - floppyDiskDevice.setStorage( StorageType.FILE, image ); - } else { - // floppy device exists, so update existing floppy device - floppyDiskDevice.setReadOnly( readOnly ); - floppyDiskDevice.setStorage( StorageType.FILE, image ); - } - } - - @Override - public boolean addCdrom( String image ) - { - return this.addCdrom( this.vmDeviceIndexCdromAdd++, image ); - } - - /** - * Adds CDROM drive to the QEMU virtual machine configuration. - * - * @param index current index of CDROM drive to be added to the virtual machine configuration. - * @param image path to a virtual image that will be inserted as CDROM into the drive. - * @return result state of adding the CDROM drive. - */ - public boolean addCdrom( int index, String image ) - { - ArrayList cdromDiskDevices = this.vmConfig.getDiskCdromDevices(); - DiskCdrom cdromDiskDevice = QemuMetaDataUtils.getArrayIndex( cdromDiskDevices, index ); - - if ( cdromDiskDevice == null ) { - // CDROM device does not exist, so create new CDROM device - cdromDiskDevice = this.vmConfig.addDiskCdromDevice(); - cdromDiskDevice.setBusType( BusType.SATA ); - String targetDevName = QemuMetaDataUtils.createAlphabeticalDeviceName( "sd", index ); - cdromDiskDevice.setTargetDevice( targetDevName ); - cdromDiskDevice.setReadOnly( true ); - - if ( image == null ) { - cdromDiskDevice.setStorage( StorageType.BLOCK, CDROM_DEFAULT_PHYSICAL_DRIVE ); - } else { - cdromDiskDevice.setStorage( StorageType.FILE, image ); - } - } else { - // CDROM device exists, so update existing CDROM device - cdromDiskDevice.setReadOnly( true ); - - if ( image == null ) { - cdromDiskDevice.setStorage( StorageType.BLOCK, CDROM_DEFAULT_PHYSICAL_DRIVE ); - } else { - cdromDiskDevice.setStorage( StorageType.FILE, image ); - } - } - - return false; - } - - @Override - public boolean addCpuCoreCount( int nrOfCores ) - { - this.vmConfig.setVCpu( nrOfCores ); - - boolean isVCpuSet = this.vmConfig.getVCpu() == nrOfCores; - - return isVCpuSet; - } - - @Override - public void setSoundCard( SoundCardType type ) - { - QemuSoundCardMeta soundDeviceConfig = this.soundCards.get( type ); - ArrayList soundDevices = this.vmConfig.getSoundDevices(); - Sound.Model soundDeviceModel = soundDeviceConfig.getModel(); - - if ( soundDevices.isEmpty() ) { - // create new sound device with 'soundDeviceModel' hardware - Sound soundDevice = this.vmConfig.addSoundDevice(); - soundDevice.setModel( soundDeviceModel ); - } else { - // update sound device model type of existing sound devices - for ( Sound soundDevice : soundDevices ) { - soundDevice.setModel( soundDeviceModel ); - } - } - } - - @Override - public SoundCardType getSoundCard() - { - ArrayList soundDevices = this.vmConfig.getSoundDevices(); - SoundCardType soundDeviceType = SoundCardType.DEFAULT; - - if ( soundDevices.isEmpty() ) { - // the VM configuration does not contain a sound card device - soundDeviceType = SoundCardType.NONE; - } else { - // the VM configuration at least one sound card device, so return the type of the first one - Sound.Model soundDeviceModel = soundDevices.get( 0 ).getModel(); - soundDeviceType = QemuMetaDataUtils.convertSoundDeviceModel( soundDeviceModel ); - } - - return soundDeviceType; - } - - @Override - public void setDDAcceleration( DDAcceleration type ) - { - QemuDDAccelMeta accelerationConfig = this.ddacc.get( type ); - ArrayList graphicDevices = this.vmConfig.getGraphicDevices(); - ArrayList

+ + +
+ + +
+ + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + + +
+ + + + + + + + + + + +
+ + + +
+ + +
+ + + + + + + + +
+ +