Removing Old Kernel Images from /lib/modules

So using my handy-dandy ‘HDGraph for Linux’ command, I recently identified an Ubuntu (10.04.4 LTS) server that had a bloated /lib/modules directory.

me@there:/lib/modules$ ls -al
total 140
drwxr-xr-x 33 root root  4096 2017-06-02 18:10 .
drwxr-xr-x 13 root root 12288 2015-04-02 06:42 ..
drwxr-xr-x  4 root root  4096 2014-05-29 12:25 2.6.32-41-generic
drwxr-xr-x  4 root root  4096 2014-05-29 12:25 2.6.32-42-generic
drwxr-xr-x  4 root root  4096 2014-05-29 12:25 2.6.32-43-generic
drwxr-xr-x  4 root root  4096 2014-05-29 12:25 2.6.32-44-generic
drwxr-xr-x  4 root root  4096 2014-05-29 12:25 2.6.32-45-generic
drwxr-xr-x  4 root root  4096 2014-05-29 12:25 2.6.32-46-generic
drwxr-xr-x  4 root root  4096 2014-05-29 12:26 2.6.32-47-generic
drwxr-xr-x  4 root root  4096 2014-05-29 12:26 2.6.32-48-generic
drwxr-xr-x  4 root root  4096 2014-05-29 12:26 2.6.32-49-generic
drwxr-xr-x  4 root root  4096 2014-05-29 12:26 2.6.32-50-generic
drwxr-xr-x  4 root root  4096 2014-05-29 12:26 2.6.32-51-generic
drwxr-xr-x  4 root root  4096 2014-05-29 12:26 2.6.32-52-generic
drwxr-xr-x  4 root root  4096 2014-05-29 12:27 2.6.32-53-generic
drwxr-xr-x  4 root root  4096 2014-05-29 12:27 2.6.32-54-generic
drwxr-xr-x  4 root root  4096 2014-05-29 12:27 2.6.32-55-generic
drwxr-xr-x  4 root root  4096 2014-05-29 12:27 2.6.32-56-generic
drwxr-xr-x  4 root root  4096 2014-05-29 12:27 2.6.32-57-generic
drwxr-xr-x  4 root root  4096 2014-05-29 12:27 2.6.32-58-generic
drwxr-xr-x  4 root root  4096 2015-01-16 16:55 2.6.32-60-generic
drwxr-xr-x  4 root root  4096 2015-01-16 16:55 2.6.32-61-generic
drwxr-xr-x  4 root root  4096 2015-01-16 16:55 2.6.32-62-generic
drwxr-xr-x  4 root root  4096 2015-01-16 16:55 2.6.32-64-generic
drwxr-xr-x  4 root root  4096 2015-01-16 16:56 2.6.32-65-generic
drwxr-xr-x  4 root root  4096 2015-01-16 16:56 2.6.32-66-generic
drwxr-xr-x  4 root root  4096 2015-01-16 16:56 2.6.32-67-generic
drwxr-xr-x  4 root root  4096 2015-01-16 16:56 2.6.32-68-generic
drwxr-xr-x  4 root root  4096 2015-01-16 16:56 2.6.32-70-generic
drwxr-xr-x  4 root root  4096 2017-03-29 16:08 2.6.32-71-generic
drwxr-xr-x  4 root root  4096 2017-03-29 16:08 2.6.32-72-generic
drwxr-xr-x  4 root root  4096 2017-03-29 16:08 2.6.32-73-generic
drwxr-xr-x  4 root root  4096 2015-04-30 06:52 2.6.32-74-generic

These files are most likely on your system because you have automatic apt-get updates enabled, which is a good thing, but disk space is important also. It should be fine to remove the files, but files that are related to system updates, especially anything that mentions the ‘kernel’ make me nervous, how about you?

After a small amount of googling to see whether it was safe to just delete these files or not, I think I found the ‘correct’ way to clean them up.

One guy suggested using ‘sudo apt-get autoremove’ but this didn’t help at all.

Another forum thread had a suggestion to use the dpkg command with the -r or ‘remove’ parameter. This not only removes the large file itself but also marks the debian package that created the file as ‘removed’ (and it probably does some other cool stuff that will stop you having problems later on also).

If you are anything like me, you will want to understand things a little bit better before blindly copying and pasting commands that you have never seen before. So here goes:

To list all installed packages:
me@there:/lib/modules$ sudo dpkg-query -l | grep linux-image

