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