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 'squeeze') {
381 $config->{ostype
} = "debian-6.0";
382 } elsif ($suite eq 'lenny') {
383 $config->{ostype
} = "debian-5.0";
384 } elsif ($suite eq 'etch') {
385 $config->{ostype
} = "debian-4.0";
386 } elsif ($suite eq 'hardy') {
387 $config->{ostype
} = "ubuntu-8.04";
388 } elsif ($suite eq 'intrepid') {
389 $config->{ostype
} = "ubuntu-8.10";
390 } elsif ($suite eq 'jaunty') {
391 $config->{ostype
} = "ubuntu-9.04";
393 die "unsupported debian suite '$suite'\n";
396 my $name = $config->{name
} || die "no 'name' specified\n";
398 $name =~ m/^[a-z][0-9a-z\-\*\.]+$/ ||
399 die "illegal characters in name '$name'\n";
401 my $version = $config->{version
};
402 die "no 'version' specified\n" if !$version;
403 die "no 'section' specified\n" if !$config->{section
};
404 die "no 'description' specified\n" if !$config->{headline
};
405 die "no 'maintainer' specified\n" if !$config->{maintainer
};
407 if ($name =~ m/^$config->{ostype}/) {
408 $self->{targetname
} = "${name}_${version}_$config->{architecture}";
410 $self->{targetname
} = "$config->{ostype}-${name}_" .
411 "${version}_$config->{architecture}";
414 if (!$config->{source
}) {
415 if ($suite eq 'etch' || $suite eq 'lenny' || $suite eq 'squeeze') {
416 push @{$config->{source
}}, "http://ftp.debian.org/debian SUITE main contrib";
417 push @{$config->{source
}}, "http://ftp.debian.org/debian SUITE-updates main contrib"
418 if ($suite eq 'squeeze');
419 push @{$config->{source
}}, "http://security.debian.org SUITE/updates main contrib";
420 } elsif ($suite eq 'hardy' || $suite eq 'intrepid' || $suite eq 'jaunty') {
421 my $comp = "main restricted universe multiverse";
422 push @{$config->{source
}}, "http://archive.ubuntu.com/ubuntu SUITE $comp";
423 push @{$config->{source
}}, "http://archive.ubuntu.com/ubuntu SUITE-updates $comp";
424 push @{$config->{source
}}, "http://archive.ubuntu.com/ubuntu SUITE-security $comp";
430 foreach my $s (@{$config->{source
}}) {
431 if ($s =~ m
@^\s*((http
|ftp
)://\S
+)\s
+(\S
+)((\s
+(\S
+))+)$@) {
432 my ($url, $su, $components) = ($1, $3, $4);
433 $su =~ s/SUITE/$suite/;
434 $components =~ s/^\s+//;
435 $components =~ s/\s+$//;
437 foreach my $co (split (/\s+/, $components)) {
440 $ca = ['main'] if !$ca;
448 die "syntax error in source spezification '$s'\n";
452 foreach my $m (@{$config->{mirror
}}) {
453 if ($m =~ m
@^\s*((http
|ftp
)://\S
+)\s
*=>\s
*((http
|ftp
)://\S
+)\s
*$@) {
454 my ($ms, $md) = ($1, $3);
456 foreach my $ss (@$sources) {
457 if ($ss->{source
} eq $ms) {
463 die "unusable mirror $ms\n" if !$found;
465 die "syntax error in mirror spezification '$m'\n";
469 $self->{sources
} = $sources;
471 $self->{infodir
} = "info";
473 $self->__allocate_ve ();
475 $self->{cachedir
} = ($config->{cachedir
} || 'cache') . "/$suite";;
477 my $incl = [qw
(less ssh openssh-server logrotate
)];
479 my $excl = [qw
(modutils reiserfsprogs ppp pppconfig pppoe
480 pppoeconf nfs-common mtools ntp
)];
482 # ubuntu has too many dependencies on udev, so
483 # we cannot exclude it (instead we disable udevd)
484 if ($suite eq 'hardy') {
485 push @$excl, qw(kbd);
486 push @$excl, qw(apparmor apparmor-utils ntfs-3g
488 } elsif($suite eq 'intrepid' || $suite eq 'jaunty') {
489 push @$excl, qw(apparmor apparmor-utils libapparmor1 libapparmor-perl
490 libntfs-3g28 ntfs-3g friendly-recovery);
492 push @$excl, qw(udev module-init-tools pciutils hdparm
496 $self->{incl
} = $incl;
497 $self->{excl
} = $excl;
505 my $infodir = $self->{infodir
};
506 my $arch = $self->{config
}->{architecture
};
512 my $logfd = $self->{logfd
} = IO
::File-
>new (">$self->{logfile}") ||
513 die "unable to open log file";
515 foreach my $ss (@{$self->{sources
}}) {
516 my $src = $ss->{mirror
} || $ss->{source
};
517 my $path = "dists/$ss->{suite}/Release";
518 my $url = "$src/$path";
519 my $target = __url_to_filename
("$ss->{source}/$path");
521 $self->download ($url, "$infodir/$target");
522 $self->download ("$url.gpg", "$infodir/$target.gpg");
523 # fixme: impl. verify (needs --keyring option)
527 warn "Release info ignored\n";
529 foreach my $comp (@{$ss->{comp
}}) {
530 $path = "dists/$ss->{suite}/$comp/binary-$arch/Packages.gz";
531 $target = "$infodir/" . __url_to_filename
("$ss->{source}/$path");
532 my $pkgsrc = "$src/$path";
533 $self->download ($pkgsrc, $target);
534 $self->run_command ("gzip -d '$target'");
540 my ($self, $filename, $size) = @_;
542 my $config = $self->{config
};
546 $data .= "Name: $config->{name}\n";
547 $data .= "Version: $config->{version}\n";
548 $data .= "Type: openvz\n";
549 $data .= "OS: $config->{ostype}\n";
550 $data .= "Section: $config->{section}\n";
551 $data .= "Maintainer: $config->{maintainer}\n";
552 $data .= "Architecture: $config->{architecture}\n";
553 $data .= "Installed-Size: $size\n";
556 $data .= "Infopage: $config->{infopage}\n" if $config->{infopage
};
557 $data .= "ManageUrl: $config->{manageurl}\n" if $config->{manageurl
};
558 $data .= "Certified: $config->{certified}\n" if $config->{certified
};
561 $data .= "Description: $config->{headline}\n";
562 $data .= "$config->{description}\n" if $config->{description
};
564 write_file
($data, $filename, 0644);
568 my ($self, $opts) = @_;
570 my $suite = $self->{config
}->{suite
};
571 my $infodir = $self->{infodir
};
572 my $arch = $self->{config
}->{architecture
};
574 my $instpkgs = $self->read_installed ();
575 my $pkginfo = $self->pkginfo();
576 my $veid = $self->{veid
};
577 my $rootdir = $self->vz_root_dir();
579 my $vestat = $self->ve_status();
580 die "ve not running - unable to finalize\n" if !$vestat->{running
};
583 if (-f
"$rootdir/etc/init.d/mysql") {
584 $self->ve_command ("/etc/init.d/mysql stop");
587 if (!($opts->{keepmycnf
} || (-f
"$rootdir/etc/init.d/mysql_randompw"))) {
588 unlink "$rootdir/root/.my.cnf";
591 if ($suite eq 'etch') {
592 # enable apache2 startup
593 if ($instpkgs->{apache2
}) {
594 write_file
("NO_START=0\n", "$rootdir/etc/default/apache2");
596 unlink "$rootdir/etc/default/apache2";
599 $self->logmsg ("cleanup package status\n");
600 # prevent auto selection of all standard, required or important
601 # packages which are not installed
602 foreach my $pkg (keys %$pkginfo) {
603 my $pri = $pkginfo->{$pkg}->{priority
};
604 if ($pri && ($pri eq 'required' || $pri eq 'important'
605 || $pri eq 'standard')) {
606 if (!$instpkgs->{$pkg}) {
607 $self->ve_dpkg_set_selection ($pkg, 'purge');
612 $self->ve_command ("apt-get clean");
614 $self->logmsg ("update available package list\n");
616 $self->ve_command ("dpkg --clear-avail");
617 foreach my $ss (@{$self->{sources
}}) {
618 my $relsrc = __url_to_filename
("$ss->{source}/dists/$ss->{suite}/Release");
619 if (-f
"$infodir/$relsrc" && -f
"$infodir/$relsrc.gpg") {
620 $self->run_command ("cp '$infodir/$relsrc' '$rootdir/var/lib/apt/lists/$relsrc'");
621 $self->run_command ("cp '$infodir/$relsrc.gpg' '$rootdir/var/lib/apt/lists/$relsrc.gpg'");
623 foreach my $comp (@{$ss->{comp
}}) {
624 my $src = __url_to_filename
("$ss->{source}/dists/$ss->{suite}/" .
625 "$comp/binary-$arch/Packages");
626 my $target = "/var/lib/apt/lists/$src";
627 $self->run_command ("cp '$infodir/$src' '$rootdir/$target'");
628 $self->ve_command ("dpkg --merge-avail '$target'");
632 # set dselect default method
633 write_file
("apt apt\n", "$rootdir/var/lib/dpkg/cmethopt");
635 $self->ve_divert_remove ("/usr/sbin/policy-rc.d");
637 $self->ve_divert_remove ("/sbin/start-stop-daemon");
639 $self->ve_divert_remove ("/sbin/init");
641 # finally stop the VE
642 $self->run_command ("vzctl stop $veid --fast");
643 $rootdir = $self->vz_priv_dir();
645 unlink "$rootdir/sbin/defenv";
647 unlink <$rootdir/root/dead.letter
*>;
649 unlink "$rootdir/var/log/init.log";
651 unlink "$rootdir/aquota.group";
653 unlink "$rootdir/aquota.user";
655 write_file
("", "$rootdir/var/log/syslog");
657 $self->logmsg ("detecting final size: ");
659 my $sizestr = $self->run_command ("du -sm $rootdir", undef, 1);
661 if ($sizestr =~ m/^(\d+)\s+\Q$rootdir\E$/) {
664 die "unable to detect size\n";
666 $self->logmsg ("$size MB\n");
668 $self->write_config ("$rootdir/etc/appliance.info", $size);
670 $self->logmsg ("creating final appliance archive\n");
672 my $target = "$self->{targetname}.tar";
676 $self->run_command ("tar cpf $target --numeric-owner -C '$rootdir' ./etc/appliance.info");
677 $self->run_command ("tar rpf $target --numeric-owner -C '$rootdir' --exclude ./etc/appliance.info .");
678 $self->run_command ("gzip $target");
684 my $rootdir = $self->vz_priv_dir();
686 my $pkgfilelist = "$rootdir/var/lib/dpkg/status";
688 open (PKGLST
, "<$pkgfilelist") ||
689 die "unable to open '$pkgfilelist'";
693 while (my $rec = <PKGLST
>) {
699 while ($rec =~ s/^([^:]+):\s+(.*)\s*\n//) {
703 my $pkg = $res->{'package'};
704 if (my $status = $res->{status
}) {
705 my @sa = split (/\s+/, $status);
707 if ($stat && ($stat ne 'purge')) {
708 $pkglist->{$pkg} = $res;
721 my $veid = $self->{veid
};
723 return "/var/lib/vz/root/$veid";
729 my $veid = $self->{veid
};
731 return "/var/lib/vz/private/$veid";
737 my $veid = $self->{veid
};
739 my $res = $self->run_command ("vzctl status $veid", undef, 1);
742 if ($res =~ m/^CTID\s+$veid\s+(exist|deleted)\s+(mounted|unmounted)\s+(running|down)$/) {
744 exist
=> $1 eq 'exist',
745 mounted
=> $2 eq 'mounted',
746 running
=> $3 eq 'running',
749 die "unable to parse ve status";
754 my ($self, $cmd, $input) = @_;
756 my $veid = $self->{veid
};
758 if (ref ($cmd) eq 'ARRAY') {
759 unshift @$cmd, 'vzctl', 'exec2', $veid, 'defenv';
760 $self->run_command ($cmd, $input);
762 $self->run_command ("vzctl exec2 $veid defenv $cmd", $input);
766 # like ve_command, but pipes stdin correctly
768 my ($self, @cmd) = @_;
770 my $veid = $self->{veid
};
773 my $pid = open2
($reader, "<&STDIN", 'vzctl', 'exec2', $veid,
774 'defenv', @cmd) || die "unable to exec command";
776 while (defined (my $line = <$reader>)) {
777 $self->logmsg ($line);
783 die "ve_exec failed - status $rc\n" if $rc != 0;
787 my ($self, $filename) = @_;
789 $self->ve_command ("dpkg-divert --add --divert '$filename.distrib' " .
790 "--rename '$filename'");
792 sub ve_divert_remove
{
793 my ($self, $filename) = @_;
795 my $rootdir = $self->vz_root_dir();
797 unlink "$rootdir/$filename";
798 $self->ve_command ("dpkg-divert --remove --rename '$filename'");
801 sub ve_debconfig_set
{
802 my ($self, $dcdata) = @_;
804 my $rootdir = $self->vz_root_dir();
805 my $cfgfile = "/tmp/debconf.txt";
806 write_file
($dcdata, "$rootdir/$cfgfile");
807 $self->ve_command ("debconf-set-selections $cfgfile");
808 unlink "$rootdir/$cfgfile";
811 sub ve_dpkg_set_selection
{
812 my ($self, $pkg, $status) = @_;
814 $self->ve_command ("dpkg --set-selections", "$pkg $status");
818 my ($self, $cmd, @pkglist) = @_;
820 return if !scalar (@pkglist);
822 my $pkginfo = $self->pkginfo();
824 my $rootdir = $self->vz_root_dir();
825 my $cachedir = $self->{cachedir
};
829 foreach my $pkg (@pkglist) {
830 my $filename = $self->getpkgfile ($pkg);
831 $self->run_command ("cp '$cachedir/$filename' '$rootdir/$filename'");
832 push @files, "/$filename";
833 $self->logmsg ("$cmd: $pkg\n");
836 my $fl = join (' ', @files);
838 if ($cmd eq 'install') {
839 $self->ve_command ("dpkg --force-depends --force-confold --install $fl");
840 } elsif ($cmd eq 'unpack') {
841 $self->ve_command ("dpkg --force-depends --unpack $fl");
843 die "internal error";
846 foreach my $fn (@files) { unlink "$rootdir$fn"; }
852 my $veid = $self->{veid
}; # fixme
854 my $vestat = $self->ve_status();
855 if ($vestat->{running
}) {
856 $self->run_command ("vzctl stop $veid --fast");
857 } elsif ($vestat->{mounted
}) {
858 $self->run_command ("vzctl umount $veid");
860 if ($vestat->{exist
}) {
861 $self->run_command ("vzctl destroy $veid");
863 unlink $self->{veconffile
};
870 my $root = $self->vz_root_dir();
871 my $priv = $self->vz_priv_dir();
873 my $veid = $self->{veid
}; # fixme
875 $self->logmsg ("initialize VE $veid\n");
878 my $vestat = $self->ve_status();
879 if ($vestat->{running
}) {
880 $self->run_command ("vzctl stop $veid --fast");
881 } elsif ($vestat->{mounted
}) {
882 $self->run_command ("vzctl umount $veid");
894 $self->run_command ("vzctl mount $veid");
897 sub __deb_version_cmp
{
898 my ($cur, $op, $new) = @_;
900 if (system("dpkg", "--compare-versions", $cur, $op, $new) == 0) {
907 sub __parse_packages
{
908 my ($pkginfo, $filename, $src) = @_;
911 open (PKGLST
, "<$filename") ||
912 die "unable to open '$filename'";
914 while (my $rec = <PKGLST
>) {
921 while ($rec =~ s/^([^:]+):\s+(.*)\s*\n//) {
925 my $pkg = $res->{'package'};
926 if ($pkg && $res->{'filename'}) {
928 if (my $info = $pkginfo->{$pkg}) {
929 $cur = $info->{version
};
931 my $new = $res->{version
};
932 if (!$cur || __deb_version_cmp
($cur, 'lt', $new)) {
934 $res->{url
} = "$src/$res->{'filename'}";
936 die "no url for package '$pkg'" if !$res->{url
};
938 $pkginfo->{$pkg} = $res;
949 return $self->{pkginfo
} if $self->{pkginfo
};
951 my $infodir = $self->{infodir
};
952 my $arch = $self->{config
}->{architecture
};
954 my $availfn = "$infodir/available";
959 # reading 'available' is faster, because it only contains latest version
960 # (no need to do slow version compares)
962 __parse_packages
($pkginfo, $availfn);
963 $self->{pkginfo
} = $pkginfo;
967 $self->logmsg ("generating available package list\n");
969 foreach my $ss (@{$self->{sources
}}) {
970 foreach my $comp (@{$ss->{comp
}}) {
971 my $url = "$ss->{source}/dists/$ss->{suite}/$comp/binary-$arch/Packages";
972 my $pkgfilelist = "$infodir/" . __url_to_filename
($url);
974 my $src = $ss->{mirror
} || $ss->{source
};
976 __parse_packages
($pkginfo, $pkgfilelist, $src);
980 if (my $dep = $self->{config
}->{depends
}) {
981 foreach my $d (split (/,/, $dep)) {
982 if ($d =~ m/^\s*(\S+)\s*(\((\S+)\s+(\S+)\)\s*)?$/) {
983 my ($pkg, $op, $rver) = ($1, $3, $4);
984 $self->logmsg ("checking dependencies: $d\n");
985 my $info = $pkginfo->{$pkg};
986 die "package '$pkg' not available\n" if !$info;
988 my $cver = $info->{version
};
989 if (!__deb_version_cmp
($cver, $op, $rver)) {
990 die "detected wrong version '$cver'\n";
994 die "syntax error in depends field";
999 $self->{pkginfo
} = $pkginfo;
1001 my $tmpfn = "$availfn.tmp$$";
1002 my $fd = IO
::File-
>new (">$tmpfn");
1003 foreach my $pkg (sort keys %$pkginfo) {
1004 my $info = $pkginfo->{$pkg};
1005 print $fd "package: $pkg\n";
1006 foreach my $k (sort keys %$info) {
1007 next if $k eq 'description';
1008 next if $k eq 'package';
1009 my $v = $info->{$k};
1010 print $fd "$k: $v\n" if $v;
1012 print $fd "description: $info->{description}\n" if $info->{description
};
1017 rename ($tmpfn, $availfn);
1022 sub __record_provides
{
1023 my ($pkginfo, $closure, $list, $skipself) = @_;
1025 foreach my $pname (@$list) {
1026 my $info = $pkginfo->{$pname};
1027 # fixme: if someone install packages directly using dpkg, there
1028 # is no entry in 'available', only in 'status'. In that case, we
1029 # should extract info from $instpkgs
1031 warn "hint: ignoring provides for '$pname' - package not in 'available' list.\n";
1034 if (my $prov = $info->{provides
}) {
1035 my @pl = split (',', $prov);
1036 foreach my $p (@pl) {
1038 if (!($skipself && (grep { $1 eq $_ } @$list))) {
1043 $closure->{$pname} = 1 if !$skipself;
1048 my ($self, $closure, $list) = @_;
1050 my $pkginfo = $self->pkginfo();
1052 # first, record provided packages
1053 __record_provides
($pkginfo, $closure, $list, 1);
1057 # then resolve dependencies
1058 foreach my $pname (@$list) {
1059 __closure_single
($pkginfo, $closure, $pkgs, $pname, $self->{excl
});
1062 return [ keys %$pkgs ];
1065 sub __closure_single
{
1066 my ($pkginfo, $closure, $pkgs, $pname, $excl) = @_;
1071 return if $closure->{$pname};
1073 my $info = $pkginfo->{$pname} || die "no such package '$pname'";
1075 my $dep = $info->{depends
};
1076 my $predep = $info->{'pre-depends'};
1078 my $size = $info->{size
};
1079 my $url = $info->{url
};
1081 $url || die "$pname: no url for package '$pname'";
1083 $pkgs->{$pname} = 1;
1085 __record_provides
($pkginfo, $closure, [$pname]) if $info->{provides
};
1087 $closure->{$pname} = 1;
1093 push @l, split (/,/, $predep) if $predep;
1094 push @l, split (/,/, $dep) if $dep;
1096 DEPEND
: foreach my $p (@l) {
1097 my @l1 = split (/\|/, $p);
1098 foreach my $p1 (@l1) {
1099 if ($p1 =~ m/^\s*(\S+).*/) {
1100 #printf (STDERR "$pname: $p --> $1\n");
1101 if ($closure->{$1}) {
1102 next DEPEND
; # dependency already met
1106 # search for non-excluded alternative
1108 foreach my $p1 (@l1) {
1109 if ($p1 =~ m/^\s*(\S+).*/) {
1110 next if grep { $1 eq $_ } @$excl;
1115 die "package '$pname' depends on exclusion '$p'\n" if !$found;
1117 #printf (STDERR "$pname: $p --> $found\n");
1119 __closure_single
($pkginfo, $closure, $pkgs, $found, $excl);
1123 sub cache_packages
{
1124 my ($self, $pkglist) = @_;
1126 foreach my $pkg (@$pkglist) {
1127 $self->getpkgfile ($pkg);
1132 my ($self, $pkg) = @_;
1134 my $pkginfo = $self->pkginfo();
1135 my $info = $pkginfo->{$pkg} || die "no such package '$pkg'";
1136 my $cachedir = $self->{cachedir
};
1138 my $url = $info->{url
};
1141 if ($url =~ m
|/([^/]+.deb
)$|) {
1144 die "internal error";
1147 return $filename if -f
"$cachedir/$filename";
1151 $self->download ($url, "$cachedir/$filename");
1156 sub install_init_script
{
1157 my ($self, $script, $runlevel, $prio) = @_;
1159 my $suite = $self->{config
}->{suite
};
1160 my $rootdir = $self->vz_root_dir();
1162 my $base = basename
($script);
1163 my $target = "$rootdir/etc/init.d/$base";
1165 $self->run_command ("install -m 0755 '$script' '$target'");
1166 if ($suite eq 'etch' || $suite eq 'lenny') {
1167 $self->ve_command ("update-rc.d $base start $prio $runlevel .");
1169 $self->ve_command ("insserv $base");
1176 my ($self, $opts) = @_;
1178 my $pkginfo = $self->pkginfo();
1179 my $veid = $self->{veid
};
1180 my $suite = $self->{config
}->{suite
};
1182 my $important = [ @{$self->{incl
}} ];
1186 my $mta = $opts->{exim
} ?
'exim' : 'postfix';
1188 if ($mta eq 'postfix') {
1189 push @$important, "postfix";
1192 foreach my $p (keys %$pkginfo) {
1193 next if grep { $p eq $_ } @{$self->{excl
}};
1194 my $pri = $pkginfo->{$p}->{priority
};
1196 next if $mta ne 'exim' && $p =~ m/exim/;
1197 next if $p =~ m/(selinux|semanage|policycoreutils)/;
1199 push @$required, $p if $pri eq 'required';
1200 push @$important, $p if $pri eq 'important';
1201 push @$standard, $p if $pri eq 'standard' && !$opts->{minimal
};
1205 $required = $self->closure ($closure, $required);
1206 $important = $self->closure ($closure, $important);
1208 if (!$opts->{minimal
}) {
1209 push @$standard, 'xbase-clients';
1210 $standard = $self->closure ($closure, $standard);
1213 # test if we have all 'ubuntu-minimal' and 'ubuntu-standard' packages
1214 # except those explicitly excluded
1215 if ($suite eq 'hardy' || $suite eq 'intrepid' || $suite eq 'jaunty') {
1216 my $mdeps = $pkginfo->{'ubuntu-minimal'}->{depends
};
1217 foreach my $d (split (/,/, $mdeps)) {
1218 if ($d =~ m/^\s*(\S+)$/) {
1220 next if $closure->{$pkg};
1221 next if grep { $pkg eq $_ } @{$self->{excl
}};
1222 die "missing ubuntu-minimal package '$pkg'\n";
1225 if (!$opts->{minimal
}) {
1226 $mdeps = $pkginfo->{'ubuntu-standard'}->{depends
};
1227 foreach my $d (split (/,/, $mdeps)) {
1228 if ($d =~ m/^\s*(\S+)$/) {
1230 next if $closure->{$pkg};
1231 next if grep { $pkg eq $_ } @{$self->{excl
}};
1232 die "missing ubuntu-standard package '$pkg'\n";
1238 # download/cache all files first
1239 $self->cache_packages ($required);
1240 $self->cache_packages ($important);
1241 $self->cache_packages ($standard);
1243 my $rootdir = $self->vz_priv_dir();
1245 # extract required packages first
1246 $self->logmsg ("create basic environment\n");
1247 foreach my $p (@$required) {
1248 my $filename = $self->getpkgfile ($p);
1249 $self->run_command ("ar -p '$self->{cachedir}/$filename' data.tar.gz | zcat | tar -C '$rootdir' -xf -");
1253 my $data = "Package: dpkg\n" .
1254 "Version: $pkginfo->{dpkg}->{version}\n" .
1255 "Status: install ok installed\n";
1257 write_file
($data, "$rootdir/var/lib/dpkg/status");
1258 write_file
("", "$rootdir/var/lib/dpkg/info/dpkg.list");
1259 write_file
("", "$rootdir/var/lib/dpkg/available");
1262 foreach my $ss (@{$self->{sources
}}) {
1263 my $url = $ss->{source
};
1264 my $comp = join (' ', @{$ss->{comp
}});
1265 $data .= "deb $url $ss->{suite} $comp\n\n";
1268 write_file
($data, "$rootdir/etc/apt/sources.list");
1270 $data = "# UNCONFIGURED FSTAB FOR BASE SYSTEM\n";
1271 write_file
($data, "$rootdir/etc/fstab", 0644);
1273 write_file
("localhost\n", "$rootdir/etc/hostname", 0644);
1275 # avoid warnings about non-existent resolv.conf
1276 write_file
("", "$rootdir/etc/resolv.conf", 0644);
1278 $data = "auto lo\niface lo inet loopback\n";
1279 write_file
($data, "$rootdir/etc/network/interfaces", 0644);
1282 $self->run_command ("tar xzf '$devicetar' -C '$rootdir'");
1284 # avoid warnings about missing default locale
1285 write_file
("LANG=\"C\"\n", "$rootdir/etc/default/locale", 0644);
1288 rename ("$rootdir/sbin/init", "$rootdir/sbin/init.org");
1289 $self->run_command ("cp '$fake_init' '$rootdir/sbin/init'");
1291 $self->run_command ("cp '$default_env' '$rootdir/sbin/defenv'");
1293 $self->run_command ("vzctl start $veid");
1294 $rootdir = $self->vz_root_dir();
1296 $self->logmsg ("initialize ld cache\n");
1297 $self->ve_command ("/sbin/ldconfig");
1298 $self->run_command ("ln -sf mawk '$rootdir/usr/bin/awk'");
1300 $self->logmsg ("installing packages\n");
1302 $self->ve_dpkg ('install', 'base-files', 'base-passwd');
1304 $self->ve_dpkg ('install', 'dpkg');
1306 $self->run_command ("ln -sf /usr/share/zoneinfo/UTC '$rootdir/etc/localtime'");
1308 $self->ve_dpkg ('install', 'libc6');
1309 $self->ve_dpkg ('install', 'perl-base');
1311 unlink "$rootdir/usr/bin/awk";
1313 $self->ve_dpkg ('install', 'mawk');
1314 $self->ve_dpkg ('install', 'debconf');
1316 # unpack required packages
1317 foreach my $p (@$required) {
1318 $self->ve_dpkg ('unpack', $p);
1321 rename ("$rootdir/sbin/init.org", "$rootdir/sbin/init");
1322 $self->ve_divert_add ("/sbin/init");
1323 $self->run_command ("cp '$fake_init' '$rootdir/sbin/init'");
1325 # disable service activation
1326 $self->ve_divert_add ("/usr/sbin/policy-rc.d");
1327 $data = "#!/bin/sh\nexit 101\n";
1328 write_file
($data, "$rootdir/usr/sbin/policy-rc.d", 755);
1330 # disable start-stop-daemon
1331 $self->ve_divert_add ("/sbin/start-stop-daemon");
1335 echo \"Warning: Fake start-stop-daemon called, doing nothing\"
1337 write_file
($data, "$rootdir/sbin/start-stop-daemon", 0755);
1340 $self->ve_divert_add ("/sbin/udevd");
1342 if ($suite eq 'etch') {
1343 # disable apache2 startup
1344 write_file
("NO_START=1\n", "$rootdir/etc/default/apache2");
1347 $self->logmsg ("configure required packages\n");
1348 $self->ve_command ("dpkg --force-confold --skip-same-version --configure -a");
1350 # set postfix defaults
1351 if ($mta eq 'postfix') {
1352 $data = "postfix postfix/main_mailer_type select Local only\n";
1353 $self->ve_debconfig_set ($data);
1355 $data = "postmaster: root\nwebmaster: root\n";
1356 write_file
($data, "$rootdir/etc/aliases");
1359 if ($suite eq 'jaunty') {
1360 # jaunty does not create /var/run/network, so network startup fails.
1361 # so we do not use tmpfs for /var/run and /var/lock
1362 $self->run_command ("sed -e 's/RAMRUN=yes/RAMRUN=no/' -e 's/RAMLOCK=yes/RAMLOCK=no/' -i $rootdir/etc/default/rcS");
1363 # and create the directory here
1364 $self->run_command ("mkdir $rootdir/var/run/network");
1367 # unpack base packages
1368 foreach my $p (@$important) {
1369 $self->ve_dpkg ('unpack', $p);
1373 $self->ve_command ("ifconfig lo up");
1375 $self->logmsg ("configure important packages\n");
1376 $self->ve_command ("dpkg --force-confold --skip-same-version --configure -a");
1378 if (-d
"$rootdir/etc/event.d") {
1379 unlink <$rootdir/etc/event.d
/tty
*>;
1382 if (-f
"$rootdir/etc/inittab") {
1383 $self->run_command ("sed -i -e '/getty/d' '$rootdir/etc/inittab'");
1386 # Link /etc/mtab to /proc/mounts, so df and friends will work:
1387 unlink "$rootdir/etc/mtab";
1388 $self->ve_command ("ln -s /proc/mounts /etc/mtab");
1391 $self->ve_command ("usermod -L root");
1393 # regenerate sshd host keys
1394 $self->install_init_script ($script_ssh_init, 2, 14);
1396 if ($mta eq 'postfix') {
1397 $data = "postfix postfix/main_mailer_type select No configuration\n";
1398 $self->ve_debconfig_set ($data);
1400 unlink "$rootdir/etc/mailname";
1401 write_file
($postfix_main_cf, "$rootdir/etc/postfix/main.cf");
1404 if (!$opts->{minimal
}) {
1405 # unpack standard packages
1406 foreach my $p (@$standard) {
1407 $self->ve_dpkg ('unpack', $p);
1410 $self->logmsg ("configure standard packages\n");
1411 $self->ve_command ("dpkg --force-confold --skip-same-version --configure -a");
1414 # disable HWCLOCK access
1415 $self->run_command ("echo 'HWCLOCKACCESS=no' >> '$rootdir/etc/default/rcS'");
1418 $self->ve_divert_add ("/usr/sbin/hald");
1420 # disable /dev/urandom init
1421 $self->run_command ("install -m 0755 '$script_init_urandom' '$rootdir/etc/init.d/urandom'");
1423 if ($suite eq 'etch' || $suite eq 'hardy' || $suite eq 'intrepid' || $suite eq 'jaunty') {
1425 $self->ve_divert_add ("/sbin/klogd");
1428 # remove unnecessays sysctl entries to avoid warnings
1430 $cmd .= ' -e \'s/^\(kernel\.printk.*\)/#\1/\'';
1431 $cmd .= ' -e \'s/^\(kernel\.maps_protect.*\)/#\1/\'';
1432 $cmd .= ' -e \'s/^\(fs\.inotify\.max_user_watches.*\)/#\1/\'';
1433 $cmd .= ' -e \'s/^\(vm\.mmap_min_addr.*\)/#\1/\'';
1434 $cmd .= " -i '$rootdir/etc/sysctl.conf'";
1435 $self->run_command ($cmd);
1437 my $bindv6only = "$rootdir/etc/sysctl.d/bindv6only.conf";
1438 if (-f
$bindv6only) {
1440 $cmd .= ' -e \'s/^\(net\.ipv6\.bindv6only.*\)/#\1/\'';
1441 $cmd .= " -i '$bindv6only'";
1442 $self->run_command ($cmd);
1445 if ($suite eq 'hardy' || $suite eq 'intrepid' || $suite eq 'jaunty') {
1446 # disable tty init (console-setup)
1448 $cmd .= ' -e \'s/^\(ACTIVE_CONSOLES=.*\)/ACTIVE_CONSOLES=/\'';
1449 $cmd .= " -i '$rootdir/etc/default/console-setup'";
1450 $self->run_command ($cmd);
1453 if ($suite eq 'intrepid') {
1454 # remove sysctl setup (avoid warnings at startup)
1455 my $filelist = "$rootdir/etc/sysctl.d/10-console-messages.conf";
1456 $filelist .= " $rootdir/etc/sysctl.d/10-process-security.conf";
1457 $filelist .= " $rootdir/etc/sysctl.d/10-network-security.conf";
1458 $self->run_command ("rm $filelist");
1460 if ($suite eq 'jaunty') {
1461 # remove sysctl setup (avoid warnings at startup)
1462 my $filelist = "$rootdir/etc/sysctl.d/10-console-messages.conf";
1463 $filelist .= " $rootdir/etc/sysctl.d/10-network-security.conf";
1464 $self->run_command ("rm $filelist");
1471 my $veid = $self->{veid
};
1473 my $vestat = $self->ve_status();
1475 if (!$vestat->{exist
}) {
1476 $self->logmsg ("Please create the appliance first (bootstrap)");
1480 if (!$vestat->{running
}) {
1481 $self->run_command ("vzctl start $veid");
1484 system ("vzctl enter $veid");
1487 sub ve_mysql_command
{
1488 my ($self, $sql, $password) = @_;
1490 #my $bootstrap = "/usr/sbin/mysqld --bootstrap --user=mysql --skip-grant-tables " .
1491 #"--skip-bdb --skip-innodb --skip-ndbcluster";
1493 $self->ve_command ("mysql", $sql);
1496 sub ve_mysql_bootstrap
{
1497 my ($self, $sql, $password) = @_;
1501 my $suite = $self->{config
}->{suite
};
1503 if ($suite eq 'squeeze') {
1504 $cmd = "/usr/sbin/mysqld --bootstrap --user=mysql --skip-grant-tables";
1507 $cmd = "/usr/sbin/mysqld --bootstrap --user=mysql --skip-grant-tables " .
1508 "--skip-bdb --skip-innodb --skip-ndbcluster";
1511 $self->ve_command ($cmd, $sql);
1514 sub compute_required
{
1515 my ($self, $pkglist) = @_;
1517 my $pkginfo = $self->pkginfo();
1518 my $instpkgs = $self->read_installed ();
1521 __record_provides
($pkginfo, $closure, [keys %$instpkgs]);
1523 return $self->closure ($closure, $pkglist);
1527 my ($self, $opts) = @_;
1529 my @supp = ('7.4', '8.1');
1530 my $pgversion = '8.1';
1532 my $suite = $self->{config
}->{suite
};
1534 if ($suite eq 'lenny' || $suite eq 'hardy' || $suite eq 'intrepid' || $suite eq 'jaunty') {
1537 } elsif ($suite eq 'squeeze') {
1542 $pgversion = $opts->{version
} if $opts->{version
};
1544 die "unsupported postgres version '$pgversion'\n"
1545 if !grep { $pgversion eq $_; } @supp;
1547 my $rootdir = $self->vz_root_dir();
1549 my $required = $self->compute_required (["postgresql-$pgversion"]);
1551 $self->cache_packages ($required);
1553 $self->ve_dpkg ('install', @$required);
1555 my $iscript = $suite eq 'squeeze' ?
'postgresql' : "postgresql-$pgversion";
1557 $self->ve_command ("/etc/init.d/$iscript start") if $opts->{start
};
1561 my ($self, $opts) = @_;
1563 my $password = $opts->{password
};
1564 my $rootdir = $self->vz_root_dir();
1566 my $suite = $self->{config
}->{suite
};
1568 my $ver = $suite eq 'squeeze' ?
'5.1' : '5.0';
1570 my $required = $self->compute_required (['mysql-common', "mysql-server-$ver"]);
1572 $self->cache_packages ($required);
1574 $self->ve_dpkg ('install', @$required);
1576 # fix security (see /usr/bin/mysql_secure_installation)
1577 my $sql = "DELETE FROM mysql.user WHERE User='';\n" .
1578 "DELETE FROM mysql.user WHERE User='root' AND Host!='localhost';\n" .
1579 "FLUSH PRIVILEGES;\n";
1580 $self->ve_mysql_bootstrap ($sql);
1584 my $rpw = $password eq 'random' ?
'admin' : $password;
1586 my $sql = "USE mysql;\n" .
1587 "UPDATE user SET password=PASSWORD(\"$rpw\") WHERE user
='root';\n" .
1588 "FLUSH PRIVILEGES
;\n";
1589 $self->ve_mysql_bootstrap ($sql);
1591 write_file ("[client
]\nuser
=root
\npassword
=\"$rpw\"\n", "$rootdir/root/.my.cnf", 0600);
1592 if ($password eq 'random') {
1593 $self->install_init_script ($script_mysql_randompw, 2, 20);
1597 $self->ve_command ("/etc/init.d/mysql start") if $opts->{start
};
1601 my ($self, $opts) = @_;
1603 my $memlimit = $opts->{memlimit
};
1604 my $rootdir = $self->vz_root_dir();
1606 my $required = $self->compute_required ([qw
(php5 php5-cli libapache2-mod-php5 php5-gd
)]);
1608 $self->cache_packages ($required);
1610 $self->ve_dpkg ('install', @$required);
1613 $self->run_command ("sed -e 's/^\\s*memory_limit\\s*=.*;/memory_limit = ${memlimit}M;/' -i $rootdir/etc/php5/apache2/php.ini");
1618 my ($self, $pkglist, $unpack) = @_;
1620 my $required = $self->compute_required ($pkglist);
1622 $self->cache_packages ($required);
1624 $self->ve_dpkg ($unpack ?
'unpack' : 'install', @$required);
1628 my ($self, $distclean) = @_;
1630 unlink $self->{logfile
};
1631 unlink "$self->{targetname}.tar";
1632 unlink "$self->{targetname}.tar.gz";
1634 $self->ve_destroy ();
1637 rmtree
$self->{cachedir
} if $distclean && !$self->{config
}->{cachedir
};
1639 rmtree
$self->{infodir
};