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";
344 } elsif ($suite eq 'wily') {
345 $config->{ostype
} = "ubuntu-15.10";
346 } elsif ($suite eq 'xenial') {
347 $config->{ostype
} = "ubuntu-16.04";
348 } elsif ($suite eq 'yakkety') {
349 $config->{ostype
} = "ubuntu-16.10";
351 die "unsupported debian suite '$suite'\n";
354 my $name = $config->{name
} || die "no 'name' specified\n";
356 $name =~ m/^[a-z][0-9a-z\-\*\.]+$/ ||
357 die "illegal characters in name '$name'\n";
359 my $version = $config->{version
};
360 die "no 'version' specified\n" if !$version;
361 die "no 'section' specified\n" if !$config->{section
};
362 die "no 'description' specified\n" if !$config->{headline
};
363 die "no 'maintainer' specified\n" if !$config->{maintainer
};
365 if ($name =~ m/^$config->{ostype}/) {
366 $self->{targetname
} = "${name}_${version}_$config->{architecture}";
368 $self->{targetname
} = "$config->{ostype}-${name}_" .
369 "${version}_$config->{architecture}";
372 if (!$config->{source
}) {
373 if ($suite eq 'etch' || $suite eq 'lenny' || $suite eq 'squeeze' ||
374 $suite eq 'wheezy' || $suite eq 'jessie' ) {
375 push @{$config->{source
}}, "http://ftp.debian.org/debian SUITE main contrib";
376 push @{$config->{source
}}, "http://ftp.debian.org/debian SUITE-updates main contrib"
377 if ($suite eq 'squeeze' || $suite eq 'wheezy' || $suite eq 'jessie');
378 push @{$config->{source
}}, "http://security.debian.org SUITE/updates main contrib";
379 } elsif ($suite eq 'hardy' || $suite eq 'intrepid' || $suite eq 'jaunty' ||
380 $suite eq 'xenial' || $suite eq 'wily' || $suite eq 'vivid' ||
381 $suite eq 'trusty' || $suite eq 'precise' || $suite eq 'yakkety') {
382 my $comp = "main restricted universe multiverse";
383 push @{$config->{source
}}, "http://archive.ubuntu.com/ubuntu SUITE $comp";
384 push @{$config->{source
}}, "http://archive.ubuntu.com/ubuntu SUITE-updates $comp";
385 push @{$config->{source
}}, "http://archive.ubuntu.com/ubuntu SUITE-security $comp";
393 foreach my $s (@{$config->{source
}}) {
394 if ($s =~ m
@^\s*((http
|ftp
)://\S
+)\s
+(\S
+)((\s
+(\S
+))+)$@) {
395 my ($url, $su, $components) = ($1, $3, $4);
396 $su =~ s/SUITE/$suite/;
397 $components =~ s/^\s+//;
398 $components =~ s/\s+$//;
400 foreach my $co (split (/\s+/, $components)) {
403 $ca = ['main'] if !$ca;
411 die "syntax error in source spezification '$s'\n";
415 foreach my $m (@{$config->{mirror
}}) {
416 if ($m =~ m
@^\s*((http
|ftp
)://\S
+)\s
*=>\s
*((http
|ftp
)://\S
+)\s
*$@) {
417 my ($ms, $md) = ($1, $3);
419 foreach my $ss (@$sources) {
420 if ($ss->{source
} eq $ms) {
426 die "unusable mirror $ms\n" if !$found;
428 die "syntax error in mirror spezification '$m'\n";
432 $self->{sources
} = $sources;
434 $self->{infodir
} = "info";
436 $self->__allocate_ve ();
438 $self->{cachedir
} = ($config->{cachedir
} || 'cache') . "/$suite";;
440 my $incl = [qw
(less ssh openssh-server logrotate
)];
442 my $excl = [qw
(modutils reiserfsprogs ppp pppconfig pppoe
443 pppoeconf nfs-common mtools ntp
)];
445 # ubuntu has too many dependencies on udev, so
446 # we cannot exclude it (instead we disable udevd)
448 if ($suite eq 'vivid' || $suite eq 'wily' || $suite eq 'xenial' ||
449 $suite eq 'yakkety') {
450 push @$incl, 'isc-dhcp-client';
451 push @$excl, qw(libmodule-build-perl);
452 } elsif ($suite eq 'trusty') {
453 push @$excl, qw(systemd systemd-services libpam-systemd libsystemd-daemon0 memtest86+);
454 } elsif ($suite eq 'precise') {
455 push @$excl, qw(systemd systemd-services libpam-systemd libsystemd-daemon0 memtest86+ ubuntu-standard);
456 } elsif ($suite eq 'hardy') {
457 push @$excl, qw(kbd);
458 push @$excl, qw(apparmor apparmor-utils ntfs-3g
460 } elsif ($suite eq 'intrepid' || $suite eq 'jaunty') {
461 push @$excl, qw(apparmor apparmor-utils libapparmor1 libapparmor-perl
462 libntfs-3g28 ntfs-3g friendly-recovery);
463 } elsif ($suite eq 'jessie') {
464 push @$incl, 'sysvinit-core'; # avoid systemd and udev
465 push @$incl, 'libperl4-corelibs-perl'; # to make lsof happy
466 push @$excl, qw(systemd systemd-sysv udev module-init-tools pciutils hdparm
469 push @$excl, qw(udev module-init-tools pciutils hdparm
473 $self->{incl
} = $incl;
474 $self->{excl
} = $excl;
482 my $infodir = $self->{infodir
};
483 my $arch = $self->{config
}->{architecture
};
489 my $logfd = $self->{logfd
} = IO
::File-
>new (">$self->{logfile}") ||
490 die "unable to open log file";
492 foreach my $ss (@{$self->{sources
}}) {
493 my $src = $ss->{mirror
} || $ss->{source
};
494 my $path = "dists/$ss->{suite}/Release";
495 my $url = "$src/$path";
496 my $target = __url_to_filename
("$ss->{source}/$path");
498 $self->download ($url, "$infodir/$target");
499 $self->download ("$url.gpg", "$infodir/$target.gpg");
500 # fixme: impl. verify (needs --keyring option)
504 warn "Release info ignored\n";
506 foreach my $comp (@{$ss->{comp
}}) {
507 $path = "dists/$ss->{suite}/$comp/binary-$arch/Packages.gz";
508 $target = "$infodir/" . __url_to_filename
("$ss->{source}/$path");
509 my $pkgsrc = "$src/$path";
510 $self->download ($pkgsrc, $target);
511 $self->run_command ("gzip -d '$target'");
517 my ($self, $filename, $size) = @_;
519 my $config = $self->{config
};
523 $data .= "Name: $config->{name}\n";
524 $data .= "Version: $config->{version}\n";
525 $data .= "Type: lxc\n";
526 $data .= "OS: $config->{ostype}\n";
527 $data .= "Section: $config->{section}\n";
528 $data .= "Maintainer: $config->{maintainer}\n";
529 $data .= "Architecture: $config->{architecture}\n";
530 $data .= "Installed-Size: $size\n";
533 $data .= "Infopage: $config->{infopage}\n" if $config->{infopage
};
534 $data .= "ManageUrl: $config->{manageurl}\n" if $config->{manageurl
};
535 $data .= "Certified: $config->{certified}\n" if $config->{certified
};
538 $data .= "Description: $config->{headline}\n";
539 $data .= "$config->{description}\n" if $config->{description
};
541 write_file
($data, $filename, 0644);
545 my ($self, $opts) = @_;
547 my $suite = $self->{config
}->{suite
};
548 my $infodir = $self->{infodir
};
549 my $arch = $self->{config
}->{architecture
};
551 my $instpkgs = $self->read_installed ();
552 my $pkginfo = $self->pkginfo();
553 my $veid = $self->{veid
};
554 my $conffile = $self->{veconffile
};
555 my $rootdir = $self->{rootfs
};
557 my $vestat = $self->ve_status();
558 die "ve not running - unable to finalize\n" if !$vestat->{running
};
561 if (-f
"$rootdir/etc/init.d/mysql") {
562 $self->ve_command ("/etc/init.d/mysql stop");
565 if (!($opts->{keepmycnf
} || (-f
"$rootdir/etc/init.d/mysql_randompw"))) {
566 unlink "$rootdir/root/.my.cnf";
569 if ($suite eq 'etch') {
570 # enable apache2 startup
571 if ($instpkgs->{apache2
}) {
572 write_file
("NO_START=0\n", "$rootdir/etc/default/apache2");
574 unlink "$rootdir/etc/default/apache2";
577 $self->logmsg ("cleanup package status\n");
578 # prevent auto selection of all standard, required or important
579 # packages which are not installed
580 foreach my $pkg (keys %$pkginfo) {
581 my $pri = $pkginfo->{$pkg}->{priority
};
582 if ($pri && ($pri eq 'required' || $pri eq 'important'
583 || $pri eq 'standard')) {
584 if (!$instpkgs->{$pkg}) {
585 $self->ve_dpkg_set_selection ($pkg, 'purge');
590 $self->ve_command ("apt-get clean");
592 $self->logmsg ("update available package list\n");
594 $self->ve_command ("dpkg --clear-avail");
595 foreach my $ss (@{$self->{sources
}}) {
596 my $relsrc = __url_to_filename
("$ss->{source}/dists/$ss->{suite}/Release");
597 if (-f
"$infodir/$relsrc" && -f
"$infodir/$relsrc.gpg") {
598 $self->run_command ("cp '$infodir/$relsrc' '$rootdir/var/lib/apt/lists/$relsrc'");
599 $self->run_command ("cp '$infodir/$relsrc.gpg' '$rootdir/var/lib/apt/lists/$relsrc.gpg'");
601 foreach my $comp (@{$ss->{comp
}}) {
602 my $src = __url_to_filename
("$ss->{source}/dists/$ss->{suite}/" .
603 "$comp/binary-$arch/Packages");
604 my $target = "/var/lib/apt/lists/$src";
605 $self->run_command ("cp '$infodir/$src' '$rootdir/$target'");
606 $self->ve_command ("dpkg --merge-avail '$target'");
610 # set dselect default method
611 write_file
("apt apt\n", "$rootdir/var/lib/dpkg/cmethopt");
613 $self->ve_divert_remove ("/usr/sbin/policy-rc.d");
615 $self->ve_divert_remove ("/sbin/start-stop-daemon");
617 $self->ve_divert_remove ("/sbin/init");
619 # finally stop the VE
620 $self->run_command ("lxc-stop -n $veid --rcfile $conffile --kill");
622 unlink "$rootdir/sbin/defenv";
624 unlink <$rootdir/root/dead.letter
*>;
626 unlink "$rootdir/var/log/init.log";
628 unlink "$rootdir/aquota.group";
630 unlink "$rootdir/aquota.user";
632 write_file
("", "$rootdir/var/log/syslog");
634 $self->logmsg ("detecting final size: ");
636 my $sizestr = $self->run_command ("du -sm $rootdir", undef, 1);
638 if ($sizestr =~ m/^(\d+)\s+\Q$rootdir\E$/) {
641 die "unable to detect size\n";
643 $self->logmsg ("$size MB\n");
645 $self->write_config ("$rootdir/etc/appliance.info", $size);
647 $self->logmsg ("creating final appliance archive\n");
649 my $target = "$self->{targetname}.tar";
653 $self->run_command ("tar cpf $target --numeric-owner -C '$rootdir' ./etc/appliance.info");
654 $self->run_command ("tar rpf $target --numeric-owner -C '$rootdir' --exclude ./etc/appliance.info .");
655 $self->run_command ("gzip $target");
661 my $rootdir = $self->{rootfs
};
663 my $pkgfilelist = "$rootdir/var/lib/dpkg/status";
665 open (PKGLST
, "<$pkgfilelist") ||
666 die "unable to open '$pkgfilelist'";
670 while (my $rec = <PKGLST
>) {
676 while ($rec =~ s/^([^:]+):\s+(.*)\s*\n//) {
680 my $pkg = $res->{'package'};
681 if (my $status = $res->{status
}) {
682 my @sa = split (/\s+/, $status);
684 if ($stat && ($stat ne 'purge')) {
685 $pkglist->{$pkg} = $res;
698 my $veid = $self->{veid
};
700 my $res = { running
=> 0 };
702 $res->{exist
} = 1 if -d
"$self->{rootfs}/usr";
704 my $filename = "/proc/net/unix";
706 # similar test is used by lcxcontainers.c: list_active_containers
707 my $fh = IO
::File-
>new ($filename, "r");
710 while (defined(my $line = <$fh>)) {
711 if ($line =~ m/^[a-f0-9]+:\s\S+\s\S+\s\S+\s\S+\s\S+\s\d+\s(\S+)$/) {
713 if ($path =~ m!^@/\S+/$veid/command$!) {
724 my ($self, $cmd, $input) = @_;
726 my $veid = $self->{veid
};
727 my $conffile = $self->{veconffile
};
729 if (ref ($cmd) eq 'ARRAY') {
730 unshift @$cmd, 'lxc-attach', '-n', $veid, '--rcfile', $conffile, '--clear-env', '--', 'defenv';
731 $self->run_command ($cmd, $input);
733 $self->run_command ("lxc-attach -n $veid --rcfile $conffile --clear-env -- defenv $cmd", $input);
737 # like ve_command, but pipes stdin correctly
739 my ($self, @cmd) = @_;
741 my $veid = $self->{veid
};
742 my $conffile = $self->{veconffile
};
745 my $pid = open2
($reader, "<&STDIN", 'lxc-attach', '-n', $veid, '--rcfile', $conffile, '--',
746 'defenv', @cmd) || die "unable to exec command";
748 while (defined (my $line = <$reader>)) {
749 $self->logmsg ($line);
755 die "ve_exec failed - status $rc\n" if $rc != 0;
759 my ($self, $filename) = @_;
761 $self->ve_command ("dpkg-divert --add --divert '$filename.distrib' " .
762 "--rename '$filename'");
764 sub ve_divert_remove
{
765 my ($self, $filename) = @_;
767 my $rootdir = $self->{rootfs
};
769 unlink "$rootdir/$filename";
770 $self->ve_command ("dpkg-divert --remove --rename '$filename'");
773 sub ve_debconfig_set
{
774 my ($self, $dcdata) = @_;
776 my $rootdir = $self->{rootfs
};
777 my $cfgfile = "/tmp/debconf.txt";
778 write_file
($dcdata, "$rootdir/$cfgfile");
779 $self->ve_command ("debconf-set-selections $cfgfile");
780 unlink "$rootdir/$cfgfile";
783 sub ve_dpkg_set_selection
{
784 my ($self, $pkg, $status) = @_;
786 $self->ve_command ("dpkg --set-selections", "$pkg $status");
790 my ($self, $cmd, @pkglist) = @_;
792 return if !scalar (@pkglist);
794 my $pkginfo = $self->pkginfo();
796 my $rootdir = $self->{rootfs
};
797 my $cachedir = $self->{cachedir
};
801 foreach my $pkg (@pkglist) {
802 my $filename = $self->getpkgfile ($pkg);
803 $self->run_command ("cp '$cachedir/$filename' '$rootdir/$filename'");
804 push @files, "/$filename";
805 $self->logmsg ("$cmd: $pkg\n");
808 my $fl = join (' ', @files);
810 if ($cmd eq 'install') {
811 $self->ve_command ("dpkg --force-depends --force-confold --install $fl");
812 } elsif ($cmd eq 'unpack') {
813 $self->ve_command ("dpkg --force-depends --unpack $fl");
815 die "internal error";
818 foreach my $fn (@files) { unlink "$rootdir$fn"; }
824 my $veid = $self->{veid
}; # fixme
825 my $conffile = $self->{veconffile
};
827 my $vestat = $self->ve_status();
828 if ($vestat->{running
}) {
829 $self->run_command ("lxc-stop -n $veid --rcfile $conffile --kill");
832 rmtree
$self->{rootfs
};
833 unlink $self->{veconffile
};
839 my $veid = $self->{veid
};
840 my $conffile = $self->{veconffile
};
842 $self->logmsg ("initialize VE $veid\n");
844 my $vestat = $self->ve_status();
845 if ($vestat->{running
}) {
846 $self->run_command ("lxc-stop -n $veid --rcfile $conffile --kill");
849 rmtree
$self->{rootfs
};
850 mkpath
$self->{rootfs
};
853 sub __deb_version_cmp
{
854 my ($cur, $op, $new) = @_;
856 if (system("dpkg", "--compare-versions", $cur, $op, $new) == 0) {
863 sub __parse_packages
{
864 my ($pkginfo, $filename, $src) = @_;
867 open (PKGLST
, "<$filename") ||
868 die "unable to open '$filename'";
870 while (my $rec = <PKGLST
>) {
877 while ($rec =~ s/^([^:]+):\s+(.*)\s*\n//) {
881 my $pkg = $res->{'package'};
882 if ($pkg && $res->{'filename'}) {
884 if (my $info = $pkginfo->{$pkg}) {
885 $cur = $info->{version
};
887 my $new = $res->{version
};
888 if (!$cur || __deb_version_cmp
($cur, 'lt', $new)) {
890 $res->{url
} = "$src/$res->{'filename'}";
892 die "no url for package '$pkg'" if !$res->{url
};
894 $pkginfo->{$pkg} = $res;
905 return $self->{pkginfo
} if $self->{pkginfo
};
907 my $infodir = $self->{infodir
};
908 my $arch = $self->{config
}->{architecture
};
910 my $availfn = "$infodir/available";
915 # reading 'available' is faster, because it only contains latest version
916 # (no need to do slow version compares)
918 __parse_packages
($pkginfo, $availfn);
919 $self->{pkginfo
} = $pkginfo;
923 $self->logmsg ("generating available package list\n");
925 foreach my $ss (@{$self->{sources
}}) {
926 foreach my $comp (@{$ss->{comp
}}) {
927 my $url = "$ss->{source}/dists/$ss->{suite}/$comp/binary-$arch/Packages";
928 my $pkgfilelist = "$infodir/" . __url_to_filename
($url);
930 my $src = $ss->{mirror
} || $ss->{source
};
932 __parse_packages
($pkginfo, $pkgfilelist, $src);
936 if (my $dep = $self->{config
}->{depends
}) {
937 foreach my $d (split (/,/, $dep)) {
938 if ($d =~ m/^\s*(\S+)\s*(\((\S+)\s+(\S+)\)\s*)?$/) {
939 my ($pkg, $op, $rver) = ($1, $3, $4);
940 $self->logmsg ("checking dependencies: $d\n");
941 my $info = $pkginfo->{$pkg};
942 die "package '$pkg' not available\n" if !$info;
944 my $cver = $info->{version
};
945 if (!__deb_version_cmp
($cver, $op, $rver)) {
946 die "detected wrong version '$cver'\n";
950 die "syntax error in depends field";
955 $self->{pkginfo
} = $pkginfo;
957 my $tmpfn = "$availfn.tmp$$";
958 my $fd = IO
::File-
>new (">$tmpfn");
959 foreach my $pkg (sort keys %$pkginfo) {
960 my $info = $pkginfo->{$pkg};
961 print $fd "package: $pkg\n";
962 foreach my $k (sort keys %$info) {
963 next if $k eq 'description';
964 next if $k eq 'package';
966 print $fd "$k: $v\n" if $v;
968 print $fd "description: $info->{description}\n" if $info->{description
};
973 rename ($tmpfn, $availfn);
978 sub __record_provides
{
979 my ($pkginfo, $closure, $list, $skipself) = @_;
981 foreach my $pname (@$list) {
982 my $info = $pkginfo->{$pname};
983 # fixme: if someone install packages directly using dpkg, there
984 # is no entry in 'available', only in 'status'. In that case, we
985 # should extract info from $instpkgs
987 warn "hint: ignoring provides for '$pname' - package not in 'available' list.\n";
990 if (my $prov = $info->{provides
}) {
991 my @pl = split (',', $prov);
992 foreach my $p (@pl) {
994 if (!($skipself && (grep { $1 eq $_ } @$list))) {
999 $closure->{$pname} = 1 if !$skipself;
1004 my ($self, $closure, $list) = @_;
1006 my $pkginfo = $self->pkginfo();
1008 # first, record provided packages
1009 __record_provides
($pkginfo, $closure, $list, 1);
1014 # then resolve dependencies
1015 foreach my $pname (@$list) {
1016 __closure_single
($pkginfo, $closure, $pkghash, $pkglist, $pname, $self->{excl
});
1022 sub __closure_single
{
1023 my ($pkginfo, $closure, $pkghash, $pkglist, $pname, $excl) = @_;
1027 $pname =~ s/:any$//;
1029 return if $closure->{$pname};
1031 my $info = $pkginfo->{$pname} || die "no such package '$pname'";
1033 my $dep = $info->{depends
};
1034 my $predep = $info->{'pre-depends'};
1036 my $size = $info->{size
};
1037 my $url = $info->{url
};
1039 $url || die "$pname: no url for package '$pname'";
1041 if (!$pkghash->{$pname}) {
1042 unshift @$pkglist, $pname;
1043 $pkghash->{$pname} = 1;
1046 __record_provides
($pkginfo, $closure, [$pname]) if $info->{provides
};
1048 $closure->{$pname} = 1;
1054 push @l, split (/,/, $predep) if $predep;
1055 push @l, split (/,/, $dep) if $dep;
1057 DEPEND
: foreach my $p (@l) {
1058 my @l1 = split (/\|/, $p);
1059 foreach my $p1 (@l1) {
1060 if ($p1 =~ m/^\s*(\S+).*/) {
1061 #printf (STDERR "$pname: $p --> $1\n");
1062 if ($closure->{$1}) {
1063 next DEPEND
; # dependency already met
1067 # search for non-excluded alternative
1069 foreach my $p1 (@l1) {
1070 if ($p1 =~ m/^\s*(\S+).*/) {
1071 next if grep { $1 eq $_ } @$excl;
1076 die "package '$pname' depends on exclusion '$p'\n" if !$found;
1078 #printf (STDERR "$pname: $p --> $found\n");
1080 __closure_single
($pkginfo, $closure, $pkghash, $pkglist, $found, $excl);
1084 sub cache_packages
{
1085 my ($self, $pkglist) = @_;
1087 foreach my $pkg (@$pkglist) {
1088 $self->getpkgfile ($pkg);
1093 my ($self, $pkg) = @_;
1095 my $pkginfo = $self->pkginfo();
1096 my $info = $pkginfo->{$pkg} || die "no such package '$pkg'";
1097 my $cachedir = $self->{cachedir
};
1099 my $url = $info->{url
};
1102 if ($url =~ m
|/([^/]+.deb
)$|) {
1105 die "internal error";
1108 return $filename if -f
"$cachedir/$filename";
1112 $self->download ($url, "$cachedir/$filename");
1117 sub install_init_script
{
1118 my ($self, $script, $runlevel, $prio) = @_;
1120 my $suite = $self->{config
}->{suite
};
1121 my $rootdir = $self->{rootfs
};
1123 my $base = basename
($script);
1124 my $target = "$rootdir/etc/init.d/$base";
1126 $self->run_command ("install -m 0755 '$script' '$target'");
1127 if ($suite eq 'etch' || $suite eq 'lenny') {
1128 $self->ve_command ("update-rc.d $base start $prio $runlevel .");
1129 } elsif ($suite eq 'xenial' || $suite eq 'wily' || $suite eq 'vivid' ||
1130 $suite eq 'yakkety') {
1131 die "unable to install init script (system uses systemd)\n";
1132 } elsif ($suite eq 'trusty' || $suite eq 'precise') {
1133 die "unable to install init script (system uses upstart)\n";
1135 $self->ve_command ("insserv $base");
1142 my ($self, $opts) = @_;
1144 my $pkginfo = $self->pkginfo();
1145 my $veid = $self->{veid
};
1146 my $suite = $self->{config
}->{suite
};
1148 my $important = [ @{$self->{incl
}} ];
1152 my $mta = $opts->{exim
} ?
'exim' : 'postfix';
1154 if ($mta eq 'postfix') {
1155 push @$important, "postfix";
1158 foreach my $p (sort keys %$pkginfo) {
1159 next if grep { $p eq $_ } @{$self->{excl
}};
1160 my $pri = $pkginfo->{$p}->{priority
};
1162 next if $mta ne 'exim' && $p =~ m/exim/;
1163 next if $p =~ m/(selinux|semanage|policycoreutils)/;
1165 push @$required, $p if $pri eq 'required';
1166 push @$important, $p if $pri eq 'important';
1167 push @$standard, $p if $pri eq 'standard' && !$opts->{minimal
};
1171 $required = $self->closure ($closure, $required);
1172 $important = $self->closure ($closure, $important);
1174 if (!$opts->{minimal
}) {
1175 push @$standard, 'xbase-clients';
1176 $standard = $self->closure ($closure, $standard);
1179 # test if we have all 'ubuntu-minimal' and 'ubuntu-standard' packages
1180 # except those explicitly excluded
1181 if ($suite eq 'hardy' || $suite eq 'intrepid' || $suite eq 'jaunty') {
1182 my $mdeps = $pkginfo->{'ubuntu-minimal'}->{depends
};
1183 foreach my $d (split (/,/, $mdeps)) {
1184 if ($d =~ m/^\s*(\S+)$/) {
1186 next if $closure->{$pkg};
1187 next if grep { $pkg eq $_ } @{$self->{excl
}};
1188 die "missing ubuntu-minimal package '$pkg'\n";
1191 if (!$opts->{minimal
}) {
1192 $mdeps = $pkginfo->{'ubuntu-standard'}->{depends
};
1193 foreach my $d (split (/,/, $mdeps)) {
1194 if ($d =~ m/^\s*(\S+)$/) {
1196 next if $closure->{$pkg};
1197 next if grep { $pkg eq $_ } @{$self->{excl
}};
1198 die "missing ubuntu-standard package '$pkg'\n";
1204 # download/cache all files first
1205 $self->cache_packages ($required);
1206 $self->cache_packages ($important);
1207 $self->cache_packages ($standard);
1209 my $rootdir = $self->{rootfs
};
1211 # extract required packages first
1212 $self->logmsg ("create basic environment\n");
1213 foreach my $p (@$required) {
1214 my $filename = $self->getpkgfile ($p);
1215 my $content = $self->run_command("ar -t '$self->{cachedir}/$filename'", undef, 1);
1216 if ($content =~ m/^data.tar.xz$/m) {
1217 $self->run_command ("ar -p '$self->{cachedir}/$filename' data.tar.xz | tar -C '$rootdir' -xJf -");
1219 $self->run_command ("ar -p '$self->{cachedir}/$filename' data.tar.gz | tar -C '$rootdir' -xzf -");
1224 my $data = "Package: dpkg\n" .
1225 "Version: $pkginfo->{dpkg}->{version}\n" .
1226 "Status: install ok installed\n";
1228 write_file
($data, "$rootdir/var/lib/dpkg/status");
1229 write_file
("", "$rootdir/var/lib/dpkg/info/dpkg.list");
1230 write_file
("", "$rootdir/var/lib/dpkg/available");
1233 foreach my $ss (@{$self->{sources
}}) {
1234 my $url = $ss->{source
};
1235 my $comp = join (' ', @{$ss->{comp
}});
1236 $data .= "deb $url $ss->{suite} $comp\n\n";
1239 write_file
($data, "$rootdir/etc/apt/sources.list");
1241 $data = "# UNCONFIGURED FSTAB FOR BASE SYSTEM\n";
1242 write_file
($data, "$rootdir/etc/fstab", 0644);
1244 write_file
("localhost\n", "$rootdir/etc/hostname", 0644);
1246 # avoid warnings about non-existent resolv.conf
1247 write_file
("", "$rootdir/etc/resolv.conf", 0644);
1249 if ($suite eq 'yakkety' || $suite eq 'xenial' || $suite eq 'wily') {
1250 # no need to configure loopback device
1252 $data = "auto lo\niface lo inet loopback\n";
1253 write_file
($data, "$rootdir/etc/network/interfaces", 0644);
1257 $self->run_command ("tar xzf '$devicetar' -C '$rootdir'");
1259 # avoid warnings about missing default locale
1260 write_file
("LANG=\"C\"\n", "$rootdir/etc/default/locale", 0644);
1263 rename ("$rootdir/sbin/init", "$rootdir/sbin/init.org");
1264 $self->run_command ("cp '$fake_init' '$rootdir/sbin/init'");
1266 $self->run_command ("cp '$default_env' '$rootdir/sbin/defenv'");
1268 $self->run_command ("lxc-start -n $veid -f $self->{veconffile}");
1270 $self->logmsg ("initialize ld cache\n");
1271 $self->ve_command ("/sbin/ldconfig");
1272 $self->run_command ("ln -sf mawk '$rootdir/usr/bin/awk'");
1274 $self->logmsg ("installing packages\n");
1276 $self->ve_dpkg ('install', 'base-files', 'base-passwd');
1278 $self->ve_dpkg ('install', 'dpkg');
1280 $self->run_command ("ln -sf /usr/share/zoneinfo/UTC '$rootdir/etc/localtime'");
1282 $self->run_command ("ln -sf bash '$rootdir/bin/sh'");
1284 $self->ve_dpkg ('install', 'libc6');
1285 $self->ve_dpkg ('install', 'perl-base');
1287 unlink "$rootdir/usr/bin/awk";
1289 $self->ve_dpkg ('install', 'mawk');
1290 $self->ve_dpkg ('install', 'debconf');
1292 # unpack required packages
1293 foreach my $p (@$required) {
1294 $self->ve_dpkg ('unpack', $p);
1297 rename ("$rootdir/sbin/init.org", "$rootdir/sbin/init");
1298 $self->ve_divert_add ("/sbin/init");
1299 $self->run_command ("cp '$fake_init' '$rootdir/sbin/init'");
1301 # disable service activation
1302 $self->ve_divert_add ("/usr/sbin/policy-rc.d");
1303 $data = "#!/bin/sh\nexit 101\n";
1304 write_file
($data, "$rootdir/usr/sbin/policy-rc.d", 755);
1306 # disable start-stop-daemon
1307 $self->ve_divert_add ("/sbin/start-stop-daemon");
1311 echo \"Warning: Fake start-stop-daemon called, doing nothing\"
1313 write_file
($data, "$rootdir/sbin/start-stop-daemon", 0755);
1316 $self->ve_divert_add ("/sbin/udevd");
1318 if ($suite eq 'etch') {
1319 # disable apache2 startup
1320 write_file
("NO_START=1\n", "$rootdir/etc/default/apache2");
1323 $self->logmsg ("configure required packages\n");
1324 $self->ve_command ("dpkg --force-confold --skip-same-version --configure -a");
1326 # set postfix defaults
1327 if ($mta eq 'postfix') {
1328 $data = "postfix postfix/main_mailer_type select Local only\n";
1329 $self->ve_debconfig_set ($data);
1331 $data = "postmaster: root\nwebmaster: root\n";
1332 write_file
($data, "$rootdir/etc/aliases");
1335 if ($suite eq 'jaunty') {
1336 # jaunty does not create /var/run/network, so network startup fails.
1337 # so we do not use tmpfs for /var/run and /var/lock
1338 $self->run_command ("sed -e 's/RAMRUN=yes/RAMRUN=no/' -e 's/RAMLOCK=yes/RAMLOCK=no/' -i $rootdir/etc/default/rcS");
1339 # and create the directory here
1340 $self->run_command ("mkdir $rootdir/var/run/network");
1343 # unpack base packages
1344 foreach my $p (@$important) {
1345 $self->ve_dpkg ('unpack', $p);
1349 $self->ve_command ("ifconfig lo up");
1351 $self->logmsg ("configure important packages\n");
1352 $self->ve_command ("dpkg --force-confold --skip-same-version --configure -a");
1354 if (-d
"$rootdir/etc/event.d") {
1355 unlink <$rootdir/etc/event.d
/tty
*>;
1358 if (-f
"$rootdir/etc/inittab") {
1359 $self->run_command ("sed -i -e '/getty\\s38400\\stty[23456]/d' '$rootdir/etc/inittab'");
1362 # Link /etc/mtab to /proc/mounts, so df and friends will work:
1363 unlink "$rootdir/etc/mtab";
1364 $self->ve_command ("ln -s /proc/mounts /etc/mtab");
1367 $self->ve_command ("usermod -L root");
1369 if ($mta eq 'postfix') {
1370 $data = "postfix postfix/main_mailer_type select No configuration\n";
1371 $self->ve_debconfig_set ($data);
1373 unlink "$rootdir/etc/mailname";
1374 write_file
($postfix_main_cf, "$rootdir/etc/postfix/main.cf");
1377 if (!$opts->{minimal
}) {
1378 # unpack standard packages
1379 foreach my $p (@$standard) {
1380 $self->ve_dpkg ('unpack', $p);
1383 $self->logmsg ("configure standard packages\n");
1384 $self->ve_command ("dpkg --force-confold --skip-same-version --configure -a");
1387 # disable HWCLOCK access
1388 $self->run_command ("echo 'HWCLOCKACCESS=no' >> '$rootdir/etc/default/rcS'");
1391 $self->ve_divert_add ("/usr/sbin/hald");
1393 # disable /dev/urandom init
1394 $self->run_command ("install -m 0755 '$script_init_urandom' '$rootdir/etc/init.d/urandom'");
1396 if ($suite eq 'etch' || $suite eq 'hardy' || $suite eq 'intrepid' || $suite eq 'jaunty') {
1398 $self->ve_divert_add ("/sbin/klogd");
1401 # remove unnecessays sysctl entries to avoid warnings
1403 $cmd .= ' -e \'s/^\(kernel\.printk.*\)/#\1/\'';
1404 $cmd .= ' -e \'s/^\(kernel\.maps_protect.*\)/#\1/\'';
1405 $cmd .= ' -e \'s/^\(fs\.inotify\.max_user_watches.*\)/#\1/\'';
1406 $cmd .= ' -e \'s/^\(vm\.mmap_min_addr.*\)/#\1/\'';
1407 $cmd .= " -i '$rootdir/etc/sysctl.conf'";
1408 $self->run_command ($cmd);
1410 my $bindv6only = "$rootdir/etc/sysctl.d/bindv6only.conf";
1411 if (-f
$bindv6only) {
1413 $cmd .= ' -e \'s/^\(net\.ipv6\.bindv6only.*\)/#\1/\'';
1414 $cmd .= " -i '$bindv6only'";
1415 $self->run_command ($cmd);
1418 if ($suite eq 'hardy' || $suite eq 'intrepid' || $suite eq 'jaunty') {
1419 # disable tty init (console-setup)
1421 $cmd .= ' -e \'s/^\(ACTIVE_CONSOLES=.*\)/ACTIVE_CONSOLES=/\'';
1422 $cmd .= " -i '$rootdir/etc/default/console-setup'";
1423 $self->run_command ($cmd);
1426 if ($suite eq 'intrepid') {
1427 # remove sysctl setup (avoid warnings at startup)
1428 my $filelist = "$rootdir/etc/sysctl.d/10-console-messages.conf";
1429 $filelist .= " $rootdir/etc/sysctl.d/10-process-security.conf";
1430 $filelist .= " $rootdir/etc/sysctl.d/10-network-security.conf";
1431 $self->run_command ("rm $filelist");
1433 if ($suite eq 'jaunty') {
1434 # remove sysctl setup (avoid warnings at startup)
1435 my $filelist = "$rootdir/etc/sysctl.d/10-console-messages.conf";
1436 $filelist .= " $rootdir/etc/sysctl.d/10-network-security.conf";
1437 $self->run_command ("rm $filelist");
1444 my $veid = $self->{veid
};
1445 my $conffile = $self->{veconffile
};
1447 my $vestat = $self->ve_status();
1449 if (!$vestat->{exist
}) {
1450 $self->logmsg ("Please create the appliance first (bootstrap)");
1454 if (!$vestat->{running
}) {
1455 $self->run_command ("lxc-start -n $veid -f $conffile");
1458 system ("lxc-attach -n $veid --rcfile $conffile --clear-env");
1461 sub ve_mysql_command
{
1462 my ($self, $sql, $password) = @_;
1464 #my $bootstrap = "/usr/sbin/mysqld --bootstrap --user=mysql --skip-grant-tables " .
1465 #"--skip-bdb --skip-innodb --skip-ndbcluster";
1467 $self->ve_command ("mysql", $sql);
1470 sub ve_mysql_bootstrap
{
1471 my ($self, $sql, $password) = @_;
1475 my $suite = $self->{config
}->{suite
};
1477 if ($suite eq 'jessie') {
1478 my $rootdir = $self->{rootfs
};
1479 $self->run_command ("sed -e 's/^key_buffer\\s*=/key_buffer_size =/' -i $rootdir/etc/mysql/my.cnf");
1482 if ($suite eq 'squeeze' || $suite eq 'wheezy' || $suite eq 'jessie') {
1483 $cmd = "/usr/sbin/mysqld --bootstrap --user=mysql --skip-grant-tables";
1486 $cmd = "/usr/sbin/mysqld --bootstrap --user=mysql --skip-grant-tables " .
1487 "--skip-bdb --skip-innodb --skip-ndbcluster";
1490 $self->ve_command ($cmd, $sql);
1493 sub compute_required
{
1494 my ($self, $pkglist) = @_;
1496 my $pkginfo = $self->pkginfo();
1497 my $instpkgs = $self->read_installed ();
1500 __record_provides
($pkginfo, $closure, [keys %$instpkgs]);
1502 return $self->closure ($closure, $pkglist);
1506 my ($self, $opts) = @_;
1508 my @supp = ('7.4', '8.1');
1509 my $pgversion = '8.1';
1511 my $suite = $self->{config
}->{suite
};
1513 if ($suite eq 'lenny' || $suite eq 'hardy' || $suite eq 'intrepid' || $suite eq 'jaunty') {
1516 } elsif ($suite eq 'squeeze') {
1519 } elsif ($suite eq 'wheezy') {
1522 } elsif ($suite eq 'jessie') {
1527 $pgversion = $opts->{version
} if $opts->{version
};
1529 die "unsupported postgres version '$pgversion'\n"
1530 if !grep { $pgversion eq $_; } @supp;
1532 my $rootdir = $self->{rootfs
};
1534 my $required = $self->compute_required (["postgresql-$pgversion"]);
1536 $self->cache_packages ($required);
1538 $self->ve_dpkg ('install', @$required);
1540 my $iscript = "postgresql-$pgversion";
1541 if ($suite eq 'squeeze' || $suite eq 'wheezy' || $suite eq 'jessie') {
1542 $iscript = 'postgresql';
1545 $self->ve_command ("/etc/init.d/$iscript start") if $opts->{start
};
1549 my ($self, $opts) = @_;
1551 my $password = $opts->{password
};
1552 my $rootdir = $self->{rootfs
};
1554 my $suite = $self->{config
}->{suite
};
1557 if ($suite eq 'squeeze') {
1559 } elsif ($suite eq 'wheezy' || $suite eq 'jessie') {
1563 my $required = $self->compute_required (['mysql-common', "mysql-server-$ver"]);
1565 $self->cache_packages ($required);
1567 $self->ve_dpkg ('install', @$required);
1569 # fix security (see /usr/bin/mysql_secure_installation)
1570 my $sql = "DELETE FROM mysql.user WHERE User='';\n" .
1571 "DELETE FROM mysql.user WHERE User='root' AND Host!='localhost';\n" .
1572 "FLUSH PRIVILEGES;\n";
1573 $self->ve_mysql_bootstrap ($sql);
1577 my $rpw = $password eq 'random' ?
'admin' : $password;
1579 my $sql = "USE mysql;\n" .
1580 "UPDATE user SET password=PASSWORD(\"$rpw\") WHERE user
='root';\n" .
1581 "FLUSH PRIVILEGES
;\n";
1582 $self->ve_mysql_bootstrap ($sql);
1584 write_file ("[client
]\nuser
=root
\npassword
=\"$rpw\"\n", "$rootdir/root/.my.cnf", 0600);
1585 if ($password eq 'random') {
1586 $self->install_init_script ($script_mysql_randompw, 2, 20);
1590 $self->ve_command ("/etc/init.d/mysql start") if $opts->{start
};
1594 my ($self, $opts) = @_;
1596 my $memlimit = $opts->{memlimit
};
1597 my $rootdir = $self->{rootfs
};
1599 my $required = $self->compute_required ([qw
(php5 php5-cli libapache2-mod-php5 php5-gd
)]);
1601 $self->cache_packages ($required);
1603 $self->ve_dpkg ('install', @$required);
1606 $self->run_command ("sed -e 's/^\\s*memory_limit\\s*=.*;/memory_limit = ${memlimit}M;/' -i $rootdir/etc/php5/apache2/php.ini");
1611 my ($self, $pkglist, $unpack) = @_;
1613 my $required = $self->compute_required ($pkglist);
1615 $self->cache_packages ($required);
1617 $self->ve_dpkg ($unpack ?
'unpack' : 'install', @$required);
1621 my ($self, $distclean) = @_;
1623 unlink $self->{logfile
};
1624 unlink "$self->{targetname}.tar";
1625 unlink "$self->{targetname}.tar.gz";
1627 $self->ve_destroy ();
1630 rmtree
$self->{cachedir
} if $distclean && !$self->{config
}->{cachedir
};
1632 rmtree
$self->{infodir
};