]>
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
36 my $autodetect_type = sub {
37 my ($self, $rootdir, $os_release) = @_;
39 if (my $id = $os_release->{ID
}) {
40 return $id if $plugins->{$id};
41 return $plugin_alias->{$id} if $plugin_alias->{$id};
44 # fallback compatibility checks
46 my $lsb_fn = "$rootdir/etc/lsb-release";
48 my $data = PVE
::Tools
::file_get_contents
($lsb_fn);
49 if ($data =~ m/^DISTRIB_ID=Ubuntu$/im) {
54 if (-f
"$rootdir/etc/debian_version") {
56 } elsif (-f
"$rootdir/etc/devuan_version") {
58 } elsif (-f
"$rootdir/etc/SuSE-brand" || -f
"$rootdir/etc/SuSE-release") {
60 } elsif (-f
"$rootdir/etc/fedora-release") {
62 } elsif (-f
"$rootdir/etc/centos-release" || -f
"$rootdir/etc/redhat-release") {
64 } elsif (-f
"$rootdir/etc/arch-release") {
66 } elsif (-f
"$rootdir/etc/alpine-release") {
68 } elsif (-f
"$rootdir/etc/gentoo-release") {
71 die "unable to detect OS distribution\n";
75 my ($class, $conf, $rootdir, $type) = @_;
77 die "no root directory\n" if !$rootdir || $rootdir eq '/';
79 my $self = bless { conf
=> $conf, rootdir
=> $rootdir};
81 my $os_release = $self->get_ct_os_release();
83 if ($conf->{ostype
} && $conf->{ostype
} eq 'unmanaged') {
85 } elsif (!defined($type)) {
86 # try to autodetect type
87 $type = &$autodetect_type($self, $rootdir, $os_release);
88 my $expected_type = $conf->{ostype
} || $type;
90 warn "got unexpected ostype ($type != $expected_type)\n"
91 if $type ne $expected_type;
94 my $plugin_class = $plugins->{$type} ||
95 "no such OS type '$type'\n";
97 my $plugin = $plugin_class->new($conf, $rootdir, $os_release);
98 $self->{plugin
} = $plugin;
99 $self->{in_chroot
} = 0;
101 # Cache some host files we need access to:
102 $plugin->{host_resolv_conf
} = PVE
::INotify
::read_file
('resolvconf');
104 # pass on user namespace information:
105 my ($id_map, $rootuid, $rootgid) = PVE
::LXC
::parse_id_maps
($conf);
107 $plugin->{id_map
} = $id_map;
108 $plugin->{rootuid
} = $rootuid;
109 $plugin->{rootgid
} = $rootgid;
115 # Forks into a chroot and executes $sub
117 my ($self, $sub) = @_;
120 return $sub->() if $self->{in_chroot
};
122 my $rootdir = $self->{rootdir
};
123 if (!-d
"$rootdir/dev" && !mkdir("$rootdir/dev")) {
124 die "failed to create temporary /dev directory: $!\n";
127 pipe(my $res_in, my $res_out) or die "pipe failed: $!\n";
130 die "fork failed: $!\n" if !defined($child);
134 # avoid recursive forks
135 $self->{in_chroot
} = 1;
137 chroot($rootdir) or die "failed to change root to: $rootdir: $!\n";
138 chdir('/') or die "failed to change to root directory\n";
141 print {$res_out} "$res";
152 my $result = do { local $/ = undef; <$res_in> };
153 while (waitpid($child, 0) != $child) {}
155 my $method = (caller(1))[3];
156 die "error in setup task $method\n";
164 return if !$self->{plugin
}; # unmanaged
167 $self->{plugin
}->template_fixup($self->{conf
});
169 $self->protected_call($code);
175 return if !$self->{plugin
}; # unmanaged
178 $self->{plugin
}->setup_network($self->{conf
});
180 $self->protected_call($code);
186 return if !$self->{plugin
}; # unmanaged
189 $self->{plugin
}->set_hostname($self->{conf
});
191 $self->protected_call($code);
197 return if !$self->{plugin
}; # unmanaged
200 $self->{plugin
}->set_dns($self->{conf
});
202 $self->protected_call($code);
208 return if !$self->{plugin
}; # unmanaged
211 $self->{plugin
}->setup_init($self->{conf
});
213 $self->protected_call($code);
216 sub set_user_password
{
217 my ($self, $user, $pw) = @_;
219 return if !$self->{plugin
}; # unmanaged
222 $self->{plugin
}->set_user_password($self->{conf
}, $user, $pw);
224 $self->protected_call($code);
227 sub rewrite_ssh_host_keys
{
230 return if !$self->{plugin
}; # unmanaged
232 my $conf = $self->{conf
};
233 my $plugin = $self->{plugin
};
234 my $rootdir = $self->{rootdir
};
236 return if ! -d
"$rootdir/etc/ssh";
239 rsa
=> 'ssh_host_rsa_key',
240 dsa
=> 'ssh_host_dsa_key',
241 ecdsa
=> 'ssh_host_ecdsa_key',
242 ed25519
=> 'ssh_host_ed25519_key',
245 my $hostname = $conf->{hostname
} || 'localhost';
246 $hostname =~ s/\..*$//;
247 my $ssh_comment = "root\@$hostname";
249 my $keygen_outfunc = sub {
252 print "done: $line\n"
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;
257 # Create temporary keys in /tmp on the host
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";
263 my $cmd = ['ssh-keygen', '-f', $file, '-t', $keytype,
264 '-N', '', '-E', 'sha256', '-C', $ssh_comment];
265 PVE
::Tools
::run_command
($cmd, outfunc
=> $keygen_outfunc);
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];
272 # Write keys out in a protected call
275 foreach my $file (keys %$keyfiles) {
276 $plugin->ct_file_set_contents($file, @{$keyfiles->{$file}});
279 $self->protected_call($code);
285 return if !$self->{plugin
}; # unmanaged
288 # Create /fastboot to skip run fsck
289 $self->{plugin
}->ct_file_set_contents('/fastboot', '');
291 $self->{plugin
}->pre_start_hook($self->{conf
});
293 $self->protected_call($code);
296 sub post_create_hook
{
297 my ($self, $root_password, $ssh_keys) = @_;
299 return if !$self->{plugin
}; # unmanaged
302 $self->{plugin
}->post_create_hook($self->{conf
}, $root_password, $ssh_keys);
304 $self->protected_call($code);
305 $self->rewrite_ssh_host_keys();
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.
319 my $parse_os_release = sub {
322 while (defined($data) && $data =~ /^(.+)$/gm) {
323 next if $1 !~ /^\s*([a-zA-Z_][a-zA-Z0-9_]*)=(.*)$/;
324 my ($var, $content) = ($1, $2);
327 if ($content =~ /^'([^']*)'/) {
328 $variables->{$var} = $1;
329 } elsif ($content =~ /^"((?:[^"\\]|\\.)*)"/) {
331 $s =~ s/(\\["'`nt\$\\])/"\"$1\""/eeg;
332 $variables->{$var} = $s;
333 } elsif ($content =~ /^([A-Za-z0-9]*)/) {
334 $variables->{$var} = $1;
340 sub get_ct_os_release
{
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');
352 my $data = $self->protected_call($code);
354 return &$parse_os_release($data);