* [RFC PATCH cluster/ha-manager/qemu-server/manager/docs 00/11] fix #6314: ha: keep guests stopped that got powered off from within
@ 2026-11-28 15:11 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
` (10 more replies)
0 siblings, 11 replies; 12+ messages in thread
From: Jakob-Tobias Winter @ 2026-11-28 15:11 UTC (permalink / raw)
To: pve-devel; +Cc: Jakob-Tobias Winter
An HA managed guest that gets powered off from within is started again
by the HA stack [0]. To keep it stopped, it has to be shut down through
Proxmox VE, which someone who only has access to the guest cannot do,
and which is not what people coming from other platforms expect.
This series adds a setting, off by default, with which the HA stack
sets the requested state to "stopped" for a virtual machine that got
powered off from within the guest. The resource is then handled like
one that was shut down through the API: it stays stopped, it is still
recovered if its node fails, and starting it through the API requests
the "started" state again. A VM that crashes is still started again.
# pvesh set /cluster/options --ha stop-on-poweroff=1
# ha-manager set vm:100 --stop-on-poweroff 0
How it works
------------
qmeventd already tells "qm cleanup" whether QEMU reported a clean
shutdown that was requested by the guest. The cleanup now passes that
on to the HA stack (qemu-server), which sets the requested state if
the setting applies (ha-manager 2/11). Two things need more than
that:
* QEMU reports a shutdown that the host asked for through ACPI or the
guest agent in the same way, and the HA stack shuts guests down
itself while their requested state stays "started": the LRM stops
all services of a node that is shut down or rebooted, and relocating
a service shuts its guest down first. With the patch sketched in [0]
those guests would stay stopped.
A shutdown or reboot through the API does the cleanup itself, and
since qemu-server 9.1.13 a flag tells whether the cleanup after a
guest is still left to do. So "qm cleanup" only reports a guest for
which it is. On top of that, the HA stack only honors a report while
the CRM state of the service is "started", the LRM of its node is in
mode "active" and the HA stack is not disarmed. This covers a
shutdown task that got aborted before its cleanup, and a guest that
powers itself off while HA is disarmed in the "ignore" mode: its
requested state would still be "stopped" after a direct start, and
arming HA would stop it again.
* The cleanup only runs once the QEMU process is gone, while the LRM
looks about every 10 seconds whether its services are running. If
its round comes in between, it starts the guest again, and the CRM
stops it shortly after. With the setting enabled, the LRM therefore
waits for a pending cleanup of the guest before it starts it, for at
most 10 seconds, and then checks whether the service still is
requested to be started (ha-manager 3/11).
With the check, the guest stayed stopped in 8 of 8 poweroffs on a
single node with real daemons; in 4 of them the round of the LRM
came first and the check held it off. With the check switched off,
and so with another order of the rounds after restarting the LRM,
the guest was started again in 2 of 8.
The series
----------
cluster 1 the setting in the HA settings of datacenter.cfg
ha-manager 2-5 the request, the wait and check in the LRM, a simulator
command for a guest that powers off, test cases
ha-manager 6 optional: a property to override the cluster wide
setting for single resources
qemu-server 7 the report from the cleanup
manager 8 checkbox in Datacenter -> Options -> HA Settings
manager 9 optional: selector in the resource edit window
docs 10, 11 description, 11 for the optional property
The property for single resources (6, 9, 11) is a nice to have on top
and can be left out without touching the rest.
Open questions
--------------
* The LRM asks the VM plugin whether the cleanup flag of qemu-server
still exists. Making a start wait for a pending cleanup was
discussed for qemu-server itself in [1]; if that is the better place,
the LRM would only need the check of the requested state.
* On a node that still uses the old cleanup, that is until the first
boot after an upgrade from a qemu-server before 9.1.13, nothing is
reported and guests are started again as before. Is that worth a
sentence in the documentation?
* qmeventd only passes on whether the guest requested the shutdown.
So a few more things count as a poweroff from within: a guest that
shuts down after a reboot through the API ran into its timeout, and
a shutdown asked for by the watchdog device with the "shutdown"
action, or through the guest agent or the monitor directly. Such a
guest stays stopped like one that is not HA managed. Is that
acceptable for an opt-in setting?
* Containers are not covered: their post-stop hook cannot tell a stop
by the host from one inside the container, and a backup in stop mode
and "pct reboot" stop them without the HA stack knowing.
* The name. "stop-on-poweroff" follows the title of [0] and the style
of "auto-rebalance".
* The series for bug 7213 [2] lets qmeventd report a reset to the
cleanup. With it, such a reset has to be left out here as well, I
can rebase onto whatever gets applied first.
Dependencies between the packages are in the notes of the patches:
qemu-server needs the pve-ha-manager with patch 2, and the checkbox a
libpve-cluster-perl with patch 1. pve-ha-manager already depends on a
qemu-server with the cleanup flag.
Testing
-------
* test suites of pve-ha-manager for every commit of the series, with
six new simulator cases and a table test for the decision; test
suite of qemu-server
* real pve-ha-crm, pve-ha-lrm, qmeventd and pvedaemon of the packages
of Proxmox VE 9.3 with the series, on a single node without KVM,
with a VM that powers itself off through ACPI: the default (started
again), the setting (stays stopped, can be started again, started
again after its process got killed), the property of a resource in
both directions, a VM that is not HA managed, a node in maintenance
mode, a reboot through the API, a poweroff right after HA got
disarmed and a shutdown through the API while it is disarmed, a
node with the old cleanup, a cleanup that is held up until the next
round of the LRM, and the poweroff repeated with and without the
check in the LRM
* both parts of the web interface in a browser
* not tested: a cluster with more than one node, so node shutdown and
relocation are only covered by the simulator and by the table test
[0] https://bugzilla.proxmox.com/show_bug.cgi?id=6314
[1] https://lore.proxmox.com/pve-devel/28c91ab7-b41d-446e-b31c-5800ced4ad61@proxmox.com/
[2] https://lore.proxmox.com/pve-devel/20261022125111.236538-1-j.klocker@proxmox.com/
cluster:
Jakob-Tobias Winter (1):
datacenter config: add stop-on-poweroff to the HA settings
src/PVE/DataCenterConfig.pm | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)
ha-manager:
Jakob-Tobias Winter (5):
fix #6314: config: request stopped state for powered off guests
lrm: do not start a service again that is about to be stopped
sim: hardware: add poweroff command for services
test: add test cases for stop-on-poweroff
resources: add stop-on-poweroff property for virtual machines
src/PVE/API2/HA/Resources.pm | 7 +
src/PVE/HA/Config.pm | 38 ++++
src/PVE/HA/LRM.pm | 24 +++
src/PVE/HA/Resources.pm | 14 ++
src/PVE/HA/Resources/PVEVM.pm | 13 ++
src/PVE/HA/Sim/Hardware.pm | 23 +++
src/PVE/HA/Tools.pm | 27 +++
src/test/Makefile | 1 +
src/test/test-stop-on-poweroff1/README | 3 +
src/test/test-stop-on-poweroff1/cmdlist | 5 +
.../test-stop-on-poweroff1/datacenter.cfg | 5 +
.../test-stop-on-poweroff1/hardware_status | 5 +
src/test/test-stop-on-poweroff1/log.expect | 32 ++++
.../test-stop-on-poweroff1/manager_status | 1 +
.../test-stop-on-poweroff1/service_config | 3 +
src/test/test-stop-on-poweroff2/README | 2 +
src/test/test-stop-on-poweroff2/cmdlist | 4 +
.../test-stop-on-poweroff2/hardware_status | 5 +
src/test/test-stop-on-poweroff2/log.expect | 27 +++
.../test-stop-on-poweroff2/manager_status | 1 +
.../test-stop-on-poweroff2/service_config | 3 +
src/test/test-stop-on-poweroff3/README | 4 +
src/test/test-stop-on-poweroff3/cmdlist | 5 +
.../test-stop-on-poweroff3/datacenter.cfg | 5 +
.../test-stop-on-poweroff3/hardware_status | 5 +
src/test/test-stop-on-poweroff3/log.expect | 34 ++++
.../test-stop-on-poweroff3/manager_status | 1 +
.../test-stop-on-poweroff3/service_config | 3 +
src/test/test-stop-on-poweroff4/README | 4 +
src/test/test-stop-on-poweroff4/cmdlist | 4 +
.../test-stop-on-poweroff4/datacenter.cfg | 5 +
.../test-stop-on-poweroff4/hardware_status | 5 +
src/test/test-stop-on-poweroff4/log.expect | 43 +++++
.../test-stop-on-poweroff4/manager_status | 1 +
.../test-stop-on-poweroff4/service_config | 3 +
src/test/test-stop-on-poweroff5/README | 5 +
src/test/test-stop-on-poweroff5/cmdlist | 5 +
.../test-stop-on-poweroff5/hardware_status | 5 +
src/test/test-stop-on-poweroff5/log.expect | 41 +++++
.../test-stop-on-poweroff5/manager_status | 1 +
.../test-stop-on-poweroff5/service_config | 4 +
src/test/test-stop-on-poweroff6/README | 3 +
src/test/test-stop-on-poweroff6/cmdlist | 4 +
.../test-stop-on-poweroff6/datacenter.cfg | 5 +
.../test-stop-on-poweroff6/hardware_status | 5 +
src/test/test-stop-on-poweroff6/log.expect | 34 ++++
.../test-stop-on-poweroff6/manager_status | 1 +
.../test-stop-on-poweroff6/service_config | 4 +
src/test/test_stop_on_poweroff.pl | 173 ++++++++++++++++++
49 files changed, 655 insertions(+)
create mode 100644 src/test/test-stop-on-poweroff1/README
create mode 100644 src/test/test-stop-on-poweroff1/cmdlist
create mode 100644 src/test/test-stop-on-poweroff1/datacenter.cfg
create mode 100644 src/test/test-stop-on-poweroff1/hardware_status
create mode 100644 src/test/test-stop-on-poweroff1/log.expect
create mode 100644 src/test/test-stop-on-poweroff1/manager_status
create mode 100644 src/test/test-stop-on-poweroff1/service_config
create mode 100644 src/test/test-stop-on-poweroff2/README
create mode 100644 src/test/test-stop-on-poweroff2/cmdlist
create mode 100644 src/test/test-stop-on-poweroff2/hardware_status
create mode 100644 src/test/test-stop-on-poweroff2/log.expect
create mode 100644 src/test/test-stop-on-poweroff2/manager_status
create mode 100644 src/test/test-stop-on-poweroff2/service_config
create mode 100644 src/test/test-stop-on-poweroff3/README
create mode 100644 src/test/test-stop-on-poweroff3/cmdlist
create mode 100644 src/test/test-stop-on-poweroff3/datacenter.cfg
create mode 100644 src/test/test-stop-on-poweroff3/hardware_status
create mode 100644 src/test/test-stop-on-poweroff3/log.expect
create mode 100644 src/test/test-stop-on-poweroff3/manager_status
create mode 100644 src/test/test-stop-on-poweroff3/service_config
create mode 100644 src/test/test-stop-on-poweroff4/README
create mode 100644 src/test/test-stop-on-poweroff4/cmdlist
create mode 100644 src/test/test-stop-on-poweroff4/datacenter.cfg
create mode 100644 src/test/test-stop-on-poweroff4/hardware_status
create mode 100644 src/test/test-stop-on-poweroff4/log.expect
create mode 100644 src/test/test-stop-on-poweroff4/manager_status
create mode 100644 src/test/test-stop-on-poweroff4/service_config
create mode 100644 src/test/test-stop-on-poweroff5/README
create mode 100644 src/test/test-stop-on-poweroff5/cmdlist
create mode 100644 src/test/test-stop-on-poweroff5/hardware_status
create mode 100644 src/test/test-stop-on-poweroff5/log.expect
create mode 100644 src/test/test-stop-on-poweroff5/manager_status
create mode 100644 src/test/test-stop-on-poweroff5/service_config
create mode 100644 src/test/test-stop-on-poweroff6/README
create mode 100644 src/test/test-stop-on-poweroff6/cmdlist
create mode 100644 src/test/test-stop-on-poweroff6/datacenter.cfg
create mode 100644 src/test/test-stop-on-poweroff6/hardware_status
create mode 100644 src/test/test-stop-on-poweroff6/log.expect
create mode 100644 src/test/test-stop-on-poweroff6/manager_status
create mode 100644 src/test/test-stop-on-poweroff6/service_config
create mode 100755 src/test/test_stop_on_poweroff.pl
qemu-server:
Jakob-Tobias Winter (1):
fix #6314: qm cleanup: report a guest poweroff to the HA stack
src/PVE/CLI/qm.pm | 24 ++++++++++++++++++++++++
1 file changed, 24 insertions(+)
manager:
Jakob-Tobias Winter (2):
ui: dc options: add stop-on-poweroff to the HA settings
ui: ha: add stop-on-poweroff selector to the resource edit window
www/manager6/dc/OptionView.js | 11 +++++++++++
www/manager6/ha/ResourceEdit.js | 28 ++++++++++++++++++++++++++++
2 files changed, 39 insertions(+)
docs:
Jakob-Tobias Winter (2):
ha-manager: document the stop-on-poweroff setting
ha-manager: document the stop-on-poweroff property of resources
ha-manager.adoc | 47 +++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 47 insertions(+)
Summary over all repositories:
54 files changed, 773 insertions(+), 1 deletion(-)
--
2.53.0
^ permalink raw reply [flat|nested] 12+ messages in thread
* [RFC PATCH cluster 01/11] datacenter config: add stop-on-poweroff to the HA settings
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 ` 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
` (9 subsequent siblings)
10 siblings, 0 replies; 12+ messages in thread
From: Jakob-Tobias Winter @ 2026-11-28 15:11 UTC (permalink / raw)
To: pve-devel; +Cc: Jakob-Tobias Winter
An HA managed virtual machine that gets powered off from within the
guest is started again by the HA stack, as its requested state still
is 'started'. This is not what users expect from a guest they have shut
down on purpose.
Add an option to the cluster wide HA settings, with which the HA stack
sets the requested state of such a guest to 'stopped' instead. It is
off by default.
Make the shutdown policy optional in the property string, so that the
new option can be set without also selecting a policy.
Signed-off-by: Jakob-Tobias Winter <tobias@stuttgart.de>
---
A node with a libpve-cluster-perl that does not know the new key drops
the whole "ha" property string when it parses datacenter.cfg, the
shutdown policy included. That is how every new key in a property
string of this file behaves; the documentation patch says to enable
the setting only after all nodes got upgraded.
src/PVE/DataCenterConfig.pm | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)
diff --git a/src/PVE/DataCenterConfig.pm b/src/PVE/DataCenterConfig.pm
index cb78fd2..741b977 100644
--- a/src/PVE/DataCenterConfig.pm
+++ b/src/PVE/DataCenterConfig.pm
@@ -197,7 +197,14 @@ register_standard_option(
);
my $ha_format = {
- shutdown_policy => get_standard_option('pve-ha-shutdown-policy'),
+ shutdown_policy => get_standard_option('pve-ha-shutdown-policy', { optional => 1 }),
+ 'stop-on-poweroff' => {
+ type => 'boolean',
+ optional => 1,
+ default => 0,
+ description => "Set the requested state of HA managed virtual machines to 'stopped' if"
+ . " they get powered off from within the guest, instead of starting them again.",
+ },
};
my $next_id_format = {
base-commit: 2a8bc69a99a399771d746d7a76e42ab3ff75cc0e
--
2.53.0
^ permalink raw reply related [flat|nested] 12+ messages in thread
* [RFC PATCH ha-manager 02/11] fix #6314: config: request stopped state for powered off guests
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 ` Jakob-Tobias Winter
2026-11-28 15:11 ` [RFC PATCH ha-manager 03/11] lrm: do not start a service again that is about to be stopped Jakob-Tobias Winter
` (8 subsequent siblings)
10 siblings, 0 replies; 12+ messages in thread
From: Jakob-Tobias Winter @ 2026-11-28 15:11 UTC (permalink / raw)
To: pve-devel; +Cc: Jakob-Tobias Winter
A guest that gets powered off from within is started again by the LRM,
as the HA stack does not learn why the guest is not running anymore.
Add a helper for the cleanup after a guest to report such a poweroff.
If stop-on-poweroff is enabled in the cluster wide HA settings, the
requested state of the service is set to stopped, like a shutdown
through the API does. The service still gets recovered if its node
fails, and starting the guest through the API requests the started
state again.
The HA stack shuts guests down itself, for example when their node is
shut down or when a service gets relocated, and an admin can do so
directly while the HA stack is disarmed. Such a guest must not end up
stopped if it gets reported, for example because the task that shut it
down got aborted. So only stop a service that the CRM just keeps
started, on a node whose LRM is in normal operation, and not while the
HA stack is disarmed.
The setting is off by default, so nothing changes without it.
Signed-off-by: Jakob-Tobias Winter <tobias@stuttgart.de>
---
src/PVE/HA/Config.pm | 38 ++++++++
src/PVE/HA/Tools.pm | 19 ++++
src/test/Makefile | 1 +
src/test/test_stop_on_poweroff.pl | 157 ++++++++++++++++++++++++++++++
4 files changed, 215 insertions(+)
create mode 100755 src/test/test_stop_on_poweroff.pl
diff --git a/src/PVE/HA/Config.pm b/src/PVE/HA/Config.pm
index 5f452ff..bde1b34 100644
--- a/src/PVE/HA/Config.pm
+++ b/src/PVE/HA/Config.pm
@@ -6,6 +6,7 @@ use warnings;
use JSON;
use PVE::Cluster qw(cfs_register_file cfs_read_file cfs_write_file cfs_lock_file);
+use PVE::DataCenterConfig;
use PVE::HA::Groups;
use PVE::HA::Helpers;
@@ -511,4 +512,41 @@ sub get_service_status {
return $status;
}
+# To be called for a guest that got powered off from within. Requests the stopped state for its
+# service, if the HA stack is configured to do so, so that the LRM does not start the guest
+# again. Returns whether the stopped state got requested.
+sub request_stop_on_poweroff {
+ my ($sid) = @_;
+
+ my $applies = sub {
+ my $cd = read_and_check_resources_config()->{$sid} || return 0;
+ my $ms = read_manager_status();
+ my $sd = $ms->{service_status}->{$sid} || return 0;
+ my $lrm_mode = read_lrm_status($sd->{node})->{mode};
+ my $ha_settings = cfs_read_file('datacenter.cfg')->{ha};
+
+ return PVE::HA::Tools::stop_on_poweroff_applies(
+ $cd, $sd, $ms->{disarm}, $lrm_mode, $ha_settings,
+ );
+ };
+
+ # this is asked for every guest, so only take the cluster wide lock if there is work to do
+ return 0 if !$applies->();
+
+ my $requested = 0;
+ lock_ha_domain(
+ sub {
+ return if !$applies->();
+
+ my $cfg = read_resources_config();
+ update_single_resource_config_inplace($cfg, $sid, { state => 'stopped' });
+ write_resources_config($cfg);
+ $requested = 1;
+ },
+ "requesting the stopped state for '$sid' failed",
+ );
+
+ return $requested;
+}
+
1;
diff --git a/src/PVE/HA/Tools.pm b/src/PVE/HA/Tools.pm
index cc8c0f1..c4d26ca 100644
--- a/src/PVE/HA/Tools.pm
+++ b/src/PVE/HA/Tools.pm
@@ -235,6 +235,25 @@ sub get_disarm_deferring_services {
return $disarm_deferring_sids;
}
+# Returns whether the stopped state gets requested for a service, whose guest got powered off
+# from within. Takes the config of the service with defaults set, its status and the disarm state
+# from the CRM, the mode of the LRM on its node and the cluster wide HA settings.
+#
+# The HA stack shuts guests down itself, and so can an admin while the HA stack is disarmed. Such
+# a guest must not end up stopped if it gets reported. So this only applies while the HA stack
+# keeps the service started, but not while it is disarmed, while it moves, stops or freezes the
+# service, or while the node of the service gets shut down or is in maintenance.
+sub stop_on_poweroff_applies {
+ my ($cd, $sd, $disarm, $lrm_mode, $ha_settings) = @_;
+
+ return 0 if !$ha_settings->{'stop-on-poweroff'};
+ return 0 if !$cd || $cd->{state} ne 'started';
+ return 0 if $disarm || !$sd || $sd->{state} ne 'started';
+ return 0 if !defined($lrm_mode) || $lrm_mode ne 'active';
+
+ return 1;
+}
+
sub get_verbose_service_state {
my ($service_state, $service_conf) = @_;
diff --git a/src/test/Makefile b/src/test/Makefile
index 462c42f..caf29b2 100644
--- a/src/test/Makefile
+++ b/src/test/Makefile
@@ -8,6 +8,7 @@ test:
./test_rules_config.pl
./test_resource_bundles.pl
./test_resource_identity_check.pl
+ ./test_stop_on_poweroff.pl
./ha-tester.pl
./test_fence_config.pl
@echo "-- end regression tests (success) --"
diff --git a/src/test/test_stop_on_poweroff.pl b/src/test/test_stop_on_poweroff.pl
new file mode 100755
index 0000000..f256424
--- /dev/null
+++ b/src/test/test_stop_on_poweroff.pl
@@ -0,0 +1,157 @@
+#!/usr/bin/perl
+
+use v5.36;
+
+use lib qw(..);
+
+use Test::More;
+
+use PVE::HA::Tools;
+
+my $enabled = { 'stop-on-poweroff' => 1 };
+
+my $tests = [
+ {
+ description => "service that the HA stack keeps started",
+ config => { state => 'started' },
+ status => { state => 'started', node => 'node1' },
+ lrm_mode => 'active',
+ ha_settings => $enabled,
+ applies => 1,
+ },
+ {
+ description => "stop-on-poweroff not set",
+ config => { state => 'started' },
+ status => { state => 'started', node => 'node1' },
+ lrm_mode => 'active',
+ ha_settings => {},
+ applies => 0,
+ },
+ {
+ description => "stop-on-poweroff disabled",
+ config => { state => 'started' },
+ status => { state => 'started', node => 'node1' },
+ lrm_mode => 'active',
+ ha_settings => { 'stop-on-poweroff' => 0 },
+ applies => 0,
+ },
+ {
+ description => "no cluster wide HA settings",
+ config => { state => 'started' },
+ status => { state => 'started', node => 'node1' },
+ lrm_mode => 'active',
+ ha_settings => undef,
+ applies => 0,
+ },
+ {
+ description => "service that is requested to be stopped already",
+ config => { state => 'stopped' },
+ status => { state => 'started', node => 'node1' },
+ lrm_mode => 'active',
+ ha_settings => $enabled,
+ applies => 0,
+ },
+ {
+ description => "service that is not configured",
+ config => undef,
+ status => { state => 'started', node => 'node1' },
+ lrm_mode => 'active',
+ ha_settings => $enabled,
+ applies => 0,
+ },
+ {
+ description => "service that the CRM does not know yet",
+ config => { state => 'started' },
+ status => undef,
+ lrm_mode => undef,
+ ha_settings => $enabled,
+ applies => 0,
+ },
+ {
+ description => "service that gets relocated",
+ config => { state => 'started' },
+ status => { state => 'relocate', node => 'node1', target => 'node2' },
+ lrm_mode => 'active',
+ ha_settings => $enabled,
+ applies => 0,
+ },
+ {
+ description => "service that gets migrated",
+ config => { state => 'started' },
+ status => { state => 'migrate', node => 'node1', target => 'node2' },
+ lrm_mode => 'active',
+ ha_settings => $enabled,
+ applies => 0,
+ },
+ {
+ description => "service that gets stopped",
+ config => { state => 'started' },
+ status => { state => 'request_stop', node => 'node1' },
+ lrm_mode => 'active',
+ ha_settings => $enabled,
+ applies => 0,
+ },
+ {
+ description => "frozen service",
+ config => { state => 'started' },
+ status => { state => 'freeze', node => 'node1' },
+ lrm_mode => 'active',
+ ha_settings => $enabled,
+ applies => 0,
+ },
+ {
+ description => "HA stack that is disarmed, with the LRM not in that mode yet",
+ config => { state => 'started' },
+ status => { state => 'started', node => 'node1' },
+ disarm => { mode => 'ignore', state => 'disarming' },
+ lrm_mode => 'active',
+ ha_settings => $enabled,
+ applies => 0,
+ },
+ {
+ description => "node that reboots",
+ config => { state => 'started' },
+ status => { state => 'started', node => 'node1' },
+ lrm_mode => 'restart',
+ ha_settings => $enabled,
+ applies => 0,
+ },
+ {
+ description => "node that shuts down",
+ config => { state => 'started' },
+ status => { state => 'started', node => 'node1' },
+ lrm_mode => 'shutdown',
+ ha_settings => $enabled,
+ applies => 0,
+ },
+ {
+ description => "node in maintenance mode",
+ config => { state => 'started' },
+ status => { state => 'started', node => 'node1' },
+ lrm_mode => 'maintenance',
+ ha_settings => $enabled,
+ applies => 0,
+ },
+ {
+ description => "node with unknown LRM mode",
+ config => { state => 'started' },
+ status => { state => 'started', node => 'node1' },
+ lrm_mode => undef,
+ ha_settings => $enabled,
+ applies => 0,
+ },
+];
+
+plan(tests => scalar($tests->@*));
+
+for my $case ($tests->@*) {
+ my ($cd, $sd, $disarm, $lrm_mode, $ha_settings) =
+ $case->@{qw(config status disarm lrm_mode ha_settings)};
+
+ my $result =
+ PVE::HA::Tools::stop_on_poweroff_applies($cd, $sd, $disarm, $lrm_mode, $ha_settings);
+
+ is($result, $case->{applies}, $case->{description});
+}
+
+done_testing();
base-commit: c86bca477f4e10763c470526ca33f8e2d656593b
--
2.53.0
^ permalink raw reply related [flat|nested] 12+ messages in thread
* [RFC PATCH ha-manager 03/11] lrm: do not start a service again that is about to be stopped
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
2026-11-28 15:11 ` [RFC PATCH ha-manager 04/11] sim: hardware: add poweroff command for services Jakob-Tobias Winter
` (7 subsequent siblings)
10 siblings, 0 replies; 12+ messages in thread
From: Jakob-Tobias Winter @ 2026-11-28 15:11 UTC (permalink / raw)
To: pve-devel; +Cc: Jakob-Tobias Winter
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
^ permalink raw reply related [flat|nested] 12+ messages in thread
* [RFC PATCH ha-manager 04/11] sim: hardware: add poweroff command for services
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
` (2 preceding siblings ...)
2026-11-28 15:11 ` [RFC PATCH ha-manager 03/11] lrm: do not start a service again that is about to be stopped Jakob-Tobias Winter
@ 2026-11-28 15:11 ` 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
` (6 subsequent siblings)
10 siblings, 0 replies; 12+ messages in thread
From: Jakob-Tobias Winter @ 2026-11-28 15:11 UTC (permalink / raw)
To: pve-devel; +Cc: Jakob-Tobias Winter
Allow test cases to let the guest of a service get powered off from
within. The service is not running anymore afterwards, and the stopped
state gets requested for it if stop-on-poweroff applies, like the
cleanup after a real guest does.
Signed-off-by: Jakob-Tobias Winter <tobias@stuttgart.de>
---
src/PVE/HA/Sim/Hardware.pm | 23 +++++++++++++++++++++++
1 file changed, 23 insertions(+)
diff --git a/src/PVE/HA/Sim/Hardware.pm b/src/PVE/HA/Sim/Hardware.pm
index 9367449..147cfb6 100644
--- a/src/PVE/HA/Sim/Hardware.pm
+++ b/src/PVE/HA/Sim/Hardware.pm
@@ -798,6 +798,7 @@ sub get_cfs_state {
# service <sid> migrate <target> [<key>=<value>[,<key>=<value>...]]
# service <sid> relocate <target>
# service <sid> stop <timeout>
+# service <sid> poweroff
# service <sid> lock/unlock [lockname]
# service <sid> add <node> [<request-state=started>] [<running=0>]
# service <sid> set-static-stats [maxcpu <cores>] [maxmem <MiB>]
@@ -972,6 +973,28 @@ sub sim_hardware_cmd {
$self->queue_crm_commands_nolock("$action $sid $param");
+ } elsif ($action eq 'poweroff') {
+
+ my $cd = $self->read_service_config()->{$sid}
+ || die "sim_hardware_cmd: service '$sid' not configured\n";
+
+ my $ss = $self->read_service_status($cd->{node});
+ $ss->{$sid} = 0;
+ $self->write_service_status($cd->{node}, $ss);
+
+ # the cleanup after the guest reports that it got powered off from within
+ my $ms = PVE::HA::Tools::read_json_from_file(
+ "$self->{statusdir}/manager_status", {},
+ );
+ my $sd = $ms->{service_status}->{$sid};
+ my $lrm_mode = $sd ? $self->read_lrm_status($sd->{node})->{mode} : undef;
+ my $ha_settings = $self->read_datacenter_conf()->{ha};
+
+ my $applies = PVE::HA::Tools::stop_on_poweroff_applies(
+ $cd, $sd, $ms->{disarm}, $lrm_mode, $ha_settings,
+ );
+ $self->set_service_state($sid, 'stopped') if $applies;
+
} elsif ($action eq 'add') {
$self->add_service(
$sid,
--
2.53.0
^ permalink raw reply related [flat|nested] 12+ messages in thread
* [RFC PATCH ha-manager 05/11] test: add test cases for stop-on-poweroff
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
` (3 preceding siblings ...)
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 ` 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
` (5 subsequent siblings)
10 siblings, 0 replies; 12+ messages in thread
From: Jakob-Tobias Winter @ 2026-11-28 15:11 UTC (permalink / raw)
To: pve-devel; +Cc: Jakob-Tobias Winter
Cover a service that gets stopped and started again later, the default
without the setting, an LRM round that comes before the CRM noticed
the request to stop the service, and a node that reboots.
Signed-off-by: Jakob-Tobias Winter <tobias@stuttgart.de>
---
src/test/test-stop-on-poweroff1/README | 3 ++
src/test/test-stop-on-poweroff1/cmdlist | 5 +++
.../test-stop-on-poweroff1/datacenter.cfg | 5 +++
.../test-stop-on-poweroff1/hardware_status | 5 +++
src/test/test-stop-on-poweroff1/log.expect | 32 ++++++++++++++
.../test-stop-on-poweroff1/manager_status | 1 +
.../test-stop-on-poweroff1/service_config | 3 ++
src/test/test-stop-on-poweroff2/README | 2 +
src/test/test-stop-on-poweroff2/cmdlist | 4 ++
.../test-stop-on-poweroff2/hardware_status | 5 +++
src/test/test-stop-on-poweroff2/log.expect | 27 ++++++++++++
.../test-stop-on-poweroff2/manager_status | 1 +
.../test-stop-on-poweroff2/service_config | 3 ++
src/test/test-stop-on-poweroff3/README | 4 ++
src/test/test-stop-on-poweroff3/cmdlist | 5 +++
.../test-stop-on-poweroff3/datacenter.cfg | 5 +++
.../test-stop-on-poweroff3/hardware_status | 5 +++
src/test/test-stop-on-poweroff3/log.expect | 34 +++++++++++++++
.../test-stop-on-poweroff3/manager_status | 1 +
.../test-stop-on-poweroff3/service_config | 3 ++
src/test/test-stop-on-poweroff4/README | 4 ++
src/test/test-stop-on-poweroff4/cmdlist | 4 ++
.../test-stop-on-poweroff4/datacenter.cfg | 5 +++
.../test-stop-on-poweroff4/hardware_status | 5 +++
src/test/test-stop-on-poweroff4/log.expect | 43 +++++++++++++++++++
.../test-stop-on-poweroff4/manager_status | 1 +
.../test-stop-on-poweroff4/service_config | 3 ++
27 files changed, 218 insertions(+)
create mode 100644 src/test/test-stop-on-poweroff1/README
create mode 100644 src/test/test-stop-on-poweroff1/cmdlist
create mode 100644 src/test/test-stop-on-poweroff1/datacenter.cfg
create mode 100644 src/test/test-stop-on-poweroff1/hardware_status
create mode 100644 src/test/test-stop-on-poweroff1/log.expect
create mode 100644 src/test/test-stop-on-poweroff1/manager_status
create mode 100644 src/test/test-stop-on-poweroff1/service_config
create mode 100644 src/test/test-stop-on-poweroff2/README
create mode 100644 src/test/test-stop-on-poweroff2/cmdlist
create mode 100644 src/test/test-stop-on-poweroff2/hardware_status
create mode 100644 src/test/test-stop-on-poweroff2/log.expect
create mode 100644 src/test/test-stop-on-poweroff2/manager_status
create mode 100644 src/test/test-stop-on-poweroff2/service_config
create mode 100644 src/test/test-stop-on-poweroff3/README
create mode 100644 src/test/test-stop-on-poweroff3/cmdlist
create mode 100644 src/test/test-stop-on-poweroff3/datacenter.cfg
create mode 100644 src/test/test-stop-on-poweroff3/hardware_status
create mode 100644 src/test/test-stop-on-poweroff3/log.expect
create mode 100644 src/test/test-stop-on-poweroff3/manager_status
create mode 100644 src/test/test-stop-on-poweroff3/service_config
create mode 100644 src/test/test-stop-on-poweroff4/README
create mode 100644 src/test/test-stop-on-poweroff4/cmdlist
create mode 100644 src/test/test-stop-on-poweroff4/datacenter.cfg
create mode 100644 src/test/test-stop-on-poweroff4/hardware_status
create mode 100644 src/test/test-stop-on-poweroff4/log.expect
create mode 100644 src/test/test-stop-on-poweroff4/manager_status
create mode 100644 src/test/test-stop-on-poweroff4/service_config
diff --git a/src/test/test-stop-on-poweroff1/README b/src/test/test-stop-on-poweroff1/README
new file mode 100644
index 0000000..162ad76
--- /dev/null
+++ b/src/test/test-stop-on-poweroff1/README
@@ -0,0 +1,3 @@
+Test that a service, whose guest gets powered off from within, is stopped
+instead of started again if stop-on-poweroff is enabled in the cluster wide HA
+settings, and that it can be started again afterwards.
diff --git a/src/test/test-stop-on-poweroff1/cmdlist b/src/test/test-stop-on-poweroff1/cmdlist
new file mode 100644
index 0000000..2b60d4a
--- /dev/null
+++ b/src/test/test-stop-on-poweroff1/cmdlist
@@ -0,0 +1,5 @@
+[
+ [ "power node1 on", "power node2 on", "power node3 on"],
+ [ "service vm:103 poweroff" ],
+ [ "service vm:103 started" ]
+]
diff --git a/src/test/test-stop-on-poweroff1/datacenter.cfg b/src/test/test-stop-on-poweroff1/datacenter.cfg
new file mode 100644
index 0000000..8f46844
--- /dev/null
+++ b/src/test/test-stop-on-poweroff1/datacenter.cfg
@@ -0,0 +1,5 @@
+{
+ "ha": {
+ "stop-on-poweroff": 1
+ }
+}
diff --git a/src/test/test-stop-on-poweroff1/hardware_status b/src/test/test-stop-on-poweroff1/hardware_status
new file mode 100644
index 0000000..451beb1
--- /dev/null
+++ b/src/test/test-stop-on-poweroff1/hardware_status
@@ -0,0 +1,5 @@
+{
+ "node1": { "power": "off", "network": "off" },
+ "node2": { "power": "off", "network": "off" },
+ "node3": { "power": "off", "network": "off" }
+}
diff --git a/src/test/test-stop-on-poweroff1/log.expect b/src/test/test-stop-on-poweroff1/log.expect
new file mode 100644
index 0000000..9703e36
--- /dev/null
+++ b/src/test/test-stop-on-poweroff1/log.expect
@@ -0,0 +1,32 @@
+info 0 hardware: starting simulation
+info 20 cmdlist: execute power node1 on
+info 20 node1/crm: status change startup => wait_for_quorum
+info 20 node1/lrm: status change startup => wait_for_agent_lock
+info 20 cmdlist: execute power node2 on
+info 20 node2/crm: status change startup => wait_for_quorum
+info 20 node2/lrm: status change startup => wait_for_agent_lock
+info 20 cmdlist: execute power node3 on
+info 20 node3/crm: status change startup => wait_for_quorum
+info 20 node3/lrm: status change startup => wait_for_agent_lock
+info 20 node1/crm: got lock 'ha_manager_lock'
+info 20 node1/crm: status change wait_for_quorum => master
+info 20 node1/crm: node 'node1': state changed from 'unknown' => 'online'
+info 20 node1/crm: node 'node2': state changed from 'unknown' => 'online'
+info 20 node1/crm: node 'node3': state changed from 'unknown' => 'online'
+info 20 node1/crm: adding new service 'vm:103' on node 'node3'
+info 20 node1/crm: service 'vm:103': state changed from 'request_start' to 'started' (node = node3)
+info 22 node2/crm: status change wait_for_quorum => slave
+info 24 node3/crm: status change wait_for_quorum => slave
+info 25 node3/lrm: got lock 'ha_agent_node3_lock'
+info 25 node3/lrm: status change wait_for_agent_lock => active
+info 25 node3/lrm: starting service vm:103
+info 25 node3/lrm: service status vm:103 started
+info 120 cmdlist: execute service vm:103 poweroff
+info 120 node1/crm: service 'vm:103': state changed from 'started' to 'request_stop'
+info 140 node1/crm: service 'vm:103': state changed from 'request_stop' to 'stopped'
+info 220 cmdlist: execute service vm:103 started
+info 220 node1/crm: service 'vm:103': state changed from 'stopped' to 'request_start' (node = node3)
+info 220 node1/crm: service 'vm:103': state changed from 'request_start' to 'started' (node = node3)
+info 225 node3/lrm: starting service vm:103
+info 225 node3/lrm: service status vm:103 started
+info 820 hardware: exit simulation - done
diff --git a/src/test/test-stop-on-poweroff1/manager_status b/src/test/test-stop-on-poweroff1/manager_status
new file mode 100644
index 0000000..0967ef4
--- /dev/null
+++ b/src/test/test-stop-on-poweroff1/manager_status
@@ -0,0 +1 @@
+{}
diff --git a/src/test/test-stop-on-poweroff1/service_config b/src/test/test-stop-on-poweroff1/service_config
new file mode 100644
index 0000000..c6860e7
--- /dev/null
+++ b/src/test/test-stop-on-poweroff1/service_config
@@ -0,0 +1,3 @@
+{
+ "vm:103": { "node": "node3", "state": "enabled" }
+}
diff --git a/src/test/test-stop-on-poweroff2/README b/src/test/test-stop-on-poweroff2/README
new file mode 100644
index 0000000..896b91f
--- /dev/null
+++ b/src/test/test-stop-on-poweroff2/README
@@ -0,0 +1,2 @@
+Test that a service, whose guest gets powered off from within, is started
+again if stop-on-poweroff is not enabled, which is the default.
diff --git a/src/test/test-stop-on-poweroff2/cmdlist b/src/test/test-stop-on-poweroff2/cmdlist
new file mode 100644
index 0000000..47ade70
--- /dev/null
+++ b/src/test/test-stop-on-poweroff2/cmdlist
@@ -0,0 +1,4 @@
+[
+ [ "power node1 on", "power node2 on", "power node3 on"],
+ [ "service vm:103 poweroff" ]
+]
diff --git a/src/test/test-stop-on-poweroff2/hardware_status b/src/test/test-stop-on-poweroff2/hardware_status
new file mode 100644
index 0000000..451beb1
--- /dev/null
+++ b/src/test/test-stop-on-poweroff2/hardware_status
@@ -0,0 +1,5 @@
+{
+ "node1": { "power": "off", "network": "off" },
+ "node2": { "power": "off", "network": "off" },
+ "node3": { "power": "off", "network": "off" }
+}
diff --git a/src/test/test-stop-on-poweroff2/log.expect b/src/test/test-stop-on-poweroff2/log.expect
new file mode 100644
index 0000000..f0f9a25
--- /dev/null
+++ b/src/test/test-stop-on-poweroff2/log.expect
@@ -0,0 +1,27 @@
+info 0 hardware: starting simulation
+info 20 cmdlist: execute power node1 on
+info 20 node1/crm: status change startup => wait_for_quorum
+info 20 node1/lrm: status change startup => wait_for_agent_lock
+info 20 cmdlist: execute power node2 on
+info 20 node2/crm: status change startup => wait_for_quorum
+info 20 node2/lrm: status change startup => wait_for_agent_lock
+info 20 cmdlist: execute power node3 on
+info 20 node3/crm: status change startup => wait_for_quorum
+info 20 node3/lrm: status change startup => wait_for_agent_lock
+info 20 node1/crm: got lock 'ha_manager_lock'
+info 20 node1/crm: status change wait_for_quorum => master
+info 20 node1/crm: node 'node1': state changed from 'unknown' => 'online'
+info 20 node1/crm: node 'node2': state changed from 'unknown' => 'online'
+info 20 node1/crm: node 'node3': state changed from 'unknown' => 'online'
+info 20 node1/crm: adding new service 'vm:103' on node 'node3'
+info 20 node1/crm: service 'vm:103': state changed from 'request_start' to 'started' (node = node3)
+info 22 node2/crm: status change wait_for_quorum => slave
+info 24 node3/crm: status change wait_for_quorum => slave
+info 25 node3/lrm: got lock 'ha_agent_node3_lock'
+info 25 node3/lrm: status change wait_for_agent_lock => active
+info 25 node3/lrm: starting service vm:103
+info 25 node3/lrm: service status vm:103 started
+info 120 cmdlist: execute service vm:103 poweroff
+info 125 node3/lrm: starting service vm:103
+info 125 node3/lrm: service status vm:103 started
+info 720 hardware: exit simulation - done
diff --git a/src/test/test-stop-on-poweroff2/manager_status b/src/test/test-stop-on-poweroff2/manager_status
new file mode 100644
index 0000000..0967ef4
--- /dev/null
+++ b/src/test/test-stop-on-poweroff2/manager_status
@@ -0,0 +1 @@
+{}
diff --git a/src/test/test-stop-on-poweroff2/service_config b/src/test/test-stop-on-poweroff2/service_config
new file mode 100644
index 0000000..c6860e7
--- /dev/null
+++ b/src/test/test-stop-on-poweroff2/service_config
@@ -0,0 +1,3 @@
+{
+ "vm:103": { "node": "node3", "state": "enabled" }
+}
diff --git a/src/test/test-stop-on-poweroff3/README b/src/test/test-stop-on-poweroff3/README
new file mode 100644
index 0000000..a898f10
--- /dev/null
+++ b/src/test/test-stop-on-poweroff3/README
@@ -0,0 +1,4 @@
+Test that the LRM does not start a service again, whose guest got powered off
+from within, if its round comes before the CRM noticed the request to stop the
+service. For that the service is on node1 and the CRM master is moved to node2,
+whose round comes later.
diff --git a/src/test/test-stop-on-poweroff3/cmdlist b/src/test/test-stop-on-poweroff3/cmdlist
new file mode 100644
index 0000000..f58f296
--- /dev/null
+++ b/src/test/test-stop-on-poweroff3/cmdlist
@@ -0,0 +1,5 @@
+[
+ [ "power node1 on", "power node2 on", "power node3 on" ],
+ [ "crm node1 stop" ],
+ [ "service vm:101 poweroff" ]
+]
diff --git a/src/test/test-stop-on-poweroff3/datacenter.cfg b/src/test/test-stop-on-poweroff3/datacenter.cfg
new file mode 100644
index 0000000..8f46844
--- /dev/null
+++ b/src/test/test-stop-on-poweroff3/datacenter.cfg
@@ -0,0 +1,5 @@
+{
+ "ha": {
+ "stop-on-poweroff": 1
+ }
+}
diff --git a/src/test/test-stop-on-poweroff3/hardware_status b/src/test/test-stop-on-poweroff3/hardware_status
new file mode 100644
index 0000000..451beb1
--- /dev/null
+++ b/src/test/test-stop-on-poweroff3/hardware_status
@@ -0,0 +1,5 @@
+{
+ "node1": { "power": "off", "network": "off" },
+ "node2": { "power": "off", "network": "off" },
+ "node3": { "power": "off", "network": "off" }
+}
diff --git a/src/test/test-stop-on-poweroff3/log.expect b/src/test/test-stop-on-poweroff3/log.expect
new file mode 100644
index 0000000..bae717a
--- /dev/null
+++ b/src/test/test-stop-on-poweroff3/log.expect
@@ -0,0 +1,34 @@
+info 0 hardware: starting simulation
+info 20 cmdlist: execute power node1 on
+info 20 node1/crm: status change startup => wait_for_quorum
+info 20 node1/lrm: status change startup => wait_for_agent_lock
+info 20 cmdlist: execute power node2 on
+info 20 node2/crm: status change startup => wait_for_quorum
+info 20 node2/lrm: status change startup => wait_for_agent_lock
+info 20 cmdlist: execute power node3 on
+info 20 node3/crm: status change startup => wait_for_quorum
+info 20 node3/lrm: status change startup => wait_for_agent_lock
+info 20 node1/crm: got lock 'ha_manager_lock'
+info 20 node1/crm: status change wait_for_quorum => master
+info 20 node1/crm: node 'node1': state changed from 'unknown' => 'online'
+info 20 node1/crm: node 'node2': state changed from 'unknown' => 'online'
+info 20 node1/crm: node 'node3': state changed from 'unknown' => 'online'
+info 20 node1/crm: adding new service 'vm:101' on node 'node1'
+info 20 node1/crm: service 'vm:101': state changed from 'request_start' to 'started' (node = node1)
+info 21 node1/lrm: got lock 'ha_agent_node1_lock'
+info 21 node1/lrm: status change wait_for_agent_lock => active
+info 21 node1/lrm: starting service vm:101
+info 21 node1/lrm: service status vm:101 started
+info 22 node2/crm: status change wait_for_quorum => slave
+info 24 node3/crm: status change wait_for_quorum => slave
+info 120 cmdlist: execute crm node1 stop
+info 120 node1/crm: server received shutdown request
+info 120 node1/crm: voluntary release CRM lock
+info 121 node1/crm: exit (loop end)
+info 122 node2/crm: got lock 'ha_manager_lock'
+info 122 node2/crm: status change slave => master
+info 220 cmdlist: execute service vm:101 poweroff
+info 220 node1/lrm: service vm:101 is not requested to be started anymore
+info 221 node2/crm: service 'vm:101': state changed from 'started' to 'request_stop'
+info 241 node2/crm: service 'vm:101': state changed from 'request_stop' to 'stopped'
+info 820 hardware: exit simulation - done
diff --git a/src/test/test-stop-on-poweroff3/manager_status b/src/test/test-stop-on-poweroff3/manager_status
new file mode 100644
index 0000000..0967ef4
--- /dev/null
+++ b/src/test/test-stop-on-poweroff3/manager_status
@@ -0,0 +1 @@
+{}
diff --git a/src/test/test-stop-on-poweroff3/service_config b/src/test/test-stop-on-poweroff3/service_config
new file mode 100644
index 0000000..f5a4ccd
--- /dev/null
+++ b/src/test/test-stop-on-poweroff3/service_config
@@ -0,0 +1,3 @@
+{
+ "vm:101": { "node": "node1", "state": "enabled" }
+}
diff --git a/src/test/test-stop-on-poweroff4/README b/src/test/test-stop-on-poweroff4/README
new file mode 100644
index 0000000..a45fd1b
--- /dev/null
+++ b/src/test/test-stop-on-poweroff4/README
@@ -0,0 +1,4 @@
+Test that a service stays requested to be started if its guest is reported as
+powered off while its node reboots, even with stop-on-poweroff enabled. The LRM
+shuts down the guests on a reboot itself, and none of them may end up stopped.
+The service must be started again after the reboot.
diff --git a/src/test/test-stop-on-poweroff4/cmdlist b/src/test/test-stop-on-poweroff4/cmdlist
new file mode 100644
index 0000000..d770f96
--- /dev/null
+++ b/src/test/test-stop-on-poweroff4/cmdlist
@@ -0,0 +1,4 @@
+[
+ [ "power node1 on", "power node2 on", "power node3 on"],
+ [ "reboot node3", "service vm:103 poweroff" ]
+]
diff --git a/src/test/test-stop-on-poweroff4/datacenter.cfg b/src/test/test-stop-on-poweroff4/datacenter.cfg
new file mode 100644
index 0000000..8f46844
--- /dev/null
+++ b/src/test/test-stop-on-poweroff4/datacenter.cfg
@@ -0,0 +1,5 @@
+{
+ "ha": {
+ "stop-on-poweroff": 1
+ }
+}
diff --git a/src/test/test-stop-on-poweroff4/hardware_status b/src/test/test-stop-on-poweroff4/hardware_status
new file mode 100644
index 0000000..451beb1
--- /dev/null
+++ b/src/test/test-stop-on-poweroff4/hardware_status
@@ -0,0 +1,5 @@
+{
+ "node1": { "power": "off", "network": "off" },
+ "node2": { "power": "off", "network": "off" },
+ "node3": { "power": "off", "network": "off" }
+}
diff --git a/src/test/test-stop-on-poweroff4/log.expect b/src/test/test-stop-on-poweroff4/log.expect
new file mode 100644
index 0000000..a03da5c
--- /dev/null
+++ b/src/test/test-stop-on-poweroff4/log.expect
@@ -0,0 +1,43 @@
+info 0 hardware: starting simulation
+info 20 cmdlist: execute power node1 on
+info 20 node1/crm: status change startup => wait_for_quorum
+info 20 node1/lrm: status change startup => wait_for_agent_lock
+info 20 cmdlist: execute power node2 on
+info 20 node2/crm: status change startup => wait_for_quorum
+info 20 node2/lrm: status change startup => wait_for_agent_lock
+info 20 cmdlist: execute power node3 on
+info 20 node3/crm: status change startup => wait_for_quorum
+info 20 node3/lrm: status change startup => wait_for_agent_lock
+info 20 node1/crm: got lock 'ha_manager_lock'
+info 20 node1/crm: status change wait_for_quorum => master
+info 20 node1/crm: node 'node1': state changed from 'unknown' => 'online'
+info 20 node1/crm: node 'node2': state changed from 'unknown' => 'online'
+info 20 node1/crm: node 'node3': state changed from 'unknown' => 'online'
+info 20 node1/crm: adding new service 'vm:103' on node 'node3'
+info 20 node1/crm: service 'vm:103': state changed from 'request_start' to 'started' (node = node3)
+info 22 node2/crm: status change wait_for_quorum => slave
+info 24 node3/crm: status change wait_for_quorum => slave
+info 25 node3/lrm: got lock 'ha_agent_node3_lock'
+info 25 node3/lrm: status change wait_for_agent_lock => active
+info 25 node3/lrm: starting service vm:103
+info 25 node3/lrm: service status vm:103 started
+info 120 cmdlist: execute reboot node3
+info 120 node3/lrm: got shutdown request with shutdown policy 'conditional'
+info 120 node3/lrm: reboot LRM, stop and freeze all services
+info 120 cmdlist: execute service vm:103 poweroff
+info 120 node1/crm: service 'vm:103': state changed from 'started' to 'freeze'
+info 126 node3/lrm: exit (loop end)
+info 126 reboot: execute crm node3 stop
+info 125 node3/crm: server received shutdown request
+info 145 node3/crm: exit (loop end)
+info 145 reboot: execute power node3 off
+info 145 reboot: execute power node3 on
+info 145 node3/crm: status change startup => wait_for_quorum
+info 140 node3/lrm: status change startup => wait_for_agent_lock
+info 160 node1/crm: service 'vm:103': state changed from 'freeze' to 'started'
+info 164 node3/crm: status change wait_for_quorum => slave
+info 165 node3/lrm: got lock 'ha_agent_node3_lock'
+info 165 node3/lrm: status change wait_for_agent_lock => active
+info 165 node3/lrm: starting service vm:103
+info 165 node3/lrm: service status vm:103 started
+info 720 hardware: exit simulation - done
diff --git a/src/test/test-stop-on-poweroff4/manager_status b/src/test/test-stop-on-poweroff4/manager_status
new file mode 100644
index 0000000..0967ef4
--- /dev/null
+++ b/src/test/test-stop-on-poweroff4/manager_status
@@ -0,0 +1 @@
+{}
diff --git a/src/test/test-stop-on-poweroff4/service_config b/src/test/test-stop-on-poweroff4/service_config
new file mode 100644
index 0000000..c6860e7
--- /dev/null
+++ b/src/test/test-stop-on-poweroff4/service_config
@@ -0,0 +1,3 @@
+{
+ "vm:103": { "node": "node3", "state": "enabled" }
+}
--
2.53.0
^ permalink raw reply related [flat|nested] 12+ messages in thread
* [RFC PATCH ha-manager 06/11] resources: add stop-on-poweroff property for virtual machines
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
` (4 preceding siblings ...)
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 ` 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
` (4 subsequent siblings)
10 siblings, 0 replies; 12+ messages in thread
From: Jakob-Tobias Winter @ 2026-11-28 15:11 UTC (permalink / raw)
To: pve-devel; +Cc: Jakob-Tobias Winter
Allow overriding the cluster wide stop-on-poweroff setting for single
HA resources, in both directions: to keep some virtual machines started
at all times while all others stay stopped after a poweroff from within
the guest, or the other way around.
Without the property, the cluster wide setting applies as before.
Containers do not get it, as nothing reports their poweroff.
Signed-off-by: Jakob-Tobias Winter <tobias@stuttgart.de>
---
src/PVE/API2/HA/Resources.pm | 7 ++++
src/PVE/HA/LRM.pm | 3 +-
src/PVE/HA/Resources.pm | 7 ++++
src/PVE/HA/Resources/PVEVM.pm | 1 +
src/PVE/HA/Tools.pm | 10 ++++-
src/test/test-stop-on-poweroff5/README | 5 +++
src/test/test-stop-on-poweroff5/cmdlist | 5 +++
.../test-stop-on-poweroff5/hardware_status | 5 +++
src/test/test-stop-on-poweroff5/log.expect | 41 +++++++++++++++++++
.../test-stop-on-poweroff5/manager_status | 1 +
.../test-stop-on-poweroff5/service_config | 4 ++
src/test/test-stop-on-poweroff6/README | 3 ++
src/test/test-stop-on-poweroff6/cmdlist | 4 ++
.../test-stop-on-poweroff6/datacenter.cfg | 5 +++
.../test-stop-on-poweroff6/hardware_status | 5 +++
src/test/test-stop-on-poweroff6/log.expect | 34 +++++++++++++++
.../test-stop-on-poweroff6/manager_status | 1 +
.../test-stop-on-poweroff6/service_config | 4 ++
src/test/test_stop_on_poweroff.pl | 16 ++++++++
19 files changed, 159 insertions(+), 2 deletions(-)
create mode 100644 src/test/test-stop-on-poweroff5/README
create mode 100644 src/test/test-stop-on-poweroff5/cmdlist
create mode 100644 src/test/test-stop-on-poweroff5/hardware_status
create mode 100644 src/test/test-stop-on-poweroff5/log.expect
create mode 100644 src/test/test-stop-on-poweroff5/manager_status
create mode 100644 src/test/test-stop-on-poweroff5/service_config
create mode 100644 src/test/test-stop-on-poweroff6/README
create mode 100644 src/test/test-stop-on-poweroff6/cmdlist
create mode 100644 src/test/test-stop-on-poweroff6/datacenter.cfg
create mode 100644 src/test/test-stop-on-poweroff6/hardware_status
create mode 100644 src/test/test-stop-on-poweroff6/log.expect
create mode 100644 src/test/test-stop-on-poweroff6/manager_status
create mode 100644 src/test/test-stop-on-poweroff6/service_config
diff --git a/src/PVE/API2/HA/Resources.pm b/src/PVE/API2/HA/Resources.pm
index d06ff19..342a564 100644
--- a/src/PVE/API2/HA/Resources.pm
+++ b/src/PVE/API2/HA/Resources.pm
@@ -196,6 +196,13 @@ __PACKAGE__->register_method({
optional => 1,
default => 1,
},
+ 'stop-on-poweroff' => {
+ description =>
+ "The requested state is set to 'stopped' if the guest gets powered"
+ . " off from within. Overrides the cluster wide HA setting.",
+ type => 'boolean',
+ optional => 1,
+ },
group => get_standard_option('pve-ha-group-id', { optional => 1 }),
max_restart => {
description => "Maximal number of tries to restart the service on"
diff --git a/src/PVE/HA/LRM.pm b/src/PVE/HA/LRM.pm
index 891fd48..bc14fc1 100644
--- a/src/PVE/HA/LRM.pm
+++ b/src/PVE/HA/LRM.pm
@@ -1032,7 +1032,8 @@ sub exec_resource_agent {
return SUCCESS if $running;
- if ($haenv->get_datacenter_settings()->{ha}->{'stop-on-poweroff'}) {
+ my $ha_settings = $haenv->get_datacenter_settings()->{ha};
+ if (PVE::HA::Tools::stop_on_poweroff_enabled($service_config, $ha_settings)) {
# 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.
diff --git a/src/PVE/HA/Resources.pm b/src/PVE/HA/Resources.pm
index 624c72b..65d2841 100644
--- a/src/PVE/HA/Resources.pm
+++ b/src/PVE/HA/Resources.pm
@@ -78,6 +78,13 @@ EODESC
optional => 1,
default => 1,
},
+ 'stop-on-poweroff' => {
+ description => "Set the requested state to 'stopped' if the guest gets powered off"
+ . " from within, instead of starting it again. Overrides the cluster wide HA"
+ . " setting.",
+ type => 'boolean',
+ optional => 1,
+ },
max_restart => {
description => "Maximal number of tries to restart the resource on"
. " a node after its start failed. When reached, the HA manager will try to"
diff --git a/src/PVE/HA/Resources/PVEVM.pm b/src/PVE/HA/Resources/PVEVM.pm
index 8fcfaad..5da6d50 100644
--- a/src/PVE/HA/Resources/PVEVM.pm
+++ b/src/PVE/HA/Resources/PVEVM.pm
@@ -54,6 +54,7 @@ sub options {
comment => { optional => 1 },
failback => { optional => 1 },
'auto-rebalance' => { optional => 1 },
+ 'stop-on-poweroff' => { optional => 1 },
max_restart => { optional => 1 },
max_relocate => { optional => 1 },
};
diff --git a/src/PVE/HA/Tools.pm b/src/PVE/HA/Tools.pm
index c4d26ca..2d62281 100644
--- a/src/PVE/HA/Tools.pm
+++ b/src/PVE/HA/Tools.pm
@@ -235,6 +235,14 @@ sub get_disarm_deferring_services {
return $disarm_deferring_sids;
}
+# Returns whether a service gets stopped instead of started again if its guest gets powered off
+# from within, which the config of the service decides before the cluster wide HA settings.
+sub stop_on_poweroff_enabled {
+ my ($cd, $ha_settings) = @_;
+
+ return $cd->{'stop-on-poweroff'} // $ha_settings->{'stop-on-poweroff'} // 0;
+}
+
# Returns whether the stopped state gets requested for a service, whose guest got powered off
# from within. Takes the config of the service with defaults set, its status and the disarm state
# from the CRM, the mode of the LRM on its node and the cluster wide HA settings.
@@ -246,8 +254,8 @@ sub get_disarm_deferring_services {
sub stop_on_poweroff_applies {
my ($cd, $sd, $disarm, $lrm_mode, $ha_settings) = @_;
- return 0 if !$ha_settings->{'stop-on-poweroff'};
return 0 if !$cd || $cd->{state} ne 'started';
+ return 0 if !stop_on_poweroff_enabled($cd, $ha_settings);
return 0 if $disarm || !$sd || $sd->{state} ne 'started';
return 0 if !defined($lrm_mode) || $lrm_mode ne 'active';
diff --git a/src/test/test-stop-on-poweroff5/README b/src/test/test-stop-on-poweroff5/README
new file mode 100644
index 0000000..6b91780
--- /dev/null
+++ b/src/test/test-stop-on-poweroff5/README
@@ -0,0 +1,5 @@
+Test that stop-on-poweroff can be enabled for a single service, while it is
+not enabled in the cluster wide HA settings. The service is on node1 and the
+CRM master is moved to node2, so that the LRM has to leave the service alone
+before the CRM noticed the request to stop it. A second service, without the
+setting, is started again.
diff --git a/src/test/test-stop-on-poweroff5/cmdlist b/src/test/test-stop-on-poweroff5/cmdlist
new file mode 100644
index 0000000..c02154f
--- /dev/null
+++ b/src/test/test-stop-on-poweroff5/cmdlist
@@ -0,0 +1,5 @@
+[
+ [ "power node1 on", "power node2 on", "power node3 on" ],
+ [ "crm node1 stop" ],
+ [ "service vm:101 poweroff", "service vm:102 poweroff" ]
+]
diff --git a/src/test/test-stop-on-poweroff5/hardware_status b/src/test/test-stop-on-poweroff5/hardware_status
new file mode 100644
index 0000000..451beb1
--- /dev/null
+++ b/src/test/test-stop-on-poweroff5/hardware_status
@@ -0,0 +1,5 @@
+{
+ "node1": { "power": "off", "network": "off" },
+ "node2": { "power": "off", "network": "off" },
+ "node3": { "power": "off", "network": "off" }
+}
diff --git a/src/test/test-stop-on-poweroff5/log.expect b/src/test/test-stop-on-poweroff5/log.expect
new file mode 100644
index 0000000..783e49c
--- /dev/null
+++ b/src/test/test-stop-on-poweroff5/log.expect
@@ -0,0 +1,41 @@
+info 0 hardware: starting simulation
+info 20 cmdlist: execute power node1 on
+info 20 node1/crm: status change startup => wait_for_quorum
+info 20 node1/lrm: status change startup => wait_for_agent_lock
+info 20 cmdlist: execute power node2 on
+info 20 node2/crm: status change startup => wait_for_quorum
+info 20 node2/lrm: status change startup => wait_for_agent_lock
+info 20 cmdlist: execute power node3 on
+info 20 node3/crm: status change startup => wait_for_quorum
+info 20 node3/lrm: status change startup => wait_for_agent_lock
+info 20 node1/crm: got lock 'ha_manager_lock'
+info 20 node1/crm: status change wait_for_quorum => master
+info 20 node1/crm: node 'node1': state changed from 'unknown' => 'online'
+info 20 node1/crm: node 'node2': state changed from 'unknown' => 'online'
+info 20 node1/crm: node 'node3': state changed from 'unknown' => 'online'
+info 20 node1/crm: adding new service 'vm:101' on node 'node1'
+info 20 node1/crm: adding new service 'vm:102' on node 'node1'
+info 20 node1/crm: service 'vm:101': state changed from 'request_start' to 'started' (node = node1)
+info 20 node1/crm: service 'vm:102': state changed from 'request_start' to 'started' (node = node1)
+info 21 node1/lrm: got lock 'ha_agent_node1_lock'
+info 21 node1/lrm: status change wait_for_agent_lock => active
+info 21 node1/lrm: starting service vm:101
+info 21 node1/lrm: service status vm:101 started
+info 21 node1/lrm: starting service vm:102
+info 21 node1/lrm: service status vm:102 started
+info 22 node2/crm: status change wait_for_quorum => slave
+info 24 node3/crm: status change wait_for_quorum => slave
+info 120 cmdlist: execute crm node1 stop
+info 120 node1/crm: server received shutdown request
+info 120 node1/crm: voluntary release CRM lock
+info 121 node1/crm: exit (loop end)
+info 122 node2/crm: got lock 'ha_manager_lock'
+info 122 node2/crm: status change slave => master
+info 220 cmdlist: execute service vm:101 poweroff
+info 220 cmdlist: execute service vm:102 poweroff
+info 220 node1/lrm: service vm:101 is not requested to be started anymore
+info 220 node1/lrm: starting service vm:102
+info 220 node1/lrm: service status vm:102 started
+info 221 node2/crm: service 'vm:101': state changed from 'started' to 'request_stop'
+info 241 node2/crm: service 'vm:101': state changed from 'request_stop' to 'stopped'
+info 820 hardware: exit simulation - done
diff --git a/src/test/test-stop-on-poweroff5/manager_status b/src/test/test-stop-on-poweroff5/manager_status
new file mode 100644
index 0000000..0967ef4
--- /dev/null
+++ b/src/test/test-stop-on-poweroff5/manager_status
@@ -0,0 +1 @@
+{}
diff --git a/src/test/test-stop-on-poweroff5/service_config b/src/test/test-stop-on-poweroff5/service_config
new file mode 100644
index 0000000..b32c87c
--- /dev/null
+++ b/src/test/test-stop-on-poweroff5/service_config
@@ -0,0 +1,4 @@
+{
+ "vm:101": { "node": "node1", "state": "enabled", "stop-on-poweroff": 1 },
+ "vm:102": { "node": "node1", "state": "enabled" }
+}
diff --git a/src/test/test-stop-on-poweroff6/README b/src/test/test-stop-on-poweroff6/README
new file mode 100644
index 0000000..b5a1824
--- /dev/null
+++ b/src/test/test-stop-on-poweroff6/README
@@ -0,0 +1,3 @@
+Test that stop-on-poweroff can be disabled for a single service, while it is
+enabled in the cluster wide HA settings. That service is started again, a
+second one without its own setting is stopped.
diff --git a/src/test/test-stop-on-poweroff6/cmdlist b/src/test/test-stop-on-poweroff6/cmdlist
new file mode 100644
index 0000000..93fb0ca
--- /dev/null
+++ b/src/test/test-stop-on-poweroff6/cmdlist
@@ -0,0 +1,4 @@
+[
+ [ "power node1 on", "power node2 on", "power node3 on"],
+ [ "service vm:103 poweroff", "service vm:104 poweroff" ]
+]
diff --git a/src/test/test-stop-on-poweroff6/datacenter.cfg b/src/test/test-stop-on-poweroff6/datacenter.cfg
new file mode 100644
index 0000000..8f46844
--- /dev/null
+++ b/src/test/test-stop-on-poweroff6/datacenter.cfg
@@ -0,0 +1,5 @@
+{
+ "ha": {
+ "stop-on-poweroff": 1
+ }
+}
diff --git a/src/test/test-stop-on-poweroff6/hardware_status b/src/test/test-stop-on-poweroff6/hardware_status
new file mode 100644
index 0000000..451beb1
--- /dev/null
+++ b/src/test/test-stop-on-poweroff6/hardware_status
@@ -0,0 +1,5 @@
+{
+ "node1": { "power": "off", "network": "off" },
+ "node2": { "power": "off", "network": "off" },
+ "node3": { "power": "off", "network": "off" }
+}
diff --git a/src/test/test-stop-on-poweroff6/log.expect b/src/test/test-stop-on-poweroff6/log.expect
new file mode 100644
index 0000000..6758478
--- /dev/null
+++ b/src/test/test-stop-on-poweroff6/log.expect
@@ -0,0 +1,34 @@
+info 0 hardware: starting simulation
+info 20 cmdlist: execute power node1 on
+info 20 node1/crm: status change startup => wait_for_quorum
+info 20 node1/lrm: status change startup => wait_for_agent_lock
+info 20 cmdlist: execute power node2 on
+info 20 node2/crm: status change startup => wait_for_quorum
+info 20 node2/lrm: status change startup => wait_for_agent_lock
+info 20 cmdlist: execute power node3 on
+info 20 node3/crm: status change startup => wait_for_quorum
+info 20 node3/lrm: status change startup => wait_for_agent_lock
+info 20 node1/crm: got lock 'ha_manager_lock'
+info 20 node1/crm: status change wait_for_quorum => master
+info 20 node1/crm: node 'node1': state changed from 'unknown' => 'online'
+info 20 node1/crm: node 'node2': state changed from 'unknown' => 'online'
+info 20 node1/crm: node 'node3': state changed from 'unknown' => 'online'
+info 20 node1/crm: adding new service 'vm:103' on node 'node3'
+info 20 node1/crm: adding new service 'vm:104' on node 'node3'
+info 20 node1/crm: service 'vm:103': state changed from 'request_start' to 'started' (node = node3)
+info 20 node1/crm: service 'vm:104': state changed from 'request_start' to 'started' (node = node3)
+info 22 node2/crm: status change wait_for_quorum => slave
+info 24 node3/crm: status change wait_for_quorum => slave
+info 25 node3/lrm: got lock 'ha_agent_node3_lock'
+info 25 node3/lrm: status change wait_for_agent_lock => active
+info 25 node3/lrm: starting service vm:103
+info 25 node3/lrm: service status vm:103 started
+info 25 node3/lrm: starting service vm:104
+info 25 node3/lrm: service status vm:104 started
+info 120 cmdlist: execute service vm:103 poweroff
+info 120 cmdlist: execute service vm:104 poweroff
+info 120 node1/crm: service 'vm:104': state changed from 'started' to 'request_stop'
+info 125 node3/lrm: starting service vm:103
+info 125 node3/lrm: service status vm:103 started
+info 140 node1/crm: service 'vm:104': state changed from 'request_stop' to 'stopped'
+info 720 hardware: exit simulation - done
diff --git a/src/test/test-stop-on-poweroff6/manager_status b/src/test/test-stop-on-poweroff6/manager_status
new file mode 100644
index 0000000..0967ef4
--- /dev/null
+++ b/src/test/test-stop-on-poweroff6/manager_status
@@ -0,0 +1 @@
+{}
diff --git a/src/test/test-stop-on-poweroff6/service_config b/src/test/test-stop-on-poweroff6/service_config
new file mode 100644
index 0000000..6b1ddf1
--- /dev/null
+++ b/src/test/test-stop-on-poweroff6/service_config
@@ -0,0 +1,4 @@
+{
+ "vm:103": { "node": "node3", "state": "enabled", "stop-on-poweroff": 0 },
+ "vm:104": { "node": "node3", "state": "enabled" }
+}
diff --git a/src/test/test_stop_on_poweroff.pl b/src/test/test_stop_on_poweroff.pl
index f256424..4b489fd 100755
--- a/src/test/test_stop_on_poweroff.pl
+++ b/src/test/test_stop_on_poweroff.pl
@@ -43,6 +43,22 @@ my $tests = [
ha_settings => undef,
applies => 0,
},
+ {
+ description => "stop-on-poweroff set for the service only",
+ config => { state => 'started', 'stop-on-poweroff' => 1 },
+ status => { state => 'started', node => 'node1' },
+ lrm_mode => 'active',
+ ha_settings => {},
+ applies => 1,
+ },
+ {
+ description => "stop-on-poweroff disabled for the service",
+ config => { state => 'started', 'stop-on-poweroff' => 0 },
+ status => { state => 'started', node => 'node1' },
+ lrm_mode => 'active',
+ ha_settings => $enabled,
+ applies => 0,
+ },
{
description => "service that is requested to be stopped already",
config => { state => 'stopped' },
--
2.53.0
^ permalink raw reply related [flat|nested] 12+ messages in thread
* [RFC PATCH qemu-server 07/11] fix #6314: qm cleanup: report a guest poweroff to the HA stack
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
` (5 preceding siblings ...)
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 ` 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
` (3 subsequent siblings)
10 siblings, 0 replies; 12+ messages in thread
From: Jakob-Tobias Winter @ 2026-11-28 15:11 UTC (permalink / raw)
To: pve-devel; +Cc: Jakob-Tobias Winter
An HA managed guest that gets powered off from within is started again
by the HA stack. Only the cleanup after the guest knows that it was
shut down cleanly on its own request, so let it tell the HA stack,
which then requests the stopped state for the guest if it is
configured to do so.
QEMU reports a shutdown through the API in the same way. But such a
shutdown does the cleanup itself, and so does a reboot through the
API. So only report a guest whose cleanup is still pending, which the
cleanup flag tells. As long as the flag cannot be used on a node, that
is until the first boot after an upgrade from a version before 9.1.13,
nothing gets reported.
Do this before the cleanup itself, as the HA stack waits for the
cleanup to be done before it starts a guest again that is not running
anymore.
Signed-off-by: Jakob-Tobias Winter <tobias@stuttgart.de>
---
Needs a versioned dependency on the pve-ha-manager release that has the
helper. With an older one, the cleanup only logs that it could not tell
the HA stack, and everything else works as before.
src/PVE/CLI/qm.pm | 24 ++++++++++++++++++++++++
1 file changed, 24 insertions(+)
diff --git a/src/PVE/CLI/qm.pm b/src/PVE/CLI/qm.pm
index 0b109fc5..37a8a534 100755
--- a/src/PVE/CLI/qm.pm
+++ b/src/PVE/CLI/qm.pm
@@ -43,6 +43,13 @@ use PVE::QemuServer::RunState;
use PVE::QemuServer::DBusVMState;
use PVE::QemuServer;
+BEGIN {
+ if (!$ENV{PVE_GENERATING_DOCS}) {
+ require PVE::HA::Config;
+ import PVE::HA::Config;
+ }
+}
+
use PVE::CLIHandler;
use base qw(PVE::CLIHandler);
@@ -1171,6 +1178,23 @@ __PACKAGE__->register_method({
my $can_use_cleanup_flag = PVE::QemuServer::RunState::can_use_cleanup_flag();
+ if (
+ $clean
+ && $guest
+ && $can_use_cleanup_flag
+ && PVE::QemuServer::RunState::cleanup_flag_exists($vmid)
+ ) {
+ # The guest got powered off from within, as a shutdown through the API would
+ # have done the cleanup already. Tell the HA stack before doing the cleanup, as
+ # it waits for that before it starts the guest again.
+ my $stop_requested =
+ eval { PVE::HA::Config::request_stop_on_poweroff("vm:$vmid") };
+ warn "could not tell the HA stack about the poweroff of VM $vmid - $@"
+ if $@;
+ warn "Requested HA state 'stopped' for powered off VM $vmid\n"
+ if $stop_requested;
+ }
+
if (!$clean || $guest || $can_use_cleanup_flag) {
# either we can use the new mechanism to check if cleanup is done, or
# vm was shutdown from inside the guest or crashed
base-commit: 2f13e34784c5e0fee0df5d7e9669e732f57ef359
--
2.53.0
^ permalink raw reply related [flat|nested] 12+ messages in thread
* [RFC PATCH manager 08/11] ui: dc options: add stop-on-poweroff to the HA settings
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
` (6 preceding siblings ...)
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 ` 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
` (2 subsequent siblings)
10 siblings, 0 replies; 12+ messages in thread
From: Jakob-Tobias Winter @ 2026-11-28 15:11 UTC (permalink / raw)
To: pve-devel; +Cc: Jakob-Tobias Winter
Allow enabling the new cluster wide HA setting, with which HA managed
virtual machines stay stopped if they get powered off from within the
guest.
Signed-off-by: Jakob-Tobias Winter <tobias@stuttgart.de>
---
Needs a libpve-cluster-perl with the new key, the API rejects the value
otherwise.
www/manager6/dc/OptionView.js | 11 +++++++++++
1 file changed, 11 insertions(+)
diff --git a/www/manager6/dc/OptionView.js b/www/manager6/dc/OptionView.js
index 6300dc91..cda7647f 100644
--- a/www/manager6/dc/OptionView.js
+++ b/www/manager6/dc/OptionView.js
@@ -178,6 +178,17 @@ Ext.define('PVE.dc.OptionView', {
],
defaultValue: '__default__',
},
+ {
+ xtype: 'proxmoxcheckbox',
+ name: 'stop-on-poweroff',
+ fieldLabel: gettext('Stop on Poweroff'),
+ autoEl: {
+ tag: 'div',
+ 'data-qtip': gettext(
+ 'Enable if HA managed VMs should stay stopped when they get powered off from within the guest.',
+ ),
+ },
+ },
],
});
me.rows.crs = {
base-commit: e5ac6d6f3d47fa3d780a8ac7c1ef271e136078c3
--
2.53.0
^ permalink raw reply related [flat|nested] 12+ messages in thread
* [RFC PATCH manager 09/11] ui: ha: add stop-on-poweroff selector to the resource edit window
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
` (7 preceding siblings ...)
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 ` 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
10 siblings, 0 replies; 12+ messages in thread
From: Jakob-Tobias Winter @ 2026-11-28 15:11 UTC (permalink / raw)
To: pve-devel; +Cc: Jakob-Tobias Winter
Allow overriding the cluster wide stop-on-poweroff setting for a single
HA resource. The selector is disabled for containers, as the HA stack
only learns about the poweroff of virtual machines.
Signed-off-by: Jakob-Tobias Winter <tobias@stuttgart.de>
---
www/manager6/ha/ResourceEdit.js | 28 ++++++++++++++++++++++++++++
1 file changed, 28 insertions(+)
diff --git a/www/manager6/ha/ResourceEdit.js b/www/manager6/ha/ResourceEdit.js
index 487d9cf3..488874df 100644
--- a/www/manager6/ha/ResourceEdit.js
+++ b/www/manager6/ha/ResourceEdit.js
@@ -60,6 +60,27 @@ Ext.define('PVE.ha.VMResourceInputPanel', {
},
});
+ // only virtual machines report that they got powered off from within the guest
+ var stopOnPoweroff = Ext.createWidget({
+ xtype: 'proxmoxKVComboBox',
+ name: 'stop-on-poweroff',
+ fieldLabel: gettext('Stop on Poweroff'),
+ value: '__default__',
+ deleteEmpty: !me.isCreate,
+ disabled: me.guestType === 'ct',
+ comboItems: [
+ ['__default__', Proxmox.Utils.defaultText],
+ ['1', Proxmox.Utils.yesText],
+ ['0', Proxmox.Utils.noText],
+ ],
+ autoEl: {
+ tag: 'div',
+ 'data-qtip': gettext(
+ 'Whether the VM should stay stopped when it gets powered off from within the guest. Defaults to the HA settings of the datacenter.',
+ ),
+ },
+ });
+
var vmidStore = me.vmid
? {}
: {
@@ -89,6 +110,12 @@ Ext.define('PVE.ha.VMResourceInputPanel', {
store: vmidStore,
allowBlank: false,
validateExists: true,
+ listeners: me.vmid
+ ? {}
+ : {
+ change: (field) =>
+ stopOnPoweroff.setDisabled(field.getSelection()?.data.type === 'lxc'),
+ },
},
{
xtype: 'proxmoxintegerfield',
@@ -137,6 +164,7 @@ Ext.define('PVE.ha.VMResourceInputPanel', {
uncheckedValue: 0,
value: 1,
},
+ stopOnPoweroff,
{
xtype: 'proxmoxKVComboBox',
name: 'state',
--
2.53.0
^ permalink raw reply related [flat|nested] 12+ messages in thread
* [RFC PATCH docs 10/11] ha-manager: document the stop-on-poweroff setting
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
` (8 preceding siblings ...)
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 ` 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
10 siblings, 0 replies; 12+ messages in thread
From: Jakob-Tobias Winter @ 2026-11-28 15:11 UTC (permalink / raw)
To: pve-devel; +Cc: Jakob-Tobias Winter
Describe what the HA stack does with a guest that gets powered off
from within, how the new setting changes that for virtual machines,
and in which cases it does not apply.
Signed-off-by: Jakob-Tobias Winter <tobias@stuttgart.de>
---
The files in generated/ are not touched.
ha-manager.adoc | 38 ++++++++++++++++++++++++++++++++++++++
1 file changed, 38 insertions(+)
diff --git a/ha-manager.adoc b/ha-manager.adoc
index c095fdc..8c9f1ea 100644
--- a/ha-manager.adoc
+++ b/ha-manager.adoc
@@ -1148,6 +1148,44 @@ kill its process if the service could not be stopped)
* *after* you fixed all errors you may request that the service starts again
+[[ha_manager_stop_on_poweroff]]
+Guest Poweroff
+--------------
+
+If the guest of a started HA resource is not running anymore, the HA stack
+starts it again. This also happens if the guest was shut down on purpose from
+within, for example with `poweroff` on the console of a virtual machine. To
+keep such a resource stopped, the guest has to be shut down through {pve}, that
+is with the web interface, the API or the command-line tools, which requests
+the `stopped` state for the resource.
+
+With the `stop-on-poweroff` setting enabled, the HA stack requests the `stopped`
+state by itself for a virtual machine that got powered off from within the
+guest. The resource is then handled like one that was shut down through {pve}:
+it stays stopped, it still gets recovered if its node fails, and starting the
+virtual machine through {pve} requests the `started` state again.
+
+The setting can be enabled in the web interface (`Datacenter` -> `Options` ->
+`HA Settings`), or directly in `datacenter.cfg`:
+
+----
+ha: stop-on-poweroff=1
+----
+
+A virtual machine that crashes, or whose process gets killed, is still started
+again, and so is one that reboots. The setting has no effect on containers.
+
+The requested state is only changed for a resource that the HA stack just keeps
+started on a node in normal operation. It stays `started` if the guest gets
+powered off while the HA stack moves the resource to another node or has frozen
+it, while the node of the resource gets shut down or is in maintenance mode, or
+while xref:ha_manager_disarm[HA is disarmed].
+
+NOTE: Enable the setting only after all nodes of the cluster got upgraded to a
+version that supports it. Older versions ignore all HA settings in
+`datacenter.cfg` as long as it contains a setting they do not know.
+
+
[[ha_manager_package_updates]]
Package Updates
---------------
base-commit: ea34e671b4f14c22497d449a627a323da2ab1512
--
2.53.0
^ permalink raw reply related [flat|nested] 12+ messages in thread
* [RFC PATCH docs 11/11] ha-manager: document the stop-on-poweroff property of resources
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
` (9 preceding siblings ...)
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 ` Jakob-Tobias Winter
10 siblings, 0 replies; 12+ messages in thread
From: Jakob-Tobias Winter @ 2026-11-28 15:11 UTC (permalink / raw)
To: pve-devel; +Cc: Jakob-Tobias Winter
Describe how the cluster wide setting can be overridden for single
HA resources.
Signed-off-by: Jakob-Tobias Winter <tobias@stuttgart.de>
---
ha-manager.adoc | 9 +++++++++
1 file changed, 9 insertions(+)
diff --git a/ha-manager.adoc b/ha-manager.adoc
index 8c9f1ea..58a5878 100644
--- a/ha-manager.adoc
+++ b/ha-manager.adoc
@@ -1172,6 +1172,15 @@ The setting can be enabled in the web interface (`Datacenter` -> `Options` ->
ha: stop-on-poweroff=1
----
+It can also be set for single resources, which overrides the cluster wide
+setting in both directions. This allows, for example, to keep a few virtual
+machines started at all times, while all others stay stopped after a poweroff
+from within the guest:
+
+----
+# ha-manager set vm:100 --stop-on-poweroff 0
+----
+
A virtual machine that crashes, or whose process gets killed, is still started
again, and so is one that reboots. The setting has no effect on containers.
--
2.53.0
^ permalink raw reply related [flat|nested] 12+ messages in thread
end of thread, other threads:[~2026-11-28 15:22 UTC | newest]
Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 ` [RFC PATCH ha-manager 03/11] lrm: do not start a service again that is about to be stopped Jakob-Tobias Winter
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
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox