]> git.proxmox.com Git - pve-container.git/blame - src/PVE/LXC/Setup.pm
cleanup replication config update
[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
f08b2779
WB
96sub protected_call {
97 my ($self, $sub) = @_;
98
238b7e3e
DM
99 die "internal error" if !$self->{plugin};
100
f08b2779
WB
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 my $child = fork();
110 die "fork failed: $!\n" if !defined($child);
111
112 if (!$child) {
113 # avoid recursive forks
114 $self->{in_chroot} = 1;
115 $self->{plugin}->{in_chroot} = 1;
116 eval {
f08b2779
WB
117 chroot($rootdir) or die "failed to change root to: $rootdir: $!\n";
118 chdir('/') or die "failed to change to root directory\n";
119 $sub->();
120 };
121 if (my $err = $@) {
797e12e8 122 warn $err;
f08b2779
WB
123 POSIX::_exit(1);
124 }
125 POSIX::_exit(0);
126 }
127 while (waitpid($child, 0) != $child) {}
952de558
WB
128 if ($? != 0) {
129 my $method = (caller(1))[3];
130 die "error in setup task $method\n";
131 }
f08b2779
WB
132}
133
142444d5
DM
134sub template_fixup {
135 my ($self) = @_;
136
238b7e3e
DM
137 return if !$self->{plugin}; # unmanaged
138
f08b2779
WB
139 my $code = sub {
140 $self->{plugin}->template_fixup($self->{conf});
141 };
142 $self->protected_call($code);
142444d5
DM
143}
144
1c7f4f65 145sub setup_network {
55fa4e09 146 my ($self) = @_;
1c7f4f65 147
238b7e3e
DM
148 return if !$self->{plugin}; # unmanaged
149
f08b2779
WB
150 my $code = sub {
151 $self->{plugin}->setup_network($self->{conf});
152 };
153 $self->protected_call($code);
1c7f4f65
DM
154}
155
156sub set_hostname {
157 my ($self) = @_;
158
238b7e3e
DM
159 return if !$self->{plugin}; # unmanaged
160
f08b2779
WB
161 my $code = sub {
162 $self->{plugin}->set_hostname($self->{conf});
163 };
164 $self->protected_call($code);
1c7f4f65
DM
165}
166
c325b32f
DM
167sub set_dns {
168 my ($self) = @_;
169
238b7e3e
DM
170 return if !$self->{plugin}; # unmanaged
171
f08b2779
WB
172 my $code = sub {
173 $self->{plugin}->set_dns($self->{conf});
174 };
175 $self->protected_call($code);
c325b32f
DM
176}
177
d66768a2
DM
178sub setup_init {
179 my ($self) = @_;
180
238b7e3e
DM
181 return if !$self->{plugin}; # unmanaged
182
f08b2779
WB
183 my $code = sub {
184 $self->{plugin}->setup_init($self->{conf});
185 };
186 $self->protected_call($code);
d66768a2
DM
187}
188
168d6b07
DM
189sub set_user_password {
190 my ($self, $user, $pw) = @_;
238b7e3e
DM
191
192 return if !$self->{plugin}; # unmanaged
193
f08b2779
WB
194 my $code = sub {
195 $self->{plugin}->set_user_password($self->{conf}, $user, $pw);
196 };
197 $self->protected_call($code);
1c7f4f65
DM
198}
199
7ee31468
DM
200sub rewrite_ssh_host_keys {
201 my ($self) = @_;
202
238b7e3e
DM
203 return if !$self->{plugin}; # unmanaged
204
797e12e8
WB
205 my $conf = $self->{conf};
206 my $plugin = $self->{plugin};
207 my $rootdir = $self->{rootdir};
208
feb7383b 209 return if ! -d "$rootdir/etc/ssh";
797e12e8
WB
210
211 my $keynames = {
797e12e8
WB
212 rsa => 'ssh_host_rsa_key',
213 dsa => 'ssh_host_dsa_key',
214 ecdsa => 'ssh_host_ecdsa_key',
215 ed25519 => 'ssh_host_ed25519_key',
216 };
217
218 my $hostname = $conf->{hostname} || 'localhost';
219 $hostname =~ s/\..*$//;
52a2d3f8 220 my $ssh_comment = "root\@$hostname";
797e12e8 221
52a2d3f8
FG
222 my $keygen_outfunc = sub {
223 my $line = shift;
224
225 print "done: $line\n"
226 if ($line =~ m/^([0-9a-f]{2}:)+[0-9a-f]{2}\s+\Q$ssh_comment\E$/i);
227 };
797e12e8 228
52a2d3f8 229 # Create temporary keys in /tmp on the host
797e12e8
WB
230 my $keyfiles = {};
231 foreach my $keytype (keys %$keynames) {
232 my $basename = $keynames->{$keytype};
233 my $file = "/tmp/$$.$basename";
234 print "Creating SSH host key '$basename' - this may take some time ...\n";
52a2d3f8
FG
235 my $cmd = ['ssh-keygen', '-f', $file, '-t', $keytype,
236 '-N', '', '-C', $ssh_comment];
237 PVE::Tools::run_command($cmd, outfunc => $keygen_outfunc);
39db5e47
WB
238 $keyfiles->{"/etc/ssh/$basename"} = [PVE::Tools::file_get_contents($file), 0600];
239 $keyfiles->{"/etc/ssh/$basename.pub"} = [PVE::Tools::file_get_contents("$file.pub"), 0644];
797e12e8
WB
240 unlink $file;
241 unlink "$file.pub";
242 }
243
244 # Write keys out in a protected call
245
f08b2779 246 my $code = sub {
797e12e8 247 foreach my $file (keys %$keyfiles) {
39db5e47 248 $plugin->ct_file_set_contents($file, @{$keyfiles->{$file}});
797e12e8 249 }
f08b2779
WB
250 };
251 $self->protected_call($code);
7ee31468
DM
252}
253
d66768a2
DM
254sub pre_start_hook {
255 my ($self) = @_;
256
238b7e3e
DM
257 return if !$self->{plugin}; # unmanaged
258
f08b2779
WB
259 my $code = sub {
260 # Create /fastboot to skip run fsck
261 $self->{plugin}->ct_file_set_contents('/fastboot', '');
2988dbbc 262
f08b2779
WB
263 $self->{plugin}->pre_start_hook($self->{conf});
264 };
265 $self->protected_call($code);
d66768a2
DM
266}
267
268sub post_create_hook {
f36ce482 269 my ($self, $root_password, $ssh_keys) = @_;
1c7f4f65 270
238b7e3e
DM
271 return if !$self->{plugin}; # unmanaged
272
f08b2779 273 my $code = sub {
f36ce482 274 $self->{plugin}->post_create_hook($self->{conf}, $root_password, $ssh_keys);
f08b2779
WB
275 };
276 $self->protected_call($code);
797e12e8 277 $self->rewrite_ssh_host_keys();
1c7f4f65
DM
278}
279
2801;