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