]> git.proxmox.com Git - dab.git/blob - DAB.pm
debian 11: use short ostype
[dab.git] / DAB.pm
1 package PVE::DAB;
2
3 use strict;
4 use warnings;
5 use IO::File;
6 use File::Path;
7 use File::Basename;
8 use IO::Select;
9 use IPC::Open2;
10 use IPC::Open3;
11 use POSIX qw (LONG_MAX);
12 use UUID;
13 use Cwd;
14
15 # fixme: lock container ?
16
17 my $dablibdir = "/usr/lib/dab";
18 my $devicetar = "$dablibdir/devices.tar.gz";
19 my $default_env = "$dablibdir/scripts/defenv";
20 my $fake_init = "$dablibdir/scripts/init.pl";
21 my $script_ssh_init = "$dablibdir/scripts/ssh_gen_host_keys";
22 my $script_mysql_randompw = "$dablibdir/scripts/mysql_randompw";
23 my $script_init_urandom = "$dablibdir/scripts/init_urandom";
24
25 my $postfix_main_cf = <<EOD;
26 # See /usr/share/postfix/main.cf.dist for a commented, more complete version
27
28 smtpd_banner = \$myhostname ESMTP \$mail_name (Debian/GNU)
29 biff = no
30
31 # appending .domain is the MUA's job.
32 append_dot_mydomain = no
33
34 # Uncomment the next line to generate "delayed mail" warnings
35 #delay_warning_time = 4h
36
37 alias_maps = hash:/etc/aliases
38 alias_database = hash:/etc/aliases
39 mydestination = \$myhostname, localhost.\$mydomain, localhost
40 relayhost =
41 mynetworks = 127.0.0.0/8
42 inet_interfaces = loopback-only
43 recipient_delimiter = +
44
45 compatibility_level = 2
46
47 EOD
48
49 # produce apt compatible filenames (/var/lib/apt/lists)
50 sub __url_to_filename {
51 my $url = shift;
52
53 $url =~ s|^\S+://||;
54 $url =~ s|_|%5f|g;
55 $url =~ s|/|_|g;
56
57 return $url;
58 }
59
60 # defaults:
61 # origin: debian
62 my $supported_suites = {
63 'bullseye' => {
64 ostype => "debian-11",
65 },
66 'buster' => {
67 ostype => "debian-10",
68 },
69 'stretch' => {
70 ostype => "debian-9.0",
71 },
72 'jessie' => {
73 ostype => "debian-8.0",
74 },
75 'wheezy' => {
76 ostype => "debian-7.0",
77 },
78 'squeeze' => {
79 ostype => "debian-6.0",
80 },
81 'lenny' => {
82 ostype => "debian-5.0",
83 },
84 'etch' => {
85 ostype => "debian-4.0",
86 },
87
88 # DEVUAN
89 'devuan-jessie' => {
90 suite => 'jessie',
91 ostype => "devuan-1.0",
92 },
93 'devuan-ascii' => {
94 suite => 'ascii',
95 ostype => "devuan-2.0",
96 },
97 'ascii' => {
98 ostype => "devuan-2.0",
99 },
100 'beowulf' => {
101 ostype => "devuan-3.0",
102 },
103 'chimaera' => {
104 ostype => "devuan-4.0",
105 },
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 },
168 'focal' => {
169 ostype => "ubuntu-20.04",
170 origin => 'ubuntu',
171 },
172 'groovy' => {
173 ostype => "ubuntu-20.10",
174 origin => 'ubuntu',
175 },
176 'hirsute' => {
177 ostype => "ubuntu-21.04",
178 origin => 'ubuntu',
179 },
180 };
181
182 sub 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
194 sub 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
212 sub 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
227 sub 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
244 sub 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
289 sub 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
356 sub logmsg {
357 my $self = shift;
358 print STDERR @_;
359 $self->writelog (@_);
360 }
361
362 sub writelog {
363 my $self = shift;
364 my $fd = $self->{logfd};
365 print $fd @_;
366 }
367
368 sub __sample_config {
369 my ($self) = @_;
370
371 my $data = '';
372 my $arch = $self->{config}->{architecture};
373
374 my $ostype = $self->{config}->{ostype};
375
376 if ($ostype =~ m/^de(bi|vu)an-/) {
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 }
383 $data .= "lxc.uts.name = localhost\n";
384 $data .= "lxc.rootfs.path = $self->{rootfs}\n";
385
386 return $data;
387 }
388
389 sub __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
399
400 $self->{working_dir} = getcwd;
401 $self->{veconffile} = "$self->{working_dir}/config";
402 $self->{rootfs} = "$self->{working_dir}/rootfs";
403
404 if ($cid) {
405 $self->{veid} = $cid;
406 return $cid;
407 }
408
409 my $uuid;
410 my $uuid_str;
411 UUID::generate($uuid);
412 UUID::unparse($uuid, $uuid_str);
413 $self->{veid} = $uuid_str;
414
415 my $fd = IO::File->new (">.veid") ||
416 die "unable to write '.veid'\n";
417 print $fd "$self->{veid}\n";
418 close ($fd);
419
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 - $!";
428
429 $self->logmsg ("allocated VE $self->{veid}\n");
430
431 return $self->{veid};
432 }
433
434 sub 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";
458
459 my $suiteinfo = get_suite_info($suite);
460 $suite = $suiteinfo->{suite};
461 $config->{ostype} = $suiteinfo->{ostype};
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}) {
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 );
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 );
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') {
502 my $comp = "main restricted universe multiverse";
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 );
508 } else {
509 die "implement me";
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)
569
570 if ($suite eq 'vivid' || $suite eq 'wily' || $suite eq 'xenial' ||
571 $suite eq 'yakkety' || $suite eq 'zesty' || $suite eq 'artful' ||
572 $suite eq 'bionic' || $suite eq 'cosmic' || $suite eq 'disco' ||
573 $suite eq 'eoan' || $suite eq 'focal' || $suite eq 'groovy'
574 || $suite eq 'hirsute'
575 ) {
576 push @$incl, 'isc-dhcp-client';
577 push @$excl, qw(libmodule-build-perl);
578 } elsif ($suite eq 'trusty') {
579 push @$excl, qw(systemd systemd-services libpam-systemd libsystemd-daemon0 memtest86+);
580 } elsif ($suite eq 'precise') {
581 push @$excl, qw(systemd systemd-services libpam-systemd libsystemd-daemon0 memtest86+ ubuntu-standard);
582 } elsif ($suite eq 'hardy') {
583 push @$excl, qw(kbd);
584 push @$excl, qw(apparmor apparmor-utils ntfs-3g
585 friendly-recovery);
586 } elsif ($suite eq 'intrepid' || $suite eq 'jaunty') {
587 push @$excl, qw(apparmor apparmor-utils libapparmor1 libapparmor-perl
588 libntfs-3g28 ntfs-3g friendly-recovery);
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);
594 } elsif ($suite eq 'stretch' || $suite eq 'buster' || $suite eq 'bullseye') {
595 push @$excl, qw(module-init-tools pciutils hdparm
596 memtest86+ parted);
597 } else {
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
608 sub 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
621 # FIXME: seems a bit like a hacky way??
622 my $COMPRESSOR = {
623 ext => 'gz',
624 decomp => 'gzip -d',
625 };
626 if ($self->{config}->{suite} eq 'bullseye') {
627 $COMPRESSOR = {
628 ext => 'xz',
629 decomp => 'xz -d',
630 };
631 }
632
633 foreach my $ss (@{$self->{sources}}) {
634 my $src = $ss->{mirror} || $ss->{source};
635 my $path = "dists/$ss->{suite}/Release";
636 my $url = "$src/$path";
637 my $target = __url_to_filename ("$ss->{source}/$path");
638 eval {
639 $self->download ($url, "$infodir/$target");
640 $self->download ("$url.gpg", "$infodir/$target.gpg");
641 # fixme: impl. verify (needs --keyring option)
642 };
643 if (my $err = $@) {
644 print $logfd $@;
645 warn "Release info ignored\n";
646 };
647
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 $self->download ($pkgsrc, $target);
653 $self->run_command ("$COMPRESSOR->{decomp} '$target'");
654 }
655 }
656 }
657
658 sub write_config {
659 my ($self, $filename, $size) = @_;
660
661 my $config = $self->{config};
662
663 my $data = '';
664
665 $data .= "Name: $config->{name}\n";
666 $data .= "Version: $config->{version}\n";
667 $data .= "Type: lxc\n";
668 $data .= "OS: $config->{ostype}\n";
669 $data .= "Section: $config->{section}\n";
670 $data .= "Maintainer: $config->{maintainer}\n";
671 $data .= "Architecture: $config->{architecture}\n";
672 $data .= "Installed-Size: $size\n";
673
674 # optional
675 $data .= "Infopage: $config->{infopage}\n" if $config->{infopage};
676 $data .= "ManageUrl: $config->{manageurl}\n" if $config->{manageurl};
677 $data .= "Certified: $config->{certified}\n" if $config->{certified};
678
679 # description
680 $data .= "Description: $config->{headline}\n";
681 $data .= "$config->{description}\n" if $config->{description};
682
683 write_file ($data, $filename, 0644);
684 }
685
686 sub finalize {
687 my ($self, $opts) = @_;
688
689 my $suite = $self->{config}->{suite};
690 my $infodir = $self->{infodir};
691 my $arch = $self->{config}->{architecture};
692
693 my $instpkgs = $self->read_installed ();
694 my $pkginfo = $self->pkginfo();
695 my $veid = $self->{veid};
696 my $conffile = $self->{veconffile};
697 my $rootdir = $self->{rootfs};
698
699 my $vestat = $self->ve_status();
700 die "ve not running - unable to finalize\n" if !$vestat->{running};
701
702 # cleanup mysqld
703 if (-f "$rootdir/etc/init.d/mysql") {
704 $self->ve_command ("/etc/init.d/mysql stop");
705 }
706
707 if (!($opts->{keepmycnf} || (-f "$rootdir/etc/init.d/mysql_randompw"))) {
708 unlink "$rootdir/root/.my.cnf";
709 }
710
711 if ($suite eq 'etch') {
712 # enable apache2 startup
713 if ($instpkgs->{apache2}) {
714 write_file ("NO_START=0\n", "$rootdir/etc/default/apache2");
715 } else {
716 unlink "$rootdir/etc/default/apache2";
717 }
718 }
719 $self->logmsg ("cleanup package status\n");
720 # prevent auto selection of all standard, required or important
721 # packages which are not installed
722 foreach my $pkg (keys %$pkginfo) {
723 my $pri = $pkginfo->{$pkg}->{priority};
724 if ($pri && ($pri eq 'required' || $pri eq 'important'
725 || $pri eq 'standard')) {
726 if (!$instpkgs->{$pkg}) {
727 $self->ve_dpkg_set_selection ($pkg, 'purge');
728 }
729 }
730 }
731
732 $self->ve_command ("apt-get clean");
733
734 $self->logmsg ("update available package list\n");
735
736 $self->ve_command ("dpkg --clear-avail");
737 foreach my $ss (@{$self->{sources}}) {
738 my $relsrc = __url_to_filename ("$ss->{source}/dists/$ss->{suite}/Release");
739 if (-f "$infodir/$relsrc" && -f "$infodir/$relsrc.gpg") {
740 $self->run_command ("cp '$infodir/$relsrc' '$rootdir/var/lib/apt/lists/$relsrc'");
741 $self->run_command ("cp '$infodir/$relsrc.gpg' '$rootdir/var/lib/apt/lists/$relsrc.gpg'");
742 }
743 foreach my $comp (@{$ss->{comp}}) {
744 my $src = __url_to_filename ("$ss->{source}/dists/$ss->{suite}/" .
745 "$comp/binary-$arch/Packages");
746 my $target = "/var/lib/apt/lists/$src";
747 $self->run_command ("cp '$infodir/$src' '$rootdir/$target'");
748 $self->ve_command ("dpkg --merge-avail '$target'");
749 }
750 }
751
752 # set dselect default method
753 write_file ("apt apt\n", "$rootdir/var/lib/dpkg/cmethopt");
754
755 $self->ve_divert_remove ("/usr/sbin/policy-rc.d");
756
757 $self->ve_divert_remove ("/sbin/start-stop-daemon");
758
759 $self->ve_divert_remove ("/sbin/init");
760
761 # finally stop the VE
762 $self->run_command ("lxc-stop -n $veid --rcfile $conffile --kill");
763
764 unlink "$rootdir/sbin/defenv";
765
766 unlink <$rootdir/root/dead.letter*>;
767
768 unlink "$rootdir/var/log/init.log";
769
770 unlink "$rootdir/aquota.group";
771
772 unlink "$rootdir/aquota.user";
773
774 write_file ("", "$rootdir/var/log/syslog");
775
776 my $get_path_size = sub {
777 my ($path) = @_;
778 my $sizestr = $self->run_command ("du -sm $path", undef, 1);
779 if ($sizestr =~ m/^(\d+)\s+\Q$path\E$/) {
780 return int($1);
781 } else {
782 die "unable to detect size for '$path'\n";
783 }
784 };
785
786 $self->logmsg ("detecting final appliance size: ");
787 my $size = $get_path_size->($rootdir);
788 $self->logmsg ("$size MB\n");
789
790 $self->write_config ("$rootdir/etc/appliance.info", $size);
791
792 $self->logmsg ("creating final appliance archive\n");
793
794 my $target = "$self->{targetname}.tar";
795
796 my $compressor = $opts->{compressor} // 'gz';
797 my $compressor2cmd_map = {
798 gz => 'gzip',
799 gzip => 'gzip',
800 zst => 'zstd -9',
801 zstd => 'zstd -9',
802 'zstd-max' => 'zstd -19 -T0', # maximal level where the decompressor can still run efficiently
803 };
804 my $compressor2ending = {
805 gzip => 'gz',
806 zstd => 'zst',
807 'zstd-max' => 'zst',
808 };
809 my $compressor_cmd = $compressor2cmd_map->{$compressor};
810 die "unkown compressor '$compressor', use one of: ". join(', ', sort keys %$compressor2cmd_map)
811 if !defined($compressor_cmd);
812
813 my $ending = $compressor2ending->{$compressor} // $compressor;
814 my $final_archive = "${target}.${ending}";
815 unlink $target;
816 unlink $final_archive;
817
818 $self->run_command ("tar cpf $target --numeric-owner -C '$rootdir' ./etc/appliance.info");
819 $self->run_command ("tar rpf $target --numeric-owner -C '$rootdir' --exclude ./etc/appliance.info .");
820 $self->run_command ("$compressor_cmd $target");
821
822 $self->logmsg ("detecting final commpressed appliance size: ");
823 $size = $get_path_size->($final_archive);
824 $self->logmsg ("$size MB\n");
825
826 $self->logmsg ("appliance archive: $final_archive\n");
827 }
828
829 sub read_installed {
830 my ($self) = @_;
831
832 my $rootdir = $self->{rootfs};
833
834 my $pkgfilelist = "$rootdir/var/lib/dpkg/status";
835 local $/ = '';
836 open (PKGLST, "<$pkgfilelist") ||
837 die "unable to open '$pkgfilelist'";
838
839 my $pkglist = {};
840
841 while (my $rec = <PKGLST>) {
842 chomp $rec;
843 $rec =~ s/\n\s+/ /g;
844 $rec .= "\n";
845 my $res = {};
846
847 while ($rec =~ s/^([^:]+):\s+(.*)\s*\n//) {
848 $res->{lc $1} = $2;
849 }
850
851 my $pkg = $res->{'package'};
852 if (my $status = $res->{status}) {
853 my @sa = split (/\s+/, $status);
854 my $stat = $sa[0];
855 if ($stat && ($stat ne 'purge')) {
856 $pkglist->{$pkg} = $res;
857 }
858 }
859 }
860
861 close (PKGLST);
862
863 return $pkglist;
864 }
865
866 sub ve_status {
867 my ($self) = @_;
868
869 my $veid = $self->{veid};
870
871 my $res = { running => 0 };
872
873 $res->{exist} = 1 if -d "$self->{rootfs}/usr";
874
875 my $filename = "/proc/net/unix";
876
877 # similar test is used by lcxcontainers.c: list_active_containers
878 my $fh = IO::File->new ($filename, "r");
879 return $res if !$fh;
880
881 while (defined(my $line = <$fh>)) {
882 if ($line =~ m/^[a-f0-9]+:\s\S+\s\S+\s\S+\s\S+\s\S+\s\d+\s(\S+)$/) {
883 my $path = $1;
884 if ($path =~ m!^@/\S+/$veid/command$!) {
885 $res->{running} = 1;
886 }
887 }
888 }
889 close($fh);
890
891 return $res;
892 }
893
894 sub ve_command {
895 my ($self, $cmd, $input) = @_;
896
897 my $veid = $self->{veid};
898 my $conffile = $self->{veconffile};
899
900 if (ref ($cmd) eq 'ARRAY') {
901 unshift @$cmd, 'lxc-attach', '-n', $veid, '--rcfile', $conffile, '--clear-env', '--', 'defenv';
902 $self->run_command ($cmd, $input);
903 } else {
904 $self->run_command ("lxc-attach -n $veid --rcfile $conffile --clear-env -- defenv $cmd", $input);
905 }
906 }
907
908 # like ve_command, but pipes stdin correctly
909 sub ve_exec {
910 my ($self, @cmd) = @_;
911
912 my $veid = $self->{veid};
913 my $conffile = $self->{veconffile};
914
915 my $reader;
916 my $pid = open2($reader, "<&STDIN", 'lxc-attach', '-n', $veid, '--rcfile', $conffile, '--',
917 'defenv', @cmd) || die "unable to exec command";
918
919 while (defined (my $line = <$reader>)) {
920 $self->logmsg ($line);
921 }
922
923 waitpid ($pid, 0);
924 my $rc = $? >> 8;
925
926 die "ve_exec failed - status $rc\n" if $rc != 0;
927 }
928
929 sub ve_divert_add {
930 my ($self, $filename) = @_;
931
932 $self->ve_command ("dpkg-divert --add --divert '$filename.distrib' " .
933 "--rename '$filename'");
934 }
935 sub ve_divert_remove {
936 my ($self, $filename) = @_;
937
938 my $rootdir = $self->{rootfs};
939
940 unlink "$rootdir/$filename";
941 $self->ve_command ("dpkg-divert --remove --rename '$filename'");
942 }
943
944 sub ve_debconfig_set {
945 my ($self, $dcdata) = @_;
946
947 my $rootdir = $self->{rootfs};
948 my $cfgfile = "/tmp/debconf.txt";
949 write_file ($dcdata, "$rootdir/$cfgfile");
950 $self->ve_command ("debconf-set-selections $cfgfile");
951 unlink "$rootdir/$cfgfile";
952 }
953
954 sub ve_dpkg_set_selection {
955 my ($self, $pkg, $status) = @_;
956
957 $self->ve_command ("dpkg --set-selections", "$pkg $status");
958 }
959
960 sub ve_dpkg {
961 my ($self, $cmd, @pkglist) = @_;
962
963 return if !scalar (@pkglist);
964
965 my $pkginfo = $self->pkginfo();
966
967 my $rootdir = $self->{rootfs};
968 my $cachedir = $self->{cachedir};
969
970 my @files;
971
972 foreach my $pkg (@pkglist) {
973 my $filename = $self->getpkgfile ($pkg);
974 $self->run_command ("cp '$cachedir/$filename' '$rootdir/$filename'");
975 push @files, "/$filename";
976 $self->logmsg ("$cmd: $pkg\n");
977 }
978
979 my $fl = join (' ', @files);
980
981 if ($cmd eq 'install') {
982 $self->ve_command ("dpkg --force-depends --force-confold --install $fl");
983 } elsif ($cmd eq 'unpack') {
984 $self->ve_command ("dpkg --force-depends --unpack $fl");
985 } else {
986 die "internal error";
987 }
988
989 foreach my $fn (@files) { unlink "$rootdir$fn"; }
990 }
991
992 sub ve_destroy {
993 my ($self) = @_;
994
995 my $veid = $self->{veid}; # fixme
996 my $conffile = $self->{veconffile};
997
998 my $vestat = $self->ve_status();
999 if ($vestat->{running}) {
1000 $self->run_command ("lxc-stop -n $veid --rcfile $conffile --kill");
1001 }
1002
1003 rmtree $self->{rootfs};
1004 unlink $self->{veconffile};
1005 }
1006
1007 sub ve_init {
1008 my ($self) = @_;
1009
1010 my $veid = $self->{veid};
1011 my $conffile = $self->{veconffile};
1012
1013 $self->logmsg ("initialize VE $veid\n");
1014
1015 my $vestat = $self->ve_status();
1016 if ($vestat->{running}) {
1017 $self->run_command ("lxc-stop -n $veid --rcfile $conffile --kill");
1018 }
1019
1020 rmtree $self->{rootfs};
1021 mkpath $self->{rootfs};
1022 }
1023
1024 sub __deb_version_cmp {
1025 my ($cur, $op, $new) = @_;
1026
1027 if (system("dpkg", "--compare-versions", $cur, $op, $new) == 0) {
1028 return 1;
1029 }
1030
1031 return 0;
1032 }
1033
1034 sub __parse_packages {
1035 my ($pkginfo, $filename, $src) = @_;
1036
1037 local $/ = '';
1038 open (PKGLST, "<$filename") ||
1039 die "unable to open '$filename'";
1040
1041 while (my $rec = <PKGLST>) {
1042 $rec =~ s/\n\s+/ /g;
1043 chomp $rec;
1044 $rec .= "\n";
1045
1046 my $res = {};
1047
1048 while ($rec =~ s/^([^:]+):\s+(.*)\s*\n//) {
1049 $res->{lc $1} = $2;
1050 }
1051
1052 my $pkg = $res->{'package'};
1053 if ($pkg && $res->{'filename'}) {
1054 my $cur;
1055 if (my $info = $pkginfo->{$pkg}) {
1056 $cur = $info->{version};
1057 }
1058 my $new = $res->{version};
1059 if (!$cur || __deb_version_cmp ($cur, 'lt', $new)) {
1060 if ($src) {
1061 $res->{url} = "$src/$res->{'filename'}";
1062 } else {
1063 die "no url for package '$pkg'" if !$res->{url};
1064 }
1065 $pkginfo->{$pkg} = $res;
1066 }
1067 }
1068 }
1069
1070 close (PKGLST);
1071 }
1072
1073 sub pkginfo {
1074 my ($self) = @_;
1075
1076 return $self->{pkginfo} if $self->{pkginfo};
1077
1078 my $infodir = $self->{infodir};
1079 my $arch = $self->{config}->{architecture};
1080
1081 my $availfn = "$infodir/available";
1082
1083 my $pkginfo = {};
1084 my $pkgcount = 0;
1085
1086 # reading 'available' is faster, because it only contains latest version
1087 # (no need to do slow version compares)
1088 if (-f $availfn) {
1089 __parse_packages ($pkginfo, $availfn);
1090 $self->{pkginfo} = $pkginfo;
1091 return $pkginfo;
1092 }
1093
1094 $self->logmsg ("generating available package list\n");
1095
1096 foreach my $ss (@{$self->{sources}}) {
1097 foreach my $comp (@{$ss->{comp}}) {
1098 my $url = "$ss->{source}/dists/$ss->{suite}/$comp/binary-$arch/Packages";
1099 my $pkgfilelist = "$infodir/" . __url_to_filename ($url);
1100
1101 my $src = $ss->{mirror} || $ss->{source};
1102
1103 __parse_packages ($pkginfo, $pkgfilelist, $src);
1104 }
1105 }
1106
1107 if (my $dep = $self->{config}->{depends}) {
1108 foreach my $d (split (/,/, $dep)) {
1109 if ($d =~ m/^\s*(\S+)\s*(\((\S+)\s+(\S+)\)\s*)?$/) {
1110 my ($pkg, $op, $rver) = ($1, $3, $4);
1111 $self->logmsg ("checking dependencies: $d\n");
1112 my $info = $pkginfo->{$pkg};
1113 die "package '$pkg' not available\n" if !$info;
1114 if ($op) {
1115 my $cver = $info->{version};
1116 if (!__deb_version_cmp ($cver, $op, $rver)) {
1117 die "detected wrong version '$cver'\n";
1118 }
1119 }
1120 } else {
1121 die "syntax error in depends field";
1122 }
1123 }
1124 }
1125
1126 $self->{pkginfo} = $pkginfo;
1127
1128 my $tmpfn = "$availfn.tmp$$";
1129 my $fd = IO::File->new (">$tmpfn");
1130 foreach my $pkg (sort keys %$pkginfo) {
1131 my $info = $pkginfo->{$pkg};
1132 print $fd "package: $pkg\n";
1133 foreach my $k (sort keys %$info) {
1134 next if $k eq 'description';
1135 next if $k eq 'package';
1136 my $v = $info->{$k};
1137 print $fd "$k: $v\n" if $v;
1138 }
1139 print $fd "description: $info->{description}\n" if $info->{description};
1140 print $fd "\n";
1141 }
1142 close ($fd);
1143
1144 rename ($tmpfn, $availfn);
1145
1146 return $pkginfo;
1147 }
1148
1149 sub __record_provides {
1150 my ($pkginfo, $closure, $list, $skipself) = @_;
1151
1152 foreach my $pname (@$list) {
1153 my $info = $pkginfo->{$pname};
1154 # fixme: if someone install packages directly using dpkg, there
1155 # is no entry in 'available', only in 'status'. In that case, we
1156 # should extract info from $instpkgs
1157 if (!$info) {
1158 warn "hint: ignoring provides for '$pname' - package not in 'available' list.\n";
1159 next;
1160 }
1161 if (my $prov = $info->{provides}) {
1162 my @pl = split (',', $prov);
1163 foreach my $p (@pl) {
1164 $p =~ m/\s*(\S+)/;
1165 if (!($skipself && (grep { $1 eq $_ } @$list))) {
1166 $closure->{$1} = 1;
1167 }
1168 }
1169 }
1170 $closure->{$pname} = 1 if !$skipself;
1171 }
1172 }
1173
1174 sub closure {
1175 my ($self, $closure, $list) = @_;
1176
1177 my $pkginfo = $self->pkginfo();
1178
1179 # first, record provided packages
1180 __record_provides ($pkginfo, $closure, $list, 1);
1181
1182 my $pkghash = {};
1183 my $pkglist = [];
1184
1185 # then resolve dependencies
1186 foreach my $pname (@$list) {
1187 __closure_single ($pkginfo, $closure, $pkghash, $pkglist, $pname, $self->{excl});
1188 }
1189
1190 return $pkglist;
1191 }
1192
1193 sub __closure_single {
1194 my ($pkginfo, $closure, $pkghash, $pkglist, $pname, $excl) = @_;
1195
1196 $pname =~ s/^\s+//;
1197 $pname =~ s/\s+$//;
1198 $pname =~ s/:any$//;
1199
1200 return if $closure->{$pname};
1201
1202 my $info = $pkginfo->{$pname} || die "no such package '$pname'";
1203
1204 my $dep = $info->{depends};
1205 my $predep = $info->{'pre-depends'};
1206
1207 my $size = $info->{size};
1208 my $url = $info->{url};
1209
1210 $url || die "$pname: no url for package '$pname'";
1211
1212 if (!$pkghash->{$pname}) {
1213 unshift @$pkglist, $pname;
1214 $pkghash->{$pname} = 1;
1215 }
1216
1217 __record_provides ($pkginfo, $closure, [$pname]) if $info->{provides};
1218
1219 $closure->{$pname} = 1;
1220
1221 #print "$url\n";
1222
1223 my @l;
1224
1225 push @l, split (/,/, $predep) if $predep;
1226 push @l, split (/,/, $dep) if $dep;
1227
1228 DEPEND: foreach my $p (@l) {
1229 my @l1 = split (/\|/, $p);
1230 foreach my $p1 (@l1) {
1231 if ($p1 =~ m/^\s*(\S+).*/) {
1232 #printf (STDERR "$pname: $p --> $1\n");
1233 if ($closure->{$1}) {
1234 next DEPEND; # dependency already met
1235 }
1236 }
1237 }
1238 # search for non-excluded alternative
1239 my $found;
1240 foreach my $p1 (@l1) {
1241 if ($p1 =~ m/^\s*(\S+).*/) {
1242 next if grep { $1 eq $_ } @$excl;
1243 $found = $1;
1244 last;
1245 }
1246 }
1247 die "package '$pname' depends on exclusion '$p'\n" if !$found;
1248
1249 #printf (STDERR "$pname: $p --> $found\n");
1250
1251 __closure_single ($pkginfo, $closure, $pkghash, $pkglist, $found, $excl);
1252 }
1253 }
1254
1255 sub cache_packages {
1256 my ($self, $pkglist) = @_;
1257
1258 foreach my $pkg (@$pkglist) {
1259 $self->getpkgfile ($pkg);
1260 }
1261 }
1262
1263 sub getpkgfile {
1264 my ($self, $pkg) = @_;
1265
1266 my $pkginfo = $self->pkginfo();
1267 my $info = $pkginfo->{$pkg} || die "no such package '$pkg'";
1268 my $cachedir = $self->{cachedir};
1269
1270 my $url = $info->{url};
1271
1272 my $filename;
1273 if ($url =~ m|/([^/]+.deb)$|) {
1274 $filename = $1;
1275 } else {
1276 die "internal error";
1277 }
1278
1279 return $filename if -f "$cachedir/$filename";
1280
1281 mkpath $cachedir;
1282
1283 $self->download ($url, "$cachedir/$filename");
1284
1285 return $filename;
1286 }
1287
1288 sub install_init_script {
1289 my ($self, $script, $runlevel, $prio) = @_;
1290
1291 my $suite = $self->{config}->{suite};
1292 my $rootdir = $self->{rootfs};
1293
1294 my $base = basename ($script);
1295 my $target = "$rootdir/etc/init.d/$base";
1296
1297 $self->run_command ("install -m 0755 '$script' '$target'");
1298 if ($suite eq 'etch' || $suite eq 'lenny') {
1299 $self->ve_command ("update-rc.d $base start $prio $runlevel .");
1300 } elsif ($suite eq 'xenial' || $suite eq 'wily' || $suite eq 'vivid' ||
1301 $suite eq 'yakkety' || $suite eq 'zesty' || $suite eq 'artful' ||
1302 $suite eq 'bionic' || $suite eq 'cosmic' || $suite eq 'disco' ||
1303 $suite eq 'eoan' || $suite eq 'focal' || $suite eq 'groovy'
1304 || $suite eq 'hirsute'
1305 ) {
1306 die "unable to install init script (system uses systemd)\n";
1307 } elsif ($suite eq 'trusty' || $suite eq 'precise') {
1308 die "unable to install init script (system uses upstart)\n";
1309 } else {
1310 $self->ve_command ("insserv $base");
1311 }
1312
1313 return $target;
1314 }
1315
1316 sub bootstrap {
1317 my ($self, $opts) = @_;
1318
1319 my $pkginfo = $self->pkginfo();
1320 my $veid = $self->{veid};
1321 my $suite = $self->{config}->{suite};
1322
1323 my $important = [ @{$self->{incl}} ];
1324 my $required;
1325 my $standard;
1326
1327 my $mta = $opts->{exim} ? 'exim' : 'postfix';
1328
1329 if ($mta eq 'postfix') {
1330 push @$important, "postfix";
1331 }
1332
1333 foreach my $p (sort keys %$pkginfo) {
1334 next if grep { $p eq $_ } @{$self->{excl}};
1335 my $pri = $pkginfo->{$p}->{priority};
1336 next if !$pri;
1337 next if $mta ne 'exim' && $p =~ m/exim/;
1338 next if $p =~ m/(selinux|semanage|policycoreutils)/;
1339
1340 push @$required, $p if $pri eq 'required';
1341 push @$important, $p if $pri eq 'important';
1342 push @$standard, $p if $pri eq 'standard' && !$opts->{minimal};
1343 }
1344
1345 my $closure = {};
1346 $required = $self->closure ($closure, $required);
1347 $important = $self->closure ($closure, $important);
1348
1349 if (!$opts->{minimal}) {
1350 push @$standard, 'xbase-clients';
1351 $standard = $self->closure ($closure, $standard);
1352 }
1353
1354 # test if we have all 'ubuntu-minimal' and 'ubuntu-standard' packages
1355 # except those explicitly excluded
1356 if ($suite eq 'hardy' || $suite eq 'intrepid' || $suite eq 'jaunty') {
1357 my $mdeps = $pkginfo->{'ubuntu-minimal'}->{depends};
1358 foreach my $d (split (/,/, $mdeps)) {
1359 if ($d =~ m/^\s*(\S+)$/) {
1360 my $pkg = $1;
1361 next if $closure->{$pkg};
1362 next if grep { $pkg eq $_ } @{$self->{excl}};
1363 die "missing ubuntu-minimal package '$pkg'\n";
1364 }
1365 }
1366 if (!$opts->{minimal}) {
1367 $mdeps = $pkginfo->{'ubuntu-standard'}->{depends};
1368 foreach my $d (split (/,/, $mdeps)) {
1369 if ($d =~ m/^\s*(\S+)$/) {
1370 my $pkg = $1;
1371 next if $closure->{$pkg};
1372 next if grep { $pkg eq $_ } @{$self->{excl}};
1373 die "missing ubuntu-standard package '$pkg'\n";
1374 }
1375 }
1376 }
1377 }
1378
1379 # download/cache all files first
1380 $self->cache_packages ($required);
1381 $self->cache_packages ($important);
1382 $self->cache_packages ($standard);
1383
1384 my $rootdir = $self->{rootfs};
1385
1386 # extract required packages first
1387 $self->logmsg ("create basic environment\n");
1388 foreach my $p (@$required) {
1389 my $filename = $self->getpkgfile ($p);
1390 my $content = $self->run_command("ar -t '$self->{cachedir}/$filename'", undef, 1);
1391 if ($content =~ m/^data.tar.xz$/m) {
1392 $self->run_command ("ar -p '$self->{cachedir}/$filename' data.tar.xz | tar -C '$rootdir' -xJf -");
1393 } else {
1394 $self->run_command ("ar -p '$self->{cachedir}/$filename' data.tar.gz | tar -C '$rootdir' -xzf -");
1395 }
1396 }
1397
1398 # fake dpkg status
1399 my $data = "Package: dpkg\n" .
1400 "Version: $pkginfo->{dpkg}->{version}\n" .
1401 "Status: install ok installed\n";
1402
1403 write_file ($data, "$rootdir/var/lib/dpkg/status");
1404 write_file ("", "$rootdir/var/lib/dpkg/info/dpkg.list");
1405 write_file ("", "$rootdir/var/lib/dpkg/available");
1406
1407 $data = '';
1408 foreach my $ss (@{$self->{sources}}) {
1409 my $url = $ss->{source};
1410 my $comp = join (' ', @{$ss->{comp}});
1411 $data .= "deb $url $ss->{suite} $comp\n\n";
1412 }
1413
1414 write_file ($data, "$rootdir/etc/apt/sources.list");
1415
1416 $data = "# UNCONFIGURED FSTAB FOR BASE SYSTEM\n";
1417 write_file ($data, "$rootdir/etc/fstab", 0644);
1418
1419 write_file ("localhost\n", "$rootdir/etc/hostname", 0644);
1420
1421 # avoid warnings about non-existent resolv.conf
1422 write_file ("", "$rootdir/etc/resolv.conf", 0644);
1423
1424 if (
1425 $suite eq 'hirsute' || $suite eq 'groovy' || $suite eq 'focal' ||
1426 $suite eq 'eoan' || $suite eq 'disco' || $suite eq 'cosmic' ||
1427 $suite eq 'bionic' || $suite eq 'artful' ||
1428 $suite eq 'zesty' || $suite eq 'yakkety' || $suite eq 'xenial' ||
1429 $suite eq 'wily') {
1430 # no need to configure loopback device
1431 } else {
1432 $data = "auto lo\niface lo inet loopback\n";
1433 mkdir "$rootdir/etc/network";
1434 write_file ($data, "$rootdir/etc/network/interfaces", 0644);
1435 }
1436
1437 # setup devices
1438 $self->run_command ("tar xzf '$devicetar' -C '$rootdir'");
1439
1440 # avoid warnings about missing default locale
1441 write_file ("LANG=\"C\"\n", "$rootdir/etc/default/locale", 0644);
1442
1443 # fake init
1444 rename ("$rootdir/sbin/init", "$rootdir/sbin/init.org");
1445 $self->run_command ("cp '$fake_init' '$rootdir/sbin/init'");
1446
1447 $self->run_command ("cp '$default_env' '$rootdir/sbin/defenv'");
1448
1449 $self->run_command ("lxc-start -n $veid -f $self->{veconffile}");
1450
1451 $self->logmsg ("initialize ld cache\n");
1452 $self->ve_command ("/sbin/ldconfig");
1453 $self->run_command ("ln -sf mawk '$rootdir/usr/bin/awk'");
1454
1455 $self->logmsg ("installing packages\n");
1456
1457 $self->ve_dpkg ('install', 'base-files', 'base-passwd');
1458
1459 $self->ve_dpkg ('install', 'dpkg');
1460
1461 $self->run_command ("ln -sf /usr/share/zoneinfo/UTC '$rootdir/etc/localtime'");
1462
1463 $self->run_command ("ln -sf bash '$rootdir/bin/sh'");
1464
1465 $self->ve_dpkg ('install', 'libc6');
1466 $self->ve_dpkg ('install', 'perl-base');
1467
1468 unlink "$rootdir/usr/bin/awk";
1469
1470 $self->ve_dpkg ('install', 'mawk');
1471 $self->ve_dpkg ('install', 'debconf');
1472
1473 # unpack required packages
1474 foreach my $p (@$required) {
1475 $self->ve_dpkg ('unpack', $p);
1476 }
1477
1478 rename ("$rootdir/sbin/init.org", "$rootdir/sbin/init");
1479 $self->ve_divert_add ("/sbin/init");
1480 $self->run_command ("cp '$fake_init' '$rootdir/sbin/init'");
1481
1482 # disable service activation
1483 $self->ve_divert_add ("/usr/sbin/policy-rc.d");
1484 $data = "#!/bin/sh\nexit 101\n";
1485 write_file ($data, "$rootdir/usr/sbin/policy-rc.d", 755);
1486
1487 # disable start-stop-daemon
1488 $self->ve_divert_add ("/sbin/start-stop-daemon");
1489 $data = <<EOD;
1490 #!/bin/sh
1491 echo
1492 echo \"Warning: Fake start-stop-daemon called, doing nothing\"
1493 EOD
1494 write_file ($data, "$rootdir/sbin/start-stop-daemon", 0755);
1495
1496 # disable udevd
1497 $self->ve_divert_add ("/sbin/udevd");
1498
1499 if ($suite eq 'etch') {
1500 # disable apache2 startup
1501 write_file ("NO_START=1\n", "$rootdir/etc/default/apache2");
1502 }
1503
1504 $self->logmsg ("configure required packages\n");
1505 $self->ve_command ("dpkg --force-confold --skip-same-version --configure -a");
1506
1507 # set postfix defaults
1508 if ($mta eq 'postfix') {
1509 $data = "postfix postfix/main_mailer_type select Local only\n";
1510 $self->ve_debconfig_set ($data);
1511
1512 $data = "postmaster: root\nwebmaster: root\n";
1513 write_file ($data, "$rootdir/etc/aliases");
1514 }
1515
1516 if ($suite eq 'jaunty') {
1517 # jaunty does not create /var/run/network, so network startup fails.
1518 # so we do not use tmpfs for /var/run and /var/lock
1519 $self->run_command ("sed -e 's/RAMRUN=yes/RAMRUN=no/' -e 's/RAMLOCK=yes/RAMLOCK=no/' -i $rootdir/etc/default/rcS");
1520 # and create the directory here
1521 $self->run_command ("mkdir $rootdir/var/run/network");
1522 }
1523
1524 # unpack base packages
1525 foreach my $p (@$important) {
1526 $self->ve_dpkg ('unpack', $p);
1527 }
1528
1529 # start loopback
1530 if (-x "$rootdir/sbin/ifconfig") {
1531 $self->ve_command ("ifconfig lo up");
1532 } else {
1533 $self->ve_command ("ip link set lo up");
1534 }
1535
1536 $self->logmsg ("configure important packages\n");
1537 $self->ve_command ("dpkg --force-confold --skip-same-version --configure -a");
1538
1539 if (-d "$rootdir/etc/event.d") {
1540 unlink <$rootdir/etc/event.d/tty*>;
1541 }
1542
1543 if (-f "$rootdir/etc/inittab") {
1544 $self->run_command ("sed -i -e '/getty\\s38400\\stty[23456]/d' '$rootdir/etc/inittab'");
1545 }
1546
1547 # Link /etc/mtab to /proc/mounts, so df and friends will work:
1548 unlink "$rootdir/etc/mtab";
1549 $self->ve_command ("ln -s /proc/mounts /etc/mtab");
1550
1551 # reset password
1552 $self->ve_command ("usermod -L root");
1553
1554 if ($mta eq 'postfix') {
1555 $data = "postfix postfix/main_mailer_type select No configuration\n";
1556 $self->ve_debconfig_set ($data);
1557
1558 unlink "$rootdir/etc/mailname";
1559 write_file ($postfix_main_cf, "$rootdir/etc/postfix/main.cf");
1560 }
1561
1562 if (!$opts->{minimal}) {
1563 # unpack standard packages
1564 foreach my $p (@$standard) {
1565 $self->ve_dpkg ('unpack', $p);
1566 }
1567
1568 $self->logmsg ("configure standard packages\n");
1569 $self->ve_command ("dpkg --force-confold --skip-same-version --configure -a");
1570 }
1571
1572 # disable HWCLOCK access
1573 $self->run_command ("echo 'HWCLOCKACCESS=no' >> '$rootdir/etc/default/rcS'");
1574
1575 # disable hald
1576 $self->ve_divert_add ("/usr/sbin/hald");
1577
1578 # disable /dev/urandom init
1579 $self->run_command ("install -m 0755 '$script_init_urandom' '$rootdir/etc/init.d/urandom'");
1580
1581 if ($suite eq 'etch' || $suite eq 'hardy' || $suite eq 'intrepid' || $suite eq 'jaunty') {
1582 # avoid klogd start
1583 $self->ve_divert_add ("/sbin/klogd");
1584 }
1585
1586 # remove unnecessays sysctl entries to avoid warnings
1587 my $cmd = 'sed';
1588 $cmd .= ' -e \'s/^\(kernel\.printk.*\)/#\1/\'';
1589 $cmd .= ' -e \'s/^\(kernel\.maps_protect.*\)/#\1/\'';
1590 $cmd .= ' -e \'s/^\(fs\.inotify\.max_user_watches.*\)/#\1/\'';
1591 $cmd .= ' -e \'s/^\(vm\.mmap_min_addr.*\)/#\1/\'';
1592 $cmd .= " -i '$rootdir/etc/sysctl.conf'";
1593 $self->run_command ($cmd);
1594
1595 my $bindv6only = "$rootdir/etc/sysctl.d/bindv6only.conf";
1596 if (-f $bindv6only) {
1597 $cmd = 'sed';
1598 $cmd .= ' -e \'s/^\(net\.ipv6\.bindv6only.*\)/#\1/\'';
1599 $cmd .= " -i '$bindv6only'";
1600 $self->run_command ($cmd);
1601 }
1602
1603 if ($suite eq 'hardy' || $suite eq 'intrepid' || $suite eq 'jaunty') {
1604 # disable tty init (console-setup)
1605 my $cmd = 'sed';
1606 $cmd .= ' -e \'s/^\(ACTIVE_CONSOLES=.*\)/ACTIVE_CONSOLES=/\'';
1607 $cmd .= " -i '$rootdir/etc/default/console-setup'";
1608 $self->run_command ($cmd);
1609 }
1610
1611 if ($suite eq 'intrepid' || $suite eq 'jaunty') {
1612 # remove sysctl setup (avoid warnings at startup)
1613 my $filelist = "$rootdir/etc/sysctl.d/10-console-messages.conf";
1614 $filelist .= " $rootdir/etc/sysctl.d/10-process-security.conf" if $suite eq 'intrepid';
1615 $filelist .= " $rootdir/etc/sysctl.d/10-network-security.conf";
1616 $self->run_command ("rm $filelist");
1617 }
1618
1619 if (-e "$rootdir/lib/systemd/system/sys-kernel-config.mount") {
1620 $self->ve_command ("ln -s /dev/null /etc/systemd/system/sys-kernel-debug.mount");
1621 }
1622 }
1623
1624 sub enter {
1625 my ($self) = @_;
1626
1627 my $veid = $self->{veid};
1628 my $conffile = $self->{veconffile};
1629
1630 my $vestat = $self->ve_status();
1631
1632 if (!$vestat->{exist}) {
1633 $self->logmsg ("Please create the appliance first (bootstrap)");
1634 return;
1635 }
1636
1637 if (!$vestat->{running}) {
1638 $self->run_command ("lxc-start -n $veid -f $conffile");
1639 }
1640
1641 system ("lxc-attach -n $veid --rcfile $conffile --clear-env");
1642 }
1643
1644 sub ve_mysql_command {
1645 my ($self, $sql, $password) = @_;
1646
1647 #my $bootstrap = "/usr/sbin/mysqld --bootstrap --user=mysql --skip-grant-tables " .
1648 #"--skip-bdb --skip-innodb --skip-ndbcluster";
1649
1650 $self->ve_command ("mysql", $sql);
1651 }
1652
1653 sub ve_mysql_bootstrap {
1654 my ($self, $sql, $password) = @_;
1655
1656 my $cmd;
1657
1658 my $suite = $self->{config}->{suite};
1659
1660 if ($suite eq 'jessie') {
1661 my $rootdir = $self->{rootfs};
1662 $self->run_command ("sed -e 's/^key_buffer\\s*=/key_buffer_size =/' -i $rootdir/etc/mysql/my.cnf");
1663 }
1664
1665 if ($suite eq 'squeeze' || $suite eq 'wheezy' || $suite eq 'jessie') {
1666 $cmd = "/usr/sbin/mysqld --bootstrap --user=mysql --skip-grant-tables";
1667
1668 } else {
1669 $cmd = "/usr/sbin/mysqld --bootstrap --user=mysql --skip-grant-tables " .
1670 "--skip-bdb --skip-innodb --skip-ndbcluster";
1671 }
1672
1673 $self->ve_command ($cmd, $sql);
1674 }
1675
1676 sub compute_required {
1677 my ($self, $pkglist) = @_;
1678
1679 my $pkginfo = $self->pkginfo();
1680 my $instpkgs = $self->read_installed ();
1681
1682 my $closure = {};
1683 __record_provides ($pkginfo, $closure, [keys %$instpkgs]);
1684
1685 return $self->closure ($closure, $pkglist);
1686 }
1687
1688 sub task_postgres {
1689 my ($self, $opts) = @_;
1690
1691 my @supp = ('7.4', '8.1');
1692 my $pgversion = '8.1';
1693
1694 my $suite = $self->{config}->{suite};
1695
1696 if ($suite eq 'lenny' || $suite eq 'hardy' || $suite eq 'intrepid' || $suite eq 'jaunty') {
1697 @supp = ('8.3');
1698 $pgversion = '8.3';
1699 } elsif ($suite eq 'squeeze') {
1700 @supp = ('8.4');
1701 $pgversion = '8.4';
1702 } elsif ($suite eq 'wheezy') {
1703 @supp = ('9.1');
1704 $pgversion = '9.1';
1705 } elsif ($suite eq 'jessie') {
1706 @supp = ('9.4');
1707 $pgversion = '9.4';
1708 } elsif ($suite eq 'stretch') {
1709 @supp = ('9.6');
1710 $pgversion = '9.6';
1711 } elsif ($suite eq 'buster') {
1712 @supp = ('11');
1713 $pgversion = '11';
1714 } elsif ($suite eq 'bullseye') {
1715 # FIXME update on freeze!
1716 @supp = ('12');
1717 $pgversion = '12';
1718 }
1719
1720 $pgversion = $opts->{version} if $opts->{version};
1721
1722 die "unsupported postgres version '$pgversion'\n"
1723 if !grep { $pgversion eq $_; } @supp;
1724
1725 my $rootdir = $self->{rootfs};
1726
1727 my $required = $self->compute_required (["postgresql-$pgversion"]);
1728
1729 $self->cache_packages ($required);
1730
1731 $self->ve_dpkg ('install', @$required);
1732
1733 my $iscript = "postgresql-$pgversion";
1734 if ($suite eq 'squeeze' || $suite eq 'wheezy' || $suite eq 'jessie' ||
1735 $suite eq 'stretch') {
1736 $iscript = 'postgresql';
1737 }
1738
1739 $self->ve_command ("/etc/init.d/$iscript start") if $opts->{start};
1740 }
1741
1742 sub task_mysql {
1743 my ($self, $opts) = @_;
1744
1745 my $password = $opts->{password};
1746 my $rootdir = $self->{rootfs};
1747
1748 my $suite = $self->{config}->{suite};
1749
1750 my $ver = '5.0';
1751 if ($suite eq 'squeeze') {
1752 $ver = '5.1';
1753 } elsif ($suite eq 'wheezy' || $suite eq 'jessie') {
1754 $ver = '5.5';
1755 } else {
1756 die "task_mysql: unsupported suite '$suite'";
1757 }
1758
1759 my $required = $self->compute_required (['mysql-common', "mysql-server-$ver"]);
1760
1761 $self->cache_packages ($required);
1762
1763 $self->ve_dpkg ('install', @$required);
1764
1765 # fix security (see /usr/bin/mysql_secure_installation)
1766 my $sql = "DELETE FROM mysql.user WHERE User='';\n" .
1767 "DELETE FROM mysql.user WHERE User='root' AND Host!='localhost';\n" .
1768 "FLUSH PRIVILEGES;\n";
1769 $self->ve_mysql_bootstrap ($sql);
1770
1771 if ($password) {
1772
1773 my $rpw = $password eq 'random' ? 'admin' : $password;
1774
1775 my $sql = "USE mysql;\n" .
1776 "UPDATE user SET password=PASSWORD(\"$rpw\") WHERE user='root';\n" .
1777 "FLUSH PRIVILEGES;\n";
1778 $self->ve_mysql_bootstrap ($sql);
1779
1780 write_file ("[client]\nuser=root\npassword=\"$rpw\"\n", "$rootdir/root/.my.cnf", 0600);
1781 if ($password eq 'random') {
1782 $self->install_init_script ($script_mysql_randompw, 2, 20);
1783 }
1784 }
1785
1786 $self->ve_command ("/etc/init.d/mysql start") if $opts->{start};
1787 }
1788
1789 sub task_php {
1790 my ($self, $opts) = @_;
1791
1792 my $memlimit = $opts->{memlimit};
1793 my $rootdir = $self->{rootfs};
1794
1795 my $required = $self->compute_required ([qw (php5 php5-cli libapache2-mod-php5 php5-gd)]);
1796
1797 $self->cache_packages ($required);
1798
1799 $self->ve_dpkg ('install', @$required);
1800
1801 if ($memlimit) {
1802 $self->run_command ("sed -e 's/^\\s*memory_limit\\s*=.*;/memory_limit = ${memlimit}M;/' -i $rootdir/etc/php5/apache2/php.ini");
1803 }
1804 }
1805
1806 sub install {
1807 my ($self, $pkglist, $unpack) = @_;
1808
1809 my $required = $self->compute_required ($pkglist);
1810
1811 $self->cache_packages ($required);
1812
1813 $self->ve_dpkg ($unpack ? 'unpack' : 'install', @$required);
1814 }
1815
1816 sub cleanup {
1817 my ($self, $distclean) = @_;
1818
1819 unlink $self->{logfile};
1820 unlink "$self->{targetname}.tar";
1821 unlink "$self->{targetname}.tar.gz";
1822
1823 $self->ve_destroy ();
1824 unlink ".veid";
1825
1826 rmtree $self->{cachedir} if $distclean && !$self->{config}->{cachedir};
1827
1828 rmtree $self->{infodir};
1829
1830 }
1831
1832 1;