]>
git.proxmox.com Git - pve-container.git/blob - src/PVE/LXC/Setup.pm
f6cf43017a1b7e7212b2e49345528c27f5fad7b0
1 package PVE
::LXC
::Setup
;
11 use PVE
::LXC
::Setup
::Alpine
;
12 use PVE
::LXC
::Setup
::ArchLinux
;
13 use PVE
::LXC
::Setup
::CentOS
;
14 use PVE
::LXC
::Setup
::Debian
;
15 use PVE
::LXC
::Setup
::Devuan
;
16 use PVE
::LXC
::Setup
::Fedora
;
17 use PVE
::LXC
::Setup
::Gentoo
;
18 use PVE
::LXC
::Setup
::SUSE
;
19 use PVE
::LXC
::Setup
::Ubuntu
;
22 alpine
=> 'PVE::LXC::Setup::Alpine',
23 archlinux
=> 'PVE::LXC::Setup::ArchLinux',
24 centos
=> 'PVE::LXC::Setup::CentOS',
25 debian
=> 'PVE::LXC::Setup::Debian',
26 devuan
=> 'PVE::LXC::Setup::Devuan',
27 fedora
=> 'PVE::LXC::Setup::Fedora',
28 gentoo
=> 'PVE::LXC::Setup::Gentoo',
29 opensuse
=> 'PVE::LXC::Setup::SUSE',
30 ubuntu
=> 'PVE::LXC::Setup::Ubuntu',
33 # a map to allow supporting related distro flavours
35 'opensuse-leap' => 'opensuse',
36 'opensuse-tumbleweed' => 'opensuse',
41 my $autodetect_type = sub {
42 my ($self, $rootdir, $os_release) = @_;
44 if (my $id = $os_release->{ID
}) {
45 return $id if $plugins->{$id};
46 return $plugin_alias->{$id} if $plugin_alias->{$id};
47 warn "unknown ID '$id' in /etc/os-release file, trying fallback detection\n";
50 # fallback compatibility checks
52 my $lsb_fn = "$rootdir/etc/lsb-release";
54 my $data = PVE
::Tools
::file_get_contents
($lsb_fn);
55 if ($data =~ m/^DISTRIB_ID=Ubuntu$/im) {
60 if (-f
"$rootdir/etc/debian_version") {
62 } elsif (-f
"$rootdir/etc/devuan_version") {
64 } elsif (-f
"$rootdir/etc/SuSE-brand" || -f
"$rootdir/etc/SuSE-release") {
66 } elsif (-f
"$rootdir/etc/fedora-release") {
68 } elsif (-f
"$rootdir/etc/centos-release" || -f
"$rootdir/etc/redhat-release") {
70 } elsif (-f
"$rootdir/etc/arch-release") {
72 } elsif (-f
"$rootdir/etc/alpine-release") {
74 } elsif (-f
"$rootdir/etc/gentoo-release") {
76 } elsif (-f
"$rootdir/etc/os-release") {
77 die "unable to detect OS distribution\n";
79 warn "/etc/os-release file not found and autodetection failed, falling back to 'unmanaged'\n";
85 my ($class, $conf, $rootdir, $type) = @_;
87 die "no root directory\n" if !$rootdir || $rootdir eq '/';
89 my $self = bless { conf
=> $conf, rootdir
=> $rootdir}, $class;
91 my $os_release = $self->get_ct_os_release();
93 if ($conf->{ostype
} && $conf->{ostype
} eq 'unmanaged') {
95 } elsif (!defined($type)) {
96 # try to autodetect type
97 $type = &$autodetect_type($self, $rootdir, $os_release);
98 my $expected_type = $conf->{ostype
} || $type;
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"
107 if ($type eq 'unmanaged') {
108 $conf->{ostype
} = $type;
112 my $plugin_class = $plugins->{$type} || die "no such OS type '$type'\n";
114 my $plugin = $plugin_class->new($conf, $rootdir, $os_release);
115 $self->{plugin
} = $plugin;
116 $self->{in_chroot
} = 0;
118 # Cache some host files we need access to:
119 $plugin->{host_resolv_conf
} = PVE
::INotify
::read_file
('resolvconf');
120 $plugin->{host_localtime
} = abs_path
('/etc/localtime');
122 # pass on user namespace information:
123 my ($id_map, $rootuid, $rootgid) = PVE
::LXC
::parse_id_maps
($conf);
125 $plugin->{id_map
} = $id_map;
126 $plugin->{rootuid
} = $rootuid;
127 $plugin->{rootgid
} = $rootgid;
133 # Forks into a chroot and executes $sub
135 my ($self, $sub) = @_;
138 return $sub->() if $self->{in_chroot
};
140 pipe(my $res_in, my $res_out) or die "pipe failed: $!\n";
143 die "fork failed: $!\n" if !defined($child);
147 # avoid recursive forks
148 $self->{in_chroot
} = 1;
150 my $rootdir = $self->{rootdir
};
151 chroot($rootdir) or die "failed to change root to: $rootdir: $!\n";
152 chdir('/') or die "failed to change to root directory\n";
155 print {$res_out} "$res";
166 my $result = do { local $/ = undef; <$res_in> };
167 while (waitpid($child, 0) != $child) {}
169 my $method = (caller(1))[3];
170 die "error in setup task $method\n";
178 return if !$self->{plugin
}; # unmanaged
180 $self->protected_call(sub { $self->{plugin
}->template_fixup($self->{conf
}) });
186 return if !$self->{plugin
}; # unmanaged
188 $self->protected_call(sub { $self->{plugin
}->setup_network($self->{conf
}) });
194 return if !$self->{plugin
}; # unmanaged
196 $self->protected_call(sub { $self->{plugin
}->set_hostname($self->{conf
}) });
202 return if !$self->{plugin
}; # unmanaged
204 $self->protected_call(sub { $self->{plugin
}->set_dns($self->{conf
}) });
210 return if !$self->{plugin
}; # unmanaged
212 $self->protected_call(sub { $self->{plugin
}->set_timezone($self->{conf
}) });
218 return if !$self->{plugin
}; # unmanaged
220 $self->protected_call(sub { $self->{plugin
}->setup_init($self->{conf
}) });
223 sub set_user_password
{
224 my ($self, $user, $pw) = @_;
226 return if !$self->{plugin
}; # unmanaged
228 $self->protected_call(sub { $self->{plugin
}->set_user_password($self->{conf
}, $user, $pw) });
231 my sub generate_ssh_key
{ # create temporary key in hosts' /run, then read and unlink
232 my ($type, $comment) = @_;
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) {
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,
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";
249 return ($key_id, $private, $public);
252 sub rewrite_ssh_host_keys
{
255 return if !$self->{plugin
}; # unmanaged
257 my $plugin = $self->{plugin
};
259 my $keynames = $plugin->ssh_host_key_types_to_generate();
261 return if ! -d
"$self->{rootdir}/etc/ssh" || !$keynames || !scalar(keys $keynames->%*);
263 my $hostname = $self->{conf
}->{hostname
} || 'localhost';
264 $hostname =~ s/\..*$//;
267 for my $keytype (keys $keynames->%*) {
268 my $basename = $keynames->{$keytype};
269 print "Creating SSH host key '$basename' - this may take some time ...\n";
270 my ($id, $private, $public) = generate_ssh_key
($keytype, "root\@$hostname");
273 push $keyfiles->@*, ["/etc/ssh/$basename", $private, 0600], ["/etc/ssh/$basename.pub", $public, 0644];
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->@*);
286 $self->protected_call(sub { $self->{plugin
}->pre_start_hook($self->{conf
}) });
289 sub post_clone_hook
{
290 my ($self, $conf) = @_;
292 $self->protected_call(sub { $self->{plugin
}->post_clone_hook($conf) });
295 sub post_create_hook
{
296 my ($self, $root_password, $ssh_keys) = @_;
298 return if !$self->{plugin
}; # unmanaged
300 $self->protected_call(sub {
301 $self->{plugin
}->post_create_hook($self->{conf
}, $root_password, $ssh_keys);
303 $self->rewrite_ssh_host_keys();
306 sub unified_cgroupv2_support
{
309 $self->protected_call(sub { $self->{plugin
}->unified_cgroupv2_support() });
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 {
326 while (defined($data) && $data =~ /^(.+)$/gm) {
327 next if $1 !~ /^\s*([a-zA-Z_][a-zA-Z0-9_]*)=(.*)$/;
328 my ($var, $content) = ($1, $2);
331 if ($content =~ /^'([^']*)'/) {
332 $variables->{$var} = $1;
333 } elsif ($content =~ /^"((?:[^"\\]|\\.)*)"/) {
335 $s =~ s/(\\["'`nt\$\\])/"\"$1\""/eeg;
336 $variables->{$var} = $s;
337 } elsif ($content =~ /^([A-Za-z0-9]*)/) {
338 $variables->{$var} = $1;
344 sub get_ct_os_release
{
347 my $data = $self->protected_call(sub {
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');
356 return &$parse_os_release($data);