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