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