]> git.proxmox.com Git - pve-container.git/blame - src/PVE/LXC/Setup.pm
setup: also set contents of /etc/timezone
[pve-container.git] / src / PVE / LXC / Setup.pm
CommitLineData
7af97ad5 1package PVE::LXC::Setup;
1c7f4f65
DM
2
3use strict;
4use warnings;
5
21ec7304 6use POSIX;
e6e308ae
OB
7use Cwd 'abs_path';
8
21ec7304
TL
9use PVE::Tools;
10
11use PVE::LXC::Setup::Alpine;
12use PVE::LXC::Setup::ArchLinux;
81a6ec3f 13use PVE::LXC::Setup::CentOS;
21ec7304
TL
14use PVE::LXC::Setup::Debian;
15use PVE::LXC::Setup::Devuan;
b0143ab1 16use PVE::LXC::Setup::Fedora;
ed027b58 17use PVE::LXC::Setup::Gentoo;
21ec7304
TL
18use PVE::LXC::Setup::SUSE;
19use PVE::LXC::Setup::Ubuntu;
7db46e47 20use PVE::LXC::Setup::Unmanaged;
1c7f4f65
DM
21
22my $plugins = {
21ec7304
TL
23 alpine => 'PVE::LXC::Setup::Alpine',
24 archlinux => 'PVE::LXC::Setup::ArchLinux',
25 centos => 'PVE::LXC::Setup::CentOS',
7af97ad5 26 debian => 'PVE::LXC::Setup::Debian',
151c5b73 27 devuan => 'PVE::LXC::Setup::Devuan',
b0143ab1 28 fedora => 'PVE::LXC::Setup::Fedora',
ed027b58 29 gentoo => 'PVE::LXC::Setup::Gentoo',
21ec7304
TL
30 opensuse => 'PVE::LXC::Setup::SUSE',
31 ubuntu => 'PVE::LXC::Setup::Ubuntu',
7db46e47 32 unmanaged => 'PVE::LXC::Setup::Unmanaged',
1c7f4f65
DM
33};
34
934a30b0
TL
35# a map to allow supporting related distro flavours
36my $plugin_alias = {
47447841 37 'opensuse-leap' => 'opensuse',
005e72bd 38 'opensuse-tumbleweed' => 'opensuse',
21ec7304
TL
39 arch => 'archlinux',
40 sles => 'opensuse',
934a30b0
TL
41};
42
bdd4194c 43my $autodetect_type = sub {
153747ff
WB
44 my ($self, $rootdir, $os_release) = @_;
45
46 if (my $id = $os_release->{ID}) {
934a30b0
TL
47 return $id if $plugins->{$id};
48 return $plugin_alias->{$id} if $plugin_alias->{$id};
5c81c821 49 warn "unknown ID '$id' in /etc/os-release file, trying fallback detection\n";
153747ff
WB
50 }
51
52 # fallback compatibility checks
a8e58e9c
DM
53
54 my $lsb_fn = "$rootdir/etc/lsb-release";
55 if (-f $lsb_fn) {
56 my $data = PVE::Tools::file_get_contents($lsb_fn);
57 if ($data =~ m/^DISTRIB_ID=Ubuntu$/im) {
58 return 'ubuntu';
59 }
75c7928e
DM
60 }
61
62 if (-f "$rootdir/etc/debian_version") {
bdd4194c 63 return "debian";
151c5b73
TL
64 } elsif (-f "$rootdir/etc/devuan_version") {
65 return "devuan";
fa7cb12b
WB
66 } elsif (-f "$rootdir/etc/SuSE-brand" || -f "$rootdir/etc/SuSE-release") {
67 return "opensuse";
b0143ab1
WB
68 } elsif (-f "$rootdir/etc/fedora-release") {
69 return "fedora";
81a6ec3f
WB
70 } elsif (-f "$rootdir/etc/centos-release" || -f "$rootdir/etc/redhat-release") {
71 return "centos";
c1d32b55
WB
72 } elsif (-f "$rootdir/etc/arch-release") {
73 return "archlinux";
a8700492
JV
74 } elsif (-f "$rootdir/etc/alpine-release") {
75 return "alpine";
ed027b58
WB
76 } elsif (-f "$rootdir/etc/gentoo-release") {
77 return "gentoo";
5f94d5e5
AE
78 } elsif (-f "$rootdir/etc/os-release") {
79 die "unable to detect OS distribution\n";
80 } else {
81 warn "/etc/os-release file not found and autodetection failed, falling back to 'unmanaged'\n";
82 return "unmanaged";
bdd4194c 83 }
bdd4194c
DM
84};
85
1c7f4f65 86sub new {
5b4657d0 87 my ($class, $conf, $rootdir, $type) = @_;
1c7f4f65 88
5b4657d0
DM
89 die "no root directory\n" if !$rootdir || $rootdir eq '/';
90
250b158c 91 my $self = bless { conf => $conf, rootdir => $rootdir}, $class;
1c7f4f65 92
153747ff
WB
93 my $os_release = $self->get_ct_os_release();
94
238b7e3e 95 if ($conf->{ostype} && $conf->{ostype} eq 'unmanaged') {
7db46e47 96 $type = 'unmanaged';
238b7e3e 97 } elsif (!defined($type)) {
bdd4194c 98 # try to autodetect type
153747ff 99 $type = &$autodetect_type($self, $rootdir, $os_release);
238b7e3e
DM
100 my $expected_type = $conf->{ostype} || $type;
101
f9955be7
TL
102 if ($type ne $expected_type) {
103 warn "WARNING: /etc not present in CT, is the rootfs mounted?\n"
104 if ! -e "$rootdir/etc";
105 warn "got unexpected ostype ($type != $expected_type)\n"
106 }
bdd4194c 107 }
238b7e3e 108
1db602d0 109 my $plugin_class = $plugins->{$type} || die "no such OS type '$type'\n";
1c7f4f65 110
153747ff 111 my $plugin = $plugin_class->new($conf, $rootdir, $os_release);
23d928a1 112 $self->{plugin} = $plugin;
f08b2779 113 $self->{in_chroot} = 0;
23d928a1
WB
114
115 # Cache some host files we need access to:
116 $plugin->{host_resolv_conf} = PVE::INotify::read_file('resolvconf');
6007e81b 117 $plugin->{host_timezone} = PVE::INotify::read_file('timezone');
db35d9f3
FE
118
119 abs_path('/etc/localtime') =~ m|^(/.+)| or die "invalid /etc/localtime\n"; # untaint
120 $plugin->{host_localtime} = $1;
c6a605f9
WB
121
122 # pass on user namespace information:
123 my ($id_map, $rootuid, $rootgid) = PVE::LXC::parse_id_maps($conf);
124 if (@$id_map) {
125 $plugin->{id_map} = $id_map;
126 $plugin->{rootuid} = $rootuid;
127 $plugin->{rootgid} = $rootgid;
128 }
1db602d0 129
1c7f4f65
DM
130 return $self;
131}
132
9ba0e0b5 133# Forks into a chroot and executes $sub
f08b2779
WB
134sub protected_call {
135 my ($self, $sub) = @_;
136
137 # avoid recursion:
138 return $sub->() if $self->{in_chroot};
139
9ba0e0b5
WB
140 pipe(my $res_in, my $res_out) or die "pipe failed: $!\n";
141
f08b2779
WB
142 my $child = fork();
143 die "fork failed: $!\n" if !defined($child);
144
145 if (!$child) {
9ba0e0b5 146 close($res_in);
f08b2779
WB
147 # avoid recursive forks
148 $self->{in_chroot} = 1;
f08b2779 149 eval {
229ab7fa 150 my $rootdir = $self->{rootdir};
f08b2779
WB
151 chroot($rootdir) or die "failed to change root to: $rootdir: $!\n";
152 chdir('/') or die "failed to change to root directory\n";
9ba0e0b5
WB
153 my $res = $sub->();
154 if (defined($res)) {
155 print {$res_out} "$res";
156 $res_out->flush();
157 }
f08b2779
WB
158 };
159 if (my $err = $@) {
797e12e8 160 warn $err;
f08b2779
WB
161 POSIX::_exit(1);
162 }
163 POSIX::_exit(0);
164 }
9ba0e0b5
WB
165 close($res_out);
166 my $result = do { local $/ = undef; <$res_in> };
f08b2779 167 while (waitpid($child, 0) != $child) {}
952de558
WB
168 if ($? != 0) {
169 my $method = (caller(1))[3];
170 die "error in setup task $method\n";
171 }
9ba0e0b5 172 return $result;
f08b2779
WB
173}
174
142444d5
DM
175sub template_fixup {
176 my ($self) = @_;
1db602d0 177 $self->protected_call(sub { $self->{plugin}->template_fixup($self->{conf}) });
142444d5 178}
f8ccd30a 179
1c7f4f65 180sub setup_network {
55fa4e09 181 my ($self) = @_;
1db602d0 182 $self->protected_call(sub { $self->{plugin}->setup_network($self->{conf}) });
1c7f4f65
DM
183}
184
185sub set_hostname {
186 my ($self) = @_;
1db602d0 187 $self->protected_call(sub { $self->{plugin}->set_hostname($self->{conf}) });
1c7f4f65
DM
188}
189
c325b32f
DM
190sub set_dns {
191 my ($self) = @_;
1db602d0 192 $self->protected_call(sub { $self->{plugin}->set_dns($self->{conf}) });
c325b32f
DM
193}
194
e6e308ae
OB
195sub set_timezone {
196 my ($self) = @_;
1db602d0 197 $self->protected_call(sub { $self->{plugin}->set_timezone($self->{conf}) });
e6e308ae
OB
198}
199
d66768a2
DM
200sub setup_init {
201 my ($self) = @_;
1db602d0 202 $self->protected_call(sub { $self->{plugin}->setup_init($self->{conf}) });
d66768a2
DM
203}
204
168d6b07
DM
205sub set_user_password {
206 my ($self, $user, $pw) = @_;
1db602d0 207 $self->protected_call(sub { $self->{plugin}->set_user_password($self->{conf}, $user, $pw) });
1c7f4f65
DM
208}
209
6d9173c3
TL
210my sub generate_ssh_key { # create temporary key in hosts' /run, then read and unlink
211 my ($type, $comment) = @_;
212
213 my $key_id = '';
214 my $keygen_outfunc = sub {
215 if ($_[0] =~ m/^((?:[0-9a-f]{2}:)+[0-9a-f]{2}|SHA256:[0-9a-z+\/]{43})\s+\Q$comment\E$/i) {
216 $key_id = $_[0];
217 }
218 };
219 my $file = "/run/pve/.tmp$$.$type";
220 PVE::Tools::run_command(
221 ['ssh-keygen', '-f', $file, '-t', $type, '-N', '', '-E', 'sha256', '-C', $comment],
222 outfunc => $keygen_outfunc,
223 );
224 my ($private) = (PVE::Tools::file_get_contents($file) =~ /^(.*)$/sg); # untaint
225 my ($public) = (PVE::Tools::file_get_contents("$file.pub") =~ /^(.*)$/sg); # untaint
226 unlink $file, "$file.pub";
227
228 return ($key_id, $private, $public);
229}
230
7ee31468
DM
231sub rewrite_ssh_host_keys {
232 my ($self) = @_;
233
797e12e8 234 my $plugin = $self->{plugin};
797e12e8 235
bc5b7618 236 my $keynames = $plugin->ssh_host_key_types_to_generate();
797e12e8 237
bc5b7618 238 return if ! -d "$self->{rootdir}/etc/ssh" || !$keynames || !scalar(keys $keynames->%*);
797e12e8 239
9cc77d55 240 my $hostname = $self->{conf}->{hostname} || 'localhost';
797e12e8
WB
241 $hostname =~ s/\..*$//;
242
6d9173c3
TL
243 my $keyfiles = [];
244 for my $keytype (keys $keynames->%*) {
797e12e8 245 my $basename = $keynames->{$keytype};
797e12e8 246 print "Creating SSH host key '$basename' - this may take some time ...\n";
6d9173c3
TL
247 my ($id, $private, $public) = generate_ssh_key($keytype, "root\@$hostname");
248 print "done: $id\n";
249
250 push $keyfiles->@*, ["/etc/ssh/$basename", $private, 0600], ["/etc/ssh/$basename.pub", $public, 0644];
797e12e8
WB
251 }
252
6d9173c3
TL
253 $self->protected_call(sub { # write them now all to the CTs rootfs at once
254 for my $file ($keyfiles->@*) {
255 $plugin->ct_file_set_contents($file->@*);
797e12e8 256 }
30e7205e 257 });
1db602d0 258}
7ee31468 259
24f37c3d
TL
260sub pre_start_hook {
261 my ($self) = @_;
262
f3aedcdb 263 $self->protected_call(sub { $self->{plugin}->pre_start_hook($self->{conf}) });
d66768a2
DM
264}
265
64d4c144
OB
266sub post_clone_hook {
267 my ($self, $conf) = @_;
268
1db602d0 269 $self->protected_call(sub { $self->{plugin}->post_clone_hook($conf) });
64d4c144
OB
270}
271
d66768a2 272sub post_create_hook {
f36ce482 273 my ($self, $root_password, $ssh_keys) = @_;
1c7f4f65 274
1db602d0 275 $self->protected_call(sub {
f36ce482 276 $self->{plugin}->post_create_hook($self->{conf}, $root_password, $ssh_keys);
1db602d0 277 });
797e12e8 278 $self->rewrite_ssh_host_keys();
1c7f4f65
DM
279}
280
42216b6b
TL
281sub unified_cgroupv2_support {
282 my ($self) = @_;
283
f8ccd30a 284 return $self->protected_call(sub { $self->{plugin}->unified_cgroupv2_support() });
42216b6b
TL
285}
286
153747ff
WB
287# os-release(5):
288# (...) a newline-separated list of environment-like shell-compatible
289# variable assignments. (...) beyond mere variable assignments, no shell
290# features are supported (this means variable expansion is explicitly not
291# supported) (...). Variable assignment values must be enclosed in double or
292# single quotes *if* they include spaces, semicolons or other special
293# characters outside of A-Z, a-z, 0-9. Shell special characters ("$", quotes,
294# backslash, backtick) must be escaped with backslashes (...). All strings
295# should be in UTF-8 format, and non-printable characters should not be used.
296# It is not supported to concatenate multiple individually quoted strings.
297# Lines beginning with "#" shall be ignored as comments.
298my $parse_os_release = sub {
299 my ($data) = @_;
300 my $variables = {};
301 while (defined($data) && $data =~ /^(.+)$/gm) {
302 next if $1 !~ /^\s*([a-zA-Z_][a-zA-Z0-9_]*)=(.*)$/;
303 my ($var, $content) = ($1, $2);
304 chomp $content;
305
306 if ($content =~ /^'([^']*)'/) {
307 $variables->{$var} = $1;
308 } elsif ($content =~ /^"((?:[^"\\]|\\.)*)"/) {
309 my $s = $1;
310 $s =~ s/(\\["'`nt\$\\])/"\"$1\""/eeg;
311 $variables->{$var} = $s;
312 } elsif ($content =~ /^([A-Za-z0-9]*)/) {
313 $variables->{$var} = $1;
314 }
315 }
316 return $variables;
317};
318
319sub get_ct_os_release {
320 my ($self) = @_;
321
1db602d0 322 my $data = $self->protected_call(sub {
153747ff
WB
323 if (-f '/etc/os-release') {
324 return PVE::Tools::file_get_contents('/etc/os-release');
325 } elsif (-f '/usr/lib/os-release') {
326 return PVE::Tools::file_get_contents('/usr/lib/os-release');
327 }
328 return undef;
1db602d0 329 });
153747ff
WB
330
331 return &$parse_os_release($data);
332}
333
1c7f4f65 3341;