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