]>
git.proxmox.com Git - pve-container.git/blob - src/PVE/LXC/Setup.pm
1 package PVE
::LXC
::Setup
;
8 use PVE
::LXC
::Setup
::Debian
;
9 use PVE
::LXC
::Setup
::Ubuntu
;
10 use PVE
::LXC
::Setup
::CentOS
;
11 use PVE
::LXC
::Setup
::Fedora
;
12 use PVE
::LXC
::Setup
::SUSE
;
13 use PVE
::LXC
::Setup
::ArchLinux
;
14 use PVE
::LXC
::Setup
::Alpine
;
15 use PVE
::LXC
::Setup
::Gentoo
;
16 use PVE
::LXC
::Setup
::Devuan
;
19 debian
=> 'PVE::LXC::Setup::Debian',
20 devuan
=> 'PVE::LXC::Setup::Devuan',
21 ubuntu
=> 'PVE::LXC::Setup::Ubuntu',
22 centos
=> 'PVE::LXC::Setup::CentOS',
23 fedora
=> 'PVE::LXC::Setup::Fedora',
24 opensuse
=> 'PVE::LXC::Setup::SUSE',
25 archlinux
=> 'PVE::LXC::Setup::ArchLinux',
26 alpine
=> 'PVE::LXC::Setup::Alpine',
27 gentoo
=> 'PVE::LXC::Setup::Gentoo',
30 # a map to allow supporting related distro flavours
34 'opensuse-leap' => 'opensuse',
37 my $autodetect_type = sub {
38 my ($self, $rootdir, $os_release) = @_;
40 if (my $id = $os_release->{ID
}) {
41 return $id if $plugins->{$id};
42 return $plugin_alias->{$id} if $plugin_alias->{$id};
45 # fallback compatibility checks
47 my $lsb_fn = "$rootdir/etc/lsb-release";
49 my $data = PVE
::Tools
::file_get_contents
($lsb_fn);
50 if ($data =~ m/^DISTRIB_ID=Ubuntu$/im) {
55 if (-f
"$rootdir/etc/debian_version") {
57 } elsif (-f
"$rootdir/etc/devuan_version") {
59 } elsif (-f
"$rootdir/etc/SuSE-brand" || -f
"$rootdir/etc/SuSE-release") {
61 } elsif (-f
"$rootdir/etc/fedora-release") {
63 } elsif (-f
"$rootdir/etc/centos-release" || -f
"$rootdir/etc/redhat-release") {
65 } elsif (-f
"$rootdir/etc/arch-release") {
67 } elsif (-f
"$rootdir/etc/alpine-release") {
69 } elsif (-f
"$rootdir/etc/gentoo-release") {
72 die "unable to detect OS distribution\n";
76 my ($class, $conf, $rootdir, $type) = @_;
78 die "no root directory\n" if !$rootdir || $rootdir eq '/';
80 my $self = bless { conf
=> $conf, rootdir
=> $rootdir};
82 my $os_release = $self->get_ct_os_release();
84 if ($conf->{ostype
} && $conf->{ostype
} eq 'unmanaged') {
86 } elsif (!defined($type)) {
87 # try to autodetect type
88 $type = &$autodetect_type($self, $rootdir, $os_release);
89 my $expected_type = $conf->{ostype
} || $type;
91 warn "got unexpected ostype ($type != $expected_type)\n"
92 if $type ne $expected_type;
95 my $plugin_class = $plugins->{$type} ||
96 "no such OS type '$type'\n";
98 my $plugin = $plugin_class->new($conf, $rootdir, $os_release);
99 $self->{plugin
} = $plugin;
100 $self->{in_chroot
} = 0;
102 # Cache some host files we need access to:
103 $plugin->{host_resolv_conf
} = PVE
::INotify
::read_file
('resolvconf');
105 # pass on user namespace information:
106 my ($id_map, $rootuid, $rootgid) = PVE
::LXC
::parse_id_maps
($conf);
108 $plugin->{id_map
} = $id_map;
109 $plugin->{rootuid
} = $rootuid;
110 $plugin->{rootgid
} = $rootgid;
116 # Forks into a chroot and executes $sub
118 my ($self, $sub) = @_;
121 return $sub->() if $self->{in_chroot
};
123 my $rootdir = $self->{rootdir
};
124 if (!-d
"$rootdir/dev" && !mkdir("$rootdir/dev")) {
125 die "failed to create temporary /dev directory: $!\n";
128 pipe(my $res_in, my $res_out) or die "pipe failed: $!\n";
131 die "fork failed: $!\n" if !defined($child);
135 # avoid recursive forks
136 $self->{in_chroot
} = 1;
138 chroot($rootdir) or die "failed to change root to: $rootdir: $!\n";
139 chdir('/') or die "failed to change to root directory\n";
142 print {$res_out} "$res";
153 my $result = do { local $/ = undef; <$res_in> };
154 while (waitpid($child, 0) != $child) {}
156 my $method = (caller(1))[3];
157 die "error in setup task $method\n";
165 return if !$self->{plugin
}; # unmanaged
168 $self->{plugin
}->template_fixup($self->{conf
});
170 $self->protected_call($code);
176 return if !$self->{plugin
}; # unmanaged
179 $self->{plugin
}->setup_network($self->{conf
});
181 $self->protected_call($code);
187 return if !$self->{plugin
}; # unmanaged
190 $self->{plugin
}->set_hostname($self->{conf
});
192 $self->protected_call($code);
198 return if !$self->{plugin
}; # unmanaged
201 $self->{plugin
}->set_dns($self->{conf
});
203 $self->protected_call($code);
209 return if !$self->{plugin
}; # unmanaged
212 $self->{plugin
}->setup_init($self->{conf
});
214 $self->protected_call($code);
217 sub set_user_password
{
218 my ($self, $user, $pw) = @_;
220 return if !$self->{plugin
}; # unmanaged
223 $self->{plugin
}->set_user_password($self->{conf
}, $user, $pw);
225 $self->protected_call($code);
228 sub rewrite_ssh_host_keys
{
231 return if !$self->{plugin
}; # unmanaged
233 my $conf = $self->{conf
};
234 my $plugin = $self->{plugin
};
235 my $rootdir = $self->{rootdir
};
237 return if ! -d
"$rootdir/etc/ssh";
240 rsa
=> 'ssh_host_rsa_key',
241 dsa
=> 'ssh_host_dsa_key',
242 ecdsa
=> 'ssh_host_ecdsa_key',
243 ed25519
=> 'ssh_host_ed25519_key',
246 my $hostname = $conf->{hostname
} || 'localhost';
247 $hostname =~ s/\..*$//;
248 my $ssh_comment = "root\@$hostname";
250 my $keygen_outfunc = sub {
253 print "done: $line\n"
254 if $line =~ m/^(?:[0-9a-f]{2}:)+[0-9a-f]{2}\s+\Q$ssh_comment\E$/i ||
255 $line =~ m/^SHA256:[0-9a-z+\/]{43}\s
+\Q
$ssh_comment\E$/i;
258 # Create temporary keys in /tmp on the host
260 foreach my $keytype (keys %$keynames) {
261 my $basename = $keynames->{$keytype};
262 my $file = "/tmp/$$.$basename";
263 print "Creating SSH host key '$basename' - this may take some time ...\n";
264 my $cmd = ['ssh-keygen', '-f', $file, '-t', $keytype,
265 '-N', '', '-E', 'sha256', '-C', $ssh_comment];
266 PVE
::Tools
::run_command
($cmd, outfunc
=> $keygen_outfunc);
267 $keyfiles->{"/etc/ssh/$basename"} = [PVE
::Tools
::file_get_contents
($file), 0600];
268 $keyfiles->{"/etc/ssh/$basename.pub"} = [PVE
::Tools
::file_get_contents
("$file.pub"), 0644];
273 # Write keys out in a protected call
276 foreach my $file (keys %$keyfiles) {
277 $plugin->ct_file_set_contents($file, @{$keyfiles->{$file}});
280 $self->protected_call($code);
286 return if !$self->{plugin
}; # unmanaged
289 # Create /fastboot to skip run fsck
290 $self->{plugin
}->ct_file_set_contents('/fastboot', '');
292 $self->{plugin
}->pre_start_hook($self->{conf
});
294 $self->protected_call($code);
297 sub post_create_hook
{
298 my ($self, $root_password, $ssh_keys) = @_;
300 return if !$self->{plugin
}; # unmanaged
303 $self->{plugin
}->post_create_hook($self->{conf
}, $root_password, $ssh_keys);
305 $self->protected_call($code);
306 $self->rewrite_ssh_host_keys();
310 # (...) a newline-separated list of environment-like shell-compatible
311 # variable assignments. (...) beyond mere variable assignments, no shell
312 # features are supported (this means variable expansion is explicitly not
313 # supported) (...). Variable assignment values must be enclosed in double or
314 # single quotes *if* they include spaces, semicolons or other special
315 # characters outside of A-Z, a-z, 0-9. Shell special characters ("$", quotes,
316 # backslash, backtick) must be escaped with backslashes (...). All strings
317 # should be in UTF-8 format, and non-printable characters should not be used.
318 # It is not supported to concatenate multiple individually quoted strings.
319 # Lines beginning with "#" shall be ignored as comments.
320 my $parse_os_release = sub {
323 while (defined($data) && $data =~ /^(.+)$/gm) {
324 next if $1 !~ /^\s*([a-zA-Z_][a-zA-Z0-9_]*)=(.*)$/;
325 my ($var, $content) = ($1, $2);
328 if ($content =~ /^'([^']*)'/) {
329 $variables->{$var} = $1;
330 } elsif ($content =~ /^"((?:[^"\\]|\\.)*)"/) {
332 $s =~ s/(\\["'`nt\$\\])/"\"$1\""/eeg;
333 $variables->{$var} = $s;
334 } elsif ($content =~ /^([A-Za-z0-9]*)/) {
335 $variables->{$var} = $1;
341 sub get_ct_os_release
{
345 if (-f
'/etc/os-release') {
346 return PVE
::Tools
::file_get_contents
('/etc/os-release');
347 } elsif (-f
'/usr/lib/os-release') {
348 return PVE
::Tools
::file_get_contents
('/usr/lib/os-release');
353 my $data = $self->protected_call($code);
355 return &$parse_os_release($data);