diff --git a/bin/omarchy-update-requires-free-space b/bin/omarchy-update-requires-free-space index 07523eb2727c..0764412b8f59 100755 --- a/bin/omarchy-update-requires-free-space +++ b/bin/omarchy-update-requires-free-space @@ -12,6 +12,63 @@ elif read -r available_bytes < <(df --output=avail --block-size=1 / 2>/dev/null (( available_bytes < 10 * 1024 * 1024 * 1024 )); then echo "You need at least 10 GiB free to safely update Omarchy." exit 1 +fi + +# A UKI + Limine boot squeezes the ESP, not root: a kernel update writes a +# fresh UKI while limine-snapper-sync still holds its retained copies, and an +# ESP that fills mid-update leaves the machine unbootable. Root's 10 GiB check +# cannot see that when the ESP is its own small filesystem, so the ask is +# sized against the largest UKI already on disk, with one copy per installed +# kernel plus one for the update and history churn (at least two copies). +# See omacom/omarchy#13355. +esp=$(sed -n 's/^ESP_PATH=["'\'']\{0,1\}\([^"'\'']*\).*/\1/p' /etc/default/limine 2>/dev/null | tail -n 1) +esp=${esp:-/boot} + +# One filesystem check is enough: an ESP living on root is already covered by +# the 10 GiB check above. +if [[ $(findmnt -nro SOURCE -T "$esp" 2>/dev/null) == "$(findmnt -nro SOURCE -T / 2>/dev/null)" ]]; then + exit 0 +fi + +# No UKIs (different bootloader, or a wiped ESP) means nothing to size against. +if uki_sizes=$(find "$esp" -xdev -type f -name '*.efi' -printf '%s\n' 2>/dev/null); then + largest_uki_bytes=$(sort -nr <<< "$uki_sizes" | head -n 1) else + if [[ -d $esp ]]; then + echo "Could not scan UKIs in $esp; permissions may prevent verifying UKI headroom. Check its free space before updating." >&2 + fi + exit 0 +fi +if ! [[ $largest_uki_bytes =~ ^[0-9]+$ ]]; then exit 0 fi + +required_copies=2 +if kernel_packages=$(pacman -Qq 2>/dev/null); then + kernel_count=0 + while read -r package; do + case "$package" in + *-headers) ;; + linux|linux-lts|linux-zen|linux-ptl|linux-t2|linux-omarchy|linux-omarchy-*) + kernel_count=$(( kernel_count + 1 )) ;; + esac + done <<< "$kernel_packages" + if (( kernel_count > 0 )); then + required_copies=$(( kernel_count + 1 )) + fi +fi +required_bytes=$(( largest_uki_bytes * required_copies )) + +if ! read -r esp_available_bytes < <(df --output=avail --block-size=1 "$esp" 2>/dev/null | tail -n 1) || + ! [[ $esp_available_bytes =~ ^[0-9]+$ ]]; then + # Unmeasurable ESP: warn rather than block the update on a bad df reading. + echo "Could not measure free space in $esp; check it has $(numfmt --to=iec -- "$required_bytes") of UKI headroom." >&2 + exit 0 +fi + +if (( esp_available_bytes >= required_bytes )); then + exit 0 +fi + +echo "You need at least $(numfmt --to=iec -- "$required_bytes") free in $esp to safely update Omarchy." +exit 1 diff --git a/test/shell.d/update-disk-space-test.sh b/test/shell.d/update-disk-space-test.sh index e40401ebfeb2..61a051c6fcdb 100644 --- a/test/shell.d/update-disk-space-test.sh +++ b/test/shell.d/update-disk-space-test.sh @@ -143,3 +143,152 @@ output=$(TEST_DF_INVALID=1 run_update -y) [[ -z $output ]] || fail "failed disk-space detection remains silent" [[ -f $snapshot_marker ]] || fail "failed disk-space detection does not block the update" pass "failed disk-space detection silently continues" + +# The ESP guard sizes its ask against the largest UKI already on disk, so it +# needs a fake ESP tree, an arg-aware df, and a sed that points ESP_PATH at +# the fake tree without touching the real /etc/default/limine. +esp_tree="$test_tmp/esp" +mkdir -p "$esp_tree/EFI/Linux" +truncate -s 300M "$esp_tree/EFI/Linux/omarchy_linux.efi" + +real_sed=$(command -v sed) + +write_stub sed ' +if [[ ${1:-} == "-n" && ${3:-} == "/etc/default/limine" ]]; then + printf "%s\n" "$TEST_ESP_PATH" +else + exec "$REAL_SED" "$@" +fi' +export REAL_SED="$real_sed" + +write_stub findmnt ' +[[ $# == 4 && $1 == "-nro" && $2 == "SOURCE" && $3 == "-T" ]] || exit 1 +if [[ $4 == "$TEST_ESP_PATH" && ${TEST_ESP_ON_ROOT:-0} == "0" ]]; then + echo /dev/esp +else + echo /dev/root +fi' + +write_stub df ' +last="${*: -1}" +if [[ $last == "/" ]]; then + if (( TEST_DF_INVALID )); then + printf "Avail\nunknown\n" + else + printf "Avail\n%s\n" "$TEST_AVAILABLE_BYTES" + fi +else + printf "Avail\n%s\n" "$TEST_ESP_AVAILABLE_BYTES" +fi' + +write_stub pacman ' +[[ $* == "-Qq" ]] || exit 1 +printf "%s\n" "${TEST_KERNEL_PACKAGES-linux}" +exit "${TEST_PACMAN_STATUS:-0}"' + +real_find=$(command -v find) +export REAL_FIND="$real_find" +write_stub find ' +if [[ ${TEST_FIND_DENIED:-0} == "1" ]]; then + printf "123\n" + echo "Permission denied" >&2 + exit 1 +else + exec "$REAL_FIND" "$@" +fi' + +run_free_space() { + TEST_ESP_ON_ROOT=${TEST_ESP_ON_ROOT:-0} \ + TEST_FIND_DENIED=${TEST_FIND_DENIED:-0} \ + TEST_KERNEL_PACKAGES=${TEST_KERNEL_PACKAGES-linux} \ + TEST_PACMAN_STATUS=${TEST_PACMAN_STATUS:-0} \ + TEST_ESP_PATH="$esp_tree" \ + TEST_ESP_AVAILABLE_BYTES=${TEST_ESP_AVAILABLE_BYTES:-$((600 * 1024 * 1024))} \ + TEST_AVAILABLE_BYTES=$((20 * 1024 * 1024 * 1024)) \ + TEST_DF_INVALID=0 \ + PATH="$stub_bin:$ROOT/bin:$PATH" \ + "$ROOT/bin/omarchy-update-requires-free-space" +} + +set +e +output=$(TEST_ESP_AVAILABLE_BYTES=$((100 * 1024 * 1024)) run_free_space) +status=$? +set -e +(( status == 1 )) || fail "ESP with less than 2x the largest UKI free blocks the update" +[[ $output == *"free in $esp_tree"* ]] || fail "ESP guard names the ESP path in its message" +pass "full ESP blocks the update and names the ESP path" + +set +e +output=$(TEST_ESP_AVAILABLE_BYTES=$((600 * 1024 * 1024)) run_free_space) +status=$? +set -e +(( status == 0 )) || fail "ESP headroom at exactly 2x the largest UKI lets the update proceed" +pass "ESP with room for one more UKI plus churn does not block" + +set +e +output=$(TEST_ESP_AVAILABLE_BYTES=$((1024)) run_free_space) +status=$? +set -e +(( status == 1 )) || fail "ESP guard blocks from the measured shortfall, not a fixed floor" +[[ $output == *"600M"* ]] || fail "ESP guard reports the human-sized requirement" +pass "ESP requirement scales with the largest UKI on disk" + +set +e +rm -rf "$esp_tree/EFI/Linux" +output=$(TEST_ESP_AVAILABLE_BYTES=$((1024)) run_free_space) +status=$? +set -e +(( status == 0 )) || fail "ESP without UKIs does not block the update" +[[ -z $output ]] || fail "ESP without UKIs skips silently" +pass "ESP guard skips when there is no UKI to size against" + +set +e +mkdir -p "$esp_tree/EFI/Linux" +truncate -s 300M "$esp_tree/EFI/Linux/omarchy_linux.efi" +output=$(TEST_ESP_ON_ROOT=1 TEST_ESP_AVAILABLE_BYTES=1 run_free_space) +status=$? +set -e +(( status == 0 )) || fail "root-backed ESP defers to the root check" +pass "ESP on the root filesystem is covered by the root check" + +[[ -z $output ]] || fail "plain ESP directory on root skips silently" + +output=$(TEST_FIND_DENIED=1 run_free_space 2>&1) +[[ $output == *"$esp_tree"* && $output == *"permissions"* && $output == *"UKI headroom"* ]] || + fail "failed UKI scan warns about permissions and names the ESP" +pass "unreadable ESP warns without blocking, even with partial find output" + +check_headroom() { + local copies="$1" status output + output=$(TEST_ESP_AVAILABLE_BYTES=$((copies * 300 * 1024 * 1024)) run_free_space) + [[ -z $output ]] || fail "exact UKI headroom boundary succeeds silently" + set +e + output=$(TEST_ESP_AVAILABLE_BYTES=$((copies * 300 * 1024 * 1024 - 1)) run_free_space) + status=$? + set -e + (( status == 1 )) || fail "one byte below UKI headroom blocks" + [[ $output == *"$(numfmt --to=iec -- "$((copies * 300 * 1024 * 1024))") free in $esp_tree"* ]] || fail "warning uses calculated kernel headroom" +} + +TEST_KERNEL_PACKAGES=$'linux\nlinux-headers' check_headroom 2 +pass "one installed kernel requires two copies and excludes headers" +TEST_KERNEL_PACKAGES=$'linux\nlinux-lts\nlinux-zen\nlinux-ptl\nlinux-t2\nlinux-omarchy\nlinux-omarchy-custom\nlinux-headers\nlinux-omarchy-headers\nlinux-omarchy-custom-headers\nunrelated' check_headroom 8 +pass "installed kernel families plus one determine UKI headroom" +TEST_KERNEL_PACKAGES= check_headroom 2 +pass "empty kernel query retains two-copy headroom" +TEST_PACMAN_STATUS=1 TEST_KERNEL_PACKAGES=$'linux\nlinux-lts' check_headroom 2 +pass "failed kernel query falls back to two copies" + +# Isolate PATH so this case also works on hosts with pacman installed. +missing_pacman_bin="$test_tmp/no-pacman" +mkdir -p "$missing_pacman_bin" +for command in sed findmnt find df tail sort head numfmt; do + ln -s "$(command -v "$command")" "$missing_pacman_bin/$command" +done +for command in sed findmnt find df; do + ln -sf "$stub_bin/$command" "$missing_pacman_bin/$command" +done +rm -f "$stub_bin/pacman" +# run_free_space prepends the stubs and repo commands to this isolated PATH. +PATH="$missing_pacman_bin" check_headroom 2 +pass "unavailable pacman falls back to two copies"