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