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