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