From: Jakob-Tobias Winter <tobias@stuttgart.de>
To: pve-devel@lists.proxmox.com
Cc: Jakob-Tobias Winter <tobias@stuttgart.de>
Subject: [RFC PATCH ha-manager 03/11] lrm: do not start a service again that is about to be stopped
Date: Sat, 28 Nov 2026 16:11:10 +0100 [thread overview]
Message-ID: <20261128151118.892037-4-tobias@stuttgart.de> (raw)
In-Reply-To: <20261128151118.892037-1-tobias@stuttgart.de>
The LRM checks about every 10 seconds whether the services it has to
keep started are running, and starts those that are not. The cleanup
after a guest that got powered off from within only runs once the
guest is gone, so the LRM can start the guest again before the stopped
state is requested. The CRM then stops the guest right after it
booted.
With stop-on-poweroff enabled, wait for a pending cleanup before
starting a service, for at most 10 seconds, and then check whether it
still is requested to be started. If not, leave the service to the
CRM. The config is read again for that, after updating the state of
the cluster file system, as the worker would otherwise still get the
config from before the cleanup. If that update fails, try again in the
next round, like the LRM itself skips a round then.
For virtual machines, the cleanup is pending as long as the flag
exists that qemu-server creates when it starts a guest.
Signed-off-by: Jakob-Tobias Winter <tobias@stuttgart.de>
---
src/PVE/HA/LRM.pm | 23 +++++++++++++++++++++++
src/PVE/HA/Resources.pm | 7 +++++++
src/PVE/HA/Resources/PVEVM.pm | 12 ++++++++++++
3 files changed, 42 insertions(+)
diff --git a/src/PVE/HA/LRM.pm b/src/PVE/HA/LRM.pm
index deffecd..891fd48 100644
--- a/src/PVE/HA/LRM.pm
+++ b/src/PVE/HA/LRM.pm
@@ -24,6 +24,9 @@ my $valid_states = {
# state giving up the watchdog and the LRM lock voluntary, ensuring the WD can do no harm
my $max_active_idle_rounds = 60;
+# seconds to wait for the cleanup after a service that is not running anymore, before starting it
+my $max_cleanup_wait_time = 10;
+
sub new {
my ($this, $haenv) = @_;
@@ -1029,6 +1032,26 @@ sub exec_resource_agent {
return SUCCESS if $running;
+ if ($haenv->get_datacenter_settings()->{ha}->{'stop-on-poweroff'}) {
+ # If the guest got powered off from within, the stopped state gets requested for the
+ # service before the cleanup after the guest is done. So wait for the cleanup, and
+ # leave a service alone that the CRM is about to stop.
+ my $waited = 0;
+ while ($plugin->cleanup_pending($haenv, $id) && $waited++ < $max_cleanup_wait_time) {
+ $haenv->sleep(1);
+ }
+
+ # the config may have changed since the start of this round, so read it again
+ if (!$haenv->cluster_state_update()) {
+ return ETRY_AGAIN; # would only get the old config, so try again in the next round
+ }
+ my $cd = $haenv->read_service_config()->{$sid};
+ if (!$cd || $cd->{state} ne 'started') {
+ $haenv->log('info', "service $sid is not requested to be started anymore");
+ return ETRY_AGAIN; # no result for the CRM, it changes the state of the service
+ }
+ }
+
$haenv->log("info", "starting service $sid");
$plugin->start($haenv, $id);
diff --git a/src/PVE/HA/Resources.pm b/src/PVE/HA/Resources.pm
index e776247..624c72b 100644
--- a/src/PVE/HA/Resources.pm
+++ b/src/PVE/HA/Resources.pm
@@ -206,6 +206,13 @@ sub check_running {
die "implement in subclass";
}
+# Whether the resource is not running anymore, while the cleanup after it is not done yet.
+sub cleanup_pending {
+ my ($class, $haenv, $id) = @_;
+
+ return 0;
+}
+
sub remove_locks {
my ($self, $haenv, $id, $locks, $service_node) = @_;
diff --git a/src/PVE/HA/Resources/PVEVM.pm b/src/PVE/HA/Resources/PVEVM.pm
index 9698937..8fcfaad 100644
--- a/src/PVE/HA/Resources/PVEVM.pm
+++ b/src/PVE/HA/Resources/PVEVM.pm
@@ -17,6 +17,8 @@ BEGIN {
import PVE::QemuServer;
require PVE::QemuServer::Monitor;
import PVE::QemuServer::Monitor;
+ require PVE::QemuServer::RunState;
+ import PVE::QemuServer::RunState;
require PVE::API2::Qemu;
import PVE::API2::Qemu;
}
@@ -199,6 +201,16 @@ sub check_running {
}
}
+sub cleanup_pending {
+ my ($class, $haenv, $vmid) = @_;
+
+ return 0 if $class->check_running($haenv, $vmid);
+
+ # the flag exists from the start of a VM until the cleanup after it is done
+ return PVE::QemuServer::RunState::can_use_cleanup_flag()
+ && PVE::QemuServer::RunState::cleanup_flag_exists($vmid);
+}
+
sub remove_locks {
my ($self, $haenv, $id, $locks, $service_node) = @_;
--
2.53.0
next prev parent reply other threads:[~2026-11-28 15:21 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-28 15:11 [RFC PATCH cluster/ha-manager/qemu-server/manager/docs 00/11] fix #6314: ha: keep guests stopped that got powered off from within Jakob-Tobias Winter
2026-11-28 15:11 ` [RFC PATCH cluster 01/11] datacenter config: add stop-on-poweroff to the HA settings Jakob-Tobias Winter
2026-11-28 15:11 ` [RFC PATCH ha-manager 02/11] fix #6314: config: request stopped state for powered off guests Jakob-Tobias Winter
2026-11-28 15:11 ` Jakob-Tobias Winter [this message]
2026-11-28 15:11 ` [RFC PATCH ha-manager 04/11] sim: hardware: add poweroff command for services Jakob-Tobias Winter
2026-11-28 15:11 ` [RFC PATCH ha-manager 05/11] test: add test cases for stop-on-poweroff Jakob-Tobias Winter
2026-11-28 15:11 ` [RFC PATCH ha-manager 06/11] resources: add stop-on-poweroff property for virtual machines Jakob-Tobias Winter
2026-11-28 15:11 ` [RFC PATCH qemu-server 07/11] fix #6314: qm cleanup: report a guest poweroff to the HA stack Jakob-Tobias Winter
2026-11-28 15:11 ` [RFC PATCH manager 08/11] ui: dc options: add stop-on-poweroff to the HA settings Jakob-Tobias Winter
2026-11-28 15:11 ` [RFC PATCH manager 09/11] ui: ha: add stop-on-poweroff selector to the resource edit window Jakob-Tobias Winter
2026-11-28 15:11 ` [RFC PATCH docs 10/11] ha-manager: document the stop-on-poweroff setting Jakob-Tobias Winter
2026-11-28 15:11 ` [RFC PATCH docs 11/11] ha-manager: document the stop-on-poweroff property of resources Jakob-Tobias Winter
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261128151118.892037-4-tobias@stuttgart.de \
--to=tobias@stuttgart.de \
--cc=pve-devel@lists.proxmox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox