--- 10_linux_zfs 2023-07-28 15:34:32.000000000 +0200 +++ 10_linux_zfs_grub_2.12~rc1 2023-08-01 10:08:36.802829248 +0200 @@ -30,8 +30,6 @@ export TEXTDOMAIN=grub export TEXTDOMAINDIR="${datarootdir}/locale" -set -u - ## Skip early if zfs utils isn't installed (instead of failing on first zpool list) if ! `which zfs >/dev/null 2>&1`; then exit 0 @@ -420,18 +418,15 @@ initrd_list="" kernel_list="" candidate_kernel_list="$(find "${boot_dir}" -maxdepth 1 -type f -regex '.*/\(vmlinuz\|vmlinux\|kernel\)-.*')" - while [ -n "${candidate_kernel_list}" ] ; do - list_basename="$(echo "${candidate_kernel_list}" | sed -e 's#.*/##')" - linux=$(version_find_latest ${list_basename}) - linux=$(echo "${candidate_kernel_list}" | while read k; do - if [ "$(basename "${k}")" = "${linux}" ]; then - echo -n "${k}" - break - fi - done) - # || true to not abort even if candidate_kernel_list is empty on last entry - candidate_kernel_list="$(echo "${candidate_kernel_list}" | fgrep -vx "${linux}"||true)" + # Perform a reverse version sort on the entire list. + # Temporarily replace the '.old' suffix by ' 1' and append ' 2' for all + # other files to order the '.old' files after their non-old counterpart + # in reverse-sorted order. + + reverse_sorted_list=$(echo $candidate_kernel_list | tr ' ' '\n' | sed -e 's/\.old$/ 1/; / 1$/! s/$/ 2/' | version_sort -r | sed -e 's/ 1$/.old/; s/ 2$//') + + for linux in ${reverse_sorted_list}; do if ! grub_file_is_not_garbage "${linux}" ; then continue fi