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