diff options
Diffstat (limited to 'core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/plugin')
16 files changed, 914 insertions, 5 deletions
diff --git a/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/plugin/qemu/App.java b/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/plugin/qemu/App.java index 8a70be33..bf76ebf2 100644 --- a/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/plugin/qemu/App.java +++ b/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/plugin/qemu/App.java @@ -1,11 +1,38 @@ package org.openslx.runvirt.plugin.qemu; +import java.io.File; + import org.apache.log4j.BasicConfigurator; import org.apache.log4j.LogManager; import org.apache.log4j.Logger; +import org.openslx.libvirt.domain.Domain; +import org.openslx.libvirt.xml.LibvirtXmlDocumentException; +import org.openslx.libvirt.xml.LibvirtXmlSerializationException; +import org.openslx.libvirt.xml.LibvirtXmlValidationException; +import org.openslx.runvirt.configuration.FilterException; +import org.openslx.runvirt.configuration.FilterManager; import org.openslx.runvirt.plugin.qemu.cmdln.CommandLineArgs; import org.openslx.runvirt.plugin.qemu.cmdln.CommandLineArgs.CmdLnOption; import org.openslx.runvirt.plugin.qemu.cmdln.CommandLineArgsException; +import org.openslx.runvirt.plugin.qemu.configuration.FilterGenericCpu; +import org.openslx.runvirt.plugin.qemu.configuration.FilterGenericDiskCdromDevices; +import org.openslx.runvirt.plugin.qemu.configuration.FilterGenericDiskFloppyDevices; +import org.openslx.runvirt.plugin.qemu.configuration.FilterGenericDiskStorageDevices; +import org.openslx.runvirt.plugin.qemu.configuration.FilterGenericFileSystemDevices; +import org.openslx.runvirt.plugin.qemu.configuration.FilterGenericInterfaceDevices; +import org.openslx.runvirt.plugin.qemu.configuration.FilterGenericMemory; +import org.openslx.runvirt.plugin.qemu.configuration.FilterGenericName; +import org.openslx.runvirt.plugin.qemu.configuration.FilterGenericParallelDevices; +import org.openslx.runvirt.plugin.qemu.configuration.FilterSpecificQemuSerialDevices; +import org.openslx.runvirt.plugin.qemu.configuration.FilterGenericUuid; +import org.openslx.runvirt.plugin.qemu.configuration.FilterSpecificQemuArchitecture; +import org.openslx.runvirt.plugin.qemu.configuration.FilterSpecificQemuNvidiaGpuPassthrough; +import org.openslx.runvirt.plugin.qemu.virtualization.LibvirtHypervisorQemu; +import org.openslx.runvirt.plugin.qemu.virtualization.LibvirtHypervisorQemu.QemuSessionType; +import org.openslx.runvirt.virtualization.LibvirtHypervisor; +import org.openslx.runvirt.virtualization.LibvirtHypervisorException; +import org.openslx.runvirt.virtualization.LibvirtVirtualMachine; +import org.openslx.runvirt.virtualization.LibvirtVirtualMachineException; /** * Run-virt QEMU plugin (command line tool) to finalize a Libvirt domain XML configuration. @@ -65,6 +92,100 @@ public class App // print command line arguments for debugging purposes App.printCmdLnArgs( cmdLn ); + // create connection to the QEMU hypervisor via Libvirt + LibvirtHypervisor hypervisor = null; + try { + hypervisor = new LibvirtHypervisorQemu( QemuSessionType.LOCAL_USER_SESSION ); + } catch ( LibvirtHypervisorException e ) { + LOGGER.error( "Failed to connect to the QEMU virtualizer (Libvirt daemon): " + e.getLocalizedMessage() ); + System.exit( 2 ); + } + + // read Libvirt XML domain configuration template + final String xmlInputFileName = cmdLn.getVmCfgInpFileName(); + Domain config = null; + try { + final File xmlInputFile = new File( xmlInputFileName ); + config = new Domain( xmlInputFile ); + } catch ( LibvirtXmlDocumentException | LibvirtXmlSerializationException | LibvirtXmlValidationException e ) { + LOGGER.error( "Failed to read VM input configuration file: " + e.getLocalizedMessage() ); + hypervisor.close(); + System.exit( 3 ); + } + + // create filter manager to finalize VM configuration + final FilterManager<Domain, CommandLineArgs> filterManager; + filterManager = new FilterManager<Domain, CommandLineArgs>( config, cmdLn ); + + // register necessary filters to finalize configuration template + filterManager.register( new FilterGenericName(), true ); + filterManager.register( new FilterGenericUuid(), true ); + filterManager.register( new FilterGenericCpu(), true ); + filterManager.register( new FilterGenericMemory(), true ); + filterManager.register( new FilterGenericDiskStorageDevices(), true ); + filterManager.register( new FilterGenericDiskCdromDevices(), true ); + filterManager.register( new FilterGenericDiskFloppyDevices(), true ); + filterManager.register( new FilterGenericInterfaceDevices(), true ); + filterManager.register( new FilterGenericParallelDevices(), true ); + filterManager.register( new FilterGenericFileSystemDevices(), true ); + + // register QEMU specific filters to finalize configuration template + if ( hypervisor instanceof LibvirtHypervisorQemu ) { + final LibvirtHypervisorQemu hypervisorQemu = LibvirtHypervisorQemu.class.cast( hypervisor ); + + filterManager.register( new FilterSpecificQemuArchitecture( hypervisorQemu ), true ); + filterManager.register( new FilterSpecificQemuSerialDevices( hypervisorQemu ), true ); + filterManager.register( new FilterSpecificQemuNvidiaGpuPassthrough( hypervisorQemu ), false ); + } + + // finalize Libvirt VM configuration template + try { + filterManager.filterAll(); + } catch ( FilterException e ) { + LOGGER.error( "Failed to finalize VM configuration file: " + e.getLocalizedMessage() ); + hypervisor.close(); + System.exit( 4 ); + } + + // write finalized configuration to file if output file is specified + final String xmlOutputFileName = cmdLn.getVmCfgOutFileName(); + if ( xmlOutputFileName != null && !xmlOutputFileName.isEmpty() ) { + try { + final File xmlOutputFile = new File( xmlOutputFileName ); + config.toXml( xmlOutputFile ); + } catch ( LibvirtXmlSerializationException e ) { + LOGGER.error( "Failed to write VM output configuration file: " + e.getLocalizedMessage() ); + hypervisor.close(); + System.exit( 5 ); + } + } + + // define QEMU VM from finalized configuration + LibvirtVirtualMachine vm = null; + try { + vm = hypervisor.registerVm( config ); + } catch ( LibvirtHypervisorException e ) { + LOGGER.error( "Failed to define VM from configuration file: " + e.getLocalizedMessage() ); + hypervisor.close(); + System.exit( 6 ); + } + + try { + vm.start(); + } catch ( LibvirtVirtualMachineException e ) { + LOGGER.error( "Failed to start defined VM: " + e.getLocalizedMessage() ); + try { + hypervisor.deregisterVm( vm ); + } catch ( LibvirtHypervisorException | LibvirtVirtualMachineException e1 ) { + LOGGER.error( "Failed to undefine VM: " + e.getLocalizedMessage() ); + } + hypervisor.close(); + System.exit( 7 ); + } + + // close connection and let VM be running + hypervisor.close(); + // return with successful exit code System.exit( 0 ); } diff --git a/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/plugin/qemu/cmdln/CommandLineArgs.java b/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/plugin/qemu/cmdln/CommandLineArgs.java index 13b8c0e2..7e1a7a22 100644 --- a/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/plugin/qemu/cmdln/CommandLineArgs.java +++ b/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/plugin/qemu/cmdln/CommandLineArgs.java @@ -289,13 +289,43 @@ public class CommandLineArgs } /** - * Returns the argument of the command line option {@link CmdLnOption#VM_AUDIO0}. + * Returns the argument of the command line option {@link CmdLnOption#VM_FSSRC0}. * - * @return argument of the command line option {@link CmdLnOption#VM_AUDIO0}. + * @return argument of the command line option {@link CmdLnOption#VM_FSSRC0}. */ - public String getVmModelSoundCard0() + public String getVmFsSrc0() { - return this.getArgument( CmdLnOption.VM_AUDIO0 ); + return this.getArgument( CmdLnOption.VM_FSSRC0 ); + } + + /** + * Returns the argument of the command line option {@link CmdLnOption#VM_FSTGT0}. + * + * @return argument of the command line option {@link CmdLnOption#VM_FSTGT0}. + */ + public String getVmFsTgt0() + { + return this.getArgument( CmdLnOption.VM_FSTGT0 ); + } + + /** + * Returns the argument of the command line option {@link CmdLnOption#VM_FSSRC1}. + * + * @return argument of the command line option {@link CmdLnOption#VM_FSSRC1}. + */ + public String getVmFsSrc1() + { + return this.getArgument( CmdLnOption.VM_FSSRC1 ); + } + + /** + * Returns the argument of the command line option {@link CmdLnOption#VM_FSTGT1}. + * + * @return argument of the command line option {@link CmdLnOption#VM_FSTGT1}. + */ + public String getVmFsTgt1() + { + return this.getArgument( CmdLnOption.VM_FSTGT1 ); } /** @@ -324,7 +354,10 @@ public class CommandLineArgs VM_PARALLEL0( 'p', "vmparallel0", true, "Device for the first parallel port interface" ), VM_SERIAL0 ( 'q', "vmserial0", true, "Device for the first serial port interface" ), VM_MAC0 ( 'a', "vmmac0", true, "MAC address for the first network interface" ), - VM_AUDIO0 ( 'x', "vmaudio0", true, "Hardware model for the first sound card" ); + VM_FSSRC0 ( 't', "vmfssrc0", true, "Source directory for first file system passthrough (shared folder)" ), + VM_FSTGT0 ( 'u', "vmfstgt0", true, "Target directory for first file system passthrough (shared folder)" ), + VM_FSSRC1 ( 'v', "vmfssrc1", true, "Source directory for second file system passthrough (shared folder)" ), + VM_FSTGT1 ( 'w', "vmfstgt1", true, "Target directory for second file system passthrough (shared folder)" ); // @formatter:on /** diff --git a/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/plugin/qemu/configuration/FilterGenericCpu.java b/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/plugin/qemu/configuration/FilterGenericCpu.java new file mode 100644 index 00000000..1fbcd568 --- /dev/null +++ b/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/plugin/qemu/configuration/FilterGenericCpu.java @@ -0,0 +1,26 @@ +package org.openslx.runvirt.plugin.qemu.configuration; + +import org.openslx.libvirt.domain.Domain; +import org.openslx.libvirt.domain.Domain.CpuCheck; +import org.openslx.libvirt.domain.Domain.CpuMode; +import org.openslx.runvirt.configuration.FilterException; +import org.openslx.runvirt.configuration.FilterGeneric; +import org.openslx.runvirt.plugin.qemu.cmdln.CommandLineArgs; + +public class FilterGenericCpu extends FilterGeneric<Domain, CommandLineArgs> +{ + private static final String FILTER_NAME = "CPU [number of cores, mode, ...]"; + + public FilterGenericCpu() + { + super( FilterGenericCpu.FILTER_NAME ); + } + + @Override + public void filter( Domain config, CommandLineArgs args ) throws FilterException + { + config.setVCpu( args.getVmNumCpus() ); + config.setCpuMode( CpuMode.HOST_PASSTHROUGH ); + config.setCpuCheck( CpuCheck.PARTIAL ); + } +} diff --git a/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/plugin/qemu/configuration/FilterGenericDiskCdromDevices.java b/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/plugin/qemu/configuration/FilterGenericDiskCdromDevices.java new file mode 100644 index 00000000..a17f3a8b --- /dev/null +++ b/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/plugin/qemu/configuration/FilterGenericDiskCdromDevices.java @@ -0,0 +1,54 @@ +package org.openslx.runvirt.plugin.qemu.configuration; + +import java.util.ArrayList; + +import org.openslx.libvirt.domain.Domain; +import org.openslx.libvirt.domain.device.Disk.StorageType; +import org.openslx.libvirt.domain.device.DiskCdrom; +import org.openslx.runvirt.configuration.FilterException; +import org.openslx.runvirt.configuration.FilterGeneric; +import org.openslx.runvirt.plugin.qemu.cmdln.CommandLineArgs; +import org.openslx.vm.QemuMetaData; +import org.openslx.vm.QemuMetaDataUtils; + +public class FilterGenericDiskCdromDevices extends FilterGeneric<Domain, CommandLineArgs> +{ + private static final String FILTER_NAME = "Disk CDROM devices"; + + public FilterGenericDiskCdromDevices() + { + super( FilterGenericDiskCdromDevices.FILTER_NAME ); + } + + private void filterDiskCdromDevice( Domain config, String fileName, int index ) throws FilterException + { + final ArrayList<DiskCdrom> devices = config.getDiskCdromDevices(); + final DiskCdrom disk = QemuMetaDataUtils.getArrayIndex( devices, index ); + + if ( disk != null ) { + if ( fileName == null ) { + // do not remove disk CDROM drive, but set local physical drive as input source + disk.setStorage( StorageType.BLOCK, QemuMetaData.CDROM_DEFAULT_PHYSICAL_DRIVE ); + } else if ( fileName.equals( "" ) ) { + // remove storage source if empty string is specified to emulate an empty CDROM drive + disk.removeStorage(); + } else { + // set disk image file as storage source of the disk CDROM drive + disk.setStorage( StorageType.FILE, fileName ); + } + } + } + + @Override + public void filter( Domain config, CommandLineArgs args ) throws FilterException + { + this.filterDiskCdromDevice( config, args.getVmDiskFileNameCdrom0(), 0 ); + this.filterDiskCdromDevice( config, args.getVmDiskFileNameCdrom1(), 1 ); + + // remove all additional disk CDROM devices + final ArrayList<DiskCdrom> devices = config.getDiskCdromDevices(); + for ( int i = 2; i < devices.size(); i++ ) { + devices.get( i ).remove(); + } + } +} diff --git a/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/plugin/qemu/configuration/FilterGenericDiskFloppyDevices.java b/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/plugin/qemu/configuration/FilterGenericDiskFloppyDevices.java new file mode 100644 index 00000000..106e499e --- /dev/null +++ b/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/plugin/qemu/configuration/FilterGenericDiskFloppyDevices.java @@ -0,0 +1,50 @@ +package org.openslx.runvirt.plugin.qemu.configuration; + +import java.util.ArrayList; + +import org.openslx.libvirt.domain.Domain; +import org.openslx.libvirt.domain.device.Disk.StorageType; +import org.openslx.libvirt.domain.device.DiskFloppy; +import org.openslx.runvirt.configuration.FilterException; +import org.openslx.runvirt.configuration.FilterGeneric; +import org.openslx.runvirt.plugin.qemu.cmdln.CommandLineArgs; +import org.openslx.vm.QemuMetaDataUtils; + +public class FilterGenericDiskFloppyDevices extends FilterGeneric<Domain, CommandLineArgs> +{ + private static final String FILTER_NAME = "Disk floppy devices"; + + public FilterGenericDiskFloppyDevices() + { + super( FilterGenericDiskFloppyDevices.FILTER_NAME ); + } + + private void filterDiskFloppyDevice( Domain config, String fileName, int index ) throws FilterException + { + final ArrayList<DiskFloppy> devices = config.getDiskFloppyDevices(); + final DiskFloppy disk = QemuMetaDataUtils.getArrayIndex( devices, index ); + + if ( disk != null ) { + if ( fileName == null ) { + // remove disk floppy device if disk image file name is not set + disk.remove(); + } else { + // set image file of disk storage if disk floppy device is available + disk.setStorage( StorageType.FILE, fileName ); + } + } + } + + @Override + public void filter( Domain config, CommandLineArgs args ) throws FilterException + { + this.filterDiskFloppyDevice( config, args.getVmDiskFileNameFloppy0(), 0 ); + this.filterDiskFloppyDevice( config, args.getVmDiskFileNameFloppy1(), 1 ); + + // remove all additional disk storage devices + final ArrayList<DiskFloppy> devices = config.getDiskFloppyDevices(); + for ( int i = 2; i < devices.size(); i++ ) { + devices.get( i ).remove(); + } + } +} diff --git a/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/plugin/qemu/configuration/FilterGenericDiskStorageDevices.java b/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/plugin/qemu/configuration/FilterGenericDiskStorageDevices.java new file mode 100644 index 00000000..0dcfddf9 --- /dev/null +++ b/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/plugin/qemu/configuration/FilterGenericDiskStorageDevices.java @@ -0,0 +1,49 @@ +package org.openslx.runvirt.plugin.qemu.configuration; + +import java.util.ArrayList; + +import org.openslx.libvirt.domain.Domain; +import org.openslx.libvirt.domain.device.Disk.StorageType; +import org.openslx.libvirt.domain.device.DiskStorage; +import org.openslx.runvirt.configuration.FilterException; +import org.openslx.runvirt.configuration.FilterGeneric; +import org.openslx.runvirt.plugin.qemu.cmdln.CommandLineArgs; +import org.openslx.vm.QemuMetaDataUtils; + +public class FilterGenericDiskStorageDevices extends FilterGeneric<Domain, CommandLineArgs> +{ + private static final String FILTER_NAME = "Disk storage devices [HDD, SSD, ...]"; + + public FilterGenericDiskStorageDevices() + { + super( FilterGenericDiskStorageDevices.FILTER_NAME ); + } + + private void filterDiskStorageDevice( Domain config, String fileName, int index ) throws FilterException + { + final ArrayList<DiskStorage> devices = config.getDiskStorageDevices(); + final DiskStorage disk = QemuMetaDataUtils.getArrayIndex( devices, index ); + + if ( disk != null ) { + if ( fileName == null ) { + // remove disk storage device if disk image file name is not set + disk.remove(); + } else { + // set image file of disk storage if disk storage device is available + disk.setStorage( StorageType.FILE, fileName ); + } + } + } + + @Override + public void filter( Domain config, CommandLineArgs args ) throws FilterException + { + this.filterDiskStorageDevice( config, args.getVmDiskFileNameHDD0(), 0 ); + + // remove all additional disk storage devices + final ArrayList<DiskStorage> devices = config.getDiskStorageDevices(); + for ( int i = 1; i < devices.size(); i++ ) { + devices.get( i ).remove(); + } + } +} diff --git a/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/plugin/qemu/configuration/FilterGenericFileSystemDevices.java b/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/plugin/qemu/configuration/FilterGenericFileSystemDevices.java new file mode 100644 index 00000000..1d06474f --- /dev/null +++ b/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/plugin/qemu/configuration/FilterGenericFileSystemDevices.java @@ -0,0 +1,64 @@ +package org.openslx.runvirt.plugin.qemu.configuration; + +import java.util.ArrayList; + +import org.openslx.libvirt.domain.Domain; +import org.openslx.libvirt.domain.device.FileSystem; +import org.openslx.libvirt.domain.device.FileSystem.AccessMode; +import org.openslx.libvirt.domain.device.FileSystem.Type; +import org.openslx.runvirt.configuration.FilterException; +import org.openslx.runvirt.configuration.FilterGeneric; +import org.openslx.runvirt.plugin.qemu.cmdln.CommandLineArgs; +import org.openslx.vm.QemuMetaDataUtils; + +public class FilterGenericFileSystemDevices extends FilterGeneric<Domain, CommandLineArgs> +{ + private static final String FILTER_NAME = "File system devices"; + + public FilterGenericFileSystemDevices() + { + super( FilterGenericFileSystemDevices.FILTER_NAME ); + } + + private void filterFileSystemDevice( Domain config, String source, String target, int index ) throws FilterException + { + final ArrayList<FileSystem> devices = config.getFileSystemDevices(); + final FileSystem fileSystem = QemuMetaDataUtils.getArrayIndex( devices, index ); + + if ( fileSystem == null ) { + // check if file system device source directory is specified + if ( source != null && target != null ) { + // file system device does not exist, so create new file system device + final FileSystem newFileSystem = config.addFileSystemDevice(); + newFileSystem.setType( Type.MOUNT ); + newFileSystem.setAccessMode( AccessMode.MAPPED ); + newFileSystem.setSource( source ); + newFileSystem.setTarget( target ); + } + } else { + if ( source == null || target == null ) { + // remove file system device since device source or target is not specified + fileSystem.remove(); + } else { + // change type, access mode, source and target of existing file system device + fileSystem.setType( Type.MOUNT ); + fileSystem.setAccessMode( AccessMode.MAPPED ); + fileSystem.setSource( source ); + fileSystem.setTarget( target ); + } + } + } + + @Override + public void filter( Domain config, CommandLineArgs args ) throws FilterException + { + this.filterFileSystemDevice( config, args.getVmFsSrc0(), args.getVmFsTgt0(), 0 ); + this.filterFileSystemDevice( config, args.getVmFsSrc1(), args.getVmFsTgt1(), 1 ); + + // remove all additional file system devices + final ArrayList<FileSystem> devices = config.getFileSystemDevices(); + for ( int i = 1; i < devices.size(); i++ ) { + devices.get( i ).remove(); + } + } +} diff --git a/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/plugin/qemu/configuration/FilterGenericInterfaceDevices.java b/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/plugin/qemu/configuration/FilterGenericInterfaceDevices.java new file mode 100644 index 00000000..ea2782ba --- /dev/null +++ b/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/plugin/qemu/configuration/FilterGenericInterfaceDevices.java @@ -0,0 +1,48 @@ +package org.openslx.runvirt.plugin.qemu.configuration; + +import java.util.ArrayList; + +import org.openslx.libvirt.domain.Domain; +import org.openslx.libvirt.domain.device.Interface; +import org.openslx.runvirt.configuration.FilterException; +import org.openslx.runvirt.configuration.FilterGeneric; +import org.openslx.runvirt.plugin.qemu.cmdln.CommandLineArgs; +import org.openslx.vm.QemuMetaDataUtils; + +public class FilterGenericInterfaceDevices extends FilterGeneric<Domain, CommandLineArgs> +{ + private static final String FILTER_NAME = "Network interface devices"; + + public FilterGenericInterfaceDevices() + { + super( FilterGenericInterfaceDevices.FILTER_NAME ); + } + + private void filterInterfaceDevice( Domain config, String macAddress, int index ) throws FilterException + { + final ArrayList<Interface> devices = config.getInterfaceDevices(); + final Interface device = QemuMetaDataUtils.getArrayIndex( devices, index ); + + if ( device != null ) { + if ( macAddress == null ) { + // remove network interface device if MAC address is not set + device.remove(); + } else { + // set MAC address of network interface device if network interface device is available + device.setMacAddress( macAddress ); + } + } + } + + @Override + public void filter( Domain config, CommandLineArgs args ) throws FilterException + { + this.filterInterfaceDevice( config, args.getVmMacAddress0(), 0 ); + + // remove all additional disk storage devices + final ArrayList<Interface> devices = config.getInterfaceDevices(); + for ( int i = 1; i < devices.size(); i++ ) { + devices.get( i ).remove(); + } + } +} diff --git a/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/plugin/qemu/configuration/FilterGenericMemory.java b/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/plugin/qemu/configuration/FilterGenericMemory.java new file mode 100644 index 00000000..d8d901d4 --- /dev/null +++ b/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/plugin/qemu/configuration/FilterGenericMemory.java @@ -0,0 +1,28 @@ +package org.openslx.runvirt.plugin.qemu.configuration; + +import java.math.BigInteger; + +import org.openslx.libvirt.domain.Domain; +import org.openslx.libvirt.domain.DomainUtils; +import org.openslx.runvirt.configuration.FilterException; +import org.openslx.runvirt.configuration.FilterGeneric; +import org.openslx.runvirt.plugin.qemu.cmdln.CommandLineArgs; + +public class FilterGenericMemory extends FilterGeneric<Domain, CommandLineArgs> +{ + private static final String FILTER_NAME = "Memory [normal, current (balloning)]"; + + public FilterGenericMemory() + { + super( FilterGenericMemory.FILTER_NAME ); + } + + @Override + public void filter( Domain config, CommandLineArgs args ) throws FilterException + { + BigInteger memory = DomainUtils.decodeMemory( args.getVmMemory(), "MiB" ); + + config.setMemory( memory ); + config.setCurrentMemory( memory ); + } +} diff --git a/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/plugin/qemu/configuration/FilterGenericName.java b/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/plugin/qemu/configuration/FilterGenericName.java new file mode 100644 index 00000000..00644b23 --- /dev/null +++ b/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/plugin/qemu/configuration/FilterGenericName.java @@ -0,0 +1,23 @@ +package org.openslx.runvirt.plugin.qemu.configuration; + +import org.openslx.libvirt.domain.Domain; +import org.openslx.runvirt.configuration.FilterException; +import org.openslx.runvirt.configuration.FilterGeneric; +import org.openslx.runvirt.plugin.qemu.cmdln.CommandLineArgs; + +public class FilterGenericName extends FilterGeneric<Domain, CommandLineArgs> +{ + private static final String FILTER_NAME = "Name [(display) name]"; + + public FilterGenericName() + { + super( FilterGenericName.FILTER_NAME ); + } + + @Override + public void filter( Domain config, CommandLineArgs args ) throws FilterException + { + config.setName( args.getVmName() ); + config.setTitle( args.getVmDisplayName() ); + } +} diff --git a/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/plugin/qemu/configuration/FilterGenericParallelDevices.java b/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/plugin/qemu/configuration/FilterGenericParallelDevices.java new file mode 100644 index 00000000..458f69cb --- /dev/null +++ b/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/plugin/qemu/configuration/FilterGenericParallelDevices.java @@ -0,0 +1,58 @@ +package org.openslx.runvirt.plugin.qemu.configuration; + +import java.util.ArrayList; + +import org.openslx.libvirt.domain.Domain; +import org.openslx.libvirt.domain.device.Parallel; +import org.openslx.libvirt.domain.device.Parallel.Type; +import org.openslx.runvirt.configuration.FilterException; +import org.openslx.runvirt.configuration.FilterGeneric; +import org.openslx.runvirt.plugin.qemu.cmdln.CommandLineArgs; +import org.openslx.vm.QemuMetaDataUtils; + +public class FilterGenericParallelDevices extends FilterGeneric<Domain, CommandLineArgs> +{ + private static final String FILTER_NAME = "Parallel devices"; + + public FilterGenericParallelDevices() + { + super( FilterGenericParallelDevices.FILTER_NAME ); + } + + private void filterParallelDevice( Domain config, String fileName, int index ) throws FilterException + { + final ArrayList<Parallel> devices = config.getParallelDevices(); + final Parallel device = QemuMetaDataUtils.getArrayIndex( devices, index ); + + if ( device == null ) { + // check if device file name is specified + if ( fileName != null ) { + // parallel port device does not exist, so create new parallel port device + final Parallel newDevice = config.addParallelDevice(); + newDevice.setType( Type.DEV ); + newDevice.setSource( fileName ); + } + } else { + if ( fileName == null ) { + // remove device since device file is not specified + device.remove(); + } else { + // change type and source of existing parallel port device + device.setType( Type.DEV ); + device.setSource( fileName ); + } + } + } + + @Override + public void filter( Domain config, CommandLineArgs args ) throws FilterException + { + this.filterParallelDevice( config, args.getVmDeviceSerial0(), 0 ); + + // remove all additional parallel devices + final ArrayList<Parallel> devices = config.getParallelDevices(); + for ( int i = 1; i < devices.size(); i++ ) { + devices.get( i ).remove(); + } + } +} diff --git a/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/plugin/qemu/configuration/FilterGenericUuid.java b/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/plugin/qemu/configuration/FilterGenericUuid.java new file mode 100644 index 00000000..f10a32b7 --- /dev/null +++ b/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/plugin/qemu/configuration/FilterGenericUuid.java @@ -0,0 +1,22 @@ +package org.openslx.runvirt.plugin.qemu.configuration; + +import org.openslx.libvirt.domain.Domain; +import org.openslx.runvirt.configuration.FilterException; +import org.openslx.runvirt.configuration.FilterGeneric; +import org.openslx.runvirt.plugin.qemu.cmdln.CommandLineArgs; + +public class FilterGenericUuid extends FilterGeneric<Domain, CommandLineArgs> +{ + private static final String FILTER_NAME = "UUID"; + + public FilterGenericUuid() + { + super( FilterGenericUuid.FILTER_NAME ); + } + + @Override + public void filter( Domain config, CommandLineArgs args ) throws FilterException + { + config.setUuid( args.getVmUuid() ); + } +} diff --git a/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/plugin/qemu/configuration/FilterSpecificQemuArchitecture.java b/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/plugin/qemu/configuration/FilterSpecificQemuArchitecture.java new file mode 100644 index 00000000..0466b5e9 --- /dev/null +++ b/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/plugin/qemu/configuration/FilterSpecificQemuArchitecture.java @@ -0,0 +1,199 @@ +package org.openslx.runvirt.plugin.qemu.configuration; + +import java.util.ArrayList; +import java.util.List; + +import org.openslx.libvirt.capabilities.Capabilities; +import org.openslx.libvirt.capabilities.guest.Guest; +import org.openslx.libvirt.capabilities.guest.Machine; +import org.openslx.libvirt.domain.Domain; +import org.openslx.libvirt.domain.Domain.OsType; +import org.openslx.libvirt.domain.Domain.Type; +import org.openslx.runvirt.configuration.FilterException; +import org.openslx.runvirt.configuration.FilterSpecific; +import org.openslx.runvirt.plugin.qemu.cmdln.CommandLineArgs; +import org.openslx.runvirt.plugin.qemu.virtualization.LibvirtHypervisorQemu; +import org.openslx.runvirt.virtualization.LibvirtHypervisorException; + +public class FilterSpecificQemuArchitecture extends FilterSpecific<Domain, CommandLineArgs, LibvirtHypervisorQemu> +{ + private static final String FILTER_NAME = "QEMU Architecture [CPU architecture, machine type, ...]"; + + // used as instance of an singelton, always use getCapabilities to retrieve caps instance + private Capabilities capabilities = null; + + public FilterSpecificQemuArchitecture( LibvirtHypervisorQemu hypervisor ) + { + super( FilterSpecificQemuArchitecture.FILTER_NAME, hypervisor ); + } + + private Capabilities getCapabilities() throws FilterException + { + // retrieve capabilities from QEMU hypervisor only once + if ( this.capabilities == null ) { + try { + this.capabilities = this.getHypervisor().getCapabilites(); + } catch ( LibvirtHypervisorException e ) { + final String errorMsg = new String( + "Failed to get host capabilities from QEMU virtualizer: " + e.getLocalizedMessage() ); + throw new FilterException( errorMsg ); + } + } + + return this.capabilities; + } + + private Guest getTargetGuestFromArchName( String architectureName ) throws FilterException + { + final List<Guest> guests = this.getCapabilities().getGuests(); + Guest targetGuest = null; + + if ( architectureName == null ) { + return targetGuest; + } + + for ( Guest guest : guests ) { + final String guestArchitectureName = guest.getArchName(); + if ( architectureName.equals( guestArchitectureName ) ) { + targetGuest = guest; + break; + } + } + + return targetGuest; + } + + private Machine getTargetMachineFromGuest( Guest guest, String machineName ) throws FilterException + { + final List<Machine> machines = guest.getArchMachines(); + Machine targetMachine = null; + + if ( machineName == null ) { + return targetMachine; + } + + for ( Machine machine : machines ) { + if ( machineName.equals( machine.getName() ) ) { + targetMachine = machine; + break; + } + } + + return targetMachine; + } + + private List<String> getCanonicalNamesFromTargetMachines( Guest guest ) throws FilterException + { + final List<Machine> machines = guest.getArchMachines(); + final List<String> canonicalNames = new ArrayList<String>(); + + for ( Machine machine : machines ) { + final String canonicalName = machine.getCanonicalMachine(); + if ( canonicalName != null ) { + canonicalNames.add( canonicalName ); + } + } + + return canonicalNames; + } + + @Override + public void filter( Domain config, CommandLineArgs args ) throws FilterException + { + // get source architecture, machine- and OS type + final String sourceArchitectureName = config.getOsArch(); + final String sourceMachine = config.getOsMachine(); + final OsType sourceOsType = config.getOsType(); + final Type sourceDomainType = config.getType(); + + // check if source architecture is supported by one of the hypervisor's guests + Guest targetGuest = null; + if ( sourceArchitectureName == null ) { + final String errorMsg = new String( "Source architecture is not specified!" ); + throw new FilterException( errorMsg ); + } else { + targetGuest = this.getTargetGuestFromArchName( sourceArchitectureName ); + if ( targetGuest == null ) { + final String errorMsg = new String( "Source architecture is not supported by the virtualizer!" ); + throw new FilterException( errorMsg ); + } + } + + // check if source machine is supported by the hypervisor + Machine targetMachine = null; + if ( sourceMachine == null ) { + final String errorMsg = new String( "Source machine type is not specified!" ); + throw new FilterException( errorMsg ); + } else { + // get all possible machine type for supported source architecture + targetMachine = this.getTargetMachineFromGuest( targetGuest, sourceMachine ); + + if ( targetMachine == null ) { + // source machine is not directly supported by the hypervisor + // check if up- or downgraded version of the chipset is supported by the hypervisor + List<String> targetMachineCanonicalNames = this.getCanonicalNamesFromTargetMachines( targetGuest ); + + // retrieve overwrite chipset name from canonical machine names + String sourceMachineOverwrite = null; + for ( String targetMachineCanonicalName : targetMachineCanonicalNames ) { + if ( sourceMachine.contains( targetMachineCanonicalName ) ) { + sourceMachineOverwrite = targetMachineCanonicalName; + break; + } + } + + // if overwrite available, patch the machine type + if ( sourceMachineOverwrite != null ) { + config.setOsMachine( sourceMachineOverwrite ); + } else { + final String errorMsg = new String( "Source machine type is not supported by the virtualizer!" ); + throw new FilterException( errorMsg ); + } + } + } + + // check if source OS type is supported by the hypervisor's architecture + if ( sourceOsType == null ) { + final String errorMsg = new String( "OS type is not specified!" ); + throw new FilterException( errorMsg ); + } else { + if ( !sourceOsType.toString().equals( targetGuest.getOsType().toString() ) ) { + final String errorMsg = new String( "OS type is not supported by the virtualizer!" ); + throw new FilterException( errorMsg ); + } + } + + // check if source domain type is supported by the hypervisor's architecture + Type targetDomainType = null; + if ( sourceDomainType == null ) { + final String errorMsg = new String( "Source domain type is not specified!" ); + throw new FilterException( errorMsg ); + } else { + final List<org.openslx.libvirt.capabilities.guest.Domain> targetDomains = targetGuest.getArchDomains(); + + // retrieve supported domain type + for ( org.openslx.libvirt.capabilities.guest.Domain domain : targetDomains ) { + final Type domainType = domain.getType(); + if ( domainType == sourceDomainType ) { + targetDomainType = domainType; + break; + } + } + + // check supported domain type + if ( targetDomainType == null ) { + final String errorMsg = new String( "Source domain type is not supported by the virtualizer!" ); + throw new FilterException( errorMsg ); + } + } + + // patch path of QEMU emulator binary + final String archEmulator = targetGuest.getArchEmulator(); + if ( archEmulator == null ) { + final String errorMsg = new String( "Emulation of source architecture is not supported by the virtualizer!" ); + throw new FilterException( errorMsg ); + } else { + config.setDevicesEmulator( targetGuest.getArchEmulator() ); + } + } +} diff --git a/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/plugin/qemu/configuration/FilterSpecificQemuNvidiaGpuPassthrough.java b/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/plugin/qemu/configuration/FilterSpecificQemuNvidiaGpuPassthrough.java new file mode 100644 index 00000000..913eeb5a --- /dev/null +++ b/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/plugin/qemu/configuration/FilterSpecificQemuNvidiaGpuPassthrough.java @@ -0,0 +1,26 @@ +package org.openslx.runvirt.plugin.qemu.configuration; + +import org.openslx.libvirt.domain.Domain; +import org.openslx.runvirt.configuration.FilterException; +import org.openslx.runvirt.configuration.FilterSpecific; +import org.openslx.runvirt.plugin.qemu.cmdln.CommandLineArgs; +import org.openslx.runvirt.plugin.qemu.virtualization.LibvirtHypervisorQemu; + +public class FilterSpecificQemuNvidiaGpuPassthrough extends FilterSpecific<Domain, CommandLineArgs, LibvirtHypervisorQemu> +{ + private static final String FILTER_NAME = "QEMU GPU passthrough [Nvidia]"; + + public FilterSpecificQemuNvidiaGpuPassthrough( LibvirtHypervisorQemu hypervisor ) + { + super( FilterSpecificQemuNvidiaGpuPassthrough.FILTER_NAME, hypervisor ); + } + + @Override + public void filter( Domain config, CommandLineArgs args ) throws FilterException + { + // check if IOMMU support is available on the host + + // TODO: implement Nvidia hypervisor shadowing + // call this filter at the end, since -> override of software graphics to 'none' necessary + } +} diff --git a/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/plugin/qemu/configuration/FilterSpecificQemuSerialDevices.java b/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/plugin/qemu/configuration/FilterSpecificQemuSerialDevices.java new file mode 100644 index 00000000..258027b8 --- /dev/null +++ b/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/plugin/qemu/configuration/FilterSpecificQemuSerialDevices.java @@ -0,0 +1,69 @@ +package org.openslx.runvirt.plugin.qemu.configuration; + +import java.util.ArrayList; +import java.util.function.Predicate; +import java.util.stream.Collectors; + +import org.openslx.libvirt.domain.Domain; +import org.openslx.libvirt.domain.device.Serial.Type; +import org.openslx.libvirt.domain.device.Serial; +import org.openslx.runvirt.configuration.FilterException; +import org.openslx.runvirt.configuration.FilterSpecific; +import org.openslx.runvirt.plugin.qemu.cmdln.CommandLineArgs; +import org.openslx.runvirt.plugin.qemu.virtualization.LibvirtHypervisorQemu; +import org.openslx.vm.QemuMetaDataUtils; + +public class FilterSpecificQemuSerialDevices extends FilterSpecific<Domain, CommandLineArgs, LibvirtHypervisorQemu> +{ + private static final String FILTER_NAME = "Serial devices"; + + public FilterSpecificQemuSerialDevices( LibvirtHypervisorQemu hypervisor ) + { + super( FilterSpecificQemuSerialDevices.FILTER_NAME, hypervisor ); + } + + private ArrayList<Serial> getSerialDevDevices( Domain config ) + { + final ArrayList<Serial> devices = config.getSerialDevices(); + final Predicate<Serial> byDeviceTypeDev = device -> device.getType() == Type.DEV; + + return devices.stream().filter( byDeviceTypeDev ).collect( Collectors.toCollection( ArrayList::new ) ); + } + + private void filterSerialDevice( Domain config, String fileName, int index ) throws FilterException + { + final ArrayList<Serial> devices = this.getSerialDevDevices( config ); + final Serial device = QemuMetaDataUtils.getArrayIndex( devices, index ); + + if ( device == null ) { + // check if device file name is specified + if ( fileName != null ) { + // serial port device is not available, so create new serial port device + final Serial newDevice = config.addSerialDevice(); + newDevice.setType( Type.DEV ); + newDevice.setSource( fileName ); + } + } else { + if ( fileName == null ) { + // remove serial port device if device file name is not set + device.remove(); + } else { + // set type and source of existing serial port device + device.setType( Type.DEV ); + device.setSource( fileName ); + } + } + } + + @Override + public void filter( Domain config, CommandLineArgs args ) throws FilterException + { + this.filterSerialDevice( config, args.getVmDeviceSerial0(), 0 ); + + // remove all additional serial devices + final ArrayList<Serial> devices = this.getSerialDevDevices( config ); + for ( int i = 1; i < devices.size(); i++ ) { + devices.get( i ).remove(); + } + } +} diff --git a/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/plugin/qemu/virtualization/LibvirtHypervisorQemu.java b/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/plugin/qemu/virtualization/LibvirtHypervisorQemu.java new file mode 100644 index 00000000..260c3f4e --- /dev/null +++ b/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/plugin/qemu/virtualization/LibvirtHypervisorQemu.java @@ -0,0 +1,39 @@ +package org.openslx.runvirt.plugin.qemu.virtualization; + +import org.openslx.runvirt.virtualization.LibvirtHypervisor; +import org.openslx.runvirt.virtualization.LibvirtHypervisorException; + +public class LibvirtHypervisorQemu extends LibvirtHypervisor +{ + public LibvirtHypervisorQemu( QemuSessionType type ) throws LibvirtHypervisorException + { + super( type.getConnectionUri() ); + } + + public enum QemuSessionType + { + // @formatter:off + LOCAL_SYSTEM_SESSION( "qemu:///system" ), + LOCAL_USER_SESSION ( "qemu:///session" ); + // @formatter:on + + private final String connectionUri; + + QemuSessionType( String connectionUri ) + { + this.connectionUri = connectionUri; + } + + public String getConnectionUri() + { + return this.connectionUri; + } + + // TODO: + // Implement capabilities -> get host architecture => decision whether to emulate or use KVM? -> change domain of XML + // fill in given HDD file, CDROM, ... + // GPU-Passthrough: patch XML with hypervisor disable bit, ..., to get Nvidia driver working + // Add hostdev für GPU passthrough -> add PCI ID arguments to cmdln parser + // + } +} |