Latest LQ Deal: Latest LQ Deals
Go Back > Blogs > flshope
User Name


Rate this Entry

Removing accumulated old Linux kernels

Posted 01-17-2017 at 04:28 PM by flshope
Updated 01-17-2017 at 09:31 PM by flshope (fix typos, add clarification)

I have a 2008 HP 2133 Mininote PC, which is a small laptop machine. It has a 32-bit VIA C7-M Processor running at 1200 MHz. The memory is 2 GB and the single hard disk is 120 GB.

I originally purchased it with a vendor-installed SUSE distribution. I later installed Ubuntu 12.04 LTS. The machine is now at 14.04.5 LTS. I generally do weekly software updates as provided by Ubuntu.

Over the years, the machine gradually accumulated a lot of old Linux kernels, and it began to dawn on me that Ubuntu was never going to clean up the mess, which reached the number 45 with the update to kernel 3.13.0-107. So I was accumulating notes, mostly from LQ, on how to deal with this and specifically how to purge kernels.

When I did the software update that included 3.13.0-106, I screwed up and neglected to plug in the external power supply. The machine lost its battery before the update finished. When I tried to restart, the boot process crashed after the Grub screen. I guessed that the new kernel installation was corrupted and tried booting by selecting the 3.13.0-105 kernel. The machine booted OK with -105 and every application I tried seemed to work OK. I purged the -106 kernel with a 'sudo apt-get purge' command, and this step removed that kernel from the Grub kernel list. The machine's default boot then was to the -105 kernel.

A few weeks later, a software update supposedly installed 3.13.0-107, but that kernel never appeared on the Grub kernel list. I had been hoping that the next kernel update after my -106 mess would have restored the machine to the latest kernel -- apparently not.

So I decided to run 'sudo aptitude' and ask it find and fix broken packages. It reported both the -106 and -107 kernels were broken. It gave an error message about the -106, saying it couldn't find the headers; but it apparently successfully repaired the -107 because on reboot that kernel appeared on the Grub kernel list and the machine booted successfully under the -107. So, for the -106 problem, I ran

sudo apt-get install linux-headers-3.13.0-106-generic
Apparently, that was the correct thing to do because the machine was now successfully booting to the -106 kernel when selected from Grub.

By that time I had found at LQ what I believed were the proper terminal commands to remove a kernel. Here is what I ran to get rid of my collection of old kernels. To make a list of the kernel candidates for removal, I ran

apt list --installed|grep linux|less
Among that output, I could see all of the installed kernels and the associated package names. I made a composite kernel list by copying stuff from the 'less' output. To accomplish the removal for each kernel to be removed, I executed:

apt list --installed|grep 3.13.0-39
sudo apt-get remove linux-image-3.13.0-39 linux-headers-3.13.0-39
apt list --installed|grep 3.13.0-39
The first 'apt list' confirmed that the expected packages were actually installed and displayed four items for each kernel. I got the 'apt-get' command from an old LQ post by tommcd. If the removal was successful, the second 'apt list' gave no response (just a return to prompt). Of course, the '-39' was updated for each kernel to be removed.

With two of the removals, I got the message

The link /vmlinuz is a damaged link
Removing symbolic link vmlinuz 
 you may need to re-run your boot loader[grub]
The link /initrd.img is a damaged link
Removing symbolic link initrd.img 
 you may need to re-run your boot loader[grub]
That scared me. So I ran 'sudo update-grub' and then rebooted. In both cases, the boot was normal.

On this old laptop, the removal process was slow and took hours to accomplish. I think I could have stacked many package names on each 'apt-get remove' command, but I didn't try that. In the end I had removed 39 old kernels and kept the most recent 6. Some people have commented that they only keep two kernels. If I had done that with my current bumbling and mess, I would have had no GUI available to get through the recovery. If you don't have at least one kernel that will boot, I don't think you can even get a virtual console. I believe a recovery medium (e.g., a boot DVD) would have been the only option.
Posted in Uncategorized
Views 1619 Comments 0
« Prev     Main     Next »
Total Comments 0




All times are GMT -5. The time now is 07:22 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration