removing stale kernel files, a script - Slackware

This is a discussion on removing stale kernel files, a script - Slackware ; Hi there, Target audience: people who compile custom kernels for their systems, keeping up to date with latest -stable version, etc. Couple years ago I wrote a script in bash that removed kernel files and modules that were no longer ...

+ Reply to Thread
Results 1 to 2 of 2

Thread: removing stale kernel files, a script

  1. removing stale kernel files, a script

    Hi there,

    Target audience: people who compile custom kernels for their systems,
    keeping up to date with latest -stable version, etc.

    Couple years ago I wrote a script in bash that removed kernel files
    and modules that were no longer referenced by /etc/lilo.conf. The
    script had a couple problems: it was tricked by whitespace variations
    in lilo.conf, and it didn't handle the slackware distro's symlinked
    kernel.

    So I rewrote the script to address these issues.

    Typical session on slack-12.1 box (running script via NFS):

    root@pooh:~# /home/common/bin/rm-old-kernels

    rm-old-kernels:
    remove old kernel files not referenced by /etc/lilo.conf

    Reading:
    /etc/lilo.conf
    /boot
    /lib/modules

    Kernel versions found:
    /etc/lilo.conf
    2.6.25.12a
    2.6.26-git11a
    2.6.26a
    vmlinuz
    /boot symlinks
    vmlinuz -> 2.6.24.5-smp
    /boot
    2.6.24.5-smp
    2.6.25.11a
    2.6.25.12a
    2.6.26-git11a
    2.6.26a
    /lib/modules
    2.6.24.5-smp
    2.6.25.11a
    2.6.25.12a
    2.6.26-git11a
    2.6.26a

    Candidates for removal:
    # rm -f /boot/*2.6.25.11a
    # rm -rf /lib/modules/2.6.25.11a

    Do you want to delete these items (yes/No)? yes

    Removing these items:
    rm -f /boot/*2.6.25.11a
    rm -rf /lib/modules/2.6.25.11a

    Done!
    root@pooh:~# /home/common/bin/rm-old-kernels

    rm-old-kernels:
    remove old kernel files not referenced by /etc/lilo.conf

    Reading:
    /etc/lilo.conf
    /boot
    /lib/modules

    Kernel versions found:
    /etc/lilo.conf
    2.6.25.12a
    2.6.26-git11a
    2.6.26a
    vmlinuz
    /boot symlinks
    vmlinuz -> 2.6.24.5-smp
    /boot
    2.6.24.5-smp
    2.6.25.12a
    2.6.26-git11a
    2.6.26a
    /lib/modules
    2.6.24.5-smp
    2.6.25.12a
    2.6.26-git11a
    2.6.26a

    Nothing to delete, done.
    - - -
    Script is GPLv2 and available from:
    http://bugsplatter.mine.nu/bash/kernel/rm-old-kernels

    Excerpt from script comments:
    #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
    # notes
    # ``````
    # The script recognises the odd slackware kernel names like
    # 'vmlinuz-ide-2.4.33.3' from slackware-11.0 and supports slackware's
    # use of kernel name symlink for the distribution kernels.
    #
    # Not tested with initrds as I don't use them, if your initrd files follow
    # the initrd-$(uname -r) naming scheme they will be removed if obsolete,
    # feedback very welcome if this is not the case.
    #
    # Written on slackware-11.0 with gawk 3.1.5, tested on slackware-11.0 and
    # slackware-12.1 systems. This program also runs from an NFS export.
    #
    # Program must be run as root, it fails when run as normal user with a
    # (Permission denied) error when attempting to read /etc/lilo.conf.
    #
    #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
    # how it works
    # `````````````
    # Scan the /etc/lilo.conf file and collect kernel references from the
    # 'image = /boot/...' lines, this reference may be to a kernel image file
    # or to a symlink.
    #
    # Get a list of 'symlink -> kernel_version' from /boot.
    #
    # Get a list of kernel_versions from /boot, dropping duplicates.
    #
    # Get a list of kernel_versions from /lib/modules/*.
    #
    # Scan the lilo list and remove referenced items from the boot and module
    # lists. What is left in the boot and module lists are the non-referenced
    # files to be removed.
    #
    # Confirm removal of the items, then remove them.
    #

    Enjoy! Review, testing and/or feedback would be much appreciated.

    Grant.
    --
    http://bugsplatter.mine.nu/

  2. Re: removing stale kernel files, a script

    On Sat, 26 Jul 2008 13:47:57 +1000, Grant wrote:

    >Hi there,
    >
    >Target audience: people who compile custom kernels for their systems,
    >keeping up to date with latest -stable version, etc.


    Updated:

    root@pooh64:~# /home/common/bin/rm-old-kernels

    rm-old-kernels:
    remove old kernel files not referenced by /etc/lilo.conf

    Reading:
    /etc/lilo.conf
    /boot
    /lib/modules

    Kernel versions found:
    /etc/lilo.conf /boot /lib/modules
    2.6.24.5 2.6.24.5 2.6.24.5
    2.6.25.12a 2.6.25.12a 2.6.25.12a
    - 2.6.25.9a 2.6.25.9a
    - 2.6.25a 2.6.25a
    2.6.26a 2.6.26a 2.6.26a
    - 2.6.26ext4-3a 2.6.26ext4-3a
    2.6.26ext4-4a 2.6.26ext4-4a 2.6.26ext4-4a
    2.6.27-rc1a 2.6.27-rc1a 2.6.27-rc1a
    - 2.6.27-rc1ext4-4a 2.6.27-rc1ext4-4a

    Candidates for removal: 2.6.25.9a 2.6.25.9a
    2.6.25a 2.6.25a
    2.6.26ext4-3a 2.6.26ext4-3a
    2.6.27-rc1ext4-4a 2.6.27-rc1ext4-4a

    Removal: use all|y|n|q for all, yes, no or quit, default no:
    rm -f /boot/*2.6.25.9a ? all
    removed
    rm -f /boot/*2.6.25a
    rm -f /boot/*2.6.26ext4-3a
    rm -f /boot/*2.6.27-rc1ext4-4a
    rm -rf /lib/modules/2.6.25.9a
    rm -rf /lib/modules/2.6.25a
    rm -rf /lib/modules/2.6.26ext4-3a
    rm -rf /lib/modules/2.6.27-rc1ext4-4a
    Done!
    root@pooh64:~# /home/common/bin/rm-old-kernels

    rm-old-kernels:
    remove old kernel files not referenced by /etc/lilo.conf

    Reading:
    /etc/lilo.conf
    /boot
    /lib/modules

    Kernel versions found:
    /etc/lilo.conf /boot /lib/modules
    2.6.24.5 2.6.24.5 2.6.24.5
    2.6.25.12a 2.6.25.12a 2.6.25.12a
    2.6.26a 2.6.26a 2.6.26a
    2.6.26ext4-4a 2.6.26ext4-4a 2.6.26ext4-4a
    2.6.27-rc1a 2.6.27-rc1a 2.6.27-rc1a

    Nothing to remove, done.

    >Script is GPLv2 and available from:
    > http://bugsplatter.mine.nu/bash/kernel/rm-old-kernels


    Grant.
    --
    http://bugsplatter.mine.nu/

+ Reply to Thread