ICS/Lotus (mostly), Linux, Travel, Skiing, Mixology, and Random Musing of Interest

Spring Cleaning Tip: Uninstalling Unused Ubuntu Kernels

Bill Malchisky  March 21 2011 01:00:00 AM
Now that Spring is here, let's talk about cleaning up your system. If you want a technical post on Linux.... you got it. This post is chock-full of useful command-line tips and syntax to help make using your Linux desktop system a lot easier, while building your confidence. Any questions, contact me directly or leave a comment to this post. As many people are now using or testing Ubuntu, I wanted to provide some technical desktop tips to help keep your machine streamlined and allow you to administer it better. If you have either upgraded your machine over a few releases --- regardless of whether you have partitioned your hard drive --- chances are you have a lot of wasted space with older kernel files.

Each package contains source code files, drives, binaries...all combined, each package can consume up to 1GB each (depending upon the modules your system needs installed). If you are finding your drive starting to fill-up, here is a good way to tidy things up and give you some free space and even increase performance if you are below 10% free space on your / partition. With Red Hat, it does a better job of managing older kernel files when you upgrade or install new files. But Ubuntu does not and just keeps upgrading and adding the files, consuming space with no obvious way to address this. They do offer a program entitled, Computer Janitor, available under "System -> Administration" which manages non-kernel and non-system packages and is aimed at novice to intermediate users.

Note: for readers new to Linux documentation, if you see the symbol "$" prefacing a command, that indicates the command prompt symbol for your user account; in contrast, if you see a "#" prefacing a command, that implies to use utilize the root (administrator) account for that command; in other words, neither symbol is part of the command.


Step 0 - Pre-Work


First, verify your current version:
$  uname -a


Linux capri 2.6.32-26-generic #48-Ubuntu SMP Wed Nov 24 10:14:11 UTC 2010 x86_64 GNU/Linux



Write this down, or copy to a text file. Although obvious to many, you want to ensure that you KEEP this kernel, which is "2.6.32-26". As a best practice, it is also good to keep the previous kernel version also, just in-case that new hardware device you installed destroys something, you can just choose the older kernel upon rebooting and save yourself a lot of grief. Oh, and Windows does not provide this capability; another plus for Linux. (Yes, I know Windows has restore points, but completely different...may contrast that in the future.)


Step 1 - Purging Kernel Package Files


Next, let's see how many installed kernels you have on your machine:
$ dpkg -l |grep linux-headers-*
ii  linux-headers-2.6.32-22              2.6.32-22.36      Header files related to Linux kernel version
ii  linux-headers-2.6.32-22-generic      2.6.32-22.36      Linux kernel headers for version 2.6.32 on x
ii  linux-headers-2.6.32-23              2.6.32-23.37      Header files related to Linux kernel version
ii  linux-headers-2.6.32-23-generic      2.6.32-23.37      Linux kernel headers for version 2.6.32 on x
ii  linux-headers-2.6.32-24              2.6.32-24.43      Header files related to Linux kernel version
ii  linux-headers-2.6.32-24-generic      2.6.32-24.43      Linux kernel headers for version 2.6.32 on x
ii  linux-headers-2.6.32-25              2.6.32-25.45      Header files related to Linux kernel version
ii  linux-headers-2.6.32-25-generic      2.6.32-25.45      Linux kernel headers for version 2.6.32 on x
ii  linux-headers-2.6.32-26              2.6.32-26.48      Header files related to Linux kernel version
ii  linux-headers-2.6.32-26-generic      2.6.32-26.48      Linux kernel headers for version 2.6.32 on x
ii  linux-headers-generic                2.6.32.26.28      Header files related to Linux kernel version







Eleven sets of files on this machine.  Note: if you have just been purging the files in /boot to make space, you are just getting a small piece of it. This method also complicates the package removal. So, make a local script or tip file with these steps and you'll have it at your finger tips whenever you need it.

I used the Debian package manager "dpkg" to list the package files and their exact names. Then I'll use "apt-get" the APT package handling utility for the uninstall process

Package name: linux-headers-2.6.32-23
Syntax: sudo apt-get remove {package name} {package name 2} {package name N}
Command: $ sudo apt-get remove linux-headers-2.6.32-24-generic linux-headers-2.6.32-24 linux-headers-2.6.32-23-generic linux-headers-2.6.32-23 linux-headers-2.6.32-22-generic linux-headers-2.6.32-22

