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 = +
45 compatibility_level = 2
49 # produce apt compatible filenames (/var/lib/apt/lists)
50 sub __url_to_filename
{
62 my $supported_suites = {
64 ostype
=> "debian-10.0",
67 ostype
=> "debian-9.0",
70 ostype
=> "debian-8.0",
73 ostype
=> "debian-7.0",
76 ostype
=> "debian-6.0",
79 ostype
=> "debian-5.0",
82 ostype
=> "debian-4.0",
88 ostype
=> "devuan-1.0",
92 ostype
=> "devuan-2.0",
95 ostype
=> "devuan-2.0",
100 ostype
=> "ubuntu-8.04",
104 ostype
=> "ubuntu-8.10",
108 ostype
=> "ubuntu-9.04",
112 ostype
=> "ubuntu-12.04",
116 ostype
=> "ubuntu-14.04",
120 ostype
=> "ubuntu-15.04",
124 ostype
=> "ubuntu-15.10",
128 ostype
=> "ubuntu-16.04",
132 ostype
=> "ubuntu-16.10",
136 ostype
=> "ubuntu-17.04",
140 ostype
=> "ubuntu-17.10",
144 ostype
=> "ubuntu-18.04",
148 ostype
=> "ubuntu-18.10",
152 ostype
=> "ubuntu-19.04",
156 ostype
=> "ubuntu-19.10",
164 my $suiteinfo = $supported_suites->{$suite} || die "unsupported suite '$suite'!\n";
167 $suiteinfo->{origin
} //= 'debian';
168 $suiteinfo->{suite
} //= $suite;
174 my ($self, $url, $path) = @_;
176 $self->logmsg ("download: $url\n");
177 my $tmpfn = "$path.tmp$$";
179 $self->run_command ("wget -q '$url' -O '$tmpfn'");
188 rename ($tmpfn, $path);
192 my ($data, $file, $perm) = @_;
194 die "no filename" if !$file;
198 my $fh = IO
::File-
>new ($file, O_WRONLY
| O_CREAT
, $perm) ||
199 die "unable to open file '$file'";
209 die "no filename" if !$file;
211 my $fh = IO
::File-
>new ($file) ||
212 die "unable to open file '$file'";
214 local $/; # slurp mode
228 my $fh = IO
::File-
>new ("<$filename") || return $res;
231 while (defined (my $line = <$fh>)) {
232 next if $line =~ m/^\#/;
233 next if $line =~ m/^\s*$/;
243 if ($rec =~ s/^Description:\s*([^\n]*)(\n\s+.*)*$//si) {
244 $res->{headline
} = $1;
245 chomp $res->{headline
};
248 $res->{description
} = $long;
249 chomp $res->{description
};
250 } elsif ($rec =~ s/^([^:]+):\s*(.*\S)\s*\n//) {
251 my ($key, $value) = (lc ($1), $2);
252 if ($key eq 'source' || $key eq 'mirror') {
253 push @{$res->{$key}}, $value;
255 die "duplicate key '$key'\n" if defined ($res->{$key});
256 $res->{$key} = $value;
259 die "unable to parse config file: $rec";
263 die "unable to parse config file" if $rec;
269 my ($self, $cmd, $input, $getoutput) = @_;
271 my $reader = IO
::File-
>new();
272 my $writer = IO
::File-
>new();
273 my $error = IO
::File-
>new();
277 my $cmdstr = ref ($cmd) eq 'ARRAY' ?
join (' ', @$cmd) : $cmd;
281 if (ref ($cmd) eq 'ARRAY') {
282 $pid = open3
($writer, $reader, $error, @$cmd) || die $!;
284 $pid = open3
($writer, $reader, $error, $cmdstr) || die $!;
291 if ($orig_pid != $$) {
292 $self->logmsg ("ERROR: command '$cmdstr' failed - fork failed\n");
299 print $writer $input if defined $input;
302 my $select = new IO
::Select
;
303 $select->add ($reader);
304 $select->add ($error);
307 my $logfd = $self->{logfd
};
309 while ($select->count) {
310 my @handles = $select->can_read ();
312 foreach my $h (@handles) {
314 my $count = sysread ($h, $buf, 4096);
315 if (!defined ($count)) {
317 die "command '$cmdstr' failed: $!";
319 $select->remove ($h) if !$count;
323 $res .= $buf if $getoutput;
330 die "command '$cmdstr' failed with exit code $ec\n" if $ec;
338 $self->writelog (@_);
343 my $fd = $self->{logfd
};
347 sub __sample_config
{
351 my $arch = $self->{config
}->{architecture
};
353 my $ostype = $self->{config
}->{ostype
};
355 if ($ostype =~ m/^de(bi|vu)an-/) {
356 $data .= "lxc.include = /usr/share/lxc/config/debian.common.conf\n";
357 } elsif ($ostype =~ m/^ubuntu-/) {
358 $data .= "lxc.include = /usr/share/lxc/config/ubuntu.common.conf\n";
360 die "unknown os type '$ostype'\n";
362 $data .= "lxc.uts.name = localhost\n";
363 $data .= "lxc.rootfs.path = $self->{rootfs}\n";
372 if (my $fd = IO
::File-
>new (".veid")) {
379 $self->{working_dir
} = getcwd
;
380 $self->{veconffile
} = "$self->{working_dir}/config";
381 $self->{rootfs
} = "$self->{working_dir}/rootfs";
384 $self->{veid
} = $cid;
390 UUID
::generate
($uuid);
391 UUID
::unparse
($uuid, $uuid_str);
392 $self->{veid
} = $uuid_str;
394 my $fd = IO
::File-
>new (">.veid") ||
395 die "unable to write '.veid'\n";
396 print $fd "$self->{veid}\n";
399 my $cdata = $self->__sample_config();
401 my $fh = IO
::File-
>new ($self->{veconffile
}, O_WRONLY
|O_CREAT
|O_EXCL
) ||
402 die "unable to write lxc config file '$self->{veconffile}' - $!";
406 mkdir $self->{rootfs
} || die "unable to create rootfs - $!";
408 $self->logmsg ("allocated VE $self->{veid}\n");
410 return $self->{veid
};
414 my ($class, $config) = @_;
416 $class = ref ($class) || $class;
420 $config = read_config
('dab.conf') if !$config;
422 $self->{config
} = $config;
426 $self->{logfile
} = "logfile";
427 $self->{logfd
} = IO
::File-
>new (">>$self->{logfile}") ||
428 die "unable to open log file";
430 my $arch = $config->{architecture
};
431 die "no 'architecture' specified\n" if !$arch;
433 die "unsupported architecture '$arch'\n"
434 if $arch !~ m/^(i386|amd64)$/;
436 my $suite = $config->{suite
} || die "no 'suite' specified\n";
438 my $suiteinfo = get_suite_info
($suite);
439 $suite = $suiteinfo->{suite
};
440 $config->{ostype
} = $suiteinfo->{ostype
};
442 my $name = $config->{name
} || die "no 'name' specified\n";
444 $name =~ m/^[a-z][0-9a-z\-\*\.]+$/ ||
445 die "illegal characters in name '$name'\n";
447 my $version = $config->{version
};
448 die "no 'version' specified\n" if !$version;
449 die "no 'section' specified\n" if !$config->{section
};
450 die "no 'description' specified\n" if !$config->{headline
};
451 die "no 'maintainer' specified\n" if !$config->{maintainer
};
453 if ($name =~ m/^$config->{ostype}/) {
454 $self->{targetname
} = "${name}_${version}_$config->{architecture}";
456 $self->{targetname
} = "$config->{ostype}-${name}_" .
457 "${version}_$config->{architecture}";
460 if (!$config->{source
}) {
461 if (lc($suiteinfo->{origin
}) eq 'debian') {
462 if ($suite eq 'etch' || $suite eq 'lenny') {
463 push @{$config->{source
}}, (
464 'http://ftp.debian.org/debian SUITE main contrib',
465 'http://security.debian.org SUITE/updates main contrib',
468 push @{$config->{source
}}, (
469 "http://ftp.debian.org/debian SUITE main contrib",
470 "http://ftp.debian.org/debian SUITE-updates main contrib",
471 "http://security.debian.org SUITE/updates main contrib",
474 } elsif (lc($suiteinfo->{origin
}) eq 'ubuntu') {
475 my $comp = "main restricted universe multiverse";
476 push @{$config->{source
}}, (
477 "http://archive.ubuntu.com/ubuntu SUITE $comp",
478 "http://archive.ubuntu.com/ubuntu SUITE-updates $comp",
479 "http://archive.ubuntu.com/ubuntu SUITE-security $comp",
488 foreach my $s (@{$config->{source
}}) {
489 if ($s =~ m
@^\s*((http
|ftp
)://\S
+)\s
+(\S
+)((\s
+(\S
+))+)$@) {
490 my ($url, $su, $components) = ($1, $3, $4);
491 $su =~ s/SUITE/$suite/;
492 $components =~ s/^\s+//;
493 $components =~ s/\s+$//;
495 foreach my $co (split (/\s+/, $components)) {
498 $ca = ['main'] if !$ca;
506 die "syntax error in source spezification '$s'\n";
510 foreach my $m (@{$config->{mirror
}}) {
511 if ($m =~ m
@^\s*((http
|ftp
)://\S
+)\s
*=>\s
*((http
|ftp
)://\S
+)\s
*$@) {
512 my ($ms, $md) = ($1, $3);
514 foreach my $ss (@$sources) {
515 if ($ss->{source
} eq $ms) {
521 die "unusable mirror $ms\n" if !$found;
523 die "syntax error in mirror spezification '$m'\n";
527 $self->{sources
} = $sources;
529 $self->{infodir
} = "info";
531 $self->__allocate_ve ();
533 $self->{cachedir
} = ($config->{cachedir
} || 'cache') . "/$suite";;
535 my $incl = [qw
(less ssh openssh-server logrotate
)];
537 my $excl = [qw
(modutils reiserfsprogs ppp pppconfig pppoe
538 pppoeconf nfs-common mtools ntp
)];
540 # ubuntu has too many dependencies on udev, so
541 # we cannot exclude it (instead we disable udevd)
543 if ($suite eq 'vivid' || $suite eq 'wily' || $suite eq 'xenial' ||
544 $suite eq 'yakkety' || $suite eq 'zesty' || $suite eq 'artful' ||
545 $suite eq 'bionic' || $suite eq 'cosmic' || $suite eq 'disco' ||
547 push @$incl, 'isc-dhcp-client';
548 push @$excl, qw(libmodule-build-perl);
549 } elsif ($suite eq 'trusty') {
550 push @$excl, qw(systemd systemd-services libpam-systemd libsystemd-daemon0 memtest86+);
551 } elsif ($suite eq 'precise') {
552 push @$excl, qw(systemd systemd-services libpam-systemd libsystemd-daemon0 memtest86+ ubuntu-standard);
553 } elsif ($suite eq 'hardy') {
554 push @$excl, qw(kbd);
555 push @$excl, qw(apparmor apparmor-utils ntfs-3g
557 } elsif ($suite eq 'intrepid' || $suite eq 'jaunty') {
558 push @$excl, qw(apparmor apparmor-utils libapparmor1 libapparmor-perl
559 libntfs-3g28 ntfs-3g friendly-recovery);
560 } elsif ($suite eq 'jessie') {
561 push @$incl, 'sysvinit-core'; # avoid systemd and udev
562 push @$incl, 'libperl4-corelibs-perl'; # to make lsof happy
563 push @$excl, qw(systemd systemd-sysv udev module-init-tools pciutils hdparm
565 } elsif ($suite eq 'stretch' || $suite eq 'buster') {
566 push @$excl, qw(module-init-tools pciutils hdparm
569 push @$excl, qw(udev module-init-tools pciutils hdparm
573 $self->{incl
} = $incl;
574 $self->{excl
} = $excl;
582 my $infodir = $self->{infodir
};
583 my $arch = $self->{config
}->{architecture
};
589 my $logfd = $self->{logfd
} = IO
::File-
>new (">$self->{logfile}") ||
590 die "unable to open log file";
592 foreach my $ss (@{$self->{sources
}}) {
593 my $src = $ss->{mirror
} || $ss->{source
};
594 my $path = "dists/$ss->{suite}/Release";
595 my $url = "$src/$path";
596 my $target = __url_to_filename
("$ss->{source}/$path");
598 $self->download ($url, "$infodir/$target");
599 $self->download ("$url.gpg", "$infodir/$target.gpg");
600 # fixme: impl. verify (needs --keyring option)
604 warn "Release info ignored\n";
606 foreach my $comp (@{$ss->{comp
}}) {
607 $path = "dists/$ss->{suite}/$comp/binary-$arch/Packages.gz";
608 $target = "$infodir/" . __url_to_filename
("$ss->{source}/$path");
609 my $pkgsrc = "$src/$path";
610 $self->download ($pkgsrc, $target);
611 $self->run_command ("gzip -d '$target'");
617 my ($self, $filename, $size) = @_;
619 my $config = $self->{config
};
623 $data .= "Name: $config->{name}\n";
624 $data .= "Version: $config->{version}\n";
625 $data .= "Type: lxc\n";
626 $data .= "OS: $config->{ostype}\n";
627 $data .= "Section: $config->{section}\n";
628 $data .= "Maintainer: $config->{maintainer}\n";
629 $data .= "Architecture: $config->{architecture}\n";
630 $data .= "Installed-Size: $size\n";
633 $data .= "Infopage: $config->{infopage}\n" if $config->{infopage
};
634 $data .= "ManageUrl: $config->{manageurl}\n" if $config->{manageurl
};
635 $data .= "Certified: $config->{certified}\n" if $config->{certified
};
638 $data .= "Description: $config->{headline}\n";
639 $data .= "$config->{description}\n" if $config->{description
};
641 write_file
($data, $filename, 0644);
645 my ($self, $opts) = @_;
647 my $suite = $self->{config
}->{suite
};
648 my $infodir = $self->{infodir
};
649 my $arch = $self->{config
}->{architecture
};
651 my $instpkgs = $self->read_installed ();
652 my $pkginfo = $self->pkginfo();
653 my $veid = $self->{veid
};
654 my $conffile = $self->{veconffile
};
655 my $rootdir = $self->{rootfs
};
657 my $vestat = $self->ve_status();
658 die "ve not running - unable to finalize\n" if !$vestat->{running
};
661 if (-f
"$rootdir/etc/init.d/mysql") {
662 $self->ve_command ("/etc/init.d/mysql stop");
665 if (!($opts->{keepmycnf
} || (-f
"$rootdir/etc/init.d/mysql_randompw"))) {
666 unlink "$rootdir/root/.my.cnf";
669 if ($suite eq 'etch') {
670 # enable apache2 startup
671 if ($instpkgs->{apache2
}) {
672 write_file
("NO_START=0\n", "$rootdir/etc/default/apache2");
674 unlink "$rootdir/etc/default/apache2";
677 $self->logmsg ("cleanup package status\n");
678 # prevent auto selection of all standard, required or important
679 # packages which are not installed
680 foreach my $pkg (keys %$pkginfo) {
681 my $pri = $pkginfo->{$pkg}->{priority
};
682 if ($pri && ($pri eq 'required' || $pri eq 'important'
683 || $pri eq 'standard')) {
684 if (!$instpkgs->{$pkg}) {
685 $self->ve_dpkg_set_selection ($pkg, 'purge');
690 $self->ve_command ("apt-get clean");
692 $self->logmsg ("update available package list\n");
694 $self->ve_command ("dpkg --clear-avail");
695 foreach my $ss (@{$self->{sources
}}) {
696 my $relsrc = __url_to_filename
("$ss->{source}/dists/$ss->{suite}/Release");
697 if (-f
"$infodir/$relsrc" && -f
"$infodir/$relsrc.gpg") {
698 $self->run_command ("cp '$infodir/$relsrc' '$rootdir/var/lib/apt/lists/$relsrc'");
699 $self->run_command ("cp '$infodir/$relsrc.gpg' '$rootdir/var/lib/apt/lists/$relsrc.gpg'");
701 foreach my $comp (@{$ss->{comp
}}) {
702 my $src = __url_to_filename
("$ss->{source}/dists/$ss->{suite}/" .
703 "$comp/binary-$arch/Packages");
704 my $target = "/var/lib/apt/lists/$src";
705 $self->run_command ("cp '$infodir/$src' '$rootdir/$target'");
706 $self->ve_command ("dpkg --merge-avail '$target'");
710 # set dselect default method
711 write_file
("apt apt\n", "$rootdir/var/lib/dpkg/cmethopt");
713 $self->ve_divert_remove ("/usr/sbin/policy-rc.d");
715 $self->ve_divert_remove ("/sbin/start-stop-daemon");
717 $self->ve_divert_remove ("/sbin/init");
719 # finally stop the VE
720 $self->run_command ("lxc-stop -n $veid --rcfile $conffile --kill");
722 unlink "$rootdir/sbin/defenv";
724 unlink <$rootdir/root/dead.letter
*>;
726 unlink "$rootdir/var/log/init.log";
728 unlink "$rootdir/aquota.group";
730 unlink "$rootdir/aquota.user";
732 write_file
("", "$rootdir/var/log/syslog");
734 $self->logmsg ("detecting final size: ");
736 my $sizestr = $self->run_command ("du -sm $rootdir", undef, 1);
738 if ($sizestr =~ m/^(\d+)\s+\Q$rootdir\E$/) {
741 die "unable to detect size\n";
743 $self->logmsg ("$size MB\n");
745 $self->write_config ("$rootdir/etc/appliance.info", $size);
747 $self->logmsg ("creating final appliance archive\n");
749 my $target = "$self->{targetname}.tar";
753 $self->run_command ("tar cpf $target --numeric-owner -C '$rootdir' ./etc/appliance.info");
754 $self->run_command ("tar rpf $target --numeric-owner -C '$rootdir' --exclude ./etc/appliance.info .");
755 $self->run_command ("gzip $target");
761 my $rootdir = $self->{rootfs
};
763 my $pkgfilelist = "$rootdir/var/lib/dpkg/status";
765 open (PKGLST
, "<$pkgfilelist") ||
766 die "unable to open '$pkgfilelist'";
770 while (my $rec = <PKGLST
>) {
776 while ($rec =~ s/^([^:]+):\s+(.*)\s*\n//) {
780 my $pkg = $res->{'package'};
781 if (my $status = $res->{status
}) {
782 my @sa = split (/\s+/, $status);
784 if ($stat && ($stat ne 'purge')) {
785 $pkglist->{$pkg} = $res;
798 my $veid = $self->{veid
};
800 my $res = { running
=> 0 };
802 $res->{exist
} = 1 if -d
"$self->{rootfs}/usr";
804 my $filename = "/proc/net/unix";
806 # similar test is used by lcxcontainers.c: list_active_containers
807 my $fh = IO
::File-
>new ($filename, "r");
810 while (defined(my $line = <$fh>)) {
811 if ($line =~ m/^[a-f0-9]+:\s\S+\s\S+\s\S+\s\S+\s\S+\s\d+\s(\S+)$/) {
813 if ($path =~ m!^@/\S+/$veid/command$!) {
824 my ($self, $cmd, $input) = @_;
826 my $veid = $self->{veid
};
827 my $conffile = $self->{veconffile
};
829 if (ref ($cmd) eq 'ARRAY') {
830 unshift @$cmd, 'lxc-attach', '-n', $veid, '--rcfile', $conffile, '--clear-env', '--', 'defenv';
831 $self->run_command ($cmd, $input);
833 $self->run_command ("lxc-attach -n $veid --rcfile $conffile --clear-env -- defenv $cmd", $input);
837 # like ve_command, but pipes stdin correctly
839 my ($self, @cmd) = @_;
841 my $veid = $self->{veid
};
842 my $conffile = $self->{veconffile
};
845 my $pid = open2
($reader, "<&STDIN", 'lxc-attach', '-n', $veid, '--rcfile', $conffile, '--',
846 'defenv', @cmd) || die "unable to exec command";
848 while (defined (my $line = <$reader>)) {
849 $self->logmsg ($line);
855 die "ve_exec failed - status $rc\n" if $rc != 0;
859 my ($self, $filename) = @_;
861 $self->ve_command ("dpkg-divert --add --divert '$filename.distrib' " .
862 "--rename '$filename'");
864 sub ve_divert_remove
{
865 my ($self, $filename) = @_;
867 my $rootdir = $self->{rootfs
};
869 unlink "$rootdir/$filename";
870 $self->ve_command ("dpkg-divert --remove --rename '$filename'");
873 sub ve_debconfig_set
{
874 my ($self, $dcdata) = @_;
876 my $rootdir = $self->{rootfs
};
877 my $cfgfile = "/tmp/debconf.txt";
878 write_file
($dcdata, "$rootdir/$cfgfile");
879 $self->ve_command ("debconf-set-selections $cfgfile");
880 unlink "$rootdir/$cfgfile";
883 sub ve_dpkg_set_selection
{
884 my ($self, $pkg, $status) = @_;
886 $self->ve_command ("dpkg --set-selections", "$pkg $status");
890 my ($self, $cmd, @pkglist) = @_;
892 return if !scalar (@pkglist);
894 my $pkginfo = $self->pkginfo();
896 my $rootdir = $self->{rootfs
};
897 my $cachedir = $self->{cachedir
};
901 foreach my $pkg (@pkglist) {
902 my $filename = $self->getpkgfile ($pkg);
903 $self->run_command ("cp '$cachedir/$filename' '$rootdir/$filename'");
904 push @files, "/$filename";
905 $self->logmsg ("$cmd: $pkg\n");
908 my $fl = join (' ', @files);
910 if ($cmd eq 'install') {
911 $self->ve_command ("dpkg --force-depends --force-confold --install $fl");
912 } elsif ($cmd eq 'unpack') {
913 $self->ve_command ("dpkg --force-depends --unpack $fl");
915 die "internal error";
918 foreach my $fn (@files) { unlink "$rootdir$fn"; }
924 my $veid = $self->{veid
}; # fixme
925 my $conffile = $self->{veconffile
};
927 my $vestat = $self->ve_status();
928 if ($vestat->{running
}) {
929 $self->run_command ("lxc-stop -n $veid --rcfile $conffile --kill");
932 rmtree
$self->{rootfs
};
933 unlink $self->{veconffile
};
939 my $veid = $self->{veid
};
940 my $conffile = $self->{veconffile
};
942 $self->logmsg ("initialize VE $veid\n");
944 my $vestat = $self->ve_status();
945 if ($vestat->{running
}) {
946 $self->run_command ("lxc-stop -n $veid --rcfile $conffile --kill");
949 rmtree
$self->{rootfs
};
950 mkpath
$self->{rootfs
};
953 sub __deb_version_cmp
{
954 my ($cur, $op, $new) = @_;
956 if (system("dpkg", "--compare-versions", $cur, $op, $new) == 0) {
963 sub __parse_packages
{
964 my ($pkginfo, $filename, $src) = @_;
967 open (PKGLST
, "<$filename") ||
968 die "unable to open '$filename'";
970 while (my $rec = <PKGLST
>) {
977 while ($rec =~ s/^([^:]+):\s+(.*)\s*\n//) {
981 my $pkg = $res->{'package'};
982 if ($pkg && $res->{'filename'}) {
984 if (my $info = $pkginfo->{$pkg}) {
985 $cur = $info->{version
};
987 my $new = $res->{version
};
988 if (!$cur || __deb_version_cmp
($cur, 'lt', $new)) {
990 $res->{url
} = "$src/$res->{'filename'}";
992 die "no url for package '$pkg'" if !$res->{url
};
994 $pkginfo->{$pkg} = $res;
1005 return $self->{pkginfo
} if $self->{pkginfo
};
1007 my $infodir = $self->{infodir
};
1008 my $arch = $self->{config
}->{architecture
};
1010 my $availfn = "$infodir/available";
1015 # reading 'available' is faster, because it only contains latest version
1016 # (no need to do slow version compares)
1018 __parse_packages
($pkginfo, $availfn);
1019 $self->{pkginfo
} = $pkginfo;
1023 $self->logmsg ("generating available package list\n");
1025 foreach my $ss (@{$self->{sources
}}) {
1026 foreach my $comp (@{$ss->{comp
}}) {
1027 my $url = "$ss->{source}/dists/$ss->{suite}/$comp/binary-$arch/Packages";
1028 my $pkgfilelist = "$infodir/" . __url_to_filename
($url);
1030 my $src = $ss->{mirror
} || $ss->{source
};
1032 __parse_packages
($pkginfo, $pkgfilelist, $src);
1036 if (my $dep = $self->{config
}->{depends
}) {
1037 foreach my $d (split (/,/, $dep)) {
1038 if ($d =~ m/^\s*(\S+)\s*(\((\S+)\s+(\S+)\)\s*)?$/) {
1039 my ($pkg, $op, $rver) = ($1, $3, $4);
1040 $self->logmsg ("checking dependencies: $d\n");
1041 my $info = $pkginfo->{$pkg};
1042 die "package '$pkg' not available\n" if !$info;
1044 my $cver = $info->{version
};
1045 if (!__deb_version_cmp
($cver, $op, $rver)) {
1046 die "detected wrong version '$cver'\n";
1050 die "syntax error in depends field";
1055 $self->{pkginfo
} = $pkginfo;
1057 my $tmpfn = "$availfn.tmp$$";
1058 my $fd = IO
::File-
>new (">$tmpfn");
1059 foreach my $pkg (sort keys %$pkginfo) {
1060 my $info = $pkginfo->{$pkg};
1061 print $fd "package: $pkg\n";
1062 foreach my $k (sort keys %$info) {
1063 next if $k eq 'description';
1064 next if $k eq 'package';
1065 my $v = $info->{$k};
1066 print $fd "$k: $v\n" if $v;
1068 print $fd "description: $info->{description}\n" if $info->{description
};
1073 rename ($tmpfn, $availfn);
1078 sub __record_provides
{
1079 my ($pkginfo, $closure, $list, $skipself) = @_;
1081 foreach my $pname (@$list) {
1082 my $info = $pkginfo->{$pname};
1083 # fixme: if someone install packages directly using dpkg, there
1084 # is no entry in 'available', only in 'status'. In that case, we
1085 # should extract info from $instpkgs
1087 warn "hint: ignoring provides for '$pname' - package not in 'available' list.\n";
1090 if (my $prov = $info->{provides
}) {
1091 my @pl = split (',', $prov);
1092 foreach my $p (@pl) {
1094 if (!($skipself && (grep { $1 eq $_ } @$list))) {
1099 $closure->{$pname} = 1 if !$skipself;
1104 my ($self, $closure, $list) = @_;
1106 my $pkginfo = $self->pkginfo();
1108 # first, record provided packages
1109 __record_provides
($pkginfo, $closure, $list, 1);
1114 # then resolve dependencies
1115 foreach my $pname (@$list) {
1116 __closure_single
($pkginfo, $closure, $pkghash, $pkglist, $pname, $self->{excl
});
1122 sub __closure_single
{
1123 my ($pkginfo, $closure, $pkghash, $pkglist, $pname, $excl) = @_;
1127 $pname =~ s/:any$//;
1129 return if $closure->{$pname};
1131 my $info = $pkginfo->{$pname} || die "no such package '$pname'";
1133 my $dep = $info->{depends
};
1134 my $predep = $info->{'pre-depends'};
1136 my $size = $info->{size
};
1137 my $url = $info->{url
};
1139 $url || die "$pname: no url for package '$pname'";
1141 if (!$pkghash->{$pname}) {
1142 unshift @$pkglist, $pname;
1143 $pkghash->{$pname} = 1;
1146 __record_provides
($pkginfo, $closure, [$pname]) if $info->{provides
};
1148 $closure->{$pname} = 1;
1154 push @l, split (/,/, $predep) if $predep;
1155 push @l, split (/,/, $dep) if $dep;
1157 DEPEND
: foreach my $p (@l) {
1158 my @l1 = split (/\|/, $p);
1159 foreach my $p1 (@l1) {
1160 if ($p1 =~ m/^\s*(\S+).*/) {
1161 #printf (STDERR "$pname: $p --> $1\n");
1162 if ($closure->{$1}) {
1163 next DEPEND
; # dependency already met
1167 # search for non-excluded alternative
1169 foreach my $p1 (@l1) {
1170 if ($p1 =~ m/^\s*(\S+).*/) {
1171 next if grep { $1 eq $_ } @$excl;
1176 die "package '$pname' depends on exclusion '$p'\n" if !$found;
1178 #printf (STDERR "$pname: $p --> $found\n");
1180 __closure_single
($pkginfo, $closure, $pkghash, $pkglist, $found, $excl);
1184 sub cache_packages
{
1185 my ($self, $pkglist) = @_;
1187 foreach my $pkg (@$pkglist) {
1188 $self->getpkgfile ($pkg);
1193 my ($self, $pkg) = @_;
1195 my $pkginfo = $self->pkginfo();
1196 my $info = $pkginfo->{$pkg} || die "no such package '$pkg'";
1197 my $cachedir = $self->{cachedir
};
1199 my $url = $info->{url
};
1202 if ($url =~ m
|/([^/]+.deb
)$|) {
1205 die "internal error";
1208 return $filename if -f
"$cachedir/$filename";
1212 $self->download ($url, "$cachedir/$filename");
1217 sub install_init_script
{
1218 my ($self, $script, $runlevel, $prio) = @_;
1220 my $suite = $self->{config
}->{suite
};
1221 my $rootdir = $self->{rootfs
};
1223 my $base = basename
($script);
1224 my $target = "$rootdir/etc/init.d/$base";
1226 $self->run_command ("install -m 0755 '$script' '$target'");
1227 if ($suite eq 'etch' || $suite eq 'lenny') {
1228 $self->ve_command ("update-rc.d $base start $prio $runlevel .");
1229 } elsif ($suite eq 'xenial' || $suite eq 'wily' || $suite eq 'vivid' ||
1230 $suite eq 'yakkety' || $suite eq 'zesty' || $suite eq 'artful' ||
1231 $suite eq 'bionic' || $suite eq 'cosmic' || $suite eq 'disco' ||
1233 die "unable to install init script (system uses systemd)\n";
1234 } elsif ($suite eq 'trusty' || $suite eq 'precise') {
1235 die "unable to install init script (system uses upstart)\n";
1237 $self->ve_command ("insserv $base");
1244 my ($self, $opts) = @_;
1246 my $pkginfo = $self->pkginfo();
1247 my $veid = $self->{veid
};
1248 my $suite = $self->{config
}->{suite
};
1250 my $important = [ @{$self->{incl
}} ];
1254 my $mta = $opts->{exim
} ?
'exim' : 'postfix';
1256 if ($mta eq 'postfix') {
1257 push @$important, "postfix";
1260 foreach my $p (sort keys %$pkginfo) {
1261 next if grep { $p eq $_ } @{$self->{excl
}};
1262 my $pri = $pkginfo->{$p}->{priority
};
1264 next if $mta ne 'exim' && $p =~ m/exim/;
1265 next if $p =~ m/(selinux|semanage|policycoreutils)/;
1267 push @$required, $p if $pri eq 'required';
1268 push @$important, $p if $pri eq 'important';
1269 push @$standard, $p if $pri eq 'standard' && !$opts->{minimal
};
1273 $required = $self->closure ($closure, $required);
1274 $important = $self->closure ($closure, $important);
1276 if (!$opts->{minimal
}) {
1277 push @$standard, 'xbase-clients';
1278 $standard = $self->closure ($closure, $standard);
1281 # test if we have all 'ubuntu-minimal' and 'ubuntu-standard' packages
1282 # except those explicitly excluded
1283 if ($suite eq 'hardy' || $suite eq 'intrepid' || $suite eq 'jaunty') {
1284 my $mdeps = $pkginfo->{'ubuntu-minimal'}->{depends
};
1285 foreach my $d (split (/,/, $mdeps)) {
1286 if ($d =~ m/^\s*(\S+)$/) {
1288 next if $closure->{$pkg};
1289 next if grep { $pkg eq $_ } @{$self->{excl
}};
1290 die "missing ubuntu-minimal package '$pkg'\n";
1293 if (!$opts->{minimal
}) {
1294 $mdeps = $pkginfo->{'ubuntu-standard'}->{depends
};
1295 foreach my $d (split (/,/, $mdeps)) {
1296 if ($d =~ m/^\s*(\S+)$/) {
1298 next if $closure->{$pkg};
1299 next if grep { $pkg eq $_ } @{$self->{excl
}};
1300 die "missing ubuntu-standard package '$pkg'\n";
1306 # download/cache all files first
1307 $self->cache_packages ($required);
1308 $self->cache_packages ($important);
1309 $self->cache_packages ($standard);
1311 my $rootdir = $self->{rootfs
};
1313 # extract required packages first
1314 $self->logmsg ("create basic environment\n");
1315 foreach my $p (@$required) {
1316 my $filename = $self->getpkgfile ($p);
1317 my $content = $self->run_command("ar -t '$self->{cachedir}/$filename'", undef, 1);
1318 if ($content =~ m/^data.tar.xz$/m) {
1319 $self->run_command ("ar -p '$self->{cachedir}/$filename' data.tar.xz | tar -C '$rootdir' -xJf -");
1321 $self->run_command ("ar -p '$self->{cachedir}/$filename' data.tar.gz | tar -C '$rootdir' -xzf -");
1326 my $data = "Package: dpkg\n" .
1327 "Version: $pkginfo->{dpkg}->{version}\n" .
1328 "Status: install ok installed\n";
1330 write_file
($data, "$rootdir/var/lib/dpkg/status");
1331 write_file
("", "$rootdir/var/lib/dpkg/info/dpkg.list");
1332 write_file
("", "$rootdir/var/lib/dpkg/available");
1335 foreach my $ss (@{$self->{sources
}}) {
1336 my $url = $ss->{source
};
1337 my $comp = join (' ', @{$ss->{comp
}});
1338 $data .= "deb $url $ss->{suite} $comp\n\n";
1341 write_file
($data, "$rootdir/etc/apt/sources.list");
1343 $data = "# UNCONFIGURED FSTAB FOR BASE SYSTEM\n";
1344 write_file
($data, "$rootdir/etc/fstab", 0644);
1346 write_file
("localhost\n", "$rootdir/etc/hostname", 0644);
1348 # avoid warnings about non-existent resolv.conf
1349 write_file
("", "$rootdir/etc/resolv.conf", 0644);
1351 if ($suite eq 'eoan' || $suite eq 'disco' || $suite eq 'cosmic' ||
1352 $suite eq 'bionic' || $suite eq 'artful' ||
1353 $suite eq 'zesty' || $suite eq 'yakkety' || $suite eq 'xenial' ||
1355 # no need to configure loopback device
1357 $data = "auto lo\niface lo inet loopback\n";
1358 mkdir "$rootdir/etc/network";
1359 write_file
($data, "$rootdir/etc/network/interfaces", 0644);
1363 $self->run_command ("tar xzf '$devicetar' -C '$rootdir'");
1365 # avoid warnings about missing default locale
1366 write_file
("LANG=\"C\"\n", "$rootdir/etc/default/locale", 0644);
1369 rename ("$rootdir/sbin/init", "$rootdir/sbin/init.org");
1370 $self->run_command ("cp '$fake_init' '$rootdir/sbin/init'");
1372 $self->run_command ("cp '$default_env' '$rootdir/sbin/defenv'");
1374 $self->run_command ("lxc-start -n $veid -f $self->{veconffile}");
1376 $self->logmsg ("initialize ld cache\n");
1377 $self->ve_command ("/sbin/ldconfig");
1378 $self->run_command ("ln -sf mawk '$rootdir/usr/bin/awk'");
1380 $self->logmsg ("installing packages\n");
1382 $self->ve_dpkg ('install', 'base-files', 'base-passwd');
1384 $self->ve_dpkg ('install', 'dpkg');
1386 $self->run_command ("ln -sf /usr/share/zoneinfo/UTC '$rootdir/etc/localtime'");
1388 $self->run_command ("ln -sf bash '$rootdir/bin/sh'");
1390 $self->ve_dpkg ('install', 'libc6');
1391 $self->ve_dpkg ('install', 'perl-base');
1393 unlink "$rootdir/usr/bin/awk";
1395 $self->ve_dpkg ('install', 'mawk');
1396 $self->ve_dpkg ('install', 'debconf');
1398 # unpack required packages
1399 foreach my $p (@$required) {
1400 $self->ve_dpkg ('unpack', $p);
1403 rename ("$rootdir/sbin/init.org", "$rootdir/sbin/init");
1404 $self->ve_divert_add ("/sbin/init");
1405 $self->run_command ("cp '$fake_init' '$rootdir/sbin/init'");
1407 # disable service activation
1408 $self->ve_divert_add ("/usr/sbin/policy-rc.d");
1409 $data = "#!/bin/sh\nexit 101\n";
1410 write_file
($data, "$rootdir/usr/sbin/policy-rc.d", 755);
1412 # disable start-stop-daemon
1413 $self->ve_divert_add ("/sbin/start-stop-daemon");
1417 echo \"Warning: Fake start-stop-daemon called, doing nothing\"
1419 write_file
($data, "$rootdir/sbin/start-stop-daemon", 0755);
1422 $self->ve_divert_add ("/sbin/udevd");
1424 if ($suite eq 'etch') {
1425 # disable apache2 startup
1426 write_file
("NO_START=1\n", "$rootdir/etc/default/apache2");
1429 $self->logmsg ("configure required packages\n");
1430 $self->ve_command ("dpkg --force-confold --skip-same-version --configure -a");
1432 # set postfix defaults
1433 if ($mta eq 'postfix') {
1434 $data = "postfix postfix/main_mailer_type select Local only\n";
1435 $self->ve_debconfig_set ($data);
1437 $data = "postmaster: root\nwebmaster: root\n";
1438 write_file
($data, "$rootdir/etc/aliases");
1441 if ($suite eq 'jaunty') {
1442 # jaunty does not create /var/run/network, so network startup fails.
1443 # so we do not use tmpfs for /var/run and /var/lock
1444 $self->run_command ("sed -e 's/RAMRUN=yes/RAMRUN=no/' -e 's/RAMLOCK=yes/RAMLOCK=no/' -i $rootdir/etc/default/rcS");
1445 # and create the directory here
1446 $self->run_command ("mkdir $rootdir/var/run/network");
1449 # unpack base packages
1450 foreach my $p (@$important) {
1451 $self->ve_dpkg ('unpack', $p);
1455 if (-x
"$rootdir/sbin/ifconfig") {
1456 $self->ve_command ("ifconfig lo up");
1458 $self->ve_command ("ip link set lo up");
1461 $self->logmsg ("configure important packages\n");
1462 $self->ve_command ("dpkg --force-confold --skip-same-version --configure -a");
1464 if (-d
"$rootdir/etc/event.d") {
1465 unlink <$rootdir/etc/event.d
/tty
*>;
1468 if (-f
"$rootdir/etc/inittab") {
1469 $self->run_command ("sed -i -e '/getty\\s38400\\stty[23456]/d' '$rootdir/etc/inittab'");
1472 # Link /etc/mtab to /proc/mounts, so df and friends will work:
1473 unlink "$rootdir/etc/mtab";
1474 $self->ve_command ("ln -s /proc/mounts /etc/mtab");
1477 $self->ve_command ("usermod -L root");
1479 if ($mta eq 'postfix') {
1480 $data = "postfix postfix/main_mailer_type select No configuration\n";
1481 $self->ve_debconfig_set ($data);
1483 unlink "$rootdir/etc/mailname";
1484 write_file
($postfix_main_cf, "$rootdir/etc/postfix/main.cf");
1487 if (!$opts->{minimal
}) {
1488 # unpack standard packages
1489 foreach my $p (@$standard) {
1490 $self->ve_dpkg ('unpack', $p);
1493 $self->logmsg ("configure standard packages\n");
1494 $self->ve_command ("dpkg --force-confold --skip-same-version --configure -a");
1497 # disable HWCLOCK access
1498 $self->run_command ("echo 'HWCLOCKACCESS=no' >> '$rootdir/etc/default/rcS'");
1501 $self->ve_divert_add ("/usr/sbin/hald");
1503 # disable /dev/urandom init
1504 $self->run_command ("install -m 0755 '$script_init_urandom' '$rootdir/etc/init.d/urandom'");
1506 if ($suite eq 'etch' || $suite eq 'hardy' || $suite eq 'intrepid' || $suite eq 'jaunty') {
1508 $self->ve_divert_add ("/sbin/klogd");
1511 # remove unnecessays sysctl entries to avoid warnings
1513 $cmd .= ' -e \'s/^\(kernel\.printk.*\)/#\1/\'';
1514 $cmd .= ' -e \'s/^\(kernel\.maps_protect.*\)/#\1/\'';
1515 $cmd .= ' -e \'s/^\(fs\.inotify\.max_user_watches.*\)/#\1/\'';
1516 $cmd .= ' -e \'s/^\(vm\.mmap_min_addr.*\)/#\1/\'';
1517 $cmd .= " -i '$rootdir/etc/sysctl.conf'";
1518 $self->run_command ($cmd);
1520 my $bindv6only = "$rootdir/etc/sysctl.d/bindv6only.conf";
1521 if (-f
$bindv6only) {
1523 $cmd .= ' -e \'s/^\(net\.ipv6\.bindv6only.*\)/#\1/\'';
1524 $cmd .= " -i '$bindv6only'";
1525 $self->run_command ($cmd);
1528 if ($suite eq 'hardy' || $suite eq 'intrepid' || $suite eq 'jaunty') {
1529 # disable tty init (console-setup)
1531 $cmd .= ' -e \'s/^\(ACTIVE_CONSOLES=.*\)/ACTIVE_CONSOLES=/\'';
1532 $cmd .= " -i '$rootdir/etc/default/console-setup'";
1533 $self->run_command ($cmd);
1536 if ($suite eq 'intrepid') {
1537 # remove sysctl setup (avoid warnings at startup)
1538 my $filelist = "$rootdir/etc/sysctl.d/10-console-messages.conf";
1539 $filelist .= " $rootdir/etc/sysctl.d/10-process-security.conf";
1540 $filelist .= " $rootdir/etc/sysctl.d/10-network-security.conf";
1541 $self->run_command ("rm $filelist");
1543 if ($suite eq 'jaunty') {
1544 # remove sysctl setup (avoid warnings at startup)
1545 my $filelist = "$rootdir/etc/sysctl.d/10-console-messages.conf";
1546 $filelist .= " $rootdir/etc/sysctl.d/10-network-security.conf";
1547 $self->run_command ("rm $filelist");
1554 my $veid = $self->{veid
};
1555 my $conffile = $self->{veconffile
};
1557 my $vestat = $self->ve_status();
1559 if (!$vestat->{exist
}) {
1560 $self->logmsg ("Please create the appliance first (bootstrap)");
1564 if (!$vestat->{running
}) {
1565 $self->run_command ("lxc-start -n $veid -f $conffile");
1568 system ("lxc-attach -n $veid --rcfile $conffile --clear-env");
1571 sub ve_mysql_command
{
1572 my ($self, $sql, $password) = @_;
1574 #my $bootstrap = "/usr/sbin/mysqld --bootstrap --user=mysql --skip-grant-tables " .
1575 #"--skip-bdb --skip-innodb --skip-ndbcluster";
1577 $self->ve_command ("mysql", $sql);
1580 sub ve_mysql_bootstrap
{
1581 my ($self, $sql, $password) = @_;
1585 my $suite = $self->{config
}->{suite
};
1587 if ($suite eq 'jessie') {
1588 my $rootdir = $self->{rootfs
};
1589 $self->run_command ("sed -e 's/^key_buffer\\s*=/key_buffer_size =/' -i $rootdir/etc/mysql/my.cnf");
1592 if ($suite eq 'squeeze' || $suite eq 'wheezy' || $suite eq 'jessie') {
1593 $cmd = "/usr/sbin/mysqld --bootstrap --user=mysql --skip-grant-tables";
1596 $cmd = "/usr/sbin/mysqld --bootstrap --user=mysql --skip-grant-tables " .
1597 "--skip-bdb --skip-innodb --skip-ndbcluster";
1600 $self->ve_command ($cmd, $sql);
1603 sub compute_required
{
1604 my ($self, $pkglist) = @_;
1606 my $pkginfo = $self->pkginfo();
1607 my $instpkgs = $self->read_installed ();
1610 __record_provides
($pkginfo, $closure, [keys %$instpkgs]);
1612 return $self->closure ($closure, $pkglist);
1616 my ($self, $opts) = @_;
1618 my @supp = ('7.4', '8.1');
1619 my $pgversion = '8.1';
1621 my $suite = $self->{config
}->{suite
};
1623 if ($suite eq 'lenny' || $suite eq 'hardy' || $suite eq 'intrepid' || $suite eq 'jaunty') {
1626 } elsif ($suite eq 'squeeze') {
1629 } elsif ($suite eq 'wheezy') {
1632 } elsif ($suite eq 'jessie') {
1635 } elsif ($suite eq 'stretch') {
1638 } elsif ($suite eq 'buster') {
1643 $pgversion = $opts->{version
} if $opts->{version
};
1645 die "unsupported postgres version '$pgversion'\n"
1646 if !grep { $pgversion eq $_; } @supp;
1648 my $rootdir = $self->{rootfs
};
1650 my $required = $self->compute_required (["postgresql-$pgversion"]);
1652 $self->cache_packages ($required);
1654 $self->ve_dpkg ('install', @$required);
1656 my $iscript = "postgresql-$pgversion";
1657 if ($suite eq 'squeeze' || $suite eq 'wheezy' || $suite eq 'jessie' ||
1658 $suite eq 'stretch') {
1659 $iscript = 'postgresql';
1662 $self->ve_command ("/etc/init.d/$iscript start") if $opts->{start
};
1666 my ($self, $opts) = @_;
1668 my $password = $opts->{password
};
1669 my $rootdir = $self->{rootfs
};
1671 my $suite = $self->{config
}->{suite
};
1674 if ($suite eq 'squeeze') {
1676 } elsif ($suite eq 'wheezy' || $suite eq 'jessie') {
1679 die "task_mysql: unsupported suite '$suite'";
1682 my $required = $self->compute_required (['mysql-common', "mysql-server-$ver"]);
1684 $self->cache_packages ($required);
1686 $self->ve_dpkg ('install', @$required);
1688 # fix security (see /usr/bin/mysql_secure_installation)
1689 my $sql = "DELETE FROM mysql.user WHERE User='';\n" .
1690 "DELETE FROM mysql.user WHERE User='root' AND Host!='localhost';\n" .
1691 "FLUSH PRIVILEGES;\n";
1692 $self->ve_mysql_bootstrap ($sql);
1696 my $rpw = $password eq 'random' ?
'admin' : $password;
1698 my $sql = "USE mysql;\n" .
1699 "UPDATE user SET password=PASSWORD(\"$rpw\") WHERE user
='root';\n" .
1700 "FLUSH PRIVILEGES
;\n";
1701 $self->ve_mysql_bootstrap ($sql);
1703 write_file ("[client
]\nuser
=root
\npassword
=\"$rpw\"\n", "$rootdir/root/.my.cnf", 0600);
1704 if ($password eq 'random') {
1705 $self->install_init_script ($script_mysql_randompw, 2, 20);
1709 $self->ve_command ("/etc/init.d/mysql start") if $opts->{start
};
1713 my ($self, $opts) = @_;
1715 my $memlimit = $opts->{memlimit
};
1716 my $rootdir = $self->{rootfs
};
1718 my $required = $self->compute_required ([qw
(php5 php5-cli libapache2-mod-php5 php5-gd
)]);
1720 $self->cache_packages ($required);
1722 $self->ve_dpkg ('install', @$required);
1725 $self->run_command ("sed -e 's/^\\s*memory_limit\\s*=.*;/memory_limit = ${memlimit}M;/' -i $rootdir/etc/php5/apache2/php.ini");
1730 my ($self, $pkglist, $unpack) = @_;
1732 my $required = $self->compute_required ($pkglist);
1734 $self->cache_packages ($required);
1736 $self->ve_dpkg ($unpack ?
'unpack' : 'install', @$required);
1740 my ($self, $distclean) = @_;
1742 unlink $self->{logfile
};
1743 unlink "$self->{targetname}.tar";
1744 unlink "$self->{targetname}.tar.gz";
1746 $self->ve_destroy ();
1749 rmtree
$self->{cachedir
} if $distclean && !$self->{config
}->{cachedir
};
1751 rmtree
$self->{infodir
};