From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [45.144.208.40]) by lore.proxmox.com (Postfix) with ESMTPS id 9FA481FF0A8 for ; Sat, 28 Nov 2026 16:22:24 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 1E67A21876; Sat, 28 Nov 2026 16:21:13 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=stuttgart.de; s=s1022; t=1795878688; bh=6IHna78QXIFRgejV5i0z/70hbDogSrarEGpu1iQa6NU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ytzToHQoC/CRcqhZ6d3kwAPFgJl7xJWhJcj1Y0ROfiUeXqmAhOaz8+qnbeIksIel0 MGSjIXCGKQC0XB5XppCZxHiQqMazLRp9GJJAE4Uepa7R2bGlwAKXRq/yAogWiB3Htg LlG42y51MD0dMjwpF+2sI5hLA8HHw/JTcNTkj3GCOEu6twxzfPojw5HL6KKs0oDWj3 DF9k/bZdR2QEC6uL2y4xEW24Dkf/MlhGAv1vcXwIKqZK2IcDPbJnFbYGxv7s+4P0ZR a2kWno3BAc8o8+/rLFAF29FvrYVZG53Cxm4bxppeV9sZuis+aOWNgGF6LZur1L8jVD qEg7zhmI9nOUQ== From: Jakob-Tobias Winter To: pve-devel@lists.proxmox.com 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 Message-ID: <20261128151118.892037-3-tobias@stuttgart.de> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261128151118.892037-1-tobias@stuttgart.de> References: <20261128151118.892037-1-tobias@stuttgart.de> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.026 Adjusted score from AWL reputation of From: address DKIM_SIGNED 0.1 Message has a DKIM or DK signature, not necessarily valid DKIM_VALID -0.1 Message has at least one valid DKIM or DK signature DKIM_VALID_AU -0.1 Message has a valid DKIM or DK signature from author's domain DKIM_VALID_EF -0.1 Message has a valid DKIM or DK signature from envelope-from domain DMARC_PASS -0.1 DMARC pass policy RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_PASS -0.001 SPF: HELO matches SPF record SPF_PASS -0.001 SPF: sender matches SPF record Message-ID-Hash: 5MJOX5EZP7HXNRIHRYGF7WSW7GOSVYXJ X-Message-ID-Hash: 5MJOX5EZP7HXNRIHRYGF7WSW7GOSVYXJ X-MailFrom: prvs=7495f99b94=tobias@stuttgart.de X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: Jakob-Tobias Winter X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox VE development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: 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 --- 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