To show only the relevant entries:

me@there:/lib/modules$ sudo dpkg-query -l | grep linux-image
rc  linux-image-2.6.32-40-generic   2.6.32-40.87                        Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-41-generic   2.6.32-41.91                        Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-42-generic   2.6.32-42.96                        Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-43-generic   2.6.32-43.97                        Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-44-generic   2.6.32-44.98                        Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-45-generic   2.6.32-45.104                       Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-46-generic   2.6.32-46.108                       Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-47-generic   2.6.32-47.109                       Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-48-generic   2.6.32-48.110                       Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-49-generic   2.6.32-49.111                       Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-50-generic   2.6.32-50.112                       Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-51-generic   2.6.32-51.113                       Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-52-generic   2.6.32-52.114                       Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-53-generic   2.6.32-53.115                       Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-54-generic   2.6.32-54.116                       Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-55-generic   2.6.32-55.117                       Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-56-generic   2.6.32-56.118                       Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-57-generic   2.6.32-57.119                       Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-58-generic   2.6.32-58.121                       Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-60-generic   2.6.32-60.122                       Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-61-generic   2.6.32-61.124                       Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-62-generic   2.6.32-62.126                       Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-64-generic   2.6.32-64.128                       Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-65-generic   2.6.32-65.131                       Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-66-generic   2.6.32-66.132                       Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-67-generic   2.6.32-67.134                       Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-68-generic   2.6.32-68.135                       Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-70-generic   2.6.32-70.137                       Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-71-generic   2.6.32-71.138                       Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-72-generic   2.6.32-72.139                       Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-73-generic   2.6.32-73.141                       Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-74-generic   2.6.32-74.142                       Linux kernel image for version 2.6.32 on x86
ii  linux-image-generic             2.6.32.74.81                        Generic Linux kernel image

Now that we can see all the relevant info, let’s try deleting the oldest package:

me@there:/lib/modules$ sudo dpkg -r linux-image-2.6.32-41-generic 
(Reading database ... 148158 files and directories currently installed.)
Removing linux-image-2.6.32-41-generic ...
Running postrm hook script /usr/sbin/update-grub.
Generating grub.cfg ...
Found linux image: /boot/vmlinuz-2.6.32-74-generic
Found initrd image: /boot/initrd.img-2.6.32-74-generic
Found linux image: /boot/vmlinuz-2.6.32-73-generic
Found initrd image: /boot/initrd.img-2.6.32-73-generic
Found linux image: /boot/vmlinuz-2.6.32-72-generic
Found initrd image: /boot/initrd.img-2.6.32-72-generic
Found linux image: /boot/vmlinuz-2.6.32-71-generic
Found initrd image: /boot/initrd.img-2.6.32-71-generic
Found linux image: /boot/vmlinuz-2.6.32-70-generic
Found initrd image: /boot/initrd.img-2.6.32-70-generic
Found linux image: /boot/vmlinuz-2.6.32-68-generic
Found initrd image: /boot/initrd.img-2.6.32-68-generic
Found linux image: /boot/vmlinuz-2.6.32-67-generic
Found initrd image: /boot/initrd.img-2.6.32-67-generic
Found linux image: /boot/vmlinuz-2.6.32-66-generic
Found initrd image: /boot/initrd.img-2.6.32-66-generic
Found linux image: /boot/vmlinuz-2.6.32-65-generic
Found initrd image: /boot/initrd.img-2.6.32-65-generic
Found linux image: /boot/vmlinuz-2.6.32-64-generic
Found initrd image: /boot/initrd.img-2.6.32-64-generic
Found linux image: /boot/vmlinuz-2.6.32-62-generic
Found initrd image: /boot/initrd.img-2.6.32-62-generic
Found linux image: /boot/vmlinuz-2.6.32-61-generic
Found initrd image: /boot/initrd.img-2.6.32-61-generic
Found linux image: /boot/vmlinuz-2.6.32-60-generic
Found initrd image: /boot/initrd.img-2.6.32-60-generic
Found linux image: /boot/vmlinuz-2.6.32-58-generic
Found initrd image: /boot/initrd.img-2.6.32-58-generic
Found linux image: /boot/vmlinuz-2.6.32-57-generic
Found initrd image: /boot/initrd.img-2.6.32-57-generic
Found linux image: /boot/vmlinuz-2.6.32-56-generic
Found initrd image: /boot/initrd.img-2.6.32-56-generic
Found linux image: /boot/vmlinuz-2.6.32-55-generic
Found initrd image: /boot/initrd.img-2.6.32-55-generic
Found linux image: /boot/vmlinuz-2.6.32-54-generic
Found initrd image: /boot/initrd.img-2.6.32-54-generic
Found linux image: /boot/vmlinuz-2.6.32-53-generic
Found initrd image: /boot/initrd.img-2.6.32-53-generic
Found linux image: /boot/vmlinuz-2.6.32-52-generic
Found initrd image: /boot/initrd.img-2.6.32-52-generic
Found linux image: /boot/vmlinuz-2.6.32-51-generic
Found initrd image: /boot/initrd.img-2.6.32-51-generic
Found linux image: /boot/vmlinuz-2.6.32-50-generic
Found initrd image: /boot/initrd.img-2.6.32-50-generic
Found linux image: /boot/vmlinuz-2.6.32-49-generic
Found initrd image: /boot/initrd.img-2.6.32-49-generic
Found linux image: /boot/vmlinuz-2.6.32-48-generic
Found initrd image: /boot/initrd.img-2.6.32-48-generic
Found linux image: /boot/vmlinuz-2.6.32-47-generic
Found initrd image: /boot/initrd.img-2.6.32-47-generic
Found linux image: /boot/vmlinuz-2.6.32-46-generic
Found initrd image: /boot/initrd.img-2.6.32-46-generic
Found linux image: /boot/vmlinuz-2.6.32-45-generic
Found initrd image: /boot/initrd.img-2.6.32-45-generic
Found linux image: /boot/vmlinuz-2.6.32-44-generic
Found initrd image: /boot/initrd.img-2.6.32-44-generic
Found linux image: /boot/vmlinuz-2.6.32-43-generic
Found initrd image: /boot/initrd.img-2.6.32-43-generic
Found linux image: /boot/vmlinuz-2.6.32-42-generic
Found initrd image: /boot/initrd.img-2.6.32-42-generic
Found memtest86+ image: /boot/memtest86+.bin
done

