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 860D31FF0B3 for ; Thu, 26 Nov 2026 14:42:09 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 466B42174E; Thu, 26 Nov 2026 14:41:58 +0100 (CET) From: Jonas Theisen To: pve-devel@lists.proxmox.com Subject: [PATCH common 03/16] systemd: add helpers to synchronize the clock immediately Date: Thu, 26 Nov 2026 14:40:00 +0100 Message-ID: <20261126134128.316681-4-j.theisen@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261126134128.316681-1-j.theisen@proxmox.com> References: <20261126134128.316681-1-j.theisen@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1795700504494 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.070 Adjusted score from AWL reputation of From: address DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment (newer systems) RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Message-ID-Hash: 4JX46EKUGYB5SLGUSMRULQ4MDUHWLFK7 X-Message-ID-Hash: 4JX46EKUGYB5SLGUSMRULQ4MDUHWLFK7 X-MailFrom: j.theisen@proxmox.com 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 X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox VE development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: After changing the NTP servers or noticing that the clock is off, admins may want to synchronize it right away instead of waiting for the time synchronization service to correct it over time. chrony supports this at runtime: a burst of measurements is requested and the automatic stepping of the 'makestep' directive is re-armed with the configured values, as described in chronyc(1). The configured stepping usually only applies to the first clock updates after chronyd started, so the clock is then stepped just like on startup. Using the configured values instead of fixed ones keeps a custom stepping policy in place, for example one that always allows stepping. Without the directive, chronyd never steps the clock, so it is only slewed sooner. systemd-timesyncd has no runtime command for this, but polls its server right after starting and steps the clock if needed, so it is restarted instead. Signed-off-by: Jonas Theisen --- src/PVE/Systemd.pm | 83 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/src/PVE/Systemd.pm b/src/PVE/Systemd.pm index 35554ce..35551a3 100644 --- a/src/PVE/Systemd.pm +++ b/src/PVE/Systemd.pm @@ -503,6 +503,89 @@ sub set_ntp_servers { return; } +sub is_ntp_sync_supported { + my ($service) = @_; + + return 0 if !defined($service); + return (grep { $_ eq $service } ('chrony', 'systemd-timesyncd')) ? 1 : 0; +} + +# Returns the threshold and limit of the effective 'makestep' directive of chronyd as array +# reference, or undef if there is none. Like chronyd, this follows the 'include' and 'confdir' +# directives in place and uses the last 'makestep' directive. +my sub get_chrony_makestep { + my $makestep; + + my sub parse_file { + use feature 'current_sub'; + my ($file, $depth) = @_; + + # chronyd refuses deeper nesting, which also prevents include loops + die "too deeply nested chrony configuration includes\n" if $depth > 10; + return if !-f $file; + + for my $line (split(/\n/, file_get_contents($file))) { + next if $line =~ m/^\s*[!;#%]/; # comment characters of chrony.conf(5) + my ($directive, @args) = split(/\s+/, trim($line)); + next if !defined($directive); + $directive = lc($directive); + + if ($directive eq 'makestep') { + # use the captures, as the values are passed to chronyc, which taint mode in the + # API daemons only allows for untainted values + my ($threshold) = ($args[0] // '') =~ m/^(\d+(?:\.\d+)?)$/; + my ($limit) = ($args[1] // '') =~ m/^(-?\d+)$/; + $makestep = [$threshold, $limit] if defined($threshold) && defined($limit); + } elsif ($directive eq 'include' && defined($args[0])) { + __SUB__->($_, $depth + 1) for glob($args[0]); + } elsif ($directive eq 'confdir') { + # if multiple directories contain a file with the same name, the first one wins + my $files = {}; + for my $dir (@args) { + for my $path (glob("$dir/*.conf")) { + my ($name) = $path =~ m|([^/]+)$|; + $files->{$name} //= $path; + } + } + __SUB__->($files->{$_}, $depth + 1) for sort keys $files->%*; + } + } + } + parse_file($chrony_conf, 0); + + return $makestep; +} + +sub sync_ntp { + my $service = get_ntp_service(); + + die "immediate synchronization is not supported for the active time synchronization service\n" + if !is_ntp_sync_supported($service); + + PVE::Tools::lock_file( + $ntp_lock_file, + 10, + sub { + if ($service eq 'chrony') { + # The configured automatic stepping usually only applies to the first few clock + # updates after chronyd started. Re-arm it with the configured values, so the burst + # of measurements steps the clock exactly when chronyd would on startup, see + # chronyc(1). Without the directive, chronyd never steps and only slews the clock. + if (my $makestep = get_chrony_makestep()) { + run(['chronyc', 'makestep', $makestep->@*], outfunc => sub { }); + } + run(['chronyc', 'burst', '1/2'], outfunc => sub { }); + } else { + # timesyncd polls its server right after starting and steps the clock if needed + run(['systemctl', 'restart', "${service}.service"]); + } + }, + ); + die $@ if $@; + + return; +} + =head3 notify() This is a pure Perl reimplementation of systemd's C mechanism as defined in -- 2.47.3