Initial commit.
This commit is contained in:
commit
f48a7aae94
62 changed files with 2925 additions and 0 deletions
10
org.jdrupes.vmoperator.runner.qemu/.checkstyle
Normal file
10
org.jdrupes.vmoperator.runner.qemu/.checkstyle
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<fileset-config file-format-version="1.2.0" simple-config="false" sync-formatter="false">
|
||||
<local-check-config name="Project Checks" location="/VM-Operator/checkstyle.xml" type="project" description="">
|
||||
<additional-data name="protect-config-file" value="false"/>
|
||||
</local-check-config>
|
||||
<fileset name="all" enabled="true" check-config-name="Project Checks" local="true">
|
||||
<file-match-pattern match-pattern="^src/" include-pattern="true"/>
|
||||
</fileset>
|
||||
</fileset-config>
|
||||
7
org.jdrupes.vmoperator.runner.qemu/.eclipse-pmd
Normal file
7
org.jdrupes.vmoperator.runner.qemu/.eclipse-pmd
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<eclipse-pmd xmlns="http://acanda.ch/eclipse-pmd/0.8" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://acanda.ch/eclipse-pmd/0.8 http://acanda.ch/eclipse-pmd/eclipse-pmd-0.8.xsd">
|
||||
<analysis enabled="true" />
|
||||
<rulesets>
|
||||
<ruleset name="Custom Rules" ref="moodle-tools-console/ruleset.xml" refcontext="workspace" />
|
||||
</rulesets>
|
||||
</eclipse-pmd>
|
||||
1
org.jdrupes.vmoperator.runner.qemu/.gitignore
vendored
Normal file
1
org.jdrupes.vmoperator.runner.qemu/.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
config.yaml
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
arguments=
|
||||
auto.sync=false
|
||||
build.scans.enabled=false
|
||||
connection.gradle.distribution=GRADLE_DISTRIBUTION(WRAPPER)
|
||||
connection.project.dir=..
|
||||
eclipse.preferences.version=1
|
||||
gradle.user.home=
|
||||
java.home=
|
||||
jvm.arguments=
|
||||
offline.mode=false
|
||||
override.workspace.settings=false
|
||||
show.console.view=false
|
||||
show.executions.view=false
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
eclipse.preferences.version=1
|
||||
encoding/<project>=UTF-8
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
eclipse.preferences.version=1
|
||||
line.separator=\n
|
||||
28
org.jdrupes.vmoperator.runner.qemu/build.gradle
Normal file
28
org.jdrupes.vmoperator.runner.qemu/build.gradle
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
/*
|
||||
* This file was generated by the Gradle 'init' task.
|
||||
*
|
||||
* This project uses @Incubating APIs which are subject to change.
|
||||
*/
|
||||
|
||||
plugins {
|
||||
id 'org.jdrupes.vmoperator.java-application-conventions'
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation 'org.jgrapes:org.jgrapes.core:[1.19.0,2)'
|
||||
implementation 'org.jgrapes:org.jgrapes.io:[2.5.0,3)'
|
||||
implementation 'org.jgrapes:org.jgrapes.http:[3.1.0,4)'
|
||||
implementation 'org.jgrapes:org.jgrapes.util:[1.26.0,2)'
|
||||
|
||||
implementation 'org.freemarker:freemarker:[2.3.32,2.4)'
|
||||
implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:[2.15.0,3]'
|
||||
|
||||
implementation project(':org.jdrupes.vmoperator.util')
|
||||
|
||||
runtimeOnly 'com.electronwill.night-config:yaml:3.6.6'
|
||||
}
|
||||
|
||||
application {
|
||||
// Define the main class for the application.
|
||||
mainClass = 'org.jdrupes.vmoperator.runner.qemu.Runner'
|
||||
}
|
||||
13
org.jdrupes.vmoperator.runner.qemu/checkstyle.xml
Normal file
13
org.jdrupes.vmoperator.runner.qemu/checkstyle.xml
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE module PUBLIC "-//Checkstyle//DTD Checkstyle Configuration 1.3//EN" "https://checkstyle.org/dtds/configuration_1_3.dtd">
|
||||
|
||||
<!--
|
||||
This configuration file was written by the eclipse-cs plugin configuration editor
|
||||
-->
|
||||
<!--
|
||||
Checkstyle-Configuration: Project Checks
|
||||
Description: none
|
||||
-->
|
||||
<module name="Checker">
|
||||
<property name="severity" value="warning"/>
|
||||
</module>
|
||||
49
org.jdrupes.vmoperator.runner.qemu/config-sample.yaml
Normal file
49
org.jdrupes.vmoperator.runner.qemu/config-sample.yaml
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
# The values in comments are the defaults.
|
||||
|
||||
"/Runner":
|
||||
# The directory used to store data files. Defaults to (depending on
|
||||
# values available):
|
||||
# * $XDG_DATA_HOME/vmrunner/${vm.name}
|
||||
# * $HOME/.local/share/vmrunner/${vm.name}
|
||||
# * ./${vm.name}
|
||||
# "dataDir": "$XDG_DATA_HOME"
|
||||
|
||||
# The directory used to store runtime files. Defaults to (depending on
|
||||
# values available):
|
||||
# * $XDG_RUNTIME_DIR/vmrunner/${vm.name}
|
||||
# * /tmp/${USER}/vmrunner/${vm.name}
|
||||
# * /tmp/vmrunner/${vm.name}
|
||||
# "runtimeDir": "$XDG_RUNTIME_DIR/vmrunner/${vm.name}"
|
||||
|
||||
# The template to use. Resolved relative to /usr/share/vmrunner/templates.
|
||||
# "template": "Standard-VM-latest.ftl.yaml"
|
||||
|
||||
# The template is copied to the data diretory when the VM starts for
|
||||
# the first time. Subsequent starts use the copy unless this option is set.
|
||||
# "updateTemplate": false
|
||||
|
||||
# Define the VM (required)
|
||||
"vm":
|
||||
# The VM's name (required)
|
||||
"name": "test-vm"
|
||||
|
||||
# The machine's uuid. If none is specified, a uuid is generated
|
||||
# and stored in the data directory. If the uuid is important
|
||||
# (e.g. because licenses depend on it) it is recommaned to specify
|
||||
# it here explicitly or to carefully backup the data directory.
|
||||
# "uuid": "generated uuid"
|
||||
|
||||
# Whether to provide a software TPM (defaults to false)
|
||||
# "useTpm": false
|
||||
|
||||
# How to boot:
|
||||
# * bios
|
||||
# * uefi
|
||||
# * secure
|
||||
# "bootMode": "uefi"
|
||||
|
||||
# RAM settings
|
||||
# "maximumRam": "512M"
|
||||
# "currentRam": "512M"
|
||||
|
||||
|
||||
29
org.jdrupes.vmoperator.runner.qemu/jul-debug.properties
Normal file
29
org.jdrupes.vmoperator.runner.qemu/jul-debug.properties
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
#
|
||||
# Ad Hoc Polling Application
|
||||
# Copyright (C) 2018 Michael N. Lipp
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or modify it
|
||||
# under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation; either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful, but
|
||||
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
# for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License along
|
||||
# with this program; if not, see <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
|
||||
|
||||
handlers=java.util.logging.ConsoleHandler
|
||||
|
||||
#org.jgrapes.level=FINE
|
||||
#org.jgrapes.core.handlerTracking.level=FINER
|
||||
|
||||
org.jdrupes.vmoperator.runner.qemu.level=FINE
|
||||
|
||||
java.util.logging.ConsoleHandler.level=ALL
|
||||
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
|
||||
java.util.logging.SimpleFormatter.format=%1$tY-%1$tm-%1$td %5$s%6$s%n
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
# Candidate paths for code and templates for firmware rom and flash
|
||||
"firmware":
|
||||
"rom":
|
||||
- "/usr/share/OVMF/OVMF_CODE.fd"
|
||||
- "/usr/share/edk2/x64/OVMF_CODE.fd"
|
||||
"flash":
|
||||
- "/usr/share/edk2/ovmf/OVMF_VARS.fd"
|
||||
- "/usr/share/edk2/x64/OVMF_CODE.fd"
|
||||
"secure":
|
||||
"flash":
|
||||
- "/usr/share/edk2/ovmf/OVMF_CODE.secboot.fd"
|
||||
- "/usr/share/edk2/x64/OVMF_CODE.secboot.fd"
|
||||
|
|
@ -0,0 +1,71 @@
|
|||
/*
|
||||
* VM-Operator
|
||||
* Copyright (C) 2023 Michael N. Lipp
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.jdrupes.vmoperator.runner.qemu;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* A command definition.
|
||||
*/
|
||||
class CommandDefinition {
|
||||
public String name;
|
||||
public final List<String> command = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* Instantiates a new process definition.
|
||||
*
|
||||
* @param name the name
|
||||
* @param jsonData the json data
|
||||
*/
|
||||
public CommandDefinition(String name, JsonNode jsonData) {
|
||||
this.name = name;
|
||||
for (JsonNode path : jsonData.get("executable")) {
|
||||
if (Files.isExecutable(Path.of(path.asText()))) {
|
||||
command.add(path.asText());
|
||||
}
|
||||
}
|
||||
if (command.isEmpty()) {
|
||||
throw new IllegalArgumentException("No executable found.");
|
||||
}
|
||||
collect(command, jsonData.get("arguments"));
|
||||
}
|
||||
|
||||
private void collect(List<String> result, JsonNode node) {
|
||||
if (!node.isArray()) {
|
||||
result.add(node.asText());
|
||||
return;
|
||||
}
|
||||
for (var element : node) {
|
||||
collect(result, element);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the name.
|
||||
*
|
||||
* @return the string
|
||||
*/
|
||||
public String name() {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,176 @@
|
|||
/*
|
||||
* VM-Operator
|
||||
* Copyright (C) 2023 Michael N. Lipp
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.jdrupes.vmoperator.runner.qemu;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.UUID;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import org.jdrupes.vmoperator.util.Dto;
|
||||
|
||||
/**
|
||||
* The configuration information from the configuration file.
|
||||
*/
|
||||
class Configuration implements Dto {
|
||||
@SuppressWarnings("PMD.FieldNamingConventions")
|
||||
protected final Logger logger = Logger.getLogger(getClass().getName());
|
||||
|
||||
public static final Object BOOT_MODE_UEFI = "uefi";
|
||||
public static final Object BOOT_MODE_SECURE = "secure";
|
||||
|
||||
public String dataDir;
|
||||
public String runtimeDir;
|
||||
public String template;
|
||||
public boolean updateTemplate;
|
||||
public Path swtpmSocket;
|
||||
public Path monitorSocket;
|
||||
public Path firmwareRom;
|
||||
public Path firmwareFlash;
|
||||
public JsonNode monitorMessages;
|
||||
@SuppressWarnings("PMD.ShortVariable")
|
||||
public Vm vm;
|
||||
|
||||
/**
|
||||
* Subsection "vm".
|
||||
*/
|
||||
@SuppressWarnings("PMD.ShortClassName")
|
||||
public static class Vm implements Dto {
|
||||
public String name;
|
||||
public String uuid;
|
||||
public boolean useTpm;
|
||||
public String bootMode = "uefi";
|
||||
public String maximumRam;
|
||||
public String currentRam;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check configuration.
|
||||
*
|
||||
* @return true, if successful
|
||||
*/
|
||||
@SuppressWarnings({ "PMD.AvoidDeeplyNestedIfStmts",
|
||||
"PMD.CognitiveComplexity", "PMD.CyclomaticComplexity",
|
||||
"PMD.NPathComplexity" })
|
||||
public boolean check() {
|
||||
if (vm == null || vm.name == null) {
|
||||
logger.severe(() -> "Configuration is missing mandatory entries.");
|
||||
return false;
|
||||
}
|
||||
if (!checkRuntimeDir() || !checkDataDir() || !checkUuid()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@SuppressWarnings("PMD.AvoidDeeplyNestedIfStmts")
|
||||
private boolean checkRuntimeDir() {
|
||||
// Runtime directory (sockets)
|
||||
if (runtimeDir == null) {
|
||||
runtimeDir = System.getenv("XDG_RUNTIME_DIR");
|
||||
if (runtimeDir == null) {
|
||||
runtimeDir = "/tmp";
|
||||
if (System.getenv("USER") != null) {
|
||||
runtimeDir += "/" + System.getenv("USER");
|
||||
}
|
||||
}
|
||||
runtimeDir += "/vmrunner/" + vm.name;
|
||||
swtpmSocket
|
||||
= Path.of(runtimeDir, "swtpm-sock");
|
||||
monitorSocket
|
||||
= Path.of(runtimeDir, "monitor.sock");
|
||||
}
|
||||
Path runtimePath = Path.of(runtimeDir);
|
||||
if (!Files.exists(runtimePath)) {
|
||||
runtimePath.toFile().mkdirs();
|
||||
}
|
||||
if (!Files.isDirectory(runtimePath) || !Files.isWritable(runtimePath)) {
|
||||
logger.severe(() -> String.format(
|
||||
"Configured runtime directory \"%s\""
|
||||
+ " does not exist or isn't writable.",
|
||||
runtimeDir));
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean checkDataDir() {
|
||||
// Data directory
|
||||
if (dataDir == null) {
|
||||
dataDir = System.getenv("XDG_DATA_HOME");
|
||||
if (dataDir == null) {
|
||||
dataDir = ".";
|
||||
if (System.getenv("HOME") != null) {
|
||||
dataDir = System.getenv("HOME") + "/.local/share";
|
||||
}
|
||||
}
|
||||
dataDir += "/vmrunner/" + vm.name;
|
||||
}
|
||||
Path dataPath = Path.of(dataDir);
|
||||
if (!Files.exists(dataPath)) {
|
||||
dataPath.toFile().mkdirs();
|
||||
}
|
||||
if (!Files.isDirectory(dataPath) || !Files.isWritable(dataPath)) {
|
||||
logger.severe(() -> String.format(
|
||||
"Configured data directory \"%s\""
|
||||
+ " does not exist or isn't writable.",
|
||||
dataDir));
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean checkUuid() {
|
||||
// Explicitly configured uuid takes precedence.
|
||||
if (vm.uuid != null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Try to read stored uuid.
|
||||
Path uuidPath = Path.of(dataDir, "uuid.txt");
|
||||
if (Files.isReadable(uuidPath)) {
|
||||
try {
|
||||
var stored
|
||||
= Files.lines(uuidPath, StandardCharsets.UTF_8).findFirst();
|
||||
if (stored.isPresent()) {
|
||||
vm.uuid = stored.get();
|
||||
return true;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
logger.log(Level.WARNING, e,
|
||||
() -> "Stored uuid cannot be read: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
// Generate new uuid
|
||||
vm.uuid = UUID.randomUUID().toString();
|
||||
try {
|
||||
Files.writeString(uuidPath, vm.uuid + "\n");
|
||||
} catch (IOException e) {
|
||||
logger.log(Level.WARNING, e,
|
||||
() -> "Cannot store uuid: " + e.getMessage());
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,522 @@
|
|||
/*
|
||||
* VM-Operator
|
||||
* Copyright (C) 2023 Michael N. Lipp
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.jdrupes.vmoperator.runner.qemu;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.JsonMappingException;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
|
||||
import freemarker.core.ParseException;
|
||||
import freemarker.template.MalformedTemplateNameException;
|
||||
import freemarker.template.TemplateException;
|
||||
import freemarker.template.TemplateExceptionHandler;
|
||||
import freemarker.template.TemplateNotFoundException;
|
||||
import java.io.File;
|
||||
import java.io.FileDescriptor;
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.io.Writer;
|
||||
import java.net.UnixDomainSocketAddress;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.HashMap;
|
||||
import java.util.Optional;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import static org.jdrupes.vmoperator.runner.qemu.Configuration.BOOT_MODE_SECURE;
|
||||
import static org.jdrupes.vmoperator.runner.qemu.Configuration.BOOT_MODE_UEFI;
|
||||
import org.jdrupes.vmoperator.util.ExtendedObjectWrapper;
|
||||
import org.jgrapes.core.Channel;
|
||||
import org.jgrapes.core.Component;
|
||||
import org.jgrapes.core.Components;
|
||||
import org.jgrapes.core.TypedIdKey;
|
||||
import org.jgrapes.core.annotation.Handler;
|
||||
import org.jgrapes.core.events.Start;
|
||||
import org.jgrapes.core.events.Stop;
|
||||
import org.jgrapes.io.NioDispatcher;
|
||||
import org.jgrapes.io.events.ConnectError;
|
||||
import org.jgrapes.io.events.Input;
|
||||
import org.jgrapes.io.events.OpenSocketConnection;
|
||||
import org.jgrapes.io.events.ProcessExited;
|
||||
import org.jgrapes.io.events.ProcessStarted;
|
||||
import org.jgrapes.io.events.StartProcess;
|
||||
import org.jgrapes.io.process.ProcessManager;
|
||||
import org.jgrapes.io.process.ProcessManager.ProcessChannel;
|
||||
import org.jgrapes.io.util.ByteBufferWriter;
|
||||
import org.jgrapes.io.util.LineCollector;
|
||||
import org.jgrapes.net.SocketConnector;
|
||||
import org.jgrapes.net.SocketIOChannel;
|
||||
import org.jgrapes.net.events.ClientConnected;
|
||||
import org.jgrapes.util.FileSystemWatcher;
|
||||
import org.jgrapes.util.YamlConfigurationStore;
|
||||
import org.jgrapes.util.events.ConfigurationUpdate;
|
||||
import org.jgrapes.util.events.FileChanged;
|
||||
import org.jgrapes.util.events.FileChanged.Kind;
|
||||
import org.jgrapes.util.events.WatchFile;
|
||||
|
||||
/**
|
||||
* The Runner.
|
||||
*
|
||||
* @startuml
|
||||
* [*] --> Setup
|
||||
* Setup --> Setup: InitialConfiguration/configure Runner
|
||||
*
|
||||
* state Startup {
|
||||
*
|
||||
* state which <<choice>>
|
||||
* state "Start swtpm" as swtpm
|
||||
* state "Start qemu" as qemu
|
||||
* state "Open monitor" as monitor
|
||||
* state success <<exitPoint>>
|
||||
* state error <<exitPoint>>
|
||||
*
|
||||
* which --> swtpm: [use swtpm]
|
||||
* which --> qemu: [else]
|
||||
*
|
||||
* swtpm: entry/start swtpm
|
||||
* swtpm --> error: StartProcessError/stop
|
||||
* swtpm -> qemu: FileChanged[swtpm socket created]
|
||||
*
|
||||
* qemu: entry/start qemu
|
||||
* qemu --> error: StartProcessError/stop
|
||||
* qemu --> monitor : FileChanged[monitor socket created]
|
||||
*
|
||||
* monitor: entry/fire OpenSocketConnection
|
||||
* monitor --> success: ClientConnected[for monitor]
|
||||
* monitor --> error: ConnectError[for monitor]
|
||||
* }
|
||||
*
|
||||
* Setup --> which: Start
|
||||
*
|
||||
* success --> Run
|
||||
* error --> [*]
|
||||
*
|
||||
* @enduml
|
||||
*
|
||||
* If the log level for `org.jdrupes.vmoperator.runner.qemu.monitor`
|
||||
* is set to fine, the messages exchanged on the monitor socket are logged.
|
||||
*/
|
||||
@SuppressWarnings("PMD.ExcessiveImports")
|
||||
public class Runner extends Component {
|
||||
|
||||
private static final String TEMPLATE_DIR = "/usr/share/vmrunner/templates";
|
||||
private static final String DEFAULT_TEMPLATE
|
||||
= "Standard-VM-latest.ftl.yaml";
|
||||
private static final String SAVED_TEMPLATE = "VM.ftl.yaml";
|
||||
private static final String FW_FLASH = "fw-flash.fd";
|
||||
|
||||
@SuppressWarnings({ "PMD.FieldNamingConventions",
|
||||
"PMD.VariableNamingConventions" })
|
||||
private static final Logger monitorLog
|
||||
= Logger.getLogger(Runner.class.getPackageName() + ".monitor");
|
||||
|
||||
private static Runner app;
|
||||
|
||||
private final ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
|
||||
private final JsonNode defaults;
|
||||
@SuppressWarnings("PMD.UseConcurrentHashMap")
|
||||
private Configuration config = new Configuration();
|
||||
private final freemarker.template.Configuration fmConfig;
|
||||
|
||||
/**
|
||||
* Instantiates a new runner.
|
||||
*
|
||||
* @throws IOException Signals that an I/O exception has occurred.
|
||||
*/
|
||||
public Runner() throws IOException {
|
||||
super(new Context());
|
||||
// Get defaults
|
||||
defaults = mapper.readValue(
|
||||
Runner.class.getResourceAsStream("defaults.yaml"), JsonNode.class);
|
||||
|
||||
// Configure freemarker library
|
||||
fmConfig = new freemarker.template.Configuration(
|
||||
freemarker.template.Configuration.VERSION_2_3_32);
|
||||
fmConfig.setDirectoryForTemplateLoading(new File("/"));
|
||||
fmConfig.setDefaultEncoding("utf-8");
|
||||
fmConfig.setObjectWrapper(new ExtendedObjectWrapper(
|
||||
fmConfig.getIncompatibleImprovements(), mapper));
|
||||
fmConfig.setTemplateExceptionHandler(
|
||||
TemplateExceptionHandler.RETHROW_HANDLER);
|
||||
fmConfig.setLogTemplateExceptions(false);
|
||||
|
||||
// Prepare component tree
|
||||
attach(new NioDispatcher());
|
||||
attach(new FileSystemWatcher(channel()));
|
||||
attach(new ProcessManager(channel()));
|
||||
attach(new SocketConnector(channel()));
|
||||
|
||||
// Configuration store with file in /etc (default)
|
||||
File config = new File(System.getProperty(
|
||||
getClass().getPackageName().toString() + ".config",
|
||||
"/etc/vmrunner/config.yaml"));
|
||||
attach(new YamlConfigurationStore(channel(), config, false));
|
||||
fire(new WatchFile(config.toPath()));
|
||||
}
|
||||
|
||||
/**
|
||||
* On configuration update.
|
||||
*
|
||||
* @param event the event
|
||||
*/
|
||||
@Handler
|
||||
public void onConfigurationUpdate(ConfigurationUpdate event) {
|
||||
event.structured(componentPath()).ifPresent(c -> {
|
||||
try {
|
||||
config = mapper.convertValue(c, Configuration.class);
|
||||
} catch (IllegalArgumentException e) {
|
||||
logger.log(Level.SEVERE, e, () -> "Invalid configuration: "
|
||||
+ e.getMessage());
|
||||
// Don't use default configuration
|
||||
config = null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the start event.
|
||||
*
|
||||
* @param event the event
|
||||
*/
|
||||
@Handler
|
||||
@SuppressWarnings({ "PMD.SystemPrintln" })
|
||||
public void onStart(Start event) {
|
||||
try {
|
||||
if (config == null || !config.check()) {
|
||||
// Invalid configuration, fail
|
||||
fire(new Stop());
|
||||
return;
|
||||
}
|
||||
|
||||
// Prepare firmware files and add to config
|
||||
setFirmwarePaths();
|
||||
|
||||
// Obtain more data from template
|
||||
var tplData = dataFromTemplate();
|
||||
|
||||
// Get process definitions etc. from processed data
|
||||
Context context = (Context) channel();
|
||||
context.swtpmDefinition = Optional.ofNullable(tplData.get("swtpm"))
|
||||
.map(d -> new CommandDefinition("swtpm", d)).orElse(null);
|
||||
context.qemuDefinition = Optional.ofNullable(tplData.get("qemu"))
|
||||
.map(d -> new CommandDefinition("qemu", d)).orElse(null);
|
||||
config.monitorMessages = tplData.get("monitorMessages");
|
||||
|
||||
// Files to watch for
|
||||
Files.deleteIfExists(config.swtpmSocket);
|
||||
fire(new WatchFile(config.swtpmSocket));
|
||||
Files.deleteIfExists(config.monitorSocket);
|
||||
fire(new WatchFile(config.monitorSocket));
|
||||
|
||||
// Start first
|
||||
if (config.vm.useTpm && context.swtpmDefinition != null) {
|
||||
startProcess(context, context.swtpmDefinition);
|
||||
return;
|
||||
}
|
||||
startProcess(context, context.qemuDefinition);
|
||||
} catch (IOException | TemplateException e) {
|
||||
logger.log(Level.SEVERE, e,
|
||||
() -> "Cannot configure runner: " + e.getMessage());
|
||||
fire(new Stop());
|
||||
}
|
||||
}
|
||||
|
||||
private void setFirmwarePaths() throws IOException {
|
||||
// Get file for firmware ROM
|
||||
JsonNode codePaths = defaults.path("firmware").path("rom");
|
||||
for (var paths = codePaths.elements(); paths.hasNext();) {
|
||||
var path = Path.of(paths.next().asText());
|
||||
if (Files.exists(path)) {
|
||||
config.firmwareRom = path;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// Get file for firmware flash, if necessary
|
||||
config.firmwareFlash = Path.of(config.dataDir, FW_FLASH);
|
||||
if (!Files.exists(config.firmwareFlash)) {
|
||||
JsonNode srcPaths = null;
|
||||
if (BOOT_MODE_UEFI.equals(config.vm.bootMode)) {
|
||||
srcPaths = defaults.path("firmware").path("flash");
|
||||
} else if (BOOT_MODE_SECURE.equals(config.vm.bootMode)) {
|
||||
srcPaths = defaults.path("firmware")
|
||||
.path("secure").path("flash");
|
||||
}
|
||||
// If UEFI boot, srcPaths != null
|
||||
if (srcPaths != null) {
|
||||
for (var paths = srcPaths.elements(); paths.hasNext();) {
|
||||
var path = Path.of(paths.next().asText());
|
||||
if (Files.exists(path)) {
|
||||
Files.copy(path, config.firmwareFlash);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private JsonNode dataFromTemplate()
|
||||
throws IOException, TemplateNotFoundException,
|
||||
MalformedTemplateNameException, ParseException, TemplateException,
|
||||
JsonProcessingException, JsonMappingException {
|
||||
// Try saved template, copy if not there (or to be updated)
|
||||
Path templatePath = Path.of(config.dataDir, SAVED_TEMPLATE);
|
||||
if (!Files.isReadable(templatePath) || config.updateTemplate) {
|
||||
// Get template
|
||||
Path sourcePath = Paths.get(TEMPLATE_DIR).resolve(Optional
|
||||
.ofNullable(config.template).orElse(DEFAULT_TEMPLATE));
|
||||
Files.deleteIfExists(templatePath);
|
||||
Files.copy(sourcePath, templatePath);
|
||||
}
|
||||
|
||||
// Configure data model
|
||||
var model = new HashMap<String, Object>();
|
||||
model.put("runtimeDir", config.runtimeDir);
|
||||
model.put("firmwareRom", config.firmwareRom.toString());
|
||||
model.put("firmwareFlash", config.firmwareFlash.toString());
|
||||
model.put("vm", config.vm);
|
||||
|
||||
// Combine template and data and parse result
|
||||
// (tempting, but no need to use a pipe here)
|
||||
var fmTemplate = fmConfig.getTemplate(templatePath.toString());
|
||||
StringWriter out = new StringWriter();
|
||||
fmTemplate.process(model, out);
|
||||
return mapper.readValue(out.toString(), JsonNode.class);
|
||||
}
|
||||
|
||||
private boolean startProcess(Context context, CommandDefinition toStart) {
|
||||
logger.fine(
|
||||
() -> "Starting process: " + String.join(" ", toStart.command));
|
||||
fire(new StartProcess(toStart.command)
|
||||
.setAssociated(Context.class, context)
|
||||
.setAssociated(CommandDefinition.class, toStart), channel());
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Watch for the creation of the swtpm socket and start the
|
||||
* qemu process if it has been created.
|
||||
*
|
||||
* @param event the event
|
||||
* @param context the context
|
||||
*/
|
||||
@Handler
|
||||
public void onFileChanged(FileChanged event, Context context) {
|
||||
if (event.change() == Kind.CREATED
|
||||
&& event.path()
|
||||
.equals(Path.of(config.runtimeDir, "swtpm-sock"))) {
|
||||
// swtpm running, start qemu
|
||||
startProcess(context, context.qemuDefinition);
|
||||
return;
|
||||
}
|
||||
var monSockPath = Path.of(config.runtimeDir, "monitor.sock");
|
||||
if (event.change() == Kind.CREATED
|
||||
&& event.path().equals(monSockPath)) {
|
||||
// qemu running, open socket
|
||||
fire(new OpenSocketConnection(
|
||||
UnixDomainSocketAddress.of(monSockPath))
|
||||
.setAssociated(Context.class, context));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Associate required data with the process channel and register the
|
||||
* channel in the context.
|
||||
*
|
||||
* @param event the event
|
||||
* @param channel the channel
|
||||
* @throws InterruptedException the interrupted exception
|
||||
*/
|
||||
@Handler
|
||||
@SuppressWarnings({ "PMD.SwitchStmtsShouldHaveDefault",
|
||||
"PMD.TooFewBranchesForASwitchStatement" })
|
||||
public void onProcessStarted(ProcessStarted event, ProcessChannel channel)
|
||||
throws InterruptedException {
|
||||
event.startEvent().associated(Context.class).ifPresent(context -> {
|
||||
// Associate the process channel with the general context
|
||||
// and with its process definition (both carried over by
|
||||
// the start event).
|
||||
channel.setAssociated(Context.class, context);
|
||||
CommandDefinition procDef
|
||||
= event.startEvent().associated(CommandDefinition.class).get();
|
||||
channel.setAssociated(CommandDefinition.class, procDef);
|
||||
|
||||
// Associate the channel with a line collector (one for
|
||||
// each stream) for logging the process's output.
|
||||
TypedIdKey.associate(channel, 1, new LineCollector().nativeCharset()
|
||||
.consumer(line -> logger
|
||||
.info(() -> procDef.name() + "(out): " + line)));
|
||||
TypedIdKey.associate(channel, 2, new LineCollector().nativeCharset()
|
||||
.consumer(line -> logger
|
||||
.info(() -> procDef.name() + "(err): " + line)));
|
||||
|
||||
// Register the channel in the context.
|
||||
switch (procDef.name) {
|
||||
case "swtpm":
|
||||
context.swtpmChannel = channel;
|
||||
break;
|
||||
case "qemu":
|
||||
context.qemuChannel = channel;
|
||||
break;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Forward output from the processes to to the log.
|
||||
*
|
||||
* @param event the event
|
||||
* @param channel the channel
|
||||
*/
|
||||
@Handler
|
||||
public void onInput(Input<?> event, ProcessChannel channel) {
|
||||
event.associated(FileDescriptor.class, Integer.class).ifPresent(
|
||||
fd -> TypedIdKey.associated(channel, LineCollector.class, fd)
|
||||
.ifPresent(lc -> lc.feed(event)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle data from qemu monitor connection.
|
||||
*
|
||||
* @param event the event
|
||||
* @param channel the channel
|
||||
*/
|
||||
@Handler
|
||||
public void onInput(Input<?> event, SocketIOChannel channel) {
|
||||
channel.associated(LineCollector.class).ifPresent(collector -> {
|
||||
collector.feed(event);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* On process exited.
|
||||
*
|
||||
* @param event the event
|
||||
* @param channel the channel
|
||||
*/
|
||||
@Handler
|
||||
public void onProcessExited(ProcessExited event, ProcessChannel channel) {
|
||||
int i = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if this is from opening the monitor socket and if true,
|
||||
* save the socket in the context and associate the channel with
|
||||
* the context. Then send the initial message to the socket.
|
||||
*
|
||||
* @param event the event
|
||||
* @param channel the channel
|
||||
*/
|
||||
@Handler
|
||||
public void onClientConnected(ClientConnected event,
|
||||
SocketIOChannel channel) {
|
||||
if (event.openEvent().address() instanceof UnixDomainSocketAddress addr
|
||||
&& addr.getPath()
|
||||
.equals(Path.of(config.runtimeDir, "monitor.sock"))) {
|
||||
event.openEvent().associated(Context.class).ifPresent(context -> {
|
||||
context.monitorChannel = channel;
|
||||
channel.setAssociated(Context.class, context);
|
||||
channel.setAssociated(LineCollector.class,
|
||||
new LineCollector().consumer(line -> {
|
||||
monitorLog.fine(() -> "monitor(in): " + line);
|
||||
}));
|
||||
channel.setAssociated(Writer.class, new ByteBufferWriter(
|
||||
channel).nativeCharset());
|
||||
writeToMonitor(context,
|
||||
config.monitorMessages.get("connect").asText());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Handler
|
||||
public void onConnectError(ConnectError event, SocketIOChannel channel) {
|
||||
if (event.event() instanceof OpenSocketConnection openEvent
|
||||
&& openEvent.address() instanceof UnixDomainSocketAddress addr
|
||||
&& addr.getPath()
|
||||
.equals(Path.of(config.runtimeDir, "monitor.sock"))) {
|
||||
openEvent.associated(Context.class).ifPresent(context -> {
|
||||
fire(new Stop());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private void writeToMonitor(Context context, String message) {
|
||||
monitorLog.fine(() -> "monitor(out): " + message);
|
||||
context.monitorChannel.associated(Writer.class)
|
||||
.ifPresent(writer -> {
|
||||
try {
|
||||
writer.append(message).append('\n').flush();
|
||||
} catch (IOException e) {
|
||||
// Cannot happen, but...
|
||||
logger.log(Level.WARNING, e, () -> e.getMessage());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* The context.
|
||||
*/
|
||||
private static class Context implements Channel {
|
||||
public CommandDefinition swtpmDefinition;
|
||||
public CommandDefinition qemuDefinition;
|
||||
public ProcessChannel swtpmChannel;
|
||||
public ProcessChannel qemuChannel;
|
||||
public SocketIOChannel monitorChannel;
|
||||
|
||||
@Override
|
||||
public Object defaultCriterion() {
|
||||
return "ProcMgr";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ProcMgr";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The main method.
|
||||
*
|
||||
* @param args the command
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
// The Runner is the root component
|
||||
try {
|
||||
app = new Runner();
|
||||
|
||||
// Prepare Stop
|
||||
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
|
||||
try {
|
||||
app.fire(new Stop(), Channel.BROADCAST);
|
||||
Components.awaitExhaustion();
|
||||
} catch (InterruptedException e) {
|
||||
// Cannot do anything about this.
|
||||
}
|
||||
}));
|
||||
|
||||
// Start the application
|
||||
Components.start(app);
|
||||
} catch (IOException | InterruptedException e) {
|
||||
Logger.getLogger(Runner.class.getName()).log(Level.SEVERE, e,
|
||||
() -> "Failed to start runner: " + e.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
/*
|
||||
* VM-Operator
|
||||
* Copyright (C) 2023 Michael N. Lipp
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.jdrupes.vmoperator.runner.qemu;
|
||||
|
|
@ -0,0 +1,61 @@
|
|||
"swtpm":
|
||||
# Candidate paths for the executable
|
||||
"executable": [ "/usr/bin/swtpm" ]
|
||||
|
||||
# Arguments may be specified as nested lists for better readability.
|
||||
# The arguments are flattened before being passed to the process.
|
||||
"arguments":
|
||||
- "socket"
|
||||
- "--tpm2"
|
||||
- [ "--tpmstate", "dir=${ runtimeDir }" ]
|
||||
- [ "--ctrl", "type=unixio,path=${ runtimeDir }/swtpm-sock,mode=0600" ]
|
||||
- "--terminate"
|
||||
|
||||
"qemu":
|
||||
# Candidate paths for the executable
|
||||
"executable": [ "/usr/bin/qemu-system-x86_64" ]
|
||||
|
||||
# Arguments may be specified as nested lists for better readability.
|
||||
# The arguments are flattened before being passed to the process.
|
||||
# Unless otherwise noted, flags can be found on
|
||||
# https://www.qemu.org/docs/master/system/invocation.html
|
||||
#
|
||||
# Useful links:
|
||||
# - https://joonas.fi/2021/02/uefi-pc-boot-process-and-uefi-with-qemu/
|
||||
"arguments":
|
||||
- "-no-user-config"
|
||||
- [ "-name", "guest=${ vm.name },debug-threads=on" ]
|
||||
- [ "-uuid", "${ vm.uuid }"]
|
||||
# Configure "modern" machine (pc-q35-7.0). USB is off, because we
|
||||
# configure (better) xhci later. No VMWare IO port (obviously).
|
||||
# For smm=on see https://scumjr.github.io/2016/01/04/playing-with-smm-and-qemu/.
|
||||
# Configure ROM/EEPROM for UEFI.
|
||||
- [ "-machine", "pc-q35-7.0,usb=off,vmport=off,dump-guest-core=off\
|
||||
<#if vm.bootMode == "secure">,smm=on</#if>\
|
||||
<#if vm.bootMode != "bios">,pflash0=fw-rom-device\
|
||||
,pflash1=fw-eeprom-device</#if>,memory-backend=pc.ram,hpet=off" ]
|
||||
# {{- if .Values.vm.secureBoot }}
|
||||
# -global driver=cfi.pflash01,property=secure,value=on
|
||||
# -object '{"qom-type":"secret","id":"masterKey0","format":"raw","file":"/var/local/qemu/master-key.aes"}'
|
||||
# {{- end }}
|
||||
<#if vm.bootMode != "bios">
|
||||
# Provide ROM/EEPROM devices (instead of built-in BIOS)
|
||||
- [ "-blockdev", "node-name=fw-rom-file,driver=file,\
|
||||
filename=${ firmwareRom },auto-read-only=true,discard=unmap" ]
|
||||
- [ "-blockdev", "node-name=fw-rom-device,driver=raw,\
|
||||
read-only=true,file=fw-rom-file" ]
|
||||
- [ "-blockdev", "node-name=fw-eeprom-file,driver=file,\
|
||||
filename=${ firmwareFlash },auto-read-only=true,discard=unmap" ]
|
||||
- [ "-blockdev", "node-name=fw-eeprom-device,driver=raw,\
|
||||
read-only=false,file=fw-eeprom-file" ]
|
||||
</#if>
|
||||
# Provide RAM
|
||||
- [ "-object", "memory-backend-ram,id=pc.ram,\
|
||||
size=${ vm.maximumRam!"512M" }" ]
|
||||
|
||||
- [ "-chardev", "socket,id=charmonitor,path=${ runtimeDir }/monitor.sock,server=on,wait=off" ]
|
||||
- [ "-mon", "chardev=charmonitor,id=monitor,mode=control" ]
|
||||
# - [ "-spice", "port=5900,disable-ticketing=on" ]
|
||||
|
||||
"monitorMessages":
|
||||
"connect": '{ "execute": "qmp_capabilities" }'
|
||||
Loading…
Add table
Add a link
Reference in a new issue