#!/bin/bash set -euo pipefail #set -x dir="/tmp/null" rm -rf "$dir" mkdir "$dir" cd "$dir" # Add all arguments as the initial core packages printf '%s\n' "$@" > keep # Packages required for a shell environment cat >>keep <disallow < Installing packages into chroot" >&2 set -x # Install requirements for this script (xargs and cmp) dnf install -y findutils diffutils # Install core packages to chroot rootfs="$(realpath rootfs)" mkdir -p "$rootfs" /dev/null echo "==> Building dependency tree" >&2 # Loop until we have the full dependency tree (no new packages found) touch old while ! cmp -s keep old do # 1. Get requirement names (not quite the same as package names) # 2. Filter out any install-time requirements # 3. Query which packages are being used to satisfy the requirements # 4. Keep just their package names # 5. Remove packages that are on the disallow list # 6. Store result as an allowlist new # Safely replace the keep list, appending the new names mv keep old cat old new > keep # Sort and deduplicate so cmp will eventually return true sort -u keep -o keep done # Determine all packages that need to be removed rpm -r "$rootfs" -qa | sed -r 's/^(.*)-.*-.*$/\1/' | sort -u > all # Set complement (all - keep) grep -vxF -f keep all > remove echo "==> $(wc -l remove | cut -d ' ' -f1) packages to erase:" >&2 cat remove echo "==> $(wc -l keep | cut -d ' ' -f1) packages to keep:" >&2 cat keep echo "" >&2 echo "==> Erasing packages" >&2 # Delete all packages that aren't needed for the core packages set -x /dev/null echo "" >&2 echo "==> Packages erased ok!" >&2