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