]> git.proxmox.com Git - pve-container.git/blob - src/PVE/LXC/Setup.pm
cleanup: remove unnecessary var
[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 die "setup error" if $? != 0;
95 }
96
97 sub template_fixup {
98 my ($self) = @_;
99
100 my $code = sub {
101 $self->{plugin}->template_fixup($self->{conf});
102 };
103 $self->protected_call($code);
104 }
105
106 sub setup_network {
107 my ($self) = @_;
108
109 my $code = sub {
110 $self->{plugin}->setup_network($self->{conf});
111 };
112 $self->protected_call($code);
113 }
114
115 sub set_hostname {
116 my ($self) = @_;
117
118 my $code = sub {
119 $self->{plugin}->set_hostname($self->{conf});
120 };
121 $self->protected_call($code);
122 }
123
124 sub set_dns {
125 my ($self) = @_;
126
127 my $code = sub {
128 $self->{plugin}->set_dns($self->{conf});
129 };
130 $self->protected_call($code);
131 }
132
133 sub setup_init {
134 my ($self) = @_;
135
136 my $code = sub {
137 $self->{plugin}->setup_init($self->{conf});
138 };
139 $self->protected_call($code);
140 }
141
142 sub set_user_password {
143 my ($self, $user, $pw) = @_;
144
145 my $code = sub {
146 $self->{plugin}->set_user_password($self->{conf}, $user, $pw);
147 };
148 $self->protected_call($code);
149 }
150
151 sub rewrite_ssh_host_keys {
152 my ($self) = @_;
153
154 my $conf = $self->{conf};
155 my $plugin = $self->{plugin};
156 my $rootdir = $self->{rootdir};
157
158 return if ! -d "$rootdir/etc/ssh";
159
160 my $keynames = {
161 rsa1 => 'ssh_host_key',
162 rsa => 'ssh_host_rsa_key',
163 dsa => 'ssh_host_dsa_key',
164 ecdsa => 'ssh_host_ecdsa_key',
165 ed25519 => 'ssh_host_ed25519_key',
166 };
167
168 my $hostname = $conf->{hostname} || 'localhost';
169 $hostname =~ s/\..*$//;
170
171 # Create temporary keys in /tmp on the host
172
173 my $keyfiles = {};
174 foreach my $keytype (keys %$keynames) {
175 my $basename = $keynames->{$keytype};
176 my $file = "/tmp/$$.$basename";
177 print "Creating SSH host key '$basename' - this may take some time ...\n";
178 my $cmd = ['ssh-keygen', '-q', '-f', $file, '-t', $keytype,
179 '-N', '', '-C', "root\@$hostname"];
180 PVE::Tools::run_command($cmd);
181 $keyfiles->{"/etc/ssh/$basename"} = PVE::Tools::file_get_contents($file);
182 $keyfiles->{"/etc/ssh/$basename.pub"} = PVE::Tools::file_get_contents("$file.pub");
183 unlink $file;
184 unlink "$file.pub";
185 }
186
187 # Write keys out in a protected call
188
189 my $code = sub {
190 foreach my $file (keys %$keyfiles) {
191 $plugin->ct_file_set_contents($file, $keyfiles->{$file});
192 }
193 };
194 $self->protected_call($code);
195 }
196
197 sub pre_start_hook {
198 my ($self) = @_;
199
200 my $code = sub {
201 # Create /fastboot to skip run fsck
202 $self->{plugin}->ct_file_set_contents('/fastboot', '');
203
204 $self->{plugin}->pre_start_hook($self->{conf});
205 };
206 $self->protected_call($code);
207 }
208
209 sub post_create_hook {
210 my ($self, $root_password) = @_;
211
212 my $code = sub {
213 $self->{plugin}->post_create_hook($self->{conf}, $root_password);
214 };
215 $self->protected_call($code);
216 $self->rewrite_ssh_host_keys();
217 }
218
219 1;