1 package PVE
::LXC
::Setup
::Base
;
20 my ($class, $conf, $rootdir, $os_release) = @_;
22 return bless { conf
=> $conf, rootdir
=> $rootdir, os_release
=> $os_release }, $class;
26 my ($self, $conf) = @_;
28 my $nameserver = $conf->{nameserver
};
29 my $searchdomains = $conf->{searchdomain
};
31 if ($conf->{'testmode'}) {
32 return ('proxmox.com', '8.8.8.8 8.8.8.9');
35 my $host_resolv_conf = $self->{host_resolv_conf
};
37 if (!defined($nameserver)) {
39 foreach my $k ("dns1", "dns2", "dns3") {
40 if (my $ns = $host_resolv_conf->{$k}) {
44 $nameserver = join(' ', @list);
47 if (!defined($searchdomains)) {
48 $searchdomains = $host_resolv_conf->{search
};
51 return ($searchdomains, $nameserver);
54 sub update_etc_hosts
{
55 my ($self, $hostip, $oldname, $newname, $searchdomains) = @_;
57 my $hosts_fn = '/etc/hosts';
58 return if $self->ct_is_file_ignored($hosts_fn);
60 my $namepart = ($newname =~ s/\..*$//r);
63 if ($newname =~ /\./) {
64 $all_names .= "$newname $namepart";
66 foreach my $domain (PVE
::Tools
::split_list
($searchdomains)) {
67 $all_names .= ' ' if $all_names;
68 $all_names .= "$newname.$domain";
70 $all_names .= ' ' if $all_names;
71 $all_names .= $newname;
77 my $lo4 = "127.0.0.1 localhost.localnet localhost\n";
78 my $lo6 = "::1 localhost.localnet localhost\n";
79 if ($self->ct_file_exists($hosts_fn)) {
80 my $data = $self->ct_file_get_contents($hosts_fn);
81 # don't take localhost entries within our hosts sections into account
82 $data = remove_pve_sections
($data);
84 # check for existing localhost entries
85 $section .= $lo4 if $data !~ /^\h*127\.0\.0\.1\h+/m;
86 $section .= $lo6 if $data !~ /^\h*::1\h+/m;
88 $section .= $lo4 . $lo6;
91 if (defined($hostip)) {
92 $section .= "$hostip $all_names\n";
93 } elsif ($namepart ne 'localhost') {
94 $section .= "127.0.1.1 $all_names\n";
96 $section .= "127.0.1.1 $namepart\n";
99 $self->ct_modify_file($hosts_fn, $section);
103 my ($self, $conf) = @_;
105 # do nothing by default
109 my ($self, $conf) = @_;
111 my ($searchdomains, $nameserver) = $self->lookup_dns_conf($conf);
115 $data .= "search " . join(' ', PVE
::Tools
::split_list
($searchdomains)) . "\n"
118 foreach my $ns ( PVE
::Tools
::split_list
($nameserver)) {
119 $data .= "nameserver $ns\n";
122 $self->ct_modify_file("/etc/resolv.conf", $data, replace
=> 1);
126 my ($self, $conf) = @_;
128 my $hostname = $conf->{hostname
} || 'localhost';
130 my $namepart = ($hostname =~ s/\..*$//r);
132 my $hostname_fn = "/etc/hostname";
134 my $oldname = $self->ct_file_read_firstline($hostname_fn) || 'localhost';
136 my ($ipv4, $ipv6) = PVE
::LXC
::get_primary_ips
($conf);
137 my $hostip = $ipv4 || $ipv6;
139 my ($searchdomains) = $self->lookup_dns_conf($conf);
141 $self->update_etc_hosts($hostip, $oldname, $hostname, $searchdomains);
143 $self->ct_file_set_contents($hostname_fn, "$namepart\n");
147 my ($self, $conf) = @_;
149 die "please implement this inside subclass"
153 my ($self, $conf) = @_;
155 die "please implement this inside subclass"
158 sub setup_systemd_console
{
159 my ($self, $conf) = @_;
161 my $systemd_dir_rel = $self->ct_is_executable("/lib/systemd/systemd") ?
162 "/lib/systemd/system" : "/usr/lib/systemd/system";
164 my $systemd_getty_service_rel = "$systemd_dir_rel/getty\@.service";
166 return if !$self->ct_file_exists($systemd_getty_service_rel);
168 my $raw = $self->ct_file_get_contents($systemd_getty_service_rel);
170 my $systemd_container_getty_service_rel = "$systemd_dir_rel/container-getty\@.service";
172 # systemd on CenoOS 7.1 is too old (version 205), so there is no
173 # container-getty service
174 if (!$self->ct_file_exists($systemd_container_getty_service_rel)) {
175 if ($raw =~ s!^ConditionPathExists=/dev/tty0$!ConditionPathExists=/dev/tty!m) {
176 $self->ct_file_set_contents($systemd_getty_service_rel, $raw);
179 # undo above change (in case someone updated systemd)
180 if ($raw =~ s!^ConditionPathExists=/dev/tty$!ConditionPathExists=/dev/tty0!m) {
181 $self->ct_file_set_contents($systemd_getty_service_rel, $raw);
185 my $ttycount = PVE
::LXC
::Config-
>get_tty_count($conf);
187 for (my $i = 1; $i < 7; $i++) {
188 my $tty_service_lnk = "/etc/systemd/system/getty.target.wants/getty\@tty$i.service";
189 if ($i > $ttycount) {
190 $self->ct_unlink($tty_service_lnk);
192 if (!$self->ct_is_symlink($tty_service_lnk)) {
193 $self->ct_unlink($tty_service_lnk);
194 $self->ct_symlink($systemd_getty_service_rel, $tty_service_lnk);
200 # A few distros as well as unprivileged containers cannot deal with the
201 # /dev/lxc/ tty subdirectory.
203 my ($self, $conf) = @_;
204 return $conf->{unprivileged
} ?
'' : 'lxc/';
207 sub setup_container_getty_service
{
208 my ($self, $conf) = @_;
210 my $systemd_dir_rel = $self->ct_is_executable("/lib/systemd/systemd") ?
211 "/lib/systemd/system" : "/usr/lib/systemd/system";
212 my $servicefile = "$systemd_dir_rel/container-getty\@.service";
213 my $raw = $self->ct_file_get_contents($servicefile);
214 my $ttyname = $self->devttydir($conf) . 'tty%I';
215 if ($raw =~ s
@pts/%I|lxc
/tty
%I@$ttyname@g) {
216 $self->ct_file_set_contents($servicefile, $raw);
220 sub setup_systemd_networkd
{
221 my ($self, $conf) = @_;
223 foreach my $k (keys %$conf) {
224 next if $k !~ m/^net(\d+)$/;
225 my $d = PVE
::LXC
::Config-
>parse_lxc_network($conf->{$k});
228 my $filename = "/etc/systemd/network/$d->{name}.network";
235 Description = Interface $d->{name} autoconfigured by PVE
239 my ($has_ipv4, $has_ipv6);
242 my @DHCPMODES = ('none', 'v4', 'v6', 'both');
243 my ($NONE, $DHCP4, $DHCP6, $BOTH) = (0, 1, 2, 3);
246 if (defined(my $ip = $d->{ip
})) {
249 } elsif ($ip ne 'manual') {
251 $data .= "Address = $ip\n";
254 if (defined(my $gw = $d->{gw
})) {
255 $data .= "Gateway = $gw\n";
256 if ($has_ipv4 && !PVE
::Network
::is_ip_in_cidr
($gw, $d->{ip
}, 4)) {
257 $routes .= "\n[Route]\nDestination = $gw/32\nScope = link\n";
261 if (defined(my $ip = $d->{ip6
})) {
264 } elsif ($ip ne 'manual') {
266 $data .= "Address = $ip\n";
269 if (defined(my $gw = $d->{gw6
})) {
270 $data .= "Gateway = $gw\n";
271 if ($has_ipv6 && !PVE
::Network
::is_ip_in_cidr
($gw, $d->{ip6
}, 6) &&
272 !PVE
::Network
::is_ip_in_cidr
($gw, 'fe80::/10', 6)) {
273 $routes .= "\n[Route]\nDestination = $gw/128\nScope = link\n";
277 $data .= "DHCP = $DHCPMODES[$dhcp]\n";
278 $data .= $routes if $routes;
280 $self->ct_file_set_contents($filename, $data);
284 sub setup_securetty
{
285 my ($self, $conf, @add) = @_;
287 my $filename = "/etc/securetty";
288 # root login is already allowed on every device if no securetty present
289 return if !$self->ct_file_exists($filename);
292 @add = qw(console tty1 tty2 tty3 tty4);
293 if (my $dir = $self->devttydir($conf)) {
294 @add = map { "${dir}$_" } @add;
298 my $data = $self->ct_file_get_contents($filename);
299 chomp $data; $data .= "\n";
300 foreach my $dev (@add) {
301 if ($data !~ m!^\Q$dev\E\s*$!m) {
305 $self->ct_file_set_contents($filename, $data);
308 my $replacepw = sub {
309 my ($self, $file, $user, $epw, $shadow) = @_;
311 my $tmpfile = "$file.$$";
314 my $src = $self->ct_open_file_read($file) ||
315 die "unable to open file '$file' - $!";
317 my $st = $self->ct_stat($src) ||
318 die "unable to stat file - $!";
320 my $dst = $self->ct_open_file_write($tmpfile) ||
321 die "unable to open file '$tmpfile' - $!";
323 # copy owner and permissions
324 chmod $st->mode, $dst;
325 chown $st->uid, $st->gid, $dst;
327 my $last_change = int(time()/(60*60*24));
329 while (defined (my $line = <$src>)) {
331 $line =~ s/^${user}:[^:]*:[^:]*:/${user}:${epw}:${last_change}:/;
333 $line =~ s/^${user}:[^:]*:/${user}:${epw}:/;
338 $src->close() || die "close '$file' failed - $!\n";
339 $dst->close() || die "close '$tmpfile' failed - $!\n";
342 $self->ct_unlink($tmpfile);
344 $self->ct_rename($tmpfile, $file);
345 $self->ct_unlink($tmpfile); # in case rename fails
349 sub set_user_password
{
350 my ($self, $conf, $user, $opt_password) = @_;
352 my $pwfile = "/etc/passwd";
354 return if !$self->ct_file_exists($pwfile);
356 my $shadow = "/etc/shadow";
358 if (defined($opt_password)) {
359 if ($opt_password !~ m/^\$(?:1|2[axy]?|5|6)\$[a-zA-Z0-9.\/]{1,16}\
$[a-zA-Z0-9
.\
/]+$/) {
360 my $time = substr (Digest
::SHA
::sha1_base64
(time), 0, 8);
361 $opt_password = crypt(encode
("utf8", $opt_password), "\$6\$$time\$");
367 if ($self->ct_file_exists($shadow)) {
368 &$replacepw ($self, $shadow, $user, $opt_password, 1);
369 &$replacepw ($self, $pwfile, $user, 'x');
371 &$replacepw ($self, $pwfile, $user, $opt_password);
375 my $parse_home_dir = sub {
376 my ($self, $passwdfile, $user) = @_;
378 my $fh = $self->ct_open_file_read($passwdfile);
379 while (defined (my $line = <$fh>)) {
381 if $line =~ m/^${user}:([^:]*:){4}([^:]*):/;
385 sub set_user_authorized_ssh_keys
{
386 my ($self, $conf, $user, $ssh_keys) = @_;
388 my $passwd = "/etc/passwd";
389 my $home = $user eq "root" ?
"/root/" : "/home/$user/";
391 $home = &$parse_home_dir($self, $passwd, $user)
392 if $self->ct_file_exists($passwd);
394 die "home directory '$home' of $user does not exist!"
395 if ! ($self->ct_is_directory($home) || $self->ct_is_symlink($home));
397 $self->ct_mkdir("$home/.ssh", 0700)
398 if ! $self->ct_is_directory("$home/.ssh");
400 $self->ct_modify_file("$home/.ssh/authorized_keys", $ssh_keys, perms
=> 0700);
403 my $randomize_crontab = sub {
404 my ($self, $conf) = @_;
407 # Note: dir_glob_foreach() untaints filenames!
408 PVE
::Tools
::dir_glob_foreach
("/etc/cron.d", qr/[A-Z\-\_a-z0-9]+/, sub {
410 push @files, "/etc/cron.d/$name";
413 my $crontab_fn = "/etc/crontab";
414 unshift @files, $crontab_fn if $self->ct_file_exists($crontab_fn);
416 foreach my $filename (@files) {
417 my $data = $self->ct_file_get_contents($filename);
419 foreach my $line (split(/\n/, $data)) {
420 # we only randomize minutes for root crontab entries
421 if ($line =~ m/^\d+(\s+\S+\s+\S+\s+\S+\s+\S+\s+root\s+\S.*)$/) {
423 my $min = int(rand()*59);
424 $new .= "$min$rest\n";
429 $self->ct_file_set_contents($filename, $new);
434 my ($self, $conf) = @_;
436 $self->setup_init($conf);
437 $self->setup_network($conf);
438 $self->set_hostname($conf);
439 $self->set_dns($conf);
444 sub post_create_hook
{
445 my ($self, $conf, $root_password, $ssh_keys) = @_;
447 $self->template_fixup($conf);
449 &$randomize_crontab($self, $conf);
451 $self->set_user_password($conf, 'root', $root_password);
452 $self->set_user_authorized_ssh_keys($conf, 'root', $ssh_keys) if $ssh_keys;
453 $self->setup_init($conf);
454 $self->setup_network($conf);
455 $self->set_hostname($conf);
456 $self->set_dns($conf);
461 # File access wrappers for container setup code.
462 # For user-namespace support these might need to take uid and gid maps into account.
464 sub ct_is_file_ignored
{
465 my ($self, $file) = @_;
466 my ($name, $path) = fileparse
($file);
467 return -f
"$path/.pve-ignore.$name";
470 sub ct_reset_ownership
{
471 my ($self, @files) = @_;
472 my $conf = $self->{conf
};
473 return if !$self->{id_map
};
475 @files = grep { !$self->ct_is_file_ignored($_) } @files;
478 my $uid = $self->{rootuid
};
479 my $gid = $self->{rootgid
};
480 chown($uid, $gid, @files);
484 my ($self, $file, $mask) = @_;
485 # mkdir goes by parameter count - an `undef' mode acts like a mode of 0000
486 if (defined($mask)) {
487 return CORE
::mkdir($file, $mask) && $self->ct_reset_ownership($file);
489 return CORE
::mkdir($file) && $self->ct_reset_ownership($file);
494 my ($self, @files) = @_;
495 foreach my $file (@files) {
496 next if $self->ct_is_file_ignored($file);
502 my ($self, $old, $new) = @_;
503 return if $self->ct_is_file_ignored($new);
504 CORE
::rename($old, $new);
507 sub ct_open_file_read
{
510 return IO
::File-
>new($file, O_RDONLY
, @_);
513 sub ct_open_file_write
{
516 $file = '/dev/null' if $self->ct_is_file_ignored($file);
517 my $fh = IO
::File-
>new($file, O_WRONLY
| O_CREAT
, @_);
518 $self->ct_reset_ownership($fh);
524 if ($self->{id_map
}) {
526 if (ref($opts) eq 'HASH') {
527 $opts->{owner
} = $self->{rootuid
} if !defined($self->{owner
});
528 $opts->{group
} = $self->{rootgid
} if !defined($self->{group
});
530 File
::Path
::make_path
(@_, $opts);
532 File
::Path
::make_path
(@_);
537 my ($self, $old, $new) = @_;
538 return if $self->ct_is_file_ignored($new);
539 return CORE
::symlink($old, $new);
543 my ($self, $name) = @_;
544 return CORE
::readlink($name);
548 my ($self, $file) = @_;
552 sub ct_is_directory
{
553 my ($self, $file) = @_;
558 my ($self, $file) = @_;
562 sub ct_is_executable
{
563 my ($self, $file) = @_;
568 my ($self, $file) = @_;
569 return File
::stat::stat($file);
572 sub ct_file_read_firstline
{
573 my ($self, $file) = @_;
574 return PVE
::Tools
::file_read_firstline
($file);
577 sub ct_file_get_contents
{
578 my ($self, $file) = @_;
579 return PVE
::Tools
::file_get_contents
($file);
582 sub ct_file_set_contents
{
583 my ($self, $file, $data, $perms) = @_;
584 return if $self->ct_is_file_ignored($file);
585 PVE
::Tools
::file_set_contents
($file, $data, $perms);
586 $self->ct_reset_ownership($file);
589 # Modify a marked portion of a file.
590 # Optionally if the file becomes empty it will be deleted.
592 my ($self, $file, $data, %options) = @_;
593 return if $self->ct_is_file_ignored($file);
595 my $head = "# --- BEGIN PVE ---\n";
596 my $tail = "# --- END PVE ---\n";
597 my $perms = $options{perms
};
598 $data .= "\n" if $data && $data !~ /\n$/;
600 if (!$self->ct_file_exists($file)) {
601 $self->ct_file_set_contents($file, $head.$data.$tail, $perms) if $data;
605 my $old = $self->ct_file_get_contents($file);
606 my @lines = split(/\n/, $old);
609 foreach my $i (0..(@lines-1)) {
610 my $line = $lines[$i];
611 $beg = $i if !defined($beg) &&
612 $line =~ /^#\s*---\s*BEGIN\s*PVE\s*/;
613 $end = $i if !defined($end) && defined($beg) &&
614 $line =~ /^#\s*---\s*END\s*PVE\s*/i;
615 last if defined($beg) && defined($end);
618 if (defined($beg) && defined($end)) {
622 splice @lines, $beg, $end-$beg+1, $head.$data.$tail;
624 if ($beg == 0 && $end == (@lines-1)) {
625 $self->ct_unlink($file) if $options{delete};
628 splice @lines, $beg, $end-$beg+1, $head.$data.$tail;
630 $self->ct_file_set_contents($file, join("\n", @lines) . "\n");
633 my $content = join("\n", @lines);
635 if (!$content && !$data && $options{delete}) {
636 $self->ct_unlink($file);
640 $data = $head.$data.$tail;
641 if ($options{replace
}) {
642 $self->ct_file_set_contents($file, $data, $perms);
643 } elsif ($options{prepend
}) {
644 $self->ct_file_set_contents($file, $data . $content, $perms);
646 $self->ct_file_set_contents($file, $content . $data, $perms);
651 sub remove_pve_sections
{
654 my $head = "# --- BEGIN PVE ---";
655 my $tail = "# --- END PVE ---";
657 # Remove the sections enclosed with the above headers and footers.
658 # from a line (^) starting with '\h*$head'
659 # to a line (the other ^) starting with '\h*$tail' up to including that
661 return $data =~ s/^\h*\Q$head\E.*^\h*\Q$tail\E.*?$//rgms;