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