apt.auto-removal.sh 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/bin/sh
  2. set -e
  3. # Author: Steve Langasek <steve.langasek@canonical.com>
  4. #
  5. # Mark as not-for-autoremoval those kernel packages that are:
  6. # - the currently booted version
  7. # - the kernel version we've been called for
  8. # - the latest kernel version (determined using rules copied from the grub
  9. # package for deciding which kernel to boot)
  10. # - the second-latest kernel version, if the booted kernel version is
  11. # already the latest and this script is called for that same version,
  12. # to ensure a fallback remains available in the event the newly-installed
  13. # kernel at this ABI fails to boot
  14. # In the common case, this results in exactly two kernels saved, but it can
  15. # result in three kernels being saved. It's better to err on the side of
  16. # saving too many kernels than saving too few.
  17. #
  18. # We generate this list and save it to /etc/apt/apt.conf.d instead of marking
  19. # packages in the database because this runs from a postinst script, and apt
  20. # will overwrite the db when it exits.
  21. eval $(apt-config shell APT_CONF_D Dir::Etc::parts/d)
  22. test -n "${APT_CONF_D}" || APT_CONF_D="/etc/apt/apt.conf.d"
  23. config_file=${APT_CONF_D}/01autoremove-kernels
  24. eval $(apt-config shell DPKG Dir::bin::dpkg/f)
  25. test -n "$DPKG" || DPKG="/usr/bin/dpkg"
  26. installed_version="$1"
  27. running_version="$(uname -r)"
  28. version_test_gt ()
  29. {
  30. local version_test_gt_sedexp="s/[._-]\(pre\|rc\|test\|git\|old\|trunk\)/~\1/g"
  31. local version_a="`echo "$1" | sed -e "$version_test_gt_sedexp"`"
  32. local version_b="`echo "$2" | sed -e "$version_test_gt_sedexp"`"
  33. $DPKG --compare-versions "$version_a" gt "$version_b"
  34. return "$?"
  35. }
  36. list=$(${DPKG} -l 'linux-image-[0-9]*'|awk '/^ii/ && $2 !~ /-dbg$/ { print $2 }' | sed -e's/linux-image-//')
  37. latest_version=""
  38. previous_version=""
  39. for i in $list; do
  40. if version_test_gt "$i" "$latest_version"; then
  41. previous_version="$latest_version"
  42. latest_version="$i"
  43. elif version_test_gt "$i" "$previous_version"; then
  44. previous_version="$i"
  45. fi
  46. done
  47. if [ "$latest_version" != "$installed_version" ] \
  48. || [ "$latest_version" != "$running_version" ] \
  49. || [ "$installed_version" != "$running_version" ]
  50. then
  51. # We have at least two kernels that we have reason to think the
  52. # user wants, so don't save the second-newest version.
  53. previous_version=
  54. fi
  55. kernels=$(sort -u <<EOF
  56. $latest_version
  57. $installed_version
  58. $running_version
  59. $previous_version
  60. EOF
  61. )
  62. cat > "$config_file".dpkg-new <<EOF
  63. // File autogenerated by $0, do not edit
  64. APT
  65. {
  66. NeverAutoRemove
  67. {
  68. EOF
  69. for kernel in $kernels; do
  70. echo " \"^linux-image-${kernel}$\";" >> "$config_file".dpkg-new
  71. echo " \"^linux-image-extra-${kernel}$\";" >> "$config_file".dpkg-new
  72. echo " \"^linux-signed-image-${kernel}$\";" >> "$config_file".dpkg-new
  73. echo " \"^linux-backports-modules-.*-${kernel}$\";" >> "$config_file".dpkg-new
  74. echo " \"^linux-headers-${kernel}$\";" >> "$config_file".dpkg-new
  75. done
  76. cat >> "$config_file".dpkg-new <<EOF
  77. };
  78. };
  79. EOF
  80. mv "$config_file".dpkg-new "$config_file"