summaryrefslogtreecommitdiffstats
path: root/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/plugin/qemu/configuration/TransformationSpecificQemuFirmware.java
blob: 3967a0b496d49fb6dfb28933d1198472ac65ae35 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package org.openslx.runvirt.plugin.qemu.configuration;

import org.openslx.firmware.FirmwareException;
import org.openslx.firmware.QemuFirmwareUtil;
import org.openslx.libvirt.domain.Domain;
import org.openslx.runvirt.plugin.qemu.cmdln.CommandLineArgs;
import org.openslx.runvirt.plugin.qemu.virtualization.LibvirtHypervisorQemu;
import org.openslx.virtualization.configuration.transformation.TransformationException;
import org.openslx.virtualization.configuration.transformation.TransformationSpecific;

/**
 * Specific firmware transformation for Libvirt/QEMU virtualization configurations.
 * 
 * @author Manuel Bentele
 * @version 1.0
 */
public class TransformationSpecificQemuFirmware
		extends TransformationSpecific<Domain, CommandLineArgs, LibvirtHypervisorQemu>
{
	/**
	 * Name of the configuration transformation.
	 */
	private static final String NAME = "QEMU Firmware [Seabios, UEFI (OVMF)]";

	/**
	 * Creates a new firmware transformation for Libvirt/QEMU virtualization configurations.
	 * 
	 * @param hypervisor Libvirt/QEMU hypervisor.
	 */
	public TransformationSpecificQemuFirmware( LibvirtHypervisorQemu virtualizer )
	{
		super( TransformationSpecificQemuFirmware.NAME, virtualizer );
	}

	/**
	 * Validates a virtualization configuration and input arguments for this transformation.
	 * 
	 * @param config virtualization configuration for the validation.
	 * @param args input arguments for the validation.
	 * @throws TransformationException validation has failed.
	 */
	private void validateInputs( Domain config, CommandLineArgs args ) throws TransformationException
	{
		if ( config == null || args == null ) {
			throw new TransformationException( "Virtualization configuration or input arguments are missing!" );
		} else if ( args.getFirmware() == null || args.getFirmware().isEmpty() ) {
			throw new TransformationException( "Path to QEMU firmware specifications directory is not specified!" );
		}
	}

	@Override
	public void transform( Domain config, CommandLineArgs args ) throws TransformationException
	{
		// validate configuration and input arguments
		this.validateInputs( config, args );

		// get OS loader from VM
		final String sourceOsLoader = config.getOsLoader();

		// get OS architecture from VM
		final String sourceOsArch = config.getOsArch();

		// get OS machine type from VM
		final String sourceOsMachine = config.getOsMachine();

		// check if OS loader is specified
		if ( sourceOsLoader != null && !sourceOsLoader.isEmpty() ) {
			// OS loader is specified so transform path to specified firmware path
			// First, lookup QEMU firmware loader for target
			String targetOsLoader = null;
			try {
				targetOsLoader = QemuFirmwareUtil.lookupTargetOsLoader( args.getFirmware(), sourceOsLoader, sourceOsArch,
						sourceOsMachine );
			} catch ( FirmwareException e ) {
				throw new TransformationException( e.getLocalizedMessage() );
			}

			// Second, set target QEMU firmware loader if specified
			if ( targetOsLoader != null && !targetOsLoader.isEmpty() ) {
				config.setOsLoader( targetOsLoader );
			}
		}
	}
}