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