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