# How do I remove kernels from /usr/src safely?

## M.Andersson

Seeing that my / partition was running low I saw that /usr/src used 18 % of total disc:

```

root@midgaard src # du -sh

3,7G    .

root@midgaard src # ls

linux                    linux-2.4.26-gentoo-r13  linux-2.4.28-gentoo-r8  linux-2.6.8-gentoo-r2  linux-2.6.9-gentoo-r4

linux-2.4.26-gentoo      linux-2.4.26-gentoo-r9   linux-2.6.10-gentoo-r6  linux-2.6.8-gentoo-r3  linux-2.6.9-gentoo-r9

linux-2.4.26-gentoo-r10  linux-2.4.28-gentoo-r5   linux-2.6.11-gentoo-r4  linux-2.6.8-gentoo-r8

linux-2.4.26-gentoo-r11  linux-2.4.28-gentoo-r7   linux-2.6.8-gentoo-r10  linux-2.6.9-gentoo-r1

```

The only one that I use is 

```

root@midgaard src # ls -l

total 64

lrwxr-xr-x   1 root root   23 mar 19 20:49 linux -> linux-2.6.10-gentoo-r6/

..

```

So what is the easiest way to delete everything but my current kernel, and is there any possibillity to have emerge-uD world NOT to download the latest /sys-kernel/gentoo-sources automaticlly?

Thanks in advance

Michael Andersson

----------

## NeddySeagoon

M.Andersson,

For each kernel tree you wish to delete yopu can do

```
rm-rf /usr/src/<kernel_dir>
```

You can remove them all, even the one you use, since it not the copy in /usr/src that is actually used for daya to day things. You only need a configured kernel here for things that build against the kernel, like some modules (nVida, glibc).

You can also remove the unused trees in /lib/modules, which are the module trees for the kerenls you have just deleted.

This is where modprobe looks for modules for the currently running kernel, so you must not get over enthusiastice here.

Another thing to chop is /usr/portage/distfiles, which is all the source files you have ever downloaded. Its never pruned.

You can chop it all, anything needed will be downloaded again next time.

----------

## jods

If you are not using the src files then you may delete them, these are only the uncompiled sources. The compiled kernel would be in your /boot.

To prevent download and upgrade of the latest kernel you may mask it. See 

http://www.gentoo.org/doc/en/handbook/handbook-x86.xml?part=3&chap=3#doc_chap3

Jodhbir

----------

## M.Andersson

Thanks for the quick reply, the modules was only 15M so I didn't feel the need to delete anything there.

Masking seems to work perfectly, deleting old kernels and downloaded distfiles has now freed almost 5G space  :Laughing:   :Laughing: 

----------

## tacker

I use this script

```
#!/bin/bash

KERNEL_DIR="/usr/src"

MODULES_DIR="/lib/modules"

# Protect linked and working kernel

KERNEL_LINKED=`readlink $KERNEL_DIR/linux`

KERNEL_ACTIVE="linux-`uname -r`"

echo "Active kernel: $KERNEL_ACTIVE"

echo "Linked kernel: $KERNEL_LINKED"

echo ""

# Remove kernel module files

echo "Removing kernel module files"

for SRC in `find $KERNEL_DIR -name "linux*" -type d -maxdepth 1`

do

    KERNEL_VERSION=${SRC:15}

    KERNEL_NAME=${SRC:9}

    if [[ $KERNEL_NAME = $KERNEL_ACTIVE ]]

    then

        echo "Protected (active): $KERNEL_NAME"

    elif [[ $KERNEL_NAME = $KERNEL_LINKED ]]

    then

        echo "Protected (linked): $KERNEL_NAME"

    else

        echo "Will remove : $MODULES_DIR/$KERNEL_VERSION"

        rm -r $MODULES_DIR/$KERNEL_VERSION

    fi

done

# Unmerge unused

echo ""

echo "Unmerging old kernels"

UNMERGE_ARGS=""

for PCK in `find /var/db/pkg/sys-kernel/ -name "gentoo-sources-*" -type d -maxdepth 1`

do

    PCK_NAME=${PCK:23}

    PCK_V=${PCK_NAME:15}

    PCK_V=${PCK_V%%-*}

    PCK_REV=${PCK_NAME##*-}

    KERNEL_NAME="linux-$PCK_V-gentoo-$PCK_REV"

    if [[ $KERNEL_NAME = $KERNEL_ACTIVE ]]

    then

        echo "Protected (active): $PCK_NAME"

    elif [[ $KERNEL_NAME = $KERNEL_LINKED ]]

    then

        echo "Protected (linked): $PCK_NAME"

    else

        echo "Will unmerge: $PCK_NAME"

        UNMERGE_ARGS="$PCK_NAME $UNMERGE_ARGS"

    fi

done

if [[ "x$UNMERGE_ARGS" != "x" ]]

then

    emerge --unmerge $UNMERGE_ARGS

fi

# Remove leftovers from /usr/src

echo ""

echo "Removing leftovers from /usr/src"

for SRC in `find $KERNEL_DIR -name "linux*" -type d -maxdepth 1`

do

    KERNEL_VERSION=${SRC:15}

    KERNEL_NAME=${SRC:9}

    if [[ $KERNEL_NAME = $KERNEL_ACTIVE ]]

    then

        echo "Protected (active): $KERNEL_NAME"

    elif [[ $KERNEL_NAME = $KERNEL_LINKED ]]

    then

        echo "Protected (linked): $KERNEL_NAME"

    else

        echo "Will remove : $KERNEL_DIR/$KERNEL_NAME"

        rm -r $KERNEL_DIR/$KERNEL_NAME

    fi

done
```

Last edited by tacker on Wed Jun 01, 2005 8:40 am; edited 1 time in total

----------

## downey

Isn't it better to unmerge the specific kernel versions.  I found if you only rm -rf the /usr/src directories then portage still thinks you have the older versions installed.  So I have been doing a "emerge -Cva" of everything but the last 2 kernel versions.  I.E. emerge -Cva gentoo-sources-2.6.11-r4.  The other benifit I find is it clears out the modules and document directories as well.

Is there something wrong doing it this way?  I hope not as I like keeping portage updated properly.

Hope that helps.

----------

## adsmith

unmerging will clear the original source files, but portage will not clear the files leftover from the build process, so unless you do a "make clean" or "make mrproper" or something like that, there will still be junk left behind in those directories.

----------

## downey

Thanks for the heads up adsmith.  It looks like tacker's script answered most of my questions.  The only thing that I noticed that the script doesn't do is remove the kernel files from /boot.  The only thing I have been missing is the removal of /lib/modules/ directories.  Is there any other things that need to be cleaned up that an unmerge wouldn't catch?

So far here's what I think would the steps for cleaning a kernel out:

1) emerge -Cva <kernel with version>

2) rm /boot/kernel-<kernel's version>

3) rm /boot/initd-<kernel's version>

4) rm /boot/System.map-<kernel's version>

5) rm /lib/modules/<kernel's version>

Cleaning up /usr/portage/distfiles also would be nice but I find it easier to just purge that every so often.

I miss anything?

----------

## adsmith

remove the extra lines from grub.conf?

----------

## VValdo

One other thing you can do

emerge -P gentoo-sources 

or whatever it is you use.  the -P is for Prune, which I believe deletes all but the latest version.

more here

W

----------

## nuke

Hey, nifty.  For some reason I got it into my head that gentoo-sources was slotted, but of course it's not.

FWIW, emerge -P runs make clean on a kernel tree before attempting to uninstall, so it will completely remove the tree under /usr/src.  You will need to manually get rid of old /lib/modules/, though.

----------

