]> git.proxmox.com Git - pve-container.git/blame - src/PVE/LXC/Setup.pm
setup: add no-op plugin for unmanaged CTs
[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');
e6e308ae 117 $plugin->{host_localtime} = abs_path('/etc/localtime');
c6a605f9
WB
118
119 # pass on user namespace information:
120 my ($id_map, $rootuid, $rootgid) = PVE::LXC::parse_id_maps($conf);
121 if (@$id_map) {
122 $plugin->{id_map} = $id_map;
123 $plugin->{rootuid} = $rootuid;
124 $plugin->{rootgid} = $rootgid;
125 }
1db602d0 126
1c7f4f65
DM
127 return $self;
128}
129
9ba0e0b5 130# Forks into a chroot and executes $sub
f08b2779
WB
131sub protected_call {
132 my ($self, $sub) = @_;
133
134 # avoid recursion:
135 return $sub->() if $self->{in_chroot};
136
9ba0e0b5
WB
137 pipe(my $res_in, my $res_out) or die "pipe failed: $!\n";
138
f08b2779
WB
139 my $child = fork();
140 die "fork failed: $!\n" if !defined($child);
141
142 if (!$child) {
9ba0e0b5 143 close($res_in);
f08b2779
WB
144 # avoid recursive forks
145 $self->{in_chroot} = 1;
f08b2779 146 eval {
229ab7fa 147 my $rootdir = $self->{rootdir};
f08b2779
WB
148 chroot($rootdir) or die "failed to change root to: $rootdir: $!\n";
149 chdir('/') or die "failed to change to root directory\n";
9ba0e0b5
WB
150 my $res = $sub->();
151 if (defined($res)) {
152 print {$res_out} "$res";
153 $res_out->flush();
154 }
f08b2779
WB
155 };
156 if (my $err = $@) {
797e12e8 157 warn $err;
f08b2779
WB
158 POSIX::_exit(1);
159 }
160 POSIX::_exit(0);
161 }
9ba0e0b5
WB
162 close($res_out);
163 my $result = do { local $/ = undef; <$res_in> };
f08b2779 164 while (waitpid($child, 0) != $child) {}
952de558
WB
165 if ($? != 0) {
166 my $method = (caller(1))[3];
167 die "error in setup task $method\n";
168 }
9ba0e0b5 169 return $result;
f08b2779
WB
170}
171
142444d5
DM
172sub template_fixup {
173 my ($self) = @_;
1db602d0 174 $self->protected_call(sub { $self->{plugin}->template_fixup($self->{conf}) });
142444d5
DM
175}
176
1c7f4f65 177sub setup_network {
55fa4e09 178 my ($self) = @_;
1db602d0 179 $self->protected_call(sub { $self->{plugin}->setup_network($self->{conf}) });
1c7f4f65
DM
180}
181
182sub set_hostname {
183 my ($self) = @_;
1db602d0 184 $self->protected_call(sub { $self->{plugin}->set_hostname($self->{conf}) });
1c7f4f65
DM
185}
186
c325b32f
DM
187sub set_dns {
188 my ($self) = @_;
1db602d0 189 $self->protected_call(sub { $self->{plugin}->set_dns($self->{conf}) });
c325b32f
DM
190}
191
e6e308ae
OB
192sub set_timezone {
193 my ($self) = @_;
1db602d0 194 $self->protected_call(sub { $self->{plugin}->set_timezone($self->{conf}) });
e6e308ae
OB
195}
196
d66768a2
DM
197sub setup_init {
198 my ($self) = @_;
1db602d0 199 $self->protected_call(sub { $self->{plugin}->setup_init($self->{conf}) });
d66768a2
DM
200}
201
168d6b07
DM
202sub set_user_password {
203 my ($self, $user, $pw) = @_;
1db602d0 204 $self->protected_call(sub { $self->{plugin}->set_user_password($self->{conf}, $user, $pw) });
1c7f4f65
DM
205}
206
6d9173c3
TL
207my sub generate_ssh_key { # create temporary key in hosts' /run, then read and unlink
208 my ($type, $comment) = @_;
209
210 my $key_id = '';
211 my $keygen_outfunc = sub {
212 if ($_[0] =~ m/^((?:[0-9a-f]{2}:)+[0-9a-f]{2}|SHA256:[0-9a-z+\/]{43})\s+\Q$comment\E$/i) {
213 $key_id = $_[0];
214 }
215 };
216 my $file = "/run/pve/.tmp$$.$type";
217 PVE::Tools::run_command(
218 ['ssh-keygen', '-f', $file, '-t', $type, '-N', '', '-E', 'sha256', '-C', $comment],
219 outfunc => $keygen_outfunc,
220 );
221 my ($private) = (PVE::Tools::file_get_contents($file) =~ /^(.*)$/sg); # untaint
222 my ($public) = (PVE::Tools::file_get_contents("$file.pub") =~ /^(.*)$/sg); # untaint
223 unlink $file, "$file.pub";
224
225 return ($key_id, $private, $public);
226}
227
7ee31468
DM
228sub rewrite_ssh_host_keys {
229 my ($self) = @_;
230
238b7e3e
DM
231 return if !$self->{plugin}; # unmanaged
232
797e12e8 233 my $plugin = $self->{plugin};
797e12e8 234
bc5b7618 235 my $keynames = $plugin->ssh_host_key_types_to_generate();
797e12e8 236
bc5b7618 237 return if ! -d "$self->{rootdir}/etc/ssh" || !$keynames || !scalar(keys $keynames->%*);
797e12e8 238
9cc77d55 239 my $hostname = $self->{conf}->{hostname} || 'localhost';
797e12e8
WB
240 $hostname =~ s/\..*$//;
241
6d9173c3
TL
242 my $keyfiles = [];
243 for my $keytype (keys $keynames->%*) {
797e12e8 244 my $basename = $keynames->{$keytype};
797e12e8 245 print "Creating SSH host key '$basename' - this may take some time ...\n";
6d9173c3
TL
246 my ($id, $private, $public) = generate_ssh_key($keytype, "root\@$hostname");
247 print "done: $id\n";
248
249 push $keyfiles->@*, ["/etc/ssh/$basename", $private, 0600], ["/etc/ssh/$basename.pub", $public, 0644];
797e12e8
WB
250 }
251
6d9173c3
TL
252 $self->protected_call(sub { # write them now all to the CTs rootfs at once
253 for my $file ($keyfiles->@*) {
254 $plugin->ct_file_set_contents($file->@*);
797e12e8 255 }
30e7205e 256 });
1db602d0 257}
7ee31468 258
24f37c3d
TL
259sub pre_start_hook {
260 my ($self) = @_;
261
f3aedcdb 262 $self->protected_call(sub { $self->{plugin}->pre_start_hook($self->{conf}) });
d66768a2
DM
263}
264
64d4c144
OB
265sub post_clone_hook {
266 my ($self, $conf) = @_;
267
1db602d0 268 $self->protected_call(sub { $self->{plugin}->post_clone_hook($conf) });
64d4c144
OB
269}
270
d66768a2 271sub post_create_hook {
f36ce482 272 my ($self, $root_password, $ssh_keys) = @_;
1c7f4f65 273
1db602d0 274 $self->protected_call(sub {
f36ce482 275 $self->{plugin}->post_create_hook($self->{conf}, $root_password, $ssh_keys);
1db602d0 276 });
797e12e8 277 $self->rewrite_ssh_host_keys();
1c7f4f65
DM
278}
279
42216b6b
TL
280sub unified_cgroupv2_support {
281 my ($self) = @_;
282
283 $self->protected_call(sub { $self->{plugin}->unified_cgroupv2_support() });
284}
285
153747ff
WB
286# os-release(5):
287# (...) a newline-separated list of environment-like shell-compatible
288# variable assignments. (...) beyond mere variable assignments, no shell
289# features are supported (this means variable expansion is explicitly not
290# supported) (...). Variable assignment values must be enclosed in double or
291# single quotes *if* they include spaces, semicolons or other special
292# characters outside of A-Z, a-z, 0-9. Shell special characters ("$", quotes,
293# backslash, backtick) must be escaped with backslashes (...). All strings
294# should be in UTF-8 format, and non-printable characters should not be used.
295# It is not supported to concatenate multiple individually quoted strings.
296# Lines beginning with "#" shall be ignored as comments.
297my $parse_os_release = sub {
298 my ($data) = @_;
299 my $variables = {};
300 while (defined($data) && $data =~ /^(.+)$/gm) {
301 next if $1 !~ /^\s*([a-zA-Z_][a-zA-Z0-9_]*)=(.*)$/;
302 my ($var, $content) = ($1, $2);
303 chomp $content;
304
305 if ($content =~ /^'([^']*)'/) {
306 $variables->{$var} = $1;
307 } elsif ($content =~ /^"((?:[^"\\]|\\.)*)"/) {
308 my $s = $1;
309 $s =~ s/(\\["'`nt\$\\])/"\"$1\""/eeg;
310 $variables->{$var} = $s;
311 } elsif ($content =~ /^([A-Za-z0-9]*)/) {
312 $variables->{$var} = $1;
313 }
314 }
315 return $variables;
316};
317
318sub get_ct_os_release {
319 my ($self) = @_;
320
1db602d0 321 my $data = $self->protected_call(sub {
153747ff
WB
322 if (-f '/etc/os-release') {
323 return PVE::Tools::file_get_contents('/etc/os-release');
324 } elsif (-f '/usr/lib/os-release') {
325 return PVE::Tools::file_get_contents('/usr/lib/os-release');
326 }
327 return undef;
1db602d0 328 });
153747ff
WB
329
330 return &$parse_os_release($data);
331}
332
1c7f4f65 3331;