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