Why are old initrd files of uninstalled kernels filling up /boot partition?

On several Ubuntu machines I am constantly facing the problem of a full boot partition although I always do remove all old kernels. The problem seems to occur, as many initrd files are on the boot partition even though the related kernels are not actually installed. To give in example:

root@Jacobi:/boot# ls -lah
insgesamt 202M
drwxr-xr-x  3 root root 3,0K Jan 30 10:03 .
drwxr-xr-x 25 root root 4,0K Jan 30 10:03 ..
-rw-r--r--  1 root root 1,2M Dez 11 15:36 abi-4.4.0-104-generic
-rw-r--r--  1 root root 1,2M Jan  9 22:28 abi-4.4.0-109-generic
-rw-r--r--  1 root root 1,2M Jan 19 14:06 abi-4.4.0-112-generic
-rw-r--r--  1 root root 187K Dez 11 15:36 config-4.4.0-104-generic
-rw-r--r--  1 root root 187K Jan  9 22:28 config-4.4.0-109-generic
-rw-r--r--  1 root root 187K Jan 19 14:06 config-4.4.0-112-generic
drwxr-xr-x  5 root root 1,0K Jan 30 10:03 grub
-rw-r--r--  1 root root  10M Jan 30 10:03 initrd.img-3.13.0-39-generic
-rw-r--r--  1 root root  10M Jan 30 10:02 initrd.img-4.4.0-101-generic
-rw-r--r--  1 root root  10M Jan 30 10:02 initrd.img-4.4.0-103-generic
-rw-r--r--  1 root root  38M Jan 30 10:02 initrd.img-4.4.0-104-generic
-rw-r--r--  1 root root  38M Jan 30 10:02 initrd.img-4.4.0-109-generic
-rw-r--r--  1 root root  10M Jan 30 10:03 initrd.img-4.4.0-38-generic
-rw-r--r--  1 root root  10M Jan 30 10:03 initrd.img-4.4.0-45-generic
-rw-r--r--  1 root root  10M Jan 30 10:02 initrd.img-4.4.0-59-generic
-rw-r--r--  1 root root  10M Jan 30 10:02 initrd.img-4.4.0-77-generic
-rw-r--r--  1 root root  10M Jan 30 10:02 initrd.img-4.4.0-78-generic
-rw-r--r--  1 root root  10M Jan 30 10:02 initrd.img-4.4.0-81-generic
-rw-r--r--  1 root root 179K Jan 28  2016 memtest86+.bin
-rw-r--r--  1 root root 181K Jan 28  2016 memtest86+.elf
-rw-r--r--  1 root root 181K Jan 28  2016 memtest86+_multiboot.bin
-rw-------  1 root root 3,8M Dez 11 15:36 System.map-4.4.0-104-generic
-rw-------  1 root root 3,8M Jan  9 22:28 System.map-4.4.0-109-generic
-rw-------  1 root root 3,8M Jan 19 14:06 System.map-4.4.0-112-generic
-rw-------  1 root root 6,8M Dez 11 15:36 vmlinuz-4.4.0-104-generic
-rw-------  1 root root 6,8M Jan  9 22:28 vmlinuz-4.4.0-109-generic
-rw-------  1 root root 6,8M Jan 19 14:06 vmlinuz-4.4.0-112-generic

But only linux-image-4.4.0-104-generic and linux-image-4.4.0-109-generic are installed:

root@Jacobi:/boot# dpkg -l linux-image-\* | grep ^ii
ii  linux-image-4.4.0-104-generic       4.4.0-104.127 amd64        Linux kernel image for version 4.4.0 on 64 bit x86 SMP
ii  linux-image-4.4.0-109-generic       4.4.0-109.132 amd64        Linux kernel image for version 4.4.0 on 64 bit x86 SMP
ii  linux-image-extra-4.4.0-104-generic 4.4.0-104.127 amd64        Linux kernel extra modules for version 4.4.0 on 64 bit x86 SMP
ii  linux-image-extra-4.4.0-109-generic 4.4.0-109.132 amd64        Linux kernel extra modules for version 4.4.0 on 64 bit x86 SMP

If I delete the unnecessary initrd files manually they will be generated again as soon as I try to do an upgrade.

What could be the cause of this issue and how can I get rid of those files permanently?


Solution 1:

You should check partially removed kernels with

dpkg -l linux-image-\* | grep ^rc

and remove them with for example sudo apt-get purge linux-image-4.4.0-101-generic.

Purging will remove initramfs generation rules from /var/lib/initramfs-tools/.

If it does not help, you can remove them manually from initramfs list:

sudo rm /var/lib/initramfs-tools/3.13.0-39-generic
sudo rm /var/lib/initramfs-tools/4.4.0-101-generic
sudo rm /var/lib/initramfs-tools/4.4.0-103-generic
sudo rm /var/lib/initramfs-tools/4.4.0-38-generic
sudo rm /var/lib/initramfs-tools/4.4.0-45-generic
sudo rm /var/lib/initramfs-tools/4.4.0-59-generic
sudo rm /var/lib/initramfs-tools/4.4.0-77-generic
sudo rm /var/lib/initramfs-tools/4.4.0-78-generic
sudo rm /var/lib/initramfs-tools/4.4.0-81-generic

Usually I run purge-old-kernels followed by sudo apt-get autoremove to have only 2 recent kernels.

You can reinstall installed kernels with their initramfses:

sudo apt-get install --reinstall \
$(dpkg -l linux-image-\* | grep ^ii | awk '{print $2}')

Solution 2:

If you have already used dpkg to purge the kernels / headers and if you have already checked dpkg -l and still don't see the kernels / headers installed there, but you still see references to these old kernels in /boot in the form of initrd-img files, then the proper way to purge these references and files is with the update-initramfs command.

For example, if you only have 4.4.0-109 installed, but you still see the following in /boot:

-rw-r--r--  1 root root  10M Jan 30 10:02 initrd.img-4.4.0-103-generic
-rw-r--r--  1 root root  38M Jan 30 10:02 initrd.img-4.4.0-104-generic
-rw-r--r--  1 root root  38M Jan 30 10:02 initrd.img-4.4.0-109-generic

You can safely remove 4.4.0-104 and 4.4.0-103 from /boot with the following commands:

$ sudo update-initramfs -d -k 4.4.0-103-generic
$ sudo update-initramfs -d -k 4.4.0-104-generic
$ sudo update-initramfs -c -k all

The first two commands delete the references to those kernels in initramfs generation rules as well as the files in /boot. The last command tells initramfs to regenerate the initrd.img files based on the updated rules.

Theoretically you could also use

$ sudo update-initramfs -d -k 4.4.0-{103,104}-generic

to delete multiple kernels at once, but for some reason this didn't work for me.