Last active
February 21, 2022 13:19
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
-------------- | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Warning! While upgrading Kernels today I noticed that the ordering can be erroneous, see
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.