[pve-devel] [PATCH common v2 4/4] Fix #7175: replace timezone handling with systemd timedatectl

Stefan Mayr stefan at mayr-stefan.de
Sun Jan 25 22:01:51 CET 2026


Usage of /etc/timezone is deprecated. The tzdata maintainers recommend
switching to timedatectl.

This removes handling the timezone file from the INotify module and adds
functions to the Systemd module as a replacement

Suggested-by: Fabian Grünbichler <f.gruenbichler at proxmox.com>
Suggested-by: Maximiliano Sandroval <m.sandoval at proxmox.com>
Signed-off-by: Stefan Mayr <stefan at mayr-stefan.de>
---
 src/PVE/INotify.pm | 29 -----------------------------
 src/PVE/Systemd.pm | 35 ++++++++++++++++++++++++++++++++++-
 2 files changed, 34 insertions(+), 30 deletions(-)

diff --git a/src/PVE/INotify.pm b/src/PVE/INotify.pm
index 62b3ca8..03c871c 100644
--- a/src/PVE/INotify.pm
+++ b/src/PVE/INotify.pm
@@ -651,35 +651,6 @@ register_file(
     \&update_etc_resolv_conf,
 );
 
-sub read_etc_timezone {
-    my ($filename, $fd) = @_;
-
-    my $timezone = <$fd>;
-
-    chomp $timezone;
-
-    return $timezone;
-}
-
-sub write_etc_timezone {
-    my ($filename, $fh, $timezone) = @_;
-
-    my $tzinfo = "/usr/share/zoneinfo/$timezone";
-
-    raise_param_exc({ 'timezone' => "No such timezone" })
-        if (!-f $tzinfo);
-
-    ($timezone) = $timezone =~ m/^(.*)$/; # untaint
-
-    print $fh "$timezone\n";
-
-    unlink("/etc/localtime");
-    symlink("/usr/share/zoneinfo/$timezone", "/etc/localtime");
-
-}
-
-register_file('timezone', "/etc/timezone", \&read_etc_timezone, \&write_etc_timezone);
-
 sub read_active_workers {
     my ($filename, $fh) = @_;
 
diff --git a/src/PVE/Systemd.pm b/src/PVE/Systemd.pm
index 6ff0dc8..aabb774 100644
--- a/src/PVE/Systemd.pm
+++ b/src/PVE/Systemd.pm
@@ -10,7 +10,8 @@ use Net::DBus::Reactor;
 use POSIX qw(EINTR);
 use Socket qw(SOCK_DGRAM);
 
-use PVE::Tools qw(file_set_contents file_get_contents trim);
+use PVE::Exception qw(raise_param_exc);
+use PVE::Tools qw(file_set_contents file_get_contents run_command trim);
 
 sub escape_unit {
     my ($val, $is_path) = @_;
@@ -285,6 +286,38 @@ sub write_ini {
     file_set_contents($filename, $content);
 }
 
+# Use systemds timedatectl for managing timezone settings
+sub get_timezone {
+    my $timezone;
+
+    PVE::Tools::run_command(
+        ['timedatectl', 'show', '--property=Timezone', '--value'],
+        outfunc => sub { $timezone //= shift },
+    );
+
+    return $timezone;
+}
+
+sub set_timezone {
+    my ($timezone) = @_;
+
+    raise_param_exc({ 'timezone' => "No such timezone" })
+        if (!grep { $_ eq $timezone } list_timezones());
+
+    PVE::Tools::run_command(['timedatectl', 'set-timezone', $timezone]);
+}
+
+sub list_timezones {
+    my @timezones = ();
+
+    PVE::Tools::run_command(
+        ['timedatectl', 'list-timezones'],
+        outfunc => sub { push(@timezones, shift); },
+    );
+
+    return @timezones;
+}
+
 =head3 notify()
 
 This is a pure Perl reimplementation of systemd's C<sd_notify()> mechanism as defined in
-- 
2.34.1




More information about the pve-devel mailing list