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