]>
Commit | Line | Data |
---|---|---|
8ab34b87 DM |
1 | package PVE::DAB; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | use IO::File; | |
6 | use File::Path; | |
7 | use File::Basename; | |
8 | use IO::Select; | |
9 | use IPC::Open2; | |
10 | use IPC::Open3; | |
11 | use POSIX qw (LONG_MAX); | |
f0134ed2 DM |
12 | use UUID; |
13 | use Cwd; | |
8ab34b87 DM |
14 | |
15 | # fixme: lock container ? | |
16 | ||
17 | my $dablibdir = "/usr/lib/dab"; | |
18 | my $devicetar = "$dablibdir/devices.tar.gz"; | |
19 | my $default_env = "$dablibdir/scripts/defenv"; | |
20 | my $fake_init = "$dablibdir/scripts/init.pl"; | |
21 | my $script_ssh_init = "$dablibdir/scripts/ssh_gen_host_keys"; | |
22 | my $script_mysql_randompw = "$dablibdir/scripts/mysql_randompw"; | |
23 | my $script_init_urandom = "$dablibdir/scripts/init_urandom"; | |
24 | ||
25 | my $postfix_main_cf = <<EOD; | |
26 | # See /usr/share/postfix/main.cf.dist for a commented, more complete version | |
27 | ||
28 | smtpd_banner = \$myhostname ESMTP \$mail_name (Debian/GNU) | |
29 | biff = no | |
30 | ||
31 | # appending .domain is the MUA's job. | |
32 | append_dot_mydomain = no | |
33 | ||
34 | # Uncomment the next line to generate "delayed mail" warnings | |
35 | #delay_warning_time = 4h | |
36 | ||
37 | alias_maps = hash:/etc/aliases | |
38 | alias_database = hash:/etc/aliases | |
39 | mydestination = \$myhostname, localhost.\$mydomain, localhost | |
40 | relayhost = | |
41 | mynetworks = 127.0.0.0/8 | |
42 | inet_interfaces = loopback-only | |
43 | recipient_delimiter = + | |
44 | ||
45 | EOD | |
46 | ||
47 | # produce apt compatible filenames (/var/lib/apt/lists) | |
48 | sub __url_to_filename { | |
49 | my $url = shift; | |
50 | ||
51 | $url =~ s|^\S+://||; | |
52 | $url =~ s|_|%5f|g; | |
53 | $url =~ s|/|_|g; | |
54 | ||
55 | return $url; | |
56 | } | |
57 | ||
58 | sub download { | |
59 | my ($self, $url, $path) = @_; | |
60 | ||
61 | $self->logmsg ("download: $url\n"); | |
62 | my $tmpfn = "$path.tmp$$"; | |
63 | eval { | |
64 | $self->run_command ("wget -q '$url' -O '$tmpfn'"); | |
65 | }; | |
66 | ||
67 | my $err = $@; | |
68 | if ($err) { | |
69 | unlink $tmpfn; | |
70 | die $err; | |
71 | } | |
72 | ||
73 | rename ($tmpfn, $path); | |
74 | } | |
75 | ||
76 | sub write_file { | |
77 | my ($data, $file, $perm) = @_; | |
78 | ||
79 | die "no filename" if !$file; | |
80 | ||
81 | unlink $file; | |
82 | ||
83 | my $fh = IO::File->new ($file, O_WRONLY | O_CREAT, $perm) || | |
84 | die "unable to open file '$file'"; | |
85 | ||
86 | print $fh $data; | |
87 | ||
88 | $fh->close; | |
89 | } | |
90 | ||
91 | sub read_file { | |
92 | my ($file) = @_; | |
93 | ||
94 | die "no filename" if !$file; | |
95 | ||
96 | my $fh = IO::File->new ($file) || | |
97 | die "unable to open file '$file'"; | |
98 | ||
99 | local $/; # slurp mode | |
100 | ||
101 | my $data = <$fh>; | |
102 | ||
103 | $fh->close; | |
104 | ||
105 | return $data; | |
106 | } | |
107 | ||
108 | sub read_config { | |
109 | my ($filename) = @_; | |
110 | ||
111 | my $res = {}; | |
112 | ||
113 | my $fh = IO::File->new ("<$filename") || return $res; | |
114 | my $rec = ''; | |
115 | ||
116 | while (defined (my $line = <$fh>)) { | |
117 | next if $line =~ m/^\#/; | |
118 | next if $line =~ m/^\s*$/; | |
119 | $rec .= $line; | |
120 | }; | |
121 | ||
122 | close ($fh); | |
123 | ||
124 | chomp $rec; | |
125 | $rec .= "\n"; | |
126 | ||
127 | while ($rec) { | |
128 | if ($rec =~ s/^Description:\s*([^\n]*)(\n\s+.*)*$//si) { | |
129 | $res->{headline} = $1; | |
130 | chomp $res->{headline}; | |
131 | my $long = $2; | |
132 | $long =~ s/^\s+/ /; | |
133 | $res->{description} = $long; | |
134 | chomp $res->{description}; | |
135 | } elsif ($rec =~ s/^([^:]+):\s*(.*\S)\s*\n//) { | |
136 | my ($key, $value) = (lc ($1), $2); | |
137 | if ($key eq 'source' || $key eq 'mirror') { | |
138 | push @{$res->{$key}}, $value; | |
139 | } else { | |
140 | die "duplicate key '$key'\n" if defined ($res->{$key}); | |
141 | $res->{$key} = $value; | |
142 | } | |
143 | } else { | |
144 | die "unable to parse config file: $rec"; | |
145 | } | |
146 | } | |
147 | ||
148 | die "unable to parse config file" if $rec; | |
149 | ||
150 | return $res; | |
151 | } | |
152 | ||
153 | sub run_command { | |
154 | my ($self, $cmd, $input, $getoutput) = @_; | |
155 | ||
156 | my $reader = IO::File->new(); | |
157 | my $writer = IO::File->new(); | |
158 | my $error = IO::File->new(); | |
159 | ||
160 | my $orig_pid = $$; | |
161 | ||
162 | my $cmdstr = ref ($cmd) eq 'ARRAY' ? join (' ', @$cmd) : $cmd; | |
163 | ||
164 | my $pid; | |
165 | eval { | |
166 | if (ref ($cmd) eq 'ARRAY') { | |
167 | $pid = open3 ($writer, $reader, $error, @$cmd) || die $!; | |
168 | } else { | |
169 | $pid = open3 ($writer, $reader, $error, $cmdstr) || die $!; | |
170 | } | |
171 | }; | |
172 | ||
173 | my $err = $@; | |
174 | ||
175 | # catch exec errors | |
176 | if ($orig_pid != $$) { | |
177 | $self->logmsg ("ERROR: command '$cmdstr' failed - fork failed\n"); | |
178 | POSIX::_exit (1); | |
179 | kill ('KILL', $$); | |
180 | } | |
181 | ||
182 | die $err if $err; | |
183 | ||
184 | print $writer $input if defined $input; | |
185 | close $writer; | |
186 | ||
187 | my $select = new IO::Select; | |
188 | $select->add ($reader); | |
189 | $select->add ($error); | |
190 | ||
191 | my $res = ''; | |
192 | my $logfd = $self->{logfd}; | |
193 | ||
194 | while ($select->count) { | |
195 | my @handles = $select->can_read (); | |
196 | ||
197 | foreach my $h (@handles) { | |
198 | my $buf = ''; | |
199 | my $count = sysread ($h, $buf, 4096); | |
200 | if (!defined ($count)) { | |
201 | waitpid ($pid, 0); | |
202 | die "command '$cmdstr' failed: $!"; | |
203 | } | |
204 | $select->remove ($h) if !$count; | |
205 | ||
206 | print $logfd $buf; | |
207 | ||
208 | $res .= $buf if $getoutput; | |
209 | } | |
210 | } | |
211 | ||
212 | waitpid ($pid, 0); | |
213 | my $ec = ($? >> 8); | |
214 | ||
215 | die "command '$cmdstr' failed with exit code $ec\n" if $ec; | |
216 | ||
217 | return $res; | |
218 | } | |
219 | ||
220 | sub logmsg { | |
221 | my $self = shift; | |
222 | print STDERR @_; | |
223 | $self->writelog (@_); | |
224 | } | |
225 | ||
226 | sub writelog { | |
227 | my $self = shift; | |
228 | my $fd = $self->{logfd}; | |
229 | print $fd @_; | |
230 | } | |
231 | ||
232 | sub __sample_config { | |
f0134ed2 | 233 | my ($self) = @_; |
8ab34b87 DM |
234 | |
235 | my $data = ''; | |
f0134ed2 | 236 | my $arch = $self->{config}->{architecture}; |
8ab34b87 | 237 | |
f0134ed2 | 238 | my $ostype = $self->{config}->{ostype}; |
8ab34b87 | 239 | |
f0134ed2 DM |
240 | if ($ostype =~ m/^debian-/) { |
241 | $data .= "lxc.include = /usr/share/lxc/config/debian.common.conf\n"; | |
242 | } elsif ($ostype =~ m/^ubuntu-/) { | |
243 | $data .= "lxc.include = /usr/share/lxc/config/ubuntu.common.conf\n"; | |
244 | } else { | |
245 | die "unknown os type '$ostype'\n"; | |
246 | } | |
247 | $data .= "lxc.utsname = localhost\n"; | |
248 | $data .= "lxc.rootfs = $self->{rootfs}\n"; | |
8ab34b87 DM |
249 | |
250 | return $data; | |
251 | } | |
252 | ||
253 | sub __allocate_ve { | |
254 | my ($self) = @_; | |
255 | ||
256 | my $cid; | |
257 | if (my $fd = IO::File->new (".veid")) { | |
258 | $cid = <$fd>; | |
259 | chomp $cid; | |
260 | close ($fd); | |
261 | } | |
262 | ||
f0134ed2 DM |
263 | |
264 | $self->{working_dir} = getcwd; | |
265 | $self->{veconffile} = "$self->{working_dir}/config"; | |
266 | $self->{rootfs} = "$self->{working_dir}/rootfs"; | |
8ab34b87 DM |
267 | |
268 | if ($cid) { | |
269 | $self->{veid} = $cid; | |
8ab34b87 DM |
270 | return $cid; |
271 | } | |
272 | ||
f0134ed2 DM |
273 | my $uuid; |
274 | my $uuid_str; | |
275 | UUID::generate($uuid); | |
276 | UUID::unparse($uuid, $uuid_str); | |
277 | $self->{veid} = $uuid_str; | |
8ab34b87 DM |
278 | |
279 | my $fd = IO::File->new (">.veid") || | |
280 | die "unable to write '.veid'\n"; | |
f0134ed2 | 281 | print $fd "$self->{veid}\n"; |
8ab34b87 DM |
282 | close ($fd); |
283 | ||
f0134ed2 DM |
284 | my $cdata = $self->__sample_config(); |
285 | ||
286 | my $fh = IO::File->new ($self->{veconffile}, O_WRONLY|O_CREAT|O_EXCL) || | |
287 | die "unable to write lxc config file '$self->{veconffile}' - $!"; | |
288 | print $fh $cdata; | |
289 | close ($fh); | |
290 | ||
291 | mkdir $self->{rootfs} || die "unable to create rootfs - $!"; | |
8ab34b87 | 292 | |
f0134ed2 | 293 | $self->logmsg ("allocated VE $self->{veid}\n"); |
8ab34b87 | 294 | |
f0134ed2 | 295 | return $self->{veid}; |
8ab34b87 DM |
296 | } |
297 | ||
298 | sub new { | |
299 | my ($class, $config) = @_; | |
300 | ||
301 | $class = ref ($class) || $class; | |
302 | ||
303 | my $self = {}; | |
304 | ||
305 | $config = read_config ('dab.conf') if !$config; | |
306 | ||
307 | $self->{config} = $config; | |
308 | ||
309 | bless $self, $class; | |
310 | ||
311 | $self->{logfile} = "logfile"; | |
312 | $self->{logfd} = IO::File->new (">>$self->{logfile}") || | |
313 | die "unable to open log file"; | |
314 | ||
315 | my $arch = $config->{architecture}; | |
316 | die "no 'architecture' specified\n" if !$arch; | |
317 | ||
318 | die "unsupported architecture '$arch'\n" | |
319 | if $arch !~ m/^(i386|amd64)$/; | |
320 | ||
321 | my $suite = $config->{suite} || die "no 'suite' specified\n"; | |
b6a1907e DM |
322 | if ($suite eq 'stretch') { |
323 | $config->{ostype} = "debian-9.0"; | |
324 | } elsif ($suite eq 'jessie') { | |
5d0f537e DM |
325 | $config->{ostype} = "debian-8.0"; |
326 | } elsif ($suite eq 'wheezy') { | |
632ea034 DM |
327 | $config->{ostype} = "debian-7.0"; |
328 | } elsif ($suite eq 'squeeze') { | |
8ab34b87 DM |
329 | $config->{ostype} = "debian-6.0"; |
330 | } elsif ($suite eq 'lenny') { | |
331 | $config->{ostype} = "debian-5.0"; | |
332 | } elsif ($suite eq 'etch') { | |
333 | $config->{ostype} = "debian-4.0"; | |
334 | } elsif ($suite eq 'hardy') { | |
335 | $config->{ostype} = "ubuntu-8.04"; | |
336 | } elsif ($suite eq 'intrepid') { | |
337 | $config->{ostype} = "ubuntu-8.10"; | |
338 | } elsif ($suite eq 'jaunty') { | |
339 | $config->{ostype} = "ubuntu-9.04"; | |
91be0a6a DM |
340 | } elsif ($suite eq 'precise') { |
341 | $config->{ostype} = "ubuntu-12.04"; | |
e6b5b5ac DM |
342 | } elsif ($suite eq 'trusty') { |
343 | $config->{ostype} = "ubuntu-14.04"; | |
344 | } elsif ($suite eq 'vivid') { | |
345 | $config->{ostype} = "ubuntu-15.04"; | |
306c21dc DM |
346 | } elsif ($suite eq 'wily') { |
347 | $config->{ostype} = "ubuntu-15.10"; | |
281483b6 DM |
348 | } elsif ($suite eq 'xenial') { |
349 | $config->{ostype} = "ubuntu-16.04"; | |
ef852234 DM |
350 | } elsif ($suite eq 'yakkety') { |
351 | $config->{ostype} = "ubuntu-16.10"; | |
92146ce5 DM |
352 | } elsif ($suite eq 'zesty') { |
353 | $config->{ostype} = "ubuntu-17.04"; | |
8ab34b87 DM |
354 | } else { |
355 | die "unsupported debian suite '$suite'\n"; | |
356 | } | |
357 | ||
358 | my $name = $config->{name} || die "no 'name' specified\n"; | |
359 | ||
360 | $name =~ m/^[a-z][0-9a-z\-\*\.]+$/ || | |
361 | die "illegal characters in name '$name'\n"; | |
362 | ||
363 | my $version = $config->{version}; | |
364 | die "no 'version' specified\n" if !$version; | |
365 | die "no 'section' specified\n" if !$config->{section}; | |
366 | die "no 'description' specified\n" if !$config->{headline}; | |
367 | die "no 'maintainer' specified\n" if !$config->{maintainer}; | |
368 | ||
369 | if ($name =~ m/^$config->{ostype}/) { | |
370 | $self->{targetname} = "${name}_${version}_$config->{architecture}"; | |
371 | } else { | |
372 | $self->{targetname} = "$config->{ostype}-${name}_" . | |
373 | "${version}_$config->{architecture}"; | |
374 | } | |
375 | ||
376 | if (!$config->{source}) { | |
b6a1907e | 377 | if ($suite eq 'etch' || $suite eq 'lenny') { |
8ab34b87 | 378 | push @{$config->{source}}, "http://ftp.debian.org/debian SUITE main contrib"; |
b6a1907e DM |
379 | push @{$config->{source}}, "http://security.debian.org SUITE/updates main contrib"; |
380 | } elsif ($suite eq 'squeeze' || $suite eq 'wheezy' || | |
381 | $suite eq 'jessie' || $suite eq 'stretch' ) { | |
382 | push @{$config->{source}}, "http://ftp.debian.org/debian SUITE main contrib"; | |
383 | push @{$config->{source}}, "http://ftp.debian.org/debian SUITE-updates main contrib"; | |
8ab34b87 | 384 | push @{$config->{source}}, "http://security.debian.org SUITE/updates main contrib"; |
306c21dc | 385 | } elsif ($suite eq 'hardy' || $suite eq 'intrepid' || $suite eq 'jaunty' || |
281483b6 | 386 | $suite eq 'xenial' || $suite eq 'wily' || $suite eq 'vivid' || |
92146ce5 DM |
387 | $suite eq 'trusty' || $suite eq 'precise' || $suite eq 'yakkety' || |
388 | $suite eq 'zesty') { | |
8ab34b87 DM |
389 | my $comp = "main restricted universe multiverse"; |
390 | push @{$config->{source}}, "http://archive.ubuntu.com/ubuntu SUITE $comp"; | |
391 | push @{$config->{source}}, "http://archive.ubuntu.com/ubuntu SUITE-updates $comp"; | |
392 | push @{$config->{source}}, "http://archive.ubuntu.com/ubuntu SUITE-security $comp"; | |
e6b5b5ac DM |
393 | } else { |
394 | die "implement me"; | |
8ab34b87 DM |
395 | } |
396 | } | |
397 | ||
398 | my $sources = undef; | |
399 | ||
400 | foreach my $s (@{$config->{source}}) { | |
401 | if ($s =~ m@^\s*((http|ftp)://\S+)\s+(\S+)((\s+(\S+))+)$@) { | |
402 | my ($url, $su, $components) = ($1, $3, $4); | |
403 | $su =~ s/SUITE/$suite/; | |
404 | $components =~ s/^\s+//; | |
405 | $components =~ s/\s+$//; | |
406 | my $ca; | |
407 | foreach my $co (split (/\s+/, $components)) { | |
408 | push @$ca, $co; | |
409 | } | |
410 | $ca = ['main'] if !$ca; | |
411 | ||
412 | push @$sources, { | |
413 | source => $url, | |
414 | comp => $ca, | |
415 | suite => $su, | |
416 | }; | |
417 | } else { | |
418 | die "syntax error in source spezification '$s'\n"; | |
419 | } | |
420 | } | |
421 | ||
422 | foreach my $m (@{$config->{mirror}}) { | |
423 | if ($m =~ m@^\s*((http|ftp)://\S+)\s*=>\s*((http|ftp)://\S+)\s*$@) { | |
424 | my ($ms, $md) = ($1, $3); | |
425 | my $found; | |
426 | foreach my $ss (@$sources) { | |
427 | if ($ss->{source} eq $ms) { | |
428 | $found = 1; | |
429 | $ss->{mirror} = $md; | |
430 | last; | |
431 | } | |
432 | } | |
433 | die "unusable mirror $ms\n" if !$found; | |
434 | } else { | |
435 | die "syntax error in mirror spezification '$m'\n"; | |
436 | } | |
437 | } | |
438 | ||
439 | $self->{sources} = $sources; | |
440 | ||
441 | $self->{infodir} = "info"; | |
442 | ||
443 | $self->__allocate_ve (); | |
444 | ||
445 | $self->{cachedir} = ($config->{cachedir} || 'cache') . "/$suite";; | |
446 | ||
447 | my $incl = [qw (less ssh openssh-server logrotate)]; | |
448 | ||
449 | my $excl = [qw (modutils reiserfsprogs ppp pppconfig pppoe | |
450 | pppoeconf nfs-common mtools ntp)]; | |
451 | ||
452 | # ubuntu has too many dependencies on udev, so | |
453 | # we cannot exclude it (instead we disable udevd) | |
e6b5b5ac | 454 | |
ef852234 | 455 | if ($suite eq 'vivid' || $suite eq 'wily' || $suite eq 'xenial' || |
92146ce5 | 456 | $suite eq 'yakkety' || $suite eq 'zesty') { |
306c21dc | 457 | push @$incl, 'isc-dhcp-client'; |
881f3099 | 458 | push @$excl, qw(libmodule-build-perl); |
e6b5b5ac DM |
459 | } elsif ($suite eq 'trusty') { |
460 | push @$excl, qw(systemd systemd-services libpam-systemd libsystemd-daemon0 memtest86+); | |
91be0a6a DM |
461 | } elsif ($suite eq 'precise') { |
462 | push @$excl, qw(systemd systemd-services libpam-systemd libsystemd-daemon0 memtest86+ ubuntu-standard); | |
e6b5b5ac | 463 | } elsif ($suite eq 'hardy') { |
8ab34b87 DM |
464 | push @$excl, qw(kbd); |
465 | push @$excl, qw(apparmor apparmor-utils ntfs-3g | |
466 | friendly-recovery); | |
5d0f537e | 467 | } elsif ($suite eq 'intrepid' || $suite eq 'jaunty') { |
8ab34b87 DM |
468 | push @$excl, qw(apparmor apparmor-utils libapparmor1 libapparmor-perl |
469 | libntfs-3g28 ntfs-3g friendly-recovery); | |
5d0f537e DM |
470 | } elsif ($suite eq 'jessie') { |
471 | push @$incl, 'sysvinit-core'; # avoid systemd and udev | |
472 | push @$incl, 'libperl4-corelibs-perl'; # to make lsof happy | |
473 | push @$excl, qw(systemd systemd-sysv udev module-init-tools pciutils hdparm | |
474 | memtest86+ parted); | |
b6a1907e DM |
475 | } elsif ($suite eq 'stretch') { |
476 | push @$excl, qw(module-init-tools pciutils hdparm | |
477 | memtest86+ parted); | |
5d0f537e | 478 | } else { |
8ab34b87 DM |
479 | push @$excl, qw(udev module-init-tools pciutils hdparm |
480 | memtest86+ parted); | |
481 | } | |
482 | ||
483 | $self->{incl} = $incl; | |
484 | $self->{excl} = $excl; | |
485 | ||
486 | return $self; | |
487 | } | |
488 | ||
489 | sub initialize { | |
490 | my ($self) = @_; | |
491 | ||
492 | my $infodir = $self->{infodir}; | |
493 | my $arch = $self->{config}->{architecture}; | |
494 | ||
495 | rmtree $infodir; | |
496 | mkpath $infodir; | |
497 | ||
498 | # truncate log | |
499 | my $logfd = $self->{logfd} = IO::File->new (">$self->{logfile}") || | |
500 | die "unable to open log file"; | |
501 | ||
502 | foreach my $ss (@{$self->{sources}}) { | |
503 | my $src = $ss->{mirror} || $ss->{source}; | |
504 | my $path = "dists/$ss->{suite}/Release"; | |
505 | my $url = "$src/$path"; | |
506 | my $target = __url_to_filename ("$ss->{source}/$path"); | |
507 | eval { | |
508 | $self->download ($url, "$infodir/$target"); | |
509 | $self->download ("$url.gpg", "$infodir/$target.gpg"); | |
510 | # fixme: impl. verify (needs --keyring option) | |
511 | }; | |
512 | if (my $err = $@) { | |
513 | print $logfd $@; | |
514 | warn "Release info ignored\n"; | |
515 | }; | |
516 | foreach my $comp (@{$ss->{comp}}) { | |
517 | $path = "dists/$ss->{suite}/$comp/binary-$arch/Packages.gz"; | |
518 | $target = "$infodir/" . __url_to_filename ("$ss->{source}/$path"); | |
519 | my $pkgsrc = "$src/$path"; | |
520 | $self->download ($pkgsrc, $target); | |
521 | $self->run_command ("gzip -d '$target'"); | |
522 | } | |
523 | } | |
524 | } | |
525 | ||
526 | sub write_config { | |
527 | my ($self, $filename, $size) = @_; | |
528 | ||
529 | my $config = $self->{config}; | |
530 | ||
531 | my $data = ''; | |
532 | ||
533 | $data .= "Name: $config->{name}\n"; | |
534 | $data .= "Version: $config->{version}\n"; | |
f0134ed2 | 535 | $data .= "Type: lxc\n"; |
8ab34b87 DM |
536 | $data .= "OS: $config->{ostype}\n"; |
537 | $data .= "Section: $config->{section}\n"; | |
538 | $data .= "Maintainer: $config->{maintainer}\n"; | |
539 | $data .= "Architecture: $config->{architecture}\n"; | |
540 | $data .= "Installed-Size: $size\n"; | |
541 | ||
542 | # optional | |
543 | $data .= "Infopage: $config->{infopage}\n" if $config->{infopage}; | |
544 | $data .= "ManageUrl: $config->{manageurl}\n" if $config->{manageurl}; | |
545 | $data .= "Certified: $config->{certified}\n" if $config->{certified}; | |
546 | ||
547 | # description | |
548 | $data .= "Description: $config->{headline}\n"; | |
549 | $data .= "$config->{description}\n" if $config->{description}; | |
550 | ||
551 | write_file ($data, $filename, 0644); | |
552 | } | |
553 | ||
554 | sub finalize { | |
555 | my ($self, $opts) = @_; | |
556 | ||
557 | my $suite = $self->{config}->{suite}; | |
558 | my $infodir = $self->{infodir}; | |
559 | my $arch = $self->{config}->{architecture}; | |
560 | ||
561 | my $instpkgs = $self->read_installed (); | |
562 | my $pkginfo = $self->pkginfo(); | |
563 | my $veid = $self->{veid}; | |
6fe8088f | 564 | my $conffile = $self->{veconffile}; |
f0134ed2 | 565 | my $rootdir = $self->{rootfs}; |
8ab34b87 DM |
566 | |
567 | my $vestat = $self->ve_status(); | |
568 | die "ve not running - unable to finalize\n" if !$vestat->{running}; | |
569 | ||
570 | # cleanup mysqld | |
571 | if (-f "$rootdir/etc/init.d/mysql") { | |
572 | $self->ve_command ("/etc/init.d/mysql stop"); | |
573 | } | |
574 | ||
575 | if (!($opts->{keepmycnf} || (-f "$rootdir/etc/init.d/mysql_randompw"))) { | |
576 | unlink "$rootdir/root/.my.cnf"; | |
577 | } | |
578 | ||
579 | if ($suite eq 'etch') { | |
580 | # enable apache2 startup | |
581 | if ($instpkgs->{apache2}) { | |
582 | write_file ("NO_START=0\n", "$rootdir/etc/default/apache2"); | |
583 | } else { | |
584 | unlink "$rootdir/etc/default/apache2"; | |
585 | } | |
586 | } | |
587 | $self->logmsg ("cleanup package status\n"); | |
588 | # prevent auto selection of all standard, required or important | |
589 | # packages which are not installed | |
590 | foreach my $pkg (keys %$pkginfo) { | |
591 | my $pri = $pkginfo->{$pkg}->{priority}; | |
592 | if ($pri && ($pri eq 'required' || $pri eq 'important' | |
593 | || $pri eq 'standard')) { | |
594 | if (!$instpkgs->{$pkg}) { | |
595 | $self->ve_dpkg_set_selection ($pkg, 'purge'); | |
596 | } | |
597 | } | |
598 | } | |
599 | ||
600 | $self->ve_command ("apt-get clean"); | |
601 | ||
602 | $self->logmsg ("update available package list\n"); | |
603 | ||
604 | $self->ve_command ("dpkg --clear-avail"); | |
605 | foreach my $ss (@{$self->{sources}}) { | |
606 | my $relsrc = __url_to_filename ("$ss->{source}/dists/$ss->{suite}/Release"); | |
607 | if (-f "$infodir/$relsrc" && -f "$infodir/$relsrc.gpg") { | |
608 | $self->run_command ("cp '$infodir/$relsrc' '$rootdir/var/lib/apt/lists/$relsrc'"); | |
609 | $self->run_command ("cp '$infodir/$relsrc.gpg' '$rootdir/var/lib/apt/lists/$relsrc.gpg'"); | |
610 | } | |
611 | foreach my $comp (@{$ss->{comp}}) { | |
612 | my $src = __url_to_filename ("$ss->{source}/dists/$ss->{suite}/" . | |
613 | "$comp/binary-$arch/Packages"); | |
614 | my $target = "/var/lib/apt/lists/$src"; | |
615 | $self->run_command ("cp '$infodir/$src' '$rootdir/$target'"); | |
616 | $self->ve_command ("dpkg --merge-avail '$target'"); | |
617 | } | |
618 | } | |
619 | ||
620 | # set dselect default method | |
621 | write_file ("apt apt\n", "$rootdir/var/lib/dpkg/cmethopt"); | |
622 | ||
623 | $self->ve_divert_remove ("/usr/sbin/policy-rc.d"); | |
624 | ||
625 | $self->ve_divert_remove ("/sbin/start-stop-daemon"); | |
626 | ||
627 | $self->ve_divert_remove ("/sbin/init"); | |
628 | ||
629 | # finally stop the VE | |
6fe8088f | 630 | $self->run_command ("lxc-stop -n $veid --rcfile $conffile --kill"); |
8ab34b87 DM |
631 | |
632 | unlink "$rootdir/sbin/defenv"; | |
633 | ||
634 | unlink <$rootdir/root/dead.letter*>; | |
635 | ||
636 | unlink "$rootdir/var/log/init.log"; | |
637 | ||
638 | unlink "$rootdir/aquota.group"; | |
639 | ||
640 | unlink "$rootdir/aquota.user"; | |
641 | ||
642 | write_file ("", "$rootdir/var/log/syslog"); | |
643 | ||
644 | $self->logmsg ("detecting final size: "); | |
645 | ||
646 | my $sizestr = $self->run_command ("du -sm $rootdir", undef, 1); | |
647 | my $size; | |
648 | if ($sizestr =~ m/^(\d+)\s+\Q$rootdir\E$/) { | |
649 | $size = $1; | |
650 | } else { | |
651 | die "unable to detect size\n"; | |
652 | } | |
653 | $self->logmsg ("$size MB\n"); | |
654 | ||
655 | $self->write_config ("$rootdir/etc/appliance.info", $size); | |
656 | ||
657 | $self->logmsg ("creating final appliance archive\n"); | |
658 | ||
659 | my $target = "$self->{targetname}.tar"; | |
660 | unlink $target; | |
661 | unlink "$target.gz"; | |
662 | ||
663 | $self->run_command ("tar cpf $target --numeric-owner -C '$rootdir' ./etc/appliance.info"); | |
664 | $self->run_command ("tar rpf $target --numeric-owner -C '$rootdir' --exclude ./etc/appliance.info ."); | |
665 | $self->run_command ("gzip $target"); | |
666 | } | |
667 | ||
668 | sub read_installed { | |
669 | my ($self) = @_; | |
670 | ||
f0134ed2 | 671 | my $rootdir = $self->{rootfs}; |
8ab34b87 DM |
672 | |
673 | my $pkgfilelist = "$rootdir/var/lib/dpkg/status"; | |
674 | local $/ = ''; | |
675 | open (PKGLST, "<$pkgfilelist") || | |
676 | die "unable to open '$pkgfilelist'"; | |
677 | ||
678 | my $pkglist = {}; | |
679 | ||
680 | while (my $rec = <PKGLST>) { | |
681 | chomp $rec; | |
682 | $rec =~ s/\n\s+/ /g; | |
683 | $rec .= "\n"; | |
684 | my $res = {}; | |
685 | ||
686 | while ($rec =~ s/^([^:]+):\s+(.*)\s*\n//) { | |
687 | $res->{lc $1} = $2; | |
688 | } | |
689 | ||
690 | my $pkg = $res->{'package'}; | |
691 | if (my $status = $res->{status}) { | |
692 | my @sa = split (/\s+/, $status); | |
693 | my $stat = $sa[0]; | |
694 | if ($stat && ($stat ne 'purge')) { | |
695 | $pkglist->{$pkg} = $res; | |
696 | } | |
697 | } | |
698 | } | |
699 | ||
700 | close (PKGLST); | |
701 | ||
702 | return $pkglist; | |
703 | } | |
704 | ||
f0134ed2 | 705 | sub ve_status { |
8ab34b87 DM |
706 | my ($self) = @_; |
707 | ||
708 | my $veid = $self->{veid}; | |
709 | ||
f0134ed2 | 710 | my $res = { running => 0 }; |
8ab34b87 | 711 | |
f0134ed2 | 712 | $res->{exist} = 1 if -d "$self->{rootfs}/usr"; |
8ab34b87 | 713 | |
f0134ed2 | 714 | my $filename = "/proc/net/unix"; |
8ab34b87 | 715 | |
f0134ed2 DM |
716 | # similar test is used by lcxcontainers.c: list_active_containers |
717 | my $fh = IO::File->new ($filename, "r"); | |
718 | return $res if !$fh; | |
8ab34b87 | 719 | |
f0134ed2 DM |
720 | while (defined(my $line = <$fh>)) { |
721 | if ($line =~ m/^[a-f0-9]+:\s\S+\s\S+\s\S+\s\S+\s\S+\s\d+\s(\S+)$/) { | |
722 | my $path = $1; | |
723 | if ($path =~ m!^@/\S+/$veid/command$!) { | |
724 | $res->{running} = 1; | |
725 | } | |
726 | } | |
8ab34b87 | 727 | } |
f0134ed2 DM |
728 | close($fh); |
729 | ||
730 | return $res; | |
8ab34b87 DM |
731 | } |
732 | ||
733 | sub ve_command { | |
734 | my ($self, $cmd, $input) = @_; | |
735 | ||
736 | my $veid = $self->{veid}; | |
6fe8088f | 737 | my $conffile = $self->{veconffile}; |
8ab34b87 DM |
738 | |
739 | if (ref ($cmd) eq 'ARRAY') { | |
6fe8088f | 740 | unshift @$cmd, 'lxc-attach', '-n', $veid, '--rcfile', $conffile, '--clear-env', '--', 'defenv'; |
8ab34b87 DM |
741 | $self->run_command ($cmd, $input); |
742 | } else { | |
6fe8088f | 743 | $self->run_command ("lxc-attach -n $veid --rcfile $conffile --clear-env -- defenv $cmd", $input); |
8ab34b87 DM |
744 | } |
745 | } | |
746 | ||
747 | # like ve_command, but pipes stdin correctly | |
748 | sub ve_exec { | |
749 | my ($self, @cmd) = @_; | |
750 | ||
751 | my $veid = $self->{veid}; | |
6fe8088f | 752 | my $conffile = $self->{veconffile}; |
8ab34b87 DM |
753 | |
754 | my $reader; | |
6fe8088f | 755 | my $pid = open2($reader, "<&STDIN", 'lxc-attach', '-n', $veid, '--rcfile', $conffile, '--', |
8ab34b87 DM |
756 | 'defenv', @cmd) || die "unable to exec command"; |
757 | ||
758 | while (defined (my $line = <$reader>)) { | |
759 | $self->logmsg ($line); | |
760 | } | |
761 | ||
762 | waitpid ($pid, 0); | |
763 | my $rc = $? >> 8; | |
764 | ||
765 | die "ve_exec failed - status $rc\n" if $rc != 0; | |
766 | } | |
767 | ||
768 | sub ve_divert_add { | |
769 | my ($self, $filename) = @_; | |
770 | ||
771 | $self->ve_command ("dpkg-divert --add --divert '$filename.distrib' " . | |
772 | "--rename '$filename'"); | |
773 | } | |
774 | sub ve_divert_remove { | |
775 | my ($self, $filename) = @_; | |
776 | ||
f0134ed2 | 777 | my $rootdir = $self->{rootfs}; |
8ab34b87 DM |
778 | |
779 | unlink "$rootdir/$filename"; | |
780 | $self->ve_command ("dpkg-divert --remove --rename '$filename'"); | |
781 | } | |
782 | ||
783 | sub ve_debconfig_set { | |
784 | my ($self, $dcdata) = @_; | |
785 | ||
f0134ed2 | 786 | my $rootdir = $self->{rootfs}; |
8ab34b87 DM |
787 | my $cfgfile = "/tmp/debconf.txt"; |
788 | write_file ($dcdata, "$rootdir/$cfgfile"); | |
789 | $self->ve_command ("debconf-set-selections $cfgfile"); | |
790 | unlink "$rootdir/$cfgfile"; | |
791 | } | |
792 | ||
793 | sub ve_dpkg_set_selection { | |
794 | my ($self, $pkg, $status) = @_; | |
795 | ||
796 | $self->ve_command ("dpkg --set-selections", "$pkg $status"); | |
797 | } | |
798 | ||
799 | sub ve_dpkg { | |
800 | my ($self, $cmd, @pkglist) = @_; | |
801 | ||
802 | return if !scalar (@pkglist); | |
803 | ||
804 | my $pkginfo = $self->pkginfo(); | |
805 | ||
f0134ed2 | 806 | my $rootdir = $self->{rootfs}; |
8ab34b87 DM |
807 | my $cachedir = $self->{cachedir}; |
808 | ||
809 | my @files; | |
810 | ||
811 | foreach my $pkg (@pkglist) { | |
812 | my $filename = $self->getpkgfile ($pkg); | |
813 | $self->run_command ("cp '$cachedir/$filename' '$rootdir/$filename'"); | |
814 | push @files, "/$filename"; | |
815 | $self->logmsg ("$cmd: $pkg\n"); | |
816 | } | |
817 | ||
818 | my $fl = join (' ', @files); | |
819 | ||
820 | if ($cmd eq 'install') { | |
821 | $self->ve_command ("dpkg --force-depends --force-confold --install $fl"); | |
822 | } elsif ($cmd eq 'unpack') { | |
823 | $self->ve_command ("dpkg --force-depends --unpack $fl"); | |
824 | } else { | |
825 | die "internal error"; | |
826 | } | |
827 | ||
828 | foreach my $fn (@files) { unlink "$rootdir$fn"; } | |
829 | } | |
830 | ||
831 | sub ve_destroy { | |
832 | my ($self) = @_; | |
833 | ||
834 | my $veid = $self->{veid}; # fixme | |
6fe8088f | 835 | my $conffile = $self->{veconffile}; |
8ab34b87 DM |
836 | |
837 | my $vestat = $self->ve_status(); | |
838 | if ($vestat->{running}) { | |
6fe8088f | 839 | $self->run_command ("lxc-stop -n $veid --rcfile $conffile --kill"); |
8ab34b87 | 840 | } |
f0134ed2 DM |
841 | |
842 | rmtree $self->{rootfs}; | |
843 | unlink $self->{veconffile}; | |
8ab34b87 DM |
844 | } |
845 | ||
846 | sub ve_init { | |
847 | my ($self) = @_; | |
848 | ||
f0134ed2 | 849 | my $veid = $self->{veid}; |
6fe8088f | 850 | my $conffile = $self->{veconffile}; |
8ab34b87 DM |
851 | |
852 | $self->logmsg ("initialize VE $veid\n"); | |
853 | ||
f0134ed2 DM |
854 | my $vestat = $self->ve_status(); |
855 | if ($vestat->{running}) { | |
6fe8088f | 856 | $self->run_command ("lxc-stop -n $veid --rcfile $conffile --kill"); |
f0134ed2 | 857 | } |
8ab34b87 | 858 | |
f0134ed2 DM |
859 | rmtree $self->{rootfs}; |
860 | mkpath $self->{rootfs}; | |
8ab34b87 DM |
861 | } |
862 | ||
863 | sub __deb_version_cmp { | |
864 | my ($cur, $op, $new) = @_; | |
865 | ||
866 | if (system("dpkg", "--compare-versions", $cur, $op, $new) == 0) { | |
867 | return 1; | |
868 | } | |
869 | ||
870 | return 0; | |
871 | } | |
872 | ||
873 | sub __parse_packages { | |
874 | my ($pkginfo, $filename, $src) = @_; | |
875 | ||
876 | local $/ = ''; | |
877 | open (PKGLST, "<$filename") || | |
878 | die "unable to open '$filename'"; | |
879 | ||
880 | while (my $rec = <PKGLST>) { | |
881 | $rec =~ s/\n\s+/ /g; | |
882 | chomp $rec; | |
883 | $rec .= "\n"; | |
884 | ||
885 | my $res = {}; | |
886 | ||
887 | while ($rec =~ s/^([^:]+):\s+(.*)\s*\n//) { | |
888 | $res->{lc $1} = $2; | |
889 | } | |
890 | ||
891 | my $pkg = $res->{'package'}; | |
892 | if ($pkg && $res->{'filename'}) { | |
893 | my $cur; | |
894 | if (my $info = $pkginfo->{$pkg}) { | |
895 | $cur = $info->{version}; | |
896 | } | |
897 | my $new = $res->{version}; | |
898 | if (!$cur || __deb_version_cmp ($cur, 'lt', $new)) { | |
899 | if ($src) { | |
900 | $res->{url} = "$src/$res->{'filename'}"; | |
901 | } else { | |
902 | die "no url for package '$pkg'" if !$res->{url}; | |
903 | } | |
904 | $pkginfo->{$pkg} = $res; | |
905 | } | |
906 | } | |
907 | } | |
908 | ||
909 | close (PKGLST); | |
910 | } | |
911 | ||
912 | sub pkginfo { | |
913 | my ($self) = @_; | |
914 | ||
915 | return $self->{pkginfo} if $self->{pkginfo}; | |
916 | ||
917 | my $infodir = $self->{infodir}; | |
918 | my $arch = $self->{config}->{architecture}; | |
919 | ||
920 | my $availfn = "$infodir/available"; | |
921 | ||
922 | my $pkginfo = {}; | |
923 | my $pkgcount = 0; | |
924 | ||
925 | # reading 'available' is faster, because it only contains latest version | |
926 | # (no need to do slow version compares) | |
927 | if (-f $availfn) { | |
928 | __parse_packages ($pkginfo, $availfn); | |
929 | $self->{pkginfo} = $pkginfo; | |
930 | return $pkginfo; | |
931 | } | |
932 | ||
933 | $self->logmsg ("generating available package list\n"); | |
934 | ||
935 | foreach my $ss (@{$self->{sources}}) { | |
936 | foreach my $comp (@{$ss->{comp}}) { | |
937 | my $url = "$ss->{source}/dists/$ss->{suite}/$comp/binary-$arch/Packages"; | |
938 | my $pkgfilelist = "$infodir/" . __url_to_filename ($url); | |
939 | ||
940 | my $src = $ss->{mirror} || $ss->{source}; | |
941 | ||
942 | __parse_packages ($pkginfo, $pkgfilelist, $src); | |
943 | } | |
944 | } | |
945 | ||
946 | if (my $dep = $self->{config}->{depends}) { | |
947 | foreach my $d (split (/,/, $dep)) { | |
948 | if ($d =~ m/^\s*(\S+)\s*(\((\S+)\s+(\S+)\)\s*)?$/) { | |
949 | my ($pkg, $op, $rver) = ($1, $3, $4); | |
950 | $self->logmsg ("checking dependencies: $d\n"); | |
951 | my $info = $pkginfo->{$pkg}; | |
952 | die "package '$pkg' not available\n" if !$info; | |
953 | if ($op) { | |
954 | my $cver = $info->{version}; | |
955 | if (!__deb_version_cmp ($cver, $op, $rver)) { | |
956 | die "detected wrong version '$cver'\n"; | |
957 | } | |
958 | } | |
959 | } else { | |
960 | die "syntax error in depends field"; | |
961 | } | |
962 | } | |
963 | } | |
964 | ||
965 | $self->{pkginfo} = $pkginfo; | |
966 | ||
967 | my $tmpfn = "$availfn.tmp$$"; | |
968 | my $fd = IO::File->new (">$tmpfn"); | |
969 | foreach my $pkg (sort keys %$pkginfo) { | |
970 | my $info = $pkginfo->{$pkg}; | |
971 | print $fd "package: $pkg\n"; | |
972 | foreach my $k (sort keys %$info) { | |
973 | next if $k eq 'description'; | |
974 | next if $k eq 'package'; | |
975 | my $v = $info->{$k}; | |
976 | print $fd "$k: $v\n" if $v; | |
977 | } | |
978 | print $fd "description: $info->{description}\n" if $info->{description}; | |
979 | print $fd "\n"; | |
980 | } | |
981 | close ($fd); | |
982 | ||
983 | rename ($tmpfn, $availfn); | |
984 | ||
985 | return $pkginfo; | |
986 | } | |
987 | ||
988 | sub __record_provides { | |
989 | my ($pkginfo, $closure, $list, $skipself) = @_; | |
990 | ||
991 | foreach my $pname (@$list) { | |
992 | my $info = $pkginfo->{$pname}; | |
993 | # fixme: if someone install packages directly using dpkg, there | |
994 | # is no entry in 'available', only in 'status'. In that case, we | |
995 | # should extract info from $instpkgs | |
996 | if (!$info) { | |
997 | warn "hint: ignoring provides for '$pname' - package not in 'available' list.\n"; | |
998 | next; | |
999 | } | |
1000 | if (my $prov = $info->{provides}) { | |
1001 | my @pl = split (',', $prov); | |
1002 | foreach my $p (@pl) { | |
1003 | $p =~ m/\s*(\S+)/; | |
1004 | if (!($skipself && (grep { $1 eq $_ } @$list))) { | |
1005 | $closure->{$1} = 1; | |
1006 | } | |
1007 | } | |
1008 | } | |
1009 | $closure->{$pname} = 1 if !$skipself; | |
1010 | } | |
1011 | } | |
1012 | ||
1013 | sub closure { | |
1014 | my ($self, $closure, $list) = @_; | |
1015 | ||
1016 | my $pkginfo = $self->pkginfo(); | |
1017 | ||
1018 | # first, record provided packages | |
1019 | __record_provides ($pkginfo, $closure, $list, 1); | |
1020 | ||
e30a1928 DM |
1021 | my $pkghash = {}; |
1022 | my $pkglist = []; | |
8ab34b87 DM |
1023 | |
1024 | # then resolve dependencies | |
1025 | foreach my $pname (@$list) { | |
e30a1928 | 1026 | __closure_single ($pkginfo, $closure, $pkghash, $pkglist, $pname, $self->{excl}); |
8ab34b87 DM |
1027 | } |
1028 | ||
e30a1928 | 1029 | return $pkglist; |
8ab34b87 DM |
1030 | } |
1031 | ||
1032 | sub __closure_single { | |
e30a1928 | 1033 | my ($pkginfo, $closure, $pkghash, $pkglist, $pname, $excl) = @_; |
8ab34b87 DM |
1034 | |
1035 | $pname =~ s/^\s+//; | |
1036 | $pname =~ s/\s+$//; | |
5d0f537e | 1037 | $pname =~ s/:any$//; |
8ab34b87 DM |
1038 | |
1039 | return if $closure->{$pname}; | |
1040 | ||
1041 | my $info = $pkginfo->{$pname} || die "no such package '$pname'"; | |
1042 | ||
1043 | my $dep = $info->{depends}; | |
1044 | my $predep = $info->{'pre-depends'}; | |
1045 | ||
1046 | my $size = $info->{size}; | |
1047 | my $url = $info->{url}; | |
1048 | ||
1049 | $url || die "$pname: no url for package '$pname'"; | |
e30a1928 DM |
1050 | |
1051 | if (!$pkghash->{$pname}) { | |
1052 | unshift @$pkglist, $pname; | |
1053 | $pkghash->{$pname} = 1; | |
1054 | } | |
8ab34b87 DM |
1055 | |
1056 | __record_provides ($pkginfo, $closure, [$pname]) if $info->{provides}; | |
1057 | ||
1058 | $closure->{$pname} = 1; | |
1059 | ||
1060 | #print "$url\n"; | |
1061 | ||
1062 | my @l; | |
1063 | ||
1064 | push @l, split (/,/, $predep) if $predep; | |
1065 | push @l, split (/,/, $dep) if $dep; | |
1066 | ||
1067 | DEPEND: foreach my $p (@l) { | |
1068 | my @l1 = split (/\|/, $p); | |
1069 | foreach my $p1 (@l1) { | |
1070 | if ($p1 =~ m/^\s*(\S+).*/) { | |
1071 | #printf (STDERR "$pname: $p --> $1\n"); | |
1072 | if ($closure->{$1}) { | |
1073 | next DEPEND; # dependency already met | |
1074 | } | |
1075 | } | |
1076 | } | |
1077 | # search for non-excluded alternative | |
1078 | my $found; | |
1079 | foreach my $p1 (@l1) { | |
1080 | if ($p1 =~ m/^\s*(\S+).*/) { | |
1081 | next if grep { $1 eq $_ } @$excl; | |
1082 | $found = $1; | |
1083 | last; | |
1084 | } | |
1085 | } | |
1086 | die "package '$pname' depends on exclusion '$p'\n" if !$found; | |
1087 | ||
1088 | #printf (STDERR "$pname: $p --> $found\n"); | |
1089 | ||
e30a1928 | 1090 | __closure_single ($pkginfo, $closure, $pkghash, $pkglist, $found, $excl); |
8ab34b87 DM |
1091 | } |
1092 | } | |
1093 | ||
1094 | sub cache_packages { | |
1095 | my ($self, $pkglist) = @_; | |
1096 | ||
1097 | foreach my $pkg (@$pkglist) { | |
1098 | $self->getpkgfile ($pkg); | |
1099 | } | |
1100 | } | |
1101 | ||
1102 | sub getpkgfile { | |
1103 | my ($self, $pkg) = @_; | |
1104 | ||
1105 | my $pkginfo = $self->pkginfo(); | |
1106 | my $info = $pkginfo->{$pkg} || die "no such package '$pkg'"; | |
1107 | my $cachedir = $self->{cachedir}; | |
1108 | ||
1109 | my $url = $info->{url}; | |
1110 | ||
1111 | my $filename; | |
1112 | if ($url =~ m|/([^/]+.deb)$|) { | |
1113 | $filename = $1; | |
1114 | } else { | |
1115 | die "internal error"; | |
1116 | } | |
1117 | ||
1118 | return $filename if -f "$cachedir/$filename"; | |
1119 | ||
1120 | mkpath $cachedir; | |
1121 | ||
1122 | $self->download ($url, "$cachedir/$filename"); | |
1123 | ||
1124 | return $filename; | |
1125 | } | |
1126 | ||
1127 | sub install_init_script { | |
1128 | my ($self, $script, $runlevel, $prio) = @_; | |
1129 | ||
1130 | my $suite = $self->{config}->{suite}; | |
f0134ed2 | 1131 | my $rootdir = $self->{rootfs}; |
8ab34b87 DM |
1132 | |
1133 | my $base = basename ($script); | |
1134 | my $target = "$rootdir/etc/init.d/$base"; | |
1135 | ||
1136 | $self->run_command ("install -m 0755 '$script' '$target'"); | |
1137 | if ($suite eq 'etch' || $suite eq 'lenny') { | |
1138 | $self->ve_command ("update-rc.d $base start $prio $runlevel ."); | |
ef852234 | 1139 | } elsif ($suite eq 'xenial' || $suite eq 'wily' || $suite eq 'vivid' || |
92146ce5 | 1140 | $suite eq 'yakkety' || $suite eq 'zesty') { |
bb6602fa | 1141 | die "unable to install init script (system uses systemd)\n"; |
91be0a6a | 1142 | } elsif ($suite eq 'trusty' || $suite eq 'precise') { |
bb6602fa | 1143 | die "unable to install init script (system uses upstart)\n"; |
8ab34b87 DM |
1144 | } else { |
1145 | $self->ve_command ("insserv $base"); | |
1146 | } | |
1147 | ||
1148 | return $target; | |
1149 | } | |
1150 | ||
1151 | sub bootstrap { | |
1152 | my ($self, $opts) = @_; | |
1153 | ||
1154 | my $pkginfo = $self->pkginfo(); | |
1155 | my $veid = $self->{veid}; | |
1156 | my $suite = $self->{config}->{suite}; | |
1157 | ||
1158 | my $important = [ @{$self->{incl}} ]; | |
1159 | my $required; | |
1160 | my $standard; | |
1161 | ||
1162 | my $mta = $opts->{exim} ? 'exim' : 'postfix'; | |
1163 | ||
1164 | if ($mta eq 'postfix') { | |
1165 | push @$important, "postfix"; | |
1166 | } | |
1167 | ||
e30a1928 | 1168 | foreach my $p (sort keys %$pkginfo) { |
8ab34b87 DM |
1169 | next if grep { $p eq $_ } @{$self->{excl}}; |
1170 | my $pri = $pkginfo->{$p}->{priority}; | |
1171 | next if !$pri; | |
1172 | next if $mta ne 'exim' && $p =~ m/exim/; | |
1173 | next if $p =~ m/(selinux|semanage|policycoreutils)/; | |
1174 | ||
1175 | push @$required, $p if $pri eq 'required'; | |
1176 | push @$important, $p if $pri eq 'important'; | |
1177 | push @$standard, $p if $pri eq 'standard' && !$opts->{minimal}; | |
1178 | } | |
1179 | ||
1180 | my $closure = {}; | |
1181 | $required = $self->closure ($closure, $required); | |
1182 | $important = $self->closure ($closure, $important); | |
1183 | ||
1184 | if (!$opts->{minimal}) { | |
1185 | push @$standard, 'xbase-clients'; | |
1186 | $standard = $self->closure ($closure, $standard); | |
1187 | } | |
1188 | ||
1189 | # test if we have all 'ubuntu-minimal' and 'ubuntu-standard' packages | |
1190 | # except those explicitly excluded | |
1191 | if ($suite eq 'hardy' || $suite eq 'intrepid' || $suite eq 'jaunty') { | |
1192 | my $mdeps = $pkginfo->{'ubuntu-minimal'}->{depends}; | |
1193 | foreach my $d (split (/,/, $mdeps)) { | |
1194 | if ($d =~ m/^\s*(\S+)$/) { | |
1195 | my $pkg = $1; | |
1196 | next if $closure->{$pkg}; | |
1197 | next if grep { $pkg eq $_ } @{$self->{excl}}; | |
1198 | die "missing ubuntu-minimal package '$pkg'\n"; | |
1199 | } | |
1200 | } | |
1201 | if (!$opts->{minimal}) { | |
1202 | $mdeps = $pkginfo->{'ubuntu-standard'}->{depends}; | |
1203 | foreach my $d (split (/,/, $mdeps)) { | |
1204 | if ($d =~ m/^\s*(\S+)$/) { | |
1205 | my $pkg = $1; | |
1206 | next if $closure->{$pkg}; | |
1207 | next if grep { $pkg eq $_ } @{$self->{excl}}; | |
1208 | die "missing ubuntu-standard package '$pkg'\n"; | |
1209 | } | |
1210 | } | |
1211 | } | |
1212 | } | |
1213 | ||
1214 | # download/cache all files first | |
1215 | $self->cache_packages ($required); | |
1216 | $self->cache_packages ($important); | |
1217 | $self->cache_packages ($standard); | |
1218 | ||
f0134ed2 | 1219 | my $rootdir = $self->{rootfs}; |
8ab34b87 DM |
1220 | |
1221 | # extract required packages first | |
1222 | $self->logmsg ("create basic environment\n"); | |
1223 | foreach my $p (@$required) { | |
1224 | my $filename = $self->getpkgfile ($p); | |
5d0f537e DM |
1225 | my $content = $self->run_command("ar -t '$self->{cachedir}/$filename'", undef, 1); |
1226 | if ($content =~ m/^data.tar.xz$/m) { | |
1227 | $self->run_command ("ar -p '$self->{cachedir}/$filename' data.tar.xz | tar -C '$rootdir' -xJf -"); | |
1228 | } else { | |
1229 | $self->run_command ("ar -p '$self->{cachedir}/$filename' data.tar.gz | tar -C '$rootdir' -xzf -"); | |
1230 | } | |
8ab34b87 DM |
1231 | } |
1232 | ||
1233 | # fake dpkg status | |
1234 | my $data = "Package: dpkg\n" . | |
1235 | "Version: $pkginfo->{dpkg}->{version}\n" . | |
1236 | "Status: install ok installed\n"; | |
1237 | ||
1238 | write_file ($data, "$rootdir/var/lib/dpkg/status"); | |
1239 | write_file ("", "$rootdir/var/lib/dpkg/info/dpkg.list"); | |
1240 | write_file ("", "$rootdir/var/lib/dpkg/available"); | |
1241 | ||
1242 | $data = ''; | |
1243 | foreach my $ss (@{$self->{sources}}) { | |
1244 | my $url = $ss->{source}; | |
1245 | my $comp = join (' ', @{$ss->{comp}}); | |
1246 | $data .= "deb $url $ss->{suite} $comp\n\n"; | |
1247 | } | |
1248 | ||
1249 | write_file ($data, "$rootdir/etc/apt/sources.list"); | |
1250 | ||
1251 | $data = "# UNCONFIGURED FSTAB FOR BASE SYSTEM\n"; | |
1252 | write_file ($data, "$rootdir/etc/fstab", 0644); | |
1253 | ||
1254 | write_file ("localhost\n", "$rootdir/etc/hostname", 0644); | |
1255 | ||
1256 | # avoid warnings about non-existent resolv.conf | |
1257 | write_file ("", "$rootdir/etc/resolv.conf", 0644); | |
1258 | ||
92146ce5 DM |
1259 | if ($suite eq 'zesty' || $suite eq 'yakkety' || |
1260 | $suite eq 'xenial' || $suite eq 'wily') { | |
ae2443bc DM |
1261 | # no need to configure loopback device |
1262 | } else { | |
1263 | $data = "auto lo\niface lo inet loopback\n"; | |
b6a1907e | 1264 | mkdir "$rootdir/etc/network"; |
ae2443bc DM |
1265 | write_file ($data, "$rootdir/etc/network/interfaces", 0644); |
1266 | } | |
8ab34b87 DM |
1267 | |
1268 | # setup devices | |
1269 | $self->run_command ("tar xzf '$devicetar' -C '$rootdir'"); | |
1270 | ||
1271 | # avoid warnings about missing default locale | |
1272 | write_file ("LANG=\"C\"\n", "$rootdir/etc/default/locale", 0644); | |
1273 | ||
1274 | # fake init | |
1275 | rename ("$rootdir/sbin/init", "$rootdir/sbin/init.org"); | |
1276 | $self->run_command ("cp '$fake_init' '$rootdir/sbin/init'"); | |
1277 | ||
1278 | $self->run_command ("cp '$default_env' '$rootdir/sbin/defenv'"); | |
1279 | ||
f0134ed2 | 1280 | $self->run_command ("lxc-start -n $veid -f $self->{veconffile}"); |
8ab34b87 DM |
1281 | |
1282 | $self->logmsg ("initialize ld cache\n"); | |
1283 | $self->ve_command ("/sbin/ldconfig"); | |
1284 | $self->run_command ("ln -sf mawk '$rootdir/usr/bin/awk'"); | |
1285 | ||
1286 | $self->logmsg ("installing packages\n"); | |
1287 | ||
1288 | $self->ve_dpkg ('install', 'base-files', 'base-passwd'); | |
1289 | ||
1290 | $self->ve_dpkg ('install', 'dpkg'); | |
1291 | ||
1292 | $self->run_command ("ln -sf /usr/share/zoneinfo/UTC '$rootdir/etc/localtime'"); | |
756b991f DM |
1293 | |
1294 | $self->run_command ("ln -sf bash '$rootdir/bin/sh'"); | |
8ab34b87 DM |
1295 | |
1296 | $self->ve_dpkg ('install', 'libc6'); | |
1297 | $self->ve_dpkg ('install', 'perl-base'); | |
1298 | ||
1299 | unlink "$rootdir/usr/bin/awk"; | |
1300 | ||
1301 | $self->ve_dpkg ('install', 'mawk'); | |
1302 | $self->ve_dpkg ('install', 'debconf'); | |
e30a1928 | 1303 | |
8ab34b87 DM |
1304 | # unpack required packages |
1305 | foreach my $p (@$required) { | |
1306 | $self->ve_dpkg ('unpack', $p); | |
1307 | } | |
1308 | ||
1309 | rename ("$rootdir/sbin/init.org", "$rootdir/sbin/init"); | |
1310 | $self->ve_divert_add ("/sbin/init"); | |
1311 | $self->run_command ("cp '$fake_init' '$rootdir/sbin/init'"); | |
1312 | ||
1313 | # disable service activation | |
1314 | $self->ve_divert_add ("/usr/sbin/policy-rc.d"); | |
1315 | $data = "#!/bin/sh\nexit 101\n"; | |
1316 | write_file ($data, "$rootdir/usr/sbin/policy-rc.d", 755); | |
1317 | ||
1318 | # disable start-stop-daemon | |
1319 | $self->ve_divert_add ("/sbin/start-stop-daemon"); | |
1320 | $data = <<EOD; | |
1321 | #!/bin/sh | |
1322 | echo | |
1323 | echo \"Warning: Fake start-stop-daemon called, doing nothing\" | |
1324 | EOD | |
1325 | write_file ($data, "$rootdir/sbin/start-stop-daemon", 0755); | |
1326 | ||
1327 | # disable udevd | |
1328 | $self->ve_divert_add ("/sbin/udevd"); | |
1329 | ||
1330 | if ($suite eq 'etch') { | |
1331 | # disable apache2 startup | |
1332 | write_file ("NO_START=1\n", "$rootdir/etc/default/apache2"); | |
1333 | } | |
1334 | ||
1335 | $self->logmsg ("configure required packages\n"); | |
1336 | $self->ve_command ("dpkg --force-confold --skip-same-version --configure -a"); | |
1337 | ||
1338 | # set postfix defaults | |
1339 | if ($mta eq 'postfix') { | |
1340 | $data = "postfix postfix/main_mailer_type select Local only\n"; | |
1341 | $self->ve_debconfig_set ($data); | |
1342 | ||
1343 | $data = "postmaster: root\nwebmaster: root\n"; | |
1344 | write_file ($data, "$rootdir/etc/aliases"); | |
1345 | } | |
1346 | ||
1347 | if ($suite eq 'jaunty') { | |
1348 | # jaunty does not create /var/run/network, so network startup fails. | |
1349 | # so we do not use tmpfs for /var/run and /var/lock | |
1350 | $self->run_command ("sed -e 's/RAMRUN=yes/RAMRUN=no/' -e 's/RAMLOCK=yes/RAMLOCK=no/' -i $rootdir/etc/default/rcS"); | |
1351 | # and create the directory here | |
1352 | $self->run_command ("mkdir $rootdir/var/run/network"); | |
1353 | } | |
1354 | ||
1355 | # unpack base packages | |
1356 | foreach my $p (@$important) { | |
1357 | $self->ve_dpkg ('unpack', $p); | |
1358 | } | |
1359 | ||
1360 | # start loopback | |
b6a1907e DM |
1361 | if (-x "$rootdir/sbin/ifconfig") { |
1362 | $self->ve_command ("ifconfig lo up"); | |
1363 | } else { | |
1364 | $self->ve_command ("ip link set lo up"); | |
1365 | } | |
8ab34b87 DM |
1366 | |
1367 | $self->logmsg ("configure important packages\n"); | |
1368 | $self->ve_command ("dpkg --force-confold --skip-same-version --configure -a"); | |
1369 | ||
1370 | if (-d "$rootdir/etc/event.d") { | |
1371 | unlink <$rootdir/etc/event.d/tty*>; | |
1372 | } | |
1373 | ||
1374 | if (-f "$rootdir/etc/inittab") { | |
756b991f | 1375 | $self->run_command ("sed -i -e '/getty\\s38400\\stty[23456]/d' '$rootdir/etc/inittab'"); |
8ab34b87 DM |
1376 | } |
1377 | ||
1378 | # Link /etc/mtab to /proc/mounts, so df and friends will work: | |
1379 | unlink "$rootdir/etc/mtab"; | |
1380 | $self->ve_command ("ln -s /proc/mounts /etc/mtab"); | |
1381 | ||
1382 | # reset password | |
1383 | $self->ve_command ("usermod -L root"); | |
1384 | ||
8ab34b87 DM |
1385 | if ($mta eq 'postfix') { |
1386 | $data = "postfix postfix/main_mailer_type select No configuration\n"; | |
1387 | $self->ve_debconfig_set ($data); | |
1388 | ||
1389 | unlink "$rootdir/etc/mailname"; | |
1390 | write_file ($postfix_main_cf, "$rootdir/etc/postfix/main.cf"); | |
1391 | } | |
1392 | ||
1393 | if (!$opts->{minimal}) { | |
1394 | # unpack standard packages | |
1395 | foreach my $p (@$standard) { | |
1396 | $self->ve_dpkg ('unpack', $p); | |
1397 | } | |
1398 | ||
1399 | $self->logmsg ("configure standard packages\n"); | |
1400 | $self->ve_command ("dpkg --force-confold --skip-same-version --configure -a"); | |
1401 | } | |
1402 | ||
1403 | # disable HWCLOCK access | |
1404 | $self->run_command ("echo 'HWCLOCKACCESS=no' >> '$rootdir/etc/default/rcS'"); | |
1405 | ||
1406 | # disable hald | |
1407 | $self->ve_divert_add ("/usr/sbin/hald"); | |
1408 | ||
1409 | # disable /dev/urandom init | |
1410 | $self->run_command ("install -m 0755 '$script_init_urandom' '$rootdir/etc/init.d/urandom'"); | |
1411 | ||
1412 | if ($suite eq 'etch' || $suite eq 'hardy' || $suite eq 'intrepid' || $suite eq 'jaunty') { | |
1413 | # avoid klogd start | |
1414 | $self->ve_divert_add ("/sbin/klogd"); | |
1415 | } | |
1416 | ||
1417 | # remove unnecessays sysctl entries to avoid warnings | |
1418 | my $cmd = 'sed'; | |
1419 | $cmd .= ' -e \'s/^\(kernel\.printk.*\)/#\1/\''; | |
1420 | $cmd .= ' -e \'s/^\(kernel\.maps_protect.*\)/#\1/\''; | |
1421 | $cmd .= ' -e \'s/^\(fs\.inotify\.max_user_watches.*\)/#\1/\''; | |
1422 | $cmd .= ' -e \'s/^\(vm\.mmap_min_addr.*\)/#\1/\''; | |
1423 | $cmd .= " -i '$rootdir/etc/sysctl.conf'"; | |
1424 | $self->run_command ($cmd); | |
1425 | ||
1426 | my $bindv6only = "$rootdir/etc/sysctl.d/bindv6only.conf"; | |
1427 | if (-f $bindv6only) { | |
1428 | $cmd = 'sed'; | |
1429 | $cmd .= ' -e \'s/^\(net\.ipv6\.bindv6only.*\)/#\1/\''; | |
1430 | $cmd .= " -i '$bindv6only'"; | |
1431 | $self->run_command ($cmd); | |
1432 | } | |
1433 | ||
1434 | if ($suite eq 'hardy' || $suite eq 'intrepid' || $suite eq 'jaunty') { | |
1435 | # disable tty init (console-setup) | |
1436 | my $cmd = 'sed'; | |
1437 | $cmd .= ' -e \'s/^\(ACTIVE_CONSOLES=.*\)/ACTIVE_CONSOLES=/\''; | |
1438 | $cmd .= " -i '$rootdir/etc/default/console-setup'"; | |
1439 | $self->run_command ($cmd); | |
1440 | } | |
1441 | ||
1442 | if ($suite eq 'intrepid') { | |
1443 | # remove sysctl setup (avoid warnings at startup) | |
1444 | my $filelist = "$rootdir/etc/sysctl.d/10-console-messages.conf"; | |
1445 | $filelist .= " $rootdir/etc/sysctl.d/10-process-security.conf"; | |
1446 | $filelist .= " $rootdir/etc/sysctl.d/10-network-security.conf"; | |
1447 | $self->run_command ("rm $filelist"); | |
1448 | } | |
1449 | if ($suite eq 'jaunty') { | |
1450 | # remove sysctl setup (avoid warnings at startup) | |
1451 | my $filelist = "$rootdir/etc/sysctl.d/10-console-messages.conf"; | |
1452 | $filelist .= " $rootdir/etc/sysctl.d/10-network-security.conf"; | |
1453 | $self->run_command ("rm $filelist"); | |
1454 | } | |
1455 | } | |
1456 | ||
1457 | sub enter { | |
1458 | my ($self) = @_; | |
1459 | ||
1460 | my $veid = $self->{veid}; | |
6fe8088f | 1461 | my $conffile = $self->{veconffile}; |
8ab34b87 DM |
1462 | |
1463 | my $vestat = $self->ve_status(); | |
1464 | ||
1465 | if (!$vestat->{exist}) { | |
1466 | $self->logmsg ("Please create the appliance first (bootstrap)"); | |
1467 | return; | |
1468 | } | |
1469 | ||
1470 | if (!$vestat->{running}) { | |
6fe8088f | 1471 | $self->run_command ("lxc-start -n $veid -f $conffile"); |
8ab34b87 DM |
1472 | } |
1473 | ||
6fe8088f | 1474 | system ("lxc-attach -n $veid --rcfile $conffile --clear-env"); |
8ab34b87 DM |
1475 | } |
1476 | ||
1477 | sub ve_mysql_command { | |
1478 | my ($self, $sql, $password) = @_; | |
1479 | ||
1480 | #my $bootstrap = "/usr/sbin/mysqld --bootstrap --user=mysql --skip-grant-tables " . | |
1481 | #"--skip-bdb --skip-innodb --skip-ndbcluster"; | |
1482 | ||
1483 | $self->ve_command ("mysql", $sql); | |
1484 | } | |
1485 | ||
1486 | sub ve_mysql_bootstrap { | |
1487 | my ($self, $sql, $password) = @_; | |
1488 | ||
1489 | my $cmd; | |
1490 | ||
1491 | my $suite = $self->{config}->{suite}; | |
dcc99599 DM |
1492 | |
1493 | if ($suite eq 'jessie') { | |
f0134ed2 | 1494 | my $rootdir = $self->{rootfs}; |
dcc99599 DM |
1495 | $self->run_command ("sed -e 's/^key_buffer\\s*=/key_buffer_size =/' -i $rootdir/etc/mysql/my.cnf"); |
1496 | } | |
1497 | ||
12152e08 | 1498 | if ($suite eq 'squeeze' || $suite eq 'wheezy' || $suite eq 'jessie') { |
8ab34b87 DM |
1499 | $cmd = "/usr/sbin/mysqld --bootstrap --user=mysql --skip-grant-tables"; |
1500 | ||
1501 | } else { | |
1502 | $cmd = "/usr/sbin/mysqld --bootstrap --user=mysql --skip-grant-tables " . | |
1503 | "--skip-bdb --skip-innodb --skip-ndbcluster"; | |
1504 | } | |
1505 | ||
1506 | $self->ve_command ($cmd, $sql); | |
1507 | } | |
1508 | ||
1509 | sub compute_required { | |
1510 | my ($self, $pkglist) = @_; | |
1511 | ||
1512 | my $pkginfo = $self->pkginfo(); | |
1513 | my $instpkgs = $self->read_installed (); | |
1514 | ||
1515 | my $closure = {}; | |
1516 | __record_provides ($pkginfo, $closure, [keys %$instpkgs]); | |
1517 | ||
1518 | return $self->closure ($closure, $pkglist); | |
1519 | } | |
1520 | ||
1521 | sub task_postgres { | |
1522 | my ($self, $opts) = @_; | |
1523 | ||
1524 | my @supp = ('7.4', '8.1'); | |
1525 | my $pgversion = '8.1'; | |
1526 | ||
1527 | my $suite = $self->{config}->{suite}; | |
1528 | ||
1529 | if ($suite eq 'lenny' || $suite eq 'hardy' || $suite eq 'intrepid' || $suite eq 'jaunty') { | |
1530 | @supp = ('8.3'); | |
1531 | $pgversion = '8.3'; | |
1532 | } elsif ($suite eq 'squeeze') { | |
1533 | @supp = ('8.4'); | |
1534 | $pgversion = '8.4'; | |
632ea034 DM |
1535 | } elsif ($suite eq 'wheezy') { |
1536 | @supp = ('9.1'); | |
1537 | $pgversion = '9.1'; | |
5d0f537e DM |
1538 | } elsif ($suite eq 'jessie') { |
1539 | @supp = ('9.4'); | |
1540 | $pgversion = '9.4'; | |
b6a1907e DM |
1541 | } elsif ($suite eq 'stretch') { |
1542 | @supp = ('9.6'); | |
1543 | $pgversion = '9.6'; | |
8ab34b87 DM |
1544 | } |
1545 | ||
1546 | $pgversion = $opts->{version} if $opts->{version}; | |
1547 | ||
1548 | die "unsupported postgres version '$pgversion'\n" | |
1549 | if !grep { $pgversion eq $_; } @supp; | |
1550 | ||
f0134ed2 | 1551 | my $rootdir = $self->{rootfs}; |
8ab34b87 DM |
1552 | |
1553 | my $required = $self->compute_required (["postgresql-$pgversion"]); | |
1554 | ||
1555 | $self->cache_packages ($required); | |
1556 | ||
1557 | $self->ve_dpkg ('install', @$required); | |
1558 | ||
632ea034 | 1559 | my $iscript = "postgresql-$pgversion"; |
b6a1907e DM |
1560 | if ($suite eq 'squeeze' || $suite eq 'wheezy' || $suite eq 'jessie' || |
1561 | $suite eq 'stretch') { | |
1562 | $iscript = 'postgresql'; | |
632ea034 | 1563 | } |
8ab34b87 DM |
1564 | |
1565 | $self->ve_command ("/etc/init.d/$iscript start") if $opts->{start}; | |
1566 | } | |
1567 | ||
1568 | sub task_mysql { | |
1569 | my ($self, $opts) = @_; | |
1570 | ||
1571 | my $password = $opts->{password}; | |
f0134ed2 | 1572 | my $rootdir = $self->{rootfs}; |
8ab34b87 DM |
1573 | |
1574 | my $suite = $self->{config}->{suite}; | |
1575 | ||
632ea034 DM |
1576 | my $ver = '5.0'; |
1577 | if ($suite eq 'squeeze') { | |
1578 | $ver = '5.1'; | |
5d0f537e | 1579 | } elsif ($suite eq 'wheezy' || $suite eq 'jessie') { |
632ea034 | 1580 | $ver = '5.5'; |
b6a1907e DM |
1581 | } else { |
1582 | die "task_mysql: unsupported suite '$suite'"; | |
632ea034 | 1583 | } |
8ab34b87 DM |
1584 | |
1585 | my $required = $self->compute_required (['mysql-common', "mysql-server-$ver"]); | |
1586 | ||
1587 | $self->cache_packages ($required); | |
1588 | ||
1589 | $self->ve_dpkg ('install', @$required); | |
1590 | ||
1591 | # fix security (see /usr/bin/mysql_secure_installation) | |
1592 | my $sql = "DELETE FROM mysql.user WHERE User='';\n" . | |
1593 | "DELETE FROM mysql.user WHERE User='root' AND Host!='localhost';\n" . | |
1594 | "FLUSH PRIVILEGES;\n"; | |
1595 | $self->ve_mysql_bootstrap ($sql); | |
1596 | ||
1597 | if ($password) { | |
1598 | ||
1599 | my $rpw = $password eq 'random' ? 'admin' : $password; | |
1600 | ||
1601 | my $sql = "USE mysql;\n" . | |
1602 | "UPDATE user SET password=PASSWORD(\"$rpw\") WHERE user='root';\n" . | |
1603 | "FLUSH PRIVILEGES;\n"; | |
1604 | $self->ve_mysql_bootstrap ($sql); | |
1605 | ||
1606 | write_file ("[client]\nuser=root\npassword=\"$rpw\"\n", "$rootdir/root/.my.cnf", 0600); | |
1607 | if ($password eq 'random') { | |
1608 | $self->install_init_script ($script_mysql_randompw, 2, 20); | |
1609 | } | |
1610 | } | |
1611 | ||
1612 | $self->ve_command ("/etc/init.d/mysql start") if $opts->{start}; | |
1613 | } | |
1614 | ||
1615 | sub task_php { | |
1616 | my ($self, $opts) = @_; | |
1617 | ||
1618 | my $memlimit = $opts->{memlimit}; | |
f0134ed2 | 1619 | my $rootdir = $self->{rootfs}; |
8ab34b87 DM |
1620 | |
1621 | my $required = $self->compute_required ([qw (php5 php5-cli libapache2-mod-php5 php5-gd)]); | |
1622 | ||
1623 | $self->cache_packages ($required); | |
1624 | ||
1625 | $self->ve_dpkg ('install', @$required); | |
1626 | ||
1627 | if ($memlimit) { | |
1628 | $self->run_command ("sed -e 's/^\\s*memory_limit\\s*=.*;/memory_limit = ${memlimit}M;/' -i $rootdir/etc/php5/apache2/php.ini"); | |
1629 | } | |
1630 | } | |
1631 | ||
1632 | sub install { | |
1633 | my ($self, $pkglist, $unpack) = @_; | |
1634 | ||
1635 | my $required = $self->compute_required ($pkglist); | |
1636 | ||
1637 | $self->cache_packages ($required); | |
1638 | ||
1639 | $self->ve_dpkg ($unpack ? 'unpack' : 'install', @$required); | |
1640 | } | |
1641 | ||
1642 | sub cleanup { | |
1643 | my ($self, $distclean) = @_; | |
1644 | ||
1645 | unlink $self->{logfile}; | |
1646 | unlink "$self->{targetname}.tar"; | |
1647 | unlink "$self->{targetname}.tar.gz"; | |
1648 | ||
1649 | $self->ve_destroy (); | |
1650 | unlink ".veid"; | |
1651 | ||
1652 | rmtree $self->{cachedir} if $distclean && !$self->{config}->{cachedir}; | |
1653 | ||
1654 | rmtree $self->{infodir}; | |
1655 | ||
1656 | } | |
1657 | ||
1658 | 1; |