]>
git.proxmox.com Git - pve-container.git/blob - src/PVE/LXCSetup/Base.pm
751cf86a16bbc1d2bb0acda99cc336ff0cd9a6cf
1 package PVE
::LXCSetup
::Base
;
15 my ($class, $conf) = @_;
17 return bless { conf
=> $conf }, $class;
20 my $lookup_dns_conf = sub {
23 my $nameserver = $conf->{'pve.nameserver'};
24 my $searchdomains = $conf->{'pve.searchdomain'};
26 if (!($nameserver && $searchdomains)) {
28 if ($conf->{'pve.test_mode'}) {
30 $nameserver = "8.8.8.8 8.8.8.9";
31 $searchdomains = "promxox.com";
35 my $host_resolv_conf = PVE
::INotify
::read_file
('resolvconf');
37 $searchdomains = $host_resolv_conf->{search
};
40 foreach my $k ("dns1", "dns2", "dns3") {
41 if (my $ns = $host_resolv_conf->{$k}) {
45 $nameserver = join(' ', @list);
49 return ($searchdomains, $nameserver);
52 my $update_etc_hosts = sub {
53 my ($etc_hosts_data, $hostip, $oldname, $newname, $searchdomains) = @_;
60 foreach my $domain (PVE
::Tools
::split_list
($searchdomains)) {
61 $extra_names .= ' ' if $extra_names;
62 $extra_names .= "$newname.$domain";
65 foreach my $line (split(/\n/, $etc_hosts_data)) {
66 if ($line =~ m/^#/ || $line =~ m/^\s*$/) {
71 my ($ip, @names) = split(/\s+/, $line);
72 if (($ip eq '127.0.0.1') || ($ip eq '::1')) {
78 foreach my $name (@names) {
79 if ($name eq $oldname || $name eq $newname) {
82 # fixme: record extra names?
85 $found = 1 if defined($hostip) && ($ip eq $hostip);
89 if (defined($hostip)) {
90 push @lines, "$hostip $extra_names $newname";
92 push @lines, "127.0.1.1 $newname";
103 if (defined($hostip)) {
104 push @lines, "$hostip $extra_names $newname";
106 push @lines, "127.0.1.1 $newname";
110 my $found_localhost = 0;
111 foreach my $line (@lines) {
112 if ($line =~ m/^127.0.0.1\s/) {
113 $found_localhost = 1;
118 if (!$found_localhost) {
119 unshift @lines, "127.0.0.1 localhost.localnet localhost";
122 $etc_hosts_data = join("\n", @lines) . "\n";
124 return $etc_hosts_data;
128 my ($self, $conf) = @_;
130 my ($searchdomains, $nameserver) = &$lookup_dns_conf($conf);
132 my $rootfs = $conf->{'lxc.rootfs'};
134 my $filename = "$rootfs/etc/resolv.conf";
138 $data .= "search " . join(' ', PVE
::Tools
::split_list
($searchdomains)) . "\n"
141 foreach my $ns ( PVE
::Tools
::split_list
($nameserver)) {
142 $data .= "nameserver $ns\n";
145 PVE
::Tools
::file_set_contents
($filename, $data);
149 my ($self, $conf) = @_;
151 my $hostname = $conf->{'lxc.utsname'} || 'localhost';
153 $hostname =~ s/\..*$//;
155 my $rootfs = $conf->{'lxc.rootfs'};
157 my $hostname_fn = "$rootfs/etc/hostname";
159 my $oldname = PVE
::Tools
::file_read_firstline
($hostname_fn) || 'localhost';
161 my $hosts_fn = "$rootfs/etc/hosts";
162 my $etc_hosts_data = '';
165 $etc_hosts_data = PVE
::Tools
::file_get_contents
($hosts_fn);
168 my ($ipv4, $ipv6) = PVE
::LXC
::get_primary_ips
($conf);
169 my $hostip = $ipv4 || $ipv6;
171 my ($searchdomains) = &$lookup_dns_conf($conf);
173 $etc_hosts_data = &$update_etc_hosts($etc_hosts_data, $hostip, $oldname,
174 $hostname, $searchdomains);
176 PVE
::Tools
::file_set_contents
($hostname_fn, "$hostname\n");
177 PVE
::Tools
::file_set_contents
($hosts_fn, $etc_hosts_data);
181 my ($self, $conf) = @_;
183 die "please implement this inside subclass"
187 my ($self, $conf) = @_;
189 die "please implement this inside subclass"
192 my $replacepw = sub {
193 my ($file, $user, $epw) = @_;
195 my $tmpfile = "$file.$$";
198 my $src = IO
::File-
>new("<$file") ||
199 die "unable to open file '$file' - $!";
201 my $st = File
::stat::stat($src) ||
202 die "unable to stat file - $!";
204 my $dst = IO
::File-
>new(">$tmpfile") ||
205 die "unable to open file '$tmpfile' - $!";
207 # copy owner and permissions
208 chmod $st->mode, $dst;
209 chown $st->uid, $st->gid, $dst;
211 while (defined (my $line = <$src>)) {
212 $line =~ s/^${user}:[^:]*:/${user}:${epw}:/;
216 $src->close() || die "close '$file' failed - $!\n";
217 $dst->close() || die "close '$tmpfile' failed - $!\n";
222 rename $tmpfile, $file;
223 unlink $tmpfile; # in case rename fails
227 sub set_user_password
{
228 my ($self, $conf, $user, $opt_password) = @_;
230 my $rootfs = $conf->{'lxc.rootfs'};
232 my $pwfile = "$rootfs/etc/passwd";
234 return if ! -f
$pwfile;
236 my $shadow = "$rootfs/etc/shadow";
238 if (defined($opt_password)) {
239 if ($opt_password !~ m/^\$/) {
240 my $time = substr (Digest
::SHA
::sha1_base64
(time), 0, 8);
241 $opt_password = crypt(encode
("utf8", $opt_password), "\$1\$$time\$");
248 &$replacepw ($shadow, $user, $opt_password);
249 &$replacepw ($pwfile, $user, 'x');
251 &$replacepw ($pwfile, $user, $opt_password);
256 my ($self, $conf) = @_;
258 $self->setup_init($conf);
259 $self->setup_network($conf);
260 $self->set_hostname($conf);
261 $self->set_dns($conf);
266 sub post_create_hook
{
267 my ($self, $conf, $root_password) = @_;
269 $self->set_user_password($conf, 'root', $root_password);
270 $self->setup_init($conf);
271 $self->setup_network($conf);
272 $self->set_hostname($conf);
273 $self->set_dns($conf);