Important safety tip
: BEFORE you depress the enter key, triple-check that you have omitted the current kernel from this package list (the one I told you to write down). See packages with 2.6.32-26 are EXCLUDED in the package list above; also as a subtle point, in keeping with the best practice I described, I also removed the 2.6.32-25 kernel files. Use the command syntax: sudo apt-get install {package name} if you purged one too many files.

Save time by using two terminal windows then highlight the filename with your mouse in one window and return to the other window and click your middle (third) mouse button and Linux will paste the highlighted text. Depress the spacebar and proceed with the next file. This instant copy has been around since the early days of X Window environment and is a major time saver over doing similar operations in Windows (or a Mac as well).

Running the command generates descriptive output (see below) and allows you to double check your work with confidence before purging files. Also, it tells you how much space you will save. On this test machine, I previously deleted the src files (source code) and drivers on the *2.6.32-2[234] kernel files so the space regained may be less than you might see.
Reading package lists... Done
Building dependency tree      
Reading state information... Done

The following packages will be REMOVED:

linux-headers-2.6.32-22 linux-headers-2.6.32-22-generic

linux-headers-2.6.32-23 linux-headers-2.6.32-23-generic

linux-headers-2.6.32-24 linux-headers-2.6.32-24-generic

0 upgraded, 0 newly installed, 6 to remove and 11 not upgraded.

After this operation, 256MB disk space will be freed.

Do you want to continue [Y/n]?

Next step...

(Reading database ... 252424 files and directories currently installed.)

Removing linux-headers-2.6.32-22-generic ...

Removing linux-headers-2.6.32-22 ...

Removing linux-headers-2.6.32-23-generic ...

Removing linux-headers-2.6.32-23 ...

Removing linux-headers-2.6.32-24-generic ...

Removing linux-headers-2.6.32-24 ...







And in two blinks of an eye --- and faster than rebooting Windows --- all the files are gone.

You can check your disk space with the following command: df -h  --> the 'h' indicates human readable so it collapses the byte counts into units. Remove the '-h' parameter to see the full byte counts.

Before:
Filesystem            Size  Used Avail Use% Mounted on

/dev/sda2             6.5G  5.8G  361M  95% /


After:
Filesystem            Size  Used Avail Use% Mounted on

/dev/sda2             6.5G  5.7G  615M  93% /
/dev/sda2             6.5G  5.4G  807M  88% / --> post reboot; /tmp cleared



Step 2 -- Kernel File Removal


Now, got back to /boot and purge the compiled files of the equivalent version numbers and you are fine.

Typing the command, $ ls /boot from your prompt, provides the full file list in that directory:
abi-2.6.32-22-generic
abi-2.6.32-23-generic

abi-2.6.32-24-generic

abi-2.6.32-25-generic

abi-2.6.32-26-generic

config-2.6.32-22-generic
config-2.6.32-23-generic
config-2.6.32-24-generic

config-2.6.32-25-generic
config-2.6.32-26-generic
grub

initrd.img-2.6.32-22-generic

initrd.img-2.6.32-23-generic

initrd.img-2.6.32-24-generic

initrd.img-2.6.32-25-generic

initrd.img-2.6.32-26-generic

lost+found

memtest86+.bin
System.map-2.6.32-22-generic
System.map-2.6.32-23-generic

System.map-2.6.32-24-generic

System.map-2.6.32-25-generic

System.map-2.6.32-26-generic

vmcoreinfo-2.6.32-22-generic
vmcoreinfo-2.6.32-23-generic
vmcoreinfo-2.6.32-24-generic

vmcoreinfo-2.6.32-25-generic
vmcoreinfo-2.6.32-26-generic
vmlinuz-2.6.32-22-generic

vmlinuz-2.6.32-23-generic

vmlinuz-2.6.32-24-generic

vmlinuz-2.6.32-25-generic

vmlinuz-2.6.32-26-generic








From Step 1 above, we are looking to isolate only the files that match the kernel file packages we removed.

So, for the kernel packages we removed...
linux-headers-2.6.32-22 linux-headers-2.6.32-22-generic

linux-headers-2.6.32-23 linux-headers-2.6.32-23-generic

linux-headers-2.6.32-24 linux-headers-2.6.32-24-generic


We can isolate the respective files in /boot with some command-line parsing skills:
$ ls *2.6.32-2[234]*
abi-2.6.32-22-generic
abi-2.6.32-23-generic

