[WBEL-users] Removing old kernel files

Erik Williamson erik@cpsc.ucalgary.ca
Wed, 18 Aug 2004 14:29:31 -0600


This is a multi-part message in MIME format.
--------------050209080504040605000603
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit

Hi David,

I've attached a script that I deploy to our fedora and whitebox machines 
  to take care of old kernels (we do it automatically on our 
workstations, manually on servers).

It's worked well for us so far, but the usual warranty applies - if it 
breaks your system, I'm sorry... but that's all!

It does checks to make sure that it's not removing the currently running 
kernel.  You'll probably want to remove the nvidia section (we use the 
nvidia rpms from livna.org on our fedora boxes).

If you find any mistakes in this, please let me know!

Best,
Erik.

david wrote:
> Folks
> 
> Since the original "respin-1", a revised and upgraded Kernel has 
> appeared and works just fine.  All my systems have been rebooted and are 
> using this new version.  To the best of my knowledge:
> 
> Old kernel: 2.4.21-15.EL
> New Kernel: 2.4.21-15.0.4.EL
> 
> Ignoring the trailing ".EL", these names sort very nicely as 
> alphanumeric strings.
> 
> However, I am somewhat cramped for space, and want to reclaim the space 
> used by material I no longer use, like the old Kernel stuff.
> 
> Is there some syntax in "yum" or "rpm" that will do this for me, safely?
> 
> Thanks
> 
> David
> 
> _______________________________________________
> Whitebox-users mailing list
> Whitebox-users@beau.org
> http://beau.org/mailman/listinfo/whitebox-users

-- 
e r i k   w i l l i a m s o n                     erik@cpsc.ucalgary.ca
  system admin . department of computer science . university of calgary


--------------050209080504040605000603
Content-Type: text/x-sh;
 name="kernel-cleaner.sh"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="kernel-cleaner.sh"

#!/bin/bash

#VERBOSE=true
VERBOSE=false
TEST=
#TEST=--test
TEMPFILE=/tmp/.kernels.installed.$$

function f1 () {
    # Get list of installed kernels
    rpm -q --last kernel${SMP:+-smp} > ${TEMPFILE}

    if [ "X$VERBOSE" == "Xtrue" ] ; then 
	echo -n "All Installed "
	if [ "X$SMP" == "Xtrue" ] ; then 
	    echo -n "SMP"
	else
	    echo -n "UP"
	fi
	echo " kernels:"
	cat ${TEMPFILE}
	echo ""
    fi

    # Find out how many there were
    NUM_KERNELS=`wc -l ${TEMPFILE} | awk '{ print $1 }'`

    # Subtract one; we don't want to see the latest kernel.
    NUM_KERNELS=`expr $NUM_KERNELS - 1`

    # Now list all the old kernels.
    OLD_KERNELS=`tail -$NUM_KERNELS ${TEMPFILE} | awk '{ print $1 }'`

    for OLD_KERNEL in $OLD_KERNELS ; do
        if [ "X$VERBOSE" == "Xtrue" ] ; then echo -n "Looking to remove" $OLD_KERNEL "..."; fi

        if [ $OLD_KERNEL == $RUNNING_KERNEL ] ; then
            if [ "X$VERBOSE" == "Xtrue" ] ; then echo "Ignoring (currently running)"; fi
        else
	    # Find out if there's a corresponding nvidia driver installed for this kernel version. 
	    NVIDIA_VER=`rpm -q --queryformat "%{VERSION}-%{RELEASE}" $OLD_KERNEL`

	    # If there is, remove it. 
	    [ "X$NVIDIA_VER" != "X" ] && rpm --quiet $TEST -e `rpm -qa | grep $NVIDIA_VER | grep nvidia`

	    # Now remove the old kernel
            rpm --quiet -e $TEST $OLD_KERNEL
            if [ "X$VERBOSE" == "Xtrue" ] ; then echo "OK."; fi
        fi
    done
}

# Determine what kernel rpm corresponds to the running kernel.
UNAME=`uname -r`
if echo $UNAME | grep -q smp ; then 
    RUNNING_KERNEL=kernel-smp-${UNAME%%smp}
else
    RUNNING_KERNEL=kernel-${UNAME}
fi

if [ "X$VERBOSE" == "Xtrue" ] ; then 
    echo "Currently running kernel:" $RUNNING_KERNEL; 
    echo ""; 
    echo "               ** Scanning Uni-processor Kernels **"
fi

f1

if [ "X$VERBOSE" == "Xtrue" ] ; then 
    echo ""
    echo "              ** Scanning Multi-processor Kernels **"
fi

SMP=true
f1

rm -rf ${TEMPFILE}

--------------050209080504040605000603--