]> git.proxmox.com Git - dab.git/blob - DAB.pm
support Devuan 4 and 5
[dab.git] / DAB.pm
1 package PVE::DAB;
2
3 use strict;
4 use warnings;
5 use IO::File;
6 use File::Path;
7 use File::Basename;
8 use IO::Select;
9 use IPC::Open2;
10 use IPC::Open3;
11 use POSIX qw (LONG_MAX);
12 use UUID;
13 use Cwd;
14
15 # fixme: lock container ?
16
17 my $dablibdir = "/usr/lib/dab";
18 my $devicetar = "$dablibdir/devices.tar.gz";
19 my $default_env = "$dablibdir/scripts/defenv";
20 my $fake_init = "$dablibdir/scripts/init.pl";
21 my $script_ssh_init = "$dablibdir/scripts/ssh_gen_host_keys";
22 my $script_mysql_randompw = "$dablibdir/scripts/mysql_randompw";
23 my $script_init_urandom = "$dablibdir/scripts/init_urandom";
24
25 my $postfix_main_cf = <<EOD;
26 # See /usr/share/postfix/main.cf.dist for a commented, more complete version
27
28 smtpd_banner = \$myhostname ESMTP \$mail_name (Debian/GNU)
29 biff = no
30
31 # appending .domain is the MUA's job.
32 append_dot_mydomain = no
33
34 # Uncomment the next line to generate "delayed mail" warnings
35 #delay_warning_time = 4h
36
37 alias_maps = hash:/etc/aliases
38 alias_database = hash:/etc/aliases
39 mydestination = \$myhostname, localhost.\$mydomain, localhost
40 relayhost =
41 mynetworks = 127.0.0.0/8
42 inet_interfaces = loopback-only
43 recipient_delimiter = +
44
45 compatibility_level = 2
46
47 EOD
48
49 # produce apt compatible filenames (/var/lib/apt/lists)
50 sub __url_to_filename {
51 my $url = shift;
52
53 $url =~ s|^\S+://||;
54 $url =~ s|_|%5f|g;
55 $url =~ s|/|_|g;
56
57 return $url;
58 }
59
60 # defaults:
61 # origin: debian
62 my $supported_suites = {
63 'bullseye' => {
64 ostype => "debian-11.0",
65 },
66 'buster' => {
67 ostype => "debian-10",
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 zst => 'zstd',
800 };
801 my $compressor_cmd = $compressor2cmd_map->{$compressor};
802 die "unkown compressor '$compressor', use one of: ". join(', ', sort keys %$compressor2cmd_map)
803 if !defined($compressor_cmd);
804
805 my $final_archive = "${target}.${compressor}";
806 unlink $target;
807 unlink $final_archive;
808
809 $self->run_command ("tar cpf $target --numeric-owner -C '$rootdir' ./etc/appliance.info");
810 $self->run_command ("tar rpf $target --numeric-owner -C '$rootdir' --exclude ./etc/appliance.info .");
811 $self->run_command ("$compressor_cmd $target");
812
813 $self->logmsg ("detecting final commpressed appliance size: ");
814 $size = $get_path_size->($final_archive);
815 $self->logmsg ("$size MB\n");
816
817 $self->logmsg ("appliance archive: $final_archive\n");
818 }
819
820 sub read_installed {
821 my ($self) = @_;
822
823 my $rootdir = $self->{rootfs};
824
825 my $pkgfilelist = "$rootdir/var/lib/dpkg/status";
826 local $/ = '';
827 open (PKGLST, "<$pkgfilelist") ||
828 die "unable to open '$pkgfilelist'";
829
830 my $pkglist = {};
831
832 while (my $rec = <PKGLST>) {
833 chomp $rec;
834 $rec =~ s/\n\s+/ /g;
835 $rec .= "\n";
836 my $res = {};
837
838 while ($rec =~ s/^([^:]+):\s+(.*)\s*\n//) {
839 $res->{lc $1} = $2;
840 }
841
842 my $pkg = $res->{'package'};
843 if (my $status = $res->{status}) {
844 my @sa = split (/\s+/, $status);
845 my $stat = $sa[0];
846 if ($stat && ($stat ne 'purge')) {
847 $pkglist->{$pkg} = $res;
848 }
849 }
850 }
851
852 close (PKGLST);
853
854 return $pkglist;
855 }
856
857 sub ve_status {
858 my ($self) = @_;
859
860 my $veid = $self->{veid};
861
862 my $res = { running => 0 };
863
864 $res->{exist} = 1 if -d "$self->{rootfs}/usr";
865
866 my $filename = "/proc/net/unix";
867
868 # similar test is used by lcxcontainers.c: list_active_containers
869 my $fh = IO::File->new ($filename, "r");
870 return $res if !$fh;
871
872 while (defined(my $line = <$fh>)) {
873 if ($line =~ m/^[a-f0-9]+:\s\S+\s\S+\s\S+\s\S+\s\S+\s\d+\s(\S+)$/) {
874 my $path = $1;
875 if ($path =~ m!^@/\S+/$veid/command$!) {
876 $res->{running} = 1;
877 }
878 }
879 }
880 close($fh);
881
882 return $res;
883 }
884
885 sub ve_command {
886 my ($self, $cmd, $input) = @_;
887
888 my $veid = $self->{veid};
889 my $conffile = $self->{veconffile};
890
891 if (ref ($cmd) eq 'ARRAY') {
892 unshift @$cmd, 'lxc-attach', '-n', $veid, '--rcfile', $conffile, '--clear-env', '--', 'defenv';
893 $self->run_command ($cmd, $input);
894 } else {
895 $self->run_command ("lxc-attach -n $veid --rcfile $conffile --clear-env -- defenv $cmd", $input);
896 }
897 }
898
899 # like ve_command, but pipes stdin correctly
900 sub ve_exec {
901 my ($self, @cmd) = @_;
902
903 my $veid = $self->{veid};
904 my $conffile = $self->{veconffile};
905
906 my $reader;
907 my $pid = open2($reader, "<&STDIN", 'lxc-attach', '-n', $veid, '--rcfile', $conffile, '--',
908 'defenv', @cmd) || die "unable to exec command";
909
910 while (defined (my $line = <$reader>)) {
911 $self->logmsg ($line);
912 }
913
914 waitpid ($pid, 0);
915 my $rc = $? >> 8;
916
917 die "ve_exec failed - status $rc\n" if $rc != 0;
918 }
919
920 sub ve_divert_add {
921 my ($self, $filename) = @_;
922
923 $self->ve_command ("dpkg-divert --add --divert '$filename.distrib' " .
924 "--rename '$filename'");
925 }
926 sub ve_divert_remove {
927 my ($self, $filename) = @_;
928
929 my $rootdir = $self->{rootfs};
930
931 unlink "$rootdir/$filename";
932 $self->ve_command ("dpkg-divert --remove --rename '$filename'");
933 }
934
935 sub ve_debconfig_set {
936 my ($self, $dcdata) = @_;
937
938 my $rootdir = $self->{rootfs};
939 my $cfgfile = "/tmp/debconf.txt";
940 write_file ($dcdata, "$rootdir/$cfgfile");
941 $self->ve_command ("debconf-set-selections $cfgfile");
942 unlink "$rootdir/$cfgfile";
943 }
944
945 sub ve_dpkg_set_selection {
946 my ($self, $pkg, $status) = @_;
947
948 $self->ve_command ("dpkg --set-selections", "$pkg $status");
949 }
950
951 sub ve_dpkg {
952 my ($self, $cmd, @pkglist) = @_;
953
954 return if !scalar (@pkglist);
955
956 my $pkginfo = $self->pkginfo();
957
958 my $rootdir = $self->{rootfs};
959 my $cachedir = $self->{cachedir};
960
961 my @files;
962
963 foreach my $pkg (@pkglist) {
964 my $filename = $self->getpkgfile ($pkg);
965 $self->run_command ("cp '$cachedir/$filename' '$rootdir/$filename'");
966 push @files, "/$filename";
967 $self->logmsg ("$cmd: $pkg\n");
968 }
969
970 my $fl = join (' ', @files);
971
972 if ($cmd eq 'install') {
973 $self->ve_command ("dpkg --force-depends --force-confold --install $fl");
974 } elsif ($cmd eq 'unpack') {
975 $self->ve_command ("dpkg --force-depends --unpack $fl");
976 } else {
977 die "internal error";
978 }
979
980 foreach my $fn (@files) { unlink "$rootdir$fn"; }
981 }
982
983 sub ve_destroy {
984 my ($self) = @_;
985
986 my $veid = $self->{veid}; # fixme
987 my $conffile = $self->{veconffile};
988
989 my $vestat = $self->ve_status();
990 if ($vestat->{running}) {
991 $self->run_command ("lxc-stop -n $veid --rcfile $conffile --kill");
992 }
993
994 rmtree $self->{rootfs};
995 unlink $self->{veconffile};
996 }
997
998 sub ve_init {
999 my ($self) = @_;
1000
1001 my $veid = $self->{veid};
1002 my $conffile = $self->{veconffile};
1003
1004 $self->logmsg ("initialize VE $veid\n");
1005
1006 my $vestat = $self->ve_status();
1007 if ($vestat->{running}) {
1008 $self->run_command ("lxc-stop -n $veid --rcfile $conffile --kill");
1009 }
1010
1011 rmtree $self->{rootfs};
1012 mkpath $self->{rootfs};
1013 }
1014
1015 sub __deb_version_cmp {
1016 my ($cur, $op, $new) = @_;
1017
1018 if (system("dpkg", "--compare-versions", $cur, $op, $new) == 0) {
1019 return 1;
1020 }
1021
1022 return 0;
1023 }
1024
1025 sub __parse_packages {
1026 my ($pkginfo, $filename, $src) = @_;
1027
1028 local $/ = '';
1029 open (PKGLST, "<$filename") ||
1030 die "unable to open '$filename'";
1031
1032 while (my $rec = <PKGLST>) {
1033 $rec =~ s/\n\s+/ /g;
1034 chomp $rec;
1035 $rec .= "\n";
1036
1037 my $res = {};
1038
1039 while ($rec =~ s/^([^:]+):\s+(.*)\s*\n//) {
1040 $res->{lc $1} = $2;
1041 }
1042
1043 my $pkg = $res->{'package'};
1044 if ($pkg && $res->{'filename'}) {
1045 my $cur;
1046 if (my $info = $pkginfo->{$pkg}) {
1047 $cur = $info->{version};
1048 }
1049 my $new = $res->{version};
1050 if (!$cur || __deb_version_cmp ($cur, 'lt', $new)) {
1051 if ($src) {
1052 $res->{url} = "$src/$res->{'filename'}";
1053 } else {
1054 die "no url for package '$pkg'" if !$res->{url};
1055 }
1056 $pkginfo->{$pkg} = $res;
1057 }
1058 }
1059 }
1060
1061 close (PKGLST);
1062 }
1063
1064 sub pkginfo {
1065 my ($self) = @_;
1066
1067 return $self->{pkginfo} if $self->{pkginfo};
1068
1069 my $infodir = $self->{infodir};
1070 my $arch = $self->{config}->{architecture};
1071
1072 my $availfn = "$infodir/available";
1073
1074 my $pkginfo = {};
1075 my $pkgcount = 0;
1076
1077 # reading 'available' is faster, because it only contains latest version
1078 # (no need to do slow version compares)
1079 if (-f $availfn) {
1080 __parse_packages ($pkginfo, $availfn);
1081 $self->{pkginfo} = $pkginfo;
1082 return $pkginfo;
1083 }
1084
1085 $self->logmsg ("generating available package list\n");
1086
1087 foreach my $ss (@{$self->{sources}}) {
1088 foreach my $comp (@{$ss->{comp}}) {
1089 my $url = "$ss->{source}/dists/$ss->{suite}/$comp/binary-$arch/Packages";
1090 my $pkgfilelist = "$infodir/" . __url_to_filename ($url);
1091
1092 my $src = $ss->{mirror} || $ss->{source};
1093
1094 __parse_packages ($pkginfo, $pkgfilelist, $src);
1095 }
1096 }
1097
1098 if (my $dep = $self->{config}->{depends}) {
1099 foreach my $d (split (/,/, $dep)) {
1100 if ($d =~ m/^\s*(\S+)\s*(\((\S+)\s+(\S+)\)\s*)?$/) {
1101 my ($pkg, $op, $rver) = ($1, $3, $4);
1102 $self->logmsg ("checking dependencies: $d\n");
1103 my $info = $pkginfo->{$pkg};
1104 die "package '$pkg' not available\n" if !$info;
1105 if ($op) {
1106 my $cver = $info->{version};
1107 if (!__deb_version_cmp ($cver, $op, $rver)) {
1108 die "detected wrong version '$cver'\n";
1109 }
1110 }
1111 } else {
1112 die "syntax error in depends field";
1113 }
1114 }
1115 }
1116
1117 $self->{pkginfo} = $pkginfo;
1118
1119 my $tmpfn = "$availfn.tmp$$";
1120 my $fd = IO::File->new (">$tmpfn");
1121 foreach my $pkg (sort keys %$pkginfo) {
1122 my $info = $pkginfo->{$pkg};
1123 print $fd "package: $pkg\n";
1124 foreach my $k (sort keys %$info) {
1125 next if $k eq 'description';
1126 next if $k eq 'package';
1127 my $v = $info->{$k};
1128 print $fd "$k: $v\n" if $v;
1129 }
1130 print $fd "description: $info->{description}\n" if $info->{description};
1131 print $fd "\n";
1132 }
1133 close ($fd);
1134
1135 rename ($tmpfn, $availfn);
1136
1137 return $pkginfo;
1138 }
1139
1140 sub __record_provides {
1141 my ($pkginfo, $closure, $list, $skipself) = @_;
1142
1143 foreach my $pname (@$list) {
1144 my $info = $pkginfo->{$pname};
1145 # fixme: if someone install packages directly using dpkg, there
1146 # is no entry in 'available', only in 'status'. In that case, we
1147 # should extract info from $instpkgs
1148 if (!$info) {
1149 warn "hint: ignoring provides for '$pname' - package not in 'available' list.\n";
1150 next;
1151 }
1152 if (my $prov = $info->{provides}) {
1153 my @pl = split (',', $prov);
1154 foreach my $p (@pl) {
1155 $p =~ m/\s*(\S+)/;
1156 if (!($skipself && (grep { $1 eq $_ } @$list))) {
1157 $closure->{$1} = 1;
1158 }
1159 }
1160 }
1161 $closure->{$pname} = 1 if !$skipself;
1162 }
1163 }
1164
1165 sub closure {
1166 my ($self, $closure, $list) = @_;
1167
1168 my $pkginfo = $self->pkginfo();
1169
1170 # first, record provided packages
1171 __record_provides ($pkginfo, $closure, $list, 1);
1172
1173 my $pkghash = {};
1174 my $pkglist = [];
1175
1176 # then resolve dependencies
1177 foreach my $pname (@$list) {
1178 __closure_single ($pkginfo, $closure, $pkghash, $pkglist, $pname, $self->{excl});
1179 }
1180
1181 return $pkglist;
1182 }
1183
1184 sub __closure_single {
1185 my ($pkginfo, $closure, $pkghash, $pkglist, $pname, $excl) = @_;
1186
1187 $pname =~ s/^\s+//;
1188 $pname =~ s/\s+$//;
1189 $pname =~ s/:any$//;
1190
1191 return if $closure->{$pname};
1192
1193 my $info = $pkginfo->{$pname} || die "no such package '$pname'";
1194
1195 my $dep = $info->{depends};
1196 my $predep = $info->{'pre-depends'};
1197
1198 my $size = $info->{size};
1199 my $url = $info->{url};
1200
1201 $url || die "$pname: no url for package '$pname'";
1202
1203 if (!$pkghash->{$pname}) {
1204 unshift @$pkglist, $pname;
1205 $pkghash->{$pname} = 1;
1206 }
1207
1208 __record_provides ($pkginfo, $closure, [$pname]) if $info->{provides};
1209
1210 $closure->{$pname} = 1;
1211
1212 #print "$url\n";
1213
1214 my @l;
1215
1216 push @l, split (/,/, $predep) if $predep;
1217 push @l, split (/,/, $dep) if $dep;
1218
1219 DEPEND: foreach my $p (@l) {
1220 my @l1 = split (/\|/, $p);
1221 foreach my $p1 (@l1) {
1222 if ($p1 =~ m/^\s*(\S+).*/) {
1223 #printf (STDERR "$pname: $p --> $1\n");
1224 if ($closure->{$1}) {
1225 next DEPEND; # dependency already met
1226 }
1227 }
1228 }
1229 # search for non-excluded alternative
1230 my $found;
1231 foreach my $p1 (@l1) {
1232 if ($p1 =~ m/^\s*(\S+).*/) {
1233 next if grep { $1 eq $_ } @$excl;
1234 $found = $1;
1235 last;
1236 }
1237 }
1238 die "package '$pname' depends on exclusion '$p'\n" if !$found;
1239
1240 #printf (STDERR "$pname: $p --> $found\n");
1241
1242 __closure_single ($pkginfo, $closure, $pkghash, $pkglist, $found, $excl);
1243 }
1244 }
1245
1246 sub cache_packages {
1247 my ($self, $pkglist) = @_;
1248
1249 foreach my $pkg (@$pkglist) {
1250 $self->getpkgfile ($pkg);
1251 }
1252 }
1253
1254 sub getpkgfile {
1255 my ($self, $pkg) = @_;
1256
1257 my $pkginfo = $self->pkginfo();
1258 my $info = $pkginfo->{$pkg} || die "no such package '$pkg'";
1259 my $cachedir = $self->{cachedir};
1260
1261 my $url = $info->{url};
1262
1263 my $filename;
1264 if ($url =~ m|/([^/]+.deb)$|) {
1265 $filename = $1;
1266 } else {
1267 die "internal error";
1268 }
1269
1270 return $filename if -f "$cachedir/$filename";
1271
1272 mkpath $cachedir;
1273
1274 $self->download ($url, "$cachedir/$filename");
1275
1276 return $filename;
1277 }
1278
1279 sub install_init_script {
1280 my ($self, $script, $runlevel, $prio) = @_;
1281
1282 my $suite = $self->{config}->{suite};
1283 my $rootdir = $self->{rootfs};
1284
1285 my $base = basename ($script);
1286 my $target = "$rootdir/etc/init.d/$base";
1287
1288 $self->run_command ("install -m 0755 '$script' '$target'");
1289 if ($suite eq 'etch' || $suite eq 'lenny') {
1290 $self->ve_command ("update-rc.d $base start $prio $runlevel .");
1291 } elsif ($suite eq 'xenial' || $suite eq 'wily' || $suite eq 'vivid' ||
1292 $suite eq 'yakkety' || $suite eq 'zesty' || $suite eq 'artful' ||
1293 $suite eq 'bionic' || $suite eq 'cosmic' || $suite eq 'disco' ||
1294 $suite eq 'eoan' || $suite eq 'focal' || $suite eq 'groovy'
1295 || $suite eq 'hirsute'
1296 ) {
1297 die "unable to install init script (system uses systemd)\n";
1298 } elsif ($suite eq 'trusty' || $suite eq 'precise') {
1299 die "unable to install init script (system uses upstart)\n";
1300 } else {
1301 $self->ve_command ("insserv $base");
1302 }
1303
1304 return $target;
1305 }
1306
1307 sub bootstrap {
1308 my ($self, $opts) = @_;
1309
1310 my $pkginfo = $self->pkginfo();
1311 my $veid = $self->{veid};
1312 my $suite = $self->{config}->{suite};
1313
1314 my $important = [ @{$self->{incl}} ];
1315 my $required;
1316 my $standard;
1317
1318 my $mta = $opts->{exim} ? 'exim' : 'postfix';
1319
1320 if ($mta eq 'postfix') {
1321 push @$important, "postfix";
1322 }
1323
1324 foreach my $p (sort keys %$pkginfo) {
1325 next if grep { $p eq $_ } @{$self->{excl}};
1326 my $pri = $pkginfo->{$p}->{priority};
1327 next if !$pri;
1328 next if $mta ne 'exim' && $p =~ m/exim/;
1329 next if $p =~ m/(selinux|semanage|policycoreutils)/;
1330
1331 push @$required, $p if $pri eq 'required';
1332 push @$important, $p if $pri eq 'important';
1333 push @$standard, $p if $pri eq 'standard' && !$opts->{minimal};
1334 }
1335
1336 my $closure = {};
1337 $required = $self->closure ($closure, $required);
1338 $important = $self->closure ($closure, $important);
1339
1340 if (!$opts->{minimal}) {
1341 push @$standard, 'xbase-clients';
1342 $standard = $self->closure ($closure, $standard);
1343 }
1344
1345 # test if we have all 'ubuntu-minimal' and 'ubuntu-standard' packages
1346 # except those explicitly excluded
1347 if ($suite eq 'hardy' || $suite eq 'intrepid' || $suite eq 'jaunty') {
1348 my $mdeps = $pkginfo->{'ubuntu-minimal'}->{depends};
1349 foreach my $d (split (/,/, $mdeps)) {
1350 if ($d =~ m/^\s*(\S+)$/) {
1351 my $pkg = $1;
1352 next if $closure->{$pkg};
1353 next if grep { $pkg eq $_ } @{$self->{excl}};
1354 die "missing ubuntu-minimal package '$pkg'\n";
1355 }
1356 }
1357 if (!$opts->{minimal}) {
1358 $mdeps = $pkginfo->{'ubuntu-standard'}->{depends};
1359 foreach my $d (split (/,/, $mdeps)) {
1360 if ($d =~ m/^\s*(\S+)$/) {
1361 my $pkg = $1;
1362 next if $closure->{$pkg};
1363 next if grep { $pkg eq $_ } @{$self->{excl}};
1364 die "missing ubuntu-standard package '$pkg'\n";
1365 }
1366 }
1367 }
1368 }
1369
1370 # download/cache all files first
1371 $self->cache_packages ($required);
1372 $self->cache_packages ($important);
1373 $self->cache_packages ($standard);
1374
1375 my $rootdir = $self->{rootfs};
1376
1377 # extract required packages first
1378 $self->logmsg ("create basic environment\n");
1379 foreach my $p (@$required) {
1380 my $filename = $self->getpkgfile ($p);
1381 my $content = $self->run_command("ar -t '$self->{cachedir}/$filename'", undef, 1);
1382 if ($content =~ m/^data.tar.xz$/m) {
1383 $self->run_command ("ar -p '$self->{cachedir}/$filename' data.tar.xz | tar -C '$rootdir' -xJf -");
1384 } else {
1385 $self->run_command ("ar -p '$self->{cachedir}/$filename' data.tar.gz | tar -C '$rootdir' -xzf -");
1386 }
1387 }
1388
1389 # fake dpkg status
1390 my $data = "Package: dpkg\n" .
1391 "Version: $pkginfo->{dpkg}->{version}\n" .
1392 "Status: install ok installed\n";
1393
1394 write_file ($data, "$rootdir/var/lib/dpkg/status");
1395 write_file ("", "$rootdir/var/lib/dpkg/info/dpkg.list");
1396 write_file ("", "$rootdir/var/lib/dpkg/available");
1397
1398 $data = '';
1399 foreach my $ss (@{$self->{sources}}) {
1400 my $url = $ss->{source};
1401 my $comp = join (' ', @{$ss->{comp}});
1402 $data .= "deb $url $ss->{suite} $comp\n\n";
1403 }
1404
1405 write_file ($data, "$rootdir/etc/apt/sources.list");
1406
1407 $data = "# UNCONFIGURED FSTAB FOR BASE SYSTEM\n";
1408 write_file ($data, "$rootdir/etc/fstab", 0644);
1409
1410 write_file ("localhost\n", "$rootdir/etc/hostname", 0644);
1411
1412 # avoid warnings about non-existent resolv.conf
1413 write_file ("", "$rootdir/etc/resolv.conf", 0644);
1414
1415 if (
1416 $suite eq 'hirsute' || $suite eq 'groovy' || $suite eq 'focal' ||
1417 $suite eq 'eoan' || $suite eq 'disco' || $suite eq 'cosmic' ||
1418 $suite eq 'bionic' || $suite eq 'artful' ||
1419 $suite eq 'zesty' || $suite eq 'yakkety' || $suite eq 'xenial' ||
1420 $suite eq 'wily') {
1421 # no need to configure loopback device
1422 } else {
1423 $data = "auto lo\niface lo inet loopback\n";
1424 mkdir "$rootdir/etc/network";
1425 write_file ($data, "$rootdir/etc/network/interfaces", 0644);
1426 }
1427
1428 # setup devices
1429 $self->run_command ("tar xzf '$devicetar' -C '$rootdir'");
1430
1431 # avoid warnings about missing default locale
1432 write_file ("LANG=\"C\"\n", "$rootdir/etc/default/locale", 0644);
1433
1434 # fake init
1435 rename ("$rootdir/sbin/init", "$rootdir/sbin/init.org");
1436 $self->run_command ("cp '$fake_init' '$rootdir/sbin/init'");
1437
1438 $self->run_command ("cp '$default_env' '$rootdir/sbin/defenv'");
1439
1440 $self->run_command ("lxc-start -n $veid -f $self->{veconffile}");
1441
1442 $self->logmsg ("initialize ld cache\n");
1443 $self->ve_command ("/sbin/ldconfig");
1444 $self->run_command ("ln -sf mawk '$rootdir/usr/bin/awk'");
1445
1446 $self->logmsg ("installing packages\n");
1447
1448 $self->ve_dpkg ('install', 'base-files', 'base-passwd');
1449
1450 $self->ve_dpkg ('install', 'dpkg');
1451
1452 $self->run_command ("ln -sf /usr/share/zoneinfo/UTC '$rootdir/etc/localtime'");
1453
1454 $self->run_command ("ln -sf bash '$rootdir/bin/sh'");
1455
1456 $self->ve_dpkg ('install', 'libc6');
1457 $self->ve_dpkg ('install', 'perl-base');
1458
1459 unlink "$rootdir/usr/bin/awk";
1460
1461 $self->ve_dpkg ('install', 'mawk');
1462 $self->ve_dpkg ('install', 'debconf');
1463
1464 # unpack required packages
1465 foreach my $p (@$required) {
1466 $self->ve_dpkg ('unpack', $p);
1467 }
1468
1469 rename ("$rootdir/sbin/init.org", "$rootdir/sbin/init");
1470 $self->ve_divert_add ("/sbin/init");
1471 $self->run_command ("cp '$fake_init' '$rootdir/sbin/init'");
1472
1473 # disable service activation
1474 $self->ve_divert_add ("/usr/sbin/policy-rc.d");
1475 $data = "#!/bin/sh\nexit 101\n";
1476 write_file ($data, "$rootdir/usr/sbin/policy-rc.d", 755);
1477
1478 # disable start-stop-daemon
1479 $self->ve_divert_add ("/sbin/start-stop-daemon");
1480 $data = <<EOD;
1481 #!/bin/sh
1482 echo
1483 echo \"Warning: Fake start-stop-daemon called, doing nothing\"
1484 EOD
1485 write_file ($data, "$rootdir/sbin/start-stop-daemon", 0755);
1486
1487 # disable udevd
1488 $self->ve_divert_add ("/sbin/udevd");
1489
1490 if ($suite eq 'etch') {
1491 # disable apache2 startup
1492 write_file ("NO_START=1\n", "$rootdir/etc/default/apache2");
1493 }
1494
1495 $self->logmsg ("configure required packages\n");
1496 $self->ve_command ("dpkg --force-confold --skip-same-version --configure -a");
1497
1498 # set postfix defaults
1499 if ($mta eq 'postfix') {
1500 $data = "postfix postfix/main_mailer_type select Local only\n";
1501 $self->ve_debconfig_set ($data);
1502
1503 $data = "postmaster: root\nwebmaster: root\n";
1504 write_file ($data, "$rootdir/etc/aliases");
1505 }
1506
1507 if ($suite eq 'jaunty') {
1508 # jaunty does not create /var/run/network, so network startup fails.
1509 # so we do not use tmpfs for /var/run and /var/lock
1510 $self->run_command ("sed -e 's/RAMRUN=yes/RAMRUN=no/' -e 's/RAMLOCK=yes/RAMLOCK=no/' -i $rootdir/etc/default/rcS");
1511 # and create the directory here
1512 $self->run_command ("mkdir $rootdir/var/run/network");
1513 }
1514
1515 # unpack base packages
1516 foreach my $p (@$important) {
1517 $self->ve_dpkg ('unpack', $p);
1518 }
1519
1520 # start loopback
1521 if (-x "$rootdir/sbin/ifconfig") {
1522 $self->ve_command ("ifconfig lo up");
1523 } else {
1524 $self->ve_command ("ip link set lo up");
1525 }
1526
1527 $self->logmsg ("configure important packages\n");
1528 $self->ve_command ("dpkg --force-confold --skip-same-version --configure -a");
1529
1530 if (-d "$rootdir/etc/event.d") {
1531 unlink <$rootdir/etc/event.d/tty*>;
1532 }
1533
1534 if (-f "$rootdir/etc/inittab") {
1535 $self->run_command ("sed -i -e '/getty\\s38400\\stty[23456]/d' '$rootdir/etc/inittab'");
1536 }
1537
1538 # Link /etc/mtab to /proc/mounts, so df and friends will work:
1539 unlink "$rootdir/etc/mtab";
1540 $self->ve_command ("ln -s /proc/mounts /etc/mtab");
1541
1542 # reset password
1543 $self->ve_command ("usermod -L root");
1544
1545 if ($mta eq 'postfix') {
1546 $data = "postfix postfix/main_mailer_type select No configuration\n";
1547 $self->ve_debconfig_set ($data);
1548
1549 unlink "$rootdir/etc/mailname";
1550 write_file ($postfix_main_cf, "$rootdir/etc/postfix/main.cf");
1551 }
1552
1553 if (!$opts->{minimal}) {
1554 # unpack standard packages
1555 foreach my $p (@$standard) {
1556 $self->ve_dpkg ('unpack', $p);
1557 }
1558
1559 $self->logmsg ("configure standard packages\n");
1560 $self->ve_command ("dpkg --force-confold --skip-same-version --configure -a");
1561 }
1562
1563 # disable HWCLOCK access
1564 $self->run_command ("echo 'HWCLOCKACCESS=no' >> '$rootdir/etc/default/rcS'");
1565
1566 # disable hald
1567 $self->ve_divert_add ("/usr/sbin/hald");
1568
1569 # disable /dev/urandom init
1570 $self->run_command ("install -m 0755 '$script_init_urandom' '$rootdir/etc/init.d/urandom'");
1571
1572 if ($suite eq 'etch' || $suite eq 'hardy' || $suite eq 'intrepid' || $suite eq 'jaunty') {
1573 # avoid klogd start
1574 $self->ve_divert_add ("/sbin/klogd");
1575 }
1576
1577 # remove unnecessays sysctl entries to avoid warnings
1578 my $cmd = 'sed';
1579 $cmd .= ' -e \'s/^\(kernel\.printk.*\)/#\1/\'';
1580 $cmd .= ' -e \'s/^\(kernel\.maps_protect.*\)/#\1/\'';
1581 $cmd .= ' -e \'s/^\(fs\.inotify\.max_user_watches.*\)/#\1/\'';
1582 $cmd .= ' -e \'s/^\(vm\.mmap_min_addr.*\)/#\1/\'';
1583 $cmd .= " -i '$rootdir/etc/sysctl.conf'";
1584 $self->run_command ($cmd);
1585
1586 my $bindv6only = "$rootdir/etc/sysctl.d/bindv6only.conf";
1587 if (-f $bindv6only) {
1588 $cmd = 'sed';
1589 $cmd .= ' -e \'s/^\(net\.ipv6\.bindv6only.*\)/#\1/\'';
1590 $cmd .= " -i '$bindv6only'";
1591 $self->run_command ($cmd);
1592 }
1593
1594 if ($suite eq 'hardy' || $suite eq 'intrepid' || $suite eq 'jaunty') {
1595 # disable tty init (console-setup)
1596 my $cmd = 'sed';
1597 $cmd .= ' -e \'s/^\(ACTIVE_CONSOLES=.*\)/ACTIVE_CONSOLES=/\'';
1598 $cmd .= " -i '$rootdir/etc/default/console-setup'";
1599 $self->run_command ($cmd);
1600 }
1601
1602 if ($suite eq 'intrepid' || $suite eq 'jaunty') {
1603 # remove sysctl setup (avoid warnings at startup)
1604 my $filelist = "$rootdir/etc/sysctl.d/10-console-messages.conf";
1605 $filelist .= " $rootdir/etc/sysctl.d/10-process-security.conf" if $suite eq 'intrepid';
1606 $filelist .= " $rootdir/etc/sysctl.d/10-network-security.conf";
1607 $self->run_command ("rm $filelist");
1608 }
1609
1610 if (-e "$rootdir/lib/systemd/system/sys-kernel-config.mount") {
1611 $self->ve_command ("ln -s /dev/null /etc/systemd/system/sys-kernel-debug.mount");
1612 }
1613 }
1614
1615 sub enter {
1616 my ($self) = @_;
1617
1618 my $veid = $self->{veid};
1619 my $conffile = $self->{veconffile};
1620
1621 my $vestat = $self->ve_status();
1622
1623 if (!$vestat->{exist}) {
1624 $self->logmsg ("Please create the appliance first (bootstrap)");
1625 return;
1626 }
1627
1628 if (!$vestat->{running}) {
1629 $self->run_command ("lxc-start -n $veid -f $conffile");
1630 }
1631
1632 system ("lxc-attach -n $veid --rcfile $conffile --clear-env");
1633 }
1634
1635 sub ve_mysql_command {
1636 my ($self, $sql, $password) = @_;
1637
1638 #my $bootstrap = "/usr/sbin/mysqld --bootstrap --user=mysql --skip-grant-tables " .
1639 #"--skip-bdb --skip-innodb --skip-ndbcluster";
1640
1641 $self->ve_command ("mysql", $sql);
1642 }
1643
1644 sub ve_mysql_bootstrap {
1645 my ($self, $sql, $password) = @_;
1646
1647 my $cmd;
1648
1649 my $suite = $self->{config}->{suite};
1650
1651 if ($suite eq 'jessie') {
1652 my $rootdir = $self->{rootfs};
1653 $self->run_command ("sed -e 's/^key_buffer\\s*=/key_buffer_size =/' -i $rootdir/etc/mysql/my.cnf");
1654 }
1655
1656 if ($suite eq 'squeeze' || $suite eq 'wheezy' || $suite eq 'jessie') {
1657 $cmd = "/usr/sbin/mysqld --bootstrap --user=mysql --skip-grant-tables";
1658
1659 } else {
1660 $cmd = "/usr/sbin/mysqld --bootstrap --user=mysql --skip-grant-tables " .
1661 "--skip-bdb --skip-innodb --skip-ndbcluster";
1662 }
1663
1664 $self->ve_command ($cmd, $sql);
1665 }
1666
1667 sub compute_required {
1668 my ($self, $pkglist) = @_;
1669
1670 my $pkginfo = $self->pkginfo();
1671 my $instpkgs = $self->read_installed ();
1672
1673 my $closure = {};
1674 __record_provides ($pkginfo, $closure, [keys %$instpkgs]);
1675
1676 return $self->closure ($closure, $pkglist);
1677 }
1678
1679 sub task_postgres {
1680 my ($self, $opts) = @_;
1681
1682 my @supp = ('7.4', '8.1');
1683 my $pgversion = '8.1';
1684
1685 my $suite = $self->{config}->{suite};
1686
1687 if ($suite eq 'lenny' || $suite eq 'hardy' || $suite eq 'intrepid' || $suite eq 'jaunty') {
1688 @supp = ('8.3');
1689 $pgversion = '8.3';
1690 } elsif ($suite eq 'squeeze') {
1691 @supp = ('8.4');
1692 $pgversion = '8.4';
1693 } elsif ($suite eq 'wheezy') {
1694 @supp = ('9.1');
1695 $pgversion = '9.1';
1696 } elsif ($suite eq 'jessie') {
1697 @supp = ('9.4');
1698 $pgversion = '9.4';
1699 } elsif ($suite eq 'stretch') {
1700 @supp = ('9.6');
1701 $pgversion = '9.6';
1702 } elsif ($suite eq 'buster') {
1703 @supp = ('11');
1704 $pgversion = '11';
1705 } elsif ($suite eq 'bullseye') {
1706 # FIXME update on freeze!
1707 @supp = ('12');
1708 $pgversion = '12';
1709 }
1710
1711 $pgversion = $opts->{version} if $opts->{version};
1712
1713 die "unsupported postgres version '$pgversion'\n"
1714 if !grep { $pgversion eq $_; } @supp;
1715
1716 my $rootdir = $self->{rootfs};
1717
1718 my $required = $self->compute_required (["postgresql-$pgversion"]);
1719
1720 $self->cache_packages ($required);
1721
1722 $self->ve_dpkg ('install', @$required);
1723
1724 my $iscript = "postgresql-$pgversion";
1725 if ($suite eq 'squeeze' || $suite eq 'wheezy' || $suite eq 'jessie' ||
1726 $suite eq 'stretch') {
1727 $iscript = 'postgresql';
1728 }
1729
1730 $self->ve_command ("/etc/init.d/$iscript start") if $opts->{start};
1731 }
1732
1733 sub task_mysql {
1734 my ($self, $opts) = @_;
1735
1736 my $password = $opts->{password};
1737 my $rootdir = $self->{rootfs};
1738
1739 my $suite = $self->{config}->{suite};
1740
1741 my $ver = '5.0';
1742 if ($suite eq 'squeeze') {
1743 $ver = '5.1';
1744 } elsif ($suite eq 'wheezy' || $suite eq 'jessie') {
1745 $ver = '5.5';
1746 } else {
1747 die "task_mysql: unsupported suite '$suite'";
1748 }
1749
1750 my $required = $self->compute_required (['mysql-common', "mysql-server-$ver"]);
1751
1752 $self->cache_packages ($required);
1753
1754 $self->ve_dpkg ('install', @$required);
1755
1756 # fix security (see /usr/bin/mysql_secure_installation)
1757 my $sql = "DELETE FROM mysql.user WHERE User='';\n" .
1758 "DELETE FROM mysql.user WHERE User='root' AND Host!='localhost';\n" .
1759 "FLUSH PRIVILEGES;\n";
1760 $self->ve_mysql_bootstrap ($sql);
1761
1762 if ($password) {
1763
1764 my $rpw = $password eq 'random' ? 'admin' : $password;
1765
1766 my $sql = "USE mysql;\n" .
1767 "UPDATE user SET password=PASSWORD(\"$rpw\") WHERE user='root';\n" .
1768 "FLUSH PRIVILEGES;\n";
1769 $self->ve_mysql_bootstrap ($sql);
1770
1771 write_file ("[client]\nuser=root\npassword=\"$rpw\"\n", "$rootdir/root/.my.cnf", 0600);
1772 if ($password eq 'random') {
1773 $self->install_init_script ($script_mysql_randompw, 2, 20);
1774 }
1775 }
1776
1777 $self->ve_command ("/etc/init.d/mysql start") if $opts->{start};
1778 }
1779
1780 sub task_php {
1781 my ($self, $opts) = @_;
1782
1783 my $memlimit = $opts->{memlimit};
1784 my $rootdir = $self->{rootfs};
1785
1786 my $required = $self->compute_required ([qw (php5 php5-cli libapache2-mod-php5 php5-gd)]);
1787
1788 $self->cache_packages ($required);
1789
1790 $self->ve_dpkg ('install', @$required);
1791
1792 if ($memlimit) {
1793 $self->run_command ("sed -e 's/^\\s*memory_limit\\s*=.*;/memory_limit = ${memlimit}M;/' -i $rootdir/etc/php5/apache2/php.ini");
1794 }
1795 }
1796
1797 sub install {
1798 my ($self, $pkglist, $unpack) = @_;
1799
1800 my $required = $self->compute_required ($pkglist);
1801
1802 $self->cache_packages ($required);
1803
1804 $self->ve_dpkg ($unpack ? 'unpack' : 'install', @$required);
1805 }
1806
1807 sub cleanup {
1808 my ($self, $distclean) = @_;
1809
1810 unlink $self->{logfile};
1811 unlink "$self->{targetname}.tar";
1812 unlink "$self->{targetname}.tar.gz";
1813
1814 $self->ve_destroy ();
1815 unlink ".veid";
1816
1817 rmtree $self->{cachedir} if $distclean && !$self->{config}->{cachedir};
1818
1819 rmtree $self->{infodir};
1820
1821 }
1822
1823 1;