Subject: Install local packages using a local APT repo instead of using "dpkg -i"

From: Chris Lamb <[email-masked]>
Date: Wed, 16 Apr 2008 06:53:34 +0100

This patch modifies chroot_sources to build a small APT repo of the
local-packages and use that for local package installation instead of
"dpkg -i". This has a few advantages:

 * Removes the true ugliness that is "dpkg -i *.deb && apt-get install -f".
   This not only is slow and brittle, it causes scary "dpkg dependency
   error" messages to appear in build logs.

 * We can (ie. not implemented here) move to installing all the packages
   from:

    - lh_chroot_packages (ie. ${LH_PACKAGES})
    - lh_chroot_local-packages (ie. config/chroot_local-packages/*.deb)
    - lh_chroot_local-packageslists
    - lh_chroot_packages-lists
    - lh_chroot_linux-image

   .. in one shot. This would primarily improve speed as we would not keep
   invoking ${LH_APT}, and package installations can share triggers and
   suchlike -- installing a custom live-initramfs via local packages
   currently costs an additional "update-initramfs -u" call.

   It would also fix a number of obscure dependency cases, such as the one
   documented in #475739, and--if the user is using aptitude--may even
   result in better resolution choices.

 * Removes some messy and somewhat brittle code in lh_chroot_linux-image
   that edgecases a local live-initramfs.

The disadvantages are:

 * If local packages are being used and we are building in a chroot, we
   must refresh the sources list and rebuild the repo before building the
   binary images. (However, before this patch, we had to do this anyway if
   the binary mirrors were different from the chroot ones.)

 * We must add a little hack to the minimal hook to detect whether we are
   using local packages and not remove apt-utils (which creates the
   repository in lh_chroot_sources) if that is the case -- we cannot simply
   use "Install_package" inside lh_chroot_sources as we are not guaranteed
   to have working APT data because the minimal hook deliberately removes
   them!
---
 helpers/lh_chroot_linux-image    | 30 +-----------------------------
 helpers/lh_chroot_local-packages | 30 ++----------------------------
 helpers/lh_chroot_sources        | 38 +++++++++++++++++++++++++++++++++++++-
 hooks/minimal                    | 18 +++++++++++++++---
 4 files changed, 55 insertions(+), 61 deletions(-)

diff --git a/helpers/lh_chroot_linux-image b/helpers/lh_chroot_linux-image
index bc6de54..3d760f9 100755
--- a/helpers/lh_chroot_linux-image
+++ b/helpers/lh_chroot_linux-image
@@ -77,36 +77,8 @@ EOF
 			done
 		fi
 
-		if ! ls config/chroot_local-packages/${LH_INITRAMFS}*.deb > /dev/null 2>&1
-		then
-			PACKAGES="${PACKAGES} ${LH_INITRAMFS}"
-		fi
-
 		# Installing linux-image, modules and ${LH_INITRAMFS}
-		Apt install ${PACKAGES}
-
-		if ls config/chroot_local-packages/${LH_INITRAMFS}*.deb > /dev/null 2>&1
-		then
-			cp config/chroot_local-packages/${LH_INITRAMFS}*.deb chroot/root
-
-			# Installing package
-			Chroot "find /root -name *.deb" > chroot/root/initfs
-			Chroot "xargs --arg-file=/root/initfs dpkg -i" || true
-
-			# Install dependencies
-			case "${LH_APT}" in
-				aptitude)
-					Apt install ${LH_INITRAMFS}
-					;;
-				apt|apt-get)
-					Apt install -f
-					;;
-			esac
-
-			# Removing package files
-			rm -f chroot/root/${LH_INITRAMFS}*.deb
-			rm -f chroot/root/initfs
-		fi
+		Apt install ${PACKAGES} ${LH_INITRAMFS}
 
 		# Saving cache
 		Save_cache cache/packages_linux-image
diff --git a/helpers/lh_chroot_local-packages b/helpers/lh_chroot_local-packages
index 9c7ed36..15ce988 100755
--- a/helpers/lh_chroot_local-packages
+++ b/helpers/lh_chroot_local-packages
@@ -42,38 +42,12 @@ Check_lockfile .lock
 # Creating lock file
 Create_lockfile .lock
 
-if ls config/chroot_local-packages/*.deb > /dev/null 2>&1
+if ls chroot/root/local-packages/*.deb > /dev/null 2>&1
 then
 	# Restoring cache
 	Restore_cache cache/packages_local-packages
 
-	# Copying packages
-	if ls config/chroot_local-packages/*_"${LH_ARCHITECTURE}".deb > /dev/null 2>&1
-	then
-		cp config/chroot_local-packages/*_"${LH_ARCHITECTURE}".deb chroot/root
-	fi
-
-	if ls config/chroot_local-packages/*_all.deb > /dev/null 2>&1
-	then
-		cp config/chroot_local-packages/*_all.deb chroot/root
-	fi
-
-	# Installing packages
-        Chroot "find /root -name \\*.deb" > chroot/root/local-packages
-
-	if [ -s chroot/root/local-packages ]
-	then
-		Chroot "xargs --arg-file=/root/local-packages dpkg -i" || true
-	else
-		Echo_warning "Local packages must be named with suffix '_all.deb' or '_\$architecture.deb'."
-	fi
-
-	# Cleaning dependencies
-	Apt install -f
-
-	# Removing package files
-	rm -f chroot/root/*.deb
-	rm -f chroot/root/local-packages
+	Apt install $(gunzip < chroot/root/local-packages/Packages.gz | awk '/^Package: / { print $2 }')
 
 	# Saving cache
 	Save_cache cache/packages_local-packages
diff --git a/helpers/lh_chroot_sources b/helpers/lh_chroot_sources
index cf086cf..0dc3d76 100755
--- a/helpers/lh_chroot_sources
+++ b/helpers/lh_chroot_sources
@@ -77,6 +77,38 @@ case "${1}" in
 			done
 		fi
 
+		# Configure local package repository
+		if ls config/chroot_local-packages/*.deb > /dev/null 2>&1
+		then
+			rm -rf chroot/root/local-packages
+			mkdir -p chroot/root/local-packages
+
+			# Copy packages
+			if ls config/chroot_local-packages/*_"${LH_ARCHITECTURE}".deb > /dev/null 2>&1
+			then
+				cp config/chroot_local-packages/*_"${LH_ARCHITECTURE}".deb chroot/root/local-packages
+			fi
+
+			if ls config/chroot_local-packages/*_all.deb > /dev/null 2>&1
+			then
+				cp config/chroot_local-packages/*_all.deb chroot/root/local-packages
+			fi
+
+			if ls chroot/root/local-packages/*.deb > /dev/null 2>&1
+			then
+				# Generate Packages.gz
+				echo "cd /root/local-packages && apt-ftparchive packages . > Packages" | Chroot sh
+				gzip -9 chroot/root/local-packages/Packages
+
+				# Add to sources.list
+				echo "" >> chroot/etc/apt/sources.list
+				echo "# Local packages" >> chroot/etc/apt/sources.list
+				echo "deb file:/root/local-packages ./" >> chroot/etc/apt/sources.list
+			else
+				Echo_warning "Local packages must be named with suffix '_all.deb' or '_\$architecture.deb'."
+			fi
+		fi
+
 		# Update indices from cache
 		if [ "${LH_CACHE_INDICES}" = "enabled" ] && [ -d cache/indices_bootstrap ]
 		then
@@ -194,7 +226,8 @@ case "${1}" in
 		then
 			# Don't do anything if it's not required
 			if [ "${LH_MIRROR_CHROOT}" = "${LH_MIRROR_BINARY}" ] && \
-			[ "${LH_MIRROR_CHROOT_SECURITY}" = "${LH_MIRROR_BINARY_SECURITY}" ]
+			[ "${LH_MIRROR_CHROOT_SECURITY}" = "${LH_MIRROR_BINARY_SECURITY}" ] && \
+			[ ! -d chroot/root/local-packages ]
 			then
 				# Removing stage file
 				rm -f .stage/chroot_sources
@@ -257,6 +290,9 @@ case "${1}" in
 		rm -rf chroot/var/cache/apt
 		mkdir -p chroot/var/cache/apt/archives/partial
 
+		# Remove local package repository
+		rm -rf chroot/root/local-packages
+
 		# Removing stage file
 		rm -f .stage/chroot_sources
 		;;
diff --git a/hooks/minimal b/hooks/minimal
index ff02ddc..f081fcc 100755
--- a/hooks/minimal
+++ b/hooks/minimal
@@ -9,15 +9,27 @@
 
 set -e
 
-# Removing unused packages
-for PACKAGE in apt-utils aptitude man-db manpages info wget
-do
+Purge() {
+	PACKAGE="${1}"
+
 	if ! apt-get remove --purge --yes "${PACKAGE}"
 	then
 		echo "WARNING: ${PACKAGE} isn't installed"
 	fi
+}
+
+# Removing unused packages
+for PACKAGE in aptitude man-db manpages info wget
+do
+	Purge ${PACKAGE}
 done
 
+# Remove apt-utils if we do not require it for lh_chroot_sources
+if [ ! -e chroot/root/local-packages/Packages.gz ]
+then
+	Purge apt-utils
+fi
+
 apt-get autoremove --yes || true
 
 # Removing unused files
-- 
cgit v1.0