You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

84 lines
2.8 KiB

  1. #!/bin/sh
  2. set -e
  3. # Mark as not-for-autoremoval those kernel packages that are:
  4. # - the currently booted version
  5. # - the kernel version we've been called for
  6. # - the latest kernel version (as determined by debian version number)
  7. # - the second-latest kernel version
  8. #
  9. # In the common case this results in two kernels saved (booted into the
  10. # second-latest kernel, we install the latest kernel in an upgrade), but
  11. # can save up to four. Kernel refers here to a distinct release, which can
  12. # potentially be installed in multiple flavours counting as one kernel.
  13. eval $(apt-config shell APT_CONF_D Dir::Etc::parts/d)
  14. test -n "${APT_CONF_D}" || APT_CONF_D="/etc/apt/apt.conf.d"
  15. config_file="${APT_CONF_D}/01autoremove-kernels"
  16. eval $(apt-config shell DPKG Dir::bin::dpkg/f)
  17. test -n "$DPKG" || DPKG="/usr/bin/dpkg"
  18. list="$("${DPKG}" -l | awk '/^[ih][^nc][ ]+(linux|kfreebsd|gnumach)-image-[0-9]+\./ && $2 !~ /-dbg(:.*)?$/ && $2 !~ /-dbgsym(:.*)?$/ { print $2,$3; }' \
  19. | sed -e 's#^\(linux\|kfreebsd\|gnumach\)-image-##' -e 's#:[^:]\+ # #')"
  20. debverlist="$(echo "$list" | cut -d' ' -f 2 | sort --unique --reverse --version-sort)"
  21. if [ -n "$1" ]; then
  22. installed_version="$(echo "$list" | awk "\$1 == \"$1\" { print \$2;exit; }")"
  23. fi
  24. unamer="$(uname -r | tr '[A-Z]' '[a-z]')"
  25. if [ -n "$unamer" ]; then
  26. running_version="$(echo "$list" | awk "\$1 == \"$unamer\" { print \$2;exit; }")"
  27. fi
  28. # ignore the currently running version if attempting a reproducible build
  29. if [ -n "${SOURCE_DATE_EPOCH}" ]; then
  30. unamer=""
  31. running_version=""
  32. fi
  33. latest_version="$(echo "$debverlist" | sed -n 1p)"
  34. previous_version="$(echo "$debverlist" | sed -n 2p)"
  35. debkernels="$(echo "$latest_version
  36. $installed_version
  37. $running_version
  38. $previous_version" | sort -u | sed -e '/^$/ d')"
  39. kernels="$( (echo "$1
  40. $unamer"; for deb in $debkernels; do echo "$list" | awk "\$2 == \"$deb\" { print \$1; }"; done; ) \
  41. | sed -e 's#\([\.\+]\)#\\\1#g' -e '/^$/ d' | sort -u)"
  42. generateconfig() {
  43. cat <<EOF
  44. // DO NOT EDIT! File autogenerated by $0
  45. APT::NeverAutoRemove
  46. {
  47. EOF
  48. for package in $(apt-config dump --no-empty --format '%v%n' 'APT::VersionedKernelPackages'); do
  49. for kernel in $kernels; do
  50. echo " \"^${package}-${kernel}$\";"
  51. done
  52. done
  53. echo '};'
  54. if [ "${APT_AUTO_REMOVAL_KERNELS_DEBUG:-true}" = 'true' ]; then
  55. cat <<EOF
  56. /* Debug information:
  57. # dpkg list:
  58. $(dpkg -l | grep '\(linux\|kfreebsd\|gnumach\)-image-')
  59. # list of installed kernel packages:
  60. $list
  61. # list of different kernel versions:
  62. $debverlist
  63. # Installing kernel: $installed_version ($1)
  64. # Running kernel: ${running_version:-ignored} (${unamer:-ignored})
  65. # Last kernel: $latest_version
  66. # Previous kernel: $previous_version
  67. # Kernel versions list to keep:
  68. $debkernels
  69. # Kernel packages (version part) to protect:
  70. $kernels
  71. */
  72. EOF
  73. fi
  74. }
  75. generateconfig "$@" > "${config_file}.dpkg-new"
  76. mv -f "${config_file}.dpkg-new" "$config_file"
  77. chmod 444 "$config_file"