1 package PVE
::LXC
::Setup
::Base
;
20 my ($class, $conf, $rootdir) = @_;
22 return bless { conf
=> $conf, rootdir
=> $rootdir }, $class;
26 my ($self, $conf) = @_;
28 my $nameserver = $conf->{nameserver
};
29 my $searchdomains = $conf->{searchdomain
};
31 if (!($nameserver && $searchdomains)) {
33 if ($conf->{'testmode'}) {
35 $nameserver = "8.8.8.8 8.8.8.9";
36 $searchdomains = "proxmox.com";
40 my $host_resolv_conf = $self->{host_resolv_conf
};
42 $searchdomains = $host_resolv_conf->{search
};
45 foreach my $k ("dns1", "dns2", "dns3") {
46 if (my $ns = $host_resolv_conf->{$k}) {
50 $nameserver = join(' ', @list);
54 return ($searchdomains, $nameserver);
57 sub update_etc_hosts
{
58 my ($self, $hostip, $oldname, $newname, $searchdomains) = @_;
62 my $namepart = ($newname =~ s/\..*$//r);
65 if ($newname =~ /\./) {
66 $all_names .= "$newname $namepart";
68 foreach my $domain (PVE
::Tools
::split_list
($searchdomains)) {
69 $all_names .= ' ' if $all_names;
70 $all_names .= "$newname.$domain";
72 $all_names .= ' ' if $all_names;
73 $all_names .= $newname;
78 my $hosts_fn = '/etc/hosts';
80 my $lo4 = "127.0.0.1 localhost.localnet localhost\n";
81 my $lo6 = "::1 localhost.localnet localhost\n";
82 if ($self->ct_file_exists($hosts_fn)) {
83 my $data = $self->ct_file_get_contents($hosts_fn);
84 # don't take localhost entries within our hosts sections into account
85 $data = remove_pve_sections
($data);
87 # check for existing localhost entries
88 $section .= $lo4 if $data !~ /^\h*127\.0\.0\.1\h+/m;
89 $section .= $lo6 if $data !~ /^\h*::1\h+/m;
91 $section .= $lo4 . $lo6;
94 if (defined($hostip)) {
95 $section .= "$hostip $all_names\n";
97 $section .= "127.0.1.1 $namepart\n";
100 $self->ct_modify_file($hosts_fn, $section);
104 my ($self, $conf) = @_;
106 # do nothing by default
110 my ($self, $conf) = @_;
112 my ($searchdomains, $nameserver) = $self->lookup_dns_conf($conf);
116 $data .= "search " . join(' ', PVE
::Tools
::split_list
($searchdomains)) . "\n"
119 foreach my $ns ( PVE
::Tools
::split_list
($nameserver)) {
120 $data .= "nameserver $ns\n";
123 $self->ct_modify_file("/etc/resolv.conf", $data, replace
=> 1);
127 my ($self, $conf) = @_;
129 my $hostname = $conf->{hostname
} || 'localhost';
131 my $namepart = ($hostname =~ s/\..*$//r);
133 my $hostname_fn = "/etc/hostname";
135 my $oldname = $self->ct_file_read_firstline($hostname_fn) || 'localhost';
137 my ($ipv4, $ipv6) = PVE
::LXC
::get_primary_ips
($conf);
138 my $hostip = $ipv4 || $ipv6;
140 my ($searchdomains) = $self->lookup_dns_conf($conf);
142 $self->update_etc_hosts($hostip, $oldname, $hostname, $searchdomains);
144 $self->ct_file_set_contents($hostname_fn, "$namepart\n");
148 my ($self, $conf) = @_;
150 die "please implement this inside subclass"
154 my ($self, $conf) = @_;
156 die "please implement this inside subclass"
159 sub setup_systemd_console
{
160 my ($self, $conf) = @_;
162 my $systemd_dir_rel = $self->ct_is_executable("/lib/systemd/systemd") ?
163 "/lib/systemd/system" : "/usr/lib/systemd/system";
165 my $systemd_getty_service_rel = "$systemd_dir_rel/getty\@.service";
167 return if !$self->ct_file_exists($systemd_getty_service_rel);
169 my $raw = $self->ct_file_get_contents($systemd_getty_service_rel);
171 my $systemd_container_getty_service_rel = "$systemd_dir_rel/container-getty\@.service";
173 # systemd on CenoOS 7.1 is too old (version 205), so there is no
174 # container-getty service
175 if (!$self->ct_file_exists($systemd_container_getty_service_rel)) {
176 if ($raw =~ s!^ConditionPathExists=/dev/tty0$!ConditionPathExists=/dev/tty!m) {
177 $self->ct_file_set_contents($systemd_getty_service_rel, $raw);
180 # undo above change (in case someone updated systemd)
181 if ($raw =~ s!^ConditionPathExists=/dev/tty$!ConditionPathExists=/dev/tty0!m) {
182 $self->ct_file_set_contents($systemd_getty_service_rel, $raw);
186 my $ttycount = PVE
::LXC
::Config-
>get_tty_count($conf);
188 for (my $i = 1; $i < 7; $i++) {
189 my $tty_service_lnk = "/etc/systemd/system/getty.target.wants/getty\@tty$i.service";
190 if ($i > $ttycount) {
191 $self->ct_unlink($tty_service_lnk);
193 if (!$self->ct_is_symlink($tty_service_lnk)) {
194 $self->ct_unlink($tty_service_lnk);
195 $self->ct_symlink($systemd_getty_service_rel, $tty_service_lnk);
201 # A few distros as well as unprivileged containers cannot deal with the
202 # /dev/lxc/ tty subdirectory.
204 my ($self, $conf) = @_;
205 return $conf->{unprivileged
} ?
'' : 'lxc/';
208 sub setup_container_getty_service
{
209 my ($self, $conf) = @_;
211 my $systemd_dir_rel = $self->ct_is_executable("/lib/systemd/systemd") ?
212 "/lib/systemd/system" : "/usr/lib/systemd/system";
213 my $servicefile = "$systemd_dir_rel/container-getty\@.service";
214 my $raw = $self->ct_file_get_contents($servicefile);
215 my $ttyname = $self->devttydir($conf) . 'tty%I';
216 if ($raw =~ s
@pts/%I|lxc
/tty
%I@$ttyname@g) {
217 $self->ct_file_set_contents($servicefile, $raw);
221 sub setup_systemd_networkd
{
222 my ($self, $conf) = @_;
224 foreach my $k (keys %$conf) {
225 next if $k !~ m/^net(\d+)$/;
226 my $d = PVE
::LXC
::Config-
>parse_lxc_network($conf->{$k});
229 my $filename = "/etc/systemd/network/$d->{name}.network";
236 Description = Interface $d->{name} autoconfigured by PVE
240 my ($has_ipv4, $has_ipv6);
243 my @DHCPMODES = ('none', 'v4', 'v6', 'both');
244 my ($NONE, $DHCP4, $DHCP6, $BOTH) = (0, 1, 2, 3);
247 if (defined(my $ip = $d->{ip
})) {
250 } elsif ($ip ne 'manual') {
252 $data .= "Address = $ip\n";
255 if (defined(my $gw = $d->{gw
})) {
256 $data .= "Gateway = $gw\n";
257 if ($has_ipv4 && !PVE
::Network
::is_ip_in_cidr
($gw, $d->{ip
}, 4)) {
258 $routes .= "\n[Route]\nDestination = $gw/32\nScope = link\n";
262 if (defined(my $ip = $d->{ip6
})) {
265 } elsif ($ip ne 'manual') {
267 $data .= "Address = $ip\n";
270 if (defined(my $gw = $d->{gw6
})) {
271 $data .= "Gateway = $gw\n";
272 if ($has_ipv6 && !PVE
::Network
::is_ip_in_cidr
($gw, $d->{ip6
}, 6) &&
273 !PVE
::Network
::is_ip_in_cidr
($gw, 'fe80::/10', 6)) {
274 $routes .= "\n[Route]\nDestination = $gw/128\nScope = link\n";
278 $data .= "DHCP = $DHCPMODES[$dhcp]\n";
279 $data .= $routes if $routes;
281 $self->ct_file_set_contents($filename, $data);
285 sub setup_securetty
{
286 my ($self, $conf, @add) = @_;
288 my $filename = "/etc/securetty";
289 # root login is already allowed on every device if no securetty present
290 return if !$self->ct_file_exists($filename);
293 @add = qw(console tty1 tty2 tty3 tty4);
294 if (my $dir = $self->devttydir($conf)) {
295 @add = map { "${dir}$_" } @add;
299 my $data = $self->ct_file_get_contents($filename);
300 chomp $data; $data .= "\n";
301 foreach my $dev (@add) {
302 if ($data !~ m!^\Q$dev\E\s*$!m) {
306 $self->ct_file_set_contents($filename, $data);
309 my $replacepw = sub {
310 my ($self, $file, $user, $epw, $shadow) = @_;
312 my $tmpfile = "$file.$$";
315 my $src = $self->ct_open_file_read($file) ||
316 die "unable to open file '$file' - $!";
318 my $st = $self->ct_stat($src) ||
319 die "unable to stat file - $!";
321 my $dst = $self->ct_open_file_write($tmpfile) ||
322 die "unable to open file '$tmpfile' - $!";
324 # copy owner and permissions
325 chmod $st->mode, $dst;
326 chown $st->uid, $st->gid, $dst;
328 my $last_change = int(time()/(60*60*24));
330 if ($epw =~ m/^\$TEST\$/) { # for regression tests
331 $last_change = 12345;
334 while (defined (my $line = <$src>)) {
336 $line =~ s/^${user}:[^:]*:[^:]*:/${user}:${epw}:${last_change}:/;
338 $line =~ s/^${user}:[^:]*:/${user}:${epw}:/;
343 $src->close() || die "close '$file' failed - $!\n";
344 $dst->close() || die "close '$tmpfile' failed - $!\n";
347 $self->ct_unlink($tmpfile);
349 $self->ct_rename($tmpfile, $file);
350 $self->ct_unlink($tmpfile); # in case rename fails
354 sub set_user_password
{
355 my ($self, $conf, $user, $opt_password) = @_;
357 my $pwfile = "/etc/passwd";
359 return if !$self->ct_file_exists($pwfile);
361 my $shadow = "/etc/shadow";
363 if (defined($opt_password)) {
364 if ($opt_password !~ m/^\$/) {
365 my $time = substr (Digest
::SHA
::sha1_base64
(time), 0, 8);
366 $opt_password = crypt(encode
("utf8", $opt_password), "\$1\$$time\$");
372 if ($self->ct_file_exists($shadow)) {
373 &$replacepw ($self, $shadow, $user, $opt_password, 1);
374 &$replacepw ($self, $pwfile, $user, 'x');
376 &$replacepw ($self, $pwfile, $user, $opt_password);
380 my $parse_home_dir = sub {
381 my ($self, $passwdfile, $user) = @_;
383 my $fh = $self->ct_open_file_read($passwdfile);
384 while (defined (my $line = <$fh>)) {
386 if $line =~ m/^${user}:([^:]*:){4}([^:]*):/;
390 sub set_user_authorized_ssh_keys
{
391 my ($self, $conf, $user, $ssh_keys) = @_;
393 my $passwd = "/etc/passwd";
394 my $home = $user eq "root" ?
"/root/" : "/home/$user/";
396 $home = &$parse_home_dir($self, $passwd, $user)
397 if $self->ct_file_exists($passwd);
399 die "home directory '$home' of $user does not exist!"
400 if ! ($self->ct_is_directory($home) || $self->ct_is_symlink($home));
402 $self->ct_mkdir("$home/.ssh", 0700)
403 if ! $self->ct_is_directory("$home/.ssh");
405 $self->ct_modify_file("$home/.ssh/authorized_keys", $ssh_keys, perms
=> 0700);
408 my $randomize_crontab = sub {
409 my ($self, $conf) = @_;
412 # Note: dir_glob_foreach() untaints filenames!
413 PVE
::Tools
::dir_glob_foreach
("/etc/cron.d", qr/[A-Z\-\_a-z0-9]+/, sub {
415 push @files, "/etc/cron.d/$name";
418 my $crontab_fn = "/etc/crontab";
419 unshift @files, $crontab_fn if $self->ct_file_exists($crontab_fn);
421 foreach my $filename (@files) {
422 my $data = $self->ct_file_get_contents($filename);
424 foreach my $line (split(/\n/, $data)) {
425 # we only randomize minutes for root crontab entries
426 if ($line =~ m/^\d+(\s+\S+\s+\S+\s+\S+\s+\S+\s+root\s+\S.*)$/) {
428 my $min = int(rand()*59);
429 $new .= "$min$rest\n";
434 $self->ct_file_set_contents($filename, $new);
439 my ($self, $conf) = @_;
441 $self->setup_init($conf);
442 $self->setup_network($conf);
443 $self->set_hostname($conf);
444 $self->set_dns($conf);
449 sub post_create_hook
{
450 my ($self, $conf, $root_password, $ssh_keys) = @_;
452 $self->template_fixup($conf);
454 &$randomize_crontab($self, $conf);
456 $self->set_user_password($conf, 'root', $root_password);
457 $self->set_user_authorized_ssh_keys($conf, 'root', $ssh_keys) if $ssh_keys;
458 $self->setup_init($conf);
459 $self->setup_network($conf);
460 $self->set_hostname($conf);
461 $self->set_dns($conf);
466 # File access wrappers for container setup code.
467 # For user-namespace support these might need to take uid and gid maps into account.
469 sub ct_is_file_ignored
{
470 my ($self, $file) = @_;
471 my ($name, $path) = fileparse
($file);
472 return -f
"$path/.pve-ignore.$name";
475 sub ct_reset_ownership
{
476 my ($self, @files) = @_;
477 my $conf = $self->{conf
};
478 return if !$self->{id_map
};
480 @files = grep { !$self->ct_is_file_ignored($_) } @files;
483 my $uid = $self->{rootuid
};
484 my $gid = $self->{rootgid
};
485 chown($uid, $gid, @files);
489 my ($self, $file, $mask) = @_;
490 # mkdir goes by parameter count - an `undef' mode acts like a mode of 0000
491 if (defined($mask)) {
492 return CORE
::mkdir($file, $mask) && $self->ct_reset_ownership($file);
494 return CORE
::mkdir($file) && $self->ct_reset_ownership($file);
499 my ($self, @files) = @_;
500 foreach my $file (@files) {
501 next if $self->ct_is_file_ignored($file);
507 my ($self, $old, $new) = @_;
508 return if $self->ct_is_file_ignored($new);
509 CORE
::rename($old, $new);
512 sub ct_open_file_read
{
515 return IO
::File-
>new($file, O_RDONLY
, @_);
518 sub ct_open_file_write
{
521 $file = '/dev/null' if $self->ct_is_file_ignored($file);
522 my $fh = IO
::File-
>new($file, O_WRONLY
| O_CREAT
, @_);
523 $self->ct_reset_ownership($fh);
529 if ($self->{id_map
}) {
531 if (ref($opts) eq 'HASH') {
532 $opts->{owner
} = $self->{rootuid
} if !defined($self->{owner
});
533 $opts->{group
} = $self->{rootgid
} if !defined($self->{group
});
535 File
::Path
::make_path
(@_, $opts);
537 File
::Path
::make_path
(@_);
542 my ($self, $old, $new) = @_;
543 return if $self->ct_is_file_ignored($new);
544 return CORE
::symlink($old, $new);
548 my ($self, $name) = @_;
549 return CORE
::readlink($name);
553 my ($self, $file) = @_;
557 sub ct_is_directory
{
558 my ($self, $file) = @_;
563 my ($self, $file) = @_;
567 sub ct_is_executable
{
568 my ($self, $file) = @_;
573 my ($self, $file) = @_;
574 return File
::stat::stat($file);
577 sub ct_file_read_firstline
{
578 my ($self, $file) = @_;
579 return PVE
::Tools
::file_read_firstline
($file);
582 sub ct_file_get_contents
{
583 my ($self, $file) = @_;
584 return PVE
::Tools
::file_get_contents
($file);
587 sub ct_file_set_contents
{
588 my ($self, $file, $data, $perms) = @_;
589 return if $self->ct_is_file_ignored($file);
590 PVE
::Tools
::file_set_contents
($file, $data, $perms);
591 $self->ct_reset_ownership($file);
594 # Modify a marked portion of a file.
595 # Optionally if the file becomes empty it will be deleted.
597 my ($self, $file, $data, %options) = @_;
598 return if $self->ct_is_file_ignored($file);
600 my $head = "# --- BEGIN PVE ---\n";
601 my $tail = "# --- END PVE ---\n";
602 my $perms = $options{perms
};
603 $data .= "\n" if $data && $data !~ /\n$/;
605 if (!$self->ct_file_exists($file)) {
606 $self->ct_file_set_contents($file, $head.$data.$tail, $perms) if $data;
610 my $old = $self->ct_file_get_contents($file);
611 my @lines = split(/\n/, $old);
614 foreach my $i (0..(@lines-1)) {
615 my $line = $lines[$i];
616 $beg = $i if !defined($beg) &&
617 $line =~ /^#\s*---\s*BEGIN\s*PVE\s*/;
618 $end = $i if !defined($end) && defined($beg) &&
619 $line =~ /^#\s*---\s*END\s*PVE\s*/i;
620 last if defined($beg) && defined($end);
623 if (defined($beg) && defined($end)) {
627 splice @lines, $beg, $end-$beg+1, $head.$data.$tail;
629 if ($beg == 0 && $end == (@lines-1)) {
630 $self->ct_unlink($file) if $options{delete};
633 splice @lines, $beg, $end-$beg+1, $head.$data.$tail;
635 $self->ct_file_set_contents($file, join("\n", @lines) . "\n");
638 my $content = join("\n", @lines);
640 if (!$content && !$data && $options{delete}) {
641 $self->ct_unlink($file);
645 $data = $head.$data.$tail;
646 if ($options{replace
}) {
647 $self->ct_file_set_contents($file, $data, $perms);
648 } elsif ($options{prepend
}) {
649 $self->ct_file_set_contents($file, $data . $content, $perms);
651 $self->ct_file_set_contents($file, $content . $data, $perms);
656 sub remove_pve_sections
{
659 my $head = "# --- BEGIN PVE ---";
660 my $tail = "# --- END PVE ---";
662 # Remove the sections enclosed with the above headers and footers.
663 # from a line (^) starting with '\h*$head'
664 # to a line (the other ^) starting with '\h*$tail' up to including that
666 return $data =~ s/^\h*\Q$head\E.*^\h*\Q$tail\E.*?$//rgms;