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