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