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
{
63 # systemd: true (except for devuan ostypes)
64 my $supported_suites = {
66 ostype
=> "debian-12",
69 ostype
=> "debian-11",
72 ostype
=> "debian-10",
75 ostype
=> "debian-9.0",
78 ostype
=> "debian-8.0",
84 ostype
=> "debian-7.0",
90 ostype
=> "debian-6.0",
96 ostype
=> "debian-5.0",
102 ostype
=> "debian-4.0",
105 # DEVUAN (imply systemd = 0 default)
108 ostype
=> "devuan-1.0",
112 ostype
=> "devuan-2.0",
115 ostype
=> "devuan-2.0",
118 ostype
=> "devuan-3.0",
121 ostype
=> "devuan-4.0",
124 ostype
=> "devuan-5.0",
132 ostype
=> "ubuntu-8.04",
139 ostype
=> "ubuntu-8.10",
146 ostype
=> "ubuntu-9.04",
153 ostype
=> "ubuntu-12.04",
160 ostype
=> "ubuntu-14.04",
164 ostype
=> "ubuntu-15.04",
168 ostype
=> "ubuntu-15.10",
172 ostype
=> "ubuntu-16.04",
176 ostype
=> "ubuntu-16.10",
180 ostype
=> "ubuntu-17.04",
184 ostype
=> "ubuntu-17.10",
188 ostype
=> "ubuntu-18.04",
192 ostype
=> "ubuntu-18.10",
196 ostype
=> "ubuntu-19.04",
200 ostype
=> "ubuntu-19.10",
204 ostype
=> "ubuntu-20.04",
208 ostype
=> "ubuntu-20.10",
212 ostype
=> "ubuntu-21.04",
216 ostype
=> "ubuntu-21.10",
220 ostype
=> "ubuntu-22.04",
228 my $suiteinfo = $supported_suites->{$suite} || die "unsupported suite '$suite'!\n";
231 $suiteinfo->{origin
} //= 'debian';
232 $suiteinfo->{suite
} //= $suite;
234 $suiteinfo->{flags
} //= {};
235 if ($suiteinfo->{ostype
} =~ /^devuan/) {
236 $suiteinfo->{flags
}->{systemd
} //= 0;
238 $suiteinfo->{flags
}->{systemd
} //= 1;
245 my ($self, $url, $path) = @_;
246 my $tmpfn = "$path.tmp$$";
248 $self->logmsg ("download: $url\n");
250 eval { $self->run_command ("wget -q '$url' -O '$tmpfn'") };
256 rename ($tmpfn, $path);
260 my ($data, $file, $perm) = @_;
262 die "no filename" if !$file;
266 my $fh = IO
::File-
>new ($file, O_WRONLY
| O_CREAT
, $perm) ||
267 die "unable to open file '$file'";
277 die "no filename" if !$file;
279 my $fh = IO
::File-
>new ($file) ||
280 die "unable to open file '$file'";
282 local $/; # slurp mode
296 my $fh = IO
::File-
>new ("<$filename") || return $res;
299 while (defined (my $line = <$fh>)) {
300 next if $line =~ m/^\#/;
301 next if $line =~ m/^\s*$/;
311 if ($rec =~ s/^Description:\s*([^\n]*)(\n\s+.*)*$//si) {
312 $res->{headline
} = $1;
313 chomp $res->{headline
};
316 $res->{description
} = $long;
317 chomp $res->{description
};
318 } elsif ($rec =~ s/^([^:]+):\s*(.*\S)\s*\n//) {
319 my ($key, $value) = (lc ($1), $2);
320 if ($key eq 'source' || $key eq 'mirror') {
321 push @{$res->{$key}}, $value;
323 die "duplicate key '$key'\n" if defined ($res->{$key});
324 $res->{$key} = $value;
327 die "unable to parse config file: $rec";
331 die "unable to parse config file" if $rec;
337 my ($self, $cmd, $input, $getoutput) = @_;
339 my $reader = IO
::File-
>new();
340 my $writer = IO
::File-
>new();
341 my $error = IO
::File-
>new();
345 my $cmdstr = ref ($cmd) eq 'ARRAY' ?
join (' ', @$cmd) : $cmd;
349 if (ref ($cmd) eq 'ARRAY') {
350 $pid = open3
($writer, $reader, $error, @$cmd) || die $!;
352 $pid = open3
($writer, $reader, $error, $cmdstr) || die $!;
359 if ($orig_pid != $$) {
360 $self->logmsg ("ERROR: command '$cmdstr' failed - fork failed\n");
367 print $writer $input if defined $input;
370 my $select = IO
::Select-
>new();
371 $select->add ($reader);
372 $select->add ($error);
375 my $logfd = $self->{logfd
};
377 while ($select->count) {
378 my @handles = $select->can_read ();
380 foreach my $h (@handles) {
382 my $count = sysread ($h, $buf, 4096);
383 if (!defined ($count)) {
385 die "command '$cmdstr' failed: $!";
387 $select->remove ($h) if !$count;
391 $res .= $buf if $getoutput;
398 die "command '$cmdstr' failed with exit code $ec\n" if $ec;
406 $self->writelog (@_);
411 my $fd = $self->{logfd
};
415 sub __sample_config
{
419 my $arch = $self->{config
}->{architecture
};
421 my $ostype = $self->{config
}->{ostype
};
423 if ($ostype =~ m/^de(bi|vu)an-/) {
424 $data .= "lxc.include = /usr/share/lxc/config/debian.common.conf\n";
425 } elsif ($ostype =~ m/^ubuntu-/) {
426 $data .= "lxc.include = /usr/share/lxc/config/ubuntu.common.conf\n";
428 die "unknown os type '$ostype'\n";
430 $data .= "lxc.uts.name = localhost\n";
431 $data .= "lxc.rootfs.path = $self->{rootfs}\n";
440 if (my $fd = IO
::File-
>new (".veid")) {
447 $self->{working_dir
} = getcwd
;
448 $self->{veconffile
} = "$self->{working_dir}/config";
449 $self->{rootfs
} = "$self->{working_dir}/rootfs";
452 $self->{veid
} = $cid;
458 UUID
::generate
($uuid);
459 UUID
::unparse
($uuid, $uuid_str);
460 $self->{veid
} = $uuid_str;
462 my $fd = IO
::File-
>new (">.veid") ||
463 die "unable to write '.veid'\n";
464 print $fd "$self->{veid}\n";
467 my $cdata = $self->__sample_config();
469 my $fh = IO
::File-
>new ($self->{veconffile
}, O_WRONLY
|O_CREAT
|O_EXCL
) ||
470 die "unable to write lxc config file '$self->{veconffile}' - $!";
474 mkdir $self->{rootfs
} || die "unable to create rootfs - $!";
476 $self->logmsg ("allocated VE $self->{veid}\n");
478 return $self->{veid
};
481 # just use some simple heuristic for now, merge usr for releases newer than ubuntu 21.x or debian 11
485 my $ostype = $self->{config
}->{ostype
};
487 # FIXME: add configuration override posibillity
489 if ($ostype =~ m/^debian-(\d+)/) {
490 return int($1) >= 11;
491 } elsif ($ostype =~ m/^ubuntu-(\d+)/) {
492 return int($1) >= 21;
497 sub setup_usr_merge
{
500 my $rootfs = $self->{rootfs
};
501 my $arch = $self->{config
}->{architecture
};
503 # similar to https://salsa.debian.org/installer-team/debootstrap/-/blob/master/functions#L1354
504 my @merged_dirs = qw(bin sbin lib);
506 if ($arch eq 'amd64') {
507 push @merged_dirs, qw(lib32 lib64 libx32);
508 } elsif ($arch eq 'i386') {
509 push @merged_dirs, qw(lib64 libx32);
512 $self->logmsg ("setup usr-merge symlinks for '" . join("', '", @merged_dirs) . "'\n");
514 for my $dir (@merged_dirs) {
515 symlink("usr/$dir", "$rootfs/$dir") or warn "could not create symlink - $!\n";
516 mkpath
"$rootfs/usr/$dir";
521 my ($class, $config) = @_;
523 $class = ref ($class) || $class;
527 $config = read_config
('dab.conf') if !$config;
529 $self->{config
} = $config;
533 $self->{logfile
} = "logfile";
534 $self->{logfd
} = IO
::File-
>new (">>$self->{logfile}") ||
535 die "unable to open log file";
537 my $arch = $config->{architecture
};
538 die "no 'architecture' specified\n" if !$arch;
540 die "unsupported architecture '$arch'\n"
541 if $arch !~ m/^(i386|amd64)$/;
543 my $suite = $config->{suite
} || die "no 'suite' specified\n";
545 my $suiteinfo = get_suite_info
($suite);
546 $suite = $suiteinfo->{suite
};
547 $config->{ostype
} = $suiteinfo->{ostype
};
549 my $name = $config->{name
} || die "no 'name' specified\n";
551 $name =~ m/^[a-z][0-9a-z\-\*\.]+$/ ||
552 die "illegal characters in name '$name'\n";
554 my $version = $config->{version
};
555 die "no 'version' specified\n" if !$version;
556 die "no 'section' specified\n" if !$config->{section
};
557 die "no 'description' specified\n" if !$config->{headline
};
558 die "no 'maintainer' specified\n" if !$config->{maintainer
};
560 if ($name =~ m/^$config->{ostype}/) {
561 $self->{targetname
} = "${name}_${version}_$config->{architecture}";
563 $self->{targetname
} = "$config->{ostype}-${name}_" .
564 "${version}_$config->{architecture}";
567 if (!$config->{source
}) {
568 if (lc($suiteinfo->{origin
}) eq 'debian') {
569 if ($suite eq 'etch' || $suite eq 'lenny') {
570 push @{$config->{source
}}, (
571 'http://ftp.debian.org/debian SUITE main contrib',
572 'http://security.debian.org SUITE/updates main contrib',
574 } elsif ($suite eq 'bullseye' || $suite eq 'bookworm') {
575 push @{$config->{source
}}, (
576 "http://ftp.debian.org/debian SUITE main contrib",
577 "http://ftp.debian.org/debian SUITE-updates main contrib",
578 "http://security.debian.org SUITE-security main contrib",
581 push @{$config->{source
}}, (
582 "http://ftp.debian.org/debian SUITE main contrib",
583 "http://ftp.debian.org/debian SUITE-updates main contrib",
584 "http://security.debian.org SUITE/updates main contrib",
587 } elsif (lc($suiteinfo->{origin
}) eq 'ubuntu') {
588 my $comp = "main restricted universe multiverse";
589 push @{$config->{source
}}, (
590 "http://archive.ubuntu.com/ubuntu SUITE $comp",
591 "http://archive.ubuntu.com/ubuntu SUITE-updates $comp",
592 "http://archive.ubuntu.com/ubuntu SUITE-security $comp",
601 foreach my $s (@{$config->{source
}}) {
602 if ($s =~ m
@^\s*((http
|ftp
)://\S
+)\s
+(\S
+)((\s
+(\S
+))+)$@) {
603 my ($url, $su, $components) = ($1, $3, $4);
604 $su =~ s/SUITE/$suite/;
605 $components =~ s/^\s+//;
606 $components =~ s/\s+$//;
608 foreach my $co (split (/\s+/, $components)) {
611 $ca = ['main'] if !$ca;
619 die "syntax error in source spezification '$s'\n";
623 foreach my $m (@{$config->{mirror
}}) {
624 if ($m =~ m
@^\s*((http
|ftp
)://\S
+)\s
*=>\s
*((http
|ftp
)://\S
+)\s
*$@) {
625 my ($ms, $md) = ($1, $3);
627 foreach my $ss (@$sources) {
628 if ($ss->{source
} eq $ms) {
634 die "unusable mirror $ms\n" if !$found;
636 die "syntax error in mirror spezification '$m'\n";
640 $self->{sources
} = $sources;
642 $self->{infodir
} = "info";
644 $self->__allocate_ve();
646 $self->{cachedir
} = ($config->{cachedir
} || 'cache') . "/$suite";;
648 my $incl = [qw
(less ssh openssh-server logrotate
)];
650 my $excl = [qw
(modutils reiserfsprogs ppp pppconfig pppoe pppoeconf nfs-common mtools ntp
)];
652 # ubuntu has too many dependencies on udev, so
653 # we cannot exclude it (instead we disable udevd)
655 if (lc($suiteinfo->{origin
}) eq 'ubuntu' && $suiteinfo->{flags
}->{systemd
}) {
656 push @$incl, 'isc-dhcp-client';
657 push @$excl, qw(libmodule-build-perl plymouth plymouth-theme-ubuntu-text powermgmt-base);
658 if ($suite eq 'jammy') {
659 push @$excl, qw(fuse); # avoid fuse2 <-> fuse3 conflict
661 } elsif ($suite eq 'trusty') {
662 push @$excl, qw(systemd systemd-services libpam-systemd libsystemd-daemon0 memtest86+);
663 } elsif ($suite eq 'precise') {
664 push @$excl, qw(systemd systemd-services libpam-systemd libsystemd-daemon0 memtest86+ ubuntu-standard);
665 } elsif ($suite eq 'hardy') {
666 push @$excl, qw(kbd);
667 push @$excl, qw(apparmor apparmor-utils ntfs-3g friendly-recovery);
668 } elsif ($suite eq 'intrepid' || $suite eq 'jaunty') {
669 push @$excl, qw(apparmor apparmor-utils libapparmor1 libapparmor-perl libntfs-3g28);
670 push @$excl, qw(ntfs-3g friendly-recovery);
671 } elsif ($suite eq 'jessie') {
672 push @$incl, 'sysvinit-core'; # avoid systemd and udev
673 push @$incl, 'libperl4-corelibs-perl'; # to make lsof happy
674 push @$excl, qw(systemd systemd-sysv udev module-init-tools pciutils hdparm memtest86+ parted);
675 } elsif ($suite eq 'stretch' || $suite eq 'buster' || $suite eq 'bullseye' || $suite eq 'bookworm') {
676 push @$excl, qw(module-init-tools pciutils hdparm memtest86+ parted);
678 push @$excl, qw(udev module-init-tools pciutils hdparm memtest86+ parted);
681 $self->{incl
} = $incl;
682 $self->{excl
} = $excl;
690 my $infodir = $self->{infodir
};
691 my $arch = $self->{config
}->{architecture
};
697 my $logfd = $self->{logfd
} = IO
::File-
>new (">$self->{logfile}") ||
698 die "unable to open log file";
711 foreach my $ss (@{$self->{sources
}}) {
712 my $src = $ss->{mirror
} || $ss->{source
};
713 my $path = "dists/$ss->{suite}/Release";
714 my $url = "$src/$path";
715 my $target = __url_to_filename
("$ss->{source}/$path");
717 $self->download ($url, "$infodir/$target");
718 $self->download ("$url.gpg", "$infodir/$target.gpg");
719 # fixme: impl. verify (needs --keyring option)
723 warn "Release info ignored\n";
726 foreach my $comp (@{$ss->{comp
}}) {
727 foreach my $compressor (@$COMPRESSORS) {
728 $path = "dists/$ss->{suite}/$comp/binary-$arch/Packages.$compressor->{ext}";
729 $target = "$infodir/" . __url_to_filename
("$ss->{source}/$path");
730 my $pkgsrc = "$src/$path";
732 $self->download ($pkgsrc, $target);
733 $self->run_command ("$compressor->{decomp} '$target'");
736 print $logfd "could not download Packages.$compressor->{ext}\n";
746 my ($self, $filename, $size) = @_;
748 my $config = $self->{config
};
752 $data .= "Name: $config->{name}\n";
753 $data .= "Version: $config->{version}\n";
754 $data .= "Type: lxc\n";
755 $data .= "OS: $config->{ostype}\n";
756 $data .= "Section: $config->{section}\n";
757 $data .= "Maintainer: $config->{maintainer}\n";
758 $data .= "Architecture: $config->{architecture}\n";
759 $data .= "Installed-Size: $size\n";
762 $data .= "Infopage: $config->{infopage}\n" if $config->{infopage
};
763 $data .= "ManageUrl: $config->{manageurl}\n" if $config->{manageurl
};
764 $data .= "Certified: $config->{certified}\n" if $config->{certified
};
767 $data .= "Description: $config->{headline}\n";
768 $data .= "$config->{description}\n" if $config->{description
};
770 write_file
($data, $filename, 0644);
774 my ($self, $opts) = @_;
776 my $suite = $self->{config
}->{suite
};
777 my $infodir = $self->{infodir
};
778 my $arch = $self->{config
}->{architecture
};
780 my $instpkgs = $self->read_installed ();
781 my $pkginfo = $self->pkginfo();
782 my $veid = $self->{veid
};
783 my $conffile = $self->{veconffile
};
784 my $rootdir = $self->{rootfs
};
786 my $vestat = $self->ve_status();
787 die "ve not running - unable to finalize\n" if !$vestat->{running
};
790 if (-f
"$rootdir/etc/init.d/mysql") {
791 $self->ve_command ("/etc/init.d/mysql stop");
794 if (!($opts->{keepmycnf
} || (-f
"$rootdir/etc/init.d/mysql_randompw"))) {
795 unlink "$rootdir/root/.my.cnf";
798 if ($suite eq 'etch') {
799 # enable apache2 startup
800 if ($instpkgs->{apache2
}) {
801 write_file
("NO_START=0\n", "$rootdir/etc/default/apache2");
803 unlink "$rootdir/etc/default/apache2";
806 $self->logmsg ("cleanup package status\n");
807 # prevent auto selection of all standard, required or important
808 # packages which are not installed
809 foreach my $pkg (keys %$pkginfo) {
810 my $pri = $pkginfo->{$pkg}->{priority
};
811 if ($pri && ($pri eq 'required' || $pri eq 'important'
812 || $pri eq 'standard')) {
813 if (!$instpkgs->{$pkg}) {
814 $self->ve_dpkg_set_selection ($pkg, 'purge');
819 $self->ve_command ("apt-get clean");
821 $self->logmsg ("update available package list\n");
823 $self->ve_command ("dpkg --clear-avail");
824 foreach my $ss (@{$self->{sources
}}) {
825 my $relsrc = __url_to_filename
("$ss->{source}/dists/$ss->{suite}/Release");
826 if (-f
"$infodir/$relsrc" && -f
"$infodir/$relsrc.gpg") {
827 $self->run_command ("cp '$infodir/$relsrc' '$rootdir/var/lib/apt/lists/$relsrc'");
828 $self->run_command ("cp '$infodir/$relsrc.gpg' '$rootdir/var/lib/apt/lists/$relsrc.gpg'");
830 foreach my $comp (@{$ss->{comp
}}) {
831 my $src = __url_to_filename
("$ss->{source}/dists/$ss->{suite}/" .
832 "$comp/binary-$arch/Packages");
833 my $target = "/var/lib/apt/lists/$src";
834 $self->run_command ("cp '$infodir/$src' '$rootdir/$target'");
835 $self->ve_command ("dpkg --merge-avail '$target'");
839 # set dselect default method
840 write_file
("apt apt\n", "$rootdir/var/lib/dpkg/cmethopt");
842 $self->ve_divert_remove ("/usr/sbin/policy-rc.d");
844 $self->ve_divert_remove ("/sbin/start-stop-daemon");
846 $self->ve_divert_remove ("/sbin/init");
848 # finally stop the VE
849 $self->run_command ("lxc-stop -n $veid --rcfile $conffile --kill");
851 unlink "$rootdir/sbin/defenv";
852 unlink <$rootdir/root/dead.letter
*>;
853 unlink "$rootdir/var/log/init.log";
854 unlink "$rootdir/aquota.group", "$rootdir/aquota.user";
856 write_file
("", "$rootdir/var/log/syslog");
858 my $get_path_size = sub {
860 my $sizestr = $self->run_command ("du -sm $path", undef, 1);
861 if ($sizestr =~ m/^(\d+)\s+\Q$path\E$/) {
864 die "unable to detect size for '$path'\n";
868 $self->logmsg ("detecting final appliance size: ");
869 my $size = $get_path_size->($rootdir);
870 $self->logmsg ("$size MB\n");
872 $self->write_config ("$rootdir/etc/appliance.info", $size);
874 $self->logmsg ("creating final appliance archive\n");
876 my $target = "$self->{targetname}.tar";
878 my $compressor = $opts->{compressor
} // 'gz';
879 my $compressor2cmd_map = {
884 'zstd-max' => 'zstd -19 -T0', # maximal level where the decompressor can still run efficiently
886 my $compressor2ending = {
891 my $compressor_cmd = $compressor2cmd_map->{$compressor};
892 die "unkown compressor '$compressor', use one of: ". join(', ', sort keys %$compressor2cmd_map)
893 if !defined($compressor_cmd);
895 my $ending = $compressor2ending->{$compressor} // $compressor;
896 my $final_archive = "${target}.${ending}";
898 unlink $final_archive;
900 $self->run_command ("tar cpf $target --numeric-owner -C '$rootdir' ./etc/appliance.info");
901 $self->run_command ("tar rpf $target --numeric-owner -C '$rootdir' --exclude ./etc/appliance.info .");
902 $self->run_command ("$compressor_cmd $target");
904 $self->logmsg ("detecting final commpressed appliance size: ");
905 $size = $get_path_size->($final_archive);
906 $self->logmsg ("$size MB\n");
908 $self->logmsg ("appliance archive: $final_archive\n");
914 my $rootdir = $self->{rootfs
};
916 my $pkgfilelist = "$rootdir/var/lib/dpkg/status";
918 open(my $PKGLST, '<', $pkgfilelist) or die "unable to open '$pkgfilelist' - $!";
922 while (my $rec = <$PKGLST>) {
928 while ($rec =~ s/^([^:]+):\s+(.*?)\s*\n//) {
932 my $pkg = $res->{'package'};
933 if (my $status = $res->{status
}) {
934 my @sa = split (/\s+/, $status);
936 if ($stat && ($stat ne 'purge')) {
937 $pkglist->{$pkg} = $res;
950 my $veid = $self->{veid
};
952 my $res = { running
=> 0 };
954 $res->{exist
} = 1 if -d
"$self->{rootfs}/usr";
956 my $filename = "/proc/net/unix";
958 # similar test is used by lcxcontainers.c: list_active_containers
959 my $fh = IO
::File-
>new ($filename, "r");
962 while (defined(my $line = <$fh>)) {
963 if ($line =~ m/^[a-f0-9]+:\s\S+\s\S+\s\S+\s\S+\s\S+\s\d+\s(\S+)$/) {
965 if ($path =~ m!^@/\S+/$veid/command$!) {
976 my ($self, $cmd, $input) = @_;
978 my $veid = $self->{veid
};
979 my $conffile = $self->{veconffile
};
981 if (ref ($cmd) eq 'ARRAY') {
982 unshift @$cmd, 'lxc-attach', '-n', $veid, '--rcfile', $conffile, '--clear-env', '--', 'defenv';
983 $self->run_command($cmd, $input);
985 $self->run_command("lxc-attach -n $veid --rcfile $conffile --clear-env -- defenv $cmd", $input);
989 # like ve_command, but pipes stdin correctly
991 my ($self, @cmd) = @_;
993 my $veid = $self->{veid
};
994 my $conffile = $self->{veconffile
};
997 my $pid = open2
($reader, "<&STDIN", 'lxc-attach', '-n', $veid, '--rcfile', $conffile, '--',
998 'defenv', @cmd) || die "unable to exec command";
1000 while (defined (my $line = <$reader>)) {
1001 $self->logmsg ($line);
1007 die "ve_exec failed - status $rc\n" if $rc != 0;
1011 my ($self, $filename) = @_;
1013 $self->ve_command ("dpkg-divert --add --divert '$filename.distrib' " .
1014 "--rename '$filename'");
1016 sub ve_divert_remove
{
1017 my ($self, $filename) = @_;
1019 my $rootdir = $self->{rootfs
};
1021 unlink "$rootdir/$filename";
1022 $self->ve_command ("dpkg-divert --remove --rename '$filename'");
1025 sub ve_debconfig_set
{
1026 my ($self, $dcdata) = @_;
1028 my $rootdir = $self->{rootfs
};
1029 my $cfgfile = "/tmp/debconf.txt";
1030 write_file
($dcdata, "$rootdir/$cfgfile");
1031 $self->ve_command ("debconf-set-selections $cfgfile");
1032 unlink "$rootdir/$cfgfile";
1035 sub ve_dpkg_set_selection
{
1036 my ($self, $pkg, $status) = @_;
1038 $self->ve_command ("dpkg --set-selections", "$pkg $status");
1042 my ($self, $cmd, @pkglist) = @_;
1044 return if !scalar (@pkglist);
1046 my $pkginfo = $self->pkginfo();
1048 my $rootdir = $self->{rootfs
};
1049 my $cachedir = $self->{cachedir
};
1053 foreach my $pkg (@pkglist) {
1054 my $filename = $self->getpkgfile ($pkg);
1055 $self->run_command ("cp '$cachedir/$filename' '$rootdir/$filename'");
1056 push @files, "/$filename";
1057 $self->logmsg ("$cmd: $pkg\n");
1060 my $fl = join (' ', @files);
1062 if ($cmd eq 'install') {
1063 $self->ve_command ("dpkg --force-depends --force-confold --install $fl");
1064 } elsif ($cmd eq 'unpack') {
1065 $self->ve_command ("dpkg --force-depends --unpack $fl");
1067 die "internal error";
1070 foreach my $fn (@files) { unlink "$rootdir$fn"; }
1076 my $veid = $self->{veid
}; # fixme
1077 my $conffile = $self->{veconffile
};
1079 my $vestat = $self->ve_status();
1080 if ($vestat->{running
}) {
1081 $self->run_command ("lxc-stop -n $veid --rcfile $conffile --kill");
1084 rmtree
$self->{rootfs
};
1085 unlink $self->{veconffile
};
1091 my $veid = $self->{veid
};
1092 my $conffile = $self->{veconffile
};
1094 $self->logmsg ("initialize VE $veid\n");
1096 my $vestat = $self->ve_status();
1097 if ($vestat->{running
}) {
1098 $self->run_command ("lxc-stop -n $veid --rcfile $conffile --kill");
1101 rmtree
$self->{rootfs
};
1102 mkpath
$self->{rootfs
};
1105 sub __deb_version_cmp
{
1106 my ($cur, $op, $new) = @_;
1108 if (system("dpkg", "--compare-versions", $cur, $op, $new) == 0) {
1115 sub __parse_packages
{
1116 my ($pkginfo, $filename, $src) = @_;
1119 open(my $PKGLST, '<', $filename) or die "unable to open '$filename' - $!";
1121 while (my $rec = <$PKGLST>) {
1122 $rec =~ s/\n\s+/ /g;
1128 while ($rec =~ s/^([^:]+):\s+(.*?)\s*\n//) {
1132 my $pkg = $res->{'package'};
1133 if ($pkg && $res->{'filename'}) {
1135 if (my $info = $pkginfo->{$pkg}) {
1136 $cur = $info->{version
};
1138 my $new = $res->{version
};
1139 if (!$cur || __deb_version_cmp
($cur, 'lt', $new)) {
1141 $res->{url
} = "$src/$res->{'filename'}";
1143 die "no url for package '$pkg'" if !$res->{url
};
1145 $pkginfo->{$pkg} = $res;
1156 return $self->{pkginfo
} if $self->{pkginfo
};
1158 my $infodir = $self->{infodir
};
1159 my $arch = $self->{config
}->{architecture
};
1161 my $availfn = "$infodir/available";
1166 # reading 'available' is faster, because it only contains latest version
1167 # (no need to do slow version compares)
1169 __parse_packages
($pkginfo, $availfn);
1170 $self->{pkginfo
} = $pkginfo;
1174 $self->logmsg ("generating available package list\n");
1176 foreach my $ss (@{$self->{sources
}}) {
1177 foreach my $comp (@{$ss->{comp
}}) {
1178 my $url = "$ss->{source}/dists/$ss->{suite}/$comp/binary-$arch/Packages";
1179 my $pkgfilelist = "$infodir/" . __url_to_filename
($url);
1181 my $src = $ss->{mirror
} || $ss->{source
};
1183 __parse_packages
($pkginfo, $pkgfilelist, $src);
1187 if (my $dep = $self->{config
}->{depends
}) {
1188 foreach my $d (split (/,/, $dep)) {
1189 if ($d =~ m/^\s*(\S+)\s*(\((\S+)\s+(\S+)\)\s*)?$/) {
1190 my ($pkg, $op, $rver) = ($1, $3, $4);
1191 $self->logmsg ("checking dependencies: $d\n");
1192 my $info = $pkginfo->{$pkg};
1193 die "package '$pkg' not available\n" if !$info;
1195 my $cver = $info->{version
};
1196 if (!__deb_version_cmp
($cver, $op, $rver)) {
1197 die "detected wrong version '$cver'\n";
1201 die "syntax error in depends field";
1206 $self->{pkginfo
} = $pkginfo;
1208 my $tmpfn = "$availfn.tmp$$";
1209 my $fd = IO
::File-
>new (">$tmpfn");
1210 foreach my $pkg (sort keys %$pkginfo) {
1211 my $info = $pkginfo->{$pkg};
1212 print $fd "package: $pkg\n";
1213 foreach my $k (sort keys %$info) {
1214 next if $k eq 'description';
1215 next if $k eq 'package';
1216 my $v = $info->{$k};
1217 print $fd "$k: $v\n" if $v;
1219 print $fd "description: $info->{description}\n" if $info->{description
};
1224 rename ($tmpfn, $availfn);
1229 sub __record_provides
{
1230 my ($pkginfo, $closure, $list, $skipself) = @_;
1232 foreach my $pname (@$list) {
1233 my $info = $pkginfo->{$pname};
1234 # fixme: if someone install packages directly using dpkg, there
1235 # is no entry in 'available', only in 'status'. In that case, we
1236 # should extract info from $instpkgs
1238 warn "hint: ignoring provides for '$pname' - package not in 'available' list.\n";
1241 if (my $prov = $info->{provides
}) {
1242 my @pl = split (',', $prov);
1243 foreach my $p (@pl) {
1245 if (!($skipself && (grep { $1 eq $_ } @$list))) {
1250 $closure->{$pname} = 1 if !$skipself;
1255 my ($self, $closure, $list) = @_;
1257 my $pkginfo = $self->pkginfo();
1259 # first, record provided packages
1260 __record_provides
($pkginfo, $closure, $list, 1);
1265 # then resolve dependencies
1266 foreach my $pname (@$list) {
1267 __closure_single
($pkginfo, $closure, $pkghash, $pkglist, $pname, $self->{excl
});
1273 sub __closure_single
{
1274 my ($pkginfo, $closure, $pkghash, $pkglist, $pname, $excl) = @_;
1278 $pname =~ s/:any$//;
1280 return if $closure->{$pname};
1282 my $info = $pkginfo->{$pname} || die "no such package '$pname'";
1284 my $dep = $info->{depends
};
1285 my $predep = $info->{'pre-depends'};
1287 my $size = $info->{size
};
1288 my $url = $info->{url
};
1290 $url || die "$pname: no url for package '$pname'";
1292 if (!$pkghash->{$pname}) {
1293 unshift @$pkglist, $pname;
1294 $pkghash->{$pname} = 1;
1297 __record_provides
($pkginfo, $closure, [$pname]) if $info->{provides
};
1299 $closure->{$pname} = 1;
1305 push @l, split (/,/, $predep) if $predep;
1306 push @l, split (/,/, $dep) if $dep;
1308 DEPEND
: foreach my $p (@l) {
1309 my @l1 = split (/\|/, $p);
1310 foreach my $p1 (@l1) {
1311 if ($p1 =~ m/^\s*(\S+).*/) {
1312 #printf (STDERR "$pname: $p --> $1\n");
1313 if ($closure->{$1}) {
1314 next DEPEND
; # dependency already met
1318 # search for non-excluded alternative
1320 foreach my $p1 (@l1) {
1321 if ($p1 =~ m/^\s*(\S+).*/) {
1322 next if grep { $1 eq $_ } @$excl;
1327 die "package '$pname' depends on exclusion '$p'\n" if !$found;
1329 #printf (STDERR "$pname: $p --> $found\n");
1331 __closure_single
($pkginfo, $closure, $pkghash, $pkglist, $found, $excl);
1335 sub cache_packages
{
1336 my ($self, $pkglist) = @_;
1338 foreach my $pkg (@$pkglist) {
1339 $self->getpkgfile ($pkg);
1344 my ($self, $pkg) = @_;
1346 my $pkginfo = $self->pkginfo();
1347 my $info = $pkginfo->{$pkg} || die "no such package '$pkg'";
1348 my $cachedir = $self->{cachedir
};
1350 my $url = $info->{url
};
1353 if ($url =~ m
|/([^/]+.deb
)$|) {
1356 die "internal error";
1359 return $filename if -f
"$cachedir/$filename";
1363 $self->download ($url, "$cachedir/$filename");
1368 sub install_init_script
{
1369 my ($self, $script, $runlevel, $prio) = @_;
1371 my $suite = $self->{config
}->{suite
};
1372 my $suiteinfo = get_suite_info
($suite);
1373 my $rootdir = $self->{rootfs
};
1375 my $base = basename
($script);
1376 my $target = "$rootdir/etc/init.d/$base";
1378 $self->run_command ("install -m 0755 '$script' '$target'");
1379 if ($suite eq 'etch' || $suite eq 'lenny') {
1380 $self->ve_command ("update-rc.d $base start $prio $runlevel .");
1381 } elsif ($suiteinfo->{flags
}->{systemd
}) {
1382 die "unable to install init script (system uses systemd)\n";
1383 } elsif ($suite eq 'trusty' || $suite eq 'precise') {
1384 die "unable to install init script (system uses upstart)\n";
1386 $self->ve_command ("insserv $base");
1393 my ($self, $opts) = @_;
1395 my $pkginfo = $self->pkginfo();
1396 my $veid = $self->{veid
};
1397 my $suite = $self->{config
}->{suite
};
1398 my $suiteinfo = get_suite_info
($suite);
1400 my $important = [ @{$self->{incl
}} ];
1404 my $mta = $opts->{exim
} ?
'exim' : 'postfix';
1405 if ($mta eq 'postfix') {
1406 push @$important, "postfix";
1409 if ($opts->{include
}) {
1410 push @$important, split(',', $opts->{include
});
1414 if ($opts->{exclude
}) {
1415 $exclude->{$_} = 1 for split(',', $opts->{exclude
});
1418 foreach my $p (sort keys %$pkginfo) {
1419 next if grep { $p eq $_ } @{$self->{excl
}};
1420 my $pri = $pkginfo->{$p}->{priority
};
1422 next if $mta ne 'exim' && $p =~ m/exim/;
1423 next if $p =~ m/(selinux|semanage|policycoreutils)/;
1425 push @$required, $p if $pri eq 'required';
1426 next if $exclude->{$p};
1427 push @$important, $p if $pri eq 'important';
1428 push @$standard, $p if $pri eq 'standard' && !$opts->{minimal
};
1432 $required = $self->closure($closure, $required);
1433 $important = $self->closure($closure, $important);
1435 if (!$opts->{minimal
}) {
1436 $standard = $self->closure($closure, $standard);
1439 # test if we have all 'ubuntu-minimal' and 'ubuntu-standard' packages
1440 # except those explicitly excluded
1441 if ($suite eq 'hardy' || $suite eq 'intrepid' || $suite eq 'jaunty') {
1442 my $mdeps = $pkginfo->{'ubuntu-minimal'}->{depends
};
1443 foreach my $d (split (/,/, $mdeps)) {
1444 if ($d =~ m/^\s*(\S+)$/) {
1446 next if $closure->{$pkg};
1447 next if grep { $pkg eq $_ } @{$self->{excl
}};
1448 die "missing ubuntu-minimal package '$pkg'\n";
1451 if (!$opts->{minimal
}) {
1452 $mdeps = $pkginfo->{'ubuntu-standard'}->{depends
};
1453 foreach my $d (split (/,/, $mdeps)) {
1454 if ($d =~ m/^\s*(\S+)$/) {
1456 next if $closure->{$pkg};
1457 next if grep { $pkg eq $_ } @{$self->{excl
}};
1458 die "missing ubuntu-standard package '$pkg'\n";
1464 # download/cache all files first
1465 $self->cache_packages ($required);
1466 $self->cache_packages ($important);
1467 $self->cache_packages ($standard);
1469 my $rootdir = $self->{rootfs
};
1471 # extract required packages first
1472 $self->logmsg ("create basic environment\n");
1474 if ($self->can_usr_merge()) {
1475 $self->setup_usr_merge();
1478 my $compressor2opt = {
1483 my $compressor_re = join('|', keys $compressor2opt->%*);
1485 $self->logmsg ("extract required packages to rootfs\n");
1486 foreach my $p (@$required) {
1487 my $filename = $self->getpkgfile ($p);
1488 my $content = $self->run_command("ar -t '$self->{cachedir}/$filename'", undef, 1);
1489 if ($content =~ m/^(data.tar.($compressor_re))$/m) {
1491 my $tar_opts = "--keep-directory-symlink $compressor2opt->{$2}";
1493 $self->run_command("ar -p '$self->{cachedir}/$filename' '$archive' | tar -C '$rootdir' -xf - $tar_opts");
1495 die "unexpected error for $p: no data.tar.{xz,gz,zst} found...";
1500 my $data = "Package: dpkg\n" .
1501 "Version: $pkginfo->{dpkg}->{version}\n" .
1502 "Status: install ok installed\n";
1504 write_file
($data, "$rootdir/var/lib/dpkg/status");
1505 write_file
("", "$rootdir/var/lib/dpkg/info/dpkg.list");
1506 write_file
("", "$rootdir/var/lib/dpkg/available");
1509 foreach my $ss (@{$self->{sources
}}) {
1510 my $url = $ss->{source
};
1511 my $comp = join (' ', @{$ss->{comp
}});
1512 $data .= "deb $url $ss->{suite} $comp\n\n";
1515 write_file
($data, "$rootdir/etc/apt/sources.list");
1517 $data = "# UNCONFIGURED FSTAB FOR BASE SYSTEM\n";
1518 write_file
($data, "$rootdir/etc/fstab", 0644);
1520 write_file
("localhost\n", "$rootdir/etc/hostname", 0644);
1522 # avoid warnings about non-existent resolv.conf
1523 write_file
("", "$rootdir/etc/resolv.conf", 0644);
1525 if (lc($suiteinfo->{origin
}) eq 'ubuntu' && $suiteinfo->{flags
}->{systemd
}) {
1526 # no need to configure loopback device
1527 # FIXME: Debian (systemd based?) too?
1529 $data = "auto lo\niface lo inet loopback\n";
1530 mkdir "$rootdir/etc/network";
1531 write_file
($data, "$rootdir/etc/network/interfaces", 0644);
1535 $self->run_command ("tar xzf '$devicetar' -C '$rootdir'");
1537 # avoid warnings about missing default locale
1538 write_file
("LANG=\"C\"\n", "$rootdir/etc/default/locale", 0644);
1541 rename ("$rootdir/sbin/init", "$rootdir/sbin/init.org");
1542 $self->run_command ("cp '$fake_init' '$rootdir/sbin/init'");
1544 $self->run_command ("cp '$default_env' '$rootdir/sbin/defenv'");
1546 $self->run_command ("lxc-start -n $veid -f $self->{veconffile}");
1548 $self->logmsg ("initialize ld cache\n");
1549 $self->ve_command ("/sbin/ldconfig");
1550 $self->run_command ("ln -sf mawk '$rootdir/usr/bin/awk'");
1552 $self->logmsg ("installing packages\n");
1554 $self->ve_dpkg ('install', 'base-files', 'base-passwd');
1556 $self->ve_dpkg ('install', 'dpkg');
1558 $self->run_command ("ln -sf /usr/share/zoneinfo/UTC '$rootdir/etc/localtime'");
1560 $self->run_command ("ln -sf bash '$rootdir/bin/sh'");
1562 $self->ve_dpkg ('install', 'libc6');
1563 $self->ve_dpkg ('install', 'perl-base');
1565 unlink "$rootdir/usr/bin/awk";
1567 $self->ve_dpkg ('install', 'mawk');
1568 $self->ve_dpkg ('install', 'debconf');
1570 # unpack required packages
1571 foreach my $p (@$required) {
1572 $self->ve_dpkg ('unpack', $p);
1575 rename ("$rootdir/sbin/init.org", "$rootdir/sbin/init");
1576 $self->ve_divert_add ("/sbin/init");
1577 $self->run_command ("cp '$fake_init' '$rootdir/sbin/init'");
1579 # disable service activation
1580 $self->ve_divert_add ("/usr/sbin/policy-rc.d");
1581 $data = "#!/bin/sh\nexit 101\n";
1582 write_file
($data, "$rootdir/usr/sbin/policy-rc.d", 755);
1584 # disable start-stop-daemon
1585 $self->ve_divert_add ("/sbin/start-stop-daemon");
1589 echo \"Warning: Fake start-stop-daemon called, doing nothing\"
1591 write_file
($data, "$rootdir/sbin/start-stop-daemon", 0755);
1594 $self->ve_divert_add ("/sbin/udevd");
1596 if ($suite eq 'etch') {
1597 write_file
("NO_START=1\n", "$rootdir/etc/default/apache2"); # disable apache2 startup
1600 $self->logmsg ("configure required packages\n");
1601 $self->ve_command ("dpkg --force-confold --skip-same-version --configure -a");
1603 # set postfix defaults
1604 if ($mta eq 'postfix') {
1605 $data = "postfix postfix/main_mailer_type select Local only\n";
1606 $self->ve_debconfig_set ($data);
1608 $data = "postmaster: root\nwebmaster: root\n";
1609 write_file
($data, "$rootdir/etc/aliases");
1612 if ($suite eq 'jaunty') {
1613 # jaunty does not create /var/run/network, so network startup fails.
1614 # so we do not use tmpfs for /var/run and /var/lock
1615 $self->run_command ("sed -e 's/RAMRUN=yes/RAMRUN=no/' -e 's/RAMLOCK=yes/RAMLOCK=no/' -i $rootdir/etc/default/rcS");
1616 # and create the directory here
1617 $self->run_command ("mkdir $rootdir/var/run/network");
1620 # unpack base packages
1621 foreach my $p (@$important) {
1622 $self->ve_dpkg ('unpack', $p);
1626 if (-x
"$rootdir/sbin/ifconfig") {
1627 $self->ve_command ("ifconfig lo up");
1629 $self->ve_command ("ip link set lo up");
1632 $self->logmsg ("configure important packages\n");
1633 $self->ve_command ("dpkg --force-confold --skip-same-version --configure -a");
1635 if (-d
"$rootdir/etc/event.d") {
1636 unlink <$rootdir/etc/event.d
/tty
*>;
1639 if (-f
"$rootdir/etc/inittab") {
1640 $self->run_command ("sed -i -e '/getty\\s38400\\stty[23456]/d' '$rootdir/etc/inittab'");
1643 # Link /etc/mtab to /proc/mounts, so df and friends will work:
1644 unlink "$rootdir/etc/mtab";
1645 $self->ve_command ("ln -s /proc/mounts /etc/mtab");
1648 $self->ve_command ("usermod -L root");
1650 if ($mta eq 'postfix') {
1651 $data = "postfix postfix/main_mailer_type select No configuration\n";
1652 $self->ve_debconfig_set ($data);
1654 unlink "$rootdir/etc/mailname";
1655 write_file
($postfix_main_cf, "$rootdir/etc/postfix/main.cf");
1658 if (!$opts->{minimal
}) {
1659 # unpack standard packages
1660 foreach my $p (@$standard) {
1661 $self->ve_dpkg ('unpack', $p);
1664 $self->logmsg ("configure standard packages\n");
1665 $self->ve_command ("dpkg --force-confold --skip-same-version --configure -a");
1668 # disable HWCLOCK access
1669 $self->run_command ("echo 'HWCLOCKACCESS=no' >> '$rootdir/etc/default/rcS'");
1672 $self->ve_divert_add ("/usr/sbin/hald");
1674 # disable /dev/urandom init
1675 $self->run_command ("install -m 0755 '$script_init_urandom' '$rootdir/etc/init.d/urandom'");
1677 if ($suite eq 'etch' || $suite eq 'hardy' || $suite eq 'intrepid' || $suite eq 'jaunty') {
1679 $self->ve_divert_add ("/sbin/klogd");
1682 # remove unnecessays sysctl entries to avoid warnings
1684 $cmd .= ' -e \'s/^\(kernel\.printk.*\)/#\1/\'';
1685 $cmd .= ' -e \'s/^\(kernel\.maps_protect.*\)/#\1/\'';
1686 $cmd .= ' -e \'s/^\(fs\.inotify\.max_user_watches.*\)/#\1/\'';
1687 $cmd .= ' -e \'s/^\(vm\.mmap_min_addr.*\)/#\1/\'';
1688 $cmd .= " -i '$rootdir/etc/sysctl.conf'";
1689 $self->run_command ($cmd);
1691 my $bindv6only = "$rootdir/etc/sysctl.d/bindv6only.conf";
1692 if (-f
$bindv6only) {
1694 $cmd .= ' -e \'s/^\(net\.ipv6\.bindv6only.*\)/#\1/\'';
1695 $cmd .= " -i '$bindv6only'";
1696 $self->run_command ($cmd);
1699 if ($suite eq 'hardy' || $suite eq 'intrepid' || $suite eq 'jaunty') {
1700 # disable tty init (console-setup)
1702 $cmd .= ' -e \'s/^\(ACTIVE_CONSOLES=.*\)/ACTIVE_CONSOLES=/\'';
1703 $cmd .= " -i '$rootdir/etc/default/console-setup'";
1704 $self->run_command ($cmd);
1707 if ($suite eq 'intrepid' || $suite eq 'jaunty') {
1708 # remove sysctl setup (avoid warnings at startup)
1709 my $filelist = "$rootdir/etc/sysctl.d/10-console-messages.conf";
1710 $filelist .= " $rootdir/etc/sysctl.d/10-process-security.conf" if $suite eq 'intrepid';
1711 $filelist .= " $rootdir/etc/sysctl.d/10-network-security.conf";
1712 $self->run_command ("rm $filelist");
1715 if (-e
"$rootdir/lib/systemd/system/sys-kernel-config.mount") {
1716 $self->ve_command ("ln -s /dev/null /etc/systemd/system/sys-kernel-debug.mount");
1723 my $veid = $self->{veid
};
1724 my $conffile = $self->{veconffile
};
1726 my $vestat = $self->ve_status();
1728 if (!$vestat->{exist
}) {
1729 $self->logmsg ("Please create the appliance first (bootstrap)");
1733 if (!$vestat->{running
}) {
1734 $self->run_command ("lxc-start -n $veid -f $conffile");
1737 system ("lxc-attach -n $veid --rcfile $conffile --clear-env");
1740 sub ve_mysql_command
{
1741 my ($self, $sql, $password) = @_;
1743 #my $bootstrap = "/usr/sbin/mysqld --bootstrap --user=mysql --skip-grant-tables " .
1744 #"--skip-bdb --skip-innodb --skip-ndbcluster";
1746 $self->ve_command ("mysql", $sql);
1749 sub ve_mysql_bootstrap
{
1750 my ($self, $sql, $password) = @_;
1754 my $suite = $self->{config
}->{suite
};
1756 if ($suite eq 'jessie') {
1757 my $rootdir = $self->{rootfs
};
1758 $self->run_command ("sed -e 's/^key_buffer\\s*=/key_buffer_size =/' -i $rootdir/etc/mysql/my.cnf");
1761 if ($suite eq 'squeeze' || $suite eq 'wheezy' || $suite eq 'jessie') {
1762 $cmd = "/usr/sbin/mysqld --bootstrap --user=mysql --skip-grant-tables";
1765 $cmd = "/usr/sbin/mysqld --bootstrap --user=mysql --skip-grant-tables " .
1766 "--skip-bdb --skip-innodb --skip-ndbcluster";
1769 $self->ve_command ($cmd, $sql);
1772 sub compute_required
{
1773 my ($self, $pkglist) = @_;
1775 my $pkginfo = $self->pkginfo();
1776 my $instpkgs = $self->read_installed ();
1779 __record_provides
($pkginfo, $closure, [keys $instpkgs->%*]);
1781 return $self->closure ($closure, $pkglist);
1785 my ($self, $opts) = @_;
1787 my @supp = ('7.4', '8.1');
1788 my $pgversion; # NOTE: not setting that defaults to the distro default, normally the best choice
1790 my $suite = $self->{config
}->{suite
};
1792 if ($suite eq 'lenny' || $suite eq 'hardy' || $suite eq 'intrepid' || $suite eq 'jaunty') {
1795 } elsif ($suite eq 'squeeze') {
1798 } elsif ($suite eq 'wheezy') {
1801 } elsif ($suite eq 'jessie') {
1804 } elsif ($suite eq 'stretch') {
1807 } elsif ($suite eq 'buster') {
1810 } elsif ($suite eq 'bullseye') {
1812 } elsif ($suite eq 'bookworm') {
1813 # FIXME: update once froozen
1814 @supp = ('13', '14');
1816 $pgversion = $opts->{version
} if $opts->{version
};
1819 if (defined($pgversion)) {
1820 die "unsupported postgres version '$pgversion'\n" if !grep { $pgversion eq $_; } @supp;
1822 $required = $self->compute_required (["postgresql-$pgversion"]);
1824 $required = $self->compute_required (["postgresql"]);
1827 $self->cache_packages ($required);
1829 $self->ve_dpkg ('install', @$required);
1831 my $iscript = "postgresql-$pgversion";
1832 if ($suite eq 'squeeze' || $suite eq 'wheezy' || $suite eq 'jessie' ||
1833 $suite eq 'stretch') {
1834 $iscript = 'postgresql';
1837 $self->ve_command ("/etc/init.d/$iscript start") if $opts->{start
};
1841 my ($self, $opts) = @_;
1843 my $password = $opts->{password
};
1844 my $rootdir = $self->{rootfs
};
1846 my $suite = $self->{config
}->{suite
};
1849 if ($suite eq 'squeeze') {
1851 } elsif ($suite eq 'wheezy' || $suite eq 'jessie') {
1854 die "task_mysql: unsupported suite '$suite'";
1857 my $required = $self->compute_required (['mysql-common', "mysql-server-$ver"]);
1859 $self->cache_packages ($required);
1861 $self->ve_dpkg ('install', @$required);
1863 # fix security (see /usr/bin/mysql_secure_installation)
1864 my $sql = "DELETE FROM mysql.user WHERE User='';\n" .
1865 "DELETE FROM mysql.user WHERE User='root' AND Host!='localhost';\n" .
1866 "FLUSH PRIVILEGES;\n";
1867 $self->ve_mysql_bootstrap ($sql);
1871 my $rpw = $password eq 'random' ?
'admin' : $password;
1873 my $sql = "USE mysql;\n" .
1874 "UPDATE user SET password=PASSWORD(\"$rpw\") WHERE user
='root';\n" .
1875 "FLUSH PRIVILEGES
;\n";
1876 $self->ve_mysql_bootstrap ($sql);
1878 write_file ("[client
]\nuser
=root
\npassword
=\"$rpw\"\n", "$rootdir/root/.my.cnf", 0600);
1879 if ($password eq 'random') {
1880 $self->install_init_script ($script_mysql_randompw, 2, 20);
1884 $self->ve_command ("/etc/init.d/mysql start") if $opts->{start
};
1888 my ($self, $opts) = @_;
1890 my $memlimit = $opts->{memlimit
};
1891 my $rootdir = $self->{rootfs
};
1893 my $required = $self->compute_required([qw(php php-cli libapache2-mod-php php-gd)]);
1895 $self->cache_packages ($required);
1897 $self->ve_dpkg ('install', @$required);
1900 $self->run_command ("sed -e 's/^\\s*memory_limit\\s*=.*;/memory_limit = ${memlimit}M;/' -i $rootdir/etc/php5/apache2/php.ini");
1905 my ($self, $pkglist, $unpack) = @_;
1907 my $required = $self->compute_required ($pkglist);
1909 $self->cache_packages ($required);
1911 $self->ve_dpkg ($unpack ?
'unpack' : 'install', @$required);
1915 my ($self, $distclean) = @_;
1917 unlink $self->{logfile
};
1918 unlink "$self->{targetname}.tar";
1919 unlink "$self->{targetname}.tar.gz";
1921 $self->ve_destroy ();
1924 rmtree
$self->{cachedir
} if $distclean && !$self->{config
}->{cachedir
};
1926 rmtree
$self->{infodir
};