From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [IPv6:2a0f:8001:1:32::40]) by lore.proxmox.com (Postfix) with ESMTPS id 1D8191FF0A8 for ; Sat, 28 Nov 2026 16:21:29 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 32D9B21745; Sat, 28 Nov 2026 16:21:10 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=stuttgart.de; s=s1022; t=1795878688; bh=5up7EeuonGJ2/d6vQYopSupySy9NdzRtRMQQA9c6uVs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=S74jkAnjAGwRjMZkLlu8pgeTf3R73uvVsq9ar+KoixWvh8Gca1kb07R05/srAD9C0 HDxZyxynq5OYkvfZVwQyvava4tMo/kypLubK+/fEiXUnEdplBOhpIf2o+dcS0NReEf vGB61coB0Fm0Ec35LeC7N1IjlFEEdUxUb2Ikz11GjFkhzGOAOs4jKqM22hAYLaW0g7 oFrJbplvjku5SSQ5cUOSs73CpERlitsHNd26qg0bXc96VDRyv2RXo/13k2YkO1Yn1/ qkOywnDjwyox0UCJXGVSZygAxPVk7mxjcYR10y+aqtgAMyYFGE1CMvtl2iwJnY67o3 dJ/a8h1lFdvVQ== From: Jakob-Tobias Winter To: pve-devel@lists.proxmox.com Subject: [RFC PATCH ha-manager 03/11] lrm: do not start a service again that is about to be stopped Date: Sat, 28 Nov 2026 16:11:10 +0100 Message-ID: <20261128151118.892037-4-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.031 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: ZP2BJITG6PYRED77VG7JZDEB4SJLL73E X-Message-ID-Hash: ZP2BJITG6PYRED77VG7JZDEB4SJLL73E 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: 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 --- 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