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