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