11 use POSIX qw
(LONG_MAX
);
15 # fixme: lock container ?
17 my $dablibdir = "/usr/lib/dab";
18 my $devicetar = "$dablibdir/devices.tar.gz";
19 my $default_env = "$dablibdir/scripts/defenv";
20 my $fake_init = "$dablibdir/scripts/init.pl";
21 my $script_ssh_init = "$dablibdir/scripts/ssh_gen_host_keys";
22 my $script_mysql_randompw = "$dablibdir/scripts/mysql_randompw";
23 my $script_init_urandom = "$dablibdir/scripts/init_urandom";
25 my $postfix_main_cf = <<EOD;
26 # See /usr/share/postfix/main.cf.dist for a commented, more complete version
28 smtpd_banner = \$myhostname ESMTP \$mail_name (Debian/GNU)
31 # appending .domain is the MUA's job.
32 append_dot_mydomain = no
34 # Uncomment the next line to generate "delayed mail" warnings
35 #delay_warning_time = 4h
37 alias_maps = hash:/etc/aliases
38 alias_database = hash:/etc/aliases
39 mydestination = \$myhostname, localhost.\$mydomain, localhost
41 mynetworks = 127.0.0.0/8
42 inet_interfaces = loopback-only
43 recipient_delimiter = +
47 # produce apt compatible filenames (/var/lib/apt/lists)
48 sub __url_to_filename
{
59 my ($self, $url, $path) = @_;
61 $self->logmsg ("download: $url\n");
62 my $tmpfn = "$path.tmp$$";
64 $self->run_command ("wget -q '$url' -O '$tmpfn'");
73 rename ($tmpfn, $path);
77 my ($data, $file, $perm) = @_;
79 die "no filename" if !$file;
83 my $fh = IO
::File-
>new ($file, O_WRONLY
| O_CREAT
, $perm) ||
84 die "unable to open file '$file'";
94 die "no filename" if !$file;
96 my $fh = IO
::File-
>new ($file) ||
97 die "unable to open file '$file'";
99 local $/; # slurp mode
113 my $fh = IO
::File-
>new ("<$filename") || return $res;
116 while (defined (my $line = <$fh>)) {
117 next if $line =~ m/^\#/;
118 next if $line =~ m/^\s*$/;
128 if ($rec =~ s/^Description:\s*([^\n]*)(\n\s+.*)*$//si) {
129 $res->{headline
} = $1;
130 chomp $res->{headline
};
133 $res->{description
} = $long;
134 chomp $res->{description
};
135 } elsif ($rec =~ s/^([^:]+):\s*(.*\S)\s*\n//) {
136 my ($key, $value) = (lc ($1), $2);
137 if ($key eq 'source' || $key eq 'mirror') {
138 push @{$res->{$key}}, $value;
140 die "duplicate key '$key'\n" if defined ($res->{$key});
141 $res->{$key} = $value;
144 die "unable to parse config file: $rec";
148 die "unable to parse config file" if $rec;
154 my ($self, $cmd, $input, $getoutput) = @_;
156 my $reader = IO
::File-
>new();
157 my $writer = IO
::File-
>new();
158 my $error = IO
::File-
>new();
162 my $cmdstr = ref ($cmd) eq 'ARRAY' ?
join (' ', @$cmd) : $cmd;
166 if (ref ($cmd) eq 'ARRAY') {
167 $pid = open3
($writer, $reader, $error, @$cmd) || die $!;
169 $pid = open3
($writer, $reader, $error, $cmdstr) || die $!;
176 if ($orig_pid != $$) {
177 $self->logmsg ("ERROR: command '$cmdstr' failed - fork failed\n");
184 print $writer $input if defined $input;
187 my $select = new IO
::Select
;
188 $select->add ($reader);
189 $select->add ($error);
192 my $logfd = $self->{logfd
};
194 while ($select->count) {
195 my @handles = $select->can_read ();
197 foreach my $h (@handles) {
199 my $count = sysread ($h, $buf, 4096);
200 if (!defined ($count)) {
202 die "command '$cmdstr' failed: $!";
204 $select->remove ($h) if !$count;
208 $res .= $buf if $getoutput;
215 die "command '$cmdstr' failed with exit code $ec\n" if $ec;
223 $self->writelog (@_);
228 my $fd = $self->{logfd
};
232 sub __sample_config
{
236 my $arch = $self->{config
}->{architecture
};
238 my $ostype = $self->{config
}->{ostype
};
240 if ($ostype =~ m/^debian-/) {
241 $data .= "lxc.include = /usr/share/lxc/config/debian.common.conf\n";
242 } elsif ($ostype =~ m/^ubuntu-/) {
243 $data .= "lxc.include = /usr/share/lxc/config/ubuntu.common.conf\n";
245 die "unknown os type '$ostype'\n";
247 $data .= "lxc.utsname = localhost\n";
248 $data .= "lxc.rootfs = $self->{rootfs}\n";
257 if (my $fd = IO
::File-
>new (".veid")) {
264 $self->{working_dir
} = getcwd
;
265 $self->{veconffile
} = "$self->{working_dir}/config";
266 $self->{rootfs
} = "$self->{working_dir}/rootfs";
269 $self->{veid
} = $cid;
275 UUID
::generate
($uuid);
276 UUID
::unparse
($uuid, $uuid_str);
277 $self->{veid
} = $uuid_str;
279 my $fd = IO
::File-
>new (">.veid") ||
280 die "unable to write '.veid'\n";
281 print $fd "$self->{veid}\n";
284 my $cdata = $self->__sample_config();
286 my $fh = IO
::File-
>new ($self->{veconffile
}, O_WRONLY
|O_CREAT
|O_EXCL
) ||
287 die "unable to write lxc config file '$self->{veconffile}' - $!";
291 mkdir $self->{rootfs
} || die "unable to create rootfs - $!";
293 $self->logmsg ("allocated VE $self->{veid}\n");
295 return $self->{veid
};
299 my ($class, $config) = @_;
301 $class = ref ($class) || $class;
305 $config = read_config
('dab.conf') if !$config;
307 $self->{config
} = $config;
311 $self->{logfile
} = "logfile";
312 $self->{logfd
} = IO
::File-
>new (">>$self->{logfile}") ||
313 die "unable to open log file";
315 my $arch = $config->{architecture
};
316 die "no 'architecture' specified\n" if !$arch;
318 die "unsupported architecture '$arch'\n"
319 if $arch !~ m/^(i386|amd64)$/;
321 my $suite = $config->{suite
} || die "no 'suite' specified\n";
322 if ($suite eq 'jessie') {
323 $config->{ostype
} = "debian-8.0";
324 } elsif ($suite eq 'wheezy') {
325 $config->{ostype
} = "debian-7.0";
326 } elsif ($suite eq 'squeeze') {
327 $config->{ostype
} = "debian-6.0";
328 } elsif ($suite eq 'lenny') {
329 $config->{ostype
} = "debian-5.0";
330 } elsif ($suite eq 'etch') {
331 $config->{ostype
} = "debian-4.0";
332 } elsif ($suite eq 'hardy') {
333 $config->{ostype
} = "ubuntu-8.04";
334 } elsif ($suite eq 'intrepid') {
335 $config->{ostype
} = "ubuntu-8.10";
336 } elsif ($suite eq 'jaunty') {
337 $config->{ostype
} = "ubuntu-9.04";
338 } elsif ($suite eq 'precise') {
339 $config->{ostype
} = "ubuntu-12.04";
340 } elsif ($suite eq 'trusty') {
341 $config->{ostype
} = "ubuntu-14.04";
342 } elsif ($suite eq 'vivid') {
343 $config->{ostype
} = "ubuntu-15.04";
345 die "unsupported debian suite '$suite'\n";
348 my $name = $config->{name
} || die "no 'name' specified\n";
350 $name =~ m/^[a-z][0-9a-z\-\*\.]+$/ ||
351 die "illegal characters in name '$name'\n";
353 my $version = $config->{version
};
354 die "no 'version' specified\n" if !$version;
355 die "no 'section' specified\n" if !$config->{section
};
356 die "no 'description' specified\n" if !$config->{headline
};
357 die "no 'maintainer' specified\n" if !$config->{maintainer
};
359 if ($name =~ m/^$config->{ostype}/) {
360 $self->{targetname
} = "${name}_${version}_$config->{architecture}";
362 $self->{targetname
} = "$config->{ostype}-${name}_" .
363 "${version}_$config->{architecture}";
366 if (!$config->{source
}) {
367 if ($suite eq 'etch' || $suite eq 'lenny' || $suite eq 'squeeze' ||
368 $suite eq 'wheezy' || $suite eq 'jessie' ) {
369 push @{$config->{source
}}, "http://ftp.debian.org/debian SUITE main contrib";
370 push @{$config->{source
}}, "http://ftp.debian.org/debian SUITE-updates main contrib"
371 if ($suite eq 'squeeze' || $suite eq 'wheezy' || $suite eq 'jessie');
372 push @{$config->{source
}}, "http://security.debian.org SUITE/updates main contrib";
373 } elsif ($suite eq 'hardy' || $suite eq 'intrepid' || $suite eq 'jaunty' || $suite eq 'vivid' || $suite eq 'trusty' || $suite eq 'precise') {
374 my $comp = "main restricted universe multiverse";
375 push @{$config->{source
}}, "http://archive.ubuntu.com/ubuntu SUITE $comp";
376 push @{$config->{source
}}, "http://archive.ubuntu.com/ubuntu SUITE-updates $comp";
377 push @{$config->{source
}}, "http://archive.ubuntu.com/ubuntu SUITE-security $comp";
385 foreach my $s (@{$config->{source
}}) {
386 if ($s =~ m
@^\s*((http
|ftp
)://\S
+)\s
+(\S
+)((\s
+(\S
+))+)$@) {
387 my ($url, $su, $components) = ($1, $3, $4);
388 $su =~ s/SUITE/$suite/;
389 $components =~ s/^\s+//;
390 $components =~ s/\s+$//;
392 foreach my $co (split (/\s+/, $components)) {
395 $ca = ['main'] if !$ca;
403 die "syntax error in source spezification '$s'\n";
407 foreach my $m (@{$config->{mirror
}}) {
408 if ($m =~ m
@^\s*((http
|ftp
)://\S
+)\s
*=>\s
*((http
|ftp
)://\S
+)\s
*$@) {
409 my ($ms, $md) = ($1, $3);
411 foreach my $ss (@$sources) {
412 if ($ss->{source
} eq $ms) {
418 die "unusable mirror $ms\n" if !$found;
420 die "syntax error in mirror spezification '$m'\n";
424 $self->{sources
} = $sources;
426 $self->{infodir
} = "info";
428 $self->__allocate_ve ();
430 $self->{cachedir
} = ($config->{cachedir
} || 'cache') . "/$suite";;
432 my $incl = [qw
(less ssh openssh-server logrotate
)];
434 my $excl = [qw
(modutils reiserfsprogs ppp pppconfig pppoe
435 pppoeconf nfs-common mtools ntp
)];
437 # ubuntu has too many dependencies on udev, so
438 # we cannot exclude it (instead we disable udevd)
440 if ($suite eq 'vivid') {
442 } elsif ($suite eq 'trusty') {
443 push @$excl, qw(systemd systemd-services libpam-systemd libsystemd-daemon0 memtest86+);
444 } elsif ($suite eq 'precise') {
445 push @$excl, qw(systemd systemd-services libpam-systemd libsystemd-daemon0 memtest86+ ubuntu-standard);
446 } elsif ($suite eq 'hardy') {
447 push @$excl, qw(kbd);
448 push @$excl, qw(apparmor apparmor-utils ntfs-3g
450 } elsif ($suite eq 'intrepid' || $suite eq 'jaunty') {
451 push @$excl, qw(apparmor apparmor-utils libapparmor1 libapparmor-perl
452 libntfs-3g28 ntfs-3g friendly-recovery);
453 } elsif ($suite eq 'jessie') {
454 push @$incl, 'sysvinit-core'; # avoid systemd and udev
455 push @$incl, 'libperl4-corelibs-perl'; # to make lsof happy
456 push @$excl, qw(systemd systemd-sysv udev module-init-tools pciutils hdparm
459 push @$excl, qw(udev module-init-tools pciutils hdparm
463 $self->{incl
} = $incl;
464 $self->{excl
} = $excl;
472 my $infodir = $self->{infodir
};
473 my $arch = $self->{config
}->{architecture
};
479 my $logfd = $self->{logfd
} = IO
::File-
>new (">$self->{logfile}") ||
480 die "unable to open log file";
482 foreach my $ss (@{$self->{sources
}}) {
483 my $src = $ss->{mirror
} || $ss->{source
};
484 my $path = "dists/$ss->{suite}/Release";
485 my $url = "$src/$path";
486 my $target = __url_to_filename
("$ss->{source}/$path");
488 $self->download ($url, "$infodir/$target");
489 $self->download ("$url.gpg", "$infodir/$target.gpg");
490 # fixme: impl. verify (needs --keyring option)
494 warn "Release info ignored\n";
496 foreach my $comp (@{$ss->{comp
}}) {
497 $path = "dists/$ss->{suite}/$comp/binary-$arch/Packages.gz";
498 $target = "$infodir/" . __url_to_filename
("$ss->{source}/$path");
499 my $pkgsrc = "$src/$path";
500 $self->download ($pkgsrc, $target);
501 $self->run_command ("gzip -d '$target'");
507 my ($self, $filename, $size) = @_;
509 my $config = $self->{config
};
513 $data .= "Name: $config->{name}\n";
514 $data .= "Version: $config->{version}\n";
515 $data .= "Type: lxc\n";
516 $data .= "OS: $config->{ostype}\n";
517 $data .= "Section: $config->{section}\n";
518 $data .= "Maintainer: $config->{maintainer}\n";
519 $data .= "Architecture: $config->{architecture}\n";
520 $data .= "Installed-Size: $size\n";
523 $data .= "Infopage: $config->{infopage}\n" if $config->{infopage
};
524 $data .= "ManageUrl: $config->{manageurl}\n" if $config->{manageurl
};
525 $data .= "Certified: $config->{certified}\n" if $config->{certified
};
528 $data .= "Description: $config->{headline}\n";
529 $data .= "$config->{description}\n" if $config->{description
};
531 write_file
($data, $filename, 0644);
535 my ($self, $opts) = @_;
537 my $suite = $self->{config
}->{suite
};
538 my $infodir = $self->{infodir
};
539 my $arch = $self->{config
}->{architecture
};
541 my $instpkgs = $self->read_installed ();
542 my $pkginfo = $self->pkginfo();
543 my $veid = $self->{veid
};
544 my $rootdir = $self->{rootfs
};
546 my $vestat = $self->ve_status();
547 die "ve not running - unable to finalize\n" if !$vestat->{running
};
550 if (-f
"$rootdir/etc/init.d/mysql") {
551 $self->ve_command ("/etc/init.d/mysql stop");
554 if (!($opts->{keepmycnf
} || (-f
"$rootdir/etc/init.d/mysql_randompw"))) {
555 unlink "$rootdir/root/.my.cnf";
558 if ($suite eq 'etch') {
559 # enable apache2 startup
560 if ($instpkgs->{apache2
}) {
561 write_file
("NO_START=0\n", "$rootdir/etc/default/apache2");
563 unlink "$rootdir/etc/default/apache2";
566 $self->logmsg ("cleanup package status\n");
567 # prevent auto selection of all standard, required or important
568 # packages which are not installed
569 foreach my $pkg (keys %$pkginfo) {
570 my $pri = $pkginfo->{$pkg}->{priority
};
571 if ($pri && ($pri eq 'required' || $pri eq 'important'
572 || $pri eq 'standard')) {
573 if (!$instpkgs->{$pkg}) {
574 $self->ve_dpkg_set_selection ($pkg, 'purge');
579 $self->ve_command ("apt-get clean");
581 $self->logmsg ("update available package list\n");
583 $self->ve_command ("dpkg --clear-avail");
584 foreach my $ss (@{$self->{sources
}}) {
585 my $relsrc = __url_to_filename
("$ss->{source}/dists/$ss->{suite}/Release");
586 if (-f
"$infodir/$relsrc" && -f
"$infodir/$relsrc.gpg") {
587 $self->run_command ("cp '$infodir/$relsrc' '$rootdir/var/lib/apt/lists/$relsrc'");
588 $self->run_command ("cp '$infodir/$relsrc.gpg' '$rootdir/var/lib/apt/lists/$relsrc.gpg'");
590 foreach my $comp (@{$ss->{comp
}}) {
591 my $src = __url_to_filename
("$ss->{source}/dists/$ss->{suite}/" .
592 "$comp/binary-$arch/Packages");
593 my $target = "/var/lib/apt/lists/$src";
594 $self->run_command ("cp '$infodir/$src' '$rootdir/$target'");
595 $self->ve_command ("dpkg --merge-avail '$target'");
599 # set dselect default method
600 write_file
("apt apt\n", "$rootdir/var/lib/dpkg/cmethopt");
602 $self->ve_divert_remove ("/usr/sbin/policy-rc.d");
604 $self->ve_divert_remove ("/sbin/start-stop-daemon");
606 $self->ve_divert_remove ("/sbin/init");
608 # finally stop the VE
609 $self->run_command ("lxc-stop -n $veid --kill");
611 unlink "$rootdir/sbin/defenv";
613 unlink <$rootdir/root/dead.letter
*>;
615 unlink "$rootdir/var/log/init.log";
617 unlink "$rootdir/aquota.group";
619 unlink "$rootdir/aquota.user";
621 write_file
("", "$rootdir/var/log/syslog");
623 $self->logmsg ("detecting final size: ");
625 my $sizestr = $self->run_command ("du -sm $rootdir", undef, 1);
627 if ($sizestr =~ m/^(\d+)\s+\Q$rootdir\E$/) {
630 die "unable to detect size\n";
632 $self->logmsg ("$size MB\n");
634 $self->write_config ("$rootdir/etc/appliance.info", $size);
636 $self->logmsg ("creating final appliance archive\n");
638 my $target = "$self->{targetname}.tar";
642 $self->run_command ("tar cpf $target --numeric-owner -C '$rootdir' ./etc/appliance.info");
643 $self->run_command ("tar rpf $target --numeric-owner -C '$rootdir' --exclude ./etc/appliance.info .");
644 $self->run_command ("gzip $target");
650 my $rootdir = $self->{rootfs
};
652 my $pkgfilelist = "$rootdir/var/lib/dpkg/status";
654 open (PKGLST
, "<$pkgfilelist") ||
655 die "unable to open '$pkgfilelist'";
659 while (my $rec = <PKGLST
>) {
665 while ($rec =~ s/^([^:]+):\s+(.*)\s*\n//) {
669 my $pkg = $res->{'package'};
670 if (my $status = $res->{status
}) {
671 my @sa = split (/\s+/, $status);
673 if ($stat && ($stat ne 'purge')) {
674 $pkglist->{$pkg} = $res;
687 my $veid = $self->{veid
};
689 my $res = { running
=> 0 };
691 $res->{exist
} = 1 if -d
"$self->{rootfs}/usr";
693 my $filename = "/proc/net/unix";
695 # similar test is used by lcxcontainers.c: list_active_containers
696 my $fh = IO
::File-
>new ($filename, "r");
699 while (defined(my $line = <$fh>)) {
700 if ($line =~ m/^[a-f0-9]+:\s\S+\s\S+\s\S+\s\S+\s\S+\s\d+\s(\S+)$/) {
702 if ($path =~ m!^@/\S+/$veid/command$!) {
713 my ($self, $cmd, $input) = @_;
715 my $veid = $self->{veid
};
717 if (ref ($cmd) eq 'ARRAY') {
718 unshift @$cmd, 'lxc-attach', '-n', $veid, '--clear-env', '--', 'defenv';
719 $self->run_command ($cmd, $input);
721 $self->run_command ("lxc-attach -n $veid --clear-env -- defenv $cmd", $input);
725 # like ve_command, but pipes stdin correctly
727 my ($self, @cmd) = @_;
729 my $veid = $self->{veid
};
732 my $pid = open2
($reader, "<&STDIN", 'lxc-attach', '-n', $veid, '--',
733 'defenv', @cmd) || die "unable to exec command";
735 while (defined (my $line = <$reader>)) {
736 $self->logmsg ($line);
742 die "ve_exec failed - status $rc\n" if $rc != 0;
746 my ($self, $filename) = @_;
748 $self->ve_command ("dpkg-divert --add --divert '$filename.distrib' " .
749 "--rename '$filename'");
751 sub ve_divert_remove
{
752 my ($self, $filename) = @_;
754 my $rootdir = $self->{rootfs
};
756 unlink "$rootdir/$filename";
757 $self->ve_command ("dpkg-divert --remove --rename '$filename'");
760 sub ve_debconfig_set
{
761 my ($self, $dcdata) = @_;
763 my $rootdir = $self->{rootfs
};
764 my $cfgfile = "/tmp/debconf.txt";
765 write_file
($dcdata, "$rootdir/$cfgfile");
766 $self->ve_command ("debconf-set-selections $cfgfile");
767 unlink "$rootdir/$cfgfile";
770 sub ve_dpkg_set_selection
{
771 my ($self, $pkg, $status) = @_;
773 $self->ve_command ("dpkg --set-selections", "$pkg $status");
777 my ($self, $cmd, @pkglist) = @_;
779 return if !scalar (@pkglist);
781 my $pkginfo = $self->pkginfo();
783 my $rootdir = $self->{rootfs
};
784 my $cachedir = $self->{cachedir
};
788 foreach my $pkg (@pkglist) {
789 my $filename = $self->getpkgfile ($pkg);
790 $self->run_command ("cp '$cachedir/$filename' '$rootdir/$filename'");
791 push @files, "/$filename";
792 $self->logmsg ("$cmd: $pkg\n");
795 my $fl = join (' ', @files);
797 if ($cmd eq 'install') {
798 $self->ve_command ("dpkg --force-depends --force-confold --install $fl");
799 } elsif ($cmd eq 'unpack') {
800 $self->ve_command ("dpkg --force-depends --unpack $fl");
802 die "internal error";
805 foreach my $fn (@files) { unlink "$rootdir$fn"; }
811 my $veid = $self->{veid
}; # fixme
813 my $vestat = $self->ve_status();
814 if ($vestat->{running
}) {
815 $self->run_command ("lxc-stop -n $veid --kill");
818 rmtree
$self->{rootfs
};
819 unlink $self->{veconffile
};
825 my $veid = $self->{veid
};
827 $self->logmsg ("initialize VE $veid\n");
829 my $vestat = $self->ve_status();
830 if ($vestat->{running
}) {
831 $self->run_command ("lxc-stop -n $veid --kill");
834 rmtree
$self->{rootfs
};
835 mkpath
$self->{rootfs
};
838 sub __deb_version_cmp
{
839 my ($cur, $op, $new) = @_;
841 if (system("dpkg", "--compare-versions", $cur, $op, $new) == 0) {
848 sub __parse_packages
{
849 my ($pkginfo, $filename, $src) = @_;
852 open (PKGLST
, "<$filename") ||
853 die "unable to open '$filename'";
855 while (my $rec = <PKGLST
>) {
862 while ($rec =~ s/^([^:]+):\s+(.*)\s*\n//) {
866 my $pkg = $res->{'package'};
867 if ($pkg && $res->{'filename'}) {
869 if (my $info = $pkginfo->{$pkg}) {
870 $cur = $info->{version
};
872 my $new = $res->{version
};
873 if (!$cur || __deb_version_cmp
($cur, 'lt', $new)) {
875 $res->{url
} = "$src/$res->{'filename'}";
877 die "no url for package '$pkg'" if !$res->{url
};
879 $pkginfo->{$pkg} = $res;
890 return $self->{pkginfo
} if $self->{pkginfo
};
892 my $infodir = $self->{infodir
};
893 my $arch = $self->{config
}->{architecture
};
895 my $availfn = "$infodir/available";
900 # reading 'available' is faster, because it only contains latest version
901 # (no need to do slow version compares)
903 __parse_packages
($pkginfo, $availfn);
904 $self->{pkginfo
} = $pkginfo;
908 $self->logmsg ("generating available package list\n");
910 foreach my $ss (@{$self->{sources
}}) {
911 foreach my $comp (@{$ss->{comp
}}) {
912 my $url = "$ss->{source}/dists/$ss->{suite}/$comp/binary-$arch/Packages";
913 my $pkgfilelist = "$infodir/" . __url_to_filename
($url);
915 my $src = $ss->{mirror
} || $ss->{source
};
917 __parse_packages
($pkginfo, $pkgfilelist, $src);
921 if (my $dep = $self->{config
}->{depends
}) {
922 foreach my $d (split (/,/, $dep)) {
923 if ($d =~ m/^\s*(\S+)\s*(\((\S+)\s+(\S+)\)\s*)?$/) {
924 my ($pkg, $op, $rver) = ($1, $3, $4);
925 $self->logmsg ("checking dependencies: $d\n");
926 my $info = $pkginfo->{$pkg};
927 die "package '$pkg' not available\n" if !$info;
929 my $cver = $info->{version
};
930 if (!__deb_version_cmp
($cver, $op, $rver)) {
931 die "detected wrong version '$cver'\n";
935 die "syntax error in depends field";
940 $self->{pkginfo
} = $pkginfo;
942 my $tmpfn = "$availfn.tmp$$";
943 my $fd = IO
::File-
>new (">$tmpfn");
944 foreach my $pkg (sort keys %$pkginfo) {
945 my $info = $pkginfo->{$pkg};
946 print $fd "package: $pkg\n";
947 foreach my $k (sort keys %$info) {
948 next if $k eq 'description';
949 next if $k eq 'package';
951 print $fd "$k: $v\n" if $v;
953 print $fd "description: $info->{description}\n" if $info->{description
};
958 rename ($tmpfn, $availfn);
963 sub __record_provides
{
964 my ($pkginfo, $closure, $list, $skipself) = @_;
966 foreach my $pname (@$list) {
967 my $info = $pkginfo->{$pname};
968 # fixme: if someone install packages directly using dpkg, there
969 # is no entry in 'available', only in 'status'. In that case, we
970 # should extract info from $instpkgs
972 warn "hint: ignoring provides for '$pname' - package not in 'available' list.\n";
975 if (my $prov = $info->{provides
}) {
976 my @pl = split (',', $prov);
977 foreach my $p (@pl) {
979 if (!($skipself && (grep { $1 eq $_ } @$list))) {
984 $closure->{$pname} = 1 if !$skipself;
989 my ($self, $closure, $list) = @_;
991 my $pkginfo = $self->pkginfo();
993 # first, record provided packages
994 __record_provides
($pkginfo, $closure, $list, 1);
998 # then resolve dependencies
999 foreach my $pname (@$list) {
1000 __closure_single
($pkginfo, $closure, $pkgs, $pname, $self->{excl
});
1003 return [ keys %$pkgs ];
1006 sub __closure_single
{
1007 my ($pkginfo, $closure, $pkgs, $pname, $excl) = @_;
1011 $pname =~ s/:any$//;
1013 return if $closure->{$pname};
1015 my $info = $pkginfo->{$pname} || die "no such package '$pname'";
1017 my $dep = $info->{depends
};
1018 my $predep = $info->{'pre-depends'};
1020 my $size = $info->{size
};
1021 my $url = $info->{url
};
1023 $url || die "$pname: no url for package '$pname'";
1025 $pkgs->{$pname} = 1;
1027 __record_provides
($pkginfo, $closure, [$pname]) if $info->{provides
};
1029 $closure->{$pname} = 1;
1035 push @l, split (/,/, $predep) if $predep;
1036 push @l, split (/,/, $dep) if $dep;
1038 DEPEND
: foreach my $p (@l) {
1039 my @l1 = split (/\|/, $p);
1040 foreach my $p1 (@l1) {
1041 if ($p1 =~ m/^\s*(\S+).*/) {
1042 #printf (STDERR "$pname: $p --> $1\n");
1043 if ($closure->{$1}) {
1044 next DEPEND
; # dependency already met
1048 # search for non-excluded alternative
1050 foreach my $p1 (@l1) {
1051 if ($p1 =~ m/^\s*(\S+).*/) {
1052 next if grep { $1 eq $_ } @$excl;
1057 die "package '$pname' depends on exclusion '$p'\n" if !$found;
1059 #printf (STDERR "$pname: $p --> $found\n");
1061 __closure_single
($pkginfo, $closure, $pkgs, $found, $excl);
1065 sub cache_packages
{
1066 my ($self, $pkglist) = @_;
1068 foreach my $pkg (@$pkglist) {
1069 $self->getpkgfile ($pkg);
1074 my ($self, $pkg) = @_;
1076 my $pkginfo = $self->pkginfo();
1077 my $info = $pkginfo->{$pkg} || die "no such package '$pkg'";
1078 my $cachedir = $self->{cachedir
};
1080 my $url = $info->{url
};
1083 if ($url =~ m
|/([^/]+.deb
)$|) {
1086 die "internal error";
1089 return $filename if -f
"$cachedir/$filename";
1093 $self->download ($url, "$cachedir/$filename");
1098 sub install_init_script
{
1099 my ($self, $script, $runlevel, $prio) = @_;
1101 my $suite = $self->{config
}->{suite
};
1102 my $rootdir = $self->{rootfs
};
1104 my $base = basename
($script);
1105 my $target = "$rootdir/etc/init.d/$base";
1107 $self->run_command ("install -m 0755 '$script' '$target'");
1108 if ($suite eq 'etch' || $suite eq 'lenny') {
1109 $self->ve_command ("update-rc.d $base start $prio $runlevel .");
1110 } elsif ($suite eq 'vivid') {
1111 die "unable to install init script (system uses systemd)\n";
1112 } elsif ($suite eq 'trusty' || $suite eq 'precise') {
1113 die "unable to install init script (system uses upstart)\n";
1115 $self->ve_command ("insserv $base");
1122 my ($self, $opts) = @_;
1124 my $pkginfo = $self->pkginfo();
1125 my $veid = $self->{veid
};
1126 my $suite = $self->{config
}->{suite
};
1128 my $important = [ @{$self->{incl
}} ];
1132 my $mta = $opts->{exim
} ?
'exim' : 'postfix';
1134 if ($mta eq 'postfix') {
1135 push @$important, "postfix";
1138 foreach my $p (keys %$pkginfo) {
1139 next if grep { $p eq $_ } @{$self->{excl
}};
1140 my $pri = $pkginfo->{$p}->{priority
};
1142 next if $mta ne 'exim' && $p =~ m/exim/;
1143 next if $p =~ m/(selinux|semanage|policycoreutils)/;
1145 push @$required, $p if $pri eq 'required';
1146 push @$important, $p if $pri eq 'important';
1147 push @$standard, $p if $pri eq 'standard' && !$opts->{minimal
};
1151 $required = $self->closure ($closure, $required);
1152 $important = $self->closure ($closure, $important);
1154 if (!$opts->{minimal
}) {
1155 push @$standard, 'xbase-clients';
1156 $standard = $self->closure ($closure, $standard);
1159 # test if we have all 'ubuntu-minimal' and 'ubuntu-standard' packages
1160 # except those explicitly excluded
1161 if ($suite eq 'hardy' || $suite eq 'intrepid' || $suite eq 'jaunty') {
1162 my $mdeps = $pkginfo->{'ubuntu-minimal'}->{depends
};
1163 foreach my $d (split (/,/, $mdeps)) {
1164 if ($d =~ m/^\s*(\S+)$/) {
1166 next if $closure->{$pkg};
1167 next if grep { $pkg eq $_ } @{$self->{excl
}};
1168 die "missing ubuntu-minimal package '$pkg'\n";
1171 if (!$opts->{minimal
}) {
1172 $mdeps = $pkginfo->{'ubuntu-standard'}->{depends
};
1173 foreach my $d (split (/,/, $mdeps)) {
1174 if ($d =~ m/^\s*(\S+)$/) {
1176 next if $closure->{$pkg};
1177 next if grep { $pkg eq $_ } @{$self->{excl
}};
1178 die "missing ubuntu-standard package '$pkg'\n";
1184 # download/cache all files first
1185 $self->cache_packages ($required);
1186 $self->cache_packages ($important);
1187 $self->cache_packages ($standard);
1189 my $rootdir = $self->{rootfs
};
1191 # extract required packages first
1192 $self->logmsg ("create basic environment\n");
1193 foreach my $p (@$required) {
1194 my $filename = $self->getpkgfile ($p);
1195 my $content = $self->run_command("ar -t '$self->{cachedir}/$filename'", undef, 1);
1196 if ($content =~ m/^data.tar.xz$/m) {
1197 $self->run_command ("ar -p '$self->{cachedir}/$filename' data.tar.xz | tar -C '$rootdir' -xJf -");
1199 $self->run_command ("ar -p '$self->{cachedir}/$filename' data.tar.gz | tar -C '$rootdir' -xzf -");
1204 my $data = "Package: dpkg\n" .
1205 "Version: $pkginfo->{dpkg}->{version}\n" .
1206 "Status: install ok installed\n";
1208 write_file
($data, "$rootdir/var/lib/dpkg/status");
1209 write_file
("", "$rootdir/var/lib/dpkg/info/dpkg.list");
1210 write_file
("", "$rootdir/var/lib/dpkg/available");
1213 foreach my $ss (@{$self->{sources
}}) {
1214 my $url = $ss->{source
};
1215 my $comp = join (' ', @{$ss->{comp
}});
1216 $data .= "deb $url $ss->{suite} $comp\n\n";
1219 write_file
($data, "$rootdir/etc/apt/sources.list");
1221 $data = "# UNCONFIGURED FSTAB FOR BASE SYSTEM\n";
1222 write_file
($data, "$rootdir/etc/fstab", 0644);
1224 write_file
("localhost\n", "$rootdir/etc/hostname", 0644);
1226 # avoid warnings about non-existent resolv.conf
1227 write_file
("", "$rootdir/etc/resolv.conf", 0644);
1229 $data = "auto lo\niface lo inet loopback\n";
1230 write_file
($data, "$rootdir/etc/network/interfaces", 0644);
1233 $self->run_command ("tar xzf '$devicetar' -C '$rootdir'");
1235 # avoid warnings about missing default locale
1236 write_file
("LANG=\"C\"\n", "$rootdir/etc/default/locale", 0644);
1239 rename ("$rootdir/sbin/init", "$rootdir/sbin/init.org");
1240 $self->run_command ("cp '$fake_init' '$rootdir/sbin/init'");
1242 $self->run_command ("cp '$default_env' '$rootdir/sbin/defenv'");
1244 $self->run_command ("lxc-start -n $veid -f $self->{veconffile}");
1246 $self->logmsg ("initialize ld cache\n");
1247 $self->ve_command ("/sbin/ldconfig");
1248 $self->run_command ("ln -sf mawk '$rootdir/usr/bin/awk'");
1250 $self->logmsg ("installing packages\n");
1252 $self->ve_dpkg ('install', 'base-files', 'base-passwd');
1254 $self->ve_dpkg ('install', 'dpkg');
1256 $self->run_command ("ln -sf /usr/share/zoneinfo/UTC '$rootdir/etc/localtime'");
1258 $self->run_command ("ln -sf bash '$rootdir/bin/sh'");
1260 $self->ve_dpkg ('install', 'libc6');
1261 $self->ve_dpkg ('install', 'perl-base');
1263 unlink "$rootdir/usr/bin/awk";
1265 $self->ve_dpkg ('install', 'mawk');
1266 $self->ve_dpkg ('install', 'debconf');
1268 # unpack required packages
1269 foreach my $p (@$required) {
1270 $self->ve_dpkg ('unpack', $p);
1273 rename ("$rootdir/sbin/init.org", "$rootdir/sbin/init");
1274 $self->ve_divert_add ("/sbin/init");
1275 $self->run_command ("cp '$fake_init' '$rootdir/sbin/init'");
1277 # disable service activation
1278 $self->ve_divert_add ("/usr/sbin/policy-rc.d");
1279 $data = "#!/bin/sh\nexit 101\n";
1280 write_file
($data, "$rootdir/usr/sbin/policy-rc.d", 755);
1282 # disable start-stop-daemon
1283 $self->ve_divert_add ("/sbin/start-stop-daemon");
1287 echo \"Warning: Fake start-stop-daemon called, doing nothing\"
1289 write_file
($data, "$rootdir/sbin/start-stop-daemon", 0755);
1292 $self->ve_divert_add ("/sbin/udevd");
1294 if ($suite eq 'etch') {
1295 # disable apache2 startup
1296 write_file
("NO_START=1\n", "$rootdir/etc/default/apache2");
1299 $self->logmsg ("configure required packages\n");
1300 $self->ve_command ("dpkg --force-confold --skip-same-version --configure -a");
1302 # set postfix defaults
1303 if ($mta eq 'postfix') {
1304 $data = "postfix postfix/main_mailer_type select Local only\n";
1305 $self->ve_debconfig_set ($data);
1307 $data = "postmaster: root\nwebmaster: root\n";
1308 write_file
($data, "$rootdir/etc/aliases");
1311 if ($suite eq 'jaunty') {
1312 # jaunty does not create /var/run/network, so network startup fails.
1313 # so we do not use tmpfs for /var/run and /var/lock
1314 $self->run_command ("sed -e 's/RAMRUN=yes/RAMRUN=no/' -e 's/RAMLOCK=yes/RAMLOCK=no/' -i $rootdir/etc/default/rcS");
1315 # and create the directory here
1316 $self->run_command ("mkdir $rootdir/var/run/network");
1319 # unpack base packages
1320 foreach my $p (@$important) {
1321 $self->ve_dpkg ('unpack', $p);
1325 $self->ve_command ("ifconfig lo up");
1327 $self->logmsg ("configure important packages\n");
1328 $self->ve_command ("dpkg --force-confold --skip-same-version --configure -a");
1330 if (-d
"$rootdir/etc/event.d") {
1331 unlink <$rootdir/etc/event.d
/tty
*>;
1334 if (-f
"$rootdir/etc/inittab") {
1335 $self->run_command ("sed -i -e '/getty\\s38400\\stty[23456]/d' '$rootdir/etc/inittab'");
1338 # Link /etc/mtab to /proc/mounts, so df and friends will work:
1339 unlink "$rootdir/etc/mtab";
1340 $self->ve_command ("ln -s /proc/mounts /etc/mtab");
1343 $self->ve_command ("usermod -L root");
1345 if ($mta eq 'postfix') {
1346 $data = "postfix postfix/main_mailer_type select No configuration\n";
1347 $self->ve_debconfig_set ($data);
1349 unlink "$rootdir/etc/mailname";
1350 write_file
($postfix_main_cf, "$rootdir/etc/postfix/main.cf");
1353 if (!$opts->{minimal
}) {
1354 # unpack standard packages
1355 foreach my $p (@$standard) {
1356 $self->ve_dpkg ('unpack', $p);
1359 $self->logmsg ("configure standard packages\n");
1360 $self->ve_command ("dpkg --force-confold --skip-same-version --configure -a");
1363 # disable HWCLOCK access
1364 $self->run_command ("echo 'HWCLOCKACCESS=no' >> '$rootdir/etc/default/rcS'");
1367 $self->ve_divert_add ("/usr/sbin/hald");
1369 # disable /dev/urandom init
1370 $self->run_command ("install -m 0755 '$script_init_urandom' '$rootdir/etc/init.d/urandom'");
1372 if ($suite eq 'etch' || $suite eq 'hardy' || $suite eq 'intrepid' || $suite eq 'jaunty') {
1374 $self->ve_divert_add ("/sbin/klogd");
1377 # remove unnecessays sysctl entries to avoid warnings
1379 $cmd .= ' -e \'s/^\(kernel\.printk.*\)/#\1/\'';
1380 $cmd .= ' -e \'s/^\(kernel\.maps_protect.*\)/#\1/\'';
1381 $cmd .= ' -e \'s/^\(fs\.inotify\.max_user_watches.*\)/#\1/\'';
1382 $cmd .= ' -e \'s/^\(vm\.mmap_min_addr.*\)/#\1/\'';
1383 $cmd .= " -i '$rootdir/etc/sysctl.conf'";
1384 $self->run_command ($cmd);
1386 my $bindv6only = "$rootdir/etc/sysctl.d/bindv6only.conf";
1387 if (-f
$bindv6only) {
1389 $cmd .= ' -e \'s/^\(net\.ipv6\.bindv6only.*\)/#\1/\'';
1390 $cmd .= " -i '$bindv6only'";
1391 $self->run_command ($cmd);
1394 if ($suite eq 'hardy' || $suite eq 'intrepid' || $suite eq 'jaunty') {
1395 # disable tty init (console-setup)
1397 $cmd .= ' -e \'s/^\(ACTIVE_CONSOLES=.*\)/ACTIVE_CONSOLES=/\'';
1398 $cmd .= " -i '$rootdir/etc/default/console-setup'";
1399 $self->run_command ($cmd);
1402 if ($suite eq 'intrepid') {
1403 # remove sysctl setup (avoid warnings at startup)
1404 my $filelist = "$rootdir/etc/sysctl.d/10-console-messages.conf";
1405 $filelist .= " $rootdir/etc/sysctl.d/10-process-security.conf";
1406 $filelist .= " $rootdir/etc/sysctl.d/10-network-security.conf";
1407 $self->run_command ("rm $filelist");
1409 if ($suite eq 'jaunty') {
1410 # remove sysctl setup (avoid warnings at startup)
1411 my $filelist = "$rootdir/etc/sysctl.d/10-console-messages.conf";
1412 $filelist .= " $rootdir/etc/sysctl.d/10-network-security.conf";
1413 $self->run_command ("rm $filelist");
1420 my $veid = $self->{veid
};
1422 my $vestat = $self->ve_status();
1424 if (!$vestat->{exist
}) {
1425 $self->logmsg ("Please create the appliance first (bootstrap)");
1429 if (!$vestat->{running
}) {
1430 $self->run_command ("lxc-start -n $veid -f $self->{veconffile}");
1433 system ("lxc-attach -n $veid --clear-env");
1436 sub ve_mysql_command
{
1437 my ($self, $sql, $password) = @_;
1439 #my $bootstrap = "/usr/sbin/mysqld --bootstrap --user=mysql --skip-grant-tables " .
1440 #"--skip-bdb --skip-innodb --skip-ndbcluster";
1442 $self->ve_command ("mysql", $sql);
1445 sub ve_mysql_bootstrap
{
1446 my ($self, $sql, $password) = @_;
1450 my $suite = $self->{config
}->{suite
};
1452 if ($suite eq 'jessie') {
1453 my $rootdir = $self->{rootfs
};
1454 $self->run_command ("sed -e 's/^key_buffer\\s*=/key_buffer_size =/' -i $rootdir/etc/mysql/my.cnf");
1457 if ($suite eq 'squeeze' || $suite eq 'wheezy' || $suite eq 'jessie') {
1458 $cmd = "/usr/sbin/mysqld --bootstrap --user=mysql --skip-grant-tables";
1461 $cmd = "/usr/sbin/mysqld --bootstrap --user=mysql --skip-grant-tables " .
1462 "--skip-bdb --skip-innodb --skip-ndbcluster";
1465 $self->ve_command ($cmd, $sql);
1468 sub compute_required
{
1469 my ($self, $pkglist) = @_;
1471 my $pkginfo = $self->pkginfo();
1472 my $instpkgs = $self->read_installed ();
1475 __record_provides
($pkginfo, $closure, [keys %$instpkgs]);
1477 return $self->closure ($closure, $pkglist);
1481 my ($self, $opts) = @_;
1483 my @supp = ('7.4', '8.1');
1484 my $pgversion = '8.1';
1486 my $suite = $self->{config
}->{suite
};
1488 if ($suite eq 'lenny' || $suite eq 'hardy' || $suite eq 'intrepid' || $suite eq 'jaunty') {
1491 } elsif ($suite eq 'squeeze') {
1494 } elsif ($suite eq 'wheezy') {
1497 } elsif ($suite eq 'jessie') {
1502 $pgversion = $opts->{version
} if $opts->{version
};
1504 die "unsupported postgres version '$pgversion'\n"
1505 if !grep { $pgversion eq $_; } @supp;
1507 my $rootdir = $self->{rootfs
};
1509 my $required = $self->compute_required (["postgresql-$pgversion"]);
1511 $self->cache_packages ($required);
1513 $self->ve_dpkg ('install', @$required);
1515 my $iscript = "postgresql-$pgversion";
1516 if ($suite eq 'squeeze' || $suite eq 'wheezy' || $suite eq 'jessie') {
1517 $iscript = 'postgresql';
1520 $self->ve_command ("/etc/init.d/$iscript start") if $opts->{start
};
1524 my ($self, $opts) = @_;
1526 my $password = $opts->{password
};
1527 my $rootdir = $self->{rootfs
};
1529 my $suite = $self->{config
}->{suite
};
1532 if ($suite eq 'squeeze') {
1534 } elsif ($suite eq 'wheezy' || $suite eq 'jessie') {
1538 my $required = $self->compute_required (['mysql-common', "mysql-server-$ver"]);
1540 $self->cache_packages ($required);
1542 $self->ve_dpkg ('install', @$required);
1544 # fix security (see /usr/bin/mysql_secure_installation)
1545 my $sql = "DELETE FROM mysql.user WHERE User='';\n" .
1546 "DELETE FROM mysql.user WHERE User='root' AND Host!='localhost';\n" .
1547 "FLUSH PRIVILEGES;\n";
1548 $self->ve_mysql_bootstrap ($sql);
1552 my $rpw = $password eq 'random' ?
'admin' : $password;
1554 my $sql = "USE mysql;\n" .
1555 "UPDATE user SET password=PASSWORD(\"$rpw\") WHERE user
='root';\n" .
1556 "FLUSH PRIVILEGES
;\n";
1557 $self->ve_mysql_bootstrap ($sql);
1559 write_file ("[client
]\nuser
=root
\npassword
=\"$rpw\"\n", "$rootdir/root/.my.cnf", 0600);
1560 if ($password eq 'random') {
1561 $self->install_init_script ($script_mysql_randompw, 2, 20);
1565 $self->ve_command ("/etc/init.d/mysql start") if $opts->{start
};
1569 my ($self, $opts) = @_;
1571 my $memlimit = $opts->{memlimit
};
1572 my $rootdir = $self->{rootfs
};
1574 my $required = $self->compute_required ([qw
(php5 php5-cli libapache2-mod-php5 php5-gd
)]);
1576 $self->cache_packages ($required);
1578 $self->ve_dpkg ('install', @$required);
1581 $self->run_command ("sed -e 's/^\\s*memory_limit\\s*=.*;/memory_limit = ${memlimit}M;/' -i $rootdir/etc/php5/apache2/php.ini");
1586 my ($self, $pkglist, $unpack) = @_;
1588 my $required = $self->compute_required ($pkglist);
1590 $self->cache_packages ($required);
1592 $self->ve_dpkg ($unpack ?
'unpack' : 'install', @$required);
1596 my ($self, $distclean) = @_;
1598 unlink $self->{logfile
};
1599 unlink "$self->{targetname}.tar";
1600 unlink "$self->{targetname}.tar.gz";
1602 $self->ve_destroy ();
1605 rmtree
$self->{cachedir
} if $distclean && !$self->{config
}->{cachedir
};
1607 rmtree
$self->{infodir
};