<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>maintenance &amp;mdash; alan</title>
    <link>https://www.franzoni.eu/alan/tag:maintenance</link>
    <description>Alan&#39;s musings</description>
    <language>en</language>
    <pubDate>Tue, 17 Nov 2026 01:04:51 +0000</pubDate>
    <item>
      <title>Purging outdated kernels on systems with unattended-upgrades</title>
      <link>https://www.franzoni.eu/alan/purging-outdated-kernels-on-systems-with-unattended-upgrades</link>
      <description>&lt;![CDATA[Whenever I install a production system/server I tend to enable full automatic updating, because&#xA;it&#39;s usually better that something breaks because of a failed update rather than leaving a production machine unpatched; and if you pick a rather stable distribution (Centos or Ubuntu LTS are good examples) chances that you&#39;ll end up with a broken system are fairly low.&#xA;&#xA;Btw let&#39;s get back to the point: in such cases you may end up with a disk which was filled up with obsolete kernels. The following snippets allows you to get all outdated kernels which are not in use, so you can automagically pipe its output to an  apt-get remove -y in a cron job.&#xA;&#xA;That may sound trivial, but what happens if you&#39;re not careful (e.g. if you try deleting all kernels but the latest) is that you might try removing the currently running kernel - and it&#39;s pretty bad if that happens in an unattended cron job.&#xA;&#xA;!/usr/bin/env python&#xA;&#xA;print all installed kernel/headers/etc BUT the current and the latest;&#xA;suitable to be used with apt-get remove.&#xA;requires aptitude to be installed. Works on Ubuntu and probably Debian&#xA;&#xA;from subprocess import checkoutput&#xA;import re&#xA;&#xA;def runcmd(s):&#xA;    return checkoutput(s.split())&#xA;&#xA;should work for anything until kernel 3.100&#xA;kernelversionpattern = re.compile(&#34;[23]\.\d{1,2}\.\d{1,2}-\d{1,3}&#34;)&#xA;def getkernelversion(kernelstring):&#xA;    return kernelversionpattern.search(kernelstring).group(0)&#xA;&#xA;def getprovidingpackages(pkg):&#xA;    return filter(lambda x: x != &#34;&#34;,&#xA;        map(str.strip,&#xA;            runcmd(&#39;aptitude search ~i~P%s -F%%p&#39; % pkg).split(&#34;\n&#34;)&#xA;            )&#xA;        )&#xA;&#xA;def getnonmatchingpackages(packagelist, excluding):&#xA;    return [k for k in packagelist if not getkernelversion(k) in&#xA;        excluding]&#xA;&#xA;allkernels = getprovidingpackages(&#34;linux-image&#34;)&#xA;allkernels.sort() # lexicographical&#xA;&#xA;kernelversions = map(getkernelversion, allkernels)&#xA;latestversion = kernelversions[-1]&#xA;currentversion = getkernelversion(runcmd(&#34;uname -r&#34;))&#xA;&#xA;kernelstoremove = getnonmatchingpackages(allkernels, (currentversion,&#xA;    latestversion))&#xA;&#xA;implheaders = getprovidingpackages(&#34;linux-headers&#34;)&#xA;&#xA;headerstoremove = getnonmatchingpackages(implheaders, (currentversion,&#xA;    latestversion))&#xA;&#xA;This seems to be needed in Ubuntu   = 13.04&#xA;baseheaderstoremove = [header.replace(&#34;-generic&#34;, &#34;&#34;) for header in headerstoremove]&#xA;headerstoremove += baseheaderstoremove&#xA;&#xA;print &#34; &#34;.join(kernelstoremove + headerstoremove)&#xA;&#xA;Suggested use in a cron job, of course, is just:&#xA;&#xA;apt-get remove --purge -y ubuntugetobsoletekernelandheaderspkgs.py&#xA;&#xA;WARNING: recent changes to the way Ubuntu marks dependencies and provided names for kernel headers require that you uncomment a couple of lines in the script towards the end; just remove the \# at the beginning of the lines 46 and 47.&#xA;&#xA;UPDATE: in recent Ubuntu versions, just invoking apt-get autoremove --purge will do the trick. Such command will remove all automatically installed packages that are no longer required in the running system. It can remove other packages beyond outdated kernels, by the way.&#xA;&#xA;#MostlyUnixish #linux #ubuntu #debian #update #maintenance&#xA;]]&gt;</description>
      <content:encoded><![CDATA[<p>Whenever I install a production system/server I tend to enable full automatic updating, because
it&#39;s usually better that something breaks because of a failed update rather than leaving a production machine unpatched; and if you pick a rather stable distribution (Centos or Ubuntu LTS are good examples) chances that you&#39;ll end up with a broken system are fairly low.</p>

<p>Btw let&#39;s get back to the point: in such cases you may end up with a disk which was filled up with obsolete kernels. The following snippets allows you to get all outdated kernels which are not in use, so you can automagically pipe its output to an <code>apt-get remove -y</code> in a cron job.</p>

<p>That may sound trivial, but what happens if you&#39;re not careful (e.g. if you try deleting all kernels but the latest) is that you might try removing the currently running kernel – and it&#39;s pretty bad if that happens in an unattended cron job.</p>

<pre><code class="language-python">#!/usr/bin/env python

# print all installed kernel/headers/etc BUT the current and the latest;
# suitable to be used with apt-get remove.
# requires aptitude to be installed. Works on Ubuntu and probably Debian

from subprocess import check_output
import re

def runcmd(s):
    return check_output(s.split())

# should work for anything until kernel 3.100
kernel_version_pattern = re.compile(&#34;[23]\.\d{1,2}\.\d{1,2}-\d{1,3}&#34;)
def get_kernel_version(kernelstring):
    return kernel_version_pattern.search(kernelstring).group(0)

def get_providing_packages(pkg):
    return filter(lambda x: x != &#34;&#34;,
        map(str.strip,
            runcmd(&#39;aptitude search ~i~P%s -F%%p&#39; % pkg).split(&#34;\n&#34;)
            )
        )

def get_nonmatching_packages(package_list, excluding):
    return [k for k in package_list if not get_kernel_version(k) in
        excluding]

all_kernels = get_providing_packages(&#34;linux-image&#34;)
all_kernels.sort() # lexicographical

kernel_versions = map(get_kernel_version, all_kernels)
latest_version = kernel_versions[-1]
current_version = get_kernel_version(runcmd(&#34;uname -r&#34;))

kernels_to_remove = get_nonmatching_packages(all_kernels, (current_version,
    latest_version))

impl_headers = get_providing_packages(&#34;linux-headers&#34;)

headers_to_remove = get_nonmatching_packages(impl_headers, (current_version,
    latest_version))

# This seems to be needed in Ubuntu &gt;= 13.04
#base_headers_to_remove = [header.replace(&#34;-generic&#34;, &#34;&#34;) for header in headers_to_remove]
#headers_to_remove += base_headers_to_remove

print &#34; &#34;.join(kernels_to_remove + headers_to_remove)
</code></pre>

<p>Suggested use in a cron job, of course, is just:</p>

<pre><code>apt-get remove --purge -y `ubuntu_get_obsolete_kernel_and_headers_pkgs.py`
</code></pre>

<p><strong>WARNING</strong>: recent changes to the way Ubuntu marks dependencies and provided names for kernel headers require that you uncomment a couple of lines in the script towards the end; just remove the # at the beginning of the lines 46 and 47.</p>

<p><strong>UPDATE</strong>: in recent Ubuntu versions, just invoking <code>apt-get autoremove --purge</code> will do the trick. Such command will remove all automatically installed packages that are no longer required in the running system. It can remove other packages beyond outdated kernels, by the way.</p>

<p><a href="/alan/tag:MostlyUnixish" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">MostlyUnixish</span></a> <a href="/alan/tag:linux" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">linux</span></a> <a href="/alan/tag:ubuntu" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">ubuntu</span></a> <a href="/alan/tag:debian" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">debian</span></a> <a href="/alan/tag:update" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">update</span></a> <a href="/alan/tag:maintenance" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">maintenance</span></a></p>
]]></content:encoded>
      <guid>https://www.franzoni.eu/alan/purging-outdated-kernels-on-systems-with-unattended-upgrades</guid>
      <pubDate>Mon, 25 Jun 2012 20:33:00 +0000</pubDate>
    </item>
  </channel>
</rss>