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",
224 ostype
=> "ubuntu-22.10",
232 my $suiteinfo = $supported_suites->{$suite} || die "unsupported suite '$suite'!\n";
235 $suiteinfo->{origin
} //= 'debian';
236 $suiteinfo->{suite
} //= $suite;
238 $suiteinfo->{flags
} //= {};
239 if ($suiteinfo->{ostype
} =~ /^devuan/) {
240 $suiteinfo->{flags
}->{systemd
} //= 0;
242 $suiteinfo->{flags
}->{systemd
} //= 1;
249 my ($self, $url, $path) = @_;
250 my $tmpfn = "$path.tmp$$";
252 $self->logmsg ("download: $url\n");
254 eval { $self->run_command ("wget -q '$url' -O '$tmpfn'") };
260 rename ($tmpfn, $path);
264 my ($data, $file, $perm) = @_;
266 die "no filename" if !$file;
270 my $fh = IO
::File-
>new ($file, O_WRONLY
| O_CREAT
, $perm) ||
271 die "unable to open file '$file'";
281 die "no filename" if !$file;
283 my $fh = IO
::File-
>new ($file) ||
284 die "unable to open file '$file'";
286 local $/; # slurp mode
300 my $fh = IO
::File-
>new ("<$filename") || return $res;
303 while (defined (my $line = <$fh>)) {
304 next if $line =~ m/^\#/;
305 next if $line =~ m/^\s*$/;
315 if ($rec =~ s/^Description:\s*([^\n]*)(\n\s+.*)*$//si) {
316 $res->{headline
} = $1;
317 chomp $res->{headline
};
320 $res->{description
} = $long;
321 chomp $res->{description
};
322 } elsif ($rec =~ s/^([^:]+):\s*(.*\S)\s*\n//) {
323 my ($key, $value) = (lc ($1), $2);
324 if ($key eq 'source' || $key eq 'mirror') {
325 push @{$res->{$key}}, $value;
327 die "duplicate key '$key'\n" if defined ($res->{$key});
328 $res->{$key} = $value;
331 die "unable to parse config file: $rec";
335 die "unable to parse config file" if $rec;
341 my ($self, $cmd, $input, $getoutput) = @_;
343 my $reader = IO
::File-
>new();
344 my $writer = IO
::File-
>new();
345 my $error = IO
::File-
>new();
349 my $cmdstr = ref ($cmd) eq 'ARRAY' ?
join (' ', @$cmd) : $cmd;
353 if (ref ($cmd) eq 'ARRAY') {
354 $pid = open3
($writer, $reader, $error, @$cmd) || die $!;
356 $pid = open3
($writer, $reader, $error, $cmdstr) || die $!;
363 if ($orig_pid != $$) {
364 $self->logmsg ("ERROR: command '$cmdstr' failed - fork failed\n");
371 print $writer $input if defined $input;
374 my $select = IO
::Select-
>new();
375 $select->add ($reader);
376 $select->add ($error);
379 my $logfd = $self->{logfd
};
381 while ($select->count) {
382 my @handles = $select->can_read ();
384 foreach my $h (@handles) {
386 my $count = sysread ($h, $buf, 4096);
387 if (!defined ($count)) {
389 die "command '$cmdstr' failed: $!";
391 $select->remove ($h) if !$count;
395 $res .= $buf if $getoutput;
402 die "command '$cmdstr' failed with exit code $ec\n" if $ec;
410 $self->writelog (@_);
415 my $fd = $self->{logfd
};
419 sub __sample_config
{
423 my $arch = $self->{config
}->{architecture
};
425 my $ostype = $self->{config
}->{ostype
};
427 if ($ostype =~ m/^de(bi|vu)an-/) {
428 $data .= "lxc.include = /usr/share/lxc/config/debian.common.conf\n";
429 } elsif ($ostype =~ m/^ubuntu-/) {
430 $data .= "lxc.include = /usr/share/lxc/config/ubuntu.common.conf\n";
432 die "unknown os type '$ostype'\n";
434 $data .= "lxc.uts.name = localhost\n";
435 $data .= "lxc.rootfs.path = $self->{rootfs}\n";
444 if (my $fd = IO
::File-
>new (".veid")) {
451 $self->{working_dir
} = getcwd
;
452 $self->{veconffile
} = "$self->{working_dir}/config";
453 $self->{rootfs
} = "$self->{working_dir}/rootfs";
456 $self->{veid
} = $cid;
462 UUID
::generate
($uuid);
463 UUID
::unparse
($uuid, $uuid_str);
464 $self->{veid
} = $uuid_str;
466 my $fd = IO
::File-
>new (">.veid") ||
467 die "unable to write '.veid'\n";
468 print $fd "$self->{veid}\n";
471 my $cdata = $self->__sample_config();
473 my $fh = IO
::File-
>new ($self->{veconffile
}, O_WRONLY
|O_CREAT
|O_EXCL
) ||
474 die "unable to write lxc config file '$self->{veconffile}' - $!";
478 mkdir $self->{rootfs
} || die "unable to create rootfs - $!";
480 $self->logmsg ("allocated VE $self->{veid}\n");
482 return $self->{veid
};
485 # just use some simple heuristic for now, merge usr for releases newer than ubuntu 21.x or debian 11
489 my $ostype = $self->{config
}->{ostype
};
491 # FIXME: add configuration override posibillity
493 if ($ostype =~ m/^debian-(\d+)/) {
494 return int($1) >= 11;
495 } elsif ($ostype =~ m/^ubuntu-(\d+)/) {
496 return int($1) >= 21;
501 sub setup_usr_merge
{
504 my $rootfs = $self->{rootfs
};
505 my $arch = $self->{config
}->{architecture
};
507 # similar to https://salsa.debian.org/installer-team/debootstrap/-/blob/master/functions#L1354
508 my @merged_dirs = qw(bin sbin lib);
510 if ($arch eq 'amd64') {
511 push @merged_dirs, qw(lib32 lib64 libx32);
512 } elsif ($arch eq 'i386') {
513 push @merged_dirs, qw(lib64 libx32);
516 $self->logmsg ("setup usr-merge symlinks for '" . join("', '", @merged_dirs) . "'\n");
518 for my $dir (@merged_dirs) {
519 symlink("usr/$dir", "$rootfs/$dir") or warn "could not create symlink - $!\n";
520 mkpath
"$rootfs/usr/$dir";
525 my ($class, $config) = @_;
527 $class = ref ($class) || $class;
531 $config = read_config
('dab.conf') if !$config;
533 $self->{config
} = $config;
537 $self->{logfile
} = "logfile";
538 $self->{logfd
} = IO
::File-
>new (">>$self->{logfile}") ||
539 die "unable to open log file";
541 my $arch = $config->{architecture
};
542 die "no 'architecture' specified\n" if !$arch;
544 die "unsupported architecture '$arch'\n"
545 if $arch !~ m/^(i386|amd64)$/;
547 my $suite = $config->{suite
} || die "no 'suite' specified\n";
549 my $suiteinfo = get_suite_info
($suite);
550 $suite = $suiteinfo->{suite
};
551 $config->{ostype
} = $suiteinfo->{ostype
};
553 my $name = $config->{name
} || die "no 'name' specified\n";
555 $name =~ m/^[a-z][0-9a-z\-\*\.]+$/ ||
556 die "illegal characters in name '$name'\n";
558 my $version = $config->{version
};
559 die "no 'version' specified\n" if !$version;
560 die "no 'section' specified\n" if !$config->{section
};
561 die "no 'description' specified\n" if !$config->{headline
};
562 die "no 'maintainer' specified\n" if !$config->{maintainer
};
564 if ($name =~ m/^$config->{ostype}/) {
565 $self->{targetname
} = "${name}_${version}_$config->{architecture}";
567 $self->{targetname
} = "$config->{ostype}-${name}_" .
568 "${version}_$config->{architecture}";
571 if (!$config->{source
}) {
572 if (lc($suiteinfo->{origin
}) eq 'debian') {
573 if ($suite eq 'etch' || $suite eq 'lenny') {
574 push @{$config->{source
}}, (
575 'http://ftp.debian.org/debian SUITE main contrib',
576 'http://security.debian.org SUITE/updates main contrib',
578 } elsif ($suite eq 'bullseye' || $suite eq 'bookworm') {
579 push @{$config->{source
}}, (
580 "http://ftp.debian.org/debian SUITE main contrib",
581 "http://ftp.debian.org/debian SUITE-updates main contrib",
582 "http://security.debian.org SUITE-security main contrib",
585 push @{$config->{source
}}, (
586 "http://ftp.debian.org/debian SUITE main contrib",
587 "http://ftp.debian.org/debian SUITE-updates main contrib",
588 "http://security.debian.org SUITE/updates main contrib",
591 } elsif (lc($suiteinfo->{origin
}) eq 'ubuntu') {
592 my $comp = "main restricted universe multiverse";
593 push @{$config->{source
}}, (
594 "http://archive.ubuntu.com/ubuntu SUITE $comp",
595 "http://archive.ubuntu.com/ubuntu SUITE-updates $comp",
596 "http://archive.ubuntu.com/ubuntu SUITE-security $comp",
605 foreach my $s (@{$config->{source
}}) {
606 if ($s =~ m
@^\s*((http
|ftp
)://\S
+)\s
+(\S
+)((\s
+(\S
+))+)$@) {
607 my ($url, $su, $components) = ($1, $3, $4);
608 $su =~ s/SUITE/$suite/;
609 $components =~ s/^\s+//;
610 $components =~ s/\s+$//;
612 foreach my $co (split (/\s+/, $components)) {
615 $ca = ['main'] if !$ca;
623 die "syntax error in source spezification '$s'\n";
627 foreach my $m (@{$config->{mirror
}}) {
628 if ($m =~ m
@^\s*((http
|ftp
)://\S
+)\s
*=>\s
*((http
|ftp
)://\S
+)\s
*$@) {
629 my ($ms, $md) = ($1, $3);
631 foreach my $ss (@$sources) {
632 if ($ss->{source
} eq $ms) {
638 die "unusable mirror $ms\n" if !$found;
640 die "syntax error in mirror spezification '$m'\n";
644 $self->{sources
} = $sources;
646 $self->{infodir
} = "info";
648 $self->__allocate_ve();
650 $self->{cachedir
} = ($config->{cachedir
} || 'cache') . "/$suite";;
652 my $incl = [qw
(less ssh openssh-server logrotate
)];
654 my $excl = [qw
(modutils reiserfsprogs ppp pppconfig pppoe pppoeconf nfs-common mtools ntp
)];
656 # ubuntu has too many dependencies on udev, so
657 # we cannot exclude it (instead we disable udevd)
659 if (lc($suiteinfo->{origin
}) eq 'ubuntu' && $suiteinfo->{flags
}->{systemd
}) {
660 push @$incl, 'isc-dhcp-client';
661 push @$excl, qw(libmodule-build-perl plymouth plymouth-theme-ubuntu-text powermgmt-base);
662 if ($suite eq 'jammy') {
663 push @$excl, qw(fuse); # avoid fuse2 <-> fuse3 conflict
665 } elsif ($suite eq 'trusty') {
666 push @$excl, qw(systemd systemd-services libpam-systemd libsystemd-daemon0 memtest86+);
667 } elsif ($suite eq 'precise') {
668 push @$excl, qw(systemd systemd-services libpam-systemd libsystemd-daemon0 memtest86+ ubuntu-standard);
669 } elsif ($suite eq 'hardy') {
670 push @$excl, qw(kbd);
671 push @$excl, qw(apparmor apparmor-utils ntfs-3g friendly-recovery);
672 } elsif ($suite eq 'intrepid' || $suite eq 'jaunty') {
673 push @$excl, qw(apparmor apparmor-utils libapparmor1 libapparmor-perl libntfs-3g28);
674 push @$excl, qw(ntfs-3g friendly-recovery);
675 } elsif ($suite eq 'jessie') {
676 push @$incl, 'sysvinit-core'; # avoid systemd and udev
677 push @$incl, 'libperl4-corelibs-perl'; # to make lsof happy
678 push @$excl, qw(systemd systemd-sysv udev module-init-tools pciutils hdparm memtest86+ parted);
679 } elsif ($suite eq 'stretch' || $suite eq 'buster' || $suite eq 'bullseye' || $suite eq 'bookworm') {
680 push @$excl, qw(module-init-tools pciutils hdparm memtest86+ parted);
682 push @$excl, qw(udev module-init-tools pciutils hdparm memtest86+ parted);
685 $self->{incl
} = $incl;
686 $self->{excl
} = $excl;
694 my $infodir = $self->{infodir
};
695 my $arch = $self->{config
}->{architecture
};
701 my $logfd = $self->{logfd
} = IO
::File-
>new (">$self->{logfile}") ||
702 die "unable to open log file";
715 foreach my $ss (@{$self->{sources
}}) {
716 my $src = $ss->{mirror
} || $ss->{source
};
717 my $path = "dists/$ss->{suite}/Release";
718 my $url = "$src/$path";
719 my $target = __url_to_filename
("$ss->{source}/$path");
721 $self->download ($url, "$infodir/$target");
722 $self->download ("$url.gpg", "$infodir/$target.gpg");
723 # fixme: impl. verify (needs --keyring option)
727 warn "Release info ignored\n";
730 foreach my $comp (@{$ss->{comp
}}) {
731 foreach my $compressor (@$COMPRESSORS) {
732 $path = "dists/$ss->{suite}/$comp/binary-$arch/Packages.$compressor->{ext}";
733 $target = "$infodir/" . __url_to_filename
("$ss->{source}/$path");
734 my $pkgsrc = "$src/$path";
736 $self->download ($pkgsrc, $target);
737 $self->run_command ("$compressor->{decomp} '$target'");
740 print $logfd "could not download Packages.$compressor->{ext}\n";
750 my ($self, $filename, $size) = @_;
752 my $config = $self->{config
};
756 $data .= "Name: $config->{name}\n";
757 $data .= "Version: $config->{version}\n";
758 $data .= "Type: lxc\n";
759 $data .= "OS: $config->{ostype}\n";
760 $data .= "Section: $config->{section}\n";
761 $data .= "Maintainer: $config->{maintainer}\n";
762 $data .= "Architecture: $config->{architecture}\n";
763 $data .= "Installed-Size: $size\n";
766 $data .= "Infopage: $config->{infopage}\n" if $config->{infopage
};
767 $data .= "ManageUrl: $config->{manageurl}\n" if $config->{manageurl
};
768 $data .= "Certified: $config->{certified}\n" if $config->{certified
};
771 $data .= "Description: $config->{headline}\n";
772 $data .= "$config->{description}\n" if $config->{description
};
774 write_file
($data, $filename, 0644);
778 my ($self, $opts) = @_;
780 my $suite = $self->{config
}->{suite
};
781 my $infodir = $self->{infodir
};
782 my $arch = $self->{config
}->{architecture
};
784 my $instpkgs = $self->read_installed ();
785 my $pkginfo = $self->pkginfo();
786 my $veid = $self->{veid
};
787 my $conffile = $self->{veconffile
};
788 my $rootdir = $self->{rootfs
};
790 my $vestat = $self->ve_status();
791 die "ve not running - unable to finalize\n" if !$vestat->{running
};
794 if (-f
"$rootdir/etc/init.d/mysql") {
795 $self->ve_command ("/etc/init.d/mysql stop");
798 if (!($opts->{keepmycnf
} || (-f
"$rootdir/etc/init.d/mysql_randompw"))) {
799 unlink "$rootdir/root/.my.cnf";
802 if ($suite eq 'etch') {
803 # enable apache2 startup
804 if ($instpkgs->{apache2
}) {
805 write_file
("NO_START=0\n", "$rootdir/etc/default/apache2");
807 unlink "$rootdir/etc/default/apache2";
810 $self->logmsg ("cleanup package status\n");
811 # prevent auto selection of all standard, required or important
812 # packages which are not installed
813 foreach my $pkg (keys %$pkginfo) {
814 my $pri = $pkginfo->{$pkg}->{priority
};
815 if ($pri && ($pri eq 'required' || $pri eq 'important'
816 || $pri eq 'standard')) {
817 if (!$instpkgs->{$pkg}) {
818 $self->ve_dpkg_set_selection ($pkg, 'purge');
823 $self->ve_command ("apt-get clean");
825 $self->logmsg ("update available package list\n");
827 $self->ve_command ("dpkg --clear-avail");
828 foreach my $ss (@{$self->{sources
}}) {
829 my $relsrc = __url_to_filename
("$ss->{source}/dists/$ss->{suite}/Release");
830 if (-f
"$infodir/$relsrc" && -f
"$infodir/$relsrc.gpg") {
831 $self->run_command ("cp '$infodir/$relsrc' '$rootdir/var/lib/apt/lists/$relsrc'");
832 $self->run_command ("cp '$infodir/$relsrc.gpg' '$rootdir/var/lib/apt/lists/$relsrc.gpg'");
834 foreach my $comp (@{$ss->{comp
}}) {
835 my $src = __url_to_filename
("$ss->{source}/dists/$ss->{suite}/" .
836 "$comp/binary-$arch/Packages");
837 my $target = "/var/lib/apt/lists/$src";
838 $self->run_command ("cp '$infodir/$src' '$rootdir/$target'");
839 $self->ve_command ("dpkg --merge-avail '$target'");
843 # set dselect default method
844 write_file
("apt apt\n", "$rootdir/var/lib/dpkg/cmethopt");
846 $self->ve_divert_remove ("/usr/sbin/policy-rc.d");
848 $self->ve_divert_remove ("/sbin/start-stop-daemon");
850 $self->ve_divert_remove ("/sbin/init");
852 # finally stop the VE
853 $self->run_command ("lxc-stop -n $veid --rcfile $conffile --kill");
855 unlink "$rootdir/sbin/defenv";
856 unlink <$rootdir/root/dead.letter
*>;
857 unlink "$rootdir/var/log/init.log";
858 unlink "$rootdir/aquota.group", "$rootdir/aquota.user";
860 write_file
("", "$rootdir/var/log/syslog");
862 my $get_path_size = sub {
864 my $sizestr = $self->run_command ("du -sm $path", undef, 1);
865 if ($sizestr =~ m/^(\d+)\s+\Q$path\E$/) {
868 die "unable to detect size for '$path'\n";
872 $self->logmsg ("detecting final appliance size: ");
873 my $size = $get_path_size->($rootdir);
874 $self->logmsg ("$size MB\n");
876 $self->write_config ("$rootdir/etc/appliance.info", $size);
878 $self->logmsg ("creating final appliance archive\n");
880 my $target = "$self->{targetname}.tar";
882 my $compressor = $opts->{compressor
} // 'gz';
883 my $compressor2cmd_map = {
888 'zstd-max' => 'zstd -19 -T0', # maximal level where the decompressor can still run efficiently
890 my $compressor2ending = {
895 my $compressor_cmd = $compressor2cmd_map->{$compressor};
896 die "unkown compressor '$compressor', use one of: ". join(', ', sort keys %$compressor2cmd_map)
897 if !defined($compressor_cmd);
899 my $ending = $compressor2ending->{$compressor} // $compressor;
900 my $final_archive = "${target}.${ending}";
902 unlink $final_archive;
904 $self->run_command ("tar cpf $target --numeric-owner -C '$rootdir' ./etc/appliance.info");
905 $self->run_command ("tar rpf $target --numeric-owner -C '$rootdir' --exclude ./etc/appliance.info .");
906 $self->run_command ("$compressor_cmd $target");
908 $self->logmsg ("detecting final commpressed appliance size: ");
909 $size = $get_path_size->($final_archive);
910 $self->logmsg ("$size MB\n");
912 $self->logmsg ("appliance archive: $final_archive\n");
918 my $rootdir = $self->{rootfs
};
920 my $pkgfilelist = "$rootdir/var/lib/dpkg/status";
922 open(my $PKGLST, '<', $pkgfilelist) or die "unable to open '$pkgfilelist' - $!";
926 while (my $rec = <$PKGLST>) {
932 while ($rec =~ s/^([^:]+):\s+(.*?)\s*\n//) {
936 my $pkg = $res->{'package'};
937 if (my $status = $res->{status
}) {
938 my @sa = split (/\s+/, $status);
940 if ($stat && ($stat ne 'purge')) {
941 $pkglist->{$pkg} = $res;
954 my $veid = $self->{veid
};
956 my $res = { running
=> 0 };
958 $res->{exist
} = 1 if -d
"$self->{rootfs}/usr";
960 my $filename = "/proc/net/unix";
962 # similar test is used by lcxcontainers.c: list_active_containers
963 my $fh = IO
::File-
>new ($filename, "r");
966 while (defined(my $line = <$fh>)) {
967 if ($line =~ m/^[a-f0-9]+:\s\S+\s\S+\s\S+\s\S+\s\S+\s\d+\s(\S+)$/) {
969 if ($path =~ m!^@/\S+/$veid/command$!) {
980 my ($self, $cmd, $input) = @_;
982 my $veid = $self->{veid
};
983 my $conffile = $self->{veconffile
};
985 if (ref ($cmd) eq 'ARRAY') {
986 unshift @$cmd, 'lxc-attach', '-n', $veid, '--rcfile', $conffile, '--clear-env', '--', 'defenv';
987 $self->run_command($cmd, $input);
989 $self->run_command("lxc-attach -n $veid --rcfile $conffile --clear-env -- defenv $cmd", $input);
993 # like ve_command, but pipes stdin correctly
995 my ($self, @cmd) = @_;
997 my $veid = $self->{veid
};
998 my $conffile = $self->{veconffile
};
1001 my $pid = open2
($reader, "<&STDIN", 'lxc-attach', '-n', $veid, '--rcfile', $conffile, '--',
1002 'defenv', @cmd) || die "unable to exec command";
1004 while (defined (my $line = <$reader>)) {
1005 $self->logmsg ($line);
1011 die "ve_exec failed - status $rc\n" if $rc != 0;
1015 my ($self, $filename) = @_;
1017 $self->ve_command ("dpkg-divert --add --divert '$filename.distrib' " .
1018 "--rename '$filename'");
1020 sub ve_divert_remove
{
1021 my ($self, $filename) = @_;
1023 my $rootdir = $self->{rootfs
};
1025 unlink "$rootdir/$filename";
1026 $self->ve_command ("dpkg-divert --remove --rename '$filename'");
1029 sub ve_debconfig_set
{
1030 my ($self, $dcdata) = @_;
1032 my $rootdir = $self->{rootfs
};
1033 my $cfgfile = "/tmp/debconf.txt";
1034 write_file
($dcdata, "$rootdir/$cfgfile");
1035 $self->ve_command ("debconf-set-selections $cfgfile");
1036 unlink "$rootdir/$cfgfile";
1039 sub ve_dpkg_set_selection
{
1040 my ($self, $pkg, $status) = @_;
1042 $self->ve_command ("dpkg --set-selections", "$pkg $status");
1046 my ($self, $cmd, @pkglist) = @_;
1048 return if !scalar (@pkglist);
1050 my $pkginfo = $self->pkginfo();
1052 my $rootdir = $self->{rootfs
};
1053 my $cachedir = $self->{cachedir
};
1057 foreach my $pkg (@pkglist) {
1058 my $filename = $self->getpkgfile ($pkg);
1059 $self->run_command ("cp '$cachedir/$filename' '$rootdir/$filename'");
1060 push @files, "/$filename";
1061 $self->logmsg ("$cmd: $pkg\n");
1064 my $fl = join (' ', @files);
1066 if ($cmd eq 'install') {
1067 $self->ve_command ("dpkg --force-depends --force-confold --install $fl");
1068 } elsif ($cmd eq 'unpack') {
1069 $self->ve_command ("dpkg --force-depends --unpack $fl");
1071 die "internal error";
1074 foreach my $fn (@files) { unlink "$rootdir$fn"; }
1080 my $veid = $self->{veid
}; # fixme
1081 my $conffile = $self->{veconffile
};
1083 my $vestat = $self->ve_status();
1084 if ($vestat->{running
}) {
1085 $self->run_command ("lxc-stop -n $veid --rcfile $conffile --kill");
1088 rmtree
$self->{rootfs
};
1089 unlink $self->{veconffile
};
1095 my $veid = $self->{veid
};
1096 my $conffile = $self->{veconffile
};
1098 $self->logmsg ("initialize VE $veid\n");
1100 my $vestat = $self->ve_status();
1101 if ($vestat->{running
}) {
1102 $self->run_command ("lxc-stop -n $veid --rcfile $conffile --kill");
1105 rmtree
$self->{rootfs
};
1106 mkpath
$self->{rootfs
};
1109 sub __deb_version_cmp
{
1110 my ($cur, $op, $new) = @_;
1112 if (system("dpkg", "--compare-versions", $cur, $op, $new) == 0) {
1119 sub __parse_packages
{
1120 my ($pkginfo, $filename, $src) = @_;
1123 open(my $PKGLST, '<', $filename) or die "unable to open '$filename' - $!";
1125 while (my $rec = <$PKGLST>) {
1126 $rec =~ s/\n\s+/ /g;
1132 while ($rec =~ s/^([^:]+):\s+(.*?)\s*\n//) {
1136 my $pkg = $res->{'package'};
1137 if ($pkg && $res->{'filename'}) {
1139 if (my $info = $pkginfo->{$pkg}) {
1140 $cur = $info->{version
};
1142 my $new = $res->{version
};
1143 if (!$cur || __deb_version_cmp
($cur, 'lt', $new)) {
1145 $res->{url
} = "$src/$res->{'filename'}";
1147 die "no url for package '$pkg'" if !$res->{url
};
1149 $pkginfo->{$pkg} = $res;
1160 return $self->{pkginfo
} if $self->{pkginfo
};
1162 my $infodir = $self->{infodir
};
1163 my $arch = $self->{config
}->{architecture
};
1165 my $availfn = "$infodir/available";
1170 # reading 'available' is faster, because it only contains latest version
1171 # (no need to do slow version compares)
1173 __parse_packages
($pkginfo, $availfn);
1174 $self->{pkginfo
} = $pkginfo;
1178 $self->logmsg ("generating available package list\n");
1180 foreach my $ss (@{$self->{sources
}}) {
1181 foreach my $comp (@{$ss->{comp
}}) {
1182 my $url = "$ss->{source}/dists/$ss->{suite}/$comp/binary-$arch/Packages";
1183 my $pkgfilelist = "$infodir/" . __url_to_filename
($url);
1185 my $src = $ss->{mirror
} || $ss->{source
};
1187 __parse_packages
($pkginfo, $pkgfilelist, $src);
1191 if (my $dep = $self->{config
}->{depends
}) {
1192 foreach my $d (split (/,/, $dep)) {
1193 if ($d =~ m/^\s*(\S+)\s*(\((\S+)\s+(\S+)\)\s*)?$/) {
1194 my ($pkg, $op, $rver) = ($1, $3, $4);
1195 $self->logmsg ("checking dependencies: $d\n");
1196 my $info = $pkginfo->{$pkg};
1197 die "package '$pkg' not available\n" if !$info;
1199 my $cver = $info->{version
};
1200 if (!__deb_version_cmp
($cver, $op, $rver)) {
1201 die "detected wrong version '$cver'\n";
1205 die "syntax error in depends field";
1210 $self->{pkginfo
} = $pkginfo;
1212 my $tmpfn = "$availfn.tmp$$";
1213 my $fd = IO
::File-
>new (">$tmpfn");
1214 foreach my $pkg (sort keys %$pkginfo) {
1215 my $info = $pkginfo->{$pkg};
1216 print $fd "package: $pkg\n";
1217 foreach my $k (sort keys %$info) {
1218 next if $k eq 'description';
1219 next if $k eq 'package';
1220 my $v = $info->{$k};
1221 print $fd "$k: $v\n" if $v;
1223 print $fd "description: $info->{description}\n" if $info->{description
};
1228 rename ($tmpfn, $availfn);
1233 sub __record_provides
{
1234 my ($pkginfo, $closure, $list, $skipself) = @_;
1236 foreach my $pname (@$list) {
1237 my $info = $pkginfo->{$pname};
1238 # fixme: if someone install packages directly using dpkg, there
1239 # is no entry in 'available', only in 'status'. In that case, we
1240 # should extract info from $instpkgs
1242 warn "hint: ignoring provides for '$pname' - package not in 'available' list.\n";
1245 if (my $prov = $info->{provides
}) {
1246 my @pl = split (',', $prov);
1247 foreach my $p (@pl) {
1249 if (!($skipself && (grep { $1 eq $_ } @$list))) {
1254 $closure->{$pname} = 1 if !$skipself;
1259 my ($self, $closure, $list) = @_;
1261 my $pkginfo = $self->pkginfo();
1263 # first, record provided packages
1264 __record_provides
($pkginfo, $closure, $list, 1);
1269 # then resolve dependencies
1270 foreach my $pname (@$list) {
1271 __closure_single
($pkginfo, $closure, $pkghash, $pkglist, $pname, $self->{excl
});
1277 sub __closure_single
{
1278 my ($pkginfo, $closure, $pkghash, $pkglist, $pname, $excl) = @_;
1282 $pname =~ s/:any$//;
1284 return if $closure->{$pname};
1286 my $info = $pkginfo->{$pname} || die "no such package '$pname'";
1288 my $dep = $info->{depends
};
1289 my $predep = $info->{'pre-depends'};
1291 my $size = $info->{size
};
1292 my $url = $info->{url
};
1294 $url || die "$pname: no url for package '$pname'";
1296 if (!$pkghash->{$pname}) {
1297 unshift @$pkglist, $pname;
1298 $pkghash->{$pname} = 1;
1301 __record_provides
($pkginfo, $closure, [$pname]) if $info->{provides
};
1303 $closure->{$pname} = 1;
1309 push @l, split (/,/, $predep) if $predep;
1310 push @l, split (/,/, $dep) if $dep;
1312 DEPEND
: foreach my $p (@l) {
1313 my @l1 = split (/\|/, $p);
1314 foreach my $p1 (@l1) {
1315 if ($p1 =~ m/^\s*(\S+).*/) {
1316 #printf (STDERR "$pname: $p --> $1\n");
1317 if ($closure->{$1}) {
1318 next DEPEND
; # dependency already met
1322 # search for non-excluded alternative
1324 foreach my $p1 (@l1) {
1325 if ($p1 =~ m/^\s*(\S+).*/) {
1326 next if grep { $1 eq $_ } @$excl;
1331 die "package '$pname' depends on exclusion '$p'\n" if !$found;
1333 #printf (STDERR "$pname: $p --> $found\n");
1335 __closure_single
($pkginfo, $closure, $pkghash, $pkglist, $found, $excl);
1339 sub cache_packages
{
1340 my ($self, $pkglist) = @_;
1342 foreach my $pkg (@$pkglist) {
1343 $self->getpkgfile ($pkg);
1348 my ($self, $pkg) = @_;
1350 my $pkginfo = $self->pkginfo();
1351 my $info = $pkginfo->{$pkg} || die "no such package '$pkg'";
1352 my $cachedir = $self->{cachedir
};
1354 my $url = $info->{url
};
1357 if ($url =~ m
|/([^/]+.deb
)$|) {
1360 die "internal error";
1363 return $filename if -f
"$cachedir/$filename";
1367 $self->download ($url, "$cachedir/$filename");
1372 sub install_init_script
{
1373 my ($self, $script, $runlevel, $prio) = @_;
1375 my $suite = $self->{config
}->{suite
};
1376 my $suiteinfo = get_suite_info
($suite);
1377 my $rootdir = $self->{rootfs
};
1379 my $base = basename
($script);
1380 my $target = "$rootdir/etc/init.d/$base";
1382 $self->run_command ("install -m 0755 '$script' '$target'");
1383 if ($suite eq 'etch' || $suite eq 'lenny') {
1384 $self->ve_command ("update-rc.d $base start $prio $runlevel .");
1385 } elsif ($suiteinfo->{flags
}->{systemd
}) {
1386 die "unable to install init script (system uses systemd)\n";
1387 } elsif ($suite eq 'trusty' || $suite eq 'precise') {
1388 die "unable to install init script (system uses upstart)\n";
1390 $self->ve_command ("insserv $base");
1397 my ($self, $opts) = @_;
1399 my $pkginfo = $self->pkginfo();
1400 my $veid = $self->{veid
};
1401 my $suite = $self->{config
}->{suite
};
1402 my $suiteinfo = get_suite_info
($suite);
1404 my $important = [ @{$self->{incl
}} ];
1408 my $mta = $opts->{exim
} ?
'exim' : 'postfix';
1409 if ($mta eq 'postfix') {
1410 push @$important, "postfix";
1413 if ($opts->{include
}) {
1414 push @$important, split(',', $opts->{include
});
1418 if ($opts->{exclude
}) {
1419 $exclude->{$_} = 1 for split(',', $opts->{exclude
});
1422 foreach my $p (sort keys %$pkginfo) {
1423 next if grep { $p eq $_ } @{$self->{excl
}};
1424 my $pri = $pkginfo->{$p}->{priority
};
1426 next if $mta ne 'exim' && $p =~ m/exim/;
1427 next if $p =~ m/(selinux|semanage|policycoreutils)/;
1429 push @$required, $p if $pri eq 'required';
1430 next if $exclude->{$p};
1431 push @$important, $p if $pri eq 'important';
1432 push @$standard, $p if $pri eq 'standard' && !$opts->{minimal
};
1436 $required = $self->closure($closure, $required);
1437 $important = $self->closure($closure, $important);
1439 if (!$opts->{minimal
}) {
1440 $standard = $self->closure($closure, $standard);
1443 # test if we have all 'ubuntu-minimal' and 'ubuntu-standard' packages
1444 # except those explicitly excluded
1445 if ($suite eq 'hardy' || $suite eq 'intrepid' || $suite eq 'jaunty') {
1446 my $mdeps = $pkginfo->{'ubuntu-minimal'}->{depends
};
1447 foreach my $d (split (/,/, $mdeps)) {
1448 if ($d =~ m/^\s*(\S+)$/) {
1450 next if $closure->{$pkg};
1451 next if grep { $pkg eq $_ } @{$self->{excl
}};
1452 die "missing ubuntu-minimal package '$pkg'\n";
1455 if (!$opts->{minimal
}) {
1456 $mdeps = $pkginfo->{'ubuntu-standard'}->{depends
};
1457 foreach my $d (split (/,/, $mdeps)) {
1458 if ($d =~ m/^\s*(\S+)$/) {
1460 next if $closure->{$pkg};
1461 next if grep { $pkg eq $_ } @{$self->{excl
}};
1462 die "missing ubuntu-standard package '$pkg'\n";
1468 # download/cache all files first
1469 $self->cache_packages ($required);
1470 $self->cache_packages ($important);
1471 $self->cache_packages ($standard);
1473 my $rootdir = $self->{rootfs
};
1475 # extract required packages first
1476 $self->logmsg ("create basic environment\n");
1478 if ($self->can_usr_merge()) {
1479 $self->setup_usr_merge();
1482 my $compressor2opt = {
1487 my $compressor_re = join('|', keys $compressor2opt->%*);
1489 $self->logmsg ("extract required packages to rootfs\n");
1490 foreach my $p (@$required) {
1491 my $filename = $self->getpkgfile ($p);
1492 my $content = $self->run_command("ar -t '$self->{cachedir}/$filename'", undef, 1);
1493 if ($content =~ m/^(data.tar.($compressor_re))$/m) {
1495 my $tar_opts = "--keep-directory-symlink $compressor2opt->{$2}";
1497 $self->run_command("ar -p '$self->{cachedir}/$filename' '$archive' | tar -C '$rootdir' -xf - $tar_opts");
1499 die "unexpected error for $p: no data.tar.{xz,gz,zst} found...";
1504 my $data = "Package: dpkg\n" .
1505 "Version: $pkginfo->{dpkg}->{version}\n" .
1506 "Status: install ok installed\n";
1508 write_file
($data, "$rootdir/var/lib/dpkg/status");
1509 write_file
("", "$rootdir/var/lib/dpkg/info/dpkg.list");
1510 write_file
("", "$rootdir/var/lib/dpkg/available");
1513 foreach my $ss (@{$self->{sources
}}) {
1514 my $url = $ss->{source
};
1515 my $comp = join (' ', @{$ss->{comp
}});
1516 $data .= "deb $url $ss->{suite} $comp\n\n";
1519 write_file
($data, "$rootdir/etc/apt/sources.list");
1521 $data = "# UNCONFIGURED FSTAB FOR BASE SYSTEM\n";
1522 write_file
($data, "$rootdir/etc/fstab", 0644);
1524 write_file
("localhost\n", "$rootdir/etc/hostname", 0644);
1526 # avoid warnings about non-existent resolv.conf
1527 write_file
("", "$rootdir/etc/resolv.conf", 0644);
1529 if (lc($suiteinfo->{origin
}) eq 'ubuntu' && $suiteinfo->{flags
}->{systemd
}) {
1530 # no need to configure loopback device
1531 # FIXME: Debian (systemd based?) too?
1533 $data = "auto lo\niface lo inet loopback\n";
1534 mkdir "$rootdir/etc/network";
1535 write_file
($data, "$rootdir/etc/network/interfaces", 0644);
1539 $self->run_command ("tar xzf '$devicetar' -C '$rootdir'");
1541 # avoid warnings about missing default locale
1542 write_file
("LANG=\"C\"\n", "$rootdir/etc/default/locale", 0644);
1545 rename ("$rootdir/sbin/init", "$rootdir/sbin/init.org");
1546 $self->run_command ("cp '$fake_init' '$rootdir/sbin/init'");
1548 $self->run_command ("cp '$default_env' '$rootdir/sbin/defenv'");
1550 $self->run_command ("lxc-start -n $veid -f $self->{veconffile}");
1552 $self->logmsg ("initialize ld cache\n");
1553 $self->ve_command ("/sbin/ldconfig");
1554 $self->run_command ("ln -sf mawk '$rootdir/usr/bin/awk'");
1556 $self->logmsg ("installing packages\n");
1558 $self->ve_dpkg ('install', 'base-files', 'base-passwd');
1560 $self->ve_dpkg ('install', 'dpkg');
1562 $self->run_command ("ln -sf /usr/share/zoneinfo/UTC '$rootdir/etc/localtime'");
1564 $self->run_command ("ln -sf bash '$rootdir/bin/sh'");
1566 $self->ve_dpkg ('install', 'libc6');
1567 $self->ve_dpkg ('install', 'perl-base');
1569 unlink "$rootdir/usr/bin/awk";
1571 $self->ve_dpkg ('install', 'mawk');
1572 $self->ve_dpkg ('install', 'debconf');
1574 # unpack required packages
1575 foreach my $p (@$required) {
1576 $self->ve_dpkg ('unpack', $p);
1579 rename ("$rootdir/sbin/init.org", "$rootdir/sbin/init");
1580 $self->ve_divert_add ("/sbin/init");
1581 $self->run_command ("cp '$fake_init' '$rootdir/sbin/init'");
1583 # disable service activation
1584 $self->ve_divert_add ("/usr/sbin/policy-rc.d");
1585 $data = "#!/bin/sh\nexit 101\n";
1586 write_file
($data, "$rootdir/usr/sbin/policy-rc.d", 755);
1588 # disable start-stop-daemon
1589 $self->ve_divert_add ("/sbin/start-stop-daemon");
1593 echo \"Warning: Fake start-stop-daemon called, doing nothing\"
1595 write_file
($data, "$rootdir/sbin/start-stop-daemon", 0755);
1598 $self->ve_divert_add ("/sbin/udevd");
1600 if ($suite eq 'etch') {
1601 write_file
("NO_START=1\n", "$rootdir/etc/default/apache2"); # disable apache2 startup
1604 $self->logmsg ("configure required packages\n");
1605 $self->ve_command ("dpkg --force-confold --skip-same-version --configure -a");
1607 # set postfix defaults
1608 if ($mta eq 'postfix') {
1609 $data = "postfix postfix/main_mailer_type select Local only\n";
1610 $self->ve_debconfig_set ($data);
1612 $data = "postmaster: root\nwebmaster: root\n";
1613 write_file
($data, "$rootdir/etc/aliases");
1616 if ($suite eq 'jaunty') {
1617 # jaunty does not create /var/run/network, so network startup fails.
1618 # so we do not use tmpfs for /var/run and /var/lock
1619 $self->run_command ("sed -e 's/RAMRUN=yes/RAMRUN=no/' -e 's/RAMLOCK=yes/RAMLOCK=no/' -i $rootdir/etc/default/rcS");
1620 # and create the directory here
1621 $self->run_command ("mkdir $rootdir/var/run/network");
1624 # unpack base packages
1625 foreach my $p (@$important) {
1626 $self->ve_dpkg ('unpack', $p);
1630 if (-x
"$rootdir/sbin/ifconfig") {
1631 $self->ve_command ("ifconfig lo up");
1633 $self->ve_command ("ip link set lo up");
1636 $self->logmsg ("configure important packages\n");
1637 $self->ve_command ("dpkg --force-confold --skip-same-version --configure -a");
1639 if (-d
"$rootdir/etc/event.d") {
1640 unlink <$rootdir/etc/event.d
/tty
*>;
1643 if (-f
"$rootdir/etc/inittab") {
1644 $self->run_command ("sed -i -e '/getty\\s38400\\stty[23456]/d' '$rootdir/etc/inittab'");
1647 # Link /etc/mtab to /proc/mounts, so df and friends will work:
1648 unlink "$rootdir/etc/mtab";
1649 $self->ve_command ("ln -s /proc/mounts /etc/mtab");
1652 $self->ve_command ("usermod -L root");
1654 if ($mta eq 'postfix') {
1655 $data = "postfix postfix/main_mailer_type select No configuration\n";
1656 $self->ve_debconfig_set ($data);
1658 unlink "$rootdir/etc/mailname";
1659 write_file
($postfix_main_cf, "$rootdir/etc/postfix/main.cf");
1662 if (!$opts->{minimal
}) {
1663 # unpack standard packages
1664 foreach my $p (@$standard) {
1665 $self->ve_dpkg ('unpack', $p);
1668 $self->logmsg ("configure standard packages\n");
1669 $self->ve_command ("dpkg --force-confold --skip-same-version --configure -a");
1672 # disable HWCLOCK access
1673 $self->run_command ("echo 'HWCLOCKACCESS=no' >> '$rootdir/etc/default/rcS'");
1676 $self->ve_divert_add ("/usr/sbin/hald");
1678 # disable /dev/urandom init
1679 $self->run_command ("install -m 0755 '$script_init_urandom' '$rootdir/etc/init.d/urandom'");
1681 if ($suite eq 'etch' || $suite eq 'hardy' || $suite eq 'intrepid' || $suite eq 'jaunty') {
1683 $self->ve_divert_add ("/sbin/klogd");
1686 # remove unnecessays sysctl entries to avoid warnings
1688 $cmd .= ' -e \'s/^\(kernel\.printk.*\)/#\1/\'';
1689 $cmd .= ' -e \'s/^\(kernel\.maps_protect.*\)/#\1/\'';
1690 $cmd .= ' -e \'s/^\(fs\.inotify\.max_user_watches.*\)/#\1/\'';
1691 $cmd .= ' -e \'s/^\(vm\.mmap_min_addr.*\)/#\1/\'';
1692 $cmd .= " -i '$rootdir/etc/sysctl.conf'";
1693 $self->run_command ($cmd);
1695 my $bindv6only = "$rootdir/etc/sysctl.d/bindv6only.conf";
1696 if (-f
$bindv6only) {
1698 $cmd .= ' -e \'s/^\(net\.ipv6\.bindv6only.*\)/#\1/\'';
1699 $cmd .= " -i '$bindv6only'";
1700 $self->run_command ($cmd);
1703 if ($suite eq 'hardy' || $suite eq 'intrepid' || $suite eq 'jaunty') {
1704 # disable tty init (console-setup)
1706 $cmd .= ' -e \'s/^\(ACTIVE_CONSOLES=.*\)/ACTIVE_CONSOLES=/\'';
1707 $cmd .= " -i '$rootdir/etc/default/console-setup'";
1708 $self->run_command ($cmd);
1711 if ($suite eq 'intrepid' || $suite eq 'jaunty') {
1712 # remove sysctl setup (avoid warnings at startup)
1713 my $filelist = "$rootdir/etc/sysctl.d/10-console-messages.conf";
1714 $filelist .= " $rootdir/etc/sysctl.d/10-process-security.conf" if $suite eq 'intrepid';
1715 $filelist .= " $rootdir/etc/sysctl.d/10-network-security.conf";
1716 $self->run_command ("rm $filelist");
1719 if (-e
"$rootdir/lib/systemd/system/sys-kernel-config.mount") {
1720 $self->ve_command ("ln -s /dev/null /etc/systemd/system/sys-kernel-debug.mount");
1727 my $veid = $self->{veid
};
1728 my $conffile = $self->{veconffile
};
1730 my $vestat = $self->ve_status();
1732 if (!$vestat->{exist
}) {
1733 $self->logmsg ("Please create the appliance first (bootstrap)");
1737 if (!$vestat->{running
}) {
1738 $self->run_command ("lxc-start -n $veid -f $conffile");
1741 system ("lxc-attach -n $veid --rcfile $conffile --clear-env");
1744 sub ve_mysql_command
{
1745 my ($self, $sql, $password) = @_;
1747 #my $bootstrap = "/usr/sbin/mysqld --bootstrap --user=mysql --skip-grant-tables " .
1748 #"--skip-bdb --skip-innodb --skip-ndbcluster";
1750 $self->ve_command ("mysql", $sql);
1753 sub ve_mysql_bootstrap
{
1754 my ($self, $sql, $password) = @_;
1758 my $suite = $self->{config
}->{suite
};
1760 if ($suite eq 'jessie') {
1761 my $rootdir = $self->{rootfs
};
1762 $self->run_command ("sed -e 's/^key_buffer\\s*=/key_buffer_size =/' -i $rootdir/etc/mysql/my.cnf");
1765 if ($suite eq 'squeeze' || $suite eq 'wheezy' || $suite eq 'jessie') {
1766 $cmd = "/usr/sbin/mysqld --bootstrap --user=mysql --skip-grant-tables";
1769 $cmd = "/usr/sbin/mysqld --bootstrap --user=mysql --skip-grant-tables " .
1770 "--skip-bdb --skip-innodb --skip-ndbcluster";
1773 $self->ve_command ($cmd, $sql);
1776 sub compute_required
{
1777 my ($self, $pkglist) = @_;
1779 my $pkginfo = $self->pkginfo();
1780 my $instpkgs = $self->read_installed ();
1783 __record_provides
($pkginfo, $closure, [keys $instpkgs->%*]);
1785 return $self->closure ($closure, $pkglist);
1789 my ($self, $opts) = @_;
1791 my @supp = ('7.4', '8.1');
1792 my $pgversion; # NOTE: not setting that defaults to the distro default, normally the best choice
1794 my $suite = $self->{config
}->{suite
};
1796 if ($suite eq 'lenny' || $suite eq 'hardy' || $suite eq 'intrepid' || $suite eq 'jaunty') {
1799 } elsif ($suite eq 'squeeze') {
1802 } elsif ($suite eq 'wheezy') {
1805 } elsif ($suite eq 'jessie') {
1808 } elsif ($suite eq 'stretch') {
1811 } elsif ($suite eq 'buster') {
1814 } elsif ($suite eq 'bullseye') {
1816 } elsif ($suite eq 'bookworm') {
1817 # FIXME: update once froozen
1818 @supp = ('13', '14');
1820 $pgversion = $opts->{version
} if $opts->{version
};
1823 if (defined($pgversion)) {
1824 die "unsupported postgres version '$pgversion'\n" if !grep { $pgversion eq $_; } @supp;
1826 $required = $self->compute_required (["postgresql-$pgversion"]);
1828 $required = $self->compute_required (["postgresql"]);
1831 $self->cache_packages ($required);
1833 $self->ve_dpkg ('install', @$required);
1835 my $iscript = "postgresql-$pgversion";
1836 if ($suite eq 'squeeze' || $suite eq 'wheezy' || $suite eq 'jessie' ||
1837 $suite eq 'stretch') {
1838 $iscript = 'postgresql';
1841 $self->ve_command ("/etc/init.d/$iscript start") if $opts->{start
};
1845 my ($self, $opts) = @_;
1847 my $password = $opts->{password
};
1848 my $rootdir = $self->{rootfs
};
1850 my $suite = $self->{config
}->{suite
};
1853 if ($suite eq 'squeeze') {
1855 } elsif ($suite eq 'wheezy' || $suite eq 'jessie') {
1858 die "task_mysql: unsupported suite '$suite'";
1861 my $required = $self->compute_required (['mysql-common', "mysql-server-$ver"]);
1863 $self->cache_packages ($required);
1865 $self->ve_dpkg ('install', @$required);
1867 # fix security (see /usr/bin/mysql_secure_installation)
1868 my $sql = "DELETE FROM mysql.user WHERE User='';\n" .
1869 "DELETE FROM mysql.user WHERE User='root' AND Host!='localhost';\n" .
1870 "FLUSH PRIVILEGES;\n";
1871 $self->ve_mysql_bootstrap ($sql);
1875 my $rpw = $password eq 'random' ?
'admin' : $password;
1877 my $sql = "USE mysql;\n" .
1878 "UPDATE user SET password=PASSWORD(\"$rpw\") WHERE user
='root';\n" .
1879 "FLUSH PRIVILEGES
;\n";
1880 $self->ve_mysql_bootstrap ($sql);
1882 write_file ("[client
]\nuser
=root
\npassword
=\"$rpw\"\n", "$rootdir/root/.my.cnf", 0600);
1883 if ($password eq 'random') {
1884 $self->install_init_script ($script_mysql_randompw, 2, 20);
1888 $self->ve_command ("/etc/init.d/mysql start") if $opts->{start
};
1892 my ($self, $opts) = @_;
1894 my $memlimit = $opts->{memlimit
};
1895 my $rootdir = $self->{rootfs
};
1896 my $suite = $self->{config
}->{suite
};
1898 my $base_set = [qw(php-cli libapache2-mod-php php-gd)];
1899 if ($suite =~ /(?:squeeze|wheezy|jessie)$/) {
1900 $self->logmsg("WARN: using EOL php release on EOL suite");
1901 $base_set = [qw(php5 php5-cli libapache2-mod-php5 php5-gd)];
1903 my $required = $self->compute_required($base_set);
1905 $self->cache_packages ($required);
1907 $self->ve_dpkg ('install', @$required);
1910 my $sed_cmd = ['sed', '-e', "s/^\\s*memory_limit\\s*=.*;/memory_limit = ${memlimit}M;/", '-i'];
1911 if ($suite =~ /(?:squeeze|wheezy|jessie)$/) {
1912 push @$sed_cmd, "$rootdir/etc/php5/apache2/php.ini";
1915 for my $fn (glob("'${rootdir}/etc/php/*/apache2/php.ini'")) {
1916 push @$sed_cmd, "$rootdir/$fn";
1920 warn "WARN: did not found any php.ini to set the memlimit!\n";
1924 $self->run_command($sed_cmd);
1929 my ($self, $pkglist, $unpack) = @_;
1931 my $required = $self->compute_required ($pkglist);
1933 $self->cache_packages ($required);
1935 $self->ve_dpkg ($unpack ?
'unpack' : 'install', @$required);
1939 my ($self, $distclean) = @_;
1941 unlink $self->{logfile
};
1942 unlink "$self->{targetname}.tar";
1943 unlink "$self->{targetname}.tar.gz";
1945 $self->ve_destroy ();
1948 rmtree
$self->{cachedir
} if $distclean && !$self->{config
}->{cachedir
};
1950 rmtree
$self->{infodir
};