apt.auto-removal.sh 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 | awk '/^ii[ ]+(linux|kfreebsd|gnumach)-image-[0-9]+\./ && $2 !~ /-dbg$/ { print $2 }' | sed -e 's#\(linux\|kfreebsd\|gnumach\)-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="$(echo "$latest_version
  56. $installed_version
  57. $running_version
  58. $previous_version" | sort -u | sed -e 's#\.#\\.#g' )"
  59. generateconfig() {
  60. cat <<EOF
  61. // DO NOT EDIT! File autogenerated by $0
  62. APT::NeverAutoRemove
  63. {
  64. EOF
  65. apt-config dump --no-empty --format '%v%n' 'APT::VersionedKernelPackages' | while read package; do
  66. for kernel in $kernels; do
  67. echo " \"^${package}-${kernel}$\";"
  68. done
  69. done
  70. echo '};'
  71. }
  72. generateconfig > "${config_file}.dpkg-new"
  73. mv "${config_file}.dpkg-new" "$config_file"