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 06/11] resources: add stop-on-poweroff property for virtual machines
Date: Sat, 28 Nov 2026 16:11:13 +0100 [thread overview]
Message-ID: <20261128151118.892037-7-tobias@stuttgart.de> (raw)
In-Reply-To: <20261128151118.892037-1-tobias@stuttgart.de>
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
next prev parent reply other threads:[~2026-11-28 15:22 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 ` [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 ` Jakob-Tobias Winter [this message]
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-7-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