#!/usr/bin/perl # Copyright (C) 2009, 2010 Neil Williams # # This package is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . use strict; use warnings; use IO::File; use Config::Auto; use File::Basename; use Parse::Debian::Packages; use POSIX qw(locale_h); use Locale::gettext; use vars qw/ $progname $ourversion $dstrap $extra @aptsources $mirror @archives $deb $cachedir $config_str %packages $retval $str $retries $dir $include $arch $foreign $suite $url $unpack $sourcedir $msg $etcdir @e $sourcesname $libdir $dpkgdir @debootstrap %suites %components $chk $component $repo @dirs @touch %sources $section %keys $host $key $value $type $file $config $tidy $noauth $keyring %keyrings $deflist $cfgdir @extrapkgs @includes %source $setupsh $configsh $omitrequired $dryrun /; setlocale(LC_MESSAGES, ""); textdomain("multistrap"); $progname = basename($0); $ourversion = &our_version(); $unpack = "true"; while( @ARGV ) { $_= shift( @ARGV ); last if m/^--$/; if (!/^-/) { unshift(@ARGV,$_); last; } elsif (/^(-\?|-h|--help|--version)$/) { &usageversion(); exit( 0 ); } elsif (/^(-f|--file)$/) { $file = shift(@ARGV); } elsif (/^(-a|--arch)$/) { $arch = shift(@ARGV); } elsif (/^(-d|--dir)$/) { $dir = shift(@ARGV); $dir .= ($dir =~ m:/$:) ? '' : "/"; } elsif (/^(--tidy-up)$/) { $tidy++; } elsif (/^(--source-dir)$/) { $sourcedir = shift (@ARGV); $sourcedir .= ($sourcedir =~ m:/$:) ? '' : "/"; $sourcedir = (-d $sourcedir) ? $sourcedir : undef; } elsif (/^(--no-auth)$/) { $noauth++; } elsif (/^(--dry-run|--simulate)$/) { $dryrun++; } else { die "$progname: "._g("Unknown option")." $_.\n"; } } $msg = sprintf (_g("Need a configuration file - use %s -f\n"), $progname); die ($msg) if (not defined $file); $cfgdir=dirname($file); cascade($file); foreach my $inc (@includes) { # look for the full filepath or try same directory as current conf. if (not -f $inc ) { $chk = `realpath $cfgdir/$inc 2>/dev/null`; next if ($chk =~ /^\n?$/); chomp ($chk); } else { $chk = $inc; } printf (_g("%s %s using %s\n"), $progname, $ourversion, $chk); cascade($chk); } &dump_config if (defined $dryrun); # Translators: fields are: programname, versionstring, configfile. printf (_g("%s %s using %s\n"), $progname, $ourversion, $file); $host = `dpkg-architecture -qDEB_BUILD_ARCH`; chomp ($host); if ((not defined $arch) or ($arch eq "")) { $arch = $host; printf (_g("Defaulting architecture to native: %s\n"),$arch); } elsif ($arch eq $host) { printf (_g("Defaulting architecture to native: %s\n"),$arch); } else { printf (_g("Using foreign architecture: %s\n"), $arch); } $foreign++ if ($host ne $arch); unless (keys %sources and @aptsources) { my $msg = sprintf(_g("No sources defined for a foreign multistrap. Using your existing apt sources. To use different sources, list them with aptsources= in '%s'."), $file); warn ("$progname: $msg\n"); $deflist = prepare_sources_list(); } # Translators: fields are: programname, architecture, host architecture. printf (_g("%s building %s multistrap on '%s'\n"), $progname, $arch, $host); $cachedir = "var/cache/apt/"; # archives $libdir = "var/lib/apt/"; # lists $etcdir = "etc/apt/"; # sources $dpkgdir = "var/lib/dpkg/"; # state if (not -d "$dir") { my $ret = mkdir ("$dir"); die ("Unable to create directory '$dir' $!\n") if ($ret == 0); } $dir = `realpath $dir`; chomp ($dir); $dir .= ($dir =~ m:/$:) ? '' : "/"; system ("mkdir -p ${dir}${cachedir}") if (not -d "${dir}${cachedir}"); system ("mkdir -p ${dir}${libdir}") if (not -d "${dir}${libdir}"); system ("mkdir -p ${dir}${dpkgdir}") if (not -d "${dir}${dpkgdir}"); system ("mkdir -p ${dir}etc/apt/sources.list.d/") if (not -d "${dir}etc/apt/sources.list.d/"); system ("mkdir -p ${dir}etc/apt/preferences.d/") if (not -d "${dir}etc/apt/preferences.d/"); system ("mkdir -p ${dir}usr/share/info/") if (not -d "${dir}usr/share/info/"); system ("touch ${dir}usr/share/info/dir"); #system ("mkdir -p ${dir}/usr/share/doc/gcc-4.2-base/") # if (not -d "${dir}/usr/share/doc/gcc-4.2-base/"); #system ("touch ${dir}/usr/share/doc/gcc-4.2-base/.copyright"); #system ("touch ${dir}/usr/share/doc/gcc-4.2-base/#changelog.Debian.gz"); my $msg = sprintf(_g("Unable to create directory '%s'\n"), "${dir}etc/apt/preferences.d/"); die ($msg) if (not -d "${dir}etc/apt/preferences.d/"); @dirs = qw/ alternatives info parts updates/; @touch = qw/ diversions statoverride status lock/; foreach my $dpkgd (@dirs) { if (not -d "${dir}${dpkgdir}$dpkgd") { mkdir "${dir}${dpkgdir}$dpkgd"; } } foreach my $file (@touch) { utime(time, time, "${dir}${dpkgdir}/$file") or ( open(F, ">${dir}${dpkgdir}/$file") && close F ); } utime(time, time, "${dir}etc/shells") or (open(F, ">${dir}etc/shells") && close F ); if (not -d "${dir}etc/network") { mkdir "${dir}etc/network"; } if (not -d "${dir}dev") { mkdir "${dir}dev"; } # prevent the absolute symlink in libc6 from allowing # writes outside the multistrap root dir. See: #553599 if (-l "${dir}lib64" ) { my $r = readlink "${dir}lib64"; if ($r =~ m:^/:) { my $old = `pwd`; chomp ($old); unlink "${dir}lib64"; chdir ("$dir"); print _g("INF: ./lib64 -> /lib symbolic link reset to ./lib.\n"); symlink "./lib", "lib64"; chdir ("${old}"); } } else { my $old = `pwd`; chomp ($old); chdir ("$dir"); print _g("INF: Setting ./lib64 -> ./lib symbolic link.\n"); symlink "./lib", "lib64"; chdir ("${old}"); } unlink ("${dir}etc/apt/sources.list.d/multistrap.sources.list") if (-f "${dir}etc/apt/sources.list.d/multistrap.sources.list"); unlink ("${dir}etc/apt/sources.list") if (-f "${dir}etc/apt/sources.list"); foreach $repo (sort keys %suites) { if (not -e "${dir}${cachedir}") { mkdir "${dir}${cachedir}"; } if (not -e "$dir/${libdir}lists") { mkdir "$dir/${libdir}lists"; } if (not -e "$dir/${libdir}lists/partial") { mkdir "$dir/${libdir}lists/partial"; } if (not -e "$dir/${cachedir}archives") { mkdir "$dir/${cachedir}archives"; } if (not -e "$dir/${cachedir}archives/partial") { mkdir "$dir/${cachedir}archives/partial"; } } foreach my $aptsrc (@aptsources) { if (defined $deflist) { open (SOURCES, ">>${dir}etc/apt/sources.list.d/multistrap.sources.list") or die _g("Cannot open sources list"). $!; print SOURCES $deflist; close SOURCES; } elsif (-d "${dir}etc/apt/") { open (SOURCES, ">>${dir}etc/apt/sources.list.d/multistrap.sources.list") or die _g("Cannot open sources list"). $!; $mirror = $sources{$aptsrc}; $suite = $suites{$aptsrc}; $component = (defined $components{$aptsrc}) ? $components{$aptsrc} : "main"; print SOURCES< /lib symbolic link reset to ./lib after unpacking.\n"); printf (_g("ERR: Some files may have been unpacked outside %s!\n"), $dir); } else { printf (_g("\nMultistrap system installed successfully in %s.\n\n"), $dir); } } unlink ("${dir}etc/apt/sources.list.d/multistrap.sources.list") if (-f "${dir}etc/apt/sources.list.d/multistrap.sources.list"); foreach my $aptsrc (@aptsources) { if (defined $deflist) { open (SOURCES, ">>${dir}etc/apt/sources.list.d/multistrap.sources.list") or die _g("Cannot open sources list"). $!; print SOURCES $deflist; close SOURCES; } elsif (-d "${dir}etc/apt/") { open (SOURCES, ">>${dir}etc/apt/sources.list.d/multistrap.sources.list") or die _g("Cannot open sources list"). $!; $mirror = $sources{$aptsrc}; $suite = $suites{$aptsrc}; $component = (defined $components{$aptsrc}) ? $components{$aptsrc} : "main"; print SOURCES<${dir}${libdir}lists/lock"); close (LOCK); opendir (DEBS, "${dir}${cachedir}archives/") or die (_g("Cannot read apt archives directory.\n")); @archives=grep(/.*\.deb$/, readdir DEBS); closedir (DEBS); } my $old = `pwd`; chomp ($old); chdir ("${dir}"); printf (_g("Using directory %s for unpacking operations\n"), $dir); foreach $deb (sort @archives) { printf (_g("I: Extracting %s...\n"), $deb); my $ver=`LC_ALL=C dpkg -f ./${cachedir}archives/$deb Version`; my $pkg=`LC_ALL=C dpkg -f ./${cachedir}archives/$deb Package`; chomp ($ver); chomp ($pkg); mkdir ("./tmp"); my $tmpdir = `mktemp -p ./tmp -d -t multistrap.XXXXXX`; chomp ($tmpdir); my $datatar = `LC_ALL=C dpkg -X ./${cachedir}archives/$deb ${dir}`; my $exit = `echo $?`; chomp ($exit); if ($exit ne "0") { printf(_g("dpkg -X failed with error code %s\nSkipping...\n"), $exit); next; } my @lines = split("\n", $datatar); open (LIST, ">>./${dpkgdir}info/${pkg}.list"); foreach my $l (@lines) { chomp ($l); $l =~ s:^\.::; $l =~ s:^/$:/\.:; $l =~ s:/$::; print LIST "$l\n"; } close (LIST); system ("dpkg -e ./${cachedir}archives/$deb ${tmpdir}/"); opendir (MAINT, "./${tmpdir}"); my @maint=grep(!m:\.\.?:, readdir (MAINT)); closedir (MAINT); open (AVAIL, ">>./${dpkgdir}available"); open (STATUS, ">>./${dpkgdir}status"); foreach my $mscript (@maint) { rename "./${tmpdir}/$mscript", "./${dpkgdir}info/$pkg.$mscript"; if ( $mscript eq "control" ) { open (MSCRIPT, "./${dpkgdir}info/$pkg.$mscript"); my @scr=; close (MSCRIPT); my @avail = grep(!/^$/, @scr); print AVAIL @avail; print STATUS @avail; print AVAIL "\n"; print STATUS "Status: install ok unpacked\n"; unlink ("./${dpkgdir}info/$mscript"); } } close (AVAIL); if ( -f "./${dpkgdir}info/$pkg.conffiles") { print STATUS "Conffiles:\n"; printf (_g(" -> Processing conffiles for %s\n"), $pkg); open (CONF, "./${dpkgdir}info/$pkg.conffiles"); my @lines=; close (CONF); foreach my $line (@lines) { chomp ($line); my $md5=`LC_ALL=C md5sum ./$line | cut -d" " -f1`; chomp ($md5); print STATUS " $line $md5\n"; } } print STATUS "\n"; close (STATUS); system ("rm -rf ./${tmpdir}"); if (-l "${dir}lib64" ) { my $r = readlink "${dir}lib64"; if ($r =~ m:^/:) { my $old = `pwd`; chomp ($old); printf (_g("ERR: lib64 -> ./lib symbolic link clobbered by %s\n"), $pkg); unlink "${dir}lib64"; chdir ("$dir"); print _g("INF: lib64 -> /lib symbolic link reset to ./lib.\n"); symlink "./lib", "lib64"; chdir ("${old}"); } } } chdir ("$old"); print _g("I: Unpacking complete.\n"); } sub check_bin_sh { $dir = shift; my $old = `pwd`; chomp ($old); # dash refuses to configure if no existing shell is found. # (always expects a diversion to already exist). # (works OK in subsequent upgrades.) #546528 unlink ("$dir/var/lib/dpkg/info/dash.postinst"); # now ensure that a usable shell is available as /bin/sh if (not -l "$dir/bin/sh") { print (_g("ERR: ./bin/sh symbolic link does not exist.\n")); if (-f "$dir/bin/dash") { print (_g("INF: Setting ./bin/sh -> ./bin/dash\n")); chdir ("$dir/bin"); symlink ("dash", "sh"); chdir ("$old"); } elsif (-f "$dir/bin/bash") { print (_g("INF: ./bin/dash not found. Setting ./bin/sh -> ./bin/bash\n")); chdir ("$dir/bin"); symlink ("bash", "sh"); chdir ("$old"); } } if (-l "$dir/bin/sh") { print ("${dir}bin/sh found OK:\n"); system ("(cd $dir ; ls -lh bin/sh)"); } else { die ("No shell."); } } sub tidy_apt { print _g("I: Tidying up apt cache and list data.\n"); unlink ("${dir}etc/apt/sources.list") if (-f "${dir}etc/apt/sources.list"); opendir (DEBS, "${dir}${libdir}lists/") or die (_g("Cannot read apt lists directory.\n")); my @lists=grep(!m:\.\.?$:, readdir DEBS); closedir (DEBS); foreach my $file (@lists) { next if (-d $file); unlink ("${dir}${libdir}lists/$file"); } opendir (DEBS, "${dir}${cachedir}/") or die (_g("Cannot read apt cache directory.\n")); my @files=grep(!m:\.\.?$:, readdir DEBS); closedir (DEBS); foreach my $file (@files) { next if (-d $file); next unless ($file =~ /\.bin$/); unlink ("${dir}${cachedir}$file"); } if ($unpack eq "true") { opendir (DEBS, "${dir}${cachedir}/archives/") or die (_g("Cannot read apt archives directory.\n")); my @files=grep(!m:\.\.?$:, readdir DEBS); closedir (DEBS); foreach my $file (@files) { next if (-d $file); next unless ($file =~ /\.deb$/); (defined $sourcedir) ? system ("mv ${dir}${cachedir}archives/$file $sourcedir/$file") : unlink ("${dir}${cachedir}archives/$file"); ; } } } # if native arch, do a few tasks just because we can and probably should. sub native { print _g("I: Native mode - configuring unpacked packages . . .\n"); my $e=`LC_ALL=C printenv`; my $str = ($e =~ /\nUSER=root\n/) ? "" : "sudo"; $str = (-f "/usr/bin/sudo") ? "$str" : ""; my $env = "DEBIAN_FRONTEND=noninteractive ". "DEBCONF_NONINTERACTIVE_SEEN=true ". "LC_ALL=C LANGUAGE=C LANG=C"; printf (_g("I: dpkg configuration settings:\n\t%s\n"), $env); # check that we have a workable shell inside the chroot &check_bin_sh("$dir"); system ("$str $env chroot $dir dpkg --configure -a"); opendir (PRI, "${dir}/var/lib/dpkg/info") or return; my @preinsts=grep(/\.preinst$/, readdir PRI); closedir (PRI); foreach my $script (sort @preinsts) { system ("$str $env chroot $dir /var/lib/dpkg/info/$script upgrade"); } } sub get_required_debs { # emulate required="$(get_debs Priority: required)" # from debootstrap/functions # needs to be run after the first apt-get install so that # Packages files exist my @required=(); my @debs=(); opendir (PKGS, "${dir}${libdir}lists/") or die sprintf(_g("Cannot open %s directory. %s\n"), "${dir}${libdir}lists/", $!); my @lists=grep(/_Packages$/, readdir (PKGS)); closedir (PKGS); foreach my $strap (@debootstrap) { my $s = lc($strap); foreach my $l (@lists) { push (@required, $l); } } foreach my $file (@required) { my $fh = IO::File->new("${dir}${libdir}lists/$file"); my $parser = Parse::Debian::Packages->new( $fh ); while (my %package = $parser->next) { next unless $package{'Priority'} eq "required"; push @debs, $package{'Package'}; } } return \@debs; } # inherited from apt-cross sub prepare_sources_list { my @source_list=(); # collate all available/configured sources into one list if (-e "/etc/apt/sources.list") { open (SOURCES, "/etc/apt/sources.list") or die _g("cannot open apt sources list. %s",$!); @source_list = ; close (SOURCES); } if (-d "/etc/apt/sources.list.d/") { opendir (FILES, "/etc/apt/sources.list.d/") or die _g("cannot open apt sources.list directory %s\n",$!); my @files = grep(!/^\.\.?$/, readdir FILES); foreach my $f (@files) { next if ($f =~ /\.ucf-old$/); open (SOURCES, "/etc/apt/sources.list.d/$f") or die _g("cannot open /etc/apt/sources.list.d/%s %s",$f, $!); while() { push @source_list, $_; } close (SOURCES); } closedir (FILES); } return \@source_list; } sub usageversion { printf STDERR (_g(" %s version %s Usage: %s [-a ARCH] [-d DIR] -f CONFIG_FILE %s -?|-h|--help|--version Command: -f|--file CONFIG_FILE: path the the multistrap configuration file. Options: -a|--arch ARCHITECTURE: override the configuration file architecture. -d|--dir PATH: override the configuration file directory. --no-auth: do not use Secure Apt for any repositories --tidy-up: remove apt cache data and downloaded archives. --dry-run: output the configuration and exit --simulate: output the configuration and exit -?|-h|--help: print this usage message and exit --version: print this usage message and exit %s extends debootstrap to provide support for multiple repositories, using a configuration file to specify the relevant suites, architecture, extra packages and the mirror to use for each repository. Example configuration: [General] arch=armel directory=/opt/multistrap/ # same as --tidy-up option if set to true cleanup=true # same as --no-auth option if set to true # keyring packages listed in each debootstrap will # still be installed. noauth=false # extract all downloaded archives (default is true) unpack=true # aptsources is a list of sections to be used for downloading packages # and lists and placed in the /etc/apt/sources.list.d/multistrap.sources.list # of the target. Order is not important aptsources=Grip Updates # the order of sections is not important. # the debootstrap option determines which repository # is used to calculate the list of Priority: required packages. debootstrap=Debian [Debian] packages= source=http://ftp.uk.debian.org/debian keyring=debian-archive-keyring suite=lenny This will result in a completely normal debootstrap of Debian lenny from the specified mirror, for armel in /opt/multistrap/. 'Architecture' and 'directory' can be overridden on the command line. Specify a package to extend the debootstap to include that package and all dependencies. Dependencies will be calculated by apt so as to use only the most recent suitable version from all configured repositories. General settings: 'directory' specifies the top level directory where the debootstrap will be created - it is not packed into a .tgz once complete. "), $progname, $ourversion, $progname, $progname, $progname) or die ("$progname: ". _g("failed to write usage:") . "$!\n"); } sub cascade { $file = shift; $config = Config::Auto::parse($file); foreach $key (%$config) { $type = lc($key) if (ref $key ne "HASH"); $value = $key if (ref $key eq "HASH"); $keys{$type} = $value; } foreach $section (sort keys %keys) { if ($section eq "general") { $arch = $keys{$section}{'arch'} if (defined $keys{$section}{'arch'} and (not defined $arch)); $dir = $keys{$section}{'directory'} if (defined $keys{$section}{'directory'} and (not defined $dir)); # support the original value but replace by new value. $unpack = lc($keys{$section}{'unpack'}) if (defined $keys{$section}{'forceunpack'} and (not defined $unpack)); $unpack = lc($keys{$section}{'unpack'}) if (defined $keys{$section}{'unpack'} and (not defined $unpack)); $configsh = lc($keys{$section}{'configscript'}) if (defined $keys{$section}{'configscript'} and (not defined $configsh) and (-x $configsh)); $setupsh = lc($keys{$section}{'setupscript'}) if (defined $keys{$section}{'setupscript'} and (not defined $setupsh) and (-x $setupsh)); $omitrequired = lc($keys{$section}{'omitrequired'}) if (defined $keys{$section}{'omitrequired'} and (not defined $omitrequired)); $tidy++ if ((defined $keys{$section}{'cleanup'}) and ($keys{$section}{'cleanup'} eq "true") and (defined $tidy)); $noauth++ if ((defined $keys{$section}{'noauth'}) and ($keys{$section}{'noauth'} eq "true") and (defined $noauth)); $sourcedir = $keys{$section}{'retainsources'} if ((defined $keys{$section}{'retainsources'}) and (-d $keys{$section}{'retainsources'})); my @d = split(' ', lc($keys{$section}{'debootstrap'})); push @debootstrap, @d; my @a = split (' ', lc($keys{$section}{'aptsources'})); push @aptsources, @a; my @i = split (' ', lc($keys{$section}{'include'})); push @includes, @i; } else { $sources{$section}=$keys{$section}{'source'} if (not exists $source{$section}); $packages{$section}=$keys{$section}{'packages'} if (not exists $packages{$section}); $suites{$section}=$keys{$section}{'suite'} if (not exists $suites{$section}); $components{$section}=$keys{$section}{'components'} if (not exists $components{$section}); if (not defined $components{$section}) { $components{$section}='main'; } $keyrings{$section}=$keys{$section}{'keyring'} if (not exists $keyrings{$section}); push @extrapkgs, split (' ', lc($keys{$section}{'additional'})); } } } sub _g { return gettext(shift); } sub dump_config { print "Debootstrap: ".join (", ", sort @debootstrap)."\n"; print "Apt sources: ".join (", ", sort @aptsources)."\n"; print "Includes: ".join (", ", sort @includes)."\n"; print "Sources: ".join (", ", sort values %sources)."\n"; print "Packages: ".join (", ", sort values %packages)."\n"; print "Suites: ".join (", ", sort values %suites)."\n"; print "Components: ".join (", ", sort values %components)."\n"; print "Extra Packages: ".join (", ", sort @extrapkgs)."\n" if (scalar @extrapkgs > 0); print "arch: $arch\n"; print "dir: $dir\n"; print "unpack: $unpack\n" if (defined $unpack); print "configscript: $configsh\n" if (defined $configsh); print "setupscript: $setupsh\n" if (defined $setupsh); print "omitrequired: $omitrequired\n" if (defined $omitrequired); print "tidy_apt: $tidy\n" if (defined $tidy); print "no_authentication: $noauth\n" if (defined $noauth); print "source_dir: $sourcedir\n" if (defined $sourcedir); exit 0; }