]> git.proxmox.com Git - dab.git/blame - DAB.pm
fix error in default debian mysql my.cnf for jessie
[dab.git] / DAB.pm
CommitLineData
8ab34b87
DM
1package PVE::DAB;
2
3use strict;
4use warnings;
5use IO::File;
6use File::Path;
7use File::Basename;
8use IO::Select;
9use IPC::Open2;
10use IPC::Open3;
11use POSIX qw (LONG_MAX);
12
13# fixme: lock container ?
14
15my $dablibdir = "/usr/lib/dab";
16my $devicetar = "$dablibdir/devices.tar.gz";
17my $default_env = "$dablibdir/scripts/defenv";
18my $fake_init = "$dablibdir/scripts/init.pl";
19my $script_ssh_init = "$dablibdir/scripts/ssh_gen_host_keys";
20my $script_mysql_randompw = "$dablibdir/scripts/mysql_randompw";
21my $script_init_urandom = "$dablibdir/scripts/init_urandom";
22
23my $postfix_main_cf = <<EOD;
24# See /usr/share/postfix/main.cf.dist for a commented, more complete version
25
26smtpd_banner = \$myhostname ESMTP \$mail_name (Debian/GNU)
27biff = no
28
29# appending .domain is the MUA's job.
30append_dot_mydomain = no
31
32# Uncomment the next line to generate "delayed mail" warnings
33#delay_warning_time = 4h
34
35alias_maps = hash:/etc/aliases
36alias_database = hash:/etc/aliases
37mydestination = \$myhostname, localhost.\$mydomain, localhost
38relayhost =
39mynetworks = 127.0.0.0/8
40inet_interfaces = loopback-only
41recipient_delimiter = +
42
43EOD
44
45# produce apt compatible filenames (/var/lib/apt/lists)
46sub __url_to_filename {
47 my $url = shift;
48
49 $url =~ s|^\S+://||;
50 $url =~ s|_|%5f|g;
51 $url =~ s|/|_|g;
52
53 return $url;
54}
55
56sub download {
57 my ($self, $url, $path) = @_;
58
59 $self->logmsg ("download: $url\n");
60 my $tmpfn = "$path.tmp$$";
61 eval {
62 $self->run_command ("wget -q '$url' -O '$tmpfn'");
63 };
64
65 my $err = $@;
66 if ($err) {
67 unlink $tmpfn;
68 die $err;
69 }
70
71 rename ($tmpfn, $path);
72}
73
74sub write_file {
75 my ($data, $file, $perm) = @_;
76
77 die "no filename" if !$file;
78
79 unlink $file;
80
81 my $fh = IO::File->new ($file, O_WRONLY | O_CREAT, $perm) ||
82 die "unable to open file '$file'";
83
84 print $fh $data;
85
86 $fh->close;
87}
88
89sub read_file {
90 my ($file) = @_;
91
92 die "no filename" if !$file;
93
94 my $fh = IO::File->new ($file) ||
95 die "unable to open file '$file'";
96
97 local $/; # slurp mode
98
99 my $data = <$fh>;
100
101 $fh->close;
102
103 return $data;
104}
105
106sub read_config {
107 my ($filename) = @_;
108
109 my $res = {};
110
111 my $fh = IO::File->new ("<$filename") || return $res;
112 my $rec = '';
113
114 while (defined (my $line = <$fh>)) {
115 next if $line =~ m/^\#/;
116 next if $line =~ m/^\s*$/;
117 $rec .= $line;
118 };
119
120 close ($fh);
121
122 chomp $rec;
123 $rec .= "\n";
124
125 while ($rec) {
126 if ($rec =~ s/^Description:\s*([^\n]*)(\n\s+.*)*$//si) {
127 $res->{headline} = $1;
128 chomp $res->{headline};
129 my $long = $2;
130 $long =~ s/^\s+/ /;
131 $res->{description} = $long;
132 chomp $res->{description};
133 } elsif ($rec =~ s/^([^:]+):\s*(.*\S)\s*\n//) {
134 my ($key, $value) = (lc ($1), $2);
135 if ($key eq 'source' || $key eq 'mirror') {
136 push @{$res->{$key}}, $value;
137 } else {
138 die "duplicate key '$key'\n" if defined ($res->{$key});
139 $res->{$key} = $value;
140 }
141 } else {
142 die "unable to parse config file: $rec";
143 }
144 }
145
146 die "unable to parse config file" if $rec;
147
148 return $res;
149}
150
151sub run_command {
152 my ($self, $cmd, $input, $getoutput) = @_;
153
154 my $reader = IO::File->new();
155 my $writer = IO::File->new();
156 my $error = IO::File->new();
157
158 my $orig_pid = $$;
159
160 my $cmdstr = ref ($cmd) eq 'ARRAY' ? join (' ', @$cmd) : $cmd;
161
162 my $pid;
163 eval {
164 if (ref ($cmd) eq 'ARRAY') {
165 $pid = open3 ($writer, $reader, $error, @$cmd) || die $!;
166 } else {
167 $pid = open3 ($writer, $reader, $error, $cmdstr) || die $!;
168 }
169 };
170
171 my $err = $@;
172
173 # catch exec errors
174 if ($orig_pid != $$) {
175 $self->logmsg ("ERROR: command '$cmdstr' failed - fork failed\n");
176 POSIX::_exit (1);
177 kill ('KILL', $$);
178 }
179
180 die $err if $err;
181
182 print $writer $input if defined $input;
183 close $writer;
184
185 my $select = new IO::Select;
186 $select->add ($reader);
187 $select->add ($error);
188
189 my $res = '';
190 my $logfd = $self->{logfd};
191
192 while ($select->count) {
193 my @handles = $select->can_read ();
194
195 foreach my $h (@handles) {
196 my $buf = '';
197 my $count = sysread ($h, $buf, 4096);
198 if (!defined ($count)) {
199 waitpid ($pid, 0);
200 die "command '$cmdstr' failed: $!";
201 }
202 $select->remove ($h) if !$count;
203
204 print $logfd $buf;
205
206 $res .= $buf if $getoutput;
207 }
208 }
209
210 waitpid ($pid, 0);
211 my $ec = ($? >> 8);
212
213 die "command '$cmdstr' failed with exit code $ec\n" if $ec;
214
215 return $res;
216}
217
218sub logmsg {
219 my $self = shift;
220 print STDERR @_;
221 $self->writelog (@_);
222}
223
224sub writelog {
225 my $self = shift;
226 my $fd = $self->{logfd};
227 print $fd @_;
228}
229
230sub __sample_config {
231 my ($self, $mem) = @_;
232
233 my $max = LONG_MAX;
234 my $nolimit = "\"$max:$max\"";
235
236 my $defaults = {
237 128 => {},
238 256 => {},
239 512 => {},
240 1024 => {},
241 2048 => {},
242 };
243
244 die "unknown memory size" if !defined ($defaults->{$mem});
245
246 my $data = '';
247
248 $data .= "# DAB default config for ${mem}MB RAM\n\n";
249
250 $data .= "ONBOOT=\"no\"\n";
251
252 $data .= "\n# Primary parameters\n";
253 $data .= "NUMPROC=\"1024:1024\"\n";
254 $data .= "NUMTCPSOCK=$nolimit\n";
255 $data .= "NUMOTHERSOCK=$nolimit\n";
256
257 my $vmguarpages = int ($mem*1024/4);
258 $data .= "VMGUARPAGES=\"$vmguarpages:$max\"\n";
259
260 $data .= "\n# Secondary parameters\n";
261
262 $data .= "KMEMSIZE=$nolimit\n";
263
264 my $privmax = int ($vmguarpages*1.1);
265 $privmax = $vmguarpages + 12500 if ($privmax-$vmguarpages) > 12500;
266 $data .= "OOMGUARPAGES=\"$vmguarpages:$max\"\n";
267 $data .= "PRIVVMPAGES=\"$vmguarpages:$privmax\"\n";
268
269 $data .= "TCPSNDBUF=$nolimit\n";
270 $data .= "TCPRCVBUF=$nolimit\n";
271 $data .= "OTHERSOCKBUF=$nolimit\n";
272 $data .= "DGRAMRCVBUF=$nolimit\n";
273
274 $data .= "\n# Auxiliary parameters\n";
275 $data .= "NUMFILE=$nolimit\n";
276 $data .= "NUMFLOCK=$nolimit\n";
277 $data .= "NUMPTY=\"255:255\"\n";
278 $data .= "NUMSIGINFO=\"1024:1024\"\n";
279 $data .= "DCACHESIZE=$nolimit\n";
280 $data .= "LOCKEDPAGES=$nolimit\n";
281 $data .= "SHMPAGES=$nolimit\n";
282 $data .= "NUMIPTENT=$nolimit\n";
283 $data .= "PHYSPAGES=\"0:$max\"\n";
284
285 $data .= "\n# Disk quota parameters\n";
286 $data .= "DISK_QUOTA=\"no\"\n";
287 $data .= "DISKSPACE=$nolimit\n";
288 $data .= "DISKINODES=$nolimit\n";
289 $data .= "QUOTATIME=\"0\"\n";
290 $data .= "QUOTAUGIDLIMIT=\"0\"\n";
291
292 $data .= "\n# CPU fair sheduler parameter\n";
293 $data .= "CPUUNITS=\"1000\"\n\n";
294
295 $data .= "\n# Template parameter\n";
296 $data .= "OSTEMPLATE=\"$self->{targetname}\"\n";
297 $data .= "HOSTNAME=\"localhost\"\n";
298
299 return $data;
300}
301
302sub __allocate_ve {
303 my ($self) = @_;
304
305 my $cid;
306 if (my $fd = IO::File->new (".veid")) {
307 $cid = <$fd>;
308 chomp $cid;
309 close ($fd);
310 }
311
df845a3d 312 my $cfgdir = "/etc/pve/openvz";
8ab34b87
DM
313
314 if ($cid) {
315 $self->{veid} = $cid;
316 $self->{veconffile} = "$cfgdir/$cid.conf";
317 return $cid;
318 }
319
320 my $cdata = $self->__sample_config (1024);
321
322 my $veid;
323 my $startid = 90000;
324 for (my $id = $startid; $id < ($startid + 100); $id++) {
325
326 my $tmpfn = "$cfgdir/$id.conf.tmp$$";
327 my $target = "$cfgdir/$id.conf";
328
329 next if -f $target;
330
331 my $fh = IO::File->new ($target, O_WRONLY | O_CREAT | O_EXCL, 0644);
332
333 next if !$fh;
334
335 print $fh $cdata;
336 close ($fh);
337 $veid = $id;
338 last;
339 }
340
341 die "unable to allocate VE\n" if !$veid;
342
343 my $fd = IO::File->new (">.veid") ||
344 die "unable to write '.veid'\n";
345 print $fd "$veid\n";
346 close ($fd);
347
348 $self->logmsg ("allocated VE $veid\n");
349
350 $self->{veid} = $veid;
351 $self->{veconffile} = "$cfgdir/$veid.conf";
352
353 return $veid;
354}
355
356sub new {
357 my ($class, $config) = @_;
358
359 $class = ref ($class) || $class;
360
361 my $self = {};
362
363 $config = read_config ('dab.conf') if !$config;
364
365 $self->{config} = $config;
366
367 bless $self, $class;
368
369 $self->{logfile} = "logfile";
370 $self->{logfd} = IO::File->new (">>$self->{logfile}") ||
371 die "unable to open log file";
372
373 my $arch = $config->{architecture};
374 die "no 'architecture' specified\n" if !$arch;
375
376 die "unsupported architecture '$arch'\n"
377 if $arch !~ m/^(i386|amd64)$/;
378
379 my $suite = $config->{suite} || die "no 'suite' specified\n";
5d0f537e
DM
380 if ($suite eq 'jessie') {
381 $config->{ostype} = "debian-8.0";
382 } elsif ($suite eq 'wheezy') {
632ea034
DM
383 $config->{ostype} = "debian-7.0";
384 } elsif ($suite eq 'squeeze') {
8ab34b87
DM
385 $config->{ostype} = "debian-6.0";
386 } elsif ($suite eq 'lenny') {
387 $config->{ostype} = "debian-5.0";
388 } elsif ($suite eq 'etch') {
389 $config->{ostype} = "debian-4.0";
390 } elsif ($suite eq 'hardy') {
391 $config->{ostype} = "ubuntu-8.04";
392 } elsif ($suite eq 'intrepid') {
393 $config->{ostype} = "ubuntu-8.10";
394 } elsif ($suite eq 'jaunty') {
395 $config->{ostype} = "ubuntu-9.04";
396 } else {
397 die "unsupported debian suite '$suite'\n";
398 }
399
400 my $name = $config->{name} || die "no 'name' specified\n";
401
402 $name =~ m/^[a-z][0-9a-z\-\*\.]+$/ ||
403 die "illegal characters in name '$name'\n";
404
405 my $version = $config->{version};
406 die "no 'version' specified\n" if !$version;
407 die "no 'section' specified\n" if !$config->{section};
408 die "no 'description' specified\n" if !$config->{headline};
409 die "no 'maintainer' specified\n" if !$config->{maintainer};
410
411 if ($name =~ m/^$config->{ostype}/) {
412 $self->{targetname} = "${name}_${version}_$config->{architecture}";
413 } else {
414 $self->{targetname} = "$config->{ostype}-${name}_" .
415 "${version}_$config->{architecture}";
416 }
417
418 if (!$config->{source}) {
5d0f537e
DM
419 if ($suite eq 'etch' || $suite eq 'lenny' || $suite eq 'squeeze' ||
420 $suite eq 'wheezy' || $suite eq 'jessie' ) {
8ab34b87
DM
421 push @{$config->{source}}, "http://ftp.debian.org/debian SUITE main contrib";
422 push @{$config->{source}}, "http://ftp.debian.org/debian SUITE-updates main contrib"
5d0f537e 423 if ($suite eq 'squeeze' || $suite eq 'wheezy' || $suite eq 'jessie');
8ab34b87
DM
424 push @{$config->{source}}, "http://security.debian.org SUITE/updates main contrib";
425 } elsif ($suite eq 'hardy' || $suite eq 'intrepid' || $suite eq 'jaunty') {
426 my $comp = "main restricted universe multiverse";
427 push @{$config->{source}}, "http://archive.ubuntu.com/ubuntu SUITE $comp";
428 push @{$config->{source}}, "http://archive.ubuntu.com/ubuntu SUITE-updates $comp";
429 push @{$config->{source}}, "http://archive.ubuntu.com/ubuntu SUITE-security $comp";
430 }
431 }
432
433 my $sources = undef;
434
435 foreach my $s (@{$config->{source}}) {
436 if ($s =~ m@^\s*((http|ftp)://\S+)\s+(\S+)((\s+(\S+))+)$@) {
437 my ($url, $su, $components) = ($1, $3, $4);
438 $su =~ s/SUITE/$suite/;
439 $components =~ s/^\s+//;
440 $components =~ s/\s+$//;
441 my $ca;
442 foreach my $co (split (/\s+/, $components)) {
443 push @$ca, $co;
444 }
445 $ca = ['main'] if !$ca;
446
447 push @$sources, {
448 source => $url,
449 comp => $ca,
450 suite => $su,
451 };
452 } else {
453 die "syntax error in source spezification '$s'\n";
454 }
455 }
456
457 foreach my $m (@{$config->{mirror}}) {
458 if ($m =~ m@^\s*((http|ftp)://\S+)\s*=>\s*((http|ftp)://\S+)\s*$@) {
459 my ($ms, $md) = ($1, $3);
460 my $found;
461 foreach my $ss (@$sources) {
462 if ($ss->{source} eq $ms) {
463 $found = 1;
464 $ss->{mirror} = $md;
465 last;
466 }
467 }
468 die "unusable mirror $ms\n" if !$found;
469 } else {
470 die "syntax error in mirror spezification '$m'\n";
471 }
472 }
473
474 $self->{sources} = $sources;
475
476 $self->{infodir} = "info";
477
478 $self->__allocate_ve ();
479
480 $self->{cachedir} = ($config->{cachedir} || 'cache') . "/$suite";;
481
482 my $incl = [qw (less ssh openssh-server logrotate)];
483
484 my $excl = [qw (modutils reiserfsprogs ppp pppconfig pppoe
485 pppoeconf nfs-common mtools ntp)];
486
487 # ubuntu has too many dependencies on udev, so
488 # we cannot exclude it (instead we disable udevd)
489 if ($suite eq 'hardy') {
490 push @$excl, qw(kbd);
491 push @$excl, qw(apparmor apparmor-utils ntfs-3g
492 friendly-recovery);
5d0f537e 493 } elsif ($suite eq 'intrepid' || $suite eq 'jaunty') {
8ab34b87
DM
494 push @$excl, qw(apparmor apparmor-utils libapparmor1 libapparmor-perl
495 libntfs-3g28 ntfs-3g friendly-recovery);
5d0f537e
DM
496 } elsif ($suite eq 'jessie') {
497 push @$incl, 'sysvinit-core'; # avoid systemd and udev
498 push @$incl, 'libperl4-corelibs-perl'; # to make lsof happy
499 push @$excl, qw(systemd systemd-sysv udev module-init-tools pciutils hdparm
500 memtest86+ parted);
501 } else {
8ab34b87
DM
502 push @$excl, qw(udev module-init-tools pciutils hdparm
503 memtest86+ parted);
504 }
505
506 $self->{incl} = $incl;
507 $self->{excl} = $excl;
508
509 return $self;
510}
511
512sub initialize {
513 my ($self) = @_;
514
515 my $infodir = $self->{infodir};
516 my $arch = $self->{config}->{architecture};
517
518 rmtree $infodir;
519 mkpath $infodir;
520
521 # truncate log
522 my $logfd = $self->{logfd} = IO::File->new (">$self->{logfile}") ||
523 die "unable to open log file";
524
525 foreach my $ss (@{$self->{sources}}) {
526 my $src = $ss->{mirror} || $ss->{source};
527 my $path = "dists/$ss->{suite}/Release";
528 my $url = "$src/$path";
529 my $target = __url_to_filename ("$ss->{source}/$path");
530 eval {
531 $self->download ($url, "$infodir/$target");
532 $self->download ("$url.gpg", "$infodir/$target.gpg");
533 # fixme: impl. verify (needs --keyring option)
534 };
535 if (my $err = $@) {
536 print $logfd $@;
537 warn "Release info ignored\n";
538 };
539 foreach my $comp (@{$ss->{comp}}) {
540 $path = "dists/$ss->{suite}/$comp/binary-$arch/Packages.gz";
541 $target = "$infodir/" . __url_to_filename ("$ss->{source}/$path");
542 my $pkgsrc = "$src/$path";
543 $self->download ($pkgsrc, $target);
544 $self->run_command ("gzip -d '$target'");
545 }
546 }
547}
548
549sub write_config {
550 my ($self, $filename, $size) = @_;
551
552 my $config = $self->{config};
553
554 my $data = '';
555
556 $data .= "Name: $config->{name}\n";
557 $data .= "Version: $config->{version}\n";
558 $data .= "Type: openvz\n";
559 $data .= "OS: $config->{ostype}\n";
560 $data .= "Section: $config->{section}\n";
561 $data .= "Maintainer: $config->{maintainer}\n";
562 $data .= "Architecture: $config->{architecture}\n";
563 $data .= "Installed-Size: $size\n";
564
565 # optional
566 $data .= "Infopage: $config->{infopage}\n" if $config->{infopage};
567 $data .= "ManageUrl: $config->{manageurl}\n" if $config->{manageurl};
568 $data .= "Certified: $config->{certified}\n" if $config->{certified};
569
570 # description
571 $data .= "Description: $config->{headline}\n";
572 $data .= "$config->{description}\n" if $config->{description};
573
574 write_file ($data, $filename, 0644);
575}
576
577sub finalize {
578 my ($self, $opts) = @_;
579
580 my $suite = $self->{config}->{suite};
581 my $infodir = $self->{infodir};
582 my $arch = $self->{config}->{architecture};
583
584 my $instpkgs = $self->read_installed ();
585 my $pkginfo = $self->pkginfo();
586 my $veid = $self->{veid};
587 my $rootdir = $self->vz_root_dir();
588
589 my $vestat = $self->ve_status();
590 die "ve not running - unable to finalize\n" if !$vestat->{running};
591
592 # cleanup mysqld
593 if (-f "$rootdir/etc/init.d/mysql") {
594 $self->ve_command ("/etc/init.d/mysql stop");
595 }
596
597 if (!($opts->{keepmycnf} || (-f "$rootdir/etc/init.d/mysql_randompw"))) {
598 unlink "$rootdir/root/.my.cnf";
599 }
600
601 if ($suite eq 'etch') {
602 # enable apache2 startup
603 if ($instpkgs->{apache2}) {
604 write_file ("NO_START=0\n", "$rootdir/etc/default/apache2");
605 } else {
606 unlink "$rootdir/etc/default/apache2";
607 }
608 }
609 $self->logmsg ("cleanup package status\n");
610 # prevent auto selection of all standard, required or important
611 # packages which are not installed
612 foreach my $pkg (keys %$pkginfo) {
613 my $pri = $pkginfo->{$pkg}->{priority};
614 if ($pri && ($pri eq 'required' || $pri eq 'important'
615 || $pri eq 'standard')) {
616 if (!$instpkgs->{$pkg}) {
617 $self->ve_dpkg_set_selection ($pkg, 'purge');
618 }
619 }
620 }
621
622 $self->ve_command ("apt-get clean");
623
624 $self->logmsg ("update available package list\n");
625
626 $self->ve_command ("dpkg --clear-avail");
627 foreach my $ss (@{$self->{sources}}) {
628 my $relsrc = __url_to_filename ("$ss->{source}/dists/$ss->{suite}/Release");
629 if (-f "$infodir/$relsrc" && -f "$infodir/$relsrc.gpg") {
630 $self->run_command ("cp '$infodir/$relsrc' '$rootdir/var/lib/apt/lists/$relsrc'");
631 $self->run_command ("cp '$infodir/$relsrc.gpg' '$rootdir/var/lib/apt/lists/$relsrc.gpg'");
632 }
633 foreach my $comp (@{$ss->{comp}}) {
634 my $src = __url_to_filename ("$ss->{source}/dists/$ss->{suite}/" .
635 "$comp/binary-$arch/Packages");
636 my $target = "/var/lib/apt/lists/$src";
637 $self->run_command ("cp '$infodir/$src' '$rootdir/$target'");
638 $self->ve_command ("dpkg --merge-avail '$target'");
639 }
640 }
641
642 # set dselect default method
643 write_file ("apt apt\n", "$rootdir/var/lib/dpkg/cmethopt");
644
645 $self->ve_divert_remove ("/usr/sbin/policy-rc.d");
646
647 $self->ve_divert_remove ("/sbin/start-stop-daemon");
648
649 $self->ve_divert_remove ("/sbin/init");
650
651 # finally stop the VE
652 $self->run_command ("vzctl stop $veid --fast");
653 $rootdir = $self->vz_priv_dir();
654
655 unlink "$rootdir/sbin/defenv";
656
657 unlink <$rootdir/root/dead.letter*>;
658
659 unlink "$rootdir/var/log/init.log";
660
661 unlink "$rootdir/aquota.group";
662
663 unlink "$rootdir/aquota.user";
664
665 write_file ("", "$rootdir/var/log/syslog");
666
667 $self->logmsg ("detecting final size: ");
668
669 my $sizestr = $self->run_command ("du -sm $rootdir", undef, 1);
670 my $size;
671 if ($sizestr =~ m/^(\d+)\s+\Q$rootdir\E$/) {
672 $size = $1;
673 } else {
674 die "unable to detect size\n";
675 }
676 $self->logmsg ("$size MB\n");
677
678 $self->write_config ("$rootdir/etc/appliance.info", $size);
679
680 $self->logmsg ("creating final appliance archive\n");
681
682 my $target = "$self->{targetname}.tar";
683 unlink $target;
684 unlink "$target.gz";
685
686 $self->run_command ("tar cpf $target --numeric-owner -C '$rootdir' ./etc/appliance.info");
687 $self->run_command ("tar rpf $target --numeric-owner -C '$rootdir' --exclude ./etc/appliance.info .");
688 $self->run_command ("gzip $target");
689}
690
691sub read_installed {
692 my ($self) = @_;
693
694 my $rootdir = $self->vz_priv_dir();
695
696 my $pkgfilelist = "$rootdir/var/lib/dpkg/status";
697 local $/ = '';
698 open (PKGLST, "<$pkgfilelist") ||
699 die "unable to open '$pkgfilelist'";
700
701 my $pkglist = {};
702
703 while (my $rec = <PKGLST>) {
704 chomp $rec;
705 $rec =~ s/\n\s+/ /g;
706 $rec .= "\n";
707 my $res = {};
708
709 while ($rec =~ s/^([^:]+):\s+(.*)\s*\n//) {
710 $res->{lc $1} = $2;
711 }
712
713 my $pkg = $res->{'package'};
714 if (my $status = $res->{status}) {
715 my @sa = split (/\s+/, $status);
716 my $stat = $sa[0];
717 if ($stat && ($stat ne 'purge')) {
718 $pkglist->{$pkg} = $res;
719 }
720 }
721 }
722
723 close (PKGLST);
724
725 return $pkglist;
726}
727
728sub vz_root_dir {
729 my ($self) = @_;
730
731 my $veid = $self->{veid};
732
733 return "/var/lib/vz/root/$veid";
734}
735
736sub vz_priv_dir {
737 my ($self) = @_;
738
739 my $veid = $self->{veid};
740
741 return "/var/lib/vz/private/$veid";
742}
743
744sub ve_status {
745 my ($self) = @_;
746
747 my $veid = $self->{veid};
748
749 my $res = $self->run_command ("vzctl status $veid", undef, 1);
750 chomp $res;
751
752 if ($res =~ m/^CTID\s+$veid\s+(exist|deleted)\s+(mounted|unmounted)\s+(running|down)$/) {
753 return {
754 exist => $1 eq 'exist',
755 mounted => $2 eq 'mounted',
756 running => $3 eq 'running',
757 };
758 } else {
759 die "unable to parse ve status";
760 }
761}
762
763sub ve_command {
764 my ($self, $cmd, $input) = @_;
765
766 my $veid = $self->{veid};
767
768 if (ref ($cmd) eq 'ARRAY') {
769 unshift @$cmd, 'vzctl', 'exec2', $veid, 'defenv';
770 $self->run_command ($cmd, $input);
771 } else {
772 $self->run_command ("vzctl exec2 $veid defenv $cmd", $input);
773 }
774}
775
776# like ve_command, but pipes stdin correctly
777sub ve_exec {
778 my ($self, @cmd) = @_;
779
780 my $veid = $self->{veid};
781
782 my $reader;
783 my $pid = open2($reader, "<&STDIN", 'vzctl', 'exec2', $veid,
784 'defenv', @cmd) || die "unable to exec command";
785
786 while (defined (my $line = <$reader>)) {
787 $self->logmsg ($line);
788 }
789
790 waitpid ($pid, 0);
791 my $rc = $? >> 8;
792
793 die "ve_exec failed - status $rc\n" if $rc != 0;
794}
795
796sub ve_divert_add {
797 my ($self, $filename) = @_;
798
799 $self->ve_command ("dpkg-divert --add --divert '$filename.distrib' " .
800 "--rename '$filename'");
801}
802sub ve_divert_remove {
803 my ($self, $filename) = @_;
804
805 my $rootdir = $self->vz_root_dir();
806
807 unlink "$rootdir/$filename";
808 $self->ve_command ("dpkg-divert --remove --rename '$filename'");
809}
810
811sub ve_debconfig_set {
812 my ($self, $dcdata) = @_;
813
814 my $rootdir = $self->vz_root_dir();
815 my $cfgfile = "/tmp/debconf.txt";
816 write_file ($dcdata, "$rootdir/$cfgfile");
817 $self->ve_command ("debconf-set-selections $cfgfile");
818 unlink "$rootdir/$cfgfile";
819}
820
821sub ve_dpkg_set_selection {
822 my ($self, $pkg, $status) = @_;
823
824 $self->ve_command ("dpkg --set-selections", "$pkg $status");
825}
826
827sub ve_dpkg {
828 my ($self, $cmd, @pkglist) = @_;
829
830 return if !scalar (@pkglist);
831
832 my $pkginfo = $self->pkginfo();
833
834 my $rootdir = $self->vz_root_dir();
835 my $cachedir = $self->{cachedir};
836
837 my @files;
838
839 foreach my $pkg (@pkglist) {
840 my $filename = $self->getpkgfile ($pkg);
841 $self->run_command ("cp '$cachedir/$filename' '$rootdir/$filename'");
842 push @files, "/$filename";
843 $self->logmsg ("$cmd: $pkg\n");
844 }
845
846 my $fl = join (' ', @files);
847
848 if ($cmd eq 'install') {
849 $self->ve_command ("dpkg --force-depends --force-confold --install $fl");
850 } elsif ($cmd eq 'unpack') {
851 $self->ve_command ("dpkg --force-depends --unpack $fl");
852 } else {
853 die "internal error";
854 }
855
856 foreach my $fn (@files) { unlink "$rootdir$fn"; }
857}
858
859sub ve_destroy {
860 my ($self) = @_;
861
862 my $veid = $self->{veid}; # fixme
863
864 my $vestat = $self->ve_status();
865 if ($vestat->{running}) {
866 $self->run_command ("vzctl stop $veid --fast");
867 } elsif ($vestat->{mounted}) {
868 $self->run_command ("vzctl umount $veid");
869 }
870 if ($vestat->{exist}) {
871 $self->run_command ("vzctl destroy $veid");
872 } else {
873 unlink $self->{veconffile};
874 }
875}
876
877sub ve_init {
878 my ($self) = @_;
879
880 my $root = $self->vz_root_dir();
881 my $priv = $self->vz_priv_dir();
882
883 my $veid = $self->{veid}; # fixme
884
885 $self->logmsg ("initialize VE $veid\n");
886
887 while (1) {
888 my $vestat = $self->ve_status();
889 if ($vestat->{running}) {
890 $self->run_command ("vzctl stop $veid --fast");
891 } elsif ($vestat->{mounted}) {
892 $self->run_command ("vzctl umount $veid");
893 } else {
894 last;
895 }
896 sleep (1);
897 }
898
899 rmtree $root;
900 rmtree $priv;
901 mkpath $root;
902 mkpath $priv;
903
904 $self->run_command ("vzctl mount $veid");
905}
906
907sub __deb_version_cmp {
908 my ($cur, $op, $new) = @_;
909
910 if (system("dpkg", "--compare-versions", $cur, $op, $new) == 0) {
911 return 1;
912 }
913
914 return 0;
915}
916
917sub __parse_packages {
918 my ($pkginfo, $filename, $src) = @_;
919
920 local $/ = '';
921 open (PKGLST, "<$filename") ||
922 die "unable to open '$filename'";
923
924 while (my $rec = <PKGLST>) {
925 $rec =~ s/\n\s+/ /g;
926 chomp $rec;
927 $rec .= "\n";
928
929 my $res = {};
930
931 while ($rec =~ s/^([^:]+):\s+(.*)\s*\n//) {
932 $res->{lc $1} = $2;
933 }
934
935 my $pkg = $res->{'package'};
936 if ($pkg && $res->{'filename'}) {
937 my $cur;
938 if (my $info = $pkginfo->{$pkg}) {
939 $cur = $info->{version};
940 }
941 my $new = $res->{version};
942 if (!$cur || __deb_version_cmp ($cur, 'lt', $new)) {
943 if ($src) {
944 $res->{url} = "$src/$res->{'filename'}";
945 } else {
946 die "no url for package '$pkg'" if !$res->{url};
947 }
948 $pkginfo->{$pkg} = $res;
949 }
950 }
951 }
952
953 close (PKGLST);
954}
955
956sub pkginfo {
957 my ($self) = @_;
958
959 return $self->{pkginfo} if $self->{pkginfo};
960
961 my $infodir = $self->{infodir};
962 my $arch = $self->{config}->{architecture};
963
964 my $availfn = "$infodir/available";
965
966 my $pkginfo = {};
967 my $pkgcount = 0;
968
969 # reading 'available' is faster, because it only contains latest version
970 # (no need to do slow version compares)
971 if (-f $availfn) {
972 __parse_packages ($pkginfo, $availfn);
973 $self->{pkginfo} = $pkginfo;
974 return $pkginfo;
975 }
976
977 $self->logmsg ("generating available package list\n");
978
979 foreach my $ss (@{$self->{sources}}) {
980 foreach my $comp (@{$ss->{comp}}) {
981 my $url = "$ss->{source}/dists/$ss->{suite}/$comp/binary-$arch/Packages";
982 my $pkgfilelist = "$infodir/" . __url_to_filename ($url);
983
984 my $src = $ss->{mirror} || $ss->{source};
985
986 __parse_packages ($pkginfo, $pkgfilelist, $src);
987 }
988 }
989
990 if (my $dep = $self->{config}->{depends}) {
991 foreach my $d (split (/,/, $dep)) {
992 if ($d =~ m/^\s*(\S+)\s*(\((\S+)\s+(\S+)\)\s*)?$/) {
993 my ($pkg, $op, $rver) = ($1, $3, $4);
994 $self->logmsg ("checking dependencies: $d\n");
995 my $info = $pkginfo->{$pkg};
996 die "package '$pkg' not available\n" if !$info;
997 if ($op) {
998 my $cver = $info->{version};
999 if (!__deb_version_cmp ($cver, $op, $rver)) {
1000 die "detected wrong version '$cver'\n";
1001 }
1002 }
1003 } else {
1004 die "syntax error in depends field";
1005 }
1006 }
1007 }
1008
1009 $self->{pkginfo} = $pkginfo;
1010
1011 my $tmpfn = "$availfn.tmp$$";
1012 my $fd = IO::File->new (">$tmpfn");
1013 foreach my $pkg (sort keys %$pkginfo) {
1014 my $info = $pkginfo->{$pkg};
1015 print $fd "package: $pkg\n";
1016 foreach my $k (sort keys %$info) {
1017 next if $k eq 'description';
1018 next if $k eq 'package';
1019 my $v = $info->{$k};
1020 print $fd "$k: $v\n" if $v;
1021 }
1022 print $fd "description: $info->{description}\n" if $info->{description};
1023 print $fd "\n";
1024 }
1025 close ($fd);
1026
1027 rename ($tmpfn, $availfn);
1028
1029 return $pkginfo;
1030}
1031
1032sub __record_provides {
1033 my ($pkginfo, $closure, $list, $skipself) = @_;
1034
1035 foreach my $pname (@$list) {
1036 my $info = $pkginfo->{$pname};
1037 # fixme: if someone install packages directly using dpkg, there
1038 # is no entry in 'available', only in 'status'. In that case, we
1039 # should extract info from $instpkgs
1040 if (!$info) {
1041 warn "hint: ignoring provides for '$pname' - package not in 'available' list.\n";
1042 next;
1043 }
1044 if (my $prov = $info->{provides}) {
1045 my @pl = split (',', $prov);
1046 foreach my $p (@pl) {
1047 $p =~ m/\s*(\S+)/;
1048 if (!($skipself && (grep { $1 eq $_ } @$list))) {
1049 $closure->{$1} = 1;
1050 }
1051 }
1052 }
1053 $closure->{$pname} = 1 if !$skipself;
1054 }
1055}
1056
1057sub closure {
1058 my ($self, $closure, $list) = @_;
1059
1060 my $pkginfo = $self->pkginfo();
1061
1062 # first, record provided packages
1063 __record_provides ($pkginfo, $closure, $list, 1);
1064
1065 my $pkgs = {};
1066
1067 # then resolve dependencies
1068 foreach my $pname (@$list) {
1069 __closure_single ($pkginfo, $closure, $pkgs, $pname, $self->{excl});
1070 }
1071
1072 return [ keys %$pkgs ];
1073}
1074
1075sub __closure_single {
1076 my ($pkginfo, $closure, $pkgs, $pname, $excl) = @_;
1077
1078 $pname =~ s/^\s+//;
1079 $pname =~ s/\s+$//;
5d0f537e 1080 $pname =~ s/:any$//;
8ab34b87
DM
1081
1082 return if $closure->{$pname};
1083
1084 my $info = $pkginfo->{$pname} || die "no such package '$pname'";
1085
1086 my $dep = $info->{depends};
1087 my $predep = $info->{'pre-depends'};
1088
1089 my $size = $info->{size};
1090 my $url = $info->{url};
1091
1092 $url || die "$pname: no url for package '$pname'";
1093
1094 $pkgs->{$pname} = 1;
1095
1096 __record_provides ($pkginfo, $closure, [$pname]) if $info->{provides};
1097
1098 $closure->{$pname} = 1;
1099
1100 #print "$url\n";
1101
1102 my @l;
1103
1104 push @l, split (/,/, $predep) if $predep;
1105 push @l, split (/,/, $dep) if $dep;
1106
1107 DEPEND: foreach my $p (@l) {
1108 my @l1 = split (/\|/, $p);
1109 foreach my $p1 (@l1) {
1110 if ($p1 =~ m/^\s*(\S+).*/) {
1111 #printf (STDERR "$pname: $p --> $1\n");
1112 if ($closure->{$1}) {
1113 next DEPEND; # dependency already met
1114 }
1115 }
1116 }
1117 # search for non-excluded alternative
1118 my $found;
1119 foreach my $p1 (@l1) {
1120 if ($p1 =~ m/^\s*(\S+).*/) {
1121 next if grep { $1 eq $_ } @$excl;
1122 $found = $1;
1123 last;
1124 }
1125 }
1126 die "package '$pname' depends on exclusion '$p'\n" if !$found;
1127
1128 #printf (STDERR "$pname: $p --> $found\n");
1129
1130 __closure_single ($pkginfo, $closure, $pkgs, $found, $excl);
1131 }
1132}
1133
1134sub cache_packages {
1135 my ($self, $pkglist) = @_;
1136
1137 foreach my $pkg (@$pkglist) {
1138 $self->getpkgfile ($pkg);
1139 }
1140}
1141
1142sub getpkgfile {
1143 my ($self, $pkg) = @_;
1144
1145 my $pkginfo = $self->pkginfo();
1146 my $info = $pkginfo->{$pkg} || die "no such package '$pkg'";
1147 my $cachedir = $self->{cachedir};
1148
1149 my $url = $info->{url};
1150
1151 my $filename;
1152 if ($url =~ m|/([^/]+.deb)$|) {
1153 $filename = $1;
1154 } else {
1155 die "internal error";
1156 }
1157
1158 return $filename if -f "$cachedir/$filename";
1159
1160 mkpath $cachedir;
1161
1162 $self->download ($url, "$cachedir/$filename");
1163
1164 return $filename;
1165}
1166
1167sub install_init_script {
1168 my ($self, $script, $runlevel, $prio) = @_;
1169
1170 my $suite = $self->{config}->{suite};
1171 my $rootdir = $self->vz_root_dir();
1172
1173 my $base = basename ($script);
1174 my $target = "$rootdir/etc/init.d/$base";
1175
1176 $self->run_command ("install -m 0755 '$script' '$target'");
1177 if ($suite eq 'etch' || $suite eq 'lenny') {
1178 $self->ve_command ("update-rc.d $base start $prio $runlevel .");
1179 } else {
1180 $self->ve_command ("insserv $base");
1181 }
1182
1183 return $target;
1184}
1185
1186sub bootstrap {
1187 my ($self, $opts) = @_;
1188
1189 my $pkginfo = $self->pkginfo();
1190 my $veid = $self->{veid};
1191 my $suite = $self->{config}->{suite};
1192
1193 my $important = [ @{$self->{incl}} ];
1194 my $required;
1195 my $standard;
1196
1197 my $mta = $opts->{exim} ? 'exim' : 'postfix';
1198
1199 if ($mta eq 'postfix') {
1200 push @$important, "postfix";
1201 }
1202
1203 foreach my $p (keys %$pkginfo) {
1204 next if grep { $p eq $_ } @{$self->{excl}};
1205 my $pri = $pkginfo->{$p}->{priority};
1206 next if !$pri;
1207 next if $mta ne 'exim' && $p =~ m/exim/;
1208 next if $p =~ m/(selinux|semanage|policycoreutils)/;
1209
1210 push @$required, $p if $pri eq 'required';
1211 push @$important, $p if $pri eq 'important';
1212 push @$standard, $p if $pri eq 'standard' && !$opts->{minimal};
1213 }
1214
1215 my $closure = {};
1216 $required = $self->closure ($closure, $required);
1217 $important = $self->closure ($closure, $important);
1218
1219 if (!$opts->{minimal}) {
1220 push @$standard, 'xbase-clients';
1221 $standard = $self->closure ($closure, $standard);
1222 }
1223
1224 # test if we have all 'ubuntu-minimal' and 'ubuntu-standard' packages
1225 # except those explicitly excluded
1226 if ($suite eq 'hardy' || $suite eq 'intrepid' || $suite eq 'jaunty') {
1227 my $mdeps = $pkginfo->{'ubuntu-minimal'}->{depends};
1228 foreach my $d (split (/,/, $mdeps)) {
1229 if ($d =~ m/^\s*(\S+)$/) {
1230 my $pkg = $1;
1231 next if $closure->{$pkg};
1232 next if grep { $pkg eq $_ } @{$self->{excl}};
1233 die "missing ubuntu-minimal package '$pkg'\n";
1234 }
1235 }
1236 if (!$opts->{minimal}) {
1237 $mdeps = $pkginfo->{'ubuntu-standard'}->{depends};
1238 foreach my $d (split (/,/, $mdeps)) {
1239 if ($d =~ m/^\s*(\S+)$/) {
1240 my $pkg = $1;
1241 next if $closure->{$pkg};
1242 next if grep { $pkg eq $_ } @{$self->{excl}};
1243 die "missing ubuntu-standard package '$pkg'\n";
1244 }
1245 }
1246 }
1247 }
1248
1249 # download/cache all files first
1250 $self->cache_packages ($required);
1251 $self->cache_packages ($important);
1252 $self->cache_packages ($standard);
1253
1254 my $rootdir = $self->vz_priv_dir();
1255
1256 # extract required packages first
1257 $self->logmsg ("create basic environment\n");
1258 foreach my $p (@$required) {
1259 my $filename = $self->getpkgfile ($p);
5d0f537e
DM
1260 my $content = $self->run_command("ar -t '$self->{cachedir}/$filename'", undef, 1);
1261 if ($content =~ m/^data.tar.xz$/m) {
1262 $self->run_command ("ar -p '$self->{cachedir}/$filename' data.tar.xz | tar -C '$rootdir' -xJf -");
1263 } else {
1264 $self->run_command ("ar -p '$self->{cachedir}/$filename' data.tar.gz | tar -C '$rootdir' -xzf -");
1265 }
8ab34b87
DM
1266 }
1267
1268 # fake dpkg status
1269 my $data = "Package: dpkg\n" .
1270 "Version: $pkginfo->{dpkg}->{version}\n" .
1271 "Status: install ok installed\n";
1272
1273 write_file ($data, "$rootdir/var/lib/dpkg/status");
1274 write_file ("", "$rootdir/var/lib/dpkg/info/dpkg.list");
1275 write_file ("", "$rootdir/var/lib/dpkg/available");
1276
1277 $data = '';
1278 foreach my $ss (@{$self->{sources}}) {
1279 my $url = $ss->{source};
1280 my $comp = join (' ', @{$ss->{comp}});
1281 $data .= "deb $url $ss->{suite} $comp\n\n";
1282 }
1283
1284 write_file ($data, "$rootdir/etc/apt/sources.list");
1285
1286 $data = "# UNCONFIGURED FSTAB FOR BASE SYSTEM\n";
1287 write_file ($data, "$rootdir/etc/fstab", 0644);
1288
1289 write_file ("localhost\n", "$rootdir/etc/hostname", 0644);
1290
1291 # avoid warnings about non-existent resolv.conf
1292 write_file ("", "$rootdir/etc/resolv.conf", 0644);
1293
1294 $data = "auto lo\niface lo inet loopback\n";
1295 write_file ($data, "$rootdir/etc/network/interfaces", 0644);
1296
1297 # setup devices
1298 $self->run_command ("tar xzf '$devicetar' -C '$rootdir'");
1299
1300 # avoid warnings about missing default locale
1301 write_file ("LANG=\"C\"\n", "$rootdir/etc/default/locale", 0644);
1302
1303 # fake init
1304 rename ("$rootdir/sbin/init", "$rootdir/sbin/init.org");
1305 $self->run_command ("cp '$fake_init' '$rootdir/sbin/init'");
1306
1307 $self->run_command ("cp '$default_env' '$rootdir/sbin/defenv'");
1308
1309 $self->run_command ("vzctl start $veid");
1310 $rootdir = $self->vz_root_dir();
1311
1312 $self->logmsg ("initialize ld cache\n");
1313 $self->ve_command ("/sbin/ldconfig");
1314 $self->run_command ("ln -sf mawk '$rootdir/usr/bin/awk'");
1315
1316 $self->logmsg ("installing packages\n");
1317
1318 $self->ve_dpkg ('install', 'base-files', 'base-passwd');
1319
1320 $self->ve_dpkg ('install', 'dpkg');
1321
1322 $self->run_command ("ln -sf /usr/share/zoneinfo/UTC '$rootdir/etc/localtime'");
756b991f
DM
1323
1324 $self->run_command ("ln -sf bash '$rootdir/bin/sh'");
8ab34b87
DM
1325
1326 $self->ve_dpkg ('install', 'libc6');
1327 $self->ve_dpkg ('install', 'perl-base');
1328
1329 unlink "$rootdir/usr/bin/awk";
1330
1331 $self->ve_dpkg ('install', 'mawk');
1332 $self->ve_dpkg ('install', 'debconf');
1333
1334 # unpack required packages
1335 foreach my $p (@$required) {
1336 $self->ve_dpkg ('unpack', $p);
1337 }
1338
1339 rename ("$rootdir/sbin/init.org", "$rootdir/sbin/init");
1340 $self->ve_divert_add ("/sbin/init");
1341 $self->run_command ("cp '$fake_init' '$rootdir/sbin/init'");
1342
1343 # disable service activation
1344 $self->ve_divert_add ("/usr/sbin/policy-rc.d");
1345 $data = "#!/bin/sh\nexit 101\n";
1346 write_file ($data, "$rootdir/usr/sbin/policy-rc.d", 755);
1347
1348 # disable start-stop-daemon
1349 $self->ve_divert_add ("/sbin/start-stop-daemon");
1350 $data = <<EOD;
1351#!/bin/sh
1352echo
1353echo \"Warning: Fake start-stop-daemon called, doing nothing\"
1354EOD
1355 write_file ($data, "$rootdir/sbin/start-stop-daemon", 0755);
1356
1357 # disable udevd
1358 $self->ve_divert_add ("/sbin/udevd");
1359
1360 if ($suite eq 'etch') {
1361 # disable apache2 startup
1362 write_file ("NO_START=1\n", "$rootdir/etc/default/apache2");
1363 }
1364
1365 $self->logmsg ("configure required packages\n");
1366 $self->ve_command ("dpkg --force-confold --skip-same-version --configure -a");
1367
1368 # set postfix defaults
1369 if ($mta eq 'postfix') {
1370 $data = "postfix postfix/main_mailer_type select Local only\n";
1371 $self->ve_debconfig_set ($data);
1372
1373 $data = "postmaster: root\nwebmaster: root\n";
1374 write_file ($data, "$rootdir/etc/aliases");
1375 }
1376
1377 if ($suite eq 'jaunty') {
1378 # jaunty does not create /var/run/network, so network startup fails.
1379 # so we do not use tmpfs for /var/run and /var/lock
1380 $self->run_command ("sed -e 's/RAMRUN=yes/RAMRUN=no/' -e 's/RAMLOCK=yes/RAMLOCK=no/' -i $rootdir/etc/default/rcS");
1381 # and create the directory here
1382 $self->run_command ("mkdir $rootdir/var/run/network");
1383 }
1384
1385 # unpack base packages
1386 foreach my $p (@$important) {
1387 $self->ve_dpkg ('unpack', $p);
1388 }
1389
1390 # start loopback
1391 $self->ve_command ("ifconfig lo up");
1392
1393 $self->logmsg ("configure important packages\n");
1394 $self->ve_command ("dpkg --force-confold --skip-same-version --configure -a");
1395
1396 if (-d "$rootdir/etc/event.d") {
1397 unlink <$rootdir/etc/event.d/tty*>;
1398 }
1399
1400 if (-f "$rootdir/etc/inittab") {
756b991f 1401 $self->run_command ("sed -i -e '/getty\\s38400\\stty[23456]/d' '$rootdir/etc/inittab'");
8ab34b87
DM
1402 }
1403
1404 # Link /etc/mtab to /proc/mounts, so df and friends will work:
1405 unlink "$rootdir/etc/mtab";
1406 $self->ve_command ("ln -s /proc/mounts /etc/mtab");
1407
1408 # reset password
1409 $self->ve_command ("usermod -L root");
1410
1411 # regenerate sshd host keys
1412 $self->install_init_script ($script_ssh_init, 2, 14);
1413
1414 if ($mta eq 'postfix') {
1415 $data = "postfix postfix/main_mailer_type select No configuration\n";
1416 $self->ve_debconfig_set ($data);
1417
1418 unlink "$rootdir/etc/mailname";
1419 write_file ($postfix_main_cf, "$rootdir/etc/postfix/main.cf");
1420 }
1421
1422 if (!$opts->{minimal}) {
1423 # unpack standard packages
1424 foreach my $p (@$standard) {
1425 $self->ve_dpkg ('unpack', $p);
1426 }
1427
1428 $self->logmsg ("configure standard packages\n");
1429 $self->ve_command ("dpkg --force-confold --skip-same-version --configure -a");
1430 }
1431
1432 # disable HWCLOCK access
1433 $self->run_command ("echo 'HWCLOCKACCESS=no' >> '$rootdir/etc/default/rcS'");
1434
1435 # disable hald
1436 $self->ve_divert_add ("/usr/sbin/hald");
1437
1438 # disable /dev/urandom init
1439 $self->run_command ("install -m 0755 '$script_init_urandom' '$rootdir/etc/init.d/urandom'");
1440
1441 if ($suite eq 'etch' || $suite eq 'hardy' || $suite eq 'intrepid' || $suite eq 'jaunty') {
1442 # avoid klogd start
1443 $self->ve_divert_add ("/sbin/klogd");
1444 }
1445
1446 # remove unnecessays sysctl entries to avoid warnings
1447 my $cmd = 'sed';
1448 $cmd .= ' -e \'s/^\(kernel\.printk.*\)/#\1/\'';
1449 $cmd .= ' -e \'s/^\(kernel\.maps_protect.*\)/#\1/\'';
1450 $cmd .= ' -e \'s/^\(fs\.inotify\.max_user_watches.*\)/#\1/\'';
1451 $cmd .= ' -e \'s/^\(vm\.mmap_min_addr.*\)/#\1/\'';
1452 $cmd .= " -i '$rootdir/etc/sysctl.conf'";
1453 $self->run_command ($cmd);
1454
1455 my $bindv6only = "$rootdir/etc/sysctl.d/bindv6only.conf";
1456 if (-f $bindv6only) {
1457 $cmd = 'sed';
1458 $cmd .= ' -e \'s/^\(net\.ipv6\.bindv6only.*\)/#\1/\'';
1459 $cmd .= " -i '$bindv6only'";
1460 $self->run_command ($cmd);
1461 }
1462
1463 if ($suite eq 'hardy' || $suite eq 'intrepid' || $suite eq 'jaunty') {
1464 # disable tty init (console-setup)
1465 my $cmd = 'sed';
1466 $cmd .= ' -e \'s/^\(ACTIVE_CONSOLES=.*\)/ACTIVE_CONSOLES=/\'';
1467 $cmd .= " -i '$rootdir/etc/default/console-setup'";
1468 $self->run_command ($cmd);
1469 }
1470
1471 if ($suite eq 'intrepid') {
1472 # remove sysctl setup (avoid warnings at startup)
1473 my $filelist = "$rootdir/etc/sysctl.d/10-console-messages.conf";
1474 $filelist .= " $rootdir/etc/sysctl.d/10-process-security.conf";
1475 $filelist .= " $rootdir/etc/sysctl.d/10-network-security.conf";
1476 $self->run_command ("rm $filelist");
1477 }
1478 if ($suite eq 'jaunty') {
1479 # remove sysctl setup (avoid warnings at startup)
1480 my $filelist = "$rootdir/etc/sysctl.d/10-console-messages.conf";
1481 $filelist .= " $rootdir/etc/sysctl.d/10-network-security.conf";
1482 $self->run_command ("rm $filelist");
1483 }
1484}
1485
1486sub enter {
1487 my ($self) = @_;
1488
1489 my $veid = $self->{veid};
1490
1491 my $vestat = $self->ve_status();
1492
1493 if (!$vestat->{exist}) {
1494 $self->logmsg ("Please create the appliance first (bootstrap)");
1495 return;
1496 }
1497
1498 if (!$vestat->{running}) {
1499 $self->run_command ("vzctl start $veid");
1500 }
1501
1502 system ("vzctl enter $veid");
1503}
1504
1505sub ve_mysql_command {
1506 my ($self, $sql, $password) = @_;
1507
1508 #my $bootstrap = "/usr/sbin/mysqld --bootstrap --user=mysql --skip-grant-tables " .
1509 #"--skip-bdb --skip-innodb --skip-ndbcluster";
1510
1511 $self->ve_command ("mysql", $sql);
1512}
1513
1514sub ve_mysql_bootstrap {
1515 my ($self, $sql, $password) = @_;
1516
1517 my $cmd;
1518
1519 my $suite = $self->{config}->{suite};
dcc99599
DM
1520
1521 if ($suite eq 'jessie') {
1522 my $rootdir = $self->vz_root_dir();
1523 $self->run_command ("sed -e 's/^key_buffer\\s*=/key_buffer_size =/' -i $rootdir/etc/mysql/my.cnf");
1524 }
1525
12152e08 1526 if ($suite eq 'squeeze' || $suite eq 'wheezy' || $suite eq 'jessie') {
8ab34b87
DM
1527 $cmd = "/usr/sbin/mysqld --bootstrap --user=mysql --skip-grant-tables";
1528
1529 } else {
1530 $cmd = "/usr/sbin/mysqld --bootstrap --user=mysql --skip-grant-tables " .
1531 "--skip-bdb --skip-innodb --skip-ndbcluster";
1532 }
1533
1534 $self->ve_command ($cmd, $sql);
1535}
1536
1537sub compute_required {
1538 my ($self, $pkglist) = @_;
1539
1540 my $pkginfo = $self->pkginfo();
1541 my $instpkgs = $self->read_installed ();
1542
1543 my $closure = {};
1544 __record_provides ($pkginfo, $closure, [keys %$instpkgs]);
1545
1546 return $self->closure ($closure, $pkglist);
1547}
1548
1549sub task_postgres {
1550 my ($self, $opts) = @_;
1551
1552 my @supp = ('7.4', '8.1');
1553 my $pgversion = '8.1';
1554
1555 my $suite = $self->{config}->{suite};
1556
1557 if ($suite eq 'lenny' || $suite eq 'hardy' || $suite eq 'intrepid' || $suite eq 'jaunty') {
1558 @supp = ('8.3');
1559 $pgversion = '8.3';
1560 } elsif ($suite eq 'squeeze') {
1561 @supp = ('8.4');
1562 $pgversion = '8.4';
632ea034
DM
1563 } elsif ($suite eq 'wheezy') {
1564 @supp = ('9.1');
1565 $pgversion = '9.1';
5d0f537e
DM
1566 } elsif ($suite eq 'jessie') {
1567 @supp = ('9.4');
1568 $pgversion = '9.4';
8ab34b87
DM
1569 }
1570
1571 $pgversion = $opts->{version} if $opts->{version};
1572
1573 die "unsupported postgres version '$pgversion'\n"
1574 if !grep { $pgversion eq $_; } @supp;
1575
1576 my $rootdir = $self->vz_root_dir();
1577
1578 my $required = $self->compute_required (["postgresql-$pgversion"]);
1579
1580 $self->cache_packages ($required);
1581
1582 $self->ve_dpkg ('install', @$required);
1583
632ea034 1584 my $iscript = "postgresql-$pgversion";
5d0f537e 1585 if ($suite eq 'squeeze' || $suite eq 'wheezy' || $suite eq 'jessie') {
632ea034
DM
1586 $iscript = 'postgresql';
1587 }
8ab34b87
DM
1588
1589 $self->ve_command ("/etc/init.d/$iscript start") if $opts->{start};
1590}
1591
1592sub task_mysql {
1593 my ($self, $opts) = @_;
1594
1595 my $password = $opts->{password};
1596 my $rootdir = $self->vz_root_dir();
1597
1598 my $suite = $self->{config}->{suite};
1599
632ea034
DM
1600 my $ver = '5.0';
1601 if ($suite eq 'squeeze') {
1602 $ver = '5.1';
5d0f537e 1603 } elsif ($suite eq 'wheezy' || $suite eq 'jessie') {
632ea034
DM
1604 $ver = '5.5';
1605 }
8ab34b87
DM
1606
1607 my $required = $self->compute_required (['mysql-common', "mysql-server-$ver"]);
1608
1609 $self->cache_packages ($required);
1610
1611 $self->ve_dpkg ('install', @$required);
1612
1613 # fix security (see /usr/bin/mysql_secure_installation)
1614 my $sql = "DELETE FROM mysql.user WHERE User='';\n" .
1615 "DELETE FROM mysql.user WHERE User='root' AND Host!='localhost';\n" .
1616 "FLUSH PRIVILEGES;\n";
1617 $self->ve_mysql_bootstrap ($sql);
1618
1619 if ($password) {
1620
1621 my $rpw = $password eq 'random' ? 'admin' : $password;
1622
1623 my $sql = "USE mysql;\n" .
1624 "UPDATE user SET password=PASSWORD(\"$rpw\") WHERE user='root';\n" .
1625 "FLUSH PRIVILEGES;\n";
1626 $self->ve_mysql_bootstrap ($sql);
1627
1628 write_file ("[client]\nuser=root\npassword=\"$rpw\"\n", "$rootdir/root/.my.cnf", 0600);
1629 if ($password eq 'random') {
1630 $self->install_init_script ($script_mysql_randompw, 2, 20);
1631 }
1632 }
1633
1634 $self->ve_command ("/etc/init.d/mysql start") if $opts->{start};
1635}
1636
1637sub task_php {
1638 my ($self, $opts) = @_;
1639
1640 my $memlimit = $opts->{memlimit};
1641 my $rootdir = $self->vz_root_dir();
1642
1643 my $required = $self->compute_required ([qw (php5 php5-cli libapache2-mod-php5 php5-gd)]);
1644
1645 $self->cache_packages ($required);
1646
1647 $self->ve_dpkg ('install', @$required);
1648
1649 if ($memlimit) {
1650 $self->run_command ("sed -e 's/^\\s*memory_limit\\s*=.*;/memory_limit = ${memlimit}M;/' -i $rootdir/etc/php5/apache2/php.ini");
1651 }
1652}
1653
1654sub install {
1655 my ($self, $pkglist, $unpack) = @_;
1656
1657 my $required = $self->compute_required ($pkglist);
1658
1659 $self->cache_packages ($required);
1660
1661 $self->ve_dpkg ($unpack ? 'unpack' : 'install', @$required);
1662}
1663
1664sub cleanup {
1665 my ($self, $distclean) = @_;
1666
1667 unlink $self->{logfile};
1668 unlink "$self->{targetname}.tar";
1669 unlink "$self->{targetname}.tar.gz";
1670
1671 $self->ve_destroy ();
1672 unlink ".veid";
1673
1674 rmtree $self->{cachedir} if $distclean && !$self->{config}->{cachedir};
1675
1676 rmtree $self->{infodir};
1677
1678}
1679
16801;