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