From e523741610a4ed8579642bfc755956f64c847ef3 Mon Sep 17 00:00:00 2001 From: Johannes Schauer Marin Rodrigues Date: Sat, 17 Jun 2023 13:46:32 +0200 Subject: [PATCH] add hooks/copy-host-apt-sources-and-preferences --- .../customize00.pl | 44 +++++++++++++++++++ .../setup00.sh | 43 ++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100755 hooks/copy-host-apt-sources-and-preferences/customize00.pl create mode 100755 hooks/copy-host-apt-sources-and-preferences/setup00.sh diff --git a/hooks/copy-host-apt-sources-and-preferences/customize00.pl b/hooks/copy-host-apt-sources-and-preferences/customize00.pl new file mode 100755 index 0000000..53f6059 --- /dev/null +++ b/hooks/copy-host-apt-sources-and-preferences/customize00.pl @@ -0,0 +1,44 @@ +#!/usr/bin/perl +# +# This script makes sure that all packages that are installed both locally as +# well as inside the chroot have the same version. +# +# It is implemented in Perl because there are no associative arrays in POSIX +# shell. + +use strict; +use warnings; + +sub get_pkgs { + my $root = shift; + my %pkgs = (); + open(my $fh, '-|', 'dpkg-query', "--root=$root", '--showformat', + '${binary:Package}=${Version}\n', '--show') + // die "cannot exec dpkg-query"; + while (my $line = <$fh>) { + my ($pkg, $ver) = split(/=/, $line, 2); + $pkgs{$pkg} = $ver; + } + close $fh; + if ($? != 0) { die "failed to run dpkg-query" } + return %pkgs; +} + +my %pkgs_local = get_pkgs('/'); +my %pkgs_chroot = get_pkgs($ARGV[0]); + +my @diff = (); +foreach my $pkg (keys %pkgs_chroot) { + next unless exists $pkgs_local{$pkg}; + if ($pkgs_local{$pkg} ne $pkgs_chroot{$pkg}) { + push @diff, $pkg; + } +} + +if (scalar @diff > 0) { + print STDERR "E: packages from the host and the chroot differ:\n"; + foreach my $pkg (@diff) { + print STDERR "E: $pkg $pkgs_local{$pkg} $pkgs_chroot{$pkg}\n"; + } + exit 1; +} diff --git a/hooks/copy-host-apt-sources-and-preferences/setup00.sh b/hooks/copy-host-apt-sources-and-preferences/setup00.sh new file mode 100755 index 0000000..d04e6c8 --- /dev/null +++ b/hooks/copy-host-apt-sources-and-preferences/setup00.sh @@ -0,0 +1,43 @@ +#!/bin/sh + +set -eu + +if [ "${MMDEBSTRAP_VERBOSITY:-1}" -ge 3 ]; then + set -x +fi + +if [ -n "${MMDEBSTRAP_SUITE:-}" ]; then + if [ "${MMDEBSTRAP_VERBOSITY:-1}" -ge 1 ]; then + echo "W: using a non-empty suite name $MMDEBSTRAP_SUITE does not make sense with this hook and might select the wrong Essential:yes package set" >&2 + fi +fi + +rootdir="$1" + +SOURCELIST="/etc/apt/sources.list" +eval "$(apt-config shell SOURCELIST Dir::Etc::SourceList/f)" +SOURCEPARTS="/etc/apt/sources.d/" +eval "$(apt-config shell SOURCEPARTS Dir::Etc::SourceParts/d)" +PREFERENCES="/etc/apt/preferences" +eval "$(apt-config shell PREFERENCES Dir::Etc::Preferences/f)" +PREFERENCESPARTS="/etc/apt/preferences.d/" +eval "$(apt-config shell PREFERENCESPARTS Dir::Etc::PreferencesParts/d)" + +for f in "$SOURCELIST" \ + "$SOURCEPARTS"/*.list \ + "$SOURCEPARTS"/*.sources \ + "$PREFERENCES" \ + "$PREFERENCESPARTS"/*; do + [ -e "$f" ] || continue + if [ -e "$rootdir/$f" ]; then + if [ "${MMDEBSTRAP_VERBOSITY:-1}" -ge 2 ]; then + echo "I: $f already exists in chroot, appending..." >&2 + fi + echo >> "$rootdir/$f" + fi + cat "$f" >> "$rootdir/$f" + if [ "${MMDEBSTRAP_VERBOSITY:-1}" -ge 3 ]; then + echo "D: contents of $f inside the chroot:" >&2 + cat "$rootdir/$f" >&2 + fi +done