public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
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 02/11] fix #6314: config: request stopped state for powered off guests
Date: Sat, 28 Nov 2026 16:11:09 +0100	[thread overview]
Message-ID: <20261128151118.892037-3-tobias@stuttgart.de> (raw)
In-Reply-To: <20261128151118.892037-1-tobias@stuttgart.de>

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




  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 ` Jakob-Tobias Winter [this message]
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

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-3-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
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal