From: Jonas Theisen <j.theisen@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [PATCH common 01/16] systemd: add helpers to get and set NTP servers
Date: Thu, 26 Nov 2026 14:39:58 +0100 [thread overview]
Message-ID: <20261126134128.316681-2-j.theisen@proxmox.com> (raw)
In-Reply-To: <20261126134128.316681-1-j.theisen@proxmox.com>
Allow configuring custom servers for the active NTP service.
Supported are chrony, the default on new installations, and
systemd-timesyncd. For chrony, the servers are written to a drop-in in
/etc/chrony/sources.d. Since chrony has no way to disable the default
pool from a drop-in, the pool/server/peer lines in chrony.conf are
commented out with a marker and restored once the custom servers are
removed again. For systemd-timesyncd, a drop-in setting NTP= is used,
which already takes precedence over the default servers.
NTP pools are considered separately for chrony since it is able
to distinguish between servers and pools, while timesyncd only
supports single servers.
The service is only reloaded or restarted if its configuration
actually changed. chronyd can reload the sources directory at runtime,
but needs a restart to pick up changes to chrony.conf.
Signed-off-by: Jonas Theisen <j.theisen@proxmox.com>
---
src/PVE/Systemd.pm | 173 +++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 173 insertions(+)
diff --git a/src/PVE/Systemd.pm b/src/PVE/Systemd.pm
index 1ed5271..abd3c61 100644
--- a/src/PVE/Systemd.pm
+++ b/src/PVE/Systemd.pm
@@ -13,6 +13,7 @@ use Socket qw(SOCK_DGRAM);
use PVE::Cmd qw(run);
use PVE::Exception qw(raise_param_exc);
use PVE::File qw(file_set_contents file_get_contents);
+use PVE::JSONSchema;
use PVE::Tools qw(trim);
sub escape_unit {
@@ -319,6 +320,178 @@ sub list_timezones {
return @timezones;
}
+my $ntp_lock_file = '/var/lock/proxmox-ntp.lck';
+my $chrony_conf = '/etc/chrony/chrony.conf';
+my $chrony_sources = '/etc/chrony/sources.d/proxmox.sources';
+my $timesyncd_conf_dir = '/etc/systemd/timesyncd.conf.d';
+my $timesyncd_conf = "$timesyncd_conf_dir/proxmox.conf";
+
+# Prefix for the default sources in a config file that get replaced by custom servers. It allows
+# restoring exactly those sources once the custom servers are removed again.
+my $ntp_disabled_marker = '#proxmox-disabled# ';
+
+sub get_ntp_service {
+ for my $service ('chrony', 'systemd-timesyncd') {
+ return $service if is_unit_active("${service}.service");
+ }
+ return undef;
+}
+
+# systemd-timesyncd only ever uses a single server, so it has no notion of pools
+sub is_ntp_pools_supported {
+ my ($service) = @_;
+
+ return defined($service) && $service eq 'chrony' ? 1 : 0;
+}
+
+# Returns the custom NTP servers and pools as { servers => [...], pools => [...] }, or undef if the
+# service uses its default sources.
+sub get_ntp_servers {
+ my $service = get_ntp_service() // return undef;
+
+ my $sources = { servers => [], pools => [] };
+ if ($service eq 'chrony') {
+ return undef if !-f $chrony_sources;
+
+ for my $line (split(/\n/, file_get_contents($chrony_sources))) {
+ next if $line !~ m/^\s*(server|pool)\s+(\S+)/;
+ push $sources->{ $1 eq 'pool' ? 'pools' : 'servers' }->@*, $2;
+ }
+ } elsif ($service eq 'systemd-timesyncd') {
+ return undef if !-f $timesyncd_conf;
+
+ my $ntp = read_ini($timesyncd_conf)->{Time}->{NTP} // [];
+ for my $value (ref($ntp) ? @$ntp : ($ntp)) {
+ push $sources->{servers}->@*, split(/\s+/, trim($value));
+ }
+ }
+
+ return scalar($sources->{servers}->@*) || scalar($sources->{pools}->@*) ? $sources : undef;
+}
+
+# Rewrites chrony.conf so that its default sources are disabled if $sources is defined and enabled
+# otherwise. Returns whether the file changed.
+my sub update_chrony_conf {
+ my ($sources) = @_;
+
+ my $content = file_get_contents($chrony_conf);
+ my $new_content = '';
+ for my $line (split(/^/, $content)) {
+ if (!defined($sources)) {
+ $line =~ s/^\Q$ntp_disabled_marker\E//;
+ } elsif ($line =~ m/^\s*(?:server|pool|peer)\s/) {
+ $line = "${ntp_disabled_marker}${line}";
+ }
+ $new_content .= $line;
+ }
+
+ return 0 if $new_content eq $content;
+
+ file_set_contents($chrony_conf, $new_content);
+ return 1;
+}
+
+my sub set_chrony_servers {
+ my ($sources) = @_;
+
+ my $conf_changed = update_chrony_conf($sources);
+
+ my $sources_changed = 0;
+ if (defined($sources)) {
+ my $content = join(
+ '',
+ (map { "server $_ iburst\n" } $sources->{servers}->@*),
+ (map { "pool $_ iburst\n" } $sources->{pools}->@*),
+ );
+ my $old_content = -f $chrony_sources ? file_get_contents($chrony_sources) : undef;
+ if (!defined($old_content) || $old_content ne $content) {
+ file_set_contents($chrony_sources, $content);
+ $sources_changed = 1;
+ }
+ } elsif (-f $chrony_sources) {
+ unlink($chrony_sources) or die "failed to remove '$chrony_sources' - $!\n";
+ $sources_changed = 1;
+ }
+
+ # chronyd only reads chrony.conf on startup, but can reload the sources directory at runtime
+ if ($conf_changed) {
+ run(['systemctl', 'try-restart', 'chrony.service']);
+ } elsif ($sources_changed) {
+ run(['chronyc', 'reload', 'sources'], outfunc => sub { });
+ }
+}
+
+my sub set_timesyncd_servers {
+ my ($sources) = @_;
+
+ my $changed = 0;
+ if (defined($sources)) {
+ my $content = "[Time]\nNTP=" . join(' ', $sources->{servers}->@*) . "\n";
+ my $old_content = -f $timesyncd_conf ? file_get_contents($timesyncd_conf) : undef;
+ if (!defined($old_content) || $old_content ne $content) {
+ if (!-d $timesyncd_conf_dir) {
+ mkdir($timesyncd_conf_dir) or die "failed to create '$timesyncd_conf_dir' - $!\n";
+ }
+ file_set_contents($timesyncd_conf, $content);
+ $changed = 1;
+ }
+ } elsif (-f $timesyncd_conf) {
+ unlink($timesyncd_conf) or die "failed to remove '$timesyncd_conf' - $!\n";
+ $changed = 1;
+ }
+
+ run(['systemctl', 'try-restart', 'systemd-timesyncd.service']) if $changed;
+}
+
+# Checks that the NTP servers and pools, given as for set_ntp_servers(), can be configured for the
+# active service and returns that service. Allows callers to check them before applying other
+# changes, so that a request is either applied completely or not at all.
+sub check_ntp_servers {
+ my ($servers, $pools) = @_;
+
+ my $sources = { servers => $servers // [], pools => $pools // [] };
+ for my $type ('servers', 'pools') {
+ for my $address ($sources->{$type}->@*) {
+ raise_param_exc({ "ntp-$type" => "invalid address '$address'" })
+ if !PVE::JSONSchema::pve_verify_address($address, 1);
+ }
+ }
+
+ my $service = get_ntp_service()
+ or die "no supported time synchronization service (chrony, systemd-timesyncd) active\n";
+
+ raise_param_exc({ 'ntp-pools' => "pools are not supported by $service" })
+ if scalar($sources->{pools}->@*) && !is_ntp_pools_supported($service);
+
+ return $service;
+}
+
+# Configures the custom NTP servers and pools, given as array references of addresses. If both are
+# empty or undef, the service uses its default sources again.
+sub set_ntp_servers {
+ my ($servers, $pools) = @_;
+
+ my $service = check_ntp_servers($servers, $pools);
+
+ my $sources = { servers => $servers // [], pools => $pools // [] };
+ $sources = undef if !scalar($sources->{servers}->@*) && !scalar($sources->{pools}->@*);
+
+ PVE::Tools::lock_file(
+ $ntp_lock_file,
+ 10,
+ sub {
+ if ($service eq 'chrony') {
+ set_chrony_servers($sources);
+ } elsif ($service eq 'systemd-timesyncd') {
+ set_timesyncd_servers($sources);
+ }
+ },
+ );
+ 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:41 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 ` Jonas Theisen [this message]
2026-11-26 13:39 ` [PATCH common 02/16] systemd: add helper to query clock synchronization status Jonas Theisen
2026-11-26 13:40 ` [PATCH common 03/16] systemd: add helpers to synchronize the clock immediately Jonas Theisen
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-2-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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.