Announcement

Collapse
No announcement yet.

Kernel modules left behind - an old bug no one seems to care about

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Kernel modules left behind - an old bug no one seems to care about

    When I update my system and a new kernel is installed it leaves behind module files in /lib/modules/. I'm somewhat OCD so on my main system I go there and 'rm -rf' them so I only have the kernel modules I need. Dumb really, in a way, because I'm using a 1TB drive, but still - why are these not cleaned up by the normal update? It's been reported as a bug - years ago - and Canonical seemingly doesn't care. Full disclosure - I use apt to update my system so I do not know if this is an issue when using one of the GUI package tools. The issue isn't really an apt bug because apt isn't responsible for cleaning up your system. The module files apparently fall under the category of config files, which means they are not removed with a normal apt remove or autoremove, unless you use the --purge option. Regardless, I'm tired of having to do this so went looking for solutions.


    Here's an example from my guest bedroom PC, one that's not regularly updated or often used:
    Code:
    stuart@asus-cn60:/lib/modules$ ls
    4.15.0-20-generic  4.15.0-62-generic  5.15.0-60-generic  5.15.0-78-generic  5.15.0-88-generic  5.3.0-51-generic   5.4.0-120-generic  5.4.0-48-generic  5.4.0-89-generic
    4.15.0-22-generic  4.15.0-64-generic  5.15.0-70-generic  5.15.0-79-generic  5.3.0-26-generic   5.3.0-53-generic   5.4.0-121-generic  5.4.0-77-generic  5.4.0-90-generic
    4.15.0-29-generic  4.15.0-66-generic  5.15.0-71-generic  5.15.0-82-generic  5.3.0-28-generic   5.3.0-59-generic   5.4.0-122-generic  5.4.0-80-generic  5.4.0-91-generic
    4.15.0-30-generic  5.0.0-29-generic   5.15.0-72-generic  5.15.0-83-generic  5.3.0-40-generic   5.3.0-61-generic   5.4.0-124-generic  5.4.0-81-generic  5.4.0-92-generic
    4.15.0-32-generic  5.0.0-36-generic   5.15.0-73-generic  5.15.0-84-generic  5.3.0-42-generic   5.3.0-62-generic   5.4.0-131-generic  5.4.0-84-generic
    4.15.0-33-generic  5.0.0-37-generic   5.15.0-75-generic  5.15.0-86-generic  5.3.0-45-generic   5.4.0-113-generic  5.4.0-42-generic   5.4.0-86-generic
    4.15.0-34-generic  5.15.0-58-generic  5.15.0-76-generic  5.15.0-87-generic  5.3.0-46-generic   5.4.0-117-generic  5.4.0-45-generic   5.4.0-88-generic
    ​
    Wow, right? 60 modules folders but only 2 kernels installed. Almost a gig of wasted space on a 95GB partition.

    Solutions:
    If you always use '-purge' when you update, this won't happen. And of course, being Linux, some users have created ways to correct this problem since Ubuntu has not.

    This command will remove the modules even after the kernel has been removed:

    Code:
    sudo apt purge linux-image-X.X.0-XX-generic
    Or you can install this kernel management utility 'linux-purge' to purge these files:

    To install this utility, do the following (as root). This installs the script, the manpage and bash autocompletions.

    wget https://git.launchpad.net/linux-purge/plain/update-linux-purge
    sudo chmod +x ./update-linux-purge && ./update-linux-purge

    Now, you can use the utility with the command linux-purge, and its manual page is available with man linux-purge. For instance, to purge all old kernels (that are not present inside /boot), run:

    linux-purge -b

    https://git.launchpad.net/linux-purge​

    Note: The above part about linux-purge was copied from Stack Exchange and I have not tried it. I didn't like this solution since it requires and extra step when I update and and additional program to handle what should be done by the system.

    I normally use a bash alias to upgrade:

    Code:
    alias upgrade='sudo apt full-upgrade'
    ​so I could just add '-purge' to it and that fixes it.

    I decided to try an custom apt configuration with info I stole from here.

    File: /etc/apt/apt.conf.d/99autopurge
    Code:
    APT::Get::Purge "true";
    Binary::apt::Purge "true";
    
    APT::Get::AutomaticRemove "true";
    APT::Get::Purge-Unused "true";
    Binary::apt::APT::Purge-Unused "true";
    Binary::apt::APT::AutomaticRemove "true";
    I put this on my guest computer and will see if it works.​

    Please Read Me

    #2
    A command I run more or less regularly to clean up some things is
    sudo dpkg -l | grep -E '^rc' | awk '{print \$2}' | grep -E '^linux-' | xargs sudo apt purge -y

    (I have set an alias for it, of course… )

    I am not quite sure if it does the same job, though.
    But be aware that this command will purge without asking you for any permission!

    PS: No - the command I posted does not remove the remaining modules, just other remaining stuff.
    One would actually have to compare the contents of /lib/modules/ with the currently installed kernels in a chain of commands to remove them afterwards… Or use the solution oshunluvr posted above.
    Last edited by Schwarzer Kater; Nov 03, 2023, 03:48 PM. Reason: added PS, typos
    Debian KDE & LXQt • Kubuntu & Lubuntu • openSUSE KDE • Windows • macOS X
    Desktop: Lenovo ThinkCentre M75s • Laptop: Apple MacBook Pro 13" • and others

    get rid of Snap script (20.04 +)reinstall Snap for release-upgrade script (20.04 +)
    install traditional Firefox script (22.04 +)​ • install traditional Thunderbird script (24.04)

    Comment


      #3
      FYI, on my Debian 12.1 Bookworm I only see modules for my current kernel in /lib/modules
      "A nation that is afraid to let its people judge the truth and falsehood in an open market is a nation that is afraid of its people.”
      – John F. Kennedy, February 26, 1962.

      Comment


        #4
        Hm, I just had a look in Debian 12.2 :
        I have currently installed kernels 6.5.x, 6.4.x and 6.1.0-13 LTS, but in /lib/modules/ there are still the additional kernel modules of 6.1.0-11 LTS and 6.1.0-10 LTS (I think I skipped an LTS kernel one time, but this is not the point)…
        Debian KDE & LXQt • Kubuntu & Lubuntu • openSUSE KDE • Windows • macOS X
        Desktop: Lenovo ThinkCentre M75s • Laptop: Apple MacBook Pro 13" • and others

        get rid of Snap script (20.04 +)reinstall Snap for release-upgrade script (20.04 +)
        install traditional Firefox script (22.04 +)​ • install traditional Thunderbird script (24.04)

        Comment

        Working...
        X