From: Jonas Theisen <j.theisen@proxmox.com>
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 [thread overview]
Message-ID: <20261126134128.316681-4-j.theisen@proxmox.com> (raw)
In-Reply-To: <20261126134128.316681-1-j.theisen@proxmox.com>
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 <j.theisen@proxmox.com>
---
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<sd_notify()> mechanism as defined in
--
2.47.3
next prev parent reply other threads:[~2026-11-26 13:42 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-26 13:39 [PATCH common/manager/widget-toolkit 00/16] partially fix #3809: configure and monitor NTP in the node time panel Jonas Theisen
2026-11-26 13:39 ` [PATCH common 01/16] systemd: add helpers to get and set NTP servers Jonas Theisen
2026-11-26 13:39 ` [PATCH common 02/16] systemd: add helper to query clock synchronization status Jonas Theisen
2026-11-26 13:40 ` Jonas Theisen [this message]
2026-11-26 13:40 ` [PATCH common 04/16] systemd: add helper to list the current NTP sources Jonas Theisen
2026-11-26 13:40 ` [PATCH widget-toolkit 05/16] node: time: do not open the editor for the server time row Jonas Theisen
2026-11-26 13:40 ` [PATCH widget-toolkit 06/16] partially fix #3809: node: time: show and edit NTP servers if supported Jonas Theisen
2026-11-26 13:40 ` [PATCH widget-toolkit 07/16] node: time: show clock synchronization status Jonas Theisen
2026-11-26 13:40 ` [PATCH widget-toolkit 08/16] window: add reusable service log window Jonas Theisen
2026-11-26 13:40 ` [PATCH widget-toolkit 09/16] node: time: add button to open the NTP service log Jonas Theisen
2026-11-26 13:40 ` [PATCH widget-toolkit 10/16] node: time: add button to synchronize the clock immediately Jonas Theisen
2026-11-26 13:40 ` [PATCH widget-toolkit 11/16] node: time: add window listing the current NTP sources Jonas Theisen
2026-11-26 13:40 ` [PATCH manager 12/16] partially fix #3809: api: nodes: time: allow configuring NTP servers Jonas Theisen
2026-11-26 13:40 ` [PATCH manager 13/16] api: nodes: time: report clock synchronization status Jonas Theisen
2026-11-26 13:40 ` [PATCH manager 14/16] ui: node: time: use journal for the NTP service log Jonas Theisen
2026-11-26 13:40 ` [PATCH manager 15/16] api: nodes: time: add endpoint to synchronize the clock immediately Jonas Theisen
2026-11-26 13:40 ` [PATCH manager 16/16] api: nodes: time: add endpoint to list the current NTP sources Jonas Theisen
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=20261126134128.316681-4-j.theisen@proxmox.com \
--to=j.theisen@proxmox.com \
--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