[pve-devel] [PATCH container] fix #1423: set container timezone to match host in post-create

Oguz Bektas o.bektas at proxmox.com
Thu May 28 16:27:05 CEST 2020


we cache the /etc/localtime symlink path from the node and set it in the
container rootfs if zone file exists in the container

Signed-off-by: Oguz Bektas <o.bektas at proxmox.com>
---
 src/PVE/LXC/Setup.pm      |  3 +++
 src/PVE/LXC/Setup/Base.pm | 20 +++++++++++++++++---
 2 files changed, 20 insertions(+), 3 deletions(-)

diff --git a/src/PVE/LXC/Setup.pm b/src/PVE/LXC/Setup.pm
index c738e64..929f75b 100644
--- a/src/PVE/LXC/Setup.pm
+++ b/src/PVE/LXC/Setup.pm
@@ -5,6 +5,8 @@ use warnings;
 use POSIX;
 use PVE::Tools;
 
+use Cwd 'abs_path';
+
 use PVE::LXC::Setup::Debian;
 use PVE::LXC::Setup::Ubuntu;
 use PVE::LXC::Setup::CentOS;
@@ -103,6 +105,7 @@ sub new {
 
     # Cache some host files we need access to:
     $plugin->{host_resolv_conf} = PVE::INotify::read_file('resolvconf');
+    $plugin->{host_localtime} = abs_path('/etc/localtime');
 
     # pass on user namespace information:
     my ($id_map, $rootuid, $rootgid) = PVE::LXC::parse_id_maps($conf);
diff --git a/src/PVE/LXC/Setup/Base.pm b/src/PVE/LXC/Setup/Base.pm
index 93dace7..e94e802 100644
--- a/src/PVE/LXC/Setup/Base.pm
+++ b/src/PVE/LXC/Setup/Base.pm
@@ -451,6 +451,19 @@ my $randomize_crontab = sub {
    }
 };
 
+sub set_timezone {
+    my ($self) = @_;
+
+    my $zoneinfo = $self->{host_localtime};
+
+    ($zoneinfo) = $zoneinfo =~ m/^(.*)$/; # untaint
+
+    if ($self->ct_file_exists($zoneinfo)) {
+	$self->ct_unlink("/etc/localtime");
+	$self->ct_symlink($zoneinfo, "/etc/localtime");
+    }
+}
+
 sub pre_start_hook {
     my ($self, $conf) = @_;
 
@@ -466,16 +479,17 @@ sub post_create_hook {
     my ($self, $conf, $root_password, $ssh_keys) = @_;
 
     $self->template_fixup($conf);
-    
+
     &$randomize_crontab($self, $conf);
-    
+
     $self->set_user_password($conf, 'root', $root_password);
     $self->set_user_authorized_ssh_keys($conf, 'root', $ssh_keys) if $ssh_keys;
     $self->setup_init($conf);
     $self->setup_network($conf);
     $self->set_hostname($conf);
     $self->set_dns($conf);
-    
+    $self->set_timezone();
+
     # fixme: what else ?
 }
 
-- 
2.20.1




More information about the pve-devel mailing list