]>
Commit | Line | Data |
---|---|---|
a86f8516 SL |
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) | |
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 | ||
94bfdf69 | 24 | |
94bfdf69 MV |
25 | eval $(apt-config shell APT_CONF_D Dir::Etc::parts/d) |
26 | test -n "${APT_CONF_D}" || APT_CONF_D="/etc/apt/apt.conf.d" | |
27 | config_file=${APT_CONF_D}/01autoremove-kernels | |
28 | ||
29 | eval $(apt-config shell DPKG Dir::bin::dpkg/f) | |
30 | test -n "$DPKG" || DPKG="/usr/bin/dpkg" | |
a86f8516 SL |
31 | |
32 | installed_version="$1" | |
33 | running_version="$(uname -r)" | |
34 | ||
35 | ||
36 | version_test_gt () | |
37 | { | |
38 | local version_test_gt_sedexp="s/[._-]\(pre\|rc\|test\|git\|old\|trunk\)/~\1/g" | |
39 | local version_a="`echo "$1" | sed -e "$version_test_gt_sedexp"`" | |
40 | local version_b="`echo "$2" | sed -e "$version_test_gt_sedexp"`" | |
94bfdf69 | 41 | $DPKG --compare-versions "$version_a" gt "$version_b" |
a86f8516 SL |
42 | return "$?" |
43 | } | |
44 | ||
c1f8f8c0 | 45 | list=$(${DPKG} -l 'linux-image-[0-9]*'|awk '/^ii/ && $2 !~ /-dbg$/ { print $2 }' | sed -e's/linux-image-//') |
a86f8516 SL |
46 | |
47 | latest_version="" | |
bac1fd14 | 48 | previous_version="" |
a86f8516 SL |
49 | for i in $list; do |
50 | if version_test_gt "$i" "$latest_version"; then | |
bac1fd14 | 51 | previous_version="$latest_version" |
a86f8516 | 52 | latest_version="$i" |
bac1fd14 SL |
53 | elif version_test_gt "$i" "$previous_version"; then |
54 | previous_version="$i" | |
a86f8516 SL |
55 | fi |
56 | done | |
57 | ||
bac1fd14 SL |
58 | if [ "$latest_version" != "$installed_version" ] \ |
59 | || [ "$latest_version" != "$running_version" ] \ | |
60 | || [ "$installed_version" != "$running_version" ] | |
61 | then | |
62 | # We have at least two kernels that we have reason to think the | |
63 | # user wants, so don't save the second-newest version. | |
64 | previous_version= | |
65 | fi | |
66 | ||
a86f8516 SL |
67 | kernels=$(sort -u <<EOF |
68 | $latest_version | |
69 | $installed_version | |
70 | $running_version | |
bac1fd14 | 71 | $previous_version |
a86f8516 SL |
72 | EOF |
73 | ) | |
74 | ||
b9b0f622 DK |
75 | generateconfig() { |
76 | cat <<EOF | |
90b9659c | 77 | // File autogenerated by $0, do not edit |
a86f8516 SL |
78 | APT |
79 | { | |
80 | NeverAutoRemove | |
81 | { | |
82 | EOF | |
b9b0f622 DK |
83 | apt-config dump --no-empty --format '%v%n' 'APT::VersionedKernelPackages' | while read package; do |
84 | for kernel in $kernels; do | |
85 | echo " \"^${package}-${kernel}$\";" | |
86 | done | |
87 | done | |
88 | cat <<EOF | |
a86f8516 SL |
89 | }; |
90 | }; | |
91 | EOF | |
b9b0f622 DK |
92 | } |
93 | generateconfig > "${config_file}.dpkg-new" | |
94 | mv "${config_file}.dpkg-new" "$config_file" |