]> git.proxmox.com Git - pve-container.git/blob - src/PVE/LXC/Setup.pm
f05c32ea3eeb49b468ca5987999f4714724deff6
[pve-container.git] / src / PVE / LXC / Setup.pm
1 package PVE::LXC::Setup;
2
3 use strict;
4 use warnings;
5 use POSIX;
6 use PVE::Tools;
7
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;
16
17 my $plugins = {
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 arch => 'PVE::LXC::Setup::ArchLinux',
25 alpine => 'PVE::LXC::Setup::Alpine',
26 gentoo => 'PVE::LXC::Setup::Gentoo',
27 };
28
29 my $autodetect_type = sub {
30 my ($rootdir) = @_;
31
32 my $lsb_fn = "$rootdir/etc/lsb-release";
33 if (-f $lsb_fn) {
34 my $data = PVE::Tools::file_get_contents($lsb_fn);
35 if ($data =~ m/^DISTRIB_ID=Ubuntu$/im) {
36 return 'ubuntu';
37 }
38 }
39
40 if (-f "$rootdir/etc/debian_version") {
41 return "debian";
42 } elsif (-f "$rootdir/etc/SuSE-brand" || -f "$rootdir/etc/SuSE-release") {
43 return "opensuse";
44 } elsif (-f "$rootdir/etc/fedora-release") {
45 return "fedora";
46 } elsif (-f "$rootdir/etc/centos-release" || -f "$rootdir/etc/redhat-release") {
47 return "centos";
48 } elsif (-f "$rootdir/etc/arch-release") {
49 return "archlinux";
50 } elsif (-f "$rootdir/etc/alpine-release") {
51 return "alpine";
52 } elsif (-f "$rootdir/etc/gentoo-release") {
53 return "gentoo";
54 }
55 die "unable to detect OS distribution\n";
56 };
57
58 sub new {
59 my ($class, $conf, $rootdir, $type) = @_;
60
61 die "no root directory\n" if !$rootdir || $rootdir eq '/';
62
63 my $self = bless { conf => $conf, rootdir => $rootdir};
64
65 if ($conf->{ostype} && $conf->{ostype} eq 'unmanaged') {
66 return $self;
67 } elsif (!defined($type)) {
68 # try to autodetect type
69 $type = &$autodetect_type($rootdir);
70 my $expected_type = $conf->{ostype} || $type;
71
72 die "got unexpected ostype ($type != $expected_type)\n"
73 if $type ne $expected_type;
74 }
75
76 my $plugin_class = $plugins->{$type} ||
77 "no such OS type '$type'\n";
78
79 my $plugin = $plugin_class->new($conf, $rootdir);
80 $self->{plugin} = $plugin;
81 $self->{in_chroot} = 0;
82
83 # Cache some host files we need access to:
84 $plugin->{host_resolv_conf} = PVE::INotify::read_file('resolvconf');
85
86 # pass on user namespace information:
87 my ($id_map, $rootuid, $rootgid) = PVE::LXC::parse_id_maps($conf);
88 if (@$id_map) {
89 $plugin->{id_map} = $id_map;
90 $plugin->{rootuid} = $rootuid;
91 $plugin->{rootgid} = $rootgid;
92 }
93
94 return $self;
95 }
96
97 # Forks into a chroot and executes $sub
98 sub protected_call {
99 my ($self, $sub) = @_;
100
101 # avoid recursion:
102 return $sub->() if $self->{in_chroot};
103
104 my $rootdir = $self->{rootdir};
105 if (!-d "$rootdir/dev" && !mkdir("$rootdir/dev")) {
106 die "failed to create temporary /dev directory: $!\n";
107 }
108
109 pipe(my $res_in, my $res_out) or die "pipe failed: $!\n";
110
111 my $child = fork();
112 die "fork failed: $!\n" if !defined($child);
113
114 if (!$child) {
115 close($res_in);
116 # avoid recursive forks
117 $self->{in_chroot} = 1;
118 eval {
119 chroot($rootdir) or die "failed to change root to: $rootdir: $!\n";
120 chdir('/') or die "failed to change to root directory\n";
121 my $res = $sub->();
122 if (defined($res)) {
123 print {$res_out} "$res";
124 $res_out->flush();
125 }
126 };
127 if (my $err = $@) {
128 warn $err;
129 POSIX::_exit(1);
130 }
131 POSIX::_exit(0);
132 }
133 close($res_out);
134 my $result = do { local $/ = undef; <$res_in> };
135 while (waitpid($child, 0) != $child) {}
136 if ($? != 0) {
137 my $method = (caller(1))[3];
138 die "error in setup task $method\n";
139 }
140 return $result;
141 }
142
143 sub template_fixup {
144 my ($self) = @_;
145
146 return if !$self->{plugin}; # unmanaged
147
148 my $code = sub {
149 $self->{plugin}->template_fixup($self->{conf});
150 };
151 $self->protected_call($code);
152 }
153
154 sub setup_network {
155 my ($self) = @_;
156
157 return if !$self->{plugin}; # unmanaged
158
159 my $code = sub {
160 $self->{plugin}->setup_network($self->{conf});
161 };
162 $self->protected_call($code);
163 }
164
165 sub set_hostname {
166 my ($self) = @_;
167
168 return if !$self->{plugin}; # unmanaged
169
170 my $code = sub {
171 $self->{plugin}->set_hostname($self->{conf});
172 };
173 $self->protected_call($code);
174 }
175
176 sub set_dns {
177 my ($self) = @_;
178
179 return if !$self->{plugin}; # unmanaged
180
181 my $code = sub {
182 $self->{plugin}->set_dns($self->{conf});
183 };
184 $self->protected_call($code);
185 }
186
187 sub setup_init {
188 my ($self) = @_;
189
190 return if !$self->{plugin}; # unmanaged
191
192 my $code = sub {
193 $self->{plugin}->setup_init($self->{conf});
194 };
195 $self->protected_call($code);
196 }
197
198 sub set_user_password {
199 my ($self, $user, $pw) = @_;
200
201 return if !$self->{plugin}; # unmanaged
202
203 my $code = sub {
204 $self->{plugin}->set_user_password($self->{conf}, $user, $pw);
205 };
206 $self->protected_call($code);
207 }
208
209 sub rewrite_ssh_host_keys {
210 my ($self) = @_;
211
212 return if !$self->{plugin}; # unmanaged
213
214 my $conf = $self->{conf};
215 my $plugin = $self->{plugin};
216 my $rootdir = $self->{rootdir};
217
218 return if ! -d "$rootdir/etc/ssh";
219
220 my $keynames = {
221 rsa => 'ssh_host_rsa_key',
222 dsa => 'ssh_host_dsa_key',
223 ecdsa => 'ssh_host_ecdsa_key',
224 ed25519 => 'ssh_host_ed25519_key',
225 };
226
227 my $hostname = $conf->{hostname} || 'localhost';
228 $hostname =~ s/\..*$//;
229 my $ssh_comment = "root\@$hostname";
230
231 my $keygen_outfunc = sub {
232 my $line = shift;
233
234 print "done: $line\n"
235 if $line =~ m/^(?:[0-9a-f]{2}:)+[0-9a-f]{2}\s+\Q$ssh_comment\E$/i ||
236 $line =~ m/^SHA256:[0-9a-z+\/]{43}\s+\Q$ssh_comment\E$/i;
237 };
238
239 # Create temporary keys in /tmp on the host
240 my $keyfiles = {};
241 foreach my $keytype (keys %$keynames) {
242 my $basename = $keynames->{$keytype};
243 my $file = "/tmp/$$.$basename";
244 print "Creating SSH host key '$basename' - this may take some time ...\n";
245 my $cmd = ['ssh-keygen', '-f', $file, '-t', $keytype,
246 '-N', '', '-E', 'sha256', '-C', $ssh_comment];
247 PVE::Tools::run_command($cmd, outfunc => $keygen_outfunc);
248 $keyfiles->{"/etc/ssh/$basename"} = [PVE::Tools::file_get_contents($file), 0600];
249 $keyfiles->{"/etc/ssh/$basename.pub"} = [PVE::Tools::file_get_contents("$file.pub"), 0644];
250 unlink $file;
251 unlink "$file.pub";
252 }
253
254 # Write keys out in a protected call
255
256 my $code = sub {
257 foreach my $file (keys %$keyfiles) {
258 $plugin->ct_file_set_contents($file, @{$keyfiles->{$file}});
259 }
260 };
261 $self->protected_call($code);
262 }
263
264 sub pre_start_hook {
265 my ($self) = @_;
266
267 return if !$self->{plugin}; # unmanaged
268
269 my $code = sub {
270 # Create /fastboot to skip run fsck
271 $self->{plugin}->ct_file_set_contents('/fastboot', '');
272
273 $self->{plugin}->pre_start_hook($self->{conf});
274 };
275 $self->protected_call($code);
276 }
277
278 sub post_create_hook {
279 my ($self, $root_password, $ssh_keys) = @_;
280
281 return if !$self->{plugin}; # unmanaged
282
283 my $code = sub {
284 $self->{plugin}->post_create_hook($self->{conf}, $root_password, $ssh_keys);
285 };
286 $self->protected_call($code);
287 $self->rewrite_ssh_host_keys();
288 }
289
290 1;