[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

[PATCH v2] Configure the amount of kernels to keep



This commit introduces the following configuration for keeping a
configurable amount of kernels: APT::NeverAutoRemove::KernelSize

The default and minimal amount is 2, to support the booted kernel,
latest kernel and in case the booted and latest kernel are the same, the
previous kernel. This is in line with the past behaviour.

Signed-off-by: Wesley Schwengle <wesleys@opperschaap.net>
---
 apt-pkg/algorithms.cc        | 33 +++++++++++++++++++--------------
 doc/examples/configure-index |  1 +
 2 files changed, 20 insertions(+), 14 deletions(-)

diff --git a/apt-pkg/algorithms.cc b/apt-pkg/algorithms.cc
index 3d4096a94..13d9b7575 100644
--- a/apt-pkg/algorithms.cc
+++ b/apt-pkg/algorithms.cc
@@ -1554,30 +1554,35 @@ 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 KeepKernelSize = (unsigned long) _config->FindI("APT::NeverAutoRemove::KernelSize", 2);
+   if (KeepKernelSize < 2)
+      KeepKernelSize = 2;
+
+   if (Debug)
+	 std::clog << "Amount of kernels to keep " <<  KeepKernelSize << std::endl;
+
    if (not bootedVersion.empty())
    {
       if (Debug)
 	 std::clog << "Keeping booted kernel " << bootedVersion << std::endl;
       keep.insert(bootedVersion);
    }
-   if (latest != version2unames.rend())
-   {
+
+   while (keep.size() < KeepKernelSize && versions != version2unames.rend()) {
+      auto v = versions->first;
+      if (v == bootedVersion) {
+          versions++;
+          continue;
+      }
       if (Debug)
-	 std::clog << "Keeping latest kernel " << latest->first << std::endl;
-      keep.insert(latest->first);
-   }
-   if (keep.size() < 2 && previous != version2unames.rend())
-   {
-      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/examples/configure-index b/doc/examples/configure-index
index 0d4dd31a5..7a21ad2c4 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::KernelSize "<INT>";
   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)
-- 
2.42.0.1140.gf01f4dc781


Reply to: