diff --git a/README.md b/README.md index 14fe30d..e32dca0 100644 --- a/README.md +++ b/README.md @@ -66,7 +66,7 @@ wget -qO- https://federated.sh | bash First it checks the machine. It stops if this isn't Arch, if you run it as root, if your user can't use sudo, or if there's no internet. It asks before going on if there's less than about 60 GB free, or if / isn't on btrfs (then there are no snapshots before updates, and only reinstalling Arch with btrfs changes that). If the login screen isn't GNOME's, or sound goes through PulseAudio, it offers to switch to GNOME's login screen and to PipeWire. -3) At the end, it offers to run `configure`, which connects to your Federated Core, sets up backups and signs you in to the AI agents (see "Configure" below). If the install brought in a newer kernel, it asks you to reboot first; run `configure` after the reboot. +3) At the end, it offers to run `configure`, which connects to your Federated Core, sets up backups and signs you in to the AI agents (see "Configure" below). If the install brought in a newer kernel, it asks you to reboot first, and offers to reboot now; run `configure` after the reboot. 4) If something fails, just run the command again. It skips what's already done. @@ -214,7 +214,7 @@ Each run's full output is saved in ~/.local/state/coredesktop/logs/, so you can If an install or update fails, it offers to send its log to the CoreDesktop team so we can find and fix the problem. Nothing is sent unless you say yes, and you can read exactly what would be sent first: your user name, host name, home folder, email addresses, IP addresses and Tailscale names are taken out. Along with the log go CoreDesktop's version, the kernel's, the names of the steps that failed and your sibling projects' versions. You get a report ID to mention if you contact us. To send a log later, run `report` (the newest log) or `report `; `report --view` shows what would be sent without sending anything. -It is a good idea to reboot after an update especially if you see you have a new Linux kernel installed (and that happens frequently enough for Arch). +If the update installed a newer kernel, or upgraded firmware, CPU microcode, the NVIDIA driver, systemd or glibc, it says so at the end and asks whether to reboot now. The answer is no unless you type y. Note: sometimes, for whatever reason, the update barfs. Take a look at [https://archlinux.org](https://archlinux.org) in the "Latest News" section for fixes. diff --git a/system/finish-install.sh b/system/finish-install.sh index c304e3b..7aad516 100644 --- a/system/finish-install.sh +++ b/system/finish-install.sh @@ -1,7 +1,7 @@ #!/bin/bash # # Purpose : The end of install.sh: say the install worked, say whether to -# reboot, and offer configure if something is still to do. refresh +# reboot and offer to, 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. @@ -19,6 +19,12 @@ echo -e "\e[32mCoreDesktop is installed.\e[0m" if [ ! -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 + if { : < "$TTY"; } 2> /dev/null && read -rp "Reboot now? [y/N] " answer < "$TTY" \ + && [[ "$answer" == [yY]* ]]; then + echo "Rebooting..." + systemctl reboot || echo "Couldn't reboot. Restart from the system menu instead." + fi exit 0 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." diff --git a/tests/finish-install.bats b/tests/finish-install.bats index ccc5ad5..ce6f913 100644 --- a/tests/finish-install.bats +++ b/tests/finish-install.bats @@ -69,3 +69,32 @@ answer() { [[ "$output" == *"CoreDesktop is installed."* ]] called configure } + +@test "finish-install reboots after a new kernel when told yes" { + KERNEL=0.0.0-not-installed + stub systemctl + answer y + run bash "$REPO/system/finish-install.sh" + [ "$status" -eq 0 ] + [[ "$output" == *"Reboot, then run: configure"*"Rebooting..."* ]] + called systemctl reboot +} + +@test "finish-install doesn't reboot unless told yes" { + KERNEL=0.0.0-not-installed + stub systemctl + answer "" + run bash "$REPO/system/finish-install.sh" + [ "$status" -eq 0 ] + not_called systemctl +} + +@test "finish-install doesn't ask about a reboot when the kernel is up to date" { + needs_modules + stub systemctl + answer y + run bash "$REPO/system/finish-install.sh" + [ "$status" -eq 0 ] + [[ "$output" != *"Reboot now?"* ]] + not_called systemctl +} diff --git a/tests/update.bats b/tests/update.bats index 6e1ee6f..c1db0e4 100644 --- a/tests/update.bats +++ b/tests/update.bats @@ -237,3 +237,84 @@ ran() { [[ "$output" == *"Done."* ]] ran a } + +# needs_reboot NAME: a hook that says this update needs a reboot, as +# updates.d/99-reboot-check.sh does +needs_reboot() { + hook "$1" 'mkdir -p ~/.local/state/coredesktop; echo "a newer kernel was installed" > ~/.local/state/coredesktop/reboot-needed' +} + +# answer TEXT: what's typed at update's questions +answer() { + export COREDESKTOP_TTY="$BATS_TEST_TMPDIR/answer" + echo "$1" > "$COREDESKTOP_TTY" +} + +@test "update reboots when it needs to and is told yes" { + stub systemctl + needs_reboot 99-reboot-check + answer y + + run "$CD/update" + [ "$status" -eq 0 ] + [[ "$output" == *"Done."*"Rebooting..."* ]] + called systemctl reboot +} + +@test "update doesn't reboot unless told yes" { + stub systemctl + needs_reboot 99-reboot-check + answer "" + + run "$CD/update" + [ "$status" -eq 0 ] + not_called systemctl +} + +@test "update doesn't ask about a reboot without a terminal" { + stub systemctl + needs_reboot 99-reboot-check + export COREDESKTOP_TTY="$BATS_TEST_TMPDIR/no-tty/tty" + + run "$CD/update" + [ "$status" -eq 0 ] + not_called systemctl +} + +@test "update doesn't offer a reboot an earlier update needed" { + stub systemctl + hook 10-a 'true' + mkdir -p "$HOME/.local/state/coredesktop" + touch "$HOME/.local/state/coredesktop/reboot-needed" + answer y + + run "$CD/update" + [ "$status" -eq 0 ] + not_called systemctl +} + +@test "update offers a reboot after offering to send the log of a failed step" { + printf '#!/bin/bash\necho "Send this log?"\n' > "$CD/report" + chmod +x "$CD/report" + stub systemctl + hook 10-a 'return 1' + needs_reboot 99-reboot-check + answer y + + run "$CD/update" + [ "$status" -eq 1 ] + [[ "$output" == *"Send this log?"*"Rebooting..."* ]] + called systemctl reboot +} + +@test "under script, the reboot waits for update's log to be cleaned up" { + stub systemctl + needs_reboot 99-reboot-check + answer y + export UPDATE_IN_SCRIPT=1 UPDATE_LOG="$BATS_TEST_TMPDIR/update.log" + + run "$CD/update" + [ "$status" -eq 0 ] + not_called systemctl + [ -e "$UPDATE_LOG.reboot" ] +} diff --git a/update b/update index 422a2e8..50d86dd 100755 --- a/update +++ b/update @@ -44,6 +44,31 @@ clean_log() { s/\x1b[()][0-9A-Za-z]//g; s/\x1b[=>]//g; s/\r+$//; s/.*\r//' "$1" } +# Set by updates.d/99-reboot-check.sh when this update needs a reboot +REBOOT_NEEDED="$HOME/.local/state/coredesktop/reboot-needed" + +# The same as the system menu's restart: an active desktop session needs no +# password. (sudo's cached password belongs to script(1)'s terminal, see below.) +reboot_now() { + echo "Rebooting..." + systemctl reboot || echo "Couldn't reboot. Restart from the system menu instead." +} + +# If this update needs a reboot, ask on the terminal whether to reboot now. Under +# script(1), leave it to the copy outside, so the log is cleaned up first. +offer_reboot() { + local tty="${COREDESKTOP_TTY:-/dev/tty}" answer + [ -e "$REBOOT_NEEDED" ] || return 0 + { : < "$tty"; } 2> /dev/null || return 0 + read -rp "Reboot now? [y/N] " answer < "$tty" || return 0 + [[ "$answer" == [yY]* ]] || return 0 + if [ -n "$UPDATE_IN_SCRIPT" ]; then + touch "$UPDATE_LOG.reboot" + else + reboot_now + fi +} + # Log each update to ~/.local/state/coredesktop/logs (95-cleanup.sh deletes # old ones). In a terminal, run under script(1), so everything still sees a # terminal and colors, menus and prompts work as before; otherwise use tee. @@ -52,9 +77,13 @@ if [[ -z "$UPDATE_LOG" ]] && mkdir -p -m 700 "$HOME/.local/state/coredesktop/log UPDATE_LOG="$HOME/.local/state/coredesktop/logs/update-$(date +%Y-%m-%d-%H%M%S).log" export UPDATE_LOG if [ -t 0 ] && [ -t 1 ] && command -v script > /dev/null; then - SHELL=/bin/bash script -q -e -f -c "$(printf '%q ' "$0" "$@")" "$UPDATE_LOG" + SHELL=/bin/bash UPDATE_IN_SCRIPT=1 script -q -e -f -c "$(printf '%q ' "$0" "$@")" "$UPDATE_LOG" status=$? clean_log "$UPDATE_LOG" + if [ -e "$UPDATE_LOG.reboot" ]; then + rm -f "$UPDATE_LOG.reboot" + reboot_now + fi exit "$status" fi exec > >(tee -a "$UPDATE_LOG") 2>&1 @@ -118,6 +147,7 @@ while kill -0 $$ 2>/dev/null; do sudo -n -v; sleep 60; done > /dev/null 2>&1 & SUDO_KEEPALIVE_PID=$! trap 'kill "$SUDO_KEEPALIVE_PID" 2>/dev/null' EXIT +rm -f "$REBOOT_NEEDED" FAILED=() [[ -n "$UPDATE_PULL_FAILED" ]] && FAILED+=("git pull") @@ -150,9 +180,11 @@ if (( ${#FAILED[@]} > 0 )); then # Ask, on the terminal if there is one, whether to send it to us [ -x ~/.local/bin/report ] && ~/.local/bin/report --offer "$UPDATE_LOG" "${FAILED[@]}" fi + offer_reboot exit 1 fi echo "Done." +offer_reboot exit 0 } diff --git a/updates.d/99-reboot-check.sh b/updates.d/99-reboot-check.sh index c5872bb..14c4ef1 100755 --- a/updates.d/99-reboot-check.sh +++ b/updates.d/99-reboot-check.sh @@ -2,6 +2,9 @@ source "$HOME/.local/share/coredesktop/utils.sh" +# Tells update to offer a reboot at the end +REBOOT_NEEDED="$HOME/.local/state/coredesktop/reboot-needed" + # Packages whose upgrade only fully takes effect after a reboot: the kernel and # firmware, the NVIDIA driver (the loaded module must match the installed # one), CPU microcode, and the core libraries every running program uses @@ -31,4 +34,6 @@ fi if (( ${#reasons[@]} > 0 )); then reason="$(printf '%s; ' "${reasons[@]}")" echo -e "\e[33mPlease reboot when convenient to finish this update (${reason%; }).\e[0m" + # update offers to reboot once it's done; it removes this file before its hooks run + mkdir -p "$(dirname "$REBOOT_NEEDED")" && echo "${reason%; }" > "$REBOOT_NEEDED" fi