11 use POSIX qw
(LONG_MAX
);
13 # fixme: lock container ?
15 my $dablibdir = "/usr/lib/dab";
16 my $devicetar = "$dablibdir/devices.tar.gz";
17 my $default_env = "$dablibdir/scripts/defenv";
18 my $fake_init = "$dablibdir/scripts/init.pl";
19 my $script_ssh_init = "$dablibdir/scripts/ssh_gen_host_keys";
20 my $script_mysql_randompw = "$dablibdir/scripts/mysql_randompw";
21 my $script_init_urandom = "$dablibdir/scripts/init_urandom";
23 my $postfix_main_cf = <<EOD;
24 # See /usr/share/postfix/main.cf.dist for a commented, more complete version
26 smtpd_banner = \$myhostname ESMTP \$mail_name (Debian/GNU)
29 # appending .domain is the MUA's job.
30 append_dot_mydomain = no
32 # Uncomment the next line to generate "delayed mail" warnings
33 #delay_warning_time = 4h
35 alias_maps = hash:/etc/aliases
36 alias_database = hash:/etc/aliases
37 mydestination = \$myhostname, localhost.\$mydomain, localhost
39 mynetworks = 127.0.0.0/8
40 inet_interfaces = loopback-only
41 recipient_delimiter = +
45 # produce apt compatible filenames (/var/lib/apt/lists)
46 sub __url_to_filename
{
57 my ($self, $url, $path) = @_;
59 $self->logmsg ("download: $url\n");
60 my $tmpfn = "$path.tmp$$";
62 $self->run_command ("wget -q '$url' -O '$tmpfn'");
71 rename ($tmpfn, $path);
75 my ($data, $file, $perm) = @_;
77 die "no filename" if !$file;
81 my $fh = IO
::File-
>new ($file, O_WRONLY
| O_CREAT
, $perm) ||
82 die "unable to open file '$file'";
92 die "no filename" if !$file;
94 my $fh = IO
::File-
>new ($file) ||
95 die "unable to open file '$file'";
97 local $/; # slurp mode
111 my $fh = IO
::File-
>new ("<$filename") || return $res;
114 while (defined (my $line = <$fh>)) {
115 next if $line =~ m/^\#/;
116 next if $line =~ m/^\s*$/;
126 if ($rec =~ s/^Description:\s*([^\n]*)(\n\s+.*)*$//si) {
127 $res->{headline
} = $1;
128 chomp $res->{headline
};
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;
138 die "duplicate key '$key'\n" if defined ($res->{$key});
139 $res->{$key} = $value;
142 die "unable to parse config file: $rec";
146 die "unable to parse config file" if $rec;
152 my ($self, $cmd, $input, $getoutput) = @_;
154 my $reader = IO
::File-
>new();
155 my $writer = IO
::File-
>new();
156 my $error = IO
::File-
>new();
160 my $cmdstr = ref ($cmd) eq 'ARRAY' ?
join (' ', @$cmd) : $cmd;
164 if (ref ($cmd) eq 'ARRAY') {
165 $pid = open3
($writer, $reader, $error, @$cmd) || die $!;
167 $pid = open3
($writer, $reader, $error, $cmdstr) || die $!;
174 if ($orig_pid != $$) {
175 $self->logmsg ("ERROR: command '$cmdstr' failed - fork failed\n");
182 print $writer $input if defined $input;
185 my $select = new IO
::Select
;
186 $select->add ($reader);
187 $select->add ($error);
190 my $logfd = $self->{logfd
};
192 while ($select->count) {
193 my @handles = $select->can_read ();
195 foreach my $h (@handles) {
197 my $count = sysread ($h, $buf, 4096);
198 if (!defined ($count)) {
200 die "command '$cmdstr' failed: $!";
202 $select->remove ($h) if !$count;
206 $res .= $buf if $getoutput;
213 die "command '$cmdstr' failed with exit code $ec\n" if $ec;
221 $self->writelog (@_);
226 my $fd = $self->{logfd
};
230 sub __sample_config
{
231 my ($self, $mem) = @_;
234 my $nolimit = "\"$max:$max\"";
244 die "unknown memory size
" if !defined ($defaults->{$mem});
248 $data .= "# DAB default config for ${mem}MB RAM\n\n";
250 $data .= "ONBOOT=\"no\"\n";
252 $data .= "\n# Primary parameters\n";
253 $data .= "NUMPROC=\"1024:1024\"\n";
254 $data .= "NUMTCPSOCK=$nolimit\n";
255 $data .= "NUMOTHERSOCK=$nolimit\n";
257 my $vmguarpages = int ($mem*1024/4);
258 $data .= "VMGUARPAGES=\"$vmguarpages:$max\"\n";
260 $data .= "\n# Secondary parameters\n";
262 $data .= "KMEMSIZE=$nolimit\n";
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";
269 $data .= "TCPSNDBUF=$nolimit\n";
270 $data .= "TCPRCVBUF=$nolimit\n";
271 $data .= "OTHERSOCKBUF=$nolimit\n";
272 $data .= "DGRAMRCVBUF=$nolimit\n";
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";
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";
292 $data .= "\n# CPU fair sheduler parameter\n";
293 $data .= "CPUUNITS=\"1000\"\n\n";
295 $data .= "\n# Template parameter\n";
296 $data .= "OSTEMPLATE=\"$self->{targetname}\"\n";
297 $data .= "HOSTNAME=\"localhost\"\n";
306 if (my $fd = IO
::File-
>new (".veid")) {
312 my $cfgdir = "/etc/vz/conf";
315 $self->{veid
} = $cid;
316 $self->{veconffile
} = "$cfgdir/$cid.conf";
320 my $cdata = $self->__sample_config (1024);
324 for (my $id = $startid; $id < ($startid + 100); $id++) {
326 my $tmpfn = "$cfgdir/$id.conf.tmp$$";
327 my $target = "$cfgdir/$id.conf";
331 my $fh = IO
::File-
>new ($target, O_WRONLY
| O_CREAT
| O_EXCL
, 0644);
341 die "unable to allocate VE\n" if !$veid;
343 my $fd = IO
::File-
>new (">.veid") ||
344 die "unable to write '.veid'\n";
348 $self->logmsg ("allocated VE $veid\n");
350 $self->{veid
} = $veid;
351 $self->{veconffile
} = "$cfgdir/$veid.conf";
357 my ($class, $config) = @_;
359 $class = ref ($class) || $class;
363 $config = read_config
('dab.conf') if !$config;
365 $self->{config
} = $config;
369 $self->{logfile
} = "logfile";
370 $self->{logfd
} = IO
::File-
>new (">>$self->{logfile}") ||
371 die "unable to open log file";
373 my $arch = $config->{architecture
};
374 die "no 'architecture' specified\n" if !$arch;
376 die "unsupported architecture '$arch'\n"
377 if $arch !~ m/^(i386|amd64)$/;
379 my $suite = $config->{suite
} || die "no 'suite' specified\n";
380 if ($suite eq 'wheezy') {
381 $config->{ostype
} = "debian-7.0";
382 } elsif ($suite eq 'squeeze') {
383 $config->{ostype
} = "debian-6.0";
384 } elsif ($suite eq 'lenny') {
385 $config->{ostype
} = "debian-5.0";
386 } elsif ($suite eq 'etch') {
387 $config->{ostype
} = "debian-4.0";
388 } elsif ($suite eq 'hardy') {
389 $config->{ostype
} = "ubuntu-8.04";
390 } elsif ($suite eq 'intrepid') {
391 $config->{ostype
} = "ubuntu-8.10";
392 } elsif ($suite eq 'jaunty') {
393 $config->{ostype
} = "ubuntu-9.04";
395 die "unsupported debian suite '$suite'\n";
398 my $name = $config->{name
} || die "no 'name' specified\n";
400 $name =~ m/^[a-z][0-9a-z\-\*\.]+$/ ||
401 die "illegal characters in name '$name'\n";
403 my $version = $config->{version
};
404 die "no 'version' specified\n" if !$version;
405 die "no 'section' specified\n" if !$config->{section
};
406 die "no 'description' specified\n" if !$config->{headline
};
407 die "no 'maintainer' specified\n" if !$config->{maintainer
};
409 if ($name =~ m/^$config->{ostype}/) {
410 $self->{targetname
} = "${name}_${version}_$config->{architecture}";
412 $self->{targetname
} = "$config->{ostype}-${name}_" .
413 "${version}_$config->{architecture}";
416 if (!$config->{source
}) {
417 if ($suite eq 'etch' || $suite eq 'lenny' || $suite eq 'squeeze' || $suite eq 'wheezy' ) {
418 push @{$config->{source
}}, "http://ftp.debian.org/debian SUITE main contrib";
419 push @{$config->{source
}}, "http://ftp.debian.org/debian SUITE-updates main contrib"
420 if ($suite eq 'squeeze' || $suite eq 'wheezy');
421 push @{$config->{source
}}, "http://security.debian.org SUITE/updates main contrib";
422 } elsif ($suite eq 'hardy' || $suite eq 'intrepid' || $suite eq 'jaunty') {
423 my $comp = "main restricted universe multiverse";
424 push @{$config->{source
}}, "http://archive.ubuntu.com/ubuntu SUITE $comp";
425 push @{$config->{source
}}, "http://archive.ubuntu.com/ubuntu SUITE-updates $comp";
426 push @{$config->{source
}}, "http://archive.ubuntu.com/ubuntu SUITE-security $comp";
432 foreach my $s (@{$config->{source
}}) {
433 if ($s =~ m
@^\s*((http
|ftp
)://\S
+)\s
+(\S
+)((\s
+(\S
+))+)$@) {
434 my ($url, $su, $components) = ($1, $3, $4);
435 $su =~ s/SUITE/$suite/;
436 $components =~ s/^\s+//;
437 $components =~ s/\s+$//;
439 foreach my $co (split (/\s+/, $components)) {
442 $ca = ['main'] if !$ca;
450 die "syntax error in source spezification '$s'\n";
454 foreach my $m (@{$config->{mirror
}}) {
455 if ($m =~ m
@^\s*((http
|ftp
)://\S
+)\s
*=>\s
*((http
|ftp
)://\S
+)\s
*$@) {
456 my ($ms, $md) = ($1, $3);
458 foreach my $ss (@$sources) {
459 if ($ss->{source
} eq $ms) {
465 die "unusable mirror $ms\n" if !$found;
467 die "syntax error in mirror spezification '$m'\n";
471 $self->{sources
} = $sources;
473 $self->{infodir
} = "info";
475 $self->__allocate_ve ();
477 $self->{cachedir
} = ($config->{cachedir
} || 'cache') . "/$suite";;
479 my $incl = [qw
(less ssh openssh-server logrotate
)];
481 my $excl = [qw
(modutils reiserfsprogs ppp pppconfig pppoe
482 pppoeconf nfs-common mtools ntp
)];
484 # ubuntu has too many dependencies on udev, so
485 # we cannot exclude it (instead we disable udevd)
486 if ($suite eq 'hardy') {
487 push @$excl, qw(kbd);
488 push @$excl, qw(apparmor apparmor-utils ntfs-3g
490 } elsif($suite eq 'intrepid' || $suite eq 'jaunty') {
491 push @$excl, qw(apparmor apparmor-utils libapparmor1 libapparmor-perl
492 libntfs-3g28 ntfs-3g friendly-recovery);
494 push @$excl, qw(udev module-init-tools pciutils hdparm
498 $self->{incl
} = $incl;
499 $self->{excl
} = $excl;
507 my $infodir = $self->{infodir
};
508 my $arch = $self->{config
}->{architecture
};
514 my $logfd = $self->{logfd
} = IO
::File-
>new (">$self->{logfile}") ||
515 die "unable to open log file";
517 foreach my $ss (@{$self->{sources
}}) {
518 my $src = $ss->{mirror
} || $ss->{source
};
519 my $path = "dists/$ss->{suite}/Release";
520 my $url = "$src/$path";
521 my $target = __url_to_filename
("$ss->{source}/$path");
523 $self->download ($url, "$infodir/$target");
524 $self->download ("$url.gpg", "$infodir/$target.gpg");
525 # fixme: impl. verify (needs --keyring option)
529 warn "Release info ignored\n";
531 foreach my $comp (@{$ss->{comp
}}) {
532 $path = "dists/$ss->{suite}/$comp/binary-$arch/Packages.gz";
533 $target = "$infodir/" . __url_to_filename
("$ss->{source}/$path");
534 my $pkgsrc = "$src/$path";
535 $self->download ($pkgsrc, $target);
536 $self->run_command ("gzip -d '$target'");
542 my ($self, $filename, $size) = @_;
544 my $config = $self->{config
};
548 $data .= "Name: $config->{name}\n";
549 $data .= "Version: $config->{version}\n";
550 $data .= "Type: openvz\n";
551 $data .= "OS: $config->{ostype}\n";
552 $data .= "Section: $config->{section}\n";
553 $data .= "Maintainer: $config->{maintainer}\n";
554 $data .= "Architecture: $config->{architecture}\n";
555 $data .= "Installed-Size: $size\n";
558 $data .= "Infopage: $config->{infopage}\n" if $config->{infopage
};
559 $data .= "ManageUrl: $config->{manageurl}\n" if $config->{manageurl
};
560 $data .= "Certified: $config->{certified}\n" if $config->{certified
};
563 $data .= "Description: $config->{headline}\n";
564 $data .= "$config->{description}\n" if $config->{description
};
566 write_file
($data, $filename, 0644);
570 my ($self, $opts) = @_;
572 my $suite = $self->{config
}->{suite
};
573 my $infodir = $self->{infodir
};
574 my $arch = $self->{config
}->{architecture
};
576 my $instpkgs = $self->read_installed ();
577 my $pkginfo = $self->pkginfo();
578 my $veid = $self->{veid
};
579 my $rootdir = $self->vz_root_dir();
581 my $vestat = $self->ve_status();
582 die "ve not running - unable to finalize\n" if !$vestat->{running
};
585 if (-f
"$rootdir/etc/init.d/mysql") {
586 $self->ve_command ("/etc/init.d/mysql stop");
589 if (!($opts->{keepmycnf
} || (-f
"$rootdir/etc/init.d/mysql_randompw"))) {
590 unlink "$rootdir/root/.my.cnf";
593 if ($suite eq 'etch') {
594 # enable apache2 startup
595 if ($instpkgs->{apache2
}) {
596 write_file
("NO_START=0\n", "$rootdir/etc/default/apache2");
598 unlink "$rootdir/etc/default/apache2";
601 $self->logmsg ("cleanup package status\n");
602 # prevent auto selection of all standard, required or important
603 # packages which are not installed
604 foreach my $pkg (keys %$pkginfo) {
605 my $pri = $pkginfo->{$pkg}->{priority
};
606 if ($pri && ($pri eq 'required' || $pri eq 'important'
607 || $pri eq 'standard')) {
608 if (!$instpkgs->{$pkg}) {
609 $self->ve_dpkg_set_selection ($pkg, 'purge');
614 $self->ve_command ("apt-get clean");
616 $self->logmsg ("update available package list\n");
618 $self->ve_command ("dpkg --clear-avail");
619 foreach my $ss (@{$self->{sources
}}) {
620 my $relsrc = __url_to_filename
("$ss->{source}/dists/$ss->{suite}/Release");
621 if (-f
"$infodir/$relsrc" && -f
"$infodir/$relsrc.gpg") {
622 $self->run_command ("cp '$infodir/$relsrc' '$rootdir/var/lib/apt/lists/$relsrc'");
623 $self->run_command ("cp '$infodir/$relsrc.gpg' '$rootdir/var/lib/apt/lists/$relsrc.gpg'");
625 foreach my $comp (@{$ss->{comp
}}) {
626 my $src = __url_to_filename
("$ss->{source}/dists/$ss->{suite}/" .
627 "$comp/binary-$arch/Packages");
628 my $target = "/var/lib/apt/lists/$src";
629 $self->run_command ("cp '$infodir/$src' '$rootdir/$target'");
630 $self->ve_command ("dpkg --merge-avail '$target'");
634 # set dselect default method
635 write_file
("apt apt\n", "$rootdir/var/lib/dpkg/cmethopt");
637 $self->ve_divert_remove ("/usr/sbin/policy-rc.d");
639 $self->ve_divert_remove ("/sbin/start-stop-daemon");
641 $self->ve_divert_remove ("/sbin/init");
643 # finally stop the VE
644 $self->run_command ("vzctl stop $veid --fast");
645 $rootdir = $self->vz_priv_dir();
647 unlink "$rootdir/sbin/defenv";
649 unlink <$rootdir/root/dead.letter
*>;
651 unlink "$rootdir/var/log/init.log";
653 unlink "$rootdir/aquota.group";
655 unlink "$rootdir/aquota.user";
657 write_file
("", "$rootdir/var/log/syslog");
659 $self->logmsg ("detecting final size: ");
661 my $sizestr = $self->run_command ("du -sm $rootdir", undef, 1);
663 if ($sizestr =~ m/^(\d+)\s+\Q$rootdir\E$/) {
666 die "unable to detect size\n";
668 $self->logmsg ("$size MB\n");
670 $self->write_config ("$rootdir/etc/appliance.info", $size);
672 $self->logmsg ("creating final appliance archive\n");
674 my $target = "$self->{targetname}.tar";
678 $self->run_command ("tar cpf $target --numeric-owner -C '$rootdir' ./etc/appliance.info");
679 $self->run_command ("tar rpf $target --numeric-owner -C '$rootdir' --exclude ./etc/appliance.info .");
680 $self->run_command ("gzip $target");
686 my $rootdir = $self->vz_priv_dir();
688 my $pkgfilelist = "$rootdir/var/lib/dpkg/status";
690 open (PKGLST
, "<$pkgfilelist") ||
691 die "unable to open '$pkgfilelist'";
695 while (my $rec = <PKGLST
>) {
701 while ($rec =~ s/^([^:]+):\s+(.*)\s*\n//) {
705 my $pkg = $res->{'package'};
706 if (my $status = $res->{status
}) {
707 my @sa = split (/\s+/, $status);
709 if ($stat && ($stat ne 'purge')) {
710 $pkglist->{$pkg} = $res;
723 my $veid = $self->{veid
};
725 return "/var/lib/vz/root/$veid";
731 my $veid = $self->{veid
};
733 return "/var/lib/vz/private/$veid";
739 my $veid = $self->{veid
};
741 my $res = $self->run_command ("vzctl status $veid", undef, 1);
744 if ($res =~ m/^CTID\s+$veid\s+(exist|deleted)\s+(mounted|unmounted)\s+(running|down)$/) {
746 exist
=> $1 eq 'exist',
747 mounted
=> $2 eq 'mounted',
748 running
=> $3 eq 'running',
751 die "unable to parse ve status";
756 my ($self, $cmd, $input) = @_;
758 my $veid = $self->{veid
};
760 if (ref ($cmd) eq 'ARRAY') {
761 unshift @$cmd, 'vzctl', 'exec2', $veid, 'defenv';
762 $self->run_command ($cmd, $input);
764 $self->run_command ("vzctl exec2 $veid defenv $cmd", $input);
768 # like ve_command, but pipes stdin correctly
770 my ($self, @cmd) = @_;
772 my $veid = $self->{veid
};
775 my $pid = open2
($reader, "<&STDIN", 'vzctl', 'exec2', $veid,
776 'defenv', @cmd) || die "unable to exec command";
778 while (defined (my $line = <$reader>)) {
779 $self->logmsg ($line);
785 die "ve_exec failed - status $rc\n" if $rc != 0;
789 my ($self, $filename) = @_;
791 $self->ve_command ("dpkg-divert --add --divert '$filename.distrib' " .
792 "--rename '$filename'");
794 sub ve_divert_remove
{
795 my ($self, $filename) = @_;
797 my $rootdir = $self->vz_root_dir();
799 unlink "$rootdir/$filename";
800 $self->ve_command ("dpkg-divert --remove --rename '$filename'");
803 sub ve_debconfig_set
{
804 my ($self, $dcdata) = @_;
806 my $rootdir = $self->vz_root_dir();
807 my $cfgfile = "/tmp/debconf.txt";
808 write_file
($dcdata, "$rootdir/$cfgfile");
809 $self->ve_command ("debconf-set-selections $cfgfile");
810 unlink "$rootdir/$cfgfile";
813 sub ve_dpkg_set_selection
{
814 my ($self, $pkg, $status) = @_;
816 $self->ve_command ("dpkg --set-selections", "$pkg $status");
820 my ($self, $cmd, @pkglist) = @_;
822 return if !scalar (@pkglist);
824 my $pkginfo = $self->pkginfo();
826 my $rootdir = $self->vz_root_dir();
827 my $cachedir = $self->{cachedir
};
831 foreach my $pkg (@pkglist) {
832 my $filename = $self->getpkgfile ($pkg);
833 $self->run_command ("cp '$cachedir/$filename' '$rootdir/$filename'");
834 push @files, "/$filename";
835 $self->logmsg ("$cmd: $pkg\n");
838 my $fl = join (' ', @files);
840 if ($cmd eq 'install') {
841 $self->ve_command ("dpkg --force-depends --force-confold --install $fl");
842 } elsif ($cmd eq 'unpack') {
843 $self->ve_command ("dpkg --force-depends --unpack $fl");
845 die "internal error";
848 foreach my $fn (@files) { unlink "$rootdir$fn"; }
854 my $veid = $self->{veid
}; # fixme
856 my $vestat = $self->ve_status();
857 if ($vestat->{running
}) {
858 $self->run_command ("vzctl stop $veid --fast");
859 } elsif ($vestat->{mounted
}) {
860 $self->run_command ("vzctl umount $veid");
862 if ($vestat->{exist
}) {
863 $self->run_command ("vzctl destroy $veid");
865 unlink $self->{veconffile
};
872 my $root = $self->vz_root_dir();
873 my $priv = $self->vz_priv_dir();
875 my $veid = $self->{veid
}; # fixme
877 $self->logmsg ("initialize VE $veid\n");
880 my $vestat = $self->ve_status();
881 if ($vestat->{running
}) {
882 $self->run_command ("vzctl stop $veid --fast");
883 } elsif ($vestat->{mounted
}) {
884 $self->run_command ("vzctl umount $veid");
896 $self->run_command ("vzctl mount $veid");
899 sub __deb_version_cmp
{
900 my ($cur, $op, $new) = @_;
902 if (system("dpkg", "--compare-versions", $cur, $op, $new) == 0) {
909 sub __parse_packages
{
910 my ($pkginfo, $filename, $src) = @_;
913 open (PKGLST
, "<$filename") ||
914 die "unable to open '$filename'";
916 while (my $rec = <PKGLST
>) {
923 while ($rec =~ s/^([^:]+):\s+(.*)\s*\n//) {
927 my $pkg = $res->{'package'};
928 if ($pkg && $res->{'filename'}) {
930 if (my $info = $pkginfo->{$pkg}) {
931 $cur = $info->{version
};
933 my $new = $res->{version
};
934 if (!$cur || __deb_version_cmp
($cur, 'lt', $new)) {
936 $res->{url
} = "$src/$res->{'filename'}";
938 die "no url for package '$pkg'" if !$res->{url
};
940 $pkginfo->{$pkg} = $res;
951 return $self->{pkginfo
} if $self->{pkginfo
};
953 my $infodir = $self->{infodir
};
954 my $arch = $self->{config
}->{architecture
};
956 my $availfn = "$infodir/available";
961 # reading 'available' is faster, because it only contains latest version
962 # (no need to do slow version compares)
964 __parse_packages
($pkginfo, $availfn);
965 $self->{pkginfo
} = $pkginfo;
969 $self->logmsg ("generating available package list\n");
971 foreach my $ss (@{$self->{sources
}}) {
972 foreach my $comp (@{$ss->{comp
}}) {
973 my $url = "$ss->{source}/dists/$ss->{suite}/$comp/binary-$arch/Packages";
974 my $pkgfilelist = "$infodir/" . __url_to_filename
($url);
976 my $src = $ss->{mirror
} || $ss->{source
};
978 __parse_packages
($pkginfo, $pkgfilelist, $src);
982 if (my $dep = $self->{config
}->{depends
}) {
983 foreach my $d (split (/,/, $dep)) {
984 if ($d =~ m/^\s*(\S+)\s*(\((\S+)\s+(\S+)\)\s*)?$/) {
985 my ($pkg, $op, $rver) = ($1, $3, $4);
986 $self->logmsg ("checking dependencies: $d\n");
987 my $info = $pkginfo->{$pkg};
988 die "package '$pkg' not available\n" if !$info;
990 my $cver = $info->{version
};
991 if (!__deb_version_cmp
($cver, $op, $rver)) {
992 die "detected wrong version '$cver'\n";
996 die "syntax error in depends field";
1001 $self->{pkginfo
} = $pkginfo;
1003 my $tmpfn = "$availfn.tmp$$";
1004 my $fd = IO
::File-
>new (">$tmpfn");
1005 foreach my $pkg (sort keys %$pkginfo) {
1006 my $info = $pkginfo->{$pkg};
1007 print $fd "package: $pkg\n";
1008 foreach my $k (sort keys %$info) {
1009 next if $k eq 'description';
1010 next if $k eq 'package';
1011 my $v = $info->{$k};
1012 print $fd "$k: $v\n" if $v;
1014 print $fd "description: $info->{description}\n" if $info->{description
};
1019 rename ($tmpfn, $availfn);
1024 sub __record_provides
{
1025 my ($pkginfo, $closure, $list, $skipself) = @_;
1027 foreach my $pname (@$list) {
1028 my $info = $pkginfo->{$pname};
1029 # fixme: if someone install packages directly using dpkg, there
1030 # is no entry in 'available', only in 'status'. In that case, we
1031 # should extract info from $instpkgs
1033 warn "hint: ignoring provides for '$pname' - package not in 'available' list.\n";
1036 if (my $prov = $info->{provides
}) {
1037 my @pl = split (',', $prov);
1038 foreach my $p (@pl) {
1040 if (!($skipself && (grep { $1 eq $_ } @$list))) {
1045 $closure->{$pname} = 1 if !$skipself;
1050 my ($self, $closure, $list) = @_;
1052 my $pkginfo = $self->pkginfo();
1054 # first, record provided packages
1055 __record_provides
($pkginfo, $closure, $list, 1);
1059 # then resolve dependencies
1060 foreach my $pname (@$list) {
1061 __closure_single
($pkginfo, $closure, $pkgs, $pname, $self->{excl
});
1064 return [ keys %$pkgs ];
1067 sub __closure_single
{
1068 my ($pkginfo, $closure, $pkgs, $pname, $excl) = @_;
1073 return if $closure->{$pname};
1075 my $info = $pkginfo->{$pname} || die "no such package '$pname'";
1077 my $dep = $info->{depends
};
1078 my $predep = $info->{'pre-depends'};
1080 my $size = $info->{size
};
1081 my $url = $info->{url
};
1083 $url || die "$pname: no url for package '$pname'";
1085 $pkgs->{$pname} = 1;
1087 __record_provides
($pkginfo, $closure, [$pname]) if $info->{provides
};
1089 $closure->{$pname} = 1;
1095 push @l, split (/,/, $predep) if $predep;
1096 push @l, split (/,/, $dep) if $dep;
1098 DEPEND
: foreach my $p (@l) {
1099 my @l1 = split (/\|/, $p);
1100 foreach my $p1 (@l1) {
1101 if ($p1 =~ m/^\s*(\S+).*/) {
1102 #printf (STDERR "$pname: $p --> $1\n");
1103 if ($closure->{$1}) {
1104 next DEPEND
; # dependency already met
1108 # search for non-excluded alternative
1110 foreach my $p1 (@l1) {
1111 if ($p1 =~ m/^\s*(\S+).*/) {
1112 next if grep { $1 eq $_ } @$excl;
1117 die "package '$pname' depends on exclusion '$p'\n" if !$found;
1119 #printf (STDERR "$pname: $p --> $found\n");
1121 __closure_single
($pkginfo, $closure, $pkgs, $found, $excl);
1125 sub cache_packages
{
1126 my ($self, $pkglist) = @_;
1128 foreach my $pkg (@$pkglist) {
1129 $self->getpkgfile ($pkg);
1134 my ($self, $pkg) = @_;
1136 my $pkginfo = $self->pkginfo();
1137 my $info = $pkginfo->{$pkg} || die "no such package '$pkg'";
1138 my $cachedir = $self->{cachedir
};
1140 my $url = $info->{url
};
1143 if ($url =~ m
|/([^/]+.deb
)$|) {
1146 die "internal error";
1149 return $filename if -f
"$cachedir/$filename";
1153 $self->download ($url, "$cachedir/$filename");
1158 sub install_init_script
{
1159 my ($self, $script, $runlevel, $prio) = @_;
1161 my $suite = $self->{config
}->{suite
};
1162 my $rootdir = $self->vz_root_dir();
1164 my $base = basename
($script);
1165 my $target = "$rootdir/etc/init.d/$base";
1167 $self->run_command ("install -m 0755 '$script' '$target'");
1168 if ($suite eq 'etch' || $suite eq 'lenny') {
1169 $self->ve_command ("update-rc.d $base start $prio $runlevel .");
1171 $self->ve_command ("insserv $base");
1178 my ($self, $opts) = @_;
1180 my $pkginfo = $self->pkginfo();
1181 my $veid = $self->{veid
};
1182 my $suite = $self->{config
}->{suite
};
1184 my $important = [ @{$self->{incl
}} ];
1188 my $mta = $opts->{exim
} ?
'exim' : 'postfix';
1190 if ($mta eq 'postfix') {
1191 push @$important, "postfix";
1194 foreach my $p (keys %$pkginfo) {
1195 next if grep { $p eq $_ } @{$self->{excl
}};
1196 my $pri = $pkginfo->{$p}->{priority
};
1198 next if $mta ne 'exim' && $p =~ m/exim/;
1199 next if $p =~ m/(selinux|semanage|policycoreutils)/;
1201 push @$required, $p if $pri eq 'required';
1202 push @$important, $p if $pri eq 'important';
1203 push @$standard, $p if $pri eq 'standard' && !$opts->{minimal
};
1207 $required = $self->closure ($closure, $required);
1208 $important = $self->closure ($closure, $important);
1210 if (!$opts->{minimal
}) {
1211 push @$standard, 'xbase-clients';
1212 $standard = $self->closure ($closure, $standard);
1215 # test if we have all 'ubuntu-minimal' and 'ubuntu-standard' packages
1216 # except those explicitly excluded
1217 if ($suite eq 'hardy' || $suite eq 'intrepid' || $suite eq 'jaunty') {
1218 my $mdeps = $pkginfo->{'ubuntu-minimal'}->{depends
};
1219 foreach my $d (split (/,/, $mdeps)) {
1220 if ($d =~ m/^\s*(\S+)$/) {
1222 next if $closure->{$pkg};
1223 next if grep { $pkg eq $_ } @{$self->{excl
}};
1224 die "missing ubuntu-minimal package '$pkg'\n";
1227 if (!$opts->{minimal
}) {
1228 $mdeps = $pkginfo->{'ubuntu-standard'}->{depends
};
1229 foreach my $d (split (/,/, $mdeps)) {
1230 if ($d =~ m/^\s*(\S+)$/) {
1232 next if $closure->{$pkg};
1233 next if grep { $pkg eq $_ } @{$self->{excl
}};
1234 die "missing ubuntu-standard package '$pkg'\n";
1240 # download/cache all files first
1241 $self->cache_packages ($required);
1242 $self->cache_packages ($important);
1243 $self->cache_packages ($standard);
1245 my $rootdir = $self->vz_priv_dir();
1247 # extract required packages first
1248 $self->logmsg ("create basic environment\n");
1249 foreach my $p (@$required) {
1250 my $filename = $self->getpkgfile ($p);
1251 $self->run_command ("ar -p '$self->{cachedir}/$filename' data.tar.gz | zcat | tar -C '$rootdir' -xf -");
1255 my $data = "Package: dpkg\n" .
1256 "Version: $pkginfo->{dpkg}->{version}\n" .
1257 "Status: install ok installed\n";
1259 write_file
($data, "$rootdir/var/lib/dpkg/status");
1260 write_file
("", "$rootdir/var/lib/dpkg/info/dpkg.list");
1261 write_file
("", "$rootdir/var/lib/dpkg/available");
1264 foreach my $ss (@{$self->{sources
}}) {
1265 my $url = $ss->{source
};
1266 my $comp = join (' ', @{$ss->{comp
}});
1267 $data .= "deb $url $ss->{suite} $comp\n\n";
1270 write_file
($data, "$rootdir/etc/apt/sources.list");
1272 $data = "# UNCONFIGURED FSTAB FOR BASE SYSTEM\n";
1273 write_file
($data, "$rootdir/etc/fstab", 0644);
1275 write_file
("localhost\n", "$rootdir/etc/hostname", 0644);
1277 # avoid warnings about non-existent resolv.conf
1278 write_file
("", "$rootdir/etc/resolv.conf", 0644);
1280 $data = "auto lo\niface lo inet loopback\n";
1281 write_file
($data, "$rootdir/etc/network/interfaces", 0644);
1284 $self->run_command ("tar xzf '$devicetar' -C '$rootdir'");
1286 # avoid warnings about missing default locale
1287 write_file
("LANG=\"C\"\n", "$rootdir/etc/default/locale", 0644);
1290 rename ("$rootdir/sbin/init", "$rootdir/sbin/init.org");
1291 $self->run_command ("cp '$fake_init' '$rootdir/sbin/init'");
1293 $self->run_command ("cp '$default_env' '$rootdir/sbin/defenv'");
1295 $self->run_command ("vzctl start $veid");
1296 $rootdir = $self->vz_root_dir();
1298 $self->logmsg ("initialize ld cache\n");
1299 $self->ve_command ("/sbin/ldconfig");
1300 $self->run_command ("ln -sf mawk '$rootdir/usr/bin/awk'");
1302 $self->logmsg ("installing packages\n");
1304 $self->ve_dpkg ('install', 'base-files', 'base-passwd');
1306 $self->ve_dpkg ('install', 'dpkg');
1308 $self->run_command ("ln -sf /usr/share/zoneinfo/UTC '$rootdir/etc/localtime'");
1310 $self->ve_dpkg ('install', 'libc6');
1311 $self->ve_dpkg ('install', 'perl-base');
1313 unlink "$rootdir/usr/bin/awk";
1315 $self->ve_dpkg ('install', 'mawk');
1316 $self->ve_dpkg ('install', 'debconf');
1318 # unpack required packages
1319 foreach my $p (@$required) {
1320 $self->ve_dpkg ('unpack', $p);
1323 rename ("$rootdir/sbin/init.org", "$rootdir/sbin/init");
1324 $self->ve_divert_add ("/sbin/init");
1325 $self->run_command ("cp '$fake_init' '$rootdir/sbin/init'");
1327 # disable service activation
1328 $self->ve_divert_add ("/usr/sbin/policy-rc.d");
1329 $data = "#!/bin/sh\nexit 101\n";
1330 write_file
($data, "$rootdir/usr/sbin/policy-rc.d", 755);
1332 # disable start-stop-daemon
1333 $self->ve_divert_add ("/sbin/start-stop-daemon");
1337 echo \"Warning: Fake start-stop-daemon called, doing nothing\"
1339 write_file
($data, "$rootdir/sbin/start-stop-daemon", 0755);
1342 $self->ve_divert_add ("/sbin/udevd");
1344 if ($suite eq 'etch') {
1345 # disable apache2 startup
1346 write_file
("NO_START=1\n", "$rootdir/etc/default/apache2");
1349 $self->logmsg ("configure required packages\n");
1350 $self->ve_command ("dpkg --force-confold --skip-same-version --configure -a");
1352 # set postfix defaults
1353 if ($mta eq 'postfix') {
1354 $data = "postfix postfix/main_mailer_type select Local only\n";
1355 $self->ve_debconfig_set ($data);
1357 $data = "postmaster: root\nwebmaster: root\n";
1358 write_file
($data, "$rootdir/etc/aliases");
1361 if ($suite eq 'jaunty') {
1362 # jaunty does not create /var/run/network, so network startup fails.
1363 # so we do not use tmpfs for /var/run and /var/lock
1364 $self->run_command ("sed -e 's/RAMRUN=yes/RAMRUN=no/' -e 's/RAMLOCK=yes/RAMLOCK=no/' -i $rootdir/etc/default/rcS");
1365 # and create the directory here
1366 $self->run_command ("mkdir $rootdir/var/run/network");
1369 # unpack base packages
1370 foreach my $p (@$important) {
1371 $self->ve_dpkg ('unpack', $p);
1375 $self->ve_command ("ifconfig lo up");
1377 $self->logmsg ("configure important packages\n");
1378 $self->ve_command ("dpkg --force-confold --skip-same-version --configure -a");
1380 if (-d
"$rootdir/etc/event.d") {
1381 unlink <$rootdir/etc/event.d
/tty
*>;
1384 if (-f
"$rootdir/etc/inittab") {
1385 $self->run_command ("sed -i -e '/getty/d' '$rootdir/etc/inittab'");
1388 # Link /etc/mtab to /proc/mounts, so df and friends will work:
1389 unlink "$rootdir/etc/mtab";
1390 $self->ve_command ("ln -s /proc/mounts /etc/mtab");
1393 $self->ve_command ("usermod -L root");
1395 # regenerate sshd host keys
1396 $self->install_init_script ($script_ssh_init, 2, 14);
1398 if ($mta eq 'postfix') {
1399 $data = "postfix postfix/main_mailer_type select No configuration\n";
1400 $self->ve_debconfig_set ($data);
1402 unlink "$rootdir/etc/mailname";
1403 write_file
($postfix_main_cf, "$rootdir/etc/postfix/main.cf");
1406 if (!$opts->{minimal
}) {
1407 # unpack standard packages
1408 foreach my $p (@$standard) {
1409 $self->ve_dpkg ('unpack', $p);
1412 $self->logmsg ("configure standard packages\n");
1413 $self->ve_command ("dpkg --force-confold --skip-same-version --configure -a");
1416 # disable HWCLOCK access
1417 $self->run_command ("echo 'HWCLOCKACCESS=no' >> '$rootdir/etc/default/rcS'");
1420 $self->ve_divert_add ("/usr/sbin/hald");
1422 # disable /dev/urandom init
1423 $self->run_command ("install -m 0755 '$script_init_urandom' '$rootdir/etc/init.d/urandom'");
1425 if ($suite eq 'etch' || $suite eq 'hardy' || $suite eq 'intrepid' || $suite eq 'jaunty') {
1427 $self->ve_divert_add ("/sbin/klogd");
1430 # remove unnecessays sysctl entries to avoid warnings
1432 $cmd .= ' -e \'s/^\(kernel\.printk.*\)/#\1/\'';
1433 $cmd .= ' -e \'s/^\(kernel\.maps_protect.*\)/#\1/\'';
1434 $cmd .= ' -e \'s/^\(fs\.inotify\.max_user_watches.*\)/#\1/\'';
1435 $cmd .= ' -e \'s/^\(vm\.mmap_min_addr.*\)/#\1/\'';
1436 $cmd .= " -i '$rootdir/etc/sysctl.conf'";
1437 $self->run_command ($cmd);
1439 my $bindv6only = "$rootdir/etc/sysctl.d/bindv6only.conf";
1440 if (-f
$bindv6only) {
1442 $cmd .= ' -e \'s/^\(net\.ipv6\.bindv6only.*\)/#\1/\'';
1443 $cmd .= " -i '$bindv6only'";
1444 $self->run_command ($cmd);
1447 if ($suite eq 'hardy' || $suite eq 'intrepid' || $suite eq 'jaunty') {
1448 # disable tty init (console-setup)
1450 $cmd .= ' -e \'s/^\(ACTIVE_CONSOLES=.*\)/ACTIVE_CONSOLES=/\'';
1451 $cmd .= " -i '$rootdir/etc/default/console-setup'";
1452 $self->run_command ($cmd);
1455 if ($suite eq 'intrepid') {
1456 # remove sysctl setup (avoid warnings at startup)
1457 my $filelist = "$rootdir/etc/sysctl.d/10-console-messages.conf";
1458 $filelist .= " $rootdir/etc/sysctl.d/10-process-security.conf";
1459 $filelist .= " $rootdir/etc/sysctl.d/10-network-security.conf";
1460 $self->run_command ("rm $filelist");
1462 if ($suite eq 'jaunty') {
1463 # remove sysctl setup (avoid warnings at startup)
1464 my $filelist = "$rootdir/etc/sysctl.d/10-console-messages.conf";
1465 $filelist .= " $rootdir/etc/sysctl.d/10-network-security.conf";
1466 $self->run_command ("rm $filelist");
1473 my $veid = $self->{veid
};
1475 my $vestat = $self->ve_status();
1477 if (!$vestat->{exist
}) {
1478 $self->logmsg ("Please create the appliance first (bootstrap)");
1482 if (!$vestat->{running
}) {
1483 $self->run_command ("vzctl start $veid");
1486 system ("vzctl enter $veid");
1489 sub ve_mysql_command
{
1490 my ($self, $sql, $password) = @_;
1492 #my $bootstrap = "/usr/sbin/mysqld --bootstrap --user=mysql --skip-grant-tables " .
1493 #"--skip-bdb --skip-innodb --skip-ndbcluster";
1495 $self->ve_command ("mysql", $sql);
1498 sub ve_mysql_bootstrap
{
1499 my ($self, $sql, $password) = @_;
1503 my $suite = $self->{config
}->{suite
};
1505 if ($suite eq 'squeeze' || $suite eq 'wheezy' ) {
1506 $cmd = "/usr/sbin/mysqld --bootstrap --user=mysql --skip-grant-tables";
1509 $cmd = "/usr/sbin/mysqld --bootstrap --user=mysql --skip-grant-tables " .
1510 "--skip-bdb --skip-innodb --skip-ndbcluster";
1513 $self->ve_command ($cmd, $sql);
1516 sub compute_required
{
1517 my ($self, $pkglist) = @_;
1519 my $pkginfo = $self->pkginfo();
1520 my $instpkgs = $self->read_installed ();
1523 __record_provides
($pkginfo, $closure, [keys %$instpkgs]);
1525 return $self->closure ($closure, $pkglist);
1529 my ($self, $opts) = @_;
1531 my @supp = ('7.4', '8.1');
1532 my $pgversion = '8.1';
1534 my $suite = $self->{config
}->{suite
};
1536 if ($suite eq 'lenny' || $suite eq 'hardy' || $suite eq 'intrepid' || $suite eq 'jaunty') {
1539 } elsif ($suite eq 'squeeze') {
1542 } elsif ($suite eq 'wheezy') {
1547 $pgversion = $opts->{version
} if $opts->{version
};
1549 die "unsupported postgres version '$pgversion'\n"
1550 if !grep { $pgversion eq $_; } @supp;
1552 my $rootdir = $self->vz_root_dir();
1554 my $required = $self->compute_required (["postgresql-$pgversion"]);
1556 $self->cache_packages ($required);
1558 $self->ve_dpkg ('install', @$required);
1560 my $iscript = "postgresql-$pgversion";
1561 if ($suite eq 'squeeze' || $suite eq 'wheezy') {
1562 $iscript = 'postgresql';
1565 $self->ve_command ("/etc/init.d/$iscript start") if $opts->{start
};
1569 my ($self, $opts) = @_;
1571 my $password = $opts->{password
};
1572 my $rootdir = $self->vz_root_dir();
1574 my $suite = $self->{config
}->{suite
};
1577 if ($suite eq 'squeeze') {
1579 } elsif ($suite eq 'wheezy') {
1583 my $required = $self->compute_required (['mysql-common', "mysql-server-$ver"]);
1585 $self->cache_packages ($required);
1587 $self->ve_dpkg ('install', @$required);
1589 # fix security (see /usr/bin/mysql_secure_installation)
1590 my $sql = "DELETE FROM mysql.user WHERE User='';\n" .
1591 "DELETE FROM mysql.user WHERE User='root' AND Host!='localhost';\n" .
1592 "FLUSH PRIVILEGES;\n";
1593 $self->ve_mysql_bootstrap ($sql);
1597 my $rpw = $password eq 'random' ?
'admin' : $password;
1599 my $sql = "USE mysql;\n" .
1600 "UPDATE user SET password=PASSWORD(\"$rpw\") WHERE user
='root';\n" .
1601 "FLUSH PRIVILEGES
;\n";
1602 $self->ve_mysql_bootstrap ($sql);
1604 write_file ("[client
]\nuser
=root
\npassword
=\"$rpw\"\n", "$rootdir/root/.my.cnf", 0600);
1605 if ($password eq 'random') {
1606 $self->install_init_script ($script_mysql_randompw, 2, 20);
1610 $self->ve_command ("/etc/init.d/mysql start") if $opts->{start
};
1614 my ($self, $opts) = @_;
1616 my $memlimit = $opts->{memlimit
};
1617 my $rootdir = $self->vz_root_dir();
1619 my $required = $self->compute_required ([qw
(php5 php5-cli libapache2-mod-php5 php5-gd
)]);
1621 $self->cache_packages ($required);
1623 $self->ve_dpkg ('install', @$required);
1626 $self->run_command ("sed -e 's/^\\s*memory_limit\\s*=.*;/memory_limit = ${memlimit}M;/' -i $rootdir/etc/php5/apache2/php.ini");
1631 my ($self, $pkglist, $unpack) = @_;
1633 my $required = $self->compute_required ($pkglist);
1635 $self->cache_packages ($required);
1637 $self->ve_dpkg ($unpack ?
'unpack' : 'install', @$required);
1641 my ($self, $distclean) = @_;
1643 unlink $self->{logfile
};
1644 unlink "$self->{targetname}.tar";
1645 unlink "$self->{targetname}.tar.gz";
1647 $self->ve_destroy ();
1650 rmtree
$self->{cachedir
} if $distclean && !$self->{config
}->{cachedir
};
1652 rmtree
$self->{infodir
};