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