diff options
author | bill-auger <mr.j.spam.me@gmail.com> | 2023-12-14 12:32:47 -0500 |
---|---|---|
committer | bill-auger <mr.j.spam.me@gmail.com> | 2024-01-01 20:27:16 -0500 |
commit | a56b7ffad49581349ed242cc28004e3a330e2e0a (patch) | |
tree | f2e7787424b15ecbfef155e5247557648f4e9ec6 | |
parent | e0be637161d839acb0a09884db4a1a25e0665102 (diff) |
[chcleanup.in]: WIP - systemd host, create nonsystemd chroots
-rw-r--r-- | src/chroot-tools/chcleanup.in | 101 |
1 files changed, 70 insertions, 31 deletions
diff --git a/src/chroot-tools/chcleanup.in b/src/chroot-tools/chcleanup.in index 8e298aa..71f8633 100644 --- a/src/chroot-tools/chcleanup.in +++ b/src/chroot-tools/chcleanup.in @@ -61,6 +61,10 @@ DRYRUN=${DRYRUN:-false} if [[ ! -f /.arch-chroot ]] && ! ${DRYRUN}; then error "(chcleanup): Must be run inside of a chroot" exit 1 +elif [[ ! -f /etc/pacman.conf ]]; then + error "(chcleanup): Chroot is lacking /etc/pacman.conf." +elif [[ ! -f /etc/os-release ]]; then + error "(chcleanup): Chroot is lacking /etc/os-release." fi @@ -70,25 +74,50 @@ fi # This is intentionally hard-coded. source /etc/libretools.d/chroot.conf # sets $CHROOTEXTRAPKG +CHROOTPKG=( base base-devel ) +NONSYSTEMD_PKGS=(elogind libelogind) # WIP +SYSTEMD_PKGS=() # WIP +EXCLUDE_PKGS=( '.*-keyring' ) # may be regexes +CHROOTPKG+=() # normally empty - mandatory core package-set additions +WHITELIST_PKGS=() # normally empty - allowed packages, possibly insane/outdated +PHANTOM_PKGS=() # normally empty - fake dependencies, possibly insane -CHROOTPKG=( $( pacman -Sgq base-devel) ) -CHROOTPKG+=() # DEBUG: normally empty - mandatory core package-set additions -WHITELIST_PKGS=() # DEBUG: normally empty - allowed outdated packages, possibly insane -PHANTOM_PKGS=() # DEBUG: normally empty - fake dependencies, possibly insane -PHANTOM_PKGS=( ${PHANTOM_PKGS[*]/#/--assume-installed=} ) +readonly CARCH="$(source /etc/makepkg.conf ; printf '%s' "${CARCH}")" +readonly IS_NONSYSTEMD=$( grep -E '^VARIANT_ID="+[^-"]*-openrc"+$' /etc/os-release &> /dev/null ; echo $((! $?)) ; ) # WIP # If we're running makepkg if [[ -f ./PKGBUILD ]]; then + sed_rx="s/^\\s+(|make|check)depends(|_${CARCH}) = " + if [[ ! -f ./.SRCINFO || ./PKGBUILD -nt ./.SRCINFO ]]; then sudo -u "#$(stat -c %u -- ./PKGBUILD)" sh -c 'makepkg --printsrcinfo > .SRCINFO' + # non-systemd hack + if (( IS_NONSYSTEMD )); then + declare -A nonsystemd_pkgs + nonsystemd_pkgs[systemd]=elogind + nonsystemd_pkgs[systemd-libs]=libelogind + nonsystemd_pkgs[systemd-udev]=udev + nonsystemd_pkgs[systemd-libudev]=libudev + for systemd_pkg in ${!nonsystemd_pkgs[@]}; do + nonsystemd_pkg=${nonsystemd_pkgs[$systemd_pkg]} + sed -i -E "${sed_rx}${systemd_pkg}$/\1depends\2 = ${nonsystemd_pkg}/" .SRCINFO + done + fi fi - CARCH="$(source /etc/makepkg.conf; printf '%s' "$CARCH")" - mapfile -t DEPENDS < <(sed -nE -e "s/^\\s+(|make|check)depends(|_${CARCH}) = //p" -e '/^\s*pkgname/q' < .SRCINFO) + sed_rx+='//p ; /^\s*pkgname/q' + mapfile -t DEPENDS < <(sed --silent -E "${sed_rx}" < .SRCINFO) else DEPENDS=() fi -readonly CHROOTPKG WHITELIST_PKGS PHANTOM_PKGS DEPENDS +readonly CHROOTPKG=( $(repo=$((( IS_NONSYSTEMD )) && echo nonsystemd || echo libre) + sed "s|^base$|${repo}/&|" <<<${CHROOTPKG[*]} ) ) # WIP +readonly NONSYSTEMD_PKGS +readonly EXCLUDE_PKGS +readonly EXCLUDE_RX="^($(sed 's/ /|/g' <<<${EXCLUDE_PKGS[*]}))$" +readonly WHITELIST_PKGS+=( $( (( IS_NONSYSTEMD )) && echo ${NONSYSTEMD_PKGS[*]} || echo ${SYSTEMD_PKGS[*]} ) ) # WIP +readonly PHANTOM_PKGS=( ${PHANTOM_PKGS[*]/#/--assume-installed=} ) +readonly DEPENDS ## Main Entry ## @@ -98,8 +127,9 @@ msg "Cleaning chroot..." # Sync the local repo with pacman (a crude form of `pacman -Sy`) cp /repo/repo.db /var/lib/pacman/sync/repo.db -# Setup the temporary directory +# Setup a temporary directory and configure pacman for scratch DB TEMPDIR="$(mktemp --tmpdir -d "${0##*/}.XXXXXXXXXX")" +PacmanCmd=(pacman --dbpath="$TEMPDIR/db" --hookdir="$TEMPDIR/hooks") ERROR_PKGS_FILE="$TEMPDIR/err_pkgs.txt" WHITELIST_PKGS_FILE="$TEMPDIR/pkglist.txt" trap "rm -rf -- ${TEMPDIR@Q}" EXIT @@ -107,14 +137,14 @@ trap "rm -rf -- ${TEMPDIR@Q}" EXIT # Set up a scratch pacman DB mkdir -- "$TEMPDIR/db" "$TEMPDIR/db/local" "$TEMPDIR/hooks" cp -a -t "${TEMPDIR}/db" -- /var/lib/pacman/sync -{ echo /usr/share/libalpm/hooks; pacman-conf HookDir; } | while read -r dir; do - for hook in "$dir"/*.hook; do +for hooks_dir in /usr/share/libalpm/hooks "$(pacman-conf HookDir)"; do + [[ -d "$hooks_dir" ]] || continue + + for hook in "$hooks_dir"/*.hook; do ln -sfT -- /dev/null "$TEMPDIR/hooks/${hook##*/}" done done -# Do our best to preload the scratch DB with CHROOTPKG and -PacmanCmd=(pacman --dbpath="$TEMPDIR/db" --hookdir="$TEMPDIR/hooks") # We do our best to pre-load the scratch DB with CHROOTPKG and # CHROOTEXTRAPKG packages. This is purely an optimization step. # The safety of this optimization assumes that none of CHROOTPKG, @@ -137,20 +167,29 @@ done # Collect the minimal set of packages, which are necessary, # in order to build the PKGBUILD recipe -# (base+base-devel, user-packages, PKGBUILD dependencies, +# (base-devel, user-packages, PKGBUILD dependencies, # and the entire resolved dependency chain). # This is done by installing those into a temporary pacman DB; # then querying the DB for it's complete package list. +pacman_opts=(-S --dbonly --noscriptlet --needed --noconfirm) msg2 "Collecting the minimal set of packages needed ..." -if ! "${PacmanCmd[@]}" -S --dbonly --noscriptlet --needed --noconfirm ${PHANTOM_PKGS} \ - -- ${CHROOTPKG[*]} ${CHROOTEXTRAPKG[*]} ${DEPENDS[*]} \ - <&- >& "$ERROR_PKGS_FILE" -then - error "Could not create a full list of packages, exiting." - plain "Ensure that the chroot is up-to-date." - plain "Otherwise, this is likely caused by an unsatisfied dependency." - sed 's/^/ > /' < "$ERROR_PKGS_FILE" >&2 - exit 1 +if (( ${#CHROOTPKG[*]} )) +then for var in CHROOTPKG CHROOTEXTRAPKG DEPENDS + do declare -n pkgsref="$var" +# prose " * collecting '$var' packages: ${pkgsref[*]}" # TODO: column -t maybe better? + indent <<<" * collecting '$var' packages: ${pkgsref[*]}" + done + if ! "${PacmanCmd[@]}" ${pacman_opts[*]} ${PHANTOM_PKGS[*]} \ + -- ${CHROOTPKG[*]} ${CHROOTEXTRAPKG[*]} ${DEPENDS[*]} \ + <&- >& "$ERROR_PKGS_FILE" + then error "Could not create a complete list of packages - exiting." + plain "Ensure that the chroot is up-to-date." + plain "Otherwise, this is likely caused by an unsatisfied dependency." + sed 's/^/ > /' < "$ERROR_PKGS_FILE" >&2 + exit 1 + fi +else error "Could not create a list of packages - CHROOTPKG is empty - exiting." + exit 1 fi # Generate the list of packages which should be installed. @@ -169,8 +208,8 @@ fi # so it is best to require explicit initiation of a build in this insane configuration # (via the `libremakepkg` '-I' option), and to exit otherwise, warning the user to upgrade. # If the '-I' option was detected (!SANE), the stale packages are added to the whitelist. -"${PacmanCmd[@]}" -Qq > "$WHITELIST_PKGS_FILE" -echo "${WHITELIST_PKGS[@]}" >> "$WHITELIST_PKGS_FILE" +"${PacmanCmd[@]}" -Qq | grep -vE "${EXCLUDE_RX}" > "$WHITELIST_PKGS_FILE" +echo "${WHITELIST_PKGS[@]}" >> "$WHITELIST_PKGS_FILE" if (( ${#stale_pkgs[*]} > 0 )); then insane_msg_1="Some (%d) essential packages are out-of-sync with the database." insane_msg_2="Consider upgrading the chroot system before building packages." @@ -186,8 +225,8 @@ if (( ${#stale_pkgs[*]} > 0 )); then fi # Diff installed packages against a clean chroot then remove leftovers -packages=($(comm -23 <(pacman -Qq | sort -u) \ - <(sort -u "$WHITELIST_PKGS_FILE"))) +packages=( $(comm -23 <(pacman -Qq | sort -u) \ + <(sort -u "$WHITELIST_PKGS_FILE") ) ) if [[ ${#packages[@]} = 0 ]]; then msg2 "No packages to remove" else @@ -197,12 +236,12 @@ else echo "${packages[*]}" else # Only remove leftovers, -Rcs removes too much - pacman --noconfirm ${PHANTOM_PKGS} -R --nosave "${packages[@]}" + pacman --noconfirm ${PHANTOM_PKGS[*]} -R --nosave "${packages[@]}" fi fi -packages=($(comm -13 <(pacman -Qq | sort -u) \ - <(sort -u "$WHITELIST_PKGS_FILE"))) +packages=( $(comm -13 <(pacman -Qq | sort -u) \ + <(sort -u "$WHITELIST_PKGS_FILE") ) ) if [[ ${#packages[@]} = 0 ]]; then msg2 "No packages to add" else @@ -211,7 +250,7 @@ else if ${DRYRUN}; then echo "${packages[*]}" else - pacman --noconfirm ${PHANTOM_PKGS} -S "${packages[@]}" + pacman --noconfirm ${PHANTOM_PKGS[*]} -S "${packages[@]}" fi fi |