]> git.saurik.com Git - apt.git/blame - debian/apt.auto-removal.sh
Release 1.3
[apt.git] / debian / apt.auto-removal.sh
CommitLineData
a86f8516 1#!/bin/sh
a86f8516 2set -e
a86f8516
SL
3# Mark as not-for-autoremoval those kernel packages that are:
4# - the currently booted version
5# - the kernel version we've been called for
3196dae8
DK
6# - the latest kernel version (as determined by debian version number)
7# - the second-latest kernel version
a86f8516 8#
3196dae8
DK
9# In the common case this results in two kernels saved (booted into the
10# second-latest kernel, we install the latest kernel in an upgrade), but
11# can save up to four. Kernel refers here to a distinct release, which can
12# potentially be installed in multiple flavours counting as one kernel.
94bfdf69 13
94bfdf69
MV
14eval $(apt-config shell APT_CONF_D Dir::Etc::parts/d)
15test -n "${APT_CONF_D}" || APT_CONF_D="/etc/apt/apt.conf.d"
3196dae8 16config_file="${APT_CONF_D}/01autoremove-kernels"
94bfdf69
MV
17
18eval $(apt-config shell DPKG Dir::bin::dpkg/f)
19test -n "$DPKG" || DPKG="/usr/bin/dpkg"
a86f8516 20
d94233d8 21list="$("${DPKG}" -l | awk '/^[ih][^nc][ ]+(linux|kfreebsd|gnumach)-image-[0-9]+\./ && $2 !~ /-dbg(:.*)?$/ && $2 !~ /-dbgsym(:.*)?$/ { print $2,$3; }' \
3196dae8
DK
22 | sed -e 's#^\(linux\|kfreebsd\|gnumach\)-image-##' -e 's#:[^:]\+ # #')"
23debverlist="$(echo "$list" | cut -d' ' -f 2 | sort --unique --reverse --version-sort)"
a86f8516 24
3196dae8
DK
25if [ -n "$1" ]; then
26 installed_version="$(echo "$list" | awk "\$1 == \"$1\" { print \$2;exit; }")"
27fi
15134a8e 28unamer="$(uname -r | tr '[A-Z]' '[a-z]')"
3196dae8
DK
29if [ -n "$unamer" ]; then
30 running_version="$(echo "$list" | awk "\$1 == \"$unamer\" { print \$2;exit; }")"
bac1fd14 31fi
3196dae8
DK
32latest_version="$(echo "$debverlist" | sed -n 1p)"
33previous_version="$(echo "$debverlist" | sed -n 2p)"
bac1fd14 34
3196dae8 35debkernels="$(echo "$latest_version
a86f8516
SL
36$installed_version
37$running_version
3196dae8
DK
38$previous_version" | sort -u | sed -e '/^$/ d')"
39kernels="$( (echo "$1
40$unamer"; for deb in $debkernels; do echo "$list" | awk "\$2 == \"$deb\" { print \$1; }"; done; ) \
130176bc 41 | sed -e 's#\([\.\+]\)#\\\1#g' -e '/^$/ d' | sort -u)"
a86f8516 42
b9b0f622
DK
43generateconfig() {
44 cat <<EOF
33677a0c
DK
45// DO NOT EDIT! File autogenerated by $0
46APT::NeverAutoRemove
a86f8516 47{
a86f8516 48EOF
8597ee54 49 for package in $(apt-config dump --no-empty --format '%v%n' 'APT::VersionedKernelPackages'); do
b9b0f622 50 for kernel in $kernels; do
33677a0c 51 echo " \"^${package}-${kernel}$\";"
b9b0f622
DK
52 done
53 done
33677a0c 54 echo '};'
8597ee54
DK
55 if [ "${APT_AUTO_REMOVAL_KERNELS_DEBUG:-true}" = 'true' ]; then
56 cat <<EOF
57/* Debug information:
58# dpkg list:
59$(dpkg -l | grep '\(linux\|kfreebsd\|gnumach\)-image-')
60# list of installed kernel packages:
61$list
62# list of different kernel versions:
63$debverlist
64# Installing kernel: $installed_version ($1)
65# Running kernel: $running_version ($unamer)
66# Last kernel: $latest_version
67# Previous kernel: $previous_version
68# Kernel versions list to keep:
69$debkernels
70# Kernel packages (version part) to protect:
71$kernels
72*/
73EOF
74 fi
b9b0f622 75}
8597ee54
DK
76generateconfig "$@" > "${config_file}.dpkg-new"
77mv -f "${config_file}.dpkg-new" "$config_file"
3196dae8 78chmod 444 "$config_file"