Build a failed initramfs again, and don't offer a reboot until it builds

When mkinitcpio fails during an update or install, run it once more. If
that fails too, say why (a full /boot, a hook or module that isn't
installed) and don't offer a reboot. The install now logs its upgrade and
checks the build before finish-install offers one, and nvidia.sh's rebuild
no longer stops the install. A failed build is carried into the next
update's log, so it's rebuilt and checked until it builds.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-18 07:34:51 -07:00
co-authored by Claude Opus 5.5
parent 16e5a9c79d
commit d456e31192
8 changed files with 230 additions and 23 deletions
+84 -8
View File
@@ -190,21 +190,97 @@ run_migrations() {
# Output of the last update's pacman -Su, written by updates.d/10-pacman.sh
PACMAN_UPDATE_LOG="$HOME/.local/state/coredesktop/pacman-update.log"
# True if the last update rebuilt the initramfs and mkinitcpio reported a
# failure. Rebooting then can leave the machine unbootable.
initramfs_build_failed() {
# Lines that start a new initramfs build in the log: pacman's hook, a rebuild
# by rebuild_initramfs, or a failure carried over by new_pacman_log
INITRAMFS_BUILD_START='Updating linux initcpios|==> CoreDesktop:'
# The log of the last initramfs build, if the log has one
initramfs_build_log() {
[ -f "$PACMAN_UPDATE_LOG" ] || return 1
grep -q "Updating linux initcpios" "$PACMAN_UPDATE_LOG" || return 1
grep -q "==> ERROR" "$PACMAN_UPDATE_LOG" && return 0
awk -v start="$INITRAMFS_BUILD_START" '$0 ~ start { s = ""; found = 1 } found { s = s $0 "\n" }
END { printf "%s", s; exit !found }' "$PACMAN_UPDATE_LOG"
}
# True if the last initramfs build (an update's, or rebuild_initramfs') failed.
# Rebooting then can leave the machine unbootable.
initramfs_build_failed() {
local build built succeeded
build="$(initramfs_build_log)" || return 1
grep -q "==> ERROR" <<< "$build" && return 0
# Every image built must report success. Older mkinitcpio says "==> Image
# generation successful", newer says "==> Initcpio image generation successful".
local built succeeded
built="$(grep -c "==> Building image from preset" "$PACMAN_UPDATE_LOG")"
succeeded="$(grep -cE "==> (Initcpio i|I)mage generation successful" "$PACMAN_UPDATE_LOG")"
built="$(grep -c "==> Building image from preset" <<< "$build")"
succeeded="$(grep -cE "==> (Initcpio i|I)mage generation successful" <<< "$build")"
(( succeeded == 0 || succeeded < built ))
}
# Empty the pacman log for a new upgrade. A failed build in the old one is
# carried over, so it's still rebuilt and checked if this upgrade builds none.
new_pacman_log() {
local failed=""
initramfs_build_failed && failed=1
mkdir -p "$(dirname "$PACMAN_UPDATE_LOG")"
: > "$PACMAN_UPDATE_LOG"
if [ -n "$failed" ]; then
echo "==> CoreDesktop: ==> ERROR: the initramfs failed to build last time" >> "$PACMAN_UPDATE_LOG"
fi
}
# Build every initramfs again (mkinitcpio -P), adding the output to the log for
# initramfs_build_failed
rebuild_initramfs() {
mkdir -p "$(dirname "$PACMAN_UPDATE_LOG")"
echo "==> CoreDesktop: rebuilding the initramfs (mkinitcpio -P)" | tee -a "$PACMAN_UPDATE_LOG"
sudo mkinitcpio -P 2>&1 | tee -a "$PACMAN_UPDATE_LOG"
return "${PIPESTATUS[0]}"
}
# Free space on /boot, in MB
boot_free_mb() {
df --output=avail -BM /boot 2> /dev/null | tail -n 1 | tr -dc '0-9'
}
# Say why the last build failed, as far as the log and /boot tell
explain_initramfs_failure() {
local build free hook module
build="$(initramfs_build_log)"
echo "What went wrong:"
grep -F "==> ERROR" <<< "$build" | grep -vF "CoreDesktop:" | tr -d '\r' | sort -u | sed 's/^/ /'
free="$(boot_free_mb)"
if grep -qi "no space left on device" <<< "$build" || { [ -n "$free" ] && (( free < 64 )); }; then
echo "/boot is nearly full (${free:-?} MB free), so the new images don't fit. See what's there with"
echo " du -sh /boot/*"
echo "and remove what you don't need, e.g. images for kernels that are no longer installed."
fi
while read -r hook; do
[ -n "$hook" ] || continue
echo "HOOKS lists '$hook', but no installed package provides it. Install the package that does, or"
echo "remove it from HOOKS in /etc/mkinitcpio.conf (or the file in /etc/mkinitcpio.conf.d that adds it)."
done < <(grep -oP "Hook '\K[^']+(?=' cannot be found)" <<< "$build" | sort -u)
while read -r module; do
[ -n "$module" ] || continue
echo "MODULES lists '$module', which isn't installed for this kernel. Install it, or remove it from"
echo "MODULES in /etc/mkinitcpio.conf (or the file in /etc/mkinitcpio.conf.d that adds it)."
done < <(grep -oP "module not found: '?\K[^' ]+" <<< "$build" | sort -u)
echo "Then run: sudo mkinitcpio -P, and update to check it. The full log is $PACMAN_UPDATE_LOG."
}
# If the last initramfs build failed, build it once more; if that fails too,
# say what's wrong. False while it's still broken.
check_initramfs() {
initramfs_build_failed || return 0
echo -e "\e[33mThe initramfs failed to build. Trying again...\e[0m"
rebuild_initramfs
if ! initramfs_build_failed; then
echo -e "\e[32mThe initramfs built this time.\e[0m"
return 0
fi
echo -e "\e[31mThe initramfs still doesn't build. Don't reboot until it's fixed: the computer might not start.\e[0m"
explain_initramfs_failure
return 1
}
# Where unowned_conflicts' files are moved, under their full paths, so they
# can be put back. Outside the directories they came from: a copy left in
# /etc/sddm.conf.d or /usr/lib/systemd/system-sleep would still be live.