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 EF74E1FF0B3 for ; Thu, 26 Nov 2026 14:41:51 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 09F9721721; Thu, 26 Nov 2026 14:41:44 +0100 (CET) From: Jonas Theisen 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 Message-ID: <20261126134128.316681-2-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: 1795700500312 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.071 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: TY5QBU527RSO3YG5GYQTTDD7EJBQ7I6L X-Message-ID-Hash: TY5QBU527RSO3YG5GYQTTDD7EJBQ7I6L 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: 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 --- 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 mechanism as defined in -- 2.47.3