]> git.saurik.com Git - apt.git/blame - debian/apt.auto-removal.sh
Make sure we always have at least two kernels marked not for removal
[apt.git] / debian / apt.auto-removal.sh
CommitLineData
a86f8516
SL
1#!/bin/sh
2
3set -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)
bac1fd14
SL
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
a86f8516
SL
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
24config_file=/etc/apt/apt.conf.d/01autoremove-kernels
25
26installed_version="$1"
27running_version="$(uname -r)"
28
29
30version_test_gt ()
31{
32 local version_test_gt_sedexp="s/[._-]\(pre\|rc\|test\|git\|old\|trunk\)/~\1/g"
33 local version_a="`echo "$1" | sed -e "$version_test_gt_sedexp"`"
34 local version_b="`echo "$2" | sed -e "$version_test_gt_sedexp"`"
35 dpkg --compare-versions "$version_a" gt "$version_b"
36 return "$?"
37}
38
39list=$(dpkg -l 'linux-image-[0-9]*'|awk '/^ii/ { print $2 }' | sed -e's/linux-image-//')
40
41latest_version=""
bac1fd14 42previous_version=""
a86f8516
SL
43for i in $list; do
44 if version_test_gt "$i" "$latest_version"; then
bac1fd14 45 previous_version="$latest_version"
a86f8516 46 latest_version="$i"
bac1fd14
SL
47 elif version_test_gt "$i" "$previous_version"; then
48 previous_version="$i"
a86f8516
SL
49 fi
50done
51
bac1fd14
SL
52if [ "$latest_version" != "$installed_version" ] \
53 || [ "$latest_version" != "$running_version" ] \
54 || [ "$installed_version" != "$running_version" ]
55then
56 # We have at least two kernels that we have reason to think the
57 # user wants, so don't save the second-newest version.
58 previous_version=
59fi
60
a86f8516
SL
61kernels=$(sort -u <<EOF
62$latest_version
63$installed_version
64$running_version
bac1fd14 65$previous_version
a86f8516
SL
66EOF
67)
68
69cat > "$config_file".dpkg-new <<EOF
70# File autogenerated by $0, do not edit
71APT
72{
73 NeverAutoRemove
74 {
75EOF
76for kernel in $kernels; do
77 echo " \"^linux-image-$kernel.*\";" >> "$config_file".dpkg-new
78 echo " \"^linux-image-extra-$kernel.*\";" >> "$config_file".dpkg-new
79 echo " \"^linux-signed-image-$kernel.*\";" >> "$config_file".dpkg-new
80done
81cat >> "$config_file".dpkg-new <<EOF
82 };
83};
84EOF
85mv "$config_file".dpkg-new "$config_file"