]> git.proxmox.com Git - pve-container.git/commitdiff
setup: also set contents of /etc/timezone
authorFabian Ebner <f.ebner@proxmox.com>
Thu, 7 Oct 2021 10:48:03 +0000 (12:48 +0200)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Thu, 7 Oct 2021 12:32:42 +0000 (14:32 +0200)
Some distributions like CentOS 8 and Gentoo don't have the file, so
only update if it already existed.

A slight change in behavior in set_timezone is that the warning will
now trigger if /etc/localtime is a link to $tz_path, but $tz_path does
not exist. Previously, it would return early if the link matched.

Programs that rely on /etc/timezone within the container will now see
the configured timezone too. While that is more correct, it's still a
change that might be unexpected.

Reported in the community forum:
https://forum.proxmox.com/threads/pct-create-command-with-timezone-host-option-fails-to-create-a-container.97538/

Signed-off-by: Fabian Ebner <f.ebner@proxmox.com>
src/PVE/LXC/Setup.pm
src/PVE/LXC/Setup/Base.pm

index 7c377abc68917424bb807230f904357e20229183..5cc56af7dc7cecb3cf87253b72732e8edd916bd1 100644 (file)
@@ -114,6 +114,7 @@ sub new {
 
     # Cache some host files we need access to:
     $plugin->{host_resolv_conf} = PVE::INotify::read_file('resolvconf');
+    $plugin->{host_timezone} = PVE::INotify::read_file('timezone');
 
     abs_path('/etc/localtime') =~ m|^(/.+)| or die "invalid /etc/localtime\n"; # untaint
     $plugin->{host_localtime} = $1;
index 04332ea75a9953fed79e7d3aa7cb3196c0064c7b..dafd69afc1ce649db739f4082397df4cfcaa296f 100644 (file)
@@ -469,12 +469,18 @@ sub set_timezone {
        $tz_path = $self->{host_localtime};
     }
 
-    return if abs_path('/etc/localtime') eq $tz_path;
-
     if ($self->ct_file_exists($tz_path)) {
-       my $tmpfile = "localtime.$$.new.tmpfile";
-       $self->ct_symlink($tz_path, $tmpfile);
-       $self->ct_rename($tmpfile, "/etc/localtime");
+       if (abs_path('/etc/localtime') ne $tz_path) {
+           my $tmpfile = "localtime.$$.new.tmpfile";
+           $self->ct_symlink($tz_path, $tmpfile);
+           $self->ct_rename($tmpfile, "/etc/localtime");
+       }
+
+       # not all distributions have /etc/timezone
+       if ($self->ct_file_exists('/etc/timezone')) {
+           my $contents = $zoneinfo eq 'host' ? $self->{host_timezone} : $zoneinfo;
+           $self->ct_file_set_contents('/etc/timezone', "$contents\n");
+       }
     } else {
        warn "container does not have $tz_path, timezone can not be modified\n";
     }