Add powerdown through guest agent (more reliable).
This commit is contained in:
parent
2a3774ae24
commit
2d16cbc352
5 changed files with 181 additions and 11 deletions
|
|
@ -10,7 +10,7 @@ spec:
|
||||||
image:
|
image:
|
||||||
# source: ghcr.io/mnlipp/org.jdrupes.vmoperator.runner.qemu-arch:3.3.1
|
# source: ghcr.io/mnlipp/org.jdrupes.vmoperator.runner.qemu-arch:3.3.1
|
||||||
# source: registry.mnl.de/org/jdrupes/vm-operator/org.jdrupes.vmoperator.runner.qemu-arch:testing
|
# source: registry.mnl.de/org/jdrupes/vm-operator/org.jdrupes.vmoperator.runner.qemu-arch:testing
|
||||||
source: docker-registry.lan.mnl.de/vmoperator/org.jdrupes.vmoperator.runner.qemu-arch:feature-pools
|
source: docker-registry.lan.mnl.de/vmoperator/org.jdrupes.vmoperator.runner.qemu-arch:fix-runner-poweroff
|
||||||
pullPolicy: Always
|
pullPolicy: Always
|
||||||
|
|
||||||
permissions:
|
permissions:
|
||||||
|
|
|
||||||
|
|
@ -92,8 +92,12 @@ public abstract class AgentConnector extends QemuConnector {
|
||||||
*/
|
*/
|
||||||
@Handler
|
@Handler
|
||||||
public void onVserportChanged(VserportChangeEvent event) {
|
public void onVserportChanged(VserportChangeEvent event) {
|
||||||
if (event.id().equals(channelId) && event.isOpen()) {
|
if (event.id().equals(channelId)) {
|
||||||
agentConnected();
|
if (event.isOpen()) {
|
||||||
|
agentConnected();
|
||||||
|
} else {
|
||||||
|
agentDisconnected();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -105,4 +109,14 @@ public abstract class AgentConnector extends QemuConnector {
|
||||||
protected void agentConnected() {
|
protected void agentConnected() {
|
||||||
// Default is to do nothing.
|
// Default is to do nothing.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called when the agent in the VM closes the connection. The
|
||||||
|
* default implementation does nothing.
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("PMD.EmptyMethodInAbstractClassShouldBeAbstract")
|
||||||
|
protected void agentDisconnected() {
|
||||||
|
// Default is to do nothing.
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -21,15 +21,23 @@ package org.jdrupes.vmoperator.runner.qemu;
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.time.Instant;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.Queue;
|
import java.util.Queue;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
|
import org.jdrupes.vmoperator.runner.qemu.Constants.ProcessName;
|
||||||
import org.jdrupes.vmoperator.runner.qemu.commands.QmpCommand;
|
import org.jdrupes.vmoperator.runner.qemu.commands.QmpCommand;
|
||||||
import org.jdrupes.vmoperator.runner.qemu.commands.QmpGuestGetOsinfo;
|
import org.jdrupes.vmoperator.runner.qemu.commands.QmpGuestGetOsinfo;
|
||||||
|
import org.jdrupes.vmoperator.runner.qemu.commands.QmpGuestPowerdown;
|
||||||
|
import org.jdrupes.vmoperator.runner.qemu.events.ConfigureQemu;
|
||||||
import org.jdrupes.vmoperator.runner.qemu.events.GuestAgentCommand;
|
import org.jdrupes.vmoperator.runner.qemu.events.GuestAgentCommand;
|
||||||
import org.jdrupes.vmoperator.runner.qemu.events.OsinfoEvent;
|
import org.jdrupes.vmoperator.runner.qemu.events.OsinfoEvent;
|
||||||
import org.jgrapes.core.Channel;
|
import org.jgrapes.core.Channel;
|
||||||
|
import org.jgrapes.core.Components;
|
||||||
|
import org.jgrapes.core.Components.Timer;
|
||||||
import org.jgrapes.core.annotation.Handler;
|
import org.jgrapes.core.annotation.Handler;
|
||||||
|
import org.jgrapes.core.events.Stop;
|
||||||
|
import org.jgrapes.io.events.ProcessExited;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A component that handles the communication with the guest agent.
|
* A component that handles the communication with the guest agent.
|
||||||
|
|
@ -39,7 +47,12 @@ import org.jgrapes.core.annotation.Handler;
|
||||||
*/
|
*/
|
||||||
public class GuestAgentClient extends AgentConnector {
|
public class GuestAgentClient extends AgentConnector {
|
||||||
|
|
||||||
|
private boolean connected;
|
||||||
|
private Instant powerdownStartedAt;
|
||||||
|
private int powerdownTimeout;
|
||||||
|
private Timer powerdownTimer;
|
||||||
private final Queue<QmpCommand> executing = new LinkedList<>();
|
private final Queue<QmpCommand> executing = new LinkedList<>();
|
||||||
|
private Stop suspendedStop;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Instantiates a new guest agent client.
|
* Instantiates a new guest agent client.
|
||||||
|
|
@ -56,9 +69,17 @@ public class GuestAgentClient extends AgentConnector {
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
protected void agentConnected() {
|
protected void agentConnected() {
|
||||||
|
logger.fine(() -> "guest agent connected");
|
||||||
|
connected = true;
|
||||||
rep().fire(new GuestAgentCommand(new QmpGuestGetOsinfo()));
|
rep().fire(new GuestAgentCommand(new QmpGuestGetOsinfo()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void agentDisconnected() {
|
||||||
|
logger.fine(() -> "guest agent disconnected");
|
||||||
|
connected = false;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Process agent input.
|
* Process agent input.
|
||||||
*
|
*
|
||||||
|
|
@ -88,10 +109,11 @@ public class GuestAgentClient extends AgentConnector {
|
||||||
* On guest agent command.
|
* On guest agent command.
|
||||||
*
|
*
|
||||||
* @param event the event
|
* @param event the event
|
||||||
* @throws IOException
|
* @throws IOException Signals that an I/O exception has occurred.
|
||||||
*/
|
*/
|
||||||
@Handler
|
@Handler
|
||||||
@SuppressWarnings("PMD.AvoidSynchronizedStatement")
|
@SuppressWarnings({ "PMD.AvoidSynchronizedStatement",
|
||||||
|
"PMD.AvoidDuplicateLiterals" })
|
||||||
public void onGuestAgentCommand(GuestAgentCommand event)
|
public void onGuestAgentCommand(GuestAgentCommand event)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
if (qemuChannel() == null) {
|
if (qemuChannel() == null) {
|
||||||
|
|
@ -114,4 +136,89 @@ public class GuestAgentClient extends AgentConnector {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Shutdown the VM.
|
||||||
|
*
|
||||||
|
* @param event the event
|
||||||
|
*/
|
||||||
|
@Handler(priority = 200)
|
||||||
|
@SuppressWarnings("PMD.AvoidSynchronizedStatement")
|
||||||
|
public void onStop(Stop event) {
|
||||||
|
if (!connected) {
|
||||||
|
logger.fine(() -> "No guest agent connection,"
|
||||||
|
+ " cannot send shutdown command");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// We have a connection to the guest agent attempt shutdown.
|
||||||
|
powerdownStartedAt = event.associated(Instant.class).orElseGet(() -> {
|
||||||
|
var now = Instant.now();
|
||||||
|
event.setAssociated(Instant.class, now);
|
||||||
|
return now;
|
||||||
|
});
|
||||||
|
var waitUntil = powerdownStartedAt.plusSeconds(powerdownTimeout);
|
||||||
|
if (waitUntil.isBefore(Instant.now())) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
event.suspendHandling();
|
||||||
|
suspendedStop = event;
|
||||||
|
logger.fine(() -> "Sending powerdown command, waiting for"
|
||||||
|
+ " termination until " + waitUntil);
|
||||||
|
powerdownTimer = Components.schedule(t -> {
|
||||||
|
logger.fine(() -> "Powerdown timeout reached.");
|
||||||
|
synchronized (this) {
|
||||||
|
powerdownTimer = null;
|
||||||
|
if (suspendedStop != null) {
|
||||||
|
suspendedStop.resumeHandling();
|
||||||
|
suspendedStop = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, waitUntil);
|
||||||
|
rep().fire(new GuestAgentCommand(new QmpGuestPowerdown()));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* On process exited.
|
||||||
|
*
|
||||||
|
* @param event the event
|
||||||
|
*/
|
||||||
|
@Handler
|
||||||
|
@SuppressWarnings("PMD.AvoidSynchronizedStatement")
|
||||||
|
public void onProcessExited(ProcessExited event) {
|
||||||
|
if (!event.startedBy().associated(CommandDefinition.class)
|
||||||
|
.map(cd -> ProcessName.QEMU.equals(cd.name())).orElse(false)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
synchronized (this) {
|
||||||
|
if (powerdownTimer != null) {
|
||||||
|
powerdownTimer.cancel();
|
||||||
|
}
|
||||||
|
if (suspendedStop != null) {
|
||||||
|
suspendedStop.resumeHandling();
|
||||||
|
suspendedStop = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* On configure qemu.
|
||||||
|
*
|
||||||
|
* @param event the event
|
||||||
|
*/
|
||||||
|
@Handler
|
||||||
|
@SuppressWarnings("PMD.AvoidSynchronizedStatement")
|
||||||
|
public void onConfigureQemu(ConfigureQemu event) {
|
||||||
|
int newTimeout = event.configuration().vm.powerdownTimeout;
|
||||||
|
if (powerdownTimeout != newTimeout) {
|
||||||
|
powerdownTimeout = newTimeout;
|
||||||
|
synchronized (this) {
|
||||||
|
if (powerdownTimer != null) {
|
||||||
|
powerdownTimer
|
||||||
|
.reschedule(powerdownStartedAt.plusSeconds(newTimeout));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -197,12 +197,20 @@ public class QemuMonitor extends QemuConnector {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// We have a connection to Qemu, attempt ACPI shutdown.
|
// We have a connection to Qemu, attempt ACPI shutdown if time left
|
||||||
|
powerdownStartedAt = event.associated(Instant.class).orElseGet(() -> {
|
||||||
|
var now = Instant.now();
|
||||||
|
event.setAssociated(Instant.class, now);
|
||||||
|
return now;
|
||||||
|
});
|
||||||
|
if (powerdownStartedAt.plusSeconds(powerdownTimeout)
|
||||||
|
.isBefore(Instant.now())) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
event.suspendHandling();
|
event.suspendHandling();
|
||||||
suspendedStop = event;
|
suspendedStop = event;
|
||||||
|
|
||||||
// Attempt powerdown command. If not confirmed, assume
|
// Send command. If not confirmed, assume "hanging" qemu process.
|
||||||
// "hanging" qemu process.
|
|
||||||
powerdownTimer = Components.schedule(t -> {
|
powerdownTimer = Components.schedule(t -> {
|
||||||
logger.fine(() -> "QMP powerdown command not confirmed");
|
logger.fine(() -> "QMP powerdown command not confirmed");
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
|
|
@ -213,9 +221,8 @@ public class QemuMonitor extends QemuConnector {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, Duration.ofSeconds(5));
|
}, Duration.ofSeconds(5));
|
||||||
logger.fine(() -> "Attempting QMP powerdown.");
|
logger.fine(() -> "Attempting QMP (ACPI) powerdown.");
|
||||||
powerdownStartedAt = Instant.now();
|
rep().fire(new MonitorCommand(new QmpPowerdown()));
|
||||||
fire(new MonitorCommand(new QmpPowerdown()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -241,6 +248,7 @@ public class QemuMonitor extends QemuConnector {
|
||||||
powerdownTimer = Components.schedule(t -> {
|
powerdownTimer = Components.schedule(t -> {
|
||||||
logger.fine(() -> "Powerdown timeout reached.");
|
logger.fine(() -> "Powerdown timeout reached.");
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
|
powerdownTimer = null;
|
||||||
if (suspendedStop != null) {
|
if (suspendedStop != null) {
|
||||||
suspendedStop.resumeHandling();
|
suspendedStop.resumeHandling();
|
||||||
suspendedStop = null;
|
suspendedStop = null;
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,41 @@
|
||||||
|
/*
|
||||||
|
* VM-Operator
|
||||||
|
* Copyright (C) 2025 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.commands;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
|
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A {@link QmpCommand} that powers down the guest.
|
||||||
|
*/
|
||||||
|
public class QmpGuestPowerdown extends QmpCommand {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public JsonNode toJson() {
|
||||||
|
ObjectNode cmd = mapper.createObjectNode();
|
||||||
|
cmd.put("execute", "guest-shutdown");
|
||||||
|
return cmd;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "QmpGuestPowerdown()";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue