]> git.saurik.com Git - apt.git/blame - debian/apt.auto-removal.sh
centralize unlink checks in acquire-item
[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
a86f8516 21
63c71412 22list="$("${DPKG}" -l | awk '/^[ih][^nc][ ]+(linux|kfreebsd|gnumach)-image-[0-9]+\./ && $2 !~ /-dbg$/ && $2 !~ /-dbgsym$/ { print $2,$3; }' \
3196dae8
DK
23 | sed -e 's#^\(linux\|kfreebsd\|gnumach\)-image-##' -e 's#:[^:]\+ # #')"
24debverlist="$(echo "$list" | cut -d' ' -f 2 | sort --unique --reverse --version-sort)"
a86f8516 25
3196dae8
DK
26if [ -n "$1" ]; then
27 installed_version="$(echo "$list" | awk "\$1 == \"$1\" { print \$2;exit; }")"
28fi
29unamer="$(uname -r)"
30if [ -n "$unamer" ]; then
31 running_version="$(echo "$list" | awk "\$1 == \"$unamer\" { print \$2;exit; }")"
bac1fd14 32fi
3196dae8
DK
33latest_version="$(echo "$debverlist" | sed -n 1p)"
34previous_version="$(echo "$debverlist" | sed -n 2p)"
bac1fd14 35
3196dae8 36debkernels="$(echo "$latest_version
a86f8516
SL
37$installed_version
38$running_version
3196dae8
DK
39$previous_version" | sort -u | sed -e '/^$/ d')"
40kernels="$( (echo "$1
41$unamer"; for deb in $debkernels; do echo "$list" | awk "\$2 == \"$deb\" { print \$1; }"; done; ) \
42 | sed -e 's#\.#\\.#g' -e '/^$/ d' | sort -u)"
a86f8516 43
b9b0f622
DK
44generateconfig() {
45 cat <<EOF
33677a0c
DK
46// DO NOT EDIT! File autogenerated by $0
47APT::NeverAutoRemove
a86f8516 48{
a86f8516 49EOF
8597ee54 50 for package in $(apt-config dump --no-empty --format '%v%n' 'APT::VersionedKernelPackages'); do
b9b0f622 51 for kernel in $kernels; do
33677a0c 52 echo " \"^${package}-${kernel}$\";"
b9b0f622
DK
53 done
54 done
33677a0c 55 echo '};'
8597ee54
DK
56 if [ "${APT_AUTO_REMOVAL_KERNELS_DEBUG:-true}" = 'true' ]; then
57 cat <<EOF
58/* Debug information:
59# dpkg list:
60$(dpkg -l | grep '\(linux\|kfreebsd\|gnumach\)-image-')
61# list of installed kernel packages:
62$list
63# list of different kernel versions:
64$debverlist
65# Installing kernel: $installed_version ($1)
66# Running kernel: $running_version ($unamer)
67# Last kernel: $latest_version
68# Previous kernel: $previous_version
69# Kernel versions list to keep:
70$debkernels
71# Kernel packages (version part) to protect:
72$kernels
73*/
74EOF
75 fi
b9b0f622 76}
8597ee54
DK
77generateconfig "$@" > "${config_file}.dpkg-new"
78mv -f "${config_file}.dpkg-new" "$config_file"
3196dae8 79chmod 444 "$config_file"