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 372041FF0A8 for ; Sat, 28 Nov 2026 16:22:33 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 9B6BA2188A; 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=1795878689; bh=yRUwa9h9jjSQi07fa/SEs3BW3OlzMI+7LC+s4urbEDk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=I2sl4AhQ5JsETH7a3VBITJefiSIAMjMpyTRXutCSCRKPL/AilWaKtpVy4tgCk7ibF I3Tc7Ks4oT6UThmPCWsRaTAVFuzzvJhQOUvbb5NMAH7hFld15DU0tOldQGS08YUmY/ otpuVYpkIiqIij9vFvYcEuxJwmSokffu337vsVRFsAyIY5rLgF/YdRlAHYTWWqlWD9 v8G9bLi+XNjarINO+KiuhnnggdEjcjyHbQKz+2UGezd4ldhe87Ufadyu+KnJbUCGaT T0iA1+EwtHTEftm15k+YDGA4mnK+iivO8NvPQ10xsOEKQgTFDDOeHM8Xym2lLDG9fm RkH4kbY+vbP0g== From: Jakob-Tobias Winter To: pve-devel@lists.proxmox.com 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 Message-ID: <20261128151118.892037-7-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.025 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: UQSCEIYOBGCZW556ZIJKH2FAUNZCAOOZ X-Message-ID-Hash: UQSCEIYOBGCZW556ZIJKH2FAUNZCAOOZ 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: 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 --- 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