Now the file should be gone from /lib/modules. This can be confirmed by running ls -alh /lib/modules.

If you run the sudo dpkg-query -l | grep linux-image command again you will notice that the package still shows up, but it now has an ‘rc’ listed next to it rather than an ‘ii’. This means that rather than having an installed status, the package status is now ‘Config-Files’ and the desired action is ‘Remove’ rather than ‘Install’.

Now just repeat the process for each remaining file in the folder to clean up as much disk space as you require. It might be prudent to leave the few most recent ones just in case you need to switch between kernel versions later or something along those lines (I really don’t know what the best practise is here, please let me know in the comments if you do).

To check your disk is now nice and healthy of course you can use df -h.

Enjoy! 🙂

HDGraph Equivalent for *nix Command Line

We all run low or run out of disk space every now and then. In fact, I’ve found as a sysadmin that this is one of the more frequent and annoying problems that pop up. Especially if you don’t have an easy way to figure out what is using all of that disk space.

I use HDGraph on Windows systems to do this and find it really helpful. I couldn’t live without it. Well I could, but I would have no storage left. There are some similar tools available for Linux desktops and Mac OS X also but on a headless Linux server all you have is the command line and your wits.

After a lot of googling, I found this great little command will help you find large files/folders on a Linux system. Well perhaps little is not the best choice of words, and it’s not super user friendly, but it does a great job.

The path in the command can be changed to focus on a specific folder. So to be very general you could start at / and then work your way into the biggest folder until you figure out where all that disk space has gone.

Show top 10 largest files or sub-folders in /var:
sudo du -a /var | sort -n -r | head -n 10

Show top 20 largest files or subfolders in the root folder /:
sudo du -a / | sort -n -r | head -n 20

Show the 11th-20th largest files in /var/log/:
sudo du -a /var/log/ | sort -n -r | head -n 20 | tail -n 10

The beauty of this command is that it uses standard utilities that are included with almost all *nix based operating systems including Linux and Mac OS X.

If you get this error then your hard drive is most likely 100% full and you may need to do some manual cleaning up first before you can start using the above command.
sort: write failed: /tmp/sort0uH5NO: No space left on device

Take a quick look in your home folder and in the log folders to see if there are a couple of files that you could do without. You should only need to clear a few KB of space before our handy-dandy command will have enough room to work with.

Home Directory:
cd /home/[username] or simply cd ~
rm someoldfile.1

Log Folders:
cd /var/log/
rm someoldlog.5.gz