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 = new IO
::Select
;
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 (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 (PKGLST
, "<$filename") ||
1120 die "unable to open '$filename'";
1122 while (my $rec = <PKGLST
>) {
1123 $rec =~ s/\n\s+/ /g;
1129 while ($rec =~ s/^([^:]+):\s+(.*?)\s*\n//) {
1133 my $pkg = $res->{'package'};
1134 if ($pkg && $res->{'filename'}) {
1136 if (my $info = $pkginfo->{$pkg}) {
1137 $cur = $info->{version
};
1139 my $new = $res->{version
};
1140 if (!$cur || __deb_version_cmp
($cur, 'lt', $new)) {
1142 $res->{url
} = "$src/$res->{'filename'}";
1144 die "no url for package '$pkg'" if !$res->{url
};
1146 $pkginfo->{$pkg} = $res;
1157 return $self->{pkginfo
} if $self->{pkginfo
};
1159 my $infodir = $self->{infodir
};
1160 my $arch = $self->{config
}->{architecture
};
1162 my $availfn = "$infodir/available";
1167 # reading 'available' is faster, because it only contains latest version
1168 # (no need to do slow version compares)
1170 __parse_packages
($pkginfo, $availfn);
1171 $self->{pkginfo
} = $pkginfo;
1175 $self->logmsg ("generating available package list\n");
1177 foreach my $ss (@{$self->{sources
}}) {
1178 foreach my $comp (@{$ss->{comp
}}) {
1179 my $url = "$ss->{source}/dists/$ss->{suite}/$comp/binary-$arch/Packages";
1180 my $pkgfilelist = "$infodir/" . __url_to_filename
($url);
1182 my $src = $ss->{mirror
} || $ss->{source
};
1184 __parse_packages
($pkginfo, $pkgfilelist, $src);
1188 if (my $dep = $self->{config
}->{depends
}) {
1189 foreach my $d (split (/,/, $dep)) {
1190 if ($d =~ m/^\s*(\S+)\s*(\((\S+)\s+(\S+)\)\s*)?$/) {
1191 my ($pkg, $op, $rver) = ($1, $3, $4);
1192 $self->logmsg ("checking dependencies: $d\n");
1193 my $info = $pkginfo->{$pkg};
1194 die "package '$pkg' not available\n" if !$info;
1196 my $cver = $info->{version
};
1197 if (!__deb_version_cmp
($cver, $op, $rver)) {
1198 die "detected wrong version '$cver'\n";
1202 die "syntax error in depends field";
1207 $self->{pkginfo
} = $pkginfo;
1209 my $tmpfn = "$availfn.tmp$$";
1210 my $fd = IO
::File-
>new (">$tmpfn");
1211 foreach my $pkg (sort keys %$pkginfo) {
1212 my $info = $pkginfo->{$pkg};
1213 print $fd "package: $pkg\n";
1214 foreach my $k (sort keys %$info) {
1215 next if $k eq 'description';
1216 next if $k eq 'package';
1217 my $v = $info->{$k};
1218 print $fd "$k: $v\n" if $v;
1220 print $fd "description: $info->{description}\n" if $info->{description
};
1225 rename ($tmpfn, $availfn);
1230 sub __record_provides
{
1231 my ($pkginfo, $closure, $list, $skipself) = @_;
1233 foreach my $pname (@$list) {
1234 my $info = $pkginfo->{$pname};
1235 # fixme: if someone install packages directly using dpkg, there
1236 # is no entry in 'available', only in 'status'. In that case, we
1237 # should extract info from $instpkgs
1239 warn "hint: ignoring provides for '$pname' - package not in 'available' list.\n";
1242 if (my $prov = $info->{provides
}) {
1243 my @pl = split (',', $prov);
1244 foreach my $p (@pl) {
1246 if (!($skipself && (grep { $1 eq $_ } @$list))) {
1251 $closure->{$pname} = 1 if !$skipself;
1256 my ($self, $closure, $list) = @_;
1258 my $pkginfo = $self->pkginfo();
1260 # first, record provided packages
1261 __record_provides
($pkginfo, $closure, $list, 1);
1266 # then resolve dependencies
1267 foreach my $pname (@$list) {
1268 __closure_single
($pkginfo, $closure, $pkghash, $pkglist, $pname, $self->{excl
});
1274 sub __closure_single
{
1275 my ($pkginfo, $closure, $pkghash, $pkglist, $pname, $excl) = @_;
1279 $pname =~ s/:any$//;
1281 return if $closure->{$pname};
1283 my $info = $pkginfo->{$pname} || die "no such package '$pname'";
1285 my $dep = $info->{depends
};
1286 my $predep = $info->{'pre-depends'};
1288 my $size = $info->{size
};
1289 my $url = $info->{url
};
1291 $url || die "$pname: no url for package '$pname'";
1293 if (!$pkghash->{$pname}) {
1294 unshift @$pkglist, $pname;
1295 $pkghash->{$pname} = 1;
1298 __record_provides
($pkginfo, $closure, [$pname]) if $info->{provides
};
1300 $closure->{$pname} = 1;
1306 push @l, split (/,/, $predep) if $predep;
1307 push @l, split (/,/, $dep) if $dep;
1309 DEPEND
: foreach my $p (@l) {
1310 my @l1 = split (/\|/, $p);
1311 foreach my $p1 (@l1) {
1312 if ($p1 =~ m/^\s*(\S+).*/) {
1313 #printf (STDERR "$pname: $p --> $1\n");
1314 if ($closure->{$1}) {
1315 next DEPEND
; # dependency already met
1319 # search for non-excluded alternative
1321 foreach my $p1 (@l1) {
1322 if ($p1 =~ m/^\s*(\S+).*/) {
1323 next if grep { $1 eq $_ } @$excl;
1328 die "package '$pname' depends on exclusion '$p'\n" if !$found;
1330 #printf (STDERR "$pname: $p --> $found\n");
1332 __closure_single
($pkginfo, $closure, $pkghash, $pkglist, $found, $excl);
1336 sub cache_packages
{
1337 my ($self, $pkglist) = @_;
1339 foreach my $pkg (@$pkglist) {
1340 $self->getpkgfile ($pkg);
1345 my ($self, $pkg) = @_;
1347 my $pkginfo = $self->pkginfo();
1348 my $info = $pkginfo->{$pkg} || die "no such package '$pkg'";
1349 my $cachedir = $self->{cachedir
};
1351 my $url = $info->{url
};
1354 if ($url =~ m
|/([^/]+.deb
)$|) {
1357 die "internal error";
1360 return $filename if -f
"$cachedir/$filename";
1364 $self->download ($url, "$cachedir/$filename");
1369 sub install_init_script
{
1370 my ($self, $script, $runlevel, $prio) = @_;
1372 my $suite = $self->{config
}->{suite
};
1373 my $suiteinfo = get_suite_info
($suite);
1374 my $rootdir = $self->{rootfs
};
1376 my $base = basename
($script);
1377 my $target = "$rootdir/etc/init.d/$base";
1379 $self->run_command ("install -m 0755 '$script' '$target'");
1380 if ($suite eq 'etch' || $suite eq 'lenny') {
1381 $self->ve_command ("update-rc.d $base start $prio $runlevel .");
1382 } elsif ($suiteinfo->{flags
}->{systemd
}) {
1383 die "unable to install init script (system uses systemd)\n";
1384 } elsif ($suite eq 'trusty' || $suite eq 'precise') {
1385 die "unable to install init script (system uses upstart)\n";
1387 $self->ve_command ("insserv $base");
1394 my ($self, $opts) = @_;
1396 my $pkginfo = $self->pkginfo();
1397 my $veid = $self->{veid
};
1398 my $suite = $self->{config
}->{suite
};
1399 my $suiteinfo = get_suite_info
($suite);
1401 my $important = [ @{$self->{incl
}} ];
1405 my $mta = $opts->{exim
} ?
'exim' : 'postfix';
1406 if ($mta eq 'postfix') {
1407 push @$important, "postfix";
1410 if ($opts->{include
}) {
1411 push @$important, split(',', $opts->{include
});
1415 if ($opts->{exclude
}) {
1416 $exclude->{$_} = 1 for split(',', $opts->{exclude
});
1419 foreach my $p (sort keys %$pkginfo) {
1420 next if grep { $p eq $_ } @{$self->{excl
}};
1421 my $pri = $pkginfo->{$p}->{priority
};
1423 next if $mta ne 'exim' && $p =~ m/exim/;
1424 next if $p =~ m/(selinux|semanage|policycoreutils)/;
1426 push @$required, $p if $pri eq 'required';
1427 next if $exclude->{$p};
1428 push @$important, $p if $pri eq 'important';
1429 push @$standard, $p if $pri eq 'standard' && !$opts->{minimal
};
1433 $required = $self->closure($closure, $required);
1434 $important = $self->closure($closure, $important);
1436 if (!$opts->{minimal
}) {
1437 $standard = $self->closure($closure, $standard);
1440 # test if we have all 'ubuntu-minimal' and 'ubuntu-standard' packages
1441 # except those explicitly excluded
1442 if ($suite eq 'hardy' || $suite eq 'intrepid' || $suite eq 'jaunty') {
1443 my $mdeps = $pkginfo->{'ubuntu-minimal'}->{depends
};
1444 foreach my $d (split (/,/, $mdeps)) {
1445 if ($d =~ m/^\s*(\S+)$/) {
1447 next if $closure->{$pkg};
1448 next if grep { $pkg eq $_ } @{$self->{excl
}};
1449 die "missing ubuntu-minimal package '$pkg'\n";
1452 if (!$opts->{minimal
}) {
1453 $mdeps = $pkginfo->{'ubuntu-standard'}->{depends
};
1454 foreach my $d (split (/,/, $mdeps)) {
1455 if ($d =~ m/^\s*(\S+)$/) {
1457 next if $closure->{$pkg};
1458 next if grep { $pkg eq $_ } @{$self->{excl
}};
1459 die "missing ubuntu-standard package '$pkg'\n";
1465 # download/cache all files first
1466 $self->cache_packages ($required);
1467 $self->cache_packages ($important);
1468 $self->cache_packages ($standard);
1470 my $rootdir = $self->{rootfs
};
1472 # extract required packages first
1473 $self->logmsg ("create basic environment\n");
1475 if ($self->can_usr_merge()) {
1476 $self->setup_usr_merge();
1479 my $compressor2opt = {
1484 my $compressor_re = join('|', keys $compressor2opt->%*);
1486 $self->logmsg ("extract required packages to rootfs\n");
1487 foreach my $p (@$required) {
1488 my $filename = $self->getpkgfile ($p);
1489 my $content = $self->run_command("ar -t '$self->{cachedir}/$filename'", undef, 1);
1490 if ($content =~ m/^(data.tar.($compressor_re))$/m) {
1492 my $tar_opts = "--keep-directory-symlink $compressor2opt->{$2}";
1494 $self->run_command("ar -p '$self->{cachedir}/$filename' '$archive' | tar -C '$rootdir' -xf - $tar_opts");
1496 die "unexpected error for $p: no data.tar.{xz,gz,zst} found...";
1501 my $data = "Package: dpkg\n" .
1502 "Version: $pkginfo->{dpkg}->{version}\n" .
1503 "Status: install ok installed\n";
1505 write_file
($data, "$rootdir/var/lib/dpkg/status");
1506 write_file
("", "$rootdir/var/lib/dpkg/info/dpkg.list");
1507 write_file
("", "$rootdir/var/lib/dpkg/available");
1510 foreach my $ss (@{$self->{sources
}}) {
1511 my $url = $ss->{source
};
1512 my $comp = join (' ', @{$ss->{comp
}});
1513 $data .= "deb $url $ss->{suite} $comp\n\n";
1516 write_file
($data, "$rootdir/etc/apt/sources.list");
1518 $data = "# UNCONFIGURED FSTAB FOR BASE SYSTEM\n";
1519 write_file
($data, "$rootdir/etc/fstab", 0644);
1521 write_file
("localhost\n", "$rootdir/etc/hostname", 0644);
1523 # avoid warnings about non-existent resolv.conf
1524 write_file
("", "$rootdir/etc/resolv.conf", 0644);
1526 if (lc($suiteinfo->{origin
}) eq 'ubuntu' && $suiteinfo->{flags
}->{systemd
}) {
1527 # no need to configure loopback device
1528 # FIXME: Debian (systemd based?) too?
1530 $data = "auto lo\niface lo inet loopback\n";
1531 mkdir "$rootdir/etc/network";
1532 write_file
($data, "$rootdir/etc/network/interfaces", 0644);
1536 $self->run_command ("tar xzf '$devicetar' -C '$rootdir'");
1538 # avoid warnings about missing default locale
1539 write_file
("LANG=\"C\"\n", "$rootdir/etc/default/locale", 0644);
1542 rename ("$rootdir/sbin/init", "$rootdir/sbin/init.org");
1543 $self->run_command ("cp '$fake_init' '$rootdir/sbin/init'");
1545 $self->run_command ("cp '$default_env' '$rootdir/sbin/defenv'");
1547 $self->run_command ("lxc-start -n $veid -f $self->{veconffile}");
1549 $self->logmsg ("initialize ld cache\n");
1550 $self->ve_command ("/sbin/ldconfig");
1551 $self->run_command ("ln -sf mawk '$rootdir/usr/bin/awk'");
1553 $self->logmsg ("installing packages\n");
1555 $self->ve_dpkg ('install', 'base-files', 'base-passwd');
1557 $self->ve_dpkg ('install', 'dpkg');
1559 $self->run_command ("ln -sf /usr/share/zoneinfo/UTC '$rootdir/etc/localtime'");
1561 $self->run_command ("ln -sf bash '$rootdir/bin/sh'");
1563 $self->ve_dpkg ('install', 'libc6');
1564 $self->ve_dpkg ('install', 'perl-base');
1566 unlink "$rootdir/usr/bin/awk";
1568 $self->ve_dpkg ('install', 'mawk');
1569 $self->ve_dpkg ('install', 'debconf');
1571 # unpack required packages
1572 foreach my $p (@$required) {
1573 $self->ve_dpkg ('unpack', $p);
1576 rename ("$rootdir/sbin/init.org", "$rootdir/sbin/init");
1577 $self->ve_divert_add ("/sbin/init");
1578 $self->run_command ("cp '$fake_init' '$rootdir/sbin/init'");
1580 # disable service activation
1581 $self->ve_divert_add ("/usr/sbin/policy-rc.d");
1582 $data = "#!/bin/sh\nexit 101\n";
1583 write_file
($data, "$rootdir/usr/sbin/policy-rc.d", 755);
1585 # disable start-stop-daemon
1586 $self->ve_divert_add ("/sbin/start-stop-daemon");
1590 echo \"Warning: Fake start-stop-daemon called, doing nothing\"
1592 write_file
($data, "$rootdir/sbin/start-stop-daemon", 0755);
1595 $self->ve_divert_add ("/sbin/udevd");
1597 if ($suite eq 'etch') {
1598 write_file
("NO_START=1\n", "$rootdir/etc/default/apache2"); # disable apache2 startup
1601 $self->logmsg ("configure required packages\n");
1602 $self->ve_command ("dpkg --force-confold --skip-same-version --configure -a");
1604 # set postfix defaults
1605 if ($mta eq 'postfix') {
1606 $data = "postfix postfix/main_mailer_type select Local only\n";
1607 $self->ve_debconfig_set ($data);
1609 $data = "postmaster: root\nwebmaster: root\n";
1610 write_file
($data, "$rootdir/etc/aliases");
1613 if ($suite eq 'jaunty') {
1614 # jaunty does not create /var/run/network, so network startup fails.
1615 # so we do not use tmpfs for /var/run and /var/lock
1616 $self->run_command ("sed -e 's/RAMRUN=yes/RAMRUN=no/' -e 's/RAMLOCK=yes/RAMLOCK=no/' -i $rootdir/etc/default/rcS");
1617 # and create the directory here
1618 $self->run_command ("mkdir $rootdir/var/run/network");
1621 # unpack base packages
1622 foreach my $p (@$important) {
1623 $self->ve_dpkg ('unpack', $p);
1627 if (-x
"$rootdir/sbin/ifconfig") {
1628 $self->ve_command ("ifconfig lo up");
1630 $self->ve_command ("ip link set lo up");
1633 $self->logmsg ("configure important packages\n");
1634 $self->ve_command ("dpkg --force-confold --skip-same-version --configure -a");
1636 if (-d
"$rootdir/etc/event.d") {
1637 unlink <$rootdir/etc/event.d
/tty
*>;
1640 if (-f
"$rootdir/etc/inittab") {
1641 $self->run_command ("sed -i -e '/getty\\s38400\\stty[23456]/d' '$rootdir/etc/inittab'");
1644 # Link /etc/mtab to /proc/mounts, so df and friends will work:
1645 unlink "$rootdir/etc/mtab";
1646 $self->ve_command ("ln -s /proc/mounts /etc/mtab");
1649 $self->ve_command ("usermod -L root");
1651 if ($mta eq 'postfix') {
1652 $data = "postfix postfix/main_mailer_type select No configuration\n";
1653 $self->ve_debconfig_set ($data);
1655 unlink "$rootdir/etc/mailname";
1656 write_file
($postfix_main_cf, "$rootdir/etc/postfix/main.cf");
1659 if (!$opts->{minimal
}) {
1660 # unpack standard packages
1661 foreach my $p (@$standard) {
1662 $self->ve_dpkg ('unpack', $p);
1665 $self->logmsg ("configure standard packages\n");
1666 $self->ve_command ("dpkg --force-confold --skip-same-version --configure -a");
1669 # disable HWCLOCK access
1670 $self->run_command ("echo 'HWCLOCKACCESS=no' >> '$rootdir/etc/default/rcS'");
1673 $self->ve_divert_add ("/usr/sbin/hald");
1675 # disable /dev/urandom init
1676 $self->run_command ("install -m 0755 '$script_init_urandom' '$rootdir/etc/init.d/urandom'");
1678 if ($suite eq 'etch' || $suite eq 'hardy' || $suite eq 'intrepid' || $suite eq 'jaunty') {
1680 $self->ve_divert_add ("/sbin/klogd");
1683 # remove unnecessays sysctl entries to avoid warnings
1685 $cmd .= ' -e \'s/^\(kernel\.printk.*\)/#\1/\'';
1686 $cmd .= ' -e \'s/^\(kernel\.maps_protect.*\)/#\1/\'';
1687 $cmd .= ' -e \'s/^\(fs\.inotify\.max_user_watches.*\)/#\1/\'';
1688 $cmd .= ' -e \'s/^\(vm\.mmap_min_addr.*\)/#\1/\'';
1689 $cmd .= " -i '$rootdir/etc/sysctl.conf'";
1690 $self->run_command ($cmd);
1692 my $bindv6only = "$rootdir/etc/sysctl.d/bindv6only.conf";
1693 if (-f
$bindv6only) {
1695 $cmd .= ' -e \'s/^\(net\.ipv6\.bindv6only.*\)/#\1/\'';
1696 $cmd .= " -i '$bindv6only'";
1697 $self->run_command ($cmd);
1700 if ($suite eq 'hardy' || $suite eq 'intrepid' || $suite eq 'jaunty') {
1701 # disable tty init (console-setup)
1703 $cmd .= ' -e \'s/^\(ACTIVE_CONSOLES=.*\)/ACTIVE_CONSOLES=/\'';
1704 $cmd .= " -i '$rootdir/etc/default/console-setup'";
1705 $self->run_command ($cmd);
1708 if ($suite eq 'intrepid' || $suite eq 'jaunty') {
1709 # remove sysctl setup (avoid warnings at startup)
1710 my $filelist = "$rootdir/etc/sysctl.d/10-console-messages.conf";
1711 $filelist .= " $rootdir/etc/sysctl.d/10-process-security.conf" if $suite eq 'intrepid';
1712 $filelist .= " $rootdir/etc/sysctl.d/10-network-security.conf";
1713 $self->run_command ("rm $filelist");
1716 if (-e
"$rootdir/lib/systemd/system/sys-kernel-config.mount") {
1717 $self->ve_command ("ln -s /dev/null /etc/systemd/system/sys-kernel-debug.mount");
1724 my $veid = $self->{veid
};
1725 my $conffile = $self->{veconffile
};
1727 my $vestat = $self->ve_status();
1729 if (!$vestat->{exist
}) {
1730 $self->logmsg ("Please create the appliance first (bootstrap)");
1734 if (!$vestat->{running
}) {
1735 $self->run_command ("lxc-start -n $veid -f $conffile");
1738 system ("lxc-attach -n $veid --rcfile $conffile --clear-env");
1741 sub ve_mysql_command
{
1742 my ($self, $sql, $password) = @_;
1744 #my $bootstrap = "/usr/sbin/mysqld --bootstrap --user=mysql --skip-grant-tables " .
1745 #"--skip-bdb --skip-innodb --skip-ndbcluster";
1747 $self->ve_command ("mysql", $sql);
1750 sub ve_mysql_bootstrap
{
1751 my ($self, $sql, $password) = @_;
1755 my $suite = $self->{config
}->{suite
};
1757 if ($suite eq 'jessie') {
1758 my $rootdir = $self->{rootfs
};
1759 $self->run_command ("sed -e 's/^key_buffer\\s*=/key_buffer_size =/' -i $rootdir/etc/mysql/my.cnf");
1762 if ($suite eq 'squeeze' || $suite eq 'wheezy' || $suite eq 'jessie') {
1763 $cmd = "/usr/sbin/mysqld --bootstrap --user=mysql --skip-grant-tables";
1766 $cmd = "/usr/sbin/mysqld --bootstrap --user=mysql --skip-grant-tables " .
1767 "--skip-bdb --skip-innodb --skip-ndbcluster";
1770 $self->ve_command ($cmd, $sql);
1773 sub compute_required
{
1774 my ($self, $pkglist) = @_;
1776 my $pkginfo = $self->pkginfo();
1777 my $instpkgs = $self->read_installed ();
1780 __record_provides
($pkginfo, $closure, [keys $instpkgs->%*]);
1782 return $self->closure ($closure, $pkglist);
1786 my ($self, $opts) = @_;
1788 my @supp = ('7.4', '8.1');
1789 my $pgversion; # NOTE: not setting that defaults to the distro default, normally the best choice
1791 my $suite = $self->{config
}->{suite
};
1793 if ($suite eq 'lenny' || $suite eq 'hardy' || $suite eq 'intrepid' || $suite eq 'jaunty') {
1796 } elsif ($suite eq 'squeeze') {
1799 } elsif ($suite eq 'wheezy') {
1802 } elsif ($suite eq 'jessie') {
1805 } elsif ($suite eq 'stretch') {
1808 } elsif ($suite eq 'buster') {
1811 } elsif ($suite eq 'bullseye') {
1813 } elsif ($suite eq 'bookworm') {
1814 # FIXME: update once froozen
1815 @supp = ('13', '14');
1817 $pgversion = $opts->{version
} if $opts->{version
};
1820 if (defined($pgversion)) {
1821 die "unsupported postgres version '$pgversion'\n" if !grep { $pgversion eq $_; } @supp;
1823 $required = $self->compute_required (["postgresql-$pgversion"]);
1825 $required = $self->compute_required (["postgresql"]);
1828 $self->cache_packages ($required);
1830 $self->ve_dpkg ('install', @$required);
1832 my $iscript = "postgresql-$pgversion";
1833 if ($suite eq 'squeeze' || $suite eq 'wheezy' || $suite eq 'jessie' ||
1834 $suite eq 'stretch') {
1835 $iscript = 'postgresql';
1838 $self->ve_command ("/etc/init.d/$iscript start") if $opts->{start
};
1842 my ($self, $opts) = @_;
1844 my $password = $opts->{password
};
1845 my $rootdir = $self->{rootfs
};
1847 my $suite = $self->{config
}->{suite
};
1850 if ($suite eq 'squeeze') {
1852 } elsif ($suite eq 'wheezy' || $suite eq 'jessie') {
1855 die "task_mysql: unsupported suite '$suite'";
1858 my $required = $self->compute_required (['mysql-common', "mysql-server-$ver"]);
1860 $self->cache_packages ($required);
1862 $self->ve_dpkg ('install', @$required);
1864 # fix security (see /usr/bin/mysql_secure_installation)
1865 my $sql = "DELETE FROM mysql.user WHERE User='';\n" .
1866 "DELETE FROM mysql.user WHERE User='root' AND Host!='localhost';\n" .
1867 "FLUSH PRIVILEGES;\n";
1868 $self->ve_mysql_bootstrap ($sql);
1872 my $rpw = $password eq 'random' ?
'admin' : $password;
1874 my $sql = "USE mysql;\n" .
1875 "UPDATE user SET password=PASSWORD(\"$rpw\") WHERE user
='root';\n" .
1876 "FLUSH PRIVILEGES
;\n";
1877 $self->ve_mysql_bootstrap ($sql);
1879 write_file ("[client
]\nuser
=root
\npassword
=\"$rpw\"\n", "$rootdir/root/.my.cnf", 0600);
1880 if ($password eq 'random') {
1881 $self->install_init_script ($script_mysql_randompw, 2, 20);
1885 $self->ve_command ("/etc/init.d/mysql start") if $opts->{start
};
1889 my ($self, $opts) = @_;
1891 my $memlimit = $opts->{memlimit
};
1892 my $rootdir = $self->{rootfs
};
1894 my $required = $self->compute_required([qw(php php-cli libapache2-mod-php php-gd)]);
1896 $self->cache_packages ($required);
1898 $self->ve_dpkg ('install', @$required);
1901 $self->run_command ("sed -e 's/^\\s*memory_limit\\s*=.*;/memory_limit = ${memlimit}M;/' -i $rootdir/etc/php5/apache2/php.ini");
1906 my ($self, $pkglist, $unpack) = @_;
1908 my $required = $self->compute_required ($pkglist);
1910 $self->cache_packages ($required);
1912 $self->ve_dpkg ($unpack ?
'unpack' : 'install', @$required);
1916 my ($self, $distclean) = @_;
1918 unlink $self->{logfile
};
1919 unlink "$self->{targetname}.tar";
1920 unlink "$self->{targetname}.tar.gz";
1922 $self->ve_destroy ();
1925 rmtree
$self->{cachedir
} if $distclean && !$self->{config
}->{cachedir
};
1927 rmtree
$self->{infodir
};