UPDATE 11/17/15: Another nice command to auto purge old kernels is: sudo apt-get autoremove
Also, removing old kernels is easy with sudo dpkg -r linux-image-3.2.0-83-generic
—————————
Recently I wanted to install a new package on a Ubuntu server. Typically this is as simple as issuing a
sudo apt-get install package-name
But this time around, I got a interesting error:
$ sudo apt-get install vsftpd Reading package lists... Done Building dependency tree Reading state information... Done You might want to run 'apt-get -f install' to correct these: The following packages have unmet dependencies: linux-image-server : Depends: linux-image-3.0.0-28-server but it is not going to be installed E: Unmet dependencies. Try 'apt-get -f install' with no packages (or specify a solution).
I started poking around and found that the /boot partition is full:
$ df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/server-root 36G 8.0G 26G 24% / udev 2.0G 8.0K 2.0G 1% /dev tmpfs 793M 236K 793M 1% /run none 5.0M 0 5.0M 0% /run/lock none 2.0G 0 2.0G 0% /run/shm /dev/sda1 228M 228M 0M 100% /boot
Ok, that is starting to make a bit more sense now… so we need to purge old kernel packages to free up space on the /boot partition. The first step is to identify the kernel version we are currently on so we do not delete that. Secondly, it was recommended to me that you keep the oldest kernel as it was the one the system was installed with. We can see the kernel version with:
$ uname -a Linux server 3.0.0-25-server #41-Ubuntu SMP Mon Aug 13 18:18:27 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux
So we are on 3.0.0-25-server and need to make sure not to delete that. A handy command to get a list of all the kernels you are not using is:
dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d'
I attempted to remove old kernels the “nice” way — by letting apt handle the removal:
$ sudo apt-get -y purge linux-headers-3.0.0-12-server Reading package lists... Done Building dependency tree Reading state information... Done You might want to run 'apt-get -f install' to correct these: The following packages have unmet dependencies: linux-image-server : Depends: linux-image-3.0.0-28-server but it is not going to be installed E: Unmet dependencies. Try 'apt-get -f install' with no packages (or specify a solution).
But it failed. Following the above instructions to run sudo apt-get -f install, it failed saying that there was not enough disk space on /boot (duh!). So much for being nice.
Inside the /boot partition there are three “types” of files — abi-kernel-version, config-kernel-version, initrd.img-kernel-version, System.map-kernel-version, vmcoreinfo-kernel-version, and vmlinuz-kernel-version. There will be a file for each of these for each version of the kernel you have installed. For example: vmlinuz-3.0.0-28-server. Leaving the earliest kernel version and the version I am running (reported by uname -a), I moved the other kernel files off to another location where there was ample space. It looked something like this:
$ sudo mv abi-3.0.0-16-server config-3.0.0-16-server initrd.img-3.0.0-16-server System.map-3.0.0-16-server vmcoreinfo-3.0.0-16-server vmlinuz-3.0.0-16-server /home/tnscweb/boot/
As you can see this is moving the files for kernel version 3.0.0-16 off the /boot partition.
If the boot partition just needed a bit of space freed up, you can now likely use apt-get to purge the other kernels “cleanly”. What I mean by that is apt-get also removes the kernel files from /lib/modules. You could do this by hand as well. I am not sure if it does anything beyond cleaning up /boot and /lib/modules, but I do not believe it does.