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