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
;
20 use PVE
::LXC
::Setup
::Unmanaged
;
23 alpine
=> 'PVE::LXC::Setup::Alpine',
24 archlinux
=> 'PVE::LXC::Setup::ArchLinux',
25 centos
=> 'PVE::LXC::Setup::CentOS',
26 debian
=> 'PVE::LXC::Setup::Debian',
27 devuan
=> 'PVE::LXC::Setup::Devuan',
28 fedora
=> 'PVE::LXC::Setup::Fedora',
29 gentoo
=> 'PVE::LXC::Setup::Gentoo',
30 opensuse
=> 'PVE::LXC::Setup::SUSE',
31 ubuntu
=> 'PVE::LXC::Setup::Ubuntu',
32 unmanaged
=> 'PVE::LXC::Setup::Unmanaged',
35 # a map to allow supporting related distro flavours
37 'opensuse-leap' => 'opensuse',
38 'opensuse-tumbleweed' => 'opensuse',
43 my $autodetect_type = sub {
44 my ($self, $rootdir, $os_release) = @_;
46 if (my $id = $os_release->{ID
}) {
47 return $id if $plugins->{$id};
48 return $plugin_alias->{$id} if $plugin_alias->{$id};
49 warn "unknown ID '$id' in /etc/os-release file, trying fallback detection\n";
52 # fallback compatibility checks
54 my $lsb_fn = "$rootdir/etc/lsb-release";
56 my $data = PVE
::Tools
::file_get_contents
($lsb_fn);
57 if ($data =~ m/^DISTRIB_ID=Ubuntu$/im) {
62 if (-f
"$rootdir/etc/debian_version") {
64 } elsif (-f
"$rootdir/etc/devuan_version") {
66 } elsif (-f
"$rootdir/etc/SuSE-brand" || -f
"$rootdir/etc/SuSE-release") {
68 } elsif (-f
"$rootdir/etc/fedora-release") {
70 } elsif (-f
"$rootdir/etc/centos-release" || -f
"$rootdir/etc/redhat-release") {
72 } elsif (-f
"$rootdir/etc/arch-release") {
74 } elsif (-f
"$rootdir/etc/alpine-release") {
76 } elsif (-f
"$rootdir/etc/gentoo-release") {
78 } elsif (-f
"$rootdir/etc/os-release") {
79 die "unable to detect OS distribution\n";
81 warn "/etc/os-release file not found and autodetection failed, falling back to 'unmanaged'\n";
87 my ($class, $conf, $rootdir, $type) = @_;
89 die "no root directory\n" if !$rootdir || $rootdir eq '/';
91 my $self = bless { conf
=> $conf, rootdir
=> $rootdir}, $class;
93 my $os_release = $self->get_ct_os_release();
95 if ($conf->{ostype
} && $conf->{ostype
} eq 'unmanaged') {
97 } elsif (!defined($type)) {
98 # try to autodetect type
99 $type = &$autodetect_type($self, $rootdir, $os_release);
100 my $expected_type = $conf->{ostype
} || $type;
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"
109 my $plugin_class = $plugins->{$type} || die "no such OS type '$type'\n";
111 my $plugin = $plugin_class->new($conf, $rootdir, $os_release);
112 $self->{plugin
} = $plugin;
113 $self->{in_chroot
} = 0;
115 # Cache some host files we need access to:
116 $plugin->{host_resolv_conf
} = PVE
::INotify
::read_file
('resolvconf');
118 abs_path
('/etc/localtime') =~ m
|^(/.+)| or die "invalid /etc
/localtime\n"; # untaint
119 $plugin->{host_localtime} = $1;
121 # pass on user namespace information:
122 my ($id_map, $rootuid, $rootgid) = PVE::LXC::parse_id_maps($conf);
124 $plugin->{id_map} = $id_map;
125 $plugin->{rootuid} = $rootuid;
126 $plugin->{rootgid} = $rootgid;
132 # Forks into a chroot and executes $sub
134 my ($self, $sub) = @_;
137 return $sub->() if $self->{in_chroot};
139 pipe(my $res_in, my $res_out) or die "pipe failed
: $!\n";
142 die "fork failed
: $!\n" if !defined($child);
146 # avoid recursive forks
147 $self->{in_chroot} = 1;
149 my $rootdir = $self->{rootdir};
150 chroot($rootdir) or die "failed to change root to
: $rootdir: $!\n";
151 chdir('/') or die "failed to change to root directory
\n";
154 print {$res_out} "$res";
165 my $result = do { local $/ = undef; <$res_in> };
166 while (waitpid($child, 0) != $child) {}
168 my $method = (caller(1))[3];
169 die "error
in setup task
$method\n";
176 $self->protected_call(sub { $self->{plugin}->template_fixup($self->{conf}) });
181 $self->protected_call(sub { $self->{plugin}->setup_network($self->{conf}) });
186 $self->protected_call(sub { $self->{plugin}->set_hostname($self->{conf}) });
191 $self->protected_call(sub { $self->{plugin}->set_dns($self->{conf}) });
196 $self->protected_call(sub { $self->{plugin}->set_timezone($self->{conf}) });
201 $self->protected_call(sub { $self->{plugin}->setup_init($self->{conf}) });
204 sub set_user_password {
205 my ($self, $user, $pw) = @_;
206 $self->protected_call(sub { $self->{plugin}->set_user_password($self->{conf}, $user, $pw) });
209 my sub generate_ssh_key { # create temporary key in hosts' /run, then read and unlink
210 my ($type, $comment) = @_;
213 my $keygen_outfunc = sub {
214 if ($_[0] =~ m/^((?:[0-9a-f]{2}:)+[0-9a-f]{2}|SHA256:[0-9a-z+\/]{43})\s+\Q$comment\E$/i) {
218 my $file = "/run/pve
/.tmp
$$.$type";
219 PVE::Tools::run_command(
220 ['ssh-keygen', '-f', $file, '-t', $type, '-N', '', '-E', 'sha256', '-C', $comment],
221 outfunc => $keygen_outfunc,
223 my ($private) = (PVE::Tools::file_get_contents($file) =~ /^(.*)$/sg); # untaint
224 my ($public) = (PVE::Tools::file_get_contents("$file.pub
") =~ /^(.*)$/sg); # untaint
225 unlink $file, "$file.pub
";
227 return ($key_id, $private, $public);
230 sub rewrite_ssh_host_keys {
233 my $plugin = $self->{plugin};
235 my $keynames = $plugin->ssh_host_key_types_to_generate();
237 return if ! -d "$self->{rootdir
}/etc/ssh
" || !$keynames || !scalar(keys $keynames->%*);
239 my $hostname = $self->{conf}->{hostname} || 'localhost';
240 $hostname =~ s/\..*$//;
243 for my $keytype (keys $keynames->%*) {
244 my $basename = $keynames->{$keytype};
245 print "Creating SSH host key
'$basename' - this may
take some
time ...\n";
246 my ($id, $private, $public) = generate_ssh_key($keytype, "root\
@$hostname");
249 push $keyfiles->@*, ["/etc/ssh
/$basename", $private, 0600], ["/etc
/ssh/$basename.pub
", $public, 0644];
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->@*);
262 $self->protected_call(sub { $self->{plugin}->pre_start_hook($self->{conf}) });
265 sub post_clone_hook {
266 my ($self, $conf) = @_;
268 $self->protected_call(sub { $self->{plugin}->post_clone_hook($conf) });
271 sub post_create_hook {
272 my ($self, $root_password, $ssh_keys) = @_;
274 $self->protected_call(sub {
275 $self->{plugin}->post_create_hook($self->{conf}, $root_password, $ssh_keys);
277 $self->rewrite_ssh_host_keys();
280 sub unified_cgroupv2_support {
283 return $self->protected_call(sub { $self->{plugin}->unified_cgroupv2_support() });
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.
297 my $parse_os_release = sub {
300 while (defined($data) && $data =~ /^(.+)$/gm) {
301 next if $1 !~ /^\s*([a-zA-Z_][a-zA-Z0-9_]*)=(.*)$/;
302 my ($var, $content) = ($1, $2);
305 if ($content =~ /^'([^']*)'/) {
306 $variables->{$var} = $1;
307 } elsif ($content =~ /^"((?:[^"\\]|\\.)*)"/) {
309 $s =~ s/(\\["'`nt\$\\])/"\"$1\""/eeg;
310 $variables->{$var} = $s;
311 } elsif ($content =~ /^([A-Za-z0-9]*)/) {
312 $variables->{$var} = $1;
318 sub get_ct_os_release
{
321 my $data = $self->protected_call(sub {
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');
330 return &$parse_os_release($data);