diff --git a/README.md b/README.md index 5d80c8e..60ead31 100644 --- a/README.md +++ b/README.md @@ -219,7 +219,7 @@ The script pulls the latest CoreDesktop, then runs each step in ~/.local/share/c - switches to up-to-date mirrors first if yours is more than a day behind Arch, as it would otherwise miss new packages without saying so - updates the Arch keyring, then Arch and GNOME, then AUR packages, Flatpaks, the languages you installed with mise, the pipx apps CoreDesktop installs, GNOME extensions and firmware - if the Arch upgrade stops because files no package owns are where a package now installs its own (left by `pip`, `make install` or an old installer), lists them and asks whether to move them to /var/lib/coredesktop/replaced and try again. If the second try fails too, it puts them back. If two packages conflict and pacman needs you to choose which to keep, it runs pacman again so you can answer -- warns you not to reboot if the initramfs failed to rebuild +- builds the initramfs again if it failed to build, and if that fails too, says why (a full /boot, a hook or module that isn't installed) and doesn't offer a reboot until it builds. The install does the same before it offers one. - runs any one-time migrations, the way existing machines pick up new CoreDesktop setup - lists packages that were installed only because something else needed them, and are no longer needed by anything, and asks before removing them. Packages CoreDesktop installs are never on this list. To keep one you use directly off the list, run `sudo pacman -D --asexplicit ` diff --git a/install.sh b/install.sh index f543f82..d992616 100755 --- a/install.sh +++ b/install.sh @@ -146,7 +146,12 @@ ensure_federated_repo # Bring the system fully up to date before installing anything new echo "Updating system..." sudo pacman -Sy --needed --noconfirm archlinux-keyring -sudo pacman -Su --noconfirm +# Logged for check_initramfs, before finish-install offers a reboot. Not +# through script(1): this file is usually piped into bash, and script would +# read the rest of it from stdin. +new_pacman_log +sudo pacman -Su --noconfirm < /dev/null 2>&1 | tee -a "$PACMAN_UPDATE_LOG" +[ "${PIPESTATUS[0]}" -eq 0 ] || exit 1 cd ~/ @@ -319,5 +324,9 @@ fi echo "Running migrations..." run_migrations +# The initramfs must build before finish-install offers a reboot. Its +# warnings say what to do if it doesn't. +check_initramfs || true + # Say the install worked, whether to reboot, and offer configure bash ~/.local/share/coredesktop/system/finish-install.sh diff --git a/system/finish-install.sh b/system/finish-install.sh index 7aad516..6a671be 100644 --- a/system/finish-install.sh +++ b/system/finish-install.sh @@ -1,13 +1,15 @@ #!/bin/bash # # Purpose : The end of install.sh: say the install worked, say whether to -# reboot and offer to, and offer configure if something is still to do. refresh +# reboot and offer to (not while the initramfs doesn't build), +# and offer configure if something is still to do. refresh # runs install.sh too, so it may all be done already; the install # still ends by saying it worked, so it can't be taken for a failure. # install.sh is usually piped into bash, so ask on the terminal TTY="${COREDESKTOP_TTY:-/dev/tty}" CONFIGURE="$HOME/.local/bin/configure" +source "$(dirname "$0")/../utils.sh" CONFIGURE_HINT="configure connects to your Federated Core, sets up backups and signs you in to the AI agents." echo @@ -15,8 +17,18 @@ echo -e "\e[32mCoreDesktop is installed.\e[0m" # If the running kernel's modules are gone, a newer kernel was installed during # this run. configure waits for the reboot: until then, modules it needs, such -# as Tailscale's tun and davfs2's fuse, can't be loaded. -if [ ! -d "/usr/lib/modules/$(uname -r)" ]; then +# as Tailscale's tun and davfs2's fuse, can't be loaded. The reboot waits for +# the initramfs to build. +if initramfs_build_failed; then + # check_initramfs (install.sh) has said why; offering a reboot now could + # leave the computer unable to start + echo -e "\e[31mDon't reboot yet: the initramfs doesn't build (see above, or $PACMAN_UPDATE_LOG).\e[0m" + if [ ! -d "/usr/lib/modules/$(uname -r)" ]; then + echo "Once it builds (sudo mkinitcpio -P), reboot, then run: configure" + echo "$CONFIGURE_HINT" + exit 0 + fi +elif [ ! -d "/usr/lib/modules/$(uname -r)" ]; then echo "A newer kernel has been installed. Reboot, then run: configure" echo "$CONFIGURE_HINT" # The same as the system menu's restart: an active desktop session needs no password @@ -26,8 +38,9 @@ if [ ! -d "/usr/lib/modules/$(uname -r)" ]; then systemctl reboot || echo "Couldn't reboot. Restart from the system menu instead." fi exit 0 +else + echo "Kernel is up to date, no reboot needed. You may want to consider a reboot if other significant software was updated or installed." fi -echo "Kernel is up to date, no reboot needed. You may want to consider a reboot if other significant software was updated or installed." if "$CONFIGURE" --list | grep -q '^to do'; then echo diff --git a/system/nvidia.sh b/system/nvidia.sh index 44edb77..9a60e89 100644 --- a/system/nvidia.sh +++ b/system/nvidia.sh @@ -58,10 +58,11 @@ install_packages "${PACKAGES[@]}" sudo install -d -m 755 /etc/mkinitcpio.conf.d echo 'MODULES+=(nvidia nvidia_modeset nvidia_uvm nvidia_drm)' \ | sudo tee /etc/mkinitcpio.conf.d/nvidia.conf > /dev/null -sudo mkinitcpio -P +# Logged, so install.sh's check_initramfs tries again and explains if it fails +rebuild_initramfs || true # GDM only offers Wayland on NVIDIA when these are enabled, since without them # the GPU loses its memory contents across suspend sudo systemctl enable nvidia-suspend.service nvidia-hibernate.service nvidia-resume.service -echo "NVIDIA driver installed. Reboot to start using it." +initramfs_build_failed || echo "NVIDIA driver installed. Reboot to start using it." diff --git a/tests/finish-install.bats b/tests/finish-install.bats index ce6f913..9c0eddf 100644 --- a/tests/finish-install.bats +++ b/tests/finish-install.bats @@ -98,3 +98,29 @@ answer() { [[ "$output" != *"Reboot now?"* ]] not_called systemctl } + +@test "finish-install doesn't offer a reboot while the initramfs doesn't build" { + KERNEL=0.0.0-not-installed + mkdir -p "$HOME/.local/state/coredesktop" + printf '%s\n' "(3/4) Updating linux initcpios..." "==> ERROR: module not found: 'nvidia'" \ + > "$HOME/.local/state/coredesktop/pacman-update.log" + stub systemctl + answer y + run bash "$REPO/system/finish-install.sh" + [ "$status" -eq 0 ] + [[ "$output" == *"CoreDesktop is installed."*"Don't reboot yet"*"Once it builds"* ]] + [[ "$output" != *"Reboot now?"* ]] + not_called systemctl +} + +@test "finish-install still offers configure while the initramfs doesn't build, if the kernel is current" { + needs_modules + ITEMS="to do core Federated Core" + mkdir -p "$HOME/.local/state/coredesktop" + printf '%s\n' "(3/4) Updating linux initcpios..." "==> ERROR: module not found: 'nvidia'" \ + > "$HOME/.local/state/coredesktop/pacman-update.log" + run bash "$REPO/system/finish-install.sh" + [ "$status" -eq 0 ] + [[ "$output" == *"Don't reboot yet"*"configure connects"* ]] + [[ "$output" != *"no reboot needed"* ]] +} diff --git a/tests/utils.bats b/tests/utils.bats index e302663..9373822 100644 --- a/tests/utils.bats +++ b/tests/utils.bats @@ -275,6 +275,91 @@ pacman_log() { [ "$status" -eq 0 ] } +@test "initramfs_build_failed reads only the last build: a rebuild that worked clears it" { + pacman_log "(3/4) Updating linux initcpios..." \ + "==> ERROR: Hook 'sd-plymouth' cannot be found" \ + "==> CoreDesktop: rebuilding the initramfs (mkinitcpio -P)" \ + "==> Building image from preset: /etc/mkinitcpio.d/linux.preset: 'default'" \ + "==> Image generation successful" + run initramfs_build_failed + [ "$status" -ne 0 ] +} + +@test "initramfs_build_failed is true when the rebuild failed too" { + pacman_log "(3/4) Updating linux initcpios..." \ + "==> Image generation successful" \ + "==> CoreDesktop: rebuilding the initramfs (mkinitcpio -P)" \ + "==> ERROR: module not found: 'nvidia'" + run initramfs_build_failed + [ "$status" -eq 0 ] +} + +@test "new_pacman_log carries a failed build over, and only a failed one" { + pacman_log "(3/4) Updating linux initcpios..." "==> ERROR: module not found: 'nvidia'" + new_pacman_log + initramfs_build_failed + [ "$(wc -l < "$PACMAN_UPDATE_LOG")" -eq 1 ] + # Carried over again by the next update that builds nothing + new_pacman_log + initramfs_build_failed + + pacman_log "(3/4) Updating linux initcpios..." \ + "==> Building image from preset: /etc/mkinitcpio.d/linux.preset: 'default'" \ + "==> Image generation successful" + new_pacman_log + [ ! -s "$PACMAN_UPDATE_LOG" ] +} + +# mkinitcpio -P through a fake sudo: it builds unless $MKINITCPIO_FAILS is set +fake_mkinitcpio() { + stub sudo 'echo "==> Building image from preset: /etc/mkinitcpio.d/linux.preset: '"'"'default'"'"'" +if [ -n "$MKINITCPIO_FAILS" ]; then echo "$MKINITCPIO_FAILS"; exit 1; fi +echo "==> Image generation successful"' + stub df 'printf "Avail\n%sM\n" "${BOOT_FREE:-500}"' +} + +@test "check_initramfs does nothing when the build worked" { + pacman_log "(3/4) Updating linux initcpios..." \ + "==> Building image from preset: /etc/mkinitcpio.d/linux.preset: 'default'" \ + "==> Image generation successful" + fake_mkinitcpio + run check_initramfs + [ "$status" -eq 0 ] + not_called sudo +} + +@test "check_initramfs builds it again, and is fine when that works" { + pacman_log "(3/4) Updating linux initcpios..." "==> ERROR: module not found: 'nvidia'" + fake_mkinitcpio + run check_initramfs + [ "$status" -eq 0 ] + [[ "$output" == *"Trying again"*"built this time"* ]] + called sudo mkinitcpio -P + refute initramfs_build_failed +} + +@test "check_initramfs names a missing hook when building again fails too" { + pacman_log "(3/4) Updating linux initcpios..." "==> ERROR: Hook 'sd-plymouth' cannot be found" + fake_mkinitcpio + export MKINITCPIO_FAILS="==> ERROR: Hook 'sd-plymouth' cannot be found" + run check_initramfs + [ "$status" -eq 1 ] + [[ "$output" == *"Don't reboot until it's fixed"* ]] + [[ "$output" == *"HOOKS lists 'sd-plymouth'"* ]] + [[ "$output" != *"/boot is nearly full"* ]] + initramfs_build_failed +} + +@test "explain_initramfs_failure says when /boot is full, and names a missing module" { + pacman_log "(3/4) Updating linux initcpios..." \ + "==> ERROR: module not found: 'nvidia'" \ + "cp: error writing '/boot/initramfs-linux.img': No space left on device" + fake_mkinitcpio + BOOT_FREE=3 run explain_initramfs_failure + [[ "$output" == *"/boot is nearly full (3 MB free)"* ]] + [[ "$output" == *"MODULES lists 'nvidia'"* ]] +} + # --- mirrors ---------------------------------------------------------------- setup_mirrors() { diff --git a/updates.d/10-pacman.sh b/updates.d/10-pacman.sh index 5d57440..e76361c 100644 --- a/updates.d/10-pacman.sh +++ b/updates.d/10-pacman.sh @@ -32,8 +32,7 @@ sudo pacman -Sy --needed --noconfirm archlinux-keyring || STATUS=1 # script(1) keeps pacman's progress bars while logging its output for the # initramfs check echo "Updating Arch..." -mkdir -p "$(dirname "$PACMAN_UPDATE_LOG")" -: > "$PACMAN_UPDATE_LOG" +new_pacman_log # Run pacman -Su with the options given, adding its output to the log upgrade() { @@ -90,11 +89,6 @@ retry_upgrade() { upgrade --noconfirm || retry_upgrade || STATUS=1 -if initramfs_build_failed; then - echo -e "\e[31mRebuilding the initramfs failed. Don't reboot until it's fixed - see the output above or $PACMAN_UPDATE_LOG.\e[0m" - STATUS=1 -fi - echo "Checking for new packages..." install_packages "${SYSTEM_UTILS[@]}" || STATUS=1 install_packages "${DEV_TOOLS[@]}" || STATUS=1 @@ -103,4 +97,7 @@ install_packages "${DESKTOP[@]}" || STATUS=1 install_packages "${MEDIA[@]}" || STATUS=1 install_packages "${FONTS[@]}" || STATUS=1 +# After the packages above, in case the build needed one of them +check_initramfs || STATUS=1 + exit $STATUS diff --git a/utils.sh b/utils.sh index 0586f5f..6c6e3ca 100755 --- a/utils.sh +++ b/utils.sh @@ -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.