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