]>
git.proxmox.com Git - pve-container.git/blob - src/PVE/LXCSetup/Base.pm
1 package PVE
::LXCSetup
::Base
;
8 my $update_etc_hosts = sub {
9 my ($etc_hosts_data, $hostip, $oldname, $newname, $searchdomains) = @_;
16 foreach my $domain (PVE
::Tools
::split_list
($searchdomains)) {
17 $extra_names .= ' ' if $extra_names;
18 $extra_names .= "$newname.$domain";
21 foreach my $line (split(/\n/, $etc_hosts_data)) {
22 if ($line =~ m/^#/ || $line =~ m/^\s*$/) {
27 my ($ip, @names) = split(/\s+/, $line);
28 if (($ip eq '127.0.0.1') || ($ip eq '::1')) {
34 foreach my $name (@names) {
35 if ($name eq $oldname || $name eq $newname) {
38 # fixme: record extra names?
41 $found = 1 if defined($hostip) && ($ip eq $hostip);
45 if (defined($hostip)) {
46 push @lines, "$hostip $extra_names $newname";
48 push @lines, "127.0.1.1 $newname";
59 if (defined($hostip)) {
60 push @lines, "$hostip $extra_names $newname";
62 push @lines, "127.0.1.1 $newname";
66 my $found_localhost = 0;
67 foreach my $line (@lines) {
68 if ($line =~ m/^127.0.0.1\s/) {
74 if (!$found_localhost) {
75 unshift @lines, "127.0.0.1 localhost.localnet localhost";
78 $etc_hosts_data = join("\n", @lines) . "\n";
80 return $etc_hosts_data;
84 my ($class, $conf) = @_;
86 my $nameserver = $conf->{'pve.nameserver'};
87 my $searchdomains = $conf->{'pve.searchdomain'};
89 my $host_resolv_conf = PVE
::INotify
::read_file
('resolvconf');
91 if (!($nameserver && $searchdomains)) {
92 $searchdomains = $host_resolv_conf->{search
};
95 foreach my $k ("dns1", "dns2", "dns3") {
96 if (my $ns = $host_resolv_conf->{$k}) {
100 $nameserver = join(' ', @list);
103 my $rootfs = $conf->{'lxc.rootfs'};
105 my $filename = "$rootfs/etc/resolv.conf";
109 $data .= "search " . join(' ', PVE
::Tools
::split_list
($searchdomains)) . "\n"
112 foreach my $ns ( PVE
::Tools
::split_list
($nameserver)) {
113 $data .= "nameserver $ns\n";
116 PVE
::Tools
::file_set_contents
($filename, $data);
120 my ($class, $conf) = @_;
122 my $hostname = $conf->{'lxc.utsname'} || 'localhost';
124 $hostname =~ s/\..*$//;
126 my $rootfs = $conf->{'lxc.rootfs'};
128 my $hostname_fn = "$rootfs/etc/hostname";
130 my $oldname = PVE
::Tools
::file_read_firstline
($hostname_fn) || 'localhost';
132 my $hosts_fn = "$rootfs/etc/hosts";
133 my $etc_hosts_data = '';
136 $etc_hosts_data = PVE
::Tools
::file_get_contents
($hosts_fn);
139 my ($ipv4, $ipv6) = PVE
::LXC
::get_primary_ips
($conf);
140 my $hostip = $ipv4 || $ipv6;
142 $etc_hosts_data = &$update_etc_hosts($etc_hosts_data, $hostip, $oldname,
143 $hostname, $conf->{'pve.searchdomain'});
145 PVE
::Tools
::file_set_contents
($hostname_fn, "$hostname\n");
146 PVE
::Tools
::file_set_contents
($hosts_fn, $etc_hosts_data);
150 my ($class, $conf) = @_;
152 die "please implement this inside subclass"
156 my ($class, $conf) = @_;
158 die "please implement this inside subclass"
162 my ($class, $conf) = @_;
164 $class->setup_init($conf);
165 $class->setup_network($conf);
166 $class->set_hostname($conf);
167 $class->set_dns($conf);
172 sub post_create_hook
{
173 my ($class, $conf) = @_;
175 $class->setup_init($conf);
176 $class->setup_network($conf);
177 $class->set_hostname($conf);
178 $class->set_dns($conf);