]>
Commit | Line | Data |
---|---|---|
7af97ad5 | 1 | package PVE::LXC::Setup; |
1c7f4f65 DM |
2 | |
3 | use strict; | |
4 | use warnings; | |
f08b2779 | 5 | use POSIX; |
a8e58e9c | 6 | use PVE::Tools; |
1c7f4f65 | 7 | |
e6e308ae OB |
8 | use Cwd 'abs_path'; |
9 | ||
7af97ad5 DM |
10 | use PVE::LXC::Setup::Debian; |
11 | use PVE::LXC::Setup::Ubuntu; | |
81a6ec3f | 12 | use PVE::LXC::Setup::CentOS; |
b0143ab1 | 13 | use PVE::LXC::Setup::Fedora; |
fa7cb12b | 14 | use PVE::LXC::Setup::SUSE; |
7af97ad5 | 15 | use PVE::LXC::Setup::ArchLinux; |
a8700492 | 16 | use PVE::LXC::Setup::Alpine; |
ed027b58 | 17 | use PVE::LXC::Setup::Gentoo; |
151c5b73 | 18 | use PVE::LXC::Setup::Devuan; |
1c7f4f65 DM |
19 | |
20 | my $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 |
33 | my $plugin_alias = { | |
34 | arch => 'archlinux', | |
35 | sles => 'opensuse', | |
47447841 | 36 | 'opensuse-leap' => 'opensuse', |
005e72bd | 37 | 'opensuse-tumbleweed' => 'opensuse', |
934a30b0 TL |
38 | }; |
39 | ||
bdd4194c | 40 | my $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 | 83 | sub new { |
5b4657d0 | 84 | my ($class, $conf, $rootdir, $type) = @_; |
1c7f4f65 | 85 | |
5b4657d0 DM |
86 | die "no root directory\n" if !$rootdir || $rootdir eq '/'; |
87 | ||
250b158c | 88 | my $self = bless { conf => $conf, rootdir => $rootdir}, $class; |
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 | ||
1db602d0 | 111 | my $plugin_class = $plugins->{$type} || die "no such OS type '$type'\n"; |
1c7f4f65 | 112 | |
153747ff | 113 | my $plugin = $plugin_class->new($conf, $rootdir, $os_release); |
23d928a1 | 114 | $self->{plugin} = $plugin; |
f08b2779 | 115 | $self->{in_chroot} = 0; |
23d928a1 WB |
116 | |
117 | # Cache some host files we need access to: | |
118 | $plugin->{host_resolv_conf} = PVE::INotify::read_file('resolvconf'); | |
e6e308ae | 119 | $plugin->{host_localtime} = abs_path('/etc/localtime'); |
c6a605f9 WB |
120 | |
121 | # pass on user namespace information: | |
122 | my ($id_map, $rootuid, $rootgid) = PVE::LXC::parse_id_maps($conf); | |
123 | if (@$id_map) { | |
124 | $plugin->{id_map} = $id_map; | |
125 | $plugin->{rootuid} = $rootuid; | |
126 | $plugin->{rootgid} = $rootgid; | |
127 | } | |
1db602d0 | 128 | |
1c7f4f65 DM |
129 | return $self; |
130 | } | |
131 | ||
9ba0e0b5 | 132 | # Forks into a chroot and executes $sub |
f08b2779 WB |
133 | sub protected_call { |
134 | my ($self, $sub) = @_; | |
135 | ||
136 | # avoid recursion: | |
137 | return $sub->() if $self->{in_chroot}; | |
138 | ||
9ba0e0b5 WB |
139 | pipe(my $res_in, my $res_out) or die "pipe failed: $!\n"; |
140 | ||
f08b2779 WB |
141 | my $child = fork(); |
142 | die "fork failed: $!\n" if !defined($child); | |
143 | ||
144 | if (!$child) { | |
9ba0e0b5 | 145 | close($res_in); |
f08b2779 WB |
146 | # avoid recursive forks |
147 | $self->{in_chroot} = 1; | |
f08b2779 | 148 | eval { |
229ab7fa | 149 | my $rootdir = $self->{rootdir}; |
f08b2779 WB |
150 | chroot($rootdir) or die "failed to change root to: $rootdir: $!\n"; |
151 | chdir('/') or die "failed to change to root directory\n"; | |
9ba0e0b5 WB |
152 | my $res = $sub->(); |
153 | if (defined($res)) { | |
154 | print {$res_out} "$res"; | |
155 | $res_out->flush(); | |
156 | } | |
f08b2779 WB |
157 | }; |
158 | if (my $err = $@) { | |
797e12e8 | 159 | warn $err; |
f08b2779 WB |
160 | POSIX::_exit(1); |
161 | } | |
162 | POSIX::_exit(0); | |
163 | } | |
9ba0e0b5 WB |
164 | close($res_out); |
165 | my $result = do { local $/ = undef; <$res_in> }; | |
f08b2779 | 166 | while (waitpid($child, 0) != $child) {} |
952de558 WB |
167 | if ($? != 0) { |
168 | my $method = (caller(1))[3]; | |
169 | die "error in setup task $method\n"; | |
170 | } | |
9ba0e0b5 | 171 | return $result; |
f08b2779 WB |
172 | } |
173 | ||
142444d5 DM |
174 | sub template_fixup { |
175 | my ($self) = @_; | |
176 | ||
238b7e3e DM |
177 | return if !$self->{plugin}; # unmanaged |
178 | ||
1db602d0 | 179 | $self->protected_call(sub { $self->{plugin}->template_fixup($self->{conf}) }); |
142444d5 DM |
180 | } |
181 | ||
1c7f4f65 | 182 | sub setup_network { |
55fa4e09 | 183 | my ($self) = @_; |
1c7f4f65 | 184 | |
238b7e3e DM |
185 | return if !$self->{plugin}; # unmanaged |
186 | ||
1db602d0 | 187 | $self->protected_call(sub { $self->{plugin}->setup_network($self->{conf}) }); |
1c7f4f65 DM |
188 | } |
189 | ||
190 | sub set_hostname { | |
191 | my ($self) = @_; | |
192 | ||
238b7e3e DM |
193 | return if !$self->{plugin}; # unmanaged |
194 | ||
1db602d0 | 195 | $self->protected_call(sub { $self->{plugin}->set_hostname($self->{conf}) }); |
1c7f4f65 DM |
196 | } |
197 | ||
c325b32f DM |
198 | sub set_dns { |
199 | my ($self) = @_; | |
200 | ||
238b7e3e DM |
201 | return if !$self->{plugin}; # unmanaged |
202 | ||
1db602d0 | 203 | $self->protected_call(sub { $self->{plugin}->set_dns($self->{conf}) }); |
c325b32f DM |
204 | } |
205 | ||
e6e308ae OB |
206 | sub set_timezone { |
207 | my ($self) = @_; | |
208 | ||
209 | return if !$self->{plugin}; # unmanaged | |
1db602d0 TL |
210 | |
211 | $self->protected_call(sub { $self->{plugin}->set_timezone($self->{conf}) }); | |
e6e308ae OB |
212 | } |
213 | ||
d66768a2 DM |
214 | sub setup_init { |
215 | my ($self) = @_; | |
216 | ||
238b7e3e DM |
217 | return if !$self->{plugin}; # unmanaged |
218 | ||
1db602d0 | 219 | $self->protected_call(sub { $self->{plugin}->setup_init($self->{conf}) }); |
d66768a2 DM |
220 | } |
221 | ||
168d6b07 DM |
222 | sub set_user_password { |
223 | my ($self, $user, $pw) = @_; | |
238b7e3e DM |
224 | |
225 | return if !$self->{plugin}; # unmanaged | |
226 | ||
1db602d0 | 227 | $self->protected_call(sub { $self->{plugin}->set_user_password($self->{conf}, $user, $pw) }); |
1c7f4f65 DM |
228 | } |
229 | ||
7ee31468 DM |
230 | sub rewrite_ssh_host_keys { |
231 | my ($self) = @_; | |
232 | ||
238b7e3e DM |
233 | return if !$self->{plugin}; # unmanaged |
234 | ||
797e12e8 WB |
235 | my $conf = $self->{conf}; |
236 | my $plugin = $self->{plugin}; | |
237 | my $rootdir = $self->{rootdir}; | |
238 | ||
feb7383b | 239 | return if ! -d "$rootdir/etc/ssh"; |
797e12e8 WB |
240 | |
241 | my $keynames = { | |
797e12e8 WB |
242 | rsa => 'ssh_host_rsa_key', |
243 | dsa => 'ssh_host_dsa_key', | |
30e7205e | 244 | ecdsa => 'ssh_host_ecdsa_key', |
797e12e8 WB |
245 | ed25519 => 'ssh_host_ed25519_key', |
246 | }; | |
247 | ||
248 | my $hostname = $conf->{hostname} || 'localhost'; | |
249 | $hostname =~ s/\..*$//; | |
52a2d3f8 | 250 | my $ssh_comment = "root\@$hostname"; |
797e12e8 | 251 | |
52a2d3f8 FG |
252 | my $keygen_outfunc = sub { |
253 | my $line = shift; | |
30e7205e TL |
254 | if ($line =~ m/^(?:[0-9a-f]{2}:)+[0-9a-f]{2}\s+\Q$ssh_comment\E$/i |
255 | || $line =~ m/^SHA256:[0-9a-z+\/]{43}\s+\Q$ssh_comment\E$/i | |
256 | ) { | |
257 | print "done: $line\n" | |
258 | } | |
52a2d3f8 | 259 | }; |
797e12e8 | 260 | |
52a2d3f8 | 261 | # Create temporary keys in /tmp on the host |
797e12e8 WB |
262 | my $keyfiles = {}; |
263 | foreach my $keytype (keys %$keynames) { | |
264 | my $basename = $keynames->{$keytype}; | |
265 | my $file = "/tmp/$$.$basename"; | |
266 | print "Creating SSH host key '$basename' - this may take some time ...\n"; | |
30e7205e TL |
267 | PVE::Tools::run_command( |
268 | ['ssh-keygen', '-f', $file, '-t', $keytype, '-N', '', '-E', 'sha256', '-C', $ssh_comment], | |
269 | outfunc => $keygen_outfunc, | |
270 | ); | |
39db5e47 WB |
271 | $keyfiles->{"/etc/ssh/$basename"} = [PVE::Tools::file_get_contents($file), 0600]; |
272 | $keyfiles->{"/etc/ssh/$basename.pub"} = [PVE::Tools::file_get_contents("$file.pub"), 0644]; | |
797e12e8 WB |
273 | unlink $file; |
274 | unlink "$file.pub"; | |
275 | } | |
276 | ||
277 | # Write keys out in a protected call | |
30e7205e | 278 | $self->protected_call(sub { |
797e12e8 | 279 | foreach my $file (keys %$keyfiles) { |
39db5e47 | 280 | $plugin->ct_file_set_contents($file, @{$keyfiles->{$file}}); |
797e12e8 | 281 | } |
30e7205e | 282 | }); |
1db602d0 | 283 | } |
7ee31468 | 284 | |
0c59b609 DM |
285 | my $container_emulator_path = { |
286 | 'amd64' => '/usr/bin/qemu-x86_64-static', | |
287 | 'arm64' => '/usr/bin/qemu-aarch64-static', | |
288 | }; | |
289 | ||
d66768a2 DM |
290 | sub pre_start_hook { |
291 | my ($self) = @_; | |
292 | ||
238b7e3e DM |
293 | return if !$self->{plugin}; # unmanaged |
294 | ||
a0745b47 | 295 | my $host_arch = PVE::Tools::get_host_arch(); |
0c59b609 | 296 | |
11e1612f SR |
297 | # containers use different architecture names |
298 | if ($host_arch eq 'x86_64') { | |
299 | $host_arch = 'amd64'; | |
300 | } elsif ($host_arch eq 'aarch64') { | |
301 | $host_arch = 'arm64'; | |
302 | } else { | |
303 | die "unsupported host architecture '$host_arch'\n"; | |
304 | } | |
305 | ||
0c59b609 DM |
306 | my $container_arch = $self->{conf}->{arch}; |
307 | ||
308 | $container_arch = 'amd64' if $container_arch eq 'i386'; # always use 64 bit version | |
309 | $container_arch = 'arm64' if $container_arch eq 'armhf'; # always use 64 bit version | |
310 | ||
311 | my $emul; | |
312 | my $emul_data; | |
313 | ||
314 | if ($host_arch ne $container_arch) { | |
315 | if ($emul = $container_emulator_path->{$container_arch}) { | |
316 | $emul_data = PVE::Tools::file_get_contents($emul, 10*1024*1024) if -f $emul; | |
317 | } | |
318 | } | |
319 | ||
f08b2779 | 320 | my $code = sub { |
0c59b609 DM |
321 | if ($emul && $emul_data) { |
322 | $self->{plugin}->ct_file_set_contents($emul, $emul_data, 0755); | |
323 | } | |
f08b2779 WB |
324 | # Create /fastboot to skip run fsck |
325 | $self->{plugin}->ct_file_set_contents('/fastboot', ''); | |
2988dbbc | 326 | |
f08b2779 WB |
327 | $self->{plugin}->pre_start_hook($self->{conf}); |
328 | }; | |
329 | $self->protected_call($code); | |
d66768a2 DM |
330 | } |
331 | ||
64d4c144 OB |
332 | sub post_clone_hook { |
333 | my ($self, $conf) = @_; | |
334 | ||
1db602d0 | 335 | $self->protected_call(sub { $self->{plugin}->post_clone_hook($conf) }); |
64d4c144 OB |
336 | } |
337 | ||
d66768a2 | 338 | sub post_create_hook { |
f36ce482 | 339 | my ($self, $root_password, $ssh_keys) = @_; |
1c7f4f65 | 340 | |
238b7e3e DM |
341 | return if !$self->{plugin}; # unmanaged |
342 | ||
1db602d0 | 343 | $self->protected_call(sub { |
f36ce482 | 344 | $self->{plugin}->post_create_hook($self->{conf}, $root_password, $ssh_keys); |
1db602d0 | 345 | }); |
797e12e8 | 346 | $self->rewrite_ssh_host_keys(); |
1c7f4f65 DM |
347 | } |
348 | ||
153747ff WB |
349 | # os-release(5): |
350 | # (...) a newline-separated list of environment-like shell-compatible | |
351 | # variable assignments. (...) beyond mere variable assignments, no shell | |
352 | # features are supported (this means variable expansion is explicitly not | |
353 | # supported) (...). Variable assignment values must be enclosed in double or | |
354 | # single quotes *if* they include spaces, semicolons or other special | |
355 | # characters outside of A-Z, a-z, 0-9. Shell special characters ("$", quotes, | |
356 | # backslash, backtick) must be escaped with backslashes (...). All strings | |
357 | # should be in UTF-8 format, and non-printable characters should not be used. | |
358 | # It is not supported to concatenate multiple individually quoted strings. | |
359 | # Lines beginning with "#" shall be ignored as comments. | |
360 | my $parse_os_release = sub { | |
361 | my ($data) = @_; | |
362 | my $variables = {}; | |
363 | while (defined($data) && $data =~ /^(.+)$/gm) { | |
364 | next if $1 !~ /^\s*([a-zA-Z_][a-zA-Z0-9_]*)=(.*)$/; | |
365 | my ($var, $content) = ($1, $2); | |
366 | chomp $content; | |
367 | ||
368 | if ($content =~ /^'([^']*)'/) { | |
369 | $variables->{$var} = $1; | |
370 | } elsif ($content =~ /^"((?:[^"\\]|\\.)*)"/) { | |
371 | my $s = $1; | |
372 | $s =~ s/(\\["'`nt\$\\])/"\"$1\""/eeg; | |
373 | $variables->{$var} = $s; | |
374 | } elsif ($content =~ /^([A-Za-z0-9]*)/) { | |
375 | $variables->{$var} = $1; | |
376 | } | |
377 | } | |
378 | return $variables; | |
379 | }; | |
380 | ||
381 | sub get_ct_os_release { | |
382 | my ($self) = @_; | |
383 | ||
1db602d0 | 384 | my $data = $self->protected_call(sub { |
153747ff WB |
385 | if (-f '/etc/os-release') { |
386 | return PVE::Tools::file_get_contents('/etc/os-release'); | |
387 | } elsif (-f '/usr/lib/os-release') { | |
388 | return PVE::Tools::file_get_contents('/usr/lib/os-release'); | |
389 | } | |
390 | return undef; | |
1db602d0 | 391 | }); |
153747ff WB |
392 | |
393 | return &$parse_os_release($data); | |
394 | } | |
395 | ||
f7073b99 SI |
396 | sub unified_cgroupv2_support { |
397 | my ($self) = @_; | |
398 | ||
1db602d0 | 399 | $self->protected_call(sub { $self->{plugin}->unified_cgroupv2_support() }); |
f7073b99 SI |
400 | } |
401 | ||
1c7f4f65 | 402 | 1; |