]>
Commit | Line | Data |
---|---|---|
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="$(echo "$latest_version | |
67 | $installed_version | |
68 | $running_version | |
69 | $previous_version" | sort -u | sed -e 's#\.#\\.#g' )" | |
70 | ||
71 | generateconfig() { | |
72 | cat <<EOF | |
73 | // DO NOT EDIT! File autogenerated by $0 | |
74 | APT::NeverAutoRemove | |
75 | { | |
76 | EOF | |
77 | apt-config dump --no-empty --format '%v%n' 'APT::VersionedKernelPackages' | while read package; do | |
78 | for kernel in $kernels; do | |
79 | echo " \"^${package}-${kernel}$\";" | |
80 | done | |
81 | done | |
82 | echo '};' | |
83 | } | |
84 | generateconfig > "${config_file}.dpkg-new" | |
85 | mv "${config_file}.dpkg-new" "$config_file" |