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