]> git.proxmox.com Git - pve-container.git/blame - src/PVE/LXC/Setup.pm
config: limit description/comment length to 8 KiB
[pve-container.git] / src / PVE / LXC / Setup.pm
CommitLineData
7af97ad5 1package PVE::LXC::Setup;
1c7f4f65
DM
2
3use strict;
4use warnings;
f08b2779 5use POSIX;
a8e58e9c 6use PVE::Tools;
1c7f4f65 7
e6e308ae
OB
8use Cwd 'abs_path';
9
7af97ad5
DM
10use PVE::LXC::Setup::Debian;
11use PVE::LXC::Setup::Ubuntu;
81a6ec3f 12use PVE::LXC::Setup::CentOS;
b0143ab1 13use PVE::LXC::Setup::Fedora;
fa7cb12b 14use PVE::LXC::Setup::SUSE;
7af97ad5 15use PVE::LXC::Setup::ArchLinux;
a8700492 16use PVE::LXC::Setup::Alpine;
ed027b58 17use PVE::LXC::Setup::Gentoo;
151c5b73 18use PVE::LXC::Setup::Devuan;
1c7f4f65
DM
19
20my $plugins = {
7af97ad5 21 debian => 'PVE::LXC::Setup::Debian',
151c5b73 22 devuan => 'PVE::LXC::Setup::Devuan',
7af97ad5 23 ubuntu => 'PVE::LXC::Setup::Ubuntu',
81a6ec3f 24 centos => 'PVE::LXC::Setup::CentOS',
b0143ab1 25 fedora => 'PVE::LXC::Setup::Fedora',
fa7cb12b 26 opensuse => 'PVE::LXC::Setup::SUSE',
7af97ad5 27 archlinux => 'PVE::LXC::Setup::ArchLinux',
a8700492 28 alpine => 'PVE::LXC::Setup::Alpine',
ed027b58 29 gentoo => 'PVE::LXC::Setup::Gentoo',
1c7f4f65
DM
30};
31
934a30b0
TL
32# a map to allow supporting related distro flavours
33my $plugin_alias = {
34 arch => 'archlinux',
35 sles => 'opensuse',
47447841 36 'opensuse-leap' => 'opensuse',
005e72bd 37 'opensuse-tumbleweed' => 'opensuse',
934a30b0
TL
38};
39
bdd4194c 40my $autodetect_type = sub {
153747ff
WB
41 my ($self, $rootdir, $os_release) = @_;
42
43 if (my $id = $os_release->{ID}) {
934a30b0
TL
44 return $id if $plugins->{$id};
45 return $plugin_alias->{$id} if $plugin_alias->{$id};
5c81c821 46 warn "unknown ID '$id' in /etc/os-release file, trying fallback detection\n";
153747ff
WB
47 }
48
49 # fallback compatibility checks
a8e58e9c
DM
50
51 my $lsb_fn = "$rootdir/etc/lsb-release";
52 if (-f $lsb_fn) {
53 my $data = PVE::Tools::file_get_contents($lsb_fn);
54 if ($data =~ m/^DISTRIB_ID=Ubuntu$/im) {
55 return 'ubuntu';
56 }
75c7928e
DM
57 }
58
59 if (-f "$rootdir/etc/debian_version") {
bdd4194c 60 return "debian";
151c5b73
TL
61 } elsif (-f "$rootdir/etc/devuan_version") {
62 return "devuan";
fa7cb12b
WB
63 } elsif (-f "$rootdir/etc/SuSE-brand" || -f "$rootdir/etc/SuSE-release") {
64 return "opensuse";
b0143ab1
WB
65 } elsif (-f "$rootdir/etc/fedora-release") {
66 return "fedora";
81a6ec3f
WB
67 } elsif (-f "$rootdir/etc/centos-release" || -f "$rootdir/etc/redhat-release") {
68 return "centos";
c1d32b55
WB
69 } elsif (-f "$rootdir/etc/arch-release") {
70 return "archlinux";
a8700492
JV
71 } elsif (-f "$rootdir/etc/alpine-release") {
72 return "alpine";
ed027b58
WB
73 } elsif (-f "$rootdir/etc/gentoo-release") {
74 return "gentoo";
5f94d5e5
AE
75 } elsif (-f "$rootdir/etc/os-release") {
76 die "unable to detect OS distribution\n";
77 } else {
78 warn "/etc/os-release file not found and autodetection failed, falling back to 'unmanaged'\n";
79 return "unmanaged";
bdd4194c 80 }
bdd4194c
DM
81};
82
1c7f4f65 83sub new {
5b4657d0 84 my ($class, $conf, $rootdir, $type) = @_;
1c7f4f65 85
5b4657d0
DM
86 die "no root directory\n" if !$rootdir || $rootdir eq '/';
87
5b3614f8 88 my $self = bless { conf => $conf, rootdir => $rootdir};
1c7f4f65 89
153747ff
WB
90 my $os_release = $self->get_ct_os_release();
91
238b7e3e
DM
92 if ($conf->{ostype} && $conf->{ostype} eq 'unmanaged') {
93 return $self;
94 } elsif (!defined($type)) {
bdd4194c 95 # try to autodetect type
153747ff 96 $type = &$autodetect_type($self, $rootdir, $os_release);
238b7e3e
DM
97 my $expected_type = $conf->{ostype} || $type;
98
f9955be7
TL
99 if ($type ne $expected_type) {
100 warn "WARNING: /etc not present in CT, is the rootfs mounted?\n"
101 if ! -e "$rootdir/etc";
102 warn "got unexpected ostype ($type != $expected_type)\n"
103 }
bdd4194c 104 }
238b7e3e 105
5f94d5e5
AE
106 if ($type eq 'unmanaged') {
107 $conf->{ostype} = $type;
108 return $self;
109 }
110
633a7bd8 111 my $plugin_class = $plugins->{$type} ||
1c7f4f65
DM
112 "no such OS type '$type'\n";
113
153747ff 114 my $plugin = $plugin_class->new($conf, $rootdir, $os_release);
23d928a1 115 $self->{plugin} = $plugin;
f08b2779 116 $self->{in_chroot} = 0;
23d928a1
WB
117
118 # Cache some host files we need access to:
119 $plugin->{host_resolv_conf} = PVE::INotify::read_file('resolvconf');
e6e308ae 120 $plugin->{host_localtime} = abs_path('/etc/localtime');
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 }
633a7bd8 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) = @_;
177
238b7e3e
DM
178 return if !$self->{plugin}; # unmanaged
179
f08b2779
WB
180 my $code = sub {
181 $self->{plugin}->template_fixup($self->{conf});
182 };
183 $self->protected_call($code);
142444d5
DM
184}
185
1c7f4f65 186sub setup_network {
55fa4e09 187 my ($self) = @_;
1c7f4f65 188
238b7e3e
DM
189 return if !$self->{plugin}; # unmanaged
190
f08b2779
WB
191 my $code = sub {
192 $self->{plugin}->setup_network($self->{conf});
193 };
194 $self->protected_call($code);
1c7f4f65
DM
195}
196
197sub set_hostname {
198 my ($self) = @_;
199
238b7e3e
DM
200 return if !$self->{plugin}; # unmanaged
201
f08b2779
WB
202 my $code = sub {
203 $self->{plugin}->set_hostname($self->{conf});
204 };
205 $self->protected_call($code);
1c7f4f65
DM
206}
207
c325b32f
DM
208sub set_dns {
209 my ($self) = @_;
210
238b7e3e
DM
211 return if !$self->{plugin}; # unmanaged
212
f08b2779
WB
213 my $code = sub {
214 $self->{plugin}->set_dns($self->{conf});
215 };
216 $self->protected_call($code);
c325b32f
DM
217}
218
e6e308ae
OB
219sub set_timezone {
220 my ($self) = @_;
221
222 return if !$self->{plugin}; # unmanaged
223 my $code = sub {
224 $self->{plugin}->set_timezone($self->{conf});
225 };
226 $self->protected_call($code);
227}
228
d66768a2
DM
229sub setup_init {
230 my ($self) = @_;
231
238b7e3e
DM
232 return if !$self->{plugin}; # unmanaged
233
f08b2779
WB
234 my $code = sub {
235 $self->{plugin}->setup_init($self->{conf});
236 };
237 $self->protected_call($code);
d66768a2
DM
238}
239
168d6b07
DM
240sub set_user_password {
241 my ($self, $user, $pw) = @_;
238b7e3e
DM
242
243 return if !$self->{plugin}; # unmanaged
244
f08b2779
WB
245 my $code = sub {
246 $self->{plugin}->set_user_password($self->{conf}, $user, $pw);
247 };
248 $self->protected_call($code);
1c7f4f65
DM
249}
250
7ee31468
DM
251sub rewrite_ssh_host_keys {
252 my ($self) = @_;
253
238b7e3e
DM
254 return if !$self->{plugin}; # unmanaged
255
797e12e8
WB
256 my $conf = $self->{conf};
257 my $plugin = $self->{plugin};
258 my $rootdir = $self->{rootdir};
259
feb7383b 260 return if ! -d "$rootdir/etc/ssh";
797e12e8
WB
261
262 my $keynames = {
797e12e8
WB
263 rsa => 'ssh_host_rsa_key',
264 dsa => 'ssh_host_dsa_key',
265 ecdsa => 'ssh_host_ecdsa_key',
266 ed25519 => 'ssh_host_ed25519_key',
267 };
268
269 my $hostname = $conf->{hostname} || 'localhost';
270 $hostname =~ s/\..*$//;
52a2d3f8 271 my $ssh_comment = "root\@$hostname";
797e12e8 272
52a2d3f8
FG
273 my $keygen_outfunc = sub {
274 my $line = shift;
275
276 print "done: $line\n"
bc77a52b
WB
277 if $line =~ m/^(?:[0-9a-f]{2}:)+[0-9a-f]{2}\s+\Q$ssh_comment\E$/i ||
278 $line =~ m/^SHA256:[0-9a-z+\/]{43}\s+\Q$ssh_comment\E$/i;
52a2d3f8 279 };
797e12e8 280
52a2d3f8 281 # Create temporary keys in /tmp on the host
797e12e8
WB
282 my $keyfiles = {};
283 foreach my $keytype (keys %$keynames) {
284 my $basename = $keynames->{$keytype};
285 my $file = "/tmp/$$.$basename";
286 print "Creating SSH host key '$basename' - this may take some time ...\n";
52a2d3f8 287 my $cmd = ['ssh-keygen', '-f', $file, '-t', $keytype,
bc77a52b 288 '-N', '', '-E', 'sha256', '-C', $ssh_comment];
52a2d3f8 289 PVE::Tools::run_command($cmd, outfunc => $keygen_outfunc);
39db5e47
WB
290 $keyfiles->{"/etc/ssh/$basename"} = [PVE::Tools::file_get_contents($file), 0600];
291 $keyfiles->{"/etc/ssh/$basename.pub"} = [PVE::Tools::file_get_contents("$file.pub"), 0644];
797e12e8
WB
292 unlink $file;
293 unlink "$file.pub";
294 }
295
296 # Write keys out in a protected call
297
f08b2779 298 my $code = sub {
797e12e8 299 foreach my $file (keys %$keyfiles) {
39db5e47 300 $plugin->ct_file_set_contents($file, @{$keyfiles->{$file}});
797e12e8 301 }
f08b2779
WB
302 };
303 $self->protected_call($code);
7ee31468
DM
304}
305
0c59b609
DM
306my $container_emulator_path = {
307 'amd64' => '/usr/bin/qemu-x86_64-static',
308 'arm64' => '/usr/bin/qemu-aarch64-static',
309};
310
d66768a2
DM
311sub pre_start_hook {
312 my ($self) = @_;
313
238b7e3e
DM
314 return if !$self->{plugin}; # unmanaged
315
a0745b47 316 my $host_arch = PVE::Tools::get_host_arch();
0c59b609 317
11e1612f
SR
318 # containers use different architecture names
319 if ($host_arch eq 'x86_64') {
320 $host_arch = 'amd64';
321 } elsif ($host_arch eq 'aarch64') {
322 $host_arch = 'arm64';
323 } else {
324 die "unsupported host architecture '$host_arch'\n";
325 }
326
0c59b609
DM
327 my $container_arch = $self->{conf}->{arch};
328
329 $container_arch = 'amd64' if $container_arch eq 'i386'; # always use 64 bit version
330 $container_arch = 'arm64' if $container_arch eq 'armhf'; # always use 64 bit version
331
332 my $emul;
333 my $emul_data;
334
335 if ($host_arch ne $container_arch) {
336 if ($emul = $container_emulator_path->{$container_arch}) {
337 $emul_data = PVE::Tools::file_get_contents($emul, 10*1024*1024) if -f $emul;
338 }
339 }
340
f08b2779 341 my $code = sub {
0c59b609
DM
342
343 if ($emul && $emul_data) {
344 $self->{plugin}->ct_file_set_contents($emul, $emul_data, 0755);
345 }
346
f08b2779
WB
347 # Create /fastboot to skip run fsck
348 $self->{plugin}->ct_file_set_contents('/fastboot', '');
2988dbbc 349
f08b2779
WB
350 $self->{plugin}->pre_start_hook($self->{conf});
351 };
352 $self->protected_call($code);
d66768a2
DM
353}
354
355sub post_create_hook {
f36ce482 356 my ($self, $root_password, $ssh_keys) = @_;
1c7f4f65 357
238b7e3e
DM
358 return if !$self->{plugin}; # unmanaged
359
f08b2779 360 my $code = sub {
f36ce482 361 $self->{plugin}->post_create_hook($self->{conf}, $root_password, $ssh_keys);
f08b2779
WB
362 };
363 $self->protected_call($code);
797e12e8 364 $self->rewrite_ssh_host_keys();
1c7f4f65
DM
365}
366
153747ff
WB
367# os-release(5):
368# (...) a newline-separated list of environment-like shell-compatible
369# variable assignments. (...) beyond mere variable assignments, no shell
370# features are supported (this means variable expansion is explicitly not
371# supported) (...). Variable assignment values must be enclosed in double or
372# single quotes *if* they include spaces, semicolons or other special
373# characters outside of A-Z, a-z, 0-9. Shell special characters ("$", quotes,
374# backslash, backtick) must be escaped with backslashes (...). All strings
375# should be in UTF-8 format, and non-printable characters should not be used.
376# It is not supported to concatenate multiple individually quoted strings.
377# Lines beginning with "#" shall be ignored as comments.
378my $parse_os_release = sub {
379 my ($data) = @_;
380 my $variables = {};
381 while (defined($data) && $data =~ /^(.+)$/gm) {
382 next if $1 !~ /^\s*([a-zA-Z_][a-zA-Z0-9_]*)=(.*)$/;
383 my ($var, $content) = ($1, $2);
384 chomp $content;
385
386 if ($content =~ /^'([^']*)'/) {
387 $variables->{$var} = $1;
388 } elsif ($content =~ /^"((?:[^"\\]|\\.)*)"/) {
389 my $s = $1;
390 $s =~ s/(\\["'`nt\$\\])/"\"$1\""/eeg;
391 $variables->{$var} = $s;
392 } elsif ($content =~ /^([A-Za-z0-9]*)/) {
393 $variables->{$var} = $1;
394 }
395 }
396 return $variables;
397};
398
399sub get_ct_os_release {
400 my ($self) = @_;
401
402 my $code = sub {
403 if (-f '/etc/os-release') {
404 return PVE::Tools::file_get_contents('/etc/os-release');
405 } elsif (-f '/usr/lib/os-release') {
406 return PVE::Tools::file_get_contents('/usr/lib/os-release');
407 }
408 return undef;
409 };
410
411 my $data = $self->protected_call($code);
412
413 return &$parse_os_release($data);
414}
415
1c7f4f65 4161;