Search code examples
pythonlinuxkernelgentoo

How to get kernel version by using kernel name (not current kernel)


Simply I want to clean older kernel's modules. It's "uname -r" but I need to get such information for all kernels with Python (I already know their names and can clean kernel files, initramfs and System.map). if that is possible ...

Thank you.


Solution

  • The uname command reports on the running kernel, so it won't help you. But the modules are all stored under /lib/modules. The following program can clean them all out.

    #!/usr/bin/python2
    
    import os
    import shutil
    
    moddirs = os.listdir("/lib/modules")
    moddirs.remove(os.uname()[2])
    
    for d in moddirs:
        shutil.rmtree(os.path.join("/lib/modules", d))