From 670ef8aabd4d997372554e79f8ca8930df56d2e6 Mon Sep 17 00:00:00 2001 From: Manuel Bentele Date: Mon, 17 May 2021 13:51:41 +0200 Subject: [qemu] Add viewer representation to render displays of the virtual machine --- .../openslx/runvirt/viewer/ViewerVirtViewer.java | 97 ++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/viewer/ViewerVirtViewer.java (limited to 'core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/viewer/ViewerVirtViewer.java') diff --git a/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/viewer/ViewerVirtViewer.java b/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/viewer/ViewerVirtViewer.java new file mode 100644 index 00000000..8f6e9481 --- /dev/null +++ b/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/viewer/ViewerVirtViewer.java @@ -0,0 +1,97 @@ +package org.openslx.runvirt.viewer; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.openslx.runvirt.virtualization.LibvirtHypervisor; +import org.openslx.runvirt.virtualization.LibvirtHypervisorException; +import org.openslx.runvirt.virtualization.LibvirtVirtualMachine; +import org.openslx.virtualization.Version; + +/** + * Virtual Viewer (virt-viewer) to view one or several displays of a virtual machine. + * + * @author Manuel Bentele + * @version 1.0 + */ +public class ViewerVirtViewer extends Viewer +{ + /** + * Name of the Virtual Machine Manager program. + */ + private final static String NAME = "virt-viewer"; + + /** + * Maximum number of supported displays by the Virtual Viewer. + */ + private final static int NUM_SUPPORTED_DISPLAYS = Integer.MAX_VALUE; + + /** + * Creates a new Virtual Viewer for a Libvirt virtual machine running on a Libvirt hypervisor. + * + * @param machine virtual machine to display. + * @param hypervisor remote (hypervisor) endpoint for the viewer to connect to. + */ + public ViewerVirtViewer( LibvirtVirtualMachine machine, LibvirtHypervisor hypervisor ) + { + super( ViewerVirtViewer.NAME, ViewerVirtViewer.NUM_SUPPORTED_DISPLAYS, machine, hypervisor ); + } + + @Override + public Version getVersion() throws ViewerException + { + final Version version; + + // execute viewer process with arguments: + // "virt-viewer --version" + final String versionOutput = ViewerUtils.executeViewer( ViewerVirtViewer.NAME, + new String[] { "--version" } ); + + if ( versionOutput == null ) { + version = null; + } else { + // parse version from the viewer's process output + final Pattern viewerVersionPattern = Pattern.compile( "(\\d+).(\\d+)" ); + final Matcher viewerVersionMatcher = viewerVersionPattern.matcher( versionOutput ); + + // check if version pattern was found + if ( viewerVersionMatcher.find() ) { + final short major = Short.valueOf( viewerVersionMatcher.group( 1 ) ); + final short minor = Short.valueOf( viewerVersionMatcher.group( 2 ) ); + version = new Version( major, minor ); + } else { + version = null; + } + } + + return version; + } + + @Override + public void render() throws ViewerException + { + String connectionUri = null; + String machineUuid = null; + + // get URI of the hypervisor connection and UUID of the machine + try { + connectionUri = this.getHypervisor().getConnectionUri(); + machineUuid = this.getMachine().getConfiguration().getUuid(); + } catch ( LibvirtHypervisorException e ) { + throw new ViewerException( + "Failed to retrieve the URI of the hypervisor backend or the UUID of the machine to display: " + + e.getLocalizedMessage() ); + } + + // check if URI of the hypervisor connection and UUID of the machine is specified, otherwise abort + if ( connectionUri == null || connectionUri.isEmpty() || machineUuid == null || machineUuid.isEmpty() ) { + throw new ViewerException( + "The URI of the hypervisor backend or the UUID of the machine to display is missing!" ); + } + + // execute viewer process with arguments: + // "virt-viewer --full-screen --reconnect --wait --attach --connect= --domain-name -- " + ViewerUtils.executeViewer( ViewerVirtViewer.NAME, new String[] { "--full-screen", "--reconnect", "--wait", + "--attach", "--connect=" + connectionUri, "--uuid", "--", machineUuid } ); + } +} -- cgit v1.2.3-55-g7522