]> git.saurik.com Git - apt.git/blob - debian/apt.auto-removal.sh
8f6fc2efcf522d99aa73e38c352e215d669a4544
[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 # In the common case, this results in exactly two kernels saved, but it can
13 # result in three kernels being saved. It's better to err on the side of
14 # saving too many kernels than saving too few.
15 #
16 # We generate this list and save it to /etc/apt/apt.conf.d instead of marking
17 # packages in the database because this runs from a postinst script, and apt
18 # will overwrite the db when it exits.
19
20 config_file=/etc/apt/apt.conf.d/01autoremove-kernels
21
22 installed_version="$1"
23 running_version="$(uname -r)"
24
25
26 version_test_gt ()
27 {
28 local version_test_gt_sedexp="s/[._-]\(pre\|rc\|test\|git\|old\|trunk\)/~\1/g"
29 local version_a="`echo "$1" | sed -e "$version_test_gt_sedexp"`"
30 local version_b="`echo "$2" | sed -e "$version_test_gt_sedexp"`"
31 dpkg --compare-versions "$version_a" gt "$version_b"
32 return "$?"
33 }
34
35 list=$(dpkg -l 'linux-image-[0-9]*'|awk '/^ii/ { print $2 }' | sed -e's/linux-image-//')
36
37 latest_version=""
38 for i in $list; do
39 if version_test_gt "$i" "$latest_version"; then
40 latest_version="$i"
41 fi
42 done
43
44 kernels=$(sort -u <<EOF
45 $latest_version
46 $installed_version
47 $running_version
48 EOF
49 )
50
51 cat > "$config_file".dpkg-new <<EOF
52 # File autogenerated by $0, do not edit
53 APT
54 {
55 NeverAutoRemove
56 {
57 EOF
58 for kernel in $kernels; do
59 echo " \"^linux-image-$kernel.*\";" >> "$config_file".dpkg-new
60 echo " \"^linux-image-extra-$kernel.*\";" >> "$config_file".dpkg-new
61 echo " \"^linux-signed-image-$kernel.*\";" >> "$config_file".dpkg-new
62 done
63 cat >> "$config_file".dpkg-new <<EOF
64 };
65 };
66 EOF
67 mv "$config_file".dpkg-new "$config_file"