abi-2.6.32-24-generic

config-2.6.32-22-generic

config-2.6.32-23-generic
config-2.6.32-24-generic
initrd.img-2.6.32-22-generic

initrd.img-2.6.32-23-generic

initrd.img-2.6.32-24-generic

System.map-2.6.32-22-generic
System.map-2.6.32-23-generic
System.map-2.6.32-24-generic

vmcoreinfo-2.6.32-22-generic

vmcoreinfo-2.6.32-23-generic

vmcoreinfo-2.6.32-24-generic
vmlinuz-2.6.32-22-generic
vmlinuz-2.6.32-23-generic

vmlinuz-2.6.32-24-generic








So, using the "df -h" command again, we see that before removing the /boot files, I have 31M available in that partition.


Filesystem            Size  Used Avail Use% Mounted on

/dev/sda1              92M   57M   31M  65% /boot


Note:
on my system, I have /boot in a separate disk partition. If you installed Ubuntu with the default or suggested criteria, then you will look to "/" for your mount point; your filesystem will most likely be identical.


Tip: If you want to see how much space just these files are consuming, type this command, from within the /boot directory:
$ du -hc *2.6.32-2[234]*


Then, we can just purge these files in one command, be consistent and free-up some additional space.
Tip: depressing the up-arrow key in a terminal window, allows for quick modifications and variations on the previous commands.

$ sudo rm *2.6.32-2[234]*


Note:
if you forget to preface this command with "sudo" you'll see a warning messages like this:
rm: remove write-protected regular file `abi-2.6.32-24-generic'?

Which you don't have rights to purge.

rm: cannot remove `abi-2.6.32-24-generic': Permission denied


See, here's why: -rw-r--r-- 1 root root  645813 2010-09-16 14:24 abi-2.6.32-24-generic
The file permissions prefacing the "1 root root" string indicated that unless you are root, you can only read this file.

And when you utilize the correct command, you will see the appropriate partition available space increase accordingly

Filesystem            Size  Used Avail Use% Mounted on

/dev/sda1              92M   41M   46M  47% /boot


Again, if you went with the default installation, then you will look to "/" for your free space. Now of course, the total space you recovered is based upon with kernels you have installed, and which modules are compiled into each. But this should server as a guide for a system with basic needs.

If you are ever uncertain where a directory is mounted, use this command:
df -h {full_path_to_directory}


$ df -h /usr/src

Filesystem            Size  Used Avail Use% Mounted on

/dev/sda2             6.5G  5.4G  807M  88% /


And of course, if this is your first time purging kernel files, backup /boot to increase your confidence
$ sudo mkdir /boot2; sudo cp -a /boot /boot2
 --> the "-a" indicates archive, which grabs the permissions, time-date stamp, and ownership.

Again, if you accidentally remove one too many kernel packages from Step 1, know that you can just the apt-get command to get them back:
Syntax: $ sudo apt-get install {package_name}
Command: $ sudo apt-get install linux-headers-2.6.32-22

--UPDATE
--
It is also a good idea to clean-up the grub configuration file, as the Matt Cook comment indicated. My scripts contain the update command, and wanted to include it below.
$ sudo update-grub2



Final Thoughts


Know that you should most definitely use the command line for this operation, which implies you will need to open a terminal window
(1) you can use the commands I provided in the post through a simple copy & paste operation;
(2) you can purge files with surgical precision;
(3) using a file management window with icons for deletion, allows for the introduction of human error with all of the directory tree branches, sub branches, and leaves needing removal--far riskier;
(4) the command line is a lot faster overall -- admittedly, if you are new to this approach, you will be a bit more cautious initially, as is to be expected.

Double check to make sure your current kernel file and respective packages from the version identified in Step 0 are still installed. If so, you are all set. No need to reboot, but you can if you want to ensure things are fine, after checking things of course. If you have no kernel files installed -- by accident, use the command at the end of Step 2 to repair and install those files at this time. Again, low probability of risk if you followed the precautions identified throughout the post.


Have fun cleaning-up your drive!
Comments

1Matt Cook  4/28/11 8:40:12 PM  Spring Cleaning Tip: Uninstalling Unused Ubuntu Kernels

Don't forget to remove the grub menu items for the deleted kernels.

Powered by IBM Lotus Domino 8 | Lotus User Group | Get Firefox! | This blog is listed on Planet Lotus   IBM Certified

© 2010 William Malchisky.