]>
Commit | Line | Data |
---|---|---|
a86f8516 | 1 | #!/bin/sh |
a86f8516 SL |
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) | |
bac1fd14 SL |
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 | |
a86f8516 SL |
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 | ||
94bfdf69 | 23 | |
94bfdf69 MV |
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" | |
a86f8516 SL |
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"`" | |
94bfdf69 | 40 | $DPKG --compare-versions "$version_a" gt "$version_b" |
a86f8516 SL |
41 | return "$?" |
42 | } | |
43 | ||
33677a0c | 44 | list="$(${DPKG} -l | awk '/^ii[ ]+(linux|kfreebsd|gnumach)-image-[0-9]*/ && $2 !~ /-dbg$/ { print $2 }' | sed -e 's#\(linux\|kfreebsd\|gnumach\)-image-##')" |
a86f8516 SL |
45 | |
46 | latest_version="" | |
bac1fd14 | 47 | previous_version="" |
a86f8516 SL |
48 | for i in $list; do |
49 | if version_test_gt "$i" "$latest_version"; then | |
bac1fd14 | 50 | previous_version="$latest_version" |
a86f8516 | 51 | latest_version="$i" |
bac1fd14 SL |
52 | elif version_test_gt "$i" "$previous_version"; then |
53 | previous_version="$i" | |
a86f8516 SL |
54 | fi |
55 | done | |
56 | ||
bac1fd14 SL |
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 | ||
a86f8516 SL |
66 | kernels=$(sort -u <<EOF |
67 | $latest_version | |
68 | $installed_version | |
69 | $running_version | |
bac1fd14 | 70 | $previous_version |
a86f8516 SL |
71 | EOF |
72 | ) | |
73 | ||
b9b0f622 DK |
74 | generateconfig() { |
75 | cat <<EOF | |
33677a0c DK |
76 | // DO NOT EDIT! File autogenerated by $0 |
77 | APT::NeverAutoRemove | |
a86f8516 | 78 | { |
a86f8516 | 79 | EOF |
b9b0f622 DK |
80 | apt-config dump --no-empty --format '%v%n' 'APT::VersionedKernelPackages' | while read package; do |
81 | for kernel in $kernels; do | |
33677a0c | 82 | echo " \"^${package}-${kernel}$\";" |
b9b0f622 DK |
83 | done |
84 | done | |
33677a0c | 85 | echo '};' |
b9b0f622 DK |
86 | } |
87 | generateconfig > "${config_file}.dpkg-new" | |
88 | mv "${config_file}.dpkg-new" "$config_file" |