13 use POSIX qw
(LONG_MAX
);
16 # fixme: lock container ?
18 my $dablibdir = "/usr/lib/dab";
19 my $devicetar = "$dablibdir/devices.tar.gz";
20 my $default_env = "$dablibdir/scripts/defenv";
21 my $fake_init = "$dablibdir/scripts/init.pl";
22 my $script_ssh_init = "$dablibdir/scripts/ssh_gen_host_keys";
23 my $script_mysql_randompw = "$dablibdir/scripts/mysql_randompw";
24 my $script_init_urandom = "$dablibdir/scripts/init_urandom";
26 my $postfix_main_cf = <<EOD;
27 # See /usr/share/postfix/main.cf.dist for a commented, more complete version
29 smtpd_banner = \$myhostname ESMTP \$mail_name (Debian/GNU)
32 # appending .domain is the MUA's job.
33 append_dot_mydomain = no
35 # Uncomment the next line to generate "delayed mail" warnings
36 #delay_warning_time = 4h
38 alias_maps = hash:/etc/aliases
39 alias_database = hash:/etc/aliases
40 mydestination = \$myhostname, localhost.\$mydomain, localhost
42 mynetworks = 127.0.0.0/8
43 inet_interfaces = loopback-only
44 recipient_delimiter = +
46 compatibility_level = 2
50 # produce apt compatible filenames (/var/lib/apt/lists)
51 sub __url_to_filename
{
64 # systemd: true (except for devuan ostypes)
65 my $supported_suites = {
67 ostype
=> "debian-12",
70 ostype
=> "debian-11",
73 ostype
=> "debian-10",
76 ostype
=> "debian-9.0",
79 ostype
=> "debian-8.0",
85 ostype
=> "debian-7.0",
91 ostype
=> "debian-6.0",
97 ostype
=> "debian-5.0",
103 ostype
=> "debian-4.0",
106 # DEVUAN (imply systemd = 0 default)
109 ostype
=> "devuan-1.0",
113 ostype
=> "devuan-2.0",
116 ostype
=> "devuan-2.0",
119 ostype
=> "devuan-3.0",
122 ostype
=> "devuan-4.0",
125 ostype
=> "devuan-5.0",
133 ostype
=> "ubuntu-8.04",
140 ostype
=> "ubuntu-8.10",
147 ostype
=> "ubuntu-9.04",
154 ostype
=> "ubuntu-12.04",
161 ostype
=> "ubuntu-14.04",
165 ostype
=> "ubuntu-15.04",
169 ostype
=> "ubuntu-15.10",
173 ostype
=> "ubuntu-16.04",
177 ostype
=> "ubuntu-16.10",
181 ostype
=> "ubuntu-17.04",
185 ostype
=> "ubuntu-17.10",
189 ostype
=> "ubuntu-18.04",
193 ostype
=> "ubuntu-18.10",
197 ostype
=> "ubuntu-19.04",
201 ostype
=> "ubuntu-19.10",
205 ostype
=> "ubuntu-20.04",
209 ostype
=> "ubuntu-20.10",
213 ostype
=> "ubuntu-21.04",
217 ostype
=> "ubuntu-21.10",
221 ostype
=> "ubuntu-22.04",
225 ostype
=> "ubuntu-22.10",
233 my $suiteinfo = $supported_suites->{$suite} || die "unsupported suite '$suite'!\n";
236 $suiteinfo->{origin
} //= 'debian';
237 $suiteinfo->{suite
} //= $suite;
239 $suiteinfo->{flags
} //= {};
240 if ($suiteinfo->{ostype
} =~ /^devuan/) {
241 $suiteinfo->{flags
}->{systemd
} //= 0;
243 $suiteinfo->{flags
}->{systemd
} //= 1;
250 my ($self, $url, $path) = @_;
251 my $tmpfn = "$path.tmp$$";
253 $self->logmsg ("download: $url\n");
255 eval { $self->run_command ("wget -q '$url' -O '$tmpfn'") };
261 rename ($tmpfn, $path);
265 my ($data, $file, $perm) = @_;
267 die "no filename" if !$file;
271 my $fh = IO
::File-
>new ($file, O_WRONLY
| O_CREAT
, $perm) ||
272 die "unable to open file '$file'";
282 die "no filename" if !$file;
284 my $fh = IO
::File-
>new ($file) ||
285 die "unable to open file '$file'";
287 local $/; # slurp mode
301 my $fh = IO
::File-
>new ("<$filename") || return $res;
304 while (defined (my $line = <$fh>)) {
305 next if $line =~ m/^\#/;
306 next if $line =~ m/^\s*$/;
316 if ($rec =~ s/^Description:\s*([^\n]*)(\n\s+.*)*$//si) {
317 $res->{headline
} = $1;
318 chomp $res->{headline
};
321 $res->{description
} = $long;
322 chomp $res->{description
};
323 } elsif ($rec =~ s/^([^:]+):\s*(.*\S)\s*\n//) {
324 my ($key, $value) = (lc ($1), $2);
325 if ($key eq 'source' || $key eq 'mirror') {
326 push @{$res->{$key}}, $value;
328 die "duplicate key '$key'\n" if defined ($res->{$key});
329 $res->{$key} = $value;
332 die "unable to parse config file: $rec";
336 die "unable to parse config file" if $rec;
342 my ($self, $cmd, $input, $getoutput) = @_;
344 my $reader = IO
::File-
>new();
345 my $writer = IO
::File-
>new();
346 my $error = IO
::File-
>new();
350 my $cmdstr = ref ($cmd) eq 'ARRAY' ?
join (' ', @$cmd) : $cmd;
354 if (ref ($cmd) eq 'ARRAY') {
355 $pid = open3
($writer, $reader, $error, @$cmd) || die $!;
357 $pid = open3
($writer, $reader, $error, $cmdstr) || die $!;
364 if ($orig_pid != $$) {
365 $self->logmsg ("ERROR: command '$cmdstr' failed - fork failed\n");
372 print $writer $input if defined $input;
375 my $select = IO
::Select-
>new();
376 $select->add ($reader);
377 $select->add ($error);
380 my $logfd = $self->{logfd
};
382 while ($select->count) {
383 my @handles = $select->can_read ();
385 foreach my $h (@handles) {
387 my $count = sysread ($h, $buf, 4096);
388 if (!defined ($count)) {
390 die "command '$cmdstr' failed: $!";
392 $select->remove ($h) if !$count;
396 $res .= $buf if $getoutput;
403 die "command '$cmdstr' failed with exit code $ec\n" if $ec;
411 $self->writelog (@_);
416 my $fd = $self->{logfd
};
420 sub __sample_config
{
424 my $arch = $self->{config
}->{architecture
};
426 my $ostype = $self->{config
}->{ostype
};
428 if ($ostype =~ m/^de(bi|vu)an-/) {
429 $data .= "lxc.include = /usr/share/lxc/config/debian.common.conf\n";
430 } elsif ($ostype =~ m/^ubuntu-/) {
431 $data .= "lxc.include = /usr/share/lxc/config/ubuntu.common.conf\n";
433 die "unknown os type '$ostype'\n";
435 $data .= "lxc.uts.name = localhost\n";
436 $data .= "lxc.rootfs.path = $self->{rootfs}\n";
445 if (my $fd = IO
::File-
>new (".veid")) {
452 $self->{working_dir
} = getcwd
;
453 $self->{veconffile
} = "$self->{working_dir}/config";
454 $self->{rootfs
} = "$self->{working_dir}/rootfs";
457 $self->{veid
} = $cid;
463 UUID
::generate
($uuid);
464 UUID
::unparse
($uuid, $uuid_str);
465 $self->{veid
} = $uuid_str;
467 my $fd = IO
::File-
>new (">.veid") ||
468 die "unable to write '.veid'\n";
469 print $fd "$self->{veid}\n";
472 my $cdata = $self->__sample_config();
474 my $fh = IO
::File-
>new ($self->{veconffile
}, O_WRONLY
|O_CREAT
|O_EXCL
) ||
475 die "unable to write lxc config file '$self->{veconffile}' - $!";
479 mkdir $self->{rootfs
} || die "unable to create rootfs - $!";
481 $self->logmsg ("allocated VE $self->{veid}\n");
483 return $self->{veid
};
486 # just use some simple heuristic for now, merge usr for releases newer than ubuntu 21.x or debian 11
490 my $ostype = $self->{config
}->{ostype
};
492 # FIXME: add configuration override posibillity
494 if ($ostype =~ m/^debian-(\d+)/) {
495 return int($1) >= 11;
496 } elsif ($ostype =~ m/^ubuntu-(\d+)/) {
497 return int($1) >= 21;
502 sub setup_usr_merge
{
505 my $rootfs = $self->{rootfs
};
506 my $arch = $self->{config
}->{architecture
};
508 # similar to https://salsa.debian.org/installer-team/debootstrap/-/blob/master/functions#L1354
509 my @merged_dirs = qw(bin sbin lib);
511 if ($arch eq 'amd64') {
512 push @merged_dirs, qw(lib32 lib64 libx32);
513 } elsif ($arch eq 'i386') {
514 push @merged_dirs, qw(lib64 libx32);
517 $self->logmsg ("setup usr-merge symlinks for '" . join("', '", @merged_dirs) . "'\n");
519 for my $dir (@merged_dirs) {
520 symlink("usr/$dir", "$rootfs/$dir") or warn "could not create symlink - $!\n";
521 mkpath
"$rootfs/usr/$dir";
525 sub get_target_name
{
528 my $name = $config->{name
} || die "no 'name' specified\n";
529 $name =~ m/^[a-z][0-9a-z\-\*\.]+$/ || die "illegal characters in name '$name'\n";
531 my ($version, $arch, $ostype) = $config->@{'version', 'architecture', 'ostype'};
532 $name = "${ostype}-${name}" if $name !~ m/^$ostype/;
534 return "${name}_${version}_${arch}"
538 my ($class, $config) = @_;
540 $class = ref ($class) || $class;
541 $config = read_config
('dab.conf') if !$config;
548 $self->{logfile
} = "logfile";
549 $self->{logfd
} = IO
::File-
>new (">>$self->{logfile}") || die "unable to open log file";
551 my $arch = $config->{architecture
} || die "no 'architecture' specified\n";
552 die "unsupported architecture '$arch'\n" if $arch !~ m/^(i386|amd64)$/;
554 my $suite = $config->{suite
} || die "no 'suite' specified\n";
556 my $suiteinfo = get_suite_info
($suite);
557 $suite = $suiteinfo->{suite
};
558 $config->{ostype
} = $suiteinfo->{ostype
};
560 # assert required dab.conf keys exist
561 for my $key (qw(version section headline maintainer)) {
562 die "no '$key' specified\n" if !$config->{$key};
565 $self->{targetname
} = get_target_name
($config);
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 =~ /^(?:bullseye|bookworm|trixie|forky)$/) {
575 push @{$config->{source
}}, (
576 "http://deb.debian.org/debian SUITE main contrib",
577 "http://deb.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*((https?
|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*((https?
|ftp
)://\S
+)\s
*=>\s
*((https?
|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";
639 $self->{sources
} = $sources;
640 $self->{infodir
} = "info";
642 $self->__allocate_ve();
644 $self->{cachedir
} = ($config->{cachedir
} || 'cache') . "/$suite";;
646 my $incl = [qw
(less ssh openssh-server logrotate
)];
647 my $excl = [qw
(modutils reiserfsprogs ppp pppconfig pppoe pppoeconf nfs-common mtools ntp
)];
649 # ubuntu has too many dependencies on udev, so we cannot exclude it (instead we disable udevd)
650 if (lc($suiteinfo->{origin
}) eq 'ubuntu' && $suiteinfo->{flags
}->{systemd
}) {
651 push @$incl, 'isc-dhcp-client';
652 push @$excl, qw(libmodule-build-perl libdrm-common libdrm2 libplymouth5 plymouth plymouth-theme-ubuntu-text powermgmt-base);
653 if ($suite eq 'jammy') {
654 push @$excl, qw(fuse); # avoid fuse2 <-> fuse3 conflict
656 } elsif ($suite eq 'trusty') {
657 push @$excl, qw(systemd systemd-services libpam-systemd libsystemd-daemon0 memtest86+);
658 } elsif ($suite eq 'precise') {
659 push @$excl, qw(systemd systemd-services libpam-systemd libsystemd-daemon0 memtest86+ ubuntu-standard);
660 } elsif ($suite eq 'hardy') {
661 push @$excl, qw(kbd);
662 push @$excl, qw(apparmor apparmor-utils ntfs-3g friendly-recovery);
663 } elsif ($suite eq 'intrepid' || $suite eq 'jaunty') {
664 push @$excl, qw(apparmor apparmor-utils libapparmor1 libapparmor-perl libntfs-3g28);
665 push @$excl, qw(ntfs-3g friendly-recovery);
666 } elsif ($suite eq 'jessie') {
667 push @$incl, 'sysvinit-core'; # avoid systemd and udev
668 push @$incl, 'libperl4-corelibs-perl'; # to make lsof happy
669 push @$excl, qw(systemd systemd-sysv udev module-init-tools pciutils hdparm memtest86+ parted);
670 } elsif ($suite eq 'stretch' || $suite eq 'buster' || $suite eq 'bullseye' || $suite eq 'bookworm') {
671 push @$excl, qw(module-init-tools pciutils hdparm memtest86+ parted);
673 push @$excl, qw(udev module-init-tools pciutils hdparm memtest86+ parted);
676 $self->{incl
} = $incl;
677 $self->{excl
} = $excl;
685 my $infodir = $self->{infodir
};
686 my $arch = $self->{config
}->{architecture
};
692 my $logfd = $self->{logfd
} = IO
::File-
>new (">$self->{logfile}") ||
693 die "unable to open log file";
706 foreach my $ss (@{$self->{sources
}}) {
707 my $src = $ss->{mirror
} || $ss->{source
};
708 my $path = "dists/$ss->{suite}/Release";
709 my $url = "$src/$path";
710 my $target = __url_to_filename
("$ss->{source}/$path");
712 $self->download ($url, "$infodir/$target");
713 $self->download ("$url.gpg", "$infodir/$target.gpg");
714 # fixme: impl. verify (needs --keyring option)
718 warn "Release info ignored\n";
721 foreach my $comp (@{$ss->{comp
}}) {
722 foreach my $compressor (@$COMPRESSORS) {
723 $path = "dists/$ss->{suite}/$comp/binary-$arch/Packages.$compressor->{ext}";
724 $target = "$infodir/" . __url_to_filename
("$ss->{source}/$path");
725 my $pkgsrc = "$src/$path";
727 $self->download ($pkgsrc, $target);
728 $self->run_command ("$compressor->{decomp} '$target'");
731 print $logfd "could not download Packages.$compressor->{ext}\n";
741 my ($self, $filename, $size) = @_;
743 my $config = $self->{config
};
747 $data .= "Name: $config->{name}\n";
748 $data .= "Version: $config->{version}\n";
749 $data .= "Type: lxc\n";
750 $data .= "OS: $config->{ostype}\n";
751 $data .= "Section: $config->{section}\n";
752 $data .= "Maintainer: $config->{maintainer}\n";
753 $data .= "Architecture: $config->{architecture}\n";
754 $data .= "Installed-Size: $size\n";
757 $data .= "Infopage: $config->{infopage}\n" if $config->{infopage
};
758 $data .= "ManageUrl: $config->{manageurl}\n" if $config->{manageurl
};
759 $data .= "Certified: $config->{certified}\n" if $config->{certified
};
762 $data .= "Description: $config->{headline}\n";
763 $data .= "$config->{description}\n" if $config->{description
};
765 write_file
($data, $filename, 0644);
769 my ($self, $opts) = @_;
771 my $suite = $self->{config
}->{suite
};
772 my $infodir = $self->{infodir
};
773 my $arch = $self->{config
}->{architecture
};
775 my $instpkgs = $self->read_installed ();
776 my $pkginfo = $self->pkginfo();
777 my $veid = $self->{veid
};
778 my $conffile = $self->{veconffile
};
779 my $rootdir = $self->{rootfs
};
781 my $vestat = $self->ve_status();
782 die "ve not running - unable to finalize\n" if !$vestat->{running
};
785 if (-f
"$rootdir/etc/init.d/mysql") {
786 $self->ve_command ("/etc/init.d/mysql stop");
789 if (!($opts->{keepmycnf
} || (-f
"$rootdir/etc/init.d/mysql_randompw"))) {
790 unlink "$rootdir/root/.my.cnf";
793 if ($suite eq 'etch') {
794 # enable apache2 startup
795 if ($instpkgs->{apache2
}) {
796 write_file
("NO_START=0\n", "$rootdir/etc/default/apache2");
798 unlink "$rootdir/etc/default/apache2";
801 $self->logmsg ("cleanup package status\n");
802 # prevent auto selection of all standard, required or important
803 # packages which are not installed
804 foreach my $pkg (keys %$pkginfo) {
805 my $pri = $pkginfo->{$pkg}->{priority
};
806 if ($pri && ($pri eq 'required' || $pri eq 'important'
807 || $pri eq 'standard')) {
808 if (!$instpkgs->{$pkg}) {
809 $self->ve_dpkg_set_selection ($pkg, 'purge');
814 $self->ve_command ("apt-get clean");
816 $self->logmsg ("update available package list\n");
818 $self->ve_command ("dpkg --clear-avail");
819 foreach my $ss (@{$self->{sources
}}) {
820 my $relsrc = __url_to_filename
("$ss->{source}/dists/$ss->{suite}/Release");
821 if (-f
"$infodir/$relsrc" && -f
"$infodir/$relsrc.gpg") {
822 $self->run_command ("cp '$infodir/$relsrc' '$rootdir/var/lib/apt/lists/$relsrc'");
823 $self->run_command ("cp '$infodir/$relsrc.gpg' '$rootdir/var/lib/apt/lists/$relsrc.gpg'");
825 foreach my $comp (@{$ss->{comp
}}) {
826 my $src = __url_to_filename
("$ss->{source}/dists/$ss->{suite}/" .
827 "$comp/binary-$arch/Packages");
828 my $target = "/var/lib/apt/lists/$src";
829 $self->run_command ("cp '$infodir/$src' '$rootdir/$target'");
830 $self->ve_command ("dpkg --merge-avail '$target'");
834 # set dselect default method
835 write_file
("apt apt\n", "$rootdir/var/lib/dpkg/cmethopt");
837 $self->ve_divert_remove ("/usr/sbin/policy-rc.d");
839 $self->ve_divert_remove ("/sbin/start-stop-daemon");
841 $self->ve_divert_remove ("/sbin/init");
843 # finally stop the VE
844 $self->run_command ("lxc-stop -n $veid --rcfile $conffile --kill");
846 unlink "$rootdir/sbin/defenv";
847 unlink <$rootdir/root/dead.letter
*>;
848 unlink "$rootdir/var/log/init.log";
849 unlink "$rootdir/aquota.group", "$rootdir/aquota.user";
851 write_file
("", "$rootdir/var/log/syslog");
853 my $get_path_size = sub {
855 my $sizestr = $self->run_command ("du -sm $path", undef, 1);
856 if ($sizestr =~ m/^(\d+)\s+\Q$path\E$/) {
859 die "unable to detect size for '$path'\n";
863 $self->logmsg ("detecting final appliance size: ");
864 my $size = $get_path_size->($rootdir);
865 $self->logmsg ("$size MB\n");
867 $self->write_config ("$rootdir/etc/appliance.info", $size);
869 $self->logmsg ("creating final appliance archive\n");
871 my $target = "$self->{targetname}.tar";
873 my $compressor = $opts->{compressor
} // 'gz';
874 my $compressor2cmd_map = {
877 zst
=> 'zstd --rm -9',
878 zstd
=> 'zstd --rm -9',
879 'zstd-max' => 'zstd --rm -19 -T0', # maximal level where the decompressor can still run efficiently
881 my $compressor2ending = {
886 my $compressor_cmd = $compressor2cmd_map->{$compressor};
887 die "unkown compressor '$compressor', use one of: ". join(', ', sort keys %$compressor2cmd_map)
888 if !defined($compressor_cmd);
890 my $ending = $compressor2ending->{$compressor} // $compressor;
891 my $final_archive = "${target}.${ending}";
893 unlink $final_archive;
895 $self->run_command ("tar cpf $target --numeric-owner -C '$rootdir' ./etc/appliance.info");
896 $self->run_command ("tar rpf $target --numeric-owner -C '$rootdir' --exclude ./etc/appliance.info .");
897 $self->run_command ("$compressor_cmd $target");
899 $self->logmsg ("detecting final commpressed appliance size: ");
900 $size = $get_path_size->($final_archive);
901 $self->logmsg ("$size MB\n");
903 $self->logmsg ("appliance archive: $final_archive\n");
909 my $rootdir = $self->{rootfs
};
911 my $pkgfilelist = "$rootdir/var/lib/dpkg/status";
913 open(my $PKGLST, '<', $pkgfilelist) or die "unable to open '$pkgfilelist' - $!";
917 while (my $rec = <$PKGLST>) {
923 while ($rec =~ s/^([^:]+):\s+(.*?)\s*\n//) {
927 my $pkg = $res->{'package'};
928 if (my $status = $res->{status
}) {
929 my @sa = split (/\s+/, $status);
931 if ($stat && ($stat ne 'purge')) {
932 $pkglist->{$pkg} = $res;
945 my $veid = $self->{veid
};
947 my $res = { running
=> 0 };
949 $res->{exist
} = 1 if -d
"$self->{rootfs}/usr";
951 my $filename = "/proc/net/unix";
953 # similar test is used by lcxcontainers.c: list_active_containers
954 my $fh = IO
::File-
>new ($filename, "r");
957 while (defined(my $line = <$fh>)) {
958 if ($line =~ m/^[a-f0-9]+:\s\S+\s\S+\s\S+\s\S+\s\S+\s\d+\s(\S+)$/) {
960 if ($path =~ m!^@/\S+/$veid/command$!) {
971 my ($self, $cmd, $input) = @_;
973 my $veid = $self->{veid
};
974 my $conffile = $self->{veconffile
};
976 if (ref ($cmd) eq 'ARRAY') {
977 unshift @$cmd, 'lxc-attach', '-n', $veid, '--rcfile', $conffile, '--clear-env', '--', 'defenv';
978 $self->run_command($cmd, $input);
980 $self->run_command("lxc-attach -n $veid --rcfile $conffile --clear-env -- defenv $cmd", $input);
984 # like ve_command, but pipes stdin correctly
986 my ($self, @cmd) = @_;
988 my $veid = $self->{veid
};
989 my $conffile = $self->{veconffile
};
992 my $pid = open2
($reader, "<&STDIN", 'lxc-attach', '-n', $veid, '--rcfile', $conffile, '--',
993 'defenv', @cmd) || die "unable to exec command";
995 while (defined (my $line = <$reader>)) {
996 $self->logmsg ($line);
1002 die "ve_exec failed - status $rc\n" if $rc != 0;
1006 my ($self, $filename) = @_;
1008 $self->ve_command ("dpkg-divert --add --divert '$filename.distrib' " .
1009 "--rename '$filename'");
1011 sub ve_divert_remove
{
1012 my ($self, $filename) = @_;
1014 my $rootdir = $self->{rootfs
};
1016 unlink "$rootdir/$filename";
1017 $self->ve_command ("dpkg-divert --remove --rename '$filename'");
1020 sub ve_debconfig_set
{
1021 my ($self, $dcdata) = @_;
1023 my $rootdir = $self->{rootfs
};
1024 my $cfgfile = "/tmp/debconf.txt";
1025 write_file
($dcdata, "$rootdir/$cfgfile");
1026 $self->ve_command ("debconf-set-selections $cfgfile");
1027 unlink "$rootdir/$cfgfile";
1030 sub ve_dpkg_set_selection
{
1031 my ($self, $pkg, $status) = @_;
1033 $self->ve_command ("dpkg --set-selections", "$pkg $status");
1037 my ($self, $cmd, @pkglist) = @_;
1039 return if !scalar (@pkglist);
1041 my $pkginfo = $self->pkginfo();
1043 my $rootdir = $self->{rootfs
};
1044 my $cachedir = $self->{cachedir
};
1048 foreach my $pkg (@pkglist) {
1049 my $filename = $self->getpkgfile ($pkg);
1050 $self->run_command ("cp '$cachedir/$filename' '$rootdir/$filename'");
1051 push @files, "/$filename";
1052 $self->logmsg ("$cmd: $pkg\n");
1055 my $fl = join (' ', @files);
1057 if ($cmd eq 'install') {
1058 $self->ve_command ("dpkg --force-depends --force-confold --install $fl");
1059 } elsif ($cmd eq 'unpack') {
1060 $self->ve_command ("dpkg --force-depends --unpack $fl");
1062 die "internal error";
1065 foreach my $fn (@files) { unlink "$rootdir$fn"; }
1071 my $veid = $self->{veid
}; # fixme
1072 my $conffile = $self->{veconffile
};
1074 my $vestat = $self->ve_status();
1075 if ($vestat->{running
}) {
1076 $self->run_command ("lxc-stop -n $veid --rcfile $conffile --kill");
1079 rmtree
$self->{rootfs
};
1080 unlink $self->{veconffile
};
1086 my $veid = $self->{veid
};
1087 my $conffile = $self->{veconffile
};
1089 $self->logmsg ("initialize VE $veid\n");
1091 my $vestat = $self->ve_status();
1092 if ($vestat->{running
}) {
1093 $self->run_command ("lxc-stop -n $veid --rcfile $conffile --kill");
1096 rmtree
$self->{rootfs
};
1097 mkpath
$self->{rootfs
};
1100 sub __deb_version_cmp
{
1101 my ($cur, $op, $new) = @_;
1103 if (system("dpkg", "--compare-versions", $cur, $op, $new) == 0) {
1110 sub __parse_packages
{
1111 my ($pkginfo, $filename, $src) = @_;
1114 open(my $PKGLST, '<', $filename) or die "unable to open '$filename' - $!";
1116 while (my $rec = <$PKGLST>) {
1117 $rec =~ s/\n\s+/ /g;
1123 while ($rec =~ s/^([^:]+):\s+(.*?)\s*\n//) {
1127 my $pkg = $res->{'package'};
1128 if ($pkg && $res->{'filename'}) {
1130 if (my $info = $pkginfo->{$pkg}) {
1131 $cur = $info->{version
};
1133 my $new = $res->{version
};
1134 if (!$cur || __deb_version_cmp
($cur, 'lt', $new)) {
1136 $res->{url
} = "$src/$res->{'filename'}";
1138 die "no url for package '$pkg'" if !$res->{url
};
1140 $pkginfo->{$pkg} = $res;
1151 return $self->{pkginfo
} if $self->{pkginfo
};
1153 my $infodir = $self->{infodir
};
1154 my $arch = $self->{config
}->{architecture
};
1156 my $availfn = "$infodir/available";
1161 # reading 'available' is faster, because it only contains latest version
1162 # (no need to do slow version compares)
1164 __parse_packages
($pkginfo, $availfn);
1165 $self->{pkginfo
} = $pkginfo;
1169 $self->logmsg ("generating available package list\n");
1171 foreach my $ss (@{$self->{sources
}}) {
1172 foreach my $comp (@{$ss->{comp
}}) {
1173 my $url = "$ss->{source}/dists/$ss->{suite}/$comp/binary-$arch/Packages";
1174 my $pkgfilelist = "$infodir/" . __url_to_filename
($url);
1176 my $src = $ss->{mirror
} || $ss->{source
};
1178 __parse_packages
($pkginfo, $pkgfilelist, $src);
1182 if (my $dep = $self->{config
}->{depends
}) {
1183 foreach my $d (split (/,/, $dep)) {
1184 if ($d =~ m/^\s*(\S+)\s*(\((\S+)\s+(\S+)\)\s*)?$/) {
1185 my ($pkg, $op, $rver) = ($1, $3, $4);
1186 $self->logmsg ("checking dependencies: $d\n");
1187 my $info = $pkginfo->{$pkg};
1188 die "package '$pkg' not available\n" if !$info;
1190 my $cver = $info->{version
};
1191 if (!__deb_version_cmp
($cver, $op, $rver)) {
1192 die "detected wrong version '$cver'\n";
1196 die "syntax error in depends field";
1201 $self->{pkginfo
} = $pkginfo;
1203 my $tmpfn = "$availfn.tmp$$";
1204 my $fd = IO
::File-
>new (">$tmpfn");
1205 foreach my $pkg (sort keys %$pkginfo) {
1206 my $info = $pkginfo->{$pkg};
1207 print $fd "package: $pkg\n";
1208 foreach my $k (sort keys %$info) {
1209 next if $k eq 'description';
1210 next if $k eq 'package';
1211 my $v = $info->{$k};
1212 print $fd "$k: $v\n" if $v;
1214 print $fd "description: $info->{description}\n" if $info->{description
};
1219 rename ($tmpfn, $availfn);
1224 sub __record_provides
{
1225 my ($pkginfo, $closure, $list, $skipself) = @_;
1227 foreach my $pname (@$list) {
1228 my $info = $pkginfo->{$pname};
1229 # fixme: if someone install packages directly using dpkg, there
1230 # is no entry in 'available', only in 'status'. In that case, we
1231 # should extract info from $instpkgs
1233 warn "hint: ignoring provides for '$pname' - package not in 'available' list.\n";
1236 if (my $prov = $info->{provides
}) {
1237 my @pl = split (',', $prov);
1238 foreach my $p (@pl) {
1240 if (!($skipself && (grep { $1 eq $_ } @$list))) {
1245 $closure->{$pname} = 1 if !$skipself;
1250 my ($self, $closure, $list) = @_;
1252 my $pkginfo = $self->pkginfo();
1254 # first, record provided packages
1255 __record_provides
($pkginfo, $closure, $list, 1);
1260 # then resolve dependencies
1261 foreach my $pname (@$list) {
1262 __closure_single
($pkginfo, $closure, $pkghash, $pkglist, $pname, $self->{excl
});
1268 sub __closure_single
{
1269 my ($pkginfo, $closure, $pkghash, $pkglist, $pname, $excl) = @_;
1273 $pname =~ s/:any$//;
1275 return if $closure->{$pname};
1277 my $info = $pkginfo->{$pname} || die "no such package '$pname'";
1279 my $dep = $info->{depends
};
1280 my $predep = $info->{'pre-depends'};
1282 my $size = $info->{size
};
1283 my $url = $info->{url
};
1285 $url || die "$pname: no url for package '$pname'";
1287 if (!$pkghash->{$pname}) {
1288 unshift @$pkglist, $pname;
1289 $pkghash->{$pname} = 1;
1292 __record_provides
($pkginfo, $closure, [$pname]) if $info->{provides
};
1294 $closure->{$pname} = 1;
1300 push @l, split (/,/, $predep) if $predep;
1301 push @l, split (/,/, $dep) if $dep;
1303 DEPEND
: foreach my $p (@l) {
1304 my @l1 = split (/\|/, $p);
1305 foreach my $p1 (@l1) {
1306 if ($p1 =~ m/^\s*(\S+).*/) {
1307 #printf (STDERR "$pname: $p --> $1\n");
1308 if ($closure->{$1}) {
1309 next DEPEND
; # dependency already met
1313 # search for non-excluded alternative
1315 foreach my $p1 (@l1) {
1316 if ($p1 =~ m/^\s*(\S+).*/) {
1317 next if grep { $1 eq $_ } @$excl;
1322 die "package '$pname' depends on exclusion '$p'\n" if !$found;
1324 #printf (STDERR "$pname: $p --> $found\n");
1326 __closure_single
($pkginfo, $closure, $pkghash, $pkglist, $found, $excl);
1330 sub cache_packages
{
1331 my ($self, $pkglist) = @_;
1333 foreach my $pkg (@$pkglist) {
1334 $self->getpkgfile ($pkg);
1339 my ($self, $pkg) = @_;
1341 my $pkginfo = $self->pkginfo();
1342 my $info = $pkginfo->{$pkg} || die "no such package '$pkg'";
1343 my $cachedir = $self->{cachedir
};
1345 my $url = $info->{url
};
1348 if ($url =~ m
|/([^/]+.deb
)$|) {
1351 die "internal error";
1354 return $filename if -f
"$cachedir/$filename";
1358 $self->download ($url, "$cachedir/$filename");
1363 sub install_init_script
{
1364 my ($self, $script, $runlevel, $prio) = @_;
1366 my $suite = $self->{config
}->{suite
};
1367 my $suiteinfo = get_suite_info
($suite);
1368 my $rootdir = $self->{rootfs
};
1370 my $base = basename
($script);
1371 my $target = "$rootdir/etc/init.d/$base";
1373 $self->run_command ("install -m 0755 '$script' '$target'");
1374 if ($suite eq 'etch' || $suite eq 'lenny') {
1375 $self->ve_command ("update-rc.d $base start $prio $runlevel .");
1376 } elsif ($suiteinfo->{flags
}->{systemd
}) {
1377 die "unable to install init script (system uses systemd)\n";
1378 } elsif ($suite eq 'trusty' || $suite eq 'precise') {
1379 die "unable to install init script (system uses upstart)\n";
1381 $self->ve_command ("insserv $base");
1388 my ($self, $opts) = @_;
1390 my $pkginfo = $self->pkginfo();
1391 my $veid = $self->{veid
};
1392 my $suite = $self->{config
}->{suite
};
1393 my $suiteinfo = get_suite_info
($suite);
1395 my $important = [ @{$self->{incl
}} ];
1399 my $mta = $opts->{exim
} ?
'exim' : 'postfix';
1400 if ($mta eq 'postfix') {
1401 push @$important, "postfix";
1404 if ($opts->{include
}) {
1405 push @$important, split(',', $opts->{include
});
1409 if ($opts->{exclude
}) {
1410 $exclude->{$_} = 1 for split(',', $opts->{exclude
});
1413 foreach my $p (sort keys %$pkginfo) {
1414 next if grep { $p eq $_ } @{$self->{excl
}};
1415 my $pri = $pkginfo->{$p}->{priority
};
1417 next if $mta ne 'exim' && $p =~ m/exim/;
1418 next if $p =~ m/(selinux|semanage|policycoreutils)/;
1420 push @$required, $p if $pri eq 'required';
1421 next if $exclude->{$p};
1422 push @$important, $p if $pri eq 'important';
1423 push @$standard, $p if $pri eq 'standard' && !$opts->{minimal
};
1427 $required = $self->closure($closure, $required);
1428 $important = $self->closure($closure, $important);
1430 if (!$opts->{minimal
}) {
1431 $standard = $self->closure($closure, $standard);
1434 # test if we have all 'ubuntu-minimal' and 'ubuntu-standard' packages
1435 # except those explicitly excluded
1436 if ($suite eq 'hardy' || $suite eq 'intrepid' || $suite eq 'jaunty') {
1437 my $mdeps = $pkginfo->{'ubuntu-minimal'}->{depends
};
1438 foreach my $d (split (/,/, $mdeps)) {
1439 if ($d =~ m/^\s*(\S+)$/) {
1441 next if $closure->{$pkg};
1442 next if grep { $pkg eq $_ } @{$self->{excl
}};
1443 die "missing ubuntu-minimal package '$pkg'\n";
1446 if (!$opts->{minimal
}) {
1447 $mdeps = $pkginfo->{'ubuntu-standard'}->{depends
};
1448 foreach my $d (split (/,/, $mdeps)) {
1449 if ($d =~ m/^\s*(\S+)$/) {
1451 next if $closure->{$pkg};
1452 next if grep { $pkg eq $_ } @{$self->{excl
}};
1453 die "missing ubuntu-standard package '$pkg'\n";
1459 # download/cache all files first
1460 $self->cache_packages ($required);
1461 $self->cache_packages ($important);
1462 $self->cache_packages ($standard);
1464 my $rootdir = $self->{rootfs
};
1466 # extract required packages first
1467 $self->logmsg ("create basic environment\n");
1469 if ($self->can_usr_merge()) {
1470 $self->setup_usr_merge();
1473 my $compressor2opt = {
1478 my $compressor_re = join('|', keys $compressor2opt->%*);
1480 $self->logmsg ("extract required packages to rootfs\n");
1481 foreach my $p (@$required) {
1482 my $filename = $self->getpkgfile ($p);
1483 my $content = $self->run_command("ar -t '$self->{cachedir}/$filename'", undef, 1);
1484 if ($content =~ m/^(data.tar.($compressor_re))$/m) {
1486 my $tar_opts = "--keep-directory-symlink $compressor2opt->{$2}";
1488 $self->run_command("ar -p '$self->{cachedir}/$filename' '$archive' | tar -C '$rootdir' -xf - $tar_opts");
1490 die "unexpected error for $p: no data.tar.{xz,gz,zst} found...";
1495 my $data = "Package: dpkg\n" .
1496 "Version: $pkginfo->{dpkg}->{version}\n" .
1497 "Status: install ok installed\n";
1499 write_file
($data, "$rootdir/var/lib/dpkg/status");
1500 write_file
("", "$rootdir/var/lib/dpkg/info/dpkg.list");
1501 write_file
("", "$rootdir/var/lib/dpkg/available");
1504 foreach my $ss (@{$self->{sources
}}) {
1505 my $url = $ss->{source
};
1506 my $comp = join (' ', @{$ss->{comp
}});
1507 $data .= "deb $url $ss->{suite} $comp\n\n";
1510 write_file
($data, "$rootdir/etc/apt/sources.list");
1512 $data = "# UNCONFIGURED FSTAB FOR BASE SYSTEM\n";
1513 write_file
($data, "$rootdir/etc/fstab", 0644);
1515 write_file
("localhost\n", "$rootdir/etc/hostname", 0644);
1517 # avoid warnings about non-existent resolv.conf
1518 write_file
("", "$rootdir/etc/resolv.conf", 0644);
1520 if (lc($suiteinfo->{origin
}) eq 'ubuntu' && $suiteinfo->{flags
}->{systemd
}) {
1521 # no need to configure loopback device
1522 # FIXME: Debian (systemd based?) too?
1524 $data = "auto lo\niface lo inet loopback\n";
1525 mkdir "$rootdir/etc/network";
1526 write_file
($data, "$rootdir/etc/network/interfaces", 0644);
1530 $self->run_command ("tar xzf '$devicetar' -C '$rootdir'");
1532 # avoid warnings about missing default locale
1533 write_file
("LANG=\"C\"\n", "$rootdir/etc/default/locale", 0644);
1536 rename ("$rootdir/sbin/init", "$rootdir/sbin/init.org");
1537 $self->run_command ("cp '$fake_init' '$rootdir/sbin/init'");
1539 $self->run_command ("cp '$default_env' '$rootdir/sbin/defenv'");
1541 $self->run_command ("lxc-start -n $veid -f $self->{veconffile}");
1543 $self->logmsg ("initialize ld cache\n");
1544 $self->ve_command ("/sbin/ldconfig");
1545 $self->run_command ("ln -sf mawk '$rootdir/usr/bin/awk'");
1547 $self->logmsg ("installing packages\n");
1549 $self->ve_dpkg ('install', 'base-files', 'base-passwd');
1551 $self->ve_dpkg ('install', 'dpkg');
1553 $self->run_command ("ln -sf /usr/share/zoneinfo/UTC '$rootdir/etc/localtime'");
1555 $self->run_command ("ln -sf bash '$rootdir/bin/sh'");
1557 $self->ve_dpkg ('install', 'libc6');
1558 $self->ve_dpkg ('install', 'perl-base');
1560 unlink "$rootdir/usr/bin/awk";
1562 $self->ve_dpkg ('install', 'mawk');
1563 $self->ve_dpkg ('install', 'debconf');
1565 # unpack required packages
1566 foreach my $p (@$required) {
1567 $self->ve_dpkg ('unpack', $p);
1570 rename ("$rootdir/sbin/init.org", "$rootdir/sbin/init");
1571 $self->ve_divert_add ("/sbin/init");
1572 $self->run_command ("cp '$fake_init' '$rootdir/sbin/init'");
1574 # disable service activation
1575 $self->ve_divert_add ("/usr/sbin/policy-rc.d");
1576 $data = "#!/bin/sh\nexit 101\n";
1577 write_file
($data, "$rootdir/usr/sbin/policy-rc.d", 755);
1579 # disable start-stop-daemon
1580 $self->ve_divert_add ("/sbin/start-stop-daemon");
1584 echo \"Warning: Fake start-stop-daemon called, doing nothing\"
1586 write_file
($data, "$rootdir/sbin/start-stop-daemon", 0755);
1589 $self->ve_divert_add ("/sbin/udevd");
1591 if ($suite eq 'etch') {
1592 write_file
("NO_START=1\n", "$rootdir/etc/default/apache2"); # disable apache2 startup
1595 $self->logmsg ("configure required packages\n");
1596 $self->ve_command ("dpkg --force-confold --skip-same-version --configure -a");
1598 # set postfix defaults
1599 if ($mta eq 'postfix') {
1600 $data = "postfix postfix/main_mailer_type select Local only\n";
1601 $self->ve_debconfig_set ($data);
1603 $data = "postmaster: root\nwebmaster: root\n";
1604 write_file
($data, "$rootdir/etc/aliases");
1607 if ($suite eq 'jaunty') {
1608 # jaunty does not create /var/run/network, so network startup fails.
1609 # so we do not use tmpfs for /var/run and /var/lock
1610 $self->run_command ("sed -e 's/RAMRUN=yes/RAMRUN=no/' -e 's/RAMLOCK=yes/RAMLOCK=no/' -i $rootdir/etc/default/rcS");
1611 # and create the directory here
1612 $self->run_command ("mkdir $rootdir/var/run/network");
1615 # unpack base packages
1616 foreach my $p (@$important) {
1617 $self->ve_dpkg ('unpack', $p);
1621 if (-x
"$rootdir/sbin/ifconfig") {
1622 $self->ve_command ("ifconfig lo up");
1624 $self->ve_command ("ip link set lo up");
1627 $self->logmsg ("configure important packages\n");
1628 $self->ve_command ("dpkg --force-confold --skip-same-version --configure -a");
1630 if (-d
"$rootdir/etc/event.d") {
1631 unlink <$rootdir/etc/event.d
/tty
*>;
1634 if (-f
"$rootdir/etc/inittab") {
1635 $self->run_command ("sed -i -e '/getty\\s38400\\stty[23456]/d' '$rootdir/etc/inittab'");
1638 # Link /etc/mtab to /proc/mounts, so df and friends will work:
1639 unlink "$rootdir/etc/mtab";
1640 $self->ve_command ("ln -s /proc/mounts /etc/mtab");
1643 $self->ve_command ("usermod -L root");
1645 if ($mta eq 'postfix') {
1646 $data = "postfix postfix/main_mailer_type select No configuration\n";
1647 $self->ve_debconfig_set ($data);
1649 unlink "$rootdir/etc/mailname";
1650 write_file
($postfix_main_cf, "$rootdir/etc/postfix/main.cf");
1653 if (!$opts->{minimal
}) {
1654 # unpack standard packages
1655 foreach my $p (@$standard) {
1656 $self->ve_dpkg ('unpack', $p);
1659 $self->logmsg ("configure standard packages\n");
1660 $self->ve_command ("dpkg --force-confold --skip-same-version --configure -a");
1663 # disable HWCLOCK access
1664 $self->run_command ("echo 'HWCLOCKACCESS=no' >> '$rootdir/etc/default/rcS'");
1667 $self->ve_divert_add ("/usr/sbin/hald");
1669 # disable /dev/urandom init
1670 $self->run_command ("install -m 0755 '$script_init_urandom' '$rootdir/etc/init.d/urandom'");
1672 if ($suite eq 'etch' || $suite eq 'hardy' || $suite eq 'intrepid' || $suite eq 'jaunty') {
1674 $self->ve_divert_add ("/sbin/klogd");
1677 # remove unnecessays sysctl entries to avoid warnings
1679 $cmd .= ' -e \'s/^\(kernel\.printk.*\)/#\1/\'';
1680 $cmd .= ' -e \'s/^\(kernel\.maps_protect.*\)/#\1/\'';
1681 $cmd .= ' -e \'s/^\(fs\.inotify\.max_user_watches.*\)/#\1/\'';
1682 $cmd .= ' -e \'s/^\(vm\.mmap_min_addr.*\)/#\1/\'';
1683 $cmd .= " -i '$rootdir/etc/sysctl.conf'";
1684 $self->run_command ($cmd);
1686 my $bindv6only = "$rootdir/etc/sysctl.d/bindv6only.conf";
1687 if (-f
$bindv6only) {
1689 $cmd .= ' -e \'s/^\(net\.ipv6\.bindv6only.*\)/#\1/\'';
1690 $cmd .= " -i '$bindv6only'";
1691 $self->run_command ($cmd);
1694 if ($suite eq 'hardy' || $suite eq 'intrepid' || $suite eq 'jaunty') {
1695 # disable tty init (console-setup)
1697 $cmd .= ' -e \'s/^\(ACTIVE_CONSOLES=.*\)/ACTIVE_CONSOLES=/\'';
1698 $cmd .= " -i '$rootdir/etc/default/console-setup'";
1699 $self->run_command ($cmd);
1702 if ($suite eq 'intrepid' || $suite eq 'jaunty') {
1703 # remove sysctl setup (avoid warnings at startup)
1704 my $filelist = "$rootdir/etc/sysctl.d/10-console-messages.conf";
1705 $filelist .= " $rootdir/etc/sysctl.d/10-process-security.conf" if $suite eq 'intrepid';
1706 $filelist .= " $rootdir/etc/sysctl.d/10-network-security.conf";
1707 $self->run_command ("rm $filelist");
1710 if (-e
"$rootdir/lib/systemd/system/sys-kernel-config.mount") {
1711 $self->ve_command ("ln -s /dev/null /etc/systemd/system/sys-kernel-debug.mount");
1718 my $veid = $self->{veid
};
1719 my $conffile = $self->{veconffile
};
1721 my $vestat = $self->ve_status();
1723 if (!$vestat->{exist
}) {
1724 $self->logmsg ("Please create the appliance first (bootstrap)");
1728 if (!$vestat->{running
}) {
1729 $self->run_command ("lxc-start -n $veid -f $conffile");
1732 system ("lxc-attach -n $veid --rcfile $conffile --clear-env");
1735 sub ve_mysql_command
{
1736 my ($self, $sql, $password) = @_;
1738 #my $bootstrap = "/usr/sbin/mysqld --bootstrap --user=mysql --skip-grant-tables " .
1739 #"--skip-bdb --skip-innodb --skip-ndbcluster";
1741 $self->ve_command ("mysql", $sql);
1744 sub ve_mysql_bootstrap
{
1745 my ($self, $sql, $password) = @_;
1749 my $suite = $self->{config
}->{suite
};
1751 if ($suite eq 'jessie') {
1752 my $rootdir = $self->{rootfs
};
1753 $self->run_command ("sed -e 's/^key_buffer\\s*=/key_buffer_size =/' -i $rootdir/etc/mysql/my.cnf");
1756 if ($suite eq 'squeeze' || $suite eq 'wheezy' || $suite eq 'jessie') {
1757 $cmd = "/usr/sbin/mysqld --bootstrap --user=mysql --skip-grant-tables";
1760 $cmd = "/usr/sbin/mysqld --bootstrap --user=mysql --skip-grant-tables " .
1761 "--skip-bdb --skip-innodb --skip-ndbcluster";
1764 $self->ve_command ($cmd, $sql);
1767 sub compute_required
{
1768 my ($self, $pkglist) = @_;
1770 my $pkginfo = $self->pkginfo();
1771 my $instpkgs = $self->read_installed ();
1774 __record_provides
($pkginfo, $closure, [keys $instpkgs->%*]);
1776 return $self->closure ($closure, $pkglist);
1780 my ($self, $opts) = @_;
1782 my @supp = ('7.4', '8.1');
1783 my $pgversion; # NOTE: not setting that defaults to the distro default, normally the best choice
1785 my $suite = $self->{config
}->{suite
};
1787 if ($suite eq 'lenny' || $suite eq 'hardy' || $suite eq 'intrepid' || $suite eq 'jaunty') {
1790 } elsif ($suite eq 'squeeze') {
1793 } elsif ($suite eq 'wheezy') {
1796 } elsif ($suite eq 'jessie') {
1799 } elsif ($suite eq 'stretch') {
1802 } elsif ($suite eq 'buster') {
1805 } elsif ($suite eq 'bullseye') {
1807 } elsif ($suite eq 'bookworm') {
1808 # FIXME: update once froozen
1809 @supp = ('13', '14');
1811 $pgversion = $opts->{version
} if $opts->{version
};
1814 if (defined($pgversion)) {
1815 die "unsupported postgres version '$pgversion'\n" if !grep { $pgversion eq $_; } @supp;
1817 $required = $self->compute_required (["postgresql-$pgversion"]);
1819 $required = $self->compute_required (["postgresql"]);
1822 $self->cache_packages ($required);
1824 $self->ve_dpkg ('install', @$required);
1826 my $iscript = "postgresql-$pgversion";
1827 if ($suite eq 'squeeze' || $suite eq 'wheezy' || $suite eq 'jessie' ||
1828 $suite eq 'stretch') {
1829 $iscript = 'postgresql';
1832 $self->ve_command ("/etc/init.d/$iscript start") if $opts->{start
};
1836 my ($self, $opts) = @_;
1838 my $password = $opts->{password
};
1839 my $rootdir = $self->{rootfs
};
1841 my $suite = $self->{config
}->{suite
};
1844 if ($suite eq 'squeeze') {
1846 } elsif ($suite eq 'wheezy' || $suite eq 'jessie') {
1849 die "task_mysql: unsupported suite '$suite'";
1852 my $required = $self->compute_required (['mysql-common', "mysql-server-$ver"]);
1854 $self->cache_packages ($required);
1856 $self->ve_dpkg ('install', @$required);
1858 # fix security (see /usr/bin/mysql_secure_installation)
1859 my $sql = "DELETE FROM mysql.user WHERE User='';\n" .
1860 "DELETE FROM mysql.user WHERE User='root' AND Host!='localhost';\n" .
1861 "FLUSH PRIVILEGES;\n";
1862 $self->ve_mysql_bootstrap ($sql);
1866 my $rpw = $password eq 'random' ?
'admin' : $password;
1868 my $sql = "USE mysql;\n" .
1869 "UPDATE user SET password=PASSWORD(\"$rpw\") WHERE user
='root';\n" .
1870 "FLUSH PRIVILEGES
;\n";
1871 $self->ve_mysql_bootstrap ($sql);
1873 write_file ("[client
]\nuser
=root
\npassword
=\"$rpw\"\n", "$rootdir/root/.my.cnf", 0600);
1874 if ($password eq 'random') {
1875 $self->install_init_script ($script_mysql_randompw, 2, 20);
1879 $self->ve_command ("/etc/init.d/mysql start") if $opts->{start
};
1883 my ($self, $opts) = @_;
1885 my $memlimit = $opts->{memlimit
};
1886 my $rootdir = $self->{rootfs
};
1887 my $suite = $self->{config
}->{suite
};
1889 my $base_set = [qw(php-cli libapache2-mod-php php-gd)];
1890 if ($suite =~ /(?:squeeze|wheezy|jessie)$/) {
1891 $self->logmsg("WARN: using EOL php release on EOL suite");
1892 $base_set = [qw(php5 php5-cli libapache2-mod-php5 php5-gd)];
1894 my $required = $self->compute_required($base_set);
1896 $self->cache_packages ($required);
1898 $self->ve_dpkg ('install', @$required);
1901 my $sed_cmd = ['sed', '-e', "s/^\\s*memory_limit\\s*=.*;/memory_limit = ${memlimit}M;/", '-i'];
1902 if ($suite =~ /(?:squeeze|wheezy|jessie)$/) {
1903 push @$sed_cmd, "$rootdir/etc/php5/apache2/php.ini";
1906 for my $fn (glob("'${rootdir}/etc/php/*/apache2/php.ini'")) {
1907 push @$sed_cmd, "$rootdir/$fn";
1911 warn "WARN: did not found any php.ini to set the memlimit!\n";
1915 $self->run_command($sed_cmd);
1920 my ($self, $pkglist, $unpack) = @_;
1922 my $required = $self->compute_required ($pkglist);
1924 $self->cache_packages ($required);
1926 $self->ve_dpkg ($unpack ?
'unpack' : 'install', @$required);
1930 my ($self, $distclean) = @_;
1932 unlink $self->{logfile
};
1933 unlink "$self->{targetname}.tar";
1934 unlink "$self->{targetname}.tar.gz";
1936 $self->ve_destroy ();
1939 rmtree
$self->{cachedir
} if $distclean && !$self->{config
}->{cachedir
};
1941 rmtree
$self->{infodir
};