Skip to content

Instantly share code, notes, and snippets.

@danielfaust
Last active February 21, 2022 13:19
Show Gist options
  • Save danielfaust/442a125ff5c5b17e60519a12f10f8113 to your computer and use it in GitHub Desktop.
Save danielfaust/442a125ff5c5b17e60519a12f10f8113 to your computer and use it in GitHub Desktop.
Running this script results in the printing of a recommended "apt purge" command to remove unused kernels, in order to free up space on the boot partition. It does not modify anything, it only shows what the system has installed and what can be removed. Keywords: ubuntu boot partition full no space left
import os
import re
print('--------------')
active_kernel_version = os.popen('uname -r').read().strip().replace('-generic', '')
existing_kernels = os.popen('dpkg --list | egrep -i --color "ii (linux-image|linux-headers|linux-modules)"').read().strip()
print(existing_kernels)
print('--------------')
existing_kernels = os.popen('dpkg --list | egrep -i --color "ii (linux-image)"').read().strip()
kernel_versions = []
for match in re.finditer("linux-image-([0-9.-]+)-generic", existing_kernels, re.IGNORECASE | re.MULTILINE):
kernel_version = match.group(1)
kernel_versions.append(kernel_version)
kernel_versions = sorted(kernel_versions, reverse=False)
for index, kernel_version in enumerate(kernel_versions):
print('{index}: {version}{current}'.format(index=index, version=kernel_version, current=' (active)' if kernel_version == active_kernel_version else ''))
print('--------------')
packages = []
for kernel_version in kernel_versions[:-1]:
if kernel_version != active_kernel_version:
packages.append('linux-image-{version}-generic linux-headers-{version} linux-modules-{version}-generic'.format(version=kernel_version))
if len(packages) > 0:
command = 'sudo apt purge ' + ' '.join(packages)
print('')
print(command)
else:
print('')
print(' -> all seems to be ok, no purging recommended')
print('')
print('--------------')
"""
example output:
--------------
ii linux-headers-5.4.0-70 5.4.0-70.78 all Header files related to Linux kernel version 5.4.0
ii linux-headers-5.4.0-70-generic 5.4.0-70.78 amd64 Linux kernel headers for version 5.4.0 on 64 bit x86 SMP
ii linux-headers-5.4.0-72 5.4.0-72.80 all Header files related to Linux kernel version 5.4.0
ii linux-headers-5.4.0-72-generic 5.4.0-72.80 amd64 Linux kernel headers for version 5.4.0 on 64 bit x86 SMP
ii linux-headers-generic 5.4.0.72.75 amd64 Generic Linux kernel headers
ii linux-image-5.4.0-70-generic 5.4.0-70.78 amd64 Signed kernel image generic
ii linux-image-5.4.0-72-generic 5.4.0-72.80 amd64 Signed kernel image generic
ii linux-image-generic 5.4.0.72.75 amd64 Generic Linux kernel image
ii linux-modules-5.4.0-70-generic 5.4.0-70.78 amd64 Linux kernel extra modules for version 5.4.0 on 64 bit x86 SMP
ii linux-modules-5.4.0-72-generic 5.4.0-72.80 amd64 Linux kernel extra modules for version 5.4.0 on 64 bit x86 SMP
ii linux-modules-extra-5.4.0-70-generic 5.4.0-70.78 amd64 Linux kernel extra modules for version 5.4.0 on 64 bit x86 SMP
ii linux-modules-extra-5.4.0-72-generic 5.4.0-72.80 amd64 Linux kernel extra modules for version 5.4.0 on 64 bit x86 SMP
--------------
0: 5.4.0-70
1: 5.4.0-72 (active)
--------------
sudo apt purge linux-image-5.4.0-70-generic linux-headers-5.4.0-70 linux-modules-5.4.0-70-generic
--------------
"""
@danielfaust
Copy link
Author

Warning! While upgrading Kernels today I noticed that the ordering can be erroneous, see

0: 5.4.0-100
1: 5.4.0-96
2: 5.4.0-97
3: 5.4.0-99 (active)
--------------

sudo apt purge linux-image-5.4.0-100-generic linux-headers-5.4.0-100 linux-modules-5.4.0-100-generic linux-image-5.4.0-96-generic linux-headers-5.4.0-96 linux-modules-5.4.0-96-generic linux-image-5.4.0-97-generic linux-headers-5.4.0-97 linux-modules-5.4.0-97-generic

Where it recommends to delete 5.4.0-100 which is simply wrong.

Will fix this gist when I find some time to do it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment