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