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/pve/openvz";
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 'jessie') {
381 $config->{ostype
} = "debian-8.0";
382 } elsif ($suite eq 'wheezy') {
383 $config->{ostype
} = "debian-7.0";
384 } elsif ($suite eq 'squeeze') {
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";
397 die "unsupported debian suite '$suite'\n";
400 my $name = $config->{name
} || die "no 'name' specified\n";
402 $name =~ m/^[a-z][0-9a-z\-\*\.]+$/ ||
403 die "illegal characters in name '$name'\n";
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
};
411 if ($name =~ m/^$config->{ostype}/) {
412 $self->{targetname
} = "${name}_${version}_$config->{architecture}";
414 $self->{targetname
} = "$config->{ostype}-${name}_" .
415 "${version}_$config->{architecture}";
418 if (!$config->{source
}) {
419 if ($suite eq 'etch' || $suite eq 'lenny' || $suite eq 'squeeze' ||
420 $suite eq 'wheezy' || $suite eq 'jessie' ) {
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"
423 if ($suite eq 'squeeze' || $suite eq 'wheezy' || $suite eq 'jessie');
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";
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+$//;
442 foreach my $co (split (/\s+/, $components)) {
445 $ca = ['main'] if !$ca;
453 die "syntax error in source spezification '$s'\n";
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);
461 foreach my $ss (@$sources) {
462 if ($ss->{source
} eq $ms) {
468 die "unusable mirror $ms\n" if !$found;
470 die "syntax error in mirror spezification '$m'\n";
474 $self->{sources
} = $sources;
476 $self->{infodir
} = "info";
478 $self->__allocate_ve ();
480 $self->{cachedir
} = ($config->{cachedir
} || 'cache') . "/$suite";;
482 my $incl = [qw
(less ssh openssh-server logrotate
)];
484 my $excl = [qw
(modutils reiserfsprogs ppp pppconfig pppoe
485 pppoeconf nfs-common mtools ntp
)];
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
493 } elsif ($suite eq 'intrepid' || $suite eq 'jaunty') {
494 push @$excl, qw(apparmor apparmor-utils libapparmor1 libapparmor-perl
495 libntfs-3g28 ntfs-3g friendly-recovery);
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
502 push @$excl, qw(udev module-init-tools pciutils hdparm
506 $self->{incl
} = $incl;
507 $self->{excl
} = $excl;
515 my $infodir = $self->{infodir
};
516 my $arch = $self->{config
}->{architecture
};
522 my $logfd = $self->{logfd
} = IO
::File-
>new (">$self->{logfile}") ||
523 die "unable to open log file";
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");
531 $self->download ($url, "$infodir/$target");
532 $self->download ("$url.gpg", "$infodir/$target.gpg");
533 # fixme: impl. verify (needs --keyring option)
537 warn "Release info ignored\n";
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'");
550 my ($self, $filename, $size) = @_;
552 my $config = $self->{config
};
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";
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
};
571 $data .= "Description: $config->{headline}\n";
572 $data .= "$config->{description}\n" if $config->{description
};
574 write_file
($data, $filename, 0644);
578 my ($self, $opts) = @_;
580 my $suite = $self->{config
}->{suite
};
581 my $infodir = $self->{infodir
};
582 my $arch = $self->{config
}->{architecture
};
584 my $instpkgs = $self->read_installed ();
585 my $pkginfo = $self->pkginfo();
586 my $veid = $self->{veid
};
587 my $rootdir = $self->vz_root_dir();
589 my $vestat = $self->ve_status();
590 die "ve not running - unable to finalize\n" if !$vestat->{running
};
593 if (-f
"$rootdir/etc/init.d/mysql") {
594 $self->ve_command ("/etc/init.d/mysql stop");
597 if (!($opts->{keepmycnf
} || (-f
"$rootdir/etc/init.d/mysql_randompw"))) {
598 unlink "$rootdir/root/.my.cnf";
601 if ($suite eq 'etch') {
602 # enable apache2 startup
603 if ($instpkgs->{apache2
}) {
604 write_file
("NO_START=0\n", "$rootdir/etc/default/apache2");
606 unlink "$rootdir/etc/default/apache2";
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');
622 $self->ve_command ("apt-get clean");
624 $self->logmsg ("update available package list\n");
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'");
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'");
642 # set dselect default method
643 write_file
("apt apt\n", "$rootdir/var/lib/dpkg/cmethopt");
645 $self->ve_divert_remove ("/usr/sbin/policy-rc.d");
647 $self->ve_divert_remove ("/sbin/start-stop-daemon");
649 $self->ve_divert_remove ("/sbin/init");
651 # finally stop the VE
652 $self->run_command ("vzctl stop $veid --fast");
653 $rootdir = $self->vz_priv_dir();
655 unlink "$rootdir/sbin/defenv";
657 unlink <$rootdir/root/dead.letter
*>;
659 unlink "$rootdir/var/log/init.log";
661 unlink "$rootdir/aquota.group";
663 unlink "$rootdir/aquota.user";
665 write_file
("", "$rootdir/var/log/syslog");
667 $self->logmsg ("detecting final size: ");
669 my $sizestr = $self->run_command ("du -sm $rootdir", undef, 1);
671 if ($sizestr =~ m/^(\d+)\s+\Q$rootdir\E$/) {
674 die "unable to detect size\n";
676 $self->logmsg ("$size MB\n");
678 $self->write_config ("$rootdir/etc/appliance.info", $size);
680 $self->logmsg ("creating final appliance archive\n");
682 my $target = "$self->{targetname}.tar";
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");
694 my $rootdir = $self->vz_priv_dir();
696 my $pkgfilelist = "$rootdir/var/lib/dpkg/status";
698 open (PKGLST
, "<$pkgfilelist") ||
699 die "unable to open '$pkgfilelist'";
703 while (my $rec = <PKGLST
>) {
709 while ($rec =~ s/^([^:]+):\s+(.*)\s*\n//) {
713 my $pkg = $res->{'package'};
714 if (my $status = $res->{status
}) {
715 my @sa = split (/\s+/, $status);
717 if ($stat && ($stat ne 'purge')) {
718 $pkglist->{$pkg} = $res;
731 my $veid = $self->{veid
};
733 return "/var/lib/vz/root/$veid";
739 my $veid = $self->{veid
};
741 return "/var/lib/vz/private/$veid";
747 my $veid = $self->{veid
};
749 my $res = $self->run_command ("vzctl status $veid", undef, 1);
752 if ($res =~ m/^CTID\s+$veid\s+(exist|deleted)\s+(mounted|unmounted)\s+(running|down)$/) {
754 exist
=> $1 eq 'exist',
755 mounted
=> $2 eq 'mounted',
756 running
=> $3 eq 'running',
759 die "unable to parse ve status";
764 my ($self, $cmd, $input) = @_;
766 my $veid = $self->{veid
};
768 if (ref ($cmd) eq 'ARRAY') {
769 unshift @$cmd, 'vzctl', 'exec2', $veid, 'defenv';
770 $self->run_command ($cmd, $input);
772 $self->run_command ("vzctl exec2 $veid defenv $cmd", $input);
776 # like ve_command, but pipes stdin correctly
778 my ($self, @cmd) = @_;
780 my $veid = $self->{veid
};
783 my $pid = open2
($reader, "<&STDIN", 'vzctl', 'exec2', $veid,
784 'defenv', @cmd) || die "unable to exec command";
786 while (defined (my $line = <$reader>)) {
787 $self->logmsg ($line);
793 die "ve_exec failed - status $rc\n" if $rc != 0;
797 my ($self, $filename) = @_;
799 $self->ve_command ("dpkg-divert --add --divert '$filename.distrib' " .
800 "--rename '$filename'");
802 sub ve_divert_remove
{
803 my ($self, $filename) = @_;
805 my $rootdir = $self->vz_root_dir();
807 unlink "$rootdir/$filename";
808 $self->ve_command ("dpkg-divert --remove --rename '$filename'");
811 sub ve_debconfig_set
{
812 my ($self, $dcdata) = @_;
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";
821 sub ve_dpkg_set_selection
{
822 my ($self, $pkg, $status) = @_;
824 $self->ve_command ("dpkg --set-selections", "$pkg $status");
828 my ($self, $cmd, @pkglist) = @_;
830 return if !scalar (@pkglist);
832 my $pkginfo = $self->pkginfo();
834 my $rootdir = $self->vz_root_dir();
835 my $cachedir = $self->{cachedir
};
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");
846 my $fl = join (' ', @files);
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");
853 die "internal error";
856 foreach my $fn (@files) { unlink "$rootdir$fn"; }
862 my $veid = $self->{veid
}; # fixme
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");
870 if ($vestat->{exist
}) {
871 $self->run_command ("vzctl destroy $veid");
873 unlink $self->{veconffile
};
880 my $root = $self->vz_root_dir();
881 my $priv = $self->vz_priv_dir();
883 my $veid = $self->{veid
}; # fixme
885 $self->logmsg ("initialize VE $veid\n");
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");
904 $self->run_command ("vzctl mount $veid");
907 sub __deb_version_cmp
{
908 my ($cur, $op, $new) = @_;
910 if (system("dpkg", "--compare-versions", $cur, $op, $new) == 0) {
917 sub __parse_packages
{
918 my ($pkginfo, $filename, $src) = @_;
921 open (PKGLST
, "<$filename") ||
922 die "unable to open '$filename'";
924 while (my $rec = <PKGLST
>) {
931 while ($rec =~ s/^([^:]+):\s+(.*)\s*\n//) {
935 my $pkg = $res->{'package'};
936 if ($pkg && $res->{'filename'}) {
938 if (my $info = $pkginfo->{$pkg}) {
939 $cur = $info->{version
};
941 my $new = $res->{version
};
942 if (!$cur || __deb_version_cmp
($cur, 'lt', $new)) {
944 $res->{url
} = "$src/$res->{'filename'}";
946 die "no url for package '$pkg'" if !$res->{url
};
948 $pkginfo->{$pkg} = $res;
959 return $self->{pkginfo
} if $self->{pkginfo
};
961 my $infodir = $self->{infodir
};
962 my $arch = $self->{config
}->{architecture
};
964 my $availfn = "$infodir/available";
969 # reading 'available' is faster, because it only contains latest version
970 # (no need to do slow version compares)
972 __parse_packages
($pkginfo, $availfn);
973 $self->{pkginfo
} = $pkginfo;
977 $self->logmsg ("generating available package list\n");
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);
984 my $src = $ss->{mirror
} || $ss->{source
};
986 __parse_packages
($pkginfo, $pkgfilelist, $src);
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;
998 my $cver = $info->{version
};
999 if (!__deb_version_cmp
($cver, $op, $rver)) {
1000 die "detected wrong version '$cver'\n";
1004 die "syntax error in depends field";
1009 $self->{pkginfo
} = $pkginfo;
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;
1022 print $fd "description: $info->{description}\n" if $info->{description
};
1027 rename ($tmpfn, $availfn);
1032 sub __record_provides
{
1033 my ($pkginfo, $closure, $list, $skipself) = @_;
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
1041 warn "hint: ignoring provides for '$pname' - package not in 'available' list.\n";
1044 if (my $prov = $info->{provides
}) {
1045 my @pl = split (',', $prov);
1046 foreach my $p (@pl) {
1048 if (!($skipself && (grep { $1 eq $_ } @$list))) {
1053 $closure->{$pname} = 1 if !$skipself;
1058 my ($self, $closure, $list) = @_;
1060 my $pkginfo = $self->pkginfo();
1062 # first, record provided packages
1063 __record_provides
($pkginfo, $closure, $list, 1);
1067 # then resolve dependencies
1068 foreach my $pname (@$list) {
1069 __closure_single
($pkginfo, $closure, $pkgs, $pname, $self->{excl
});
1072 return [ keys %$pkgs ];
1075 sub __closure_single
{
1076 my ($pkginfo, $closure, $pkgs, $pname, $excl) = @_;
1080 $pname =~ s/:any$//;
1082 return if $closure->{$pname};
1084 my $info = $pkginfo->{$pname} || die "no such package '$pname'";
1086 my $dep = $info->{depends
};
1087 my $predep = $info->{'pre-depends'};
1089 my $size = $info->{size
};
1090 my $url = $info->{url
};
1092 $url || die "$pname: no url for package '$pname'";
1094 $pkgs->{$pname} = 1;
1096 __record_provides
($pkginfo, $closure, [$pname]) if $info->{provides
};
1098 $closure->{$pname} = 1;
1104 push @l, split (/,/, $predep) if $predep;
1105 push @l, split (/,/, $dep) if $dep;
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
1117 # search for non-excluded alternative
1119 foreach my $p1 (@l1) {
1120 if ($p1 =~ m/^\s*(\S+).*/) {
1121 next if grep { $1 eq $_ } @$excl;
1126 die "package '$pname' depends on exclusion '$p'\n" if !$found;
1128 #printf (STDERR "$pname: $p --> $found\n");
1130 __closure_single
($pkginfo, $closure, $pkgs, $found, $excl);
1134 sub cache_packages
{
1135 my ($self, $pkglist) = @_;
1137 foreach my $pkg (@$pkglist) {
1138 $self->getpkgfile ($pkg);
1143 my ($self, $pkg) = @_;
1145 my $pkginfo = $self->pkginfo();
1146 my $info = $pkginfo->{$pkg} || die "no such package '$pkg'";
1147 my $cachedir = $self->{cachedir
};
1149 my $url = $info->{url
};
1152 if ($url =~ m
|/([^/]+.deb
)$|) {
1155 die "internal error";
1158 return $filename if -f
"$cachedir/$filename";
1162 $self->download ($url, "$cachedir/$filename");
1167 sub install_init_script
{
1168 my ($self, $script, $runlevel, $prio) = @_;
1170 my $suite = $self->{config
}->{suite
};
1171 my $rootdir = $self->vz_root_dir();
1173 my $base = basename
($script);
1174 my $target = "$rootdir/etc/init.d/$base";
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 .");
1180 $self->ve_command ("insserv $base");
1187 my ($self, $opts) = @_;
1189 my $pkginfo = $self->pkginfo();
1190 my $veid = $self->{veid
};
1191 my $suite = $self->{config
}->{suite
};
1193 my $important = [ @{$self->{incl
}} ];
1197 my $mta = $opts->{exim
} ?
'exim' : 'postfix';
1199 if ($mta eq 'postfix') {
1200 push @$important, "postfix";
1203 foreach my $p (keys %$pkginfo) {
1204 next if grep { $p eq $_ } @{$self->{excl
}};
1205 my $pri = $pkginfo->{$p}->{priority
};
1207 next if $mta ne 'exim' && $p =~ m/exim/;
1208 next if $p =~ m/(selinux|semanage|policycoreutils)/;
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
};
1216 $required = $self->closure ($closure, $required);
1217 $important = $self->closure ($closure, $important);
1219 if (!$opts->{minimal
}) {
1220 push @$standard, 'xbase-clients';
1221 $standard = $self->closure ($closure, $standard);
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+)$/) {
1231 next if $closure->{$pkg};
1232 next if grep { $pkg eq $_ } @{$self->{excl
}};
1233 die "missing ubuntu-minimal package '$pkg'\n";
1236 if (!$opts->{minimal
}) {
1237 $mdeps = $pkginfo->{'ubuntu-standard'}->{depends
};
1238 foreach my $d (split (/,/, $mdeps)) {
1239 if ($d =~ m/^\s*(\S+)$/) {
1241 next if $closure->{$pkg};
1242 next if grep { $pkg eq $_ } @{$self->{excl
}};
1243 die "missing ubuntu-standard package '$pkg'\n";
1249 # download/cache all files first
1250 $self->cache_packages ($required);
1251 $self->cache_packages ($important);
1252 $self->cache_packages ($standard);
1254 my $rootdir = $self->vz_priv_dir();
1256 # extract required packages first
1257 $self->logmsg ("create basic environment\n");
1258 foreach my $p (@$required) {
1259 my $filename = $self->getpkgfile ($p);
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 -");
1264 $self->run_command ("ar -p '$self->{cachedir}/$filename' data.tar.gz | tar -C '$rootdir' -xzf -");
1269 my $data = "Package: dpkg\n" .
1270 "Version: $pkginfo->{dpkg}->{version}\n" .
1271 "Status: install ok installed\n";
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");
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";
1284 write_file
($data, "$rootdir/etc/apt/sources.list");
1286 $data = "# UNCONFIGURED FSTAB FOR BASE SYSTEM\n";
1287 write_file
($data, "$rootdir/etc/fstab", 0644);
1289 write_file
("localhost\n", "$rootdir/etc/hostname", 0644);
1291 # avoid warnings about non-existent resolv.conf
1292 write_file
("", "$rootdir/etc/resolv.conf", 0644);
1294 $data = "auto lo\niface lo inet loopback\n";
1295 write_file
($data, "$rootdir/etc/network/interfaces", 0644);
1298 $self->run_command ("tar xzf '$devicetar' -C '$rootdir'");
1300 # avoid warnings about missing default locale
1301 write_file
("LANG=\"C\"\n", "$rootdir/etc/default/locale", 0644);
1304 rename ("$rootdir/sbin/init", "$rootdir/sbin/init.org");
1305 $self->run_command ("cp '$fake_init' '$rootdir/sbin/init'");
1307 $self->run_command ("cp '$default_env' '$rootdir/sbin/defenv'");
1309 $self->run_command ("vzctl start $veid");
1310 $rootdir = $self->vz_root_dir();
1312 $self->logmsg ("initialize ld cache\n");
1313 $self->ve_command ("/sbin/ldconfig");
1314 $self->run_command ("ln -sf mawk '$rootdir/usr/bin/awk'");
1316 $self->logmsg ("installing packages\n");
1318 $self->ve_dpkg ('install', 'base-files', 'base-passwd');
1320 $self->ve_dpkg ('install', 'dpkg');
1322 $self->run_command ("ln -sf /usr/share/zoneinfo/UTC '$rootdir/etc/localtime'");
1324 $self->run_command ("ln -sf bash '$rootdir/bin/sh'");
1326 $self->ve_dpkg ('install', 'libc6');
1327 $self->ve_dpkg ('install', 'perl-base');
1329 unlink "$rootdir/usr/bin/awk";
1331 $self->ve_dpkg ('install', 'mawk');
1332 $self->ve_dpkg ('install', 'debconf');
1334 # unpack required packages
1335 foreach my $p (@$required) {
1336 $self->ve_dpkg ('unpack', $p);
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'");
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);
1348 # disable start-stop-daemon
1349 $self->ve_divert_add ("/sbin/start-stop-daemon");
1353 echo \"Warning: Fake start-stop-daemon called, doing nothing\"
1355 write_file
($data, "$rootdir/sbin/start-stop-daemon", 0755);
1358 $self->ve_divert_add ("/sbin/udevd");
1360 if ($suite eq 'etch') {
1361 # disable apache2 startup
1362 write_file
("NO_START=1\n", "$rootdir/etc/default/apache2");
1365 $self->logmsg ("configure required packages\n");
1366 $self->ve_command ("dpkg --force-confold --skip-same-version --configure -a");
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);
1373 $data = "postmaster: root\nwebmaster: root\n";
1374 write_file
($data, "$rootdir/etc/aliases");
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");
1385 # unpack base packages
1386 foreach my $p (@$important) {
1387 $self->ve_dpkg ('unpack', $p);
1391 $self->ve_command ("ifconfig lo up");
1393 $self->logmsg ("configure important packages\n");
1394 $self->ve_command ("dpkg --force-confold --skip-same-version --configure -a");
1396 if (-d
"$rootdir/etc/event.d") {
1397 unlink <$rootdir/etc/event.d
/tty
*>;
1400 if (-f
"$rootdir/etc/inittab") {
1401 $self->run_command ("sed -i -e '/getty\\s38400\\stty[23456]/d' '$rootdir/etc/inittab'");
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");
1409 $self->ve_command ("usermod -L root");
1411 # regenerate sshd host keys
1412 $self->install_init_script ($script_ssh_init, 2, 14);
1414 if ($mta eq 'postfix') {
1415 $data = "postfix postfix/main_mailer_type select No configuration\n";
1416 $self->ve_debconfig_set ($data);
1418 unlink "$rootdir/etc/mailname";
1419 write_file
($postfix_main_cf, "$rootdir/etc/postfix/main.cf");
1422 if (!$opts->{minimal
}) {
1423 # unpack standard packages
1424 foreach my $p (@$standard) {
1425 $self->ve_dpkg ('unpack', $p);
1428 $self->logmsg ("configure standard packages\n");
1429 $self->ve_command ("dpkg --force-confold --skip-same-version --configure -a");
1432 # disable HWCLOCK access
1433 $self->run_command ("echo 'HWCLOCKACCESS=no' >> '$rootdir/etc/default/rcS'");
1436 $self->ve_divert_add ("/usr/sbin/hald");
1438 # disable /dev/urandom init
1439 $self->run_command ("install -m 0755 '$script_init_urandom' '$rootdir/etc/init.d/urandom'");
1441 if ($suite eq 'etch' || $suite eq 'hardy' || $suite eq 'intrepid' || $suite eq 'jaunty') {
1443 $self->ve_divert_add ("/sbin/klogd");
1446 # remove unnecessays sysctl entries to avoid warnings
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);
1455 my $bindv6only = "$rootdir/etc/sysctl.d/bindv6only.conf";
1456 if (-f
$bindv6only) {
1458 $cmd .= ' -e \'s/^\(net\.ipv6\.bindv6only.*\)/#\1/\'';
1459 $cmd .= " -i '$bindv6only'";
1460 $self->run_command ($cmd);
1463 if ($suite eq 'hardy' || $suite eq 'intrepid' || $suite eq 'jaunty') {
1464 # disable tty init (console-setup)
1466 $cmd .= ' -e \'s/^\(ACTIVE_CONSOLES=.*\)/ACTIVE_CONSOLES=/\'';
1467 $cmd .= " -i '$rootdir/etc/default/console-setup'";
1468 $self->run_command ($cmd);
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");
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");
1489 my $veid = $self->{veid
};
1491 my $vestat = $self->ve_status();
1493 if (!$vestat->{exist
}) {
1494 $self->logmsg ("Please create the appliance first (bootstrap)");
1498 if (!$vestat->{running
}) {
1499 $self->run_command ("vzctl start $veid");
1502 system ("vzctl enter $veid");
1505 sub ve_mysql_command
{
1506 my ($self, $sql, $password) = @_;
1508 #my $bootstrap = "/usr/sbin/mysqld --bootstrap --user=mysql --skip-grant-tables " .
1509 #"--skip-bdb --skip-innodb --skip-ndbcluster";
1511 $self->ve_command ("mysql", $sql);
1514 sub ve_mysql_bootstrap
{
1515 my ($self, $sql, $password) = @_;
1519 my $suite = $self->{config
}->{suite
};
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");
1526 if ($suite eq 'squeeze' || $suite eq 'wheezy' || $suite eq 'jessie') {
1527 $cmd = "/usr/sbin/mysqld --bootstrap --user=mysql --skip-grant-tables";
1530 $cmd = "/usr/sbin/mysqld --bootstrap --user=mysql --skip-grant-tables " .
1531 "--skip-bdb --skip-innodb --skip-ndbcluster";
1534 $self->ve_command ($cmd, $sql);
1537 sub compute_required
{
1538 my ($self, $pkglist) = @_;
1540 my $pkginfo = $self->pkginfo();
1541 my $instpkgs = $self->read_installed ();
1544 __record_provides
($pkginfo, $closure, [keys %$instpkgs]);
1546 return $self->closure ($closure, $pkglist);
1550 my ($self, $opts) = @_;
1552 my @supp = ('7.4', '8.1');
1553 my $pgversion = '8.1';
1555 my $suite = $self->{config
}->{suite
};
1557 if ($suite eq 'lenny' || $suite eq 'hardy' || $suite eq 'intrepid' || $suite eq 'jaunty') {
1560 } elsif ($suite eq 'squeeze') {
1563 } elsif ($suite eq 'wheezy') {
1566 } elsif ($suite eq 'jessie') {
1571 $pgversion = $opts->{version
} if $opts->{version
};
1573 die "unsupported postgres version '$pgversion'\n"
1574 if !grep { $pgversion eq $_; } @supp;
1576 my $rootdir = $self->vz_root_dir();
1578 my $required = $self->compute_required (["postgresql-$pgversion"]);
1580 $self->cache_packages ($required);
1582 $self->ve_dpkg ('install', @$required);
1584 my $iscript = "postgresql-$pgversion";
1585 if ($suite eq 'squeeze' || $suite eq 'wheezy' || $suite eq 'jessie') {
1586 $iscript = 'postgresql';
1589 $self->ve_command ("/etc/init.d/$iscript start") if $opts->{start
};
1593 my ($self, $opts) = @_;
1595 my $password = $opts->{password
};
1596 my $rootdir = $self->vz_root_dir();
1598 my $suite = $self->{config
}->{suite
};
1601 if ($suite eq 'squeeze') {
1603 } elsif ($suite eq 'wheezy' || $suite eq 'jessie') {
1607 my $required = $self->compute_required (['mysql-common', "mysql-server-$ver"]);
1609 $self->cache_packages ($required);
1611 $self->ve_dpkg ('install', @$required);
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);
1621 my $rpw = $password eq 'random' ?
'admin' : $password;
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);
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);
1634 $self->ve_command ("/etc/init.d/mysql start") if $opts->{start
};
1638 my ($self, $opts) = @_;
1640 my $memlimit = $opts->{memlimit
};
1641 my $rootdir = $self->vz_root_dir();
1643 my $required = $self->compute_required ([qw
(php5 php5-cli libapache2-mod-php5 php5-gd
)]);
1645 $self->cache_packages ($required);
1647 $self->ve_dpkg ('install', @$required);
1650 $self->run_command ("sed -e 's/^\\s*memory_limit\\s*=.*;/memory_limit = ${memlimit}M;/' -i $rootdir/etc/php5/apache2/php.ini");
1655 my ($self, $pkglist, $unpack) = @_;
1657 my $required = $self->compute_required ($pkglist);
1659 $self->cache_packages ($required);
1661 $self->ve_dpkg ($unpack ?
'unpack' : 'install', @$required);
1665 my ($self, $distclean) = @_;
1667 unlink $self->{logfile
};
1668 unlink "$self->{targetname}.tar";
1669 unlink "$self->{targetname}.tar.gz";
1671 $self->ve_destroy ();
1674 rmtree
$self->{cachedir
} if $distclean && !$self->{config
}->{cachedir
};
1676 rmtree
$self->{infodir
};