]>
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
;
18 debian
=> 'PVE::LXC::Setup::Debian',
19 ubuntu
=> 'PVE::LXC::Setup::Ubuntu',
20 centos
=> 'PVE::LXC::Setup::CentOS',
21 fedora
=> 'PVE::LXC::Setup::Fedora',
22 opensuse
=> 'PVE::LXC::Setup::SUSE',
23 archlinux
=> 'PVE::LXC::Setup::ArchLinux',
24 alpine
=> 'PVE::LXC::Setup::Alpine',
25 gentoo
=> 'PVE::LXC::Setup::Gentoo',
28 my $autodetect_type = sub {
31 my $lsb_fn = "$rootdir/etc/lsb-release";
33 my $data = PVE
::Tools
::file_get_contents
($lsb_fn);
34 if ($data =~ m/^DISTRIB_ID=Ubuntu$/im) {
39 if (-f
"$rootdir/etc/debian_version") {
41 } elsif (-f
"$rootdir/etc/SuSE-brand" || -f
"$rootdir/etc/SuSE-release") {
43 } elsif (-f
"$rootdir/etc/fedora-release") {
45 } elsif (-f
"$rootdir/etc/centos-release" || -f
"$rootdir/etc/redhat-release") {
47 } elsif (-f
"$rootdir/etc/arch-release") {
49 } elsif (-f
"$rootdir/etc/alpine-release") {
51 } elsif (-f
"$rootdir/etc/gentoo-release") {
54 die "unable to detect OS distribution\n";
58 my ($class, $conf, $rootdir, $type) = @_;
60 die "no root directory\n" if !$rootdir || $rootdir eq '/';
62 my $self = bless { conf
=> $conf, rootdir
=> $rootdir};
64 if ($conf->{ostype
} && $conf->{ostype
} eq 'unmanaged') {
66 } elsif (!defined($type)) {
67 # try to autodetect type
68 $type = &$autodetect_type($rootdir);
69 my $expected_type = $conf->{ostype
} || $type;
71 die "got unexpected ostype ($type != $expected_type)\n"
72 if $type ne $expected_type;
75 my $plugin_class = $plugins->{$type} ||
76 "no such OS type '$type'\n";
78 my $plugin = $plugin_class->new($conf, $rootdir);
79 $self->{plugin
} = $plugin;
80 $self->{in_chroot
} = 0;
82 # Cache some host files we need access to:
83 $plugin->{host_resolv_conf
} = PVE
::INotify
::read_file
('resolvconf');
85 # pass on user namespace information:
86 my ($id_map, $rootuid, $rootgid) = PVE
::LXC
::parse_id_maps
($conf);
88 $plugin->{id_map
} = $id_map;
89 $plugin->{rootuid
} = $rootuid;
90 $plugin->{rootgid
} = $rootgid;
97 my ($self, $sub) = @_;
99 die "internal error" if !$self->{plugin
};
102 return $sub->() if $self->{in_chroot
};
104 my $rootdir = $self->{rootdir
};
105 if (!-d
"$rootdir/dev" && !mkdir("$rootdir/dev")) {
106 die "failed to create temporary /dev directory: $!\n";
110 die "fork failed: $!\n" if !defined($child);
113 # avoid recursive forks
114 $self->{in_chroot
} = 1;
115 $self->{plugin
}->{in_chroot
} = 1;
117 chroot($rootdir) or die "failed to change root to: $rootdir: $!\n";
118 chdir('/') or die "failed to change to root directory\n";
127 while (waitpid($child, 0) != $child) {}
129 my $method = (caller(1))[3];
130 die "error in setup task $method\n";
137 return if !$self->{plugin
}; # unmanaged
140 $self->{plugin
}->template_fixup($self->{conf
});
142 $self->protected_call($code);
148 return if !$self->{plugin
}; # unmanaged
151 $self->{plugin
}->setup_network($self->{conf
});
153 $self->protected_call($code);
159 return if !$self->{plugin
}; # unmanaged
162 $self->{plugin
}->set_hostname($self->{conf
});
164 $self->protected_call($code);
170 return if !$self->{plugin
}; # unmanaged
173 $self->{plugin
}->set_dns($self->{conf
});
175 $self->protected_call($code);
181 return if !$self->{plugin
}; # unmanaged
184 $self->{plugin
}->setup_init($self->{conf
});
186 $self->protected_call($code);
189 sub set_user_password
{
190 my ($self, $user, $pw) = @_;
192 return if !$self->{plugin
}; # unmanaged
195 $self->{plugin
}->set_user_password($self->{conf
}, $user, $pw);
197 $self->protected_call($code);
200 sub rewrite_ssh_host_keys
{
203 return if !$self->{plugin
}; # unmanaged
205 my $conf = $self->{conf
};
206 my $plugin = $self->{plugin
};
207 my $rootdir = $self->{rootdir
};
209 return if ! -d
"$rootdir/etc/ssh";
212 rsa
=> 'ssh_host_rsa_key',
213 dsa
=> 'ssh_host_dsa_key',
214 ecdsa
=> 'ssh_host_ecdsa_key',
215 ed25519
=> 'ssh_host_ed25519_key',
218 my $hostname = $conf->{hostname
} || 'localhost';
219 $hostname =~ s/\..*$//;
220 my $ssh_comment = "root\@$hostname";
222 my $keygen_outfunc = sub {
225 print "done: $line\n"
226 if $line =~ m/^(?:[0-9a-f]{2}:)+[0-9a-f]{2}\s+\Q$ssh_comment\E$/i ||
227 $line =~ m/^SHA256:[0-9a-z+\/]{43}\s
+\Q
$ssh_comment\E$/i;
230 # Create temporary keys in /tmp on the host
232 foreach my $keytype (keys %$keynames) {
233 my $basename = $keynames->{$keytype};
234 my $file = "/tmp/$$.$basename";
235 print "Creating SSH host key '$basename' - this may take some time ...\n";
236 my $cmd = ['ssh-keygen', '-f', $file, '-t', $keytype,
237 '-N', '', '-E', 'sha256', '-C', $ssh_comment];
238 PVE
::Tools
::run_command
($cmd, outfunc
=> $keygen_outfunc);
239 $keyfiles->{"/etc/ssh/$basename"} = [PVE
::Tools
::file_get_contents
($file), 0600];
240 $keyfiles->{"/etc/ssh/$basename.pub"} = [PVE
::Tools
::file_get_contents
("$file.pub"), 0644];
245 # Write keys out in a protected call
248 foreach my $file (keys %$keyfiles) {
249 $plugin->ct_file_set_contents($file, @{$keyfiles->{$file}});
252 $self->protected_call($code);
258 return if !$self->{plugin
}; # unmanaged
261 # Create /fastboot to skip run fsck
262 $self->{plugin
}->ct_file_set_contents('/fastboot', '');
264 $self->{plugin
}->pre_start_hook($self->{conf
});
266 $self->protected_call($code);
269 sub post_create_hook
{
270 my ($self, $root_password, $ssh_keys) = @_;
272 return if !$self->{plugin
}; # unmanaged
275 $self->{plugin
}->post_create_hook($self->{conf
}, $root_password, $ssh_keys);
277 $self->protected_call($code);
278 $self->rewrite_ssh_host_keys();