public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Jonas Theisen <j.theisen@proxmox.com>
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	[thread overview]
Message-ID: <20261126134128.316681-13-j.theisen@proxmox.com> (raw)
In-Reply-To: <20261126134128.316681-1-j.theisen@proxmox.com>

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 <j.theisen@proxmox.com>
---

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





  parent reply	other threads:[~2026-11-26 13:43 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 ` [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 ` Jonas Theisen [this message]
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-13-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
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal