Friday, December 13, 2019

grub2 - Is it safe to delete the old kernels from menu.lst?


When the computer starts, GRUB shows a long list of previous kernels in addition to the usual stuff, e.g.


Ubuntu 10.10, kernel 2.6.35-23-generic
Ubuntu 10.10, kernel 2.6.35-23-generic (recovery mode)
Ubuntu 10.10, kernel 2.6.35-22-generic
Ubuntu 10.10, kernel 2.6.35-22-generic (recovery mode)
[6 more similar]
Ubuntu 10.10, memtest86+
Other operating systems:
Dell Utility Partition
Windows Vista (loader)

Is it safe to delete those extra "Ubuntu 10.10, kernel" lines from /etc/grub/menu.lst? Currently I have commented-out from the third one up to (not including) memtest86. But I wonder if they are ever needed for a particular reason?



Safe but pointless. The next time update-grub runs, they'll probably pop back up.


Kernels are installed as packages and so can be removed like other packages.
The massive benefit over just hitting on grub is you will free up hundreds of megabytes of disk space.


They used to be installed as non-auto but can now be safely removed with:


sudo apt-get autoremove

If you have manually installed kernels and various auxiliary packages, you can see the kernel versions installed with this command:


dpkg -l | awk '$2~/linux-(im|he|to).+[0-9]/ {print $2}'

And that'll show you something like this:


linux-headers-3.13.0-24
linux-headers-3.13.0-24-generic
linux-image-3.13.0-24-generic
linux-image-extra-3.13.0-24-generic

From there you can remove things using sudo apt-get purge . Just make sure you don't remove your current or latest kernel (uname -a to see what version you're on) and I agree with what other have said, keep the next newest one in case things blow up.


Here's my best effort at detecting and performing this safely:


current=$(uname -r)
kernels=( /lib/firmware/[0-9].* )
dpkg -l | awk '$2~/linux-(im|he|to).+[0-9]/ {print $2}' \
| grep -Ev "$current|${kernels[-1]##*/}" \
| xargs sudo apt-get purge

Notes: This will still trample over tricky situations. It'll remove things like linux-image-generic if you have a HWE stack installed. This may or may not be a big issue for you. Those packages (though they look important) won't cascade-delete your entire desktop. You'll be left with the current (because it works) and the highest version kernels. That may be one and the same if you've rebooted recently.


No comments:

Post a Comment

11.10 - Can't boot from USB after installing Ubuntu

I bought a Samsung series 5 notebook and a very strange thing happened: I installed Ubuntu 11.10 from a usb pen drive but when I restarted (...