diff options
author | Julian Andres Klode <jak@debian.org> | 2024-02-20 08:30:05 +0000 |
---|---|---|
committer | Julian Andres Klode <jak@debian.org> | 2024-02-20 08:30:05 +0000 |
commit | dfa7965f2f82ba5ada3ed4feea4630a7d4cc5895 (patch) | |
tree | d03680d77336cb847067fdfcd5020441c07e1b42 | |
parent | 33c2f0fe5b9ec1c368a7f377de5632f183b12e12 (diff) | |
parent | 8808d81b4f3abfe9e5748ed56664019ef6b2a04d (diff) |
Merge branch 'keep-kernel-size' into 'main'
Configure the amount of kernels to keep
See merge request apt-team/apt!324
-rw-r--r-- | apt-pkg/algorithms.cc | 33 | ||||
-rw-r--r-- | doc/apt.conf.5.xml | 55 | ||||
-rw-r--r-- | doc/examples/configure-index | 1 |
3 files changed, 76 insertions, 13 deletions
diff --git a/apt-pkg/algorithms.cc b/apt-pkg/algorithms.cc index d841d6dd8..24a41b70a 100644 --- a/apt-pkg/algorithms.cc +++ b/apt-pkg/algorithms.cc @@ -1574,30 +1574,37 @@ std::string GetProtectedKernelsRegex(pkgCache *cache, bool ReturnRemove) if (version2unames.size() == 0) return ""; - auto latest = version2unames.rbegin(); - auto previous = latest; - ++previous; - + auto versions = version2unames.rbegin(); std::set<std::string> keep; + auto keepKernels = (unsigned long)_config->FindI("APT::NeverAutoRemove::KernelCount", 2); + if (keepKernels < 2) + keepKernels = 2; + + if (Debug) + std::clog << "Amount of kernels to keep " << keepKernels << std::endl; + if (not bootedVersion.empty()) { if (Debug) std::clog << "Keeping booted kernel " << bootedVersion << std::endl; keep.insert(bootedVersion); } - if (latest != version2unames.rend()) - { - if (Debug) - std::clog << "Keeping latest kernel " << latest->first << std::endl; - keep.insert(latest->first); - } - if (keep.size() < 2 && previous != version2unames.rend()) + + while (keep.size() < keepKernels && versions != version2unames.rend()) { + auto v = versions->first; + if (v == bootedVersion) + { + versions++; + continue; + } if (Debug) - std::clog << "Keeping previous kernel " << previous->first << std::endl; - keep.insert(previous->first); + std::clog << "Keeping previous kernel " << v << std::endl; + keep.insert(v); + versions++; } + // Escape special characters '.' and '+' in version strings so we can build a regular expression auto escapeSpecial = [](std::string input) -> std::string { for (size_t pos = 0; (pos = input.find_first_of(".+", pos)) != input.npos; pos += 2) { diff --git a/doc/apt.conf.5.xml b/doc/apt.conf.5.xml index 7e7d64f8d..46f05d42e 100644 --- a/doc/apt.conf.5.xml +++ b/doc/apt.conf.5.xml @@ -311,6 +311,61 @@ APT::Compressor::rev { <listitem><para>The CDROM subsection controls the &apt-cdrom; tool; please see its documentation for more information about the options here.</para></listitem> </varlistentry> + + <varlistentry> + <term> + <option>NeverAutoRemove</option> + </term> + <listitem> + <para> + Never autoremove packages that match the regular expression(s). + </para> + </listitem> + </varlistentry> + + <varlistentry> + <term> + <option>Protect-Kernels</option> + </term> + <listitem> + <para> + This option tells apt autoremove that kernels are protected and + defaults to true. In case kernels are not protected they are + treated as any other package. + </para> + </listitem> + </varlistentry> + + <varlistentry> + <term> + <option>VersionedKernelPackages</option> + </term> + <listitem> + <para> + Define the regular expression(s) for versioned kernel packages. + Based on these expressions a rule set is injected into apt + similar to APT::NeverAutoRemove regular expressions. + </para> + </listitem> + </varlistentry> + + <varlistentry> + <term> + <option>NeverAutoRemove::KernelCount</option> + </term> + <listitem> + <para> + Keep a custom amount of kernels when autoremoving and defaults + to 2, meaning two kernels are kept. Apt will always keep the + running kernel and the latest one. If the latest kernel is the + same as the running kernel, the second latest kernel is kept. + Because of this, any value lower than 2 will be ignored. If you + want only the latest kernel, you should set + APT::Protect-Kernels to false. + </para> + </listitem> + </varlistentry> + </variablelist> </refsect1> diff --git a/doc/examples/configure-index b/doc/examples/configure-index index 0d4dd31a5..f37b7696c 100644 --- a/doc/examples/configure-index +++ b/doc/examples/configure-index @@ -55,6 +55,7 @@ APT Build-Profiles "<STRING_OR_LIST>"; NeverAutoRemove "<LIST>"; // list of package name regexes + NeverAutoRemove::KernelCount "<INT>"; // Keep the configured amount of kernels LastInstalledKernel "<STRING>"; // last installed kernel version VersionedKernelPackages "<LIST>"; // regular expressions to be protected from autoremoval (kernel uname will be appended) Protect-Kernels "<BOOL>"; // whether to protect installed kernels against autoremoval (default: true) |