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 727AE1FF0B3 for ; Thu, 26 Nov 2026 14:43:22 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 8794F217A5; Thu, 26 Nov 2026 14:42:35 +0100 (CET) From: Jonas Theisen To: pve-devel@lists.proxmox.com Subject: [PATCH manager 12/16] partially fix #3809: api: nodes: time: allow configuring NTP servers Date: Thu, 26 Nov 2026 14:40:09 +0100 Message-ID: <20261126134128.316681-13-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: 1795700523315 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.067 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: 3SRDRFRZ2Z4X5QJSXJLL5SZHKJYN2L4E X-Message-ID-Hash: 3SRDRFRZ2Z4X5QJSXJLL5SZHKJYN2L4E 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: Return the active time synchronization service and its custom NTP servers and pools, if any, and allow setting or resetting them via the existing time endpoint. Pools are separate from servers, as the services use multiple addresses of a pool, like they do for the default Debian pool. To stay backwards compatible, the new properties are optional and the time zone parameter of the PUT call becomes optional too, so existing callers that always pass the time zone behave as before. The NTP properties are only returned if the active service is supported, which allows clients to decide whether to offer the setting at all. Servers and pools can be reset individually with the 'delete' parameter, like for other config endpoints. Once neither is set, the service uses its default sources again. Like other list parameters, for example the Ceph monitor addresses, an empty list is accepted and has the same effect as deleting the setting. The NTP settings are checked before the time zone is changed, so a request that fails, for example because the active service does not support pools, does not change anything. As the PUT method no longer only sets the time zone, rename it from set_timezone to set_timesettings. The name is not part of the API path, so API clients are not affected. Signed-off-by: Jonas Theisen --- Notes: This depends on the NTP helpers in PVE::Systemd from pve-common. PVE/API2/Nodes.pm | 95 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 91 insertions(+), 4 deletions(-) diff --git a/PVE/API2/Nodes.pm b/PVE/API2/Nodes.pm index aa35a696..c7d8b139 100644 --- a/PVE/API2/Nodes.pm +++ b/PVE/API2/Nodes.pm @@ -1714,7 +1714,7 @@ __PACKAGE__->register_method({ permissions => { check => ['perm', '/nodes/{node}', ['Sys.Audit']], }, - description => "Read server time and time zone settings.", + description => "Read server time, time zone and NTP settings.", proxyto => 'node', parameters => { additionalProperties => 0, @@ -1730,6 +1730,32 @@ __PACKAGE__->register_method({ description => "Time zone", type => 'string', }, + 'ntp-service' => { + description => "The active time synchronization service, if configuring its" + . " NTP servers is supported.", + type => 'string', + enum => ['chrony', 'systemd-timesyncd'], + optional => 1, + }, + 'ntp-servers' => { + description => "Custom NTP servers. Neither these nor 'ntp-pools' are set if" + . " the time synchronization service uses its default sources.", + type => 'string', + format => 'address-list', + optional => 1, + }, + 'ntp-pools' => { + description => "Custom NTP pools, of which multiple addresses are used.", + type => 'string', + format => 'address-list', + optional => 1, + }, + 'ntp-pools-supported' => { + description => "Whether the active time synchronization service supports" + . " NTP pools.", + type => 'boolean', + optional => 1, + }, time => { description => "Seconds since 1970-01-01 00:00:00 UTC.", type => 'integer', @@ -1755,15 +1781,26 @@ __PACKAGE__->register_method({ localtime => $ltime, }; + if (my $service = PVE::Systemd::get_ntp_service()) { + $res->{'ntp-service'} = $service; + $res->{'ntp-pools-supported'} = PVE::Systemd::is_ntp_pools_supported($service); + if (my $sources = PVE::Systemd::get_ntp_servers()) { + for my $type ('servers', 'pools') { + my $addresses = $sources->{$type}; + $res->{"ntp-$type"} = join(',', @$addresses) if scalar(@$addresses); + } + } + } + return $res; }, }); __PACKAGE__->register_method({ - name => 'set_timezone', + name => 'set_timesettings', path => 'time', method => 'PUT', - description => "Set time zone.", + description => "Set time zone and NTP servers.", permissions => { check => ['perm', '/nodes/{node}', ['Sys.Modify']], }, @@ -1777,6 +1814,32 @@ __PACKAGE__->register_method({ description => "Time zone. The file '/usr/share/zoneinfo/zone.tab' contains the list of valid names.", type => 'string', + optional => 1, + }, + 'ntp-servers' => { + description => "NTP servers replacing the default servers of the active time" + . " synchronization service. Supported for chrony and systemd-timesyncd. An" + . " empty list is the same as deleting the setting.", + type => 'string', + format => 'address-list', + optional => 1, + }, + 'ntp-pools' => { + description => "NTP pools replacing the default sources of the active time" + . " synchronization service, of which multiple addresses are used. Not" + . " supported for systemd-timesyncd, which only uses a single server. An empty" + . " list is the same as deleting the setting.", + type => 'string', + format => 'address-list', + optional => 1, + }, + delete => { + description => "A list of settings you want to delete. If neither 'ntp-servers'" + . " nor 'ntp-pools' remain set, the time synchronization service uses its" + . " default sources again.", + type => 'string', + format => 'pve-configid-list', + optional => 1, }, }, }, @@ -1784,7 +1847,31 @@ __PACKAGE__->register_method({ code => sub { my ($param) = @_; - PVE::Systemd::set_timezone($param->{timezone}); + my $delete = { map { $_ => 1 } PVE::Tools::split_list($param->{delete} // '') }; + for my $key (sort keys $delete->%*) { + raise_param_exc({ delete => "cannot delete '$key'" }) + if $key ne 'ntp-servers' && $key ne 'ntp-pools'; + raise_param_exc({ delete => "cannot set and delete '$key' at the same time" }) + if defined($param->{$key}); + } + + my $changes = 0; + my $current = PVE::Systemd::get_ntp_servers() // { servers => [], pools => [] }; + for my $type ('servers', 'pools') { + if (defined(my $value = $param->{"ntp-$type"})) { + $current->{$type} = [PVE::Tools::split_list($value)]; + $changes = 1; + } elsif ($delete->{"ntp-$type"}) { + $current->{$type} = []; + $changes = 1; + } + } + + # check the NTP servers before changing anything, so a failing request has no effect + PVE::Systemd::check_ntp_servers($current->{servers}, $current->{pools}) if $changes; + + PVE::Systemd::set_timezone($param->{timezone}) if defined($param->{timezone}); + PVE::Systemd::set_ntp_servers($current->{servers}, $current->{pools}) if $changes; return; }, -- 2.47.3