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