I agree with cavsfan that /etc/kernel/postinst.d/apt-auto-removal should not be triggered when you delete a kernel. What is happening is that /etc/apt/apt.conf.d/01autoremove-kernels is being set to prevent kernel[0] and kernel[-2] from being autoremoved, instead of kernel[0] and kernel[-1].
Take a recent situation. After the installation of kernel 3.19.0-28, the kernels 3.19.0-25, 3.19.0-26, and 3.19.0-28 are installed, and /etc/apt/apt.conf.d/01autoremove-kernels is set to prevent 3.19.0-26 and 3.19.0-28 from being removed.
When you run the autoremove in this situation, 3.19.0-25 is removed. If /etc/kernel/postinst.d/apt-auto-removal is triggered during this removal, it will mark as not-for-autoremoval (according to the comments at the top of apt-auto-removal):
* the currently booted version - this will be 3.19.0-28
* the kernel version we've been called for - this will be 3.19.0-25 (which is the kernel we've just removed - why are we marking for non-auto-removal a kernel which has already been removed?)
* the latest kernel version - this will be 3.19.0-28
* the second-latest kernel version, if the booted kernel version is already the latest and this script is called for that same version - this doesn't apply, as this script isn't being called for the latest version, it's being called for the version we're deleting.
The result is that /etc/apt/apt.conf.d/01autoremove-kernels is set to prevent 3.19.0-25 and 3.19.0-28 from being autoremoved, while the system actually contains 3.19.0-26 and 3.19.0-28. Autoremove will, in this case, want to remove 3.19.0-26, leaving only 3.19.0-28.
I think the fact that having a kernel removal trigger /etc/kernel/postinst.d/apt-auto-removal will result in marking for non-auto-removal a kernel that is already removed alone indicates that something is not being done correctly. Either don't trigger /etc/kernel/postinst.d/apt-auto-removal on a kernel remove, or tweak /etc/kernel/postinst.d/apt-auto-removal to handle the fact that the kernel it's being called for is being deleted, not added (perhaps don't do criteria 2 (the kernel version we've been called for) and do criteria 4 (tweak criteria 4's conditions to have it apply on a deletion).
I agree with cavsfan that /etc/kernel/ postinst. d/apt-auto- removal should not be triggered when you delete a kernel. What is happening is that /etc/apt/ apt.conf. d/01autoremove- kernels is being set to prevent kernel[0] and kernel[-2] from being autoremoved, instead of kernel[0] and kernel[-1].
Take a recent situation. After the installation of kernel 3.19.0-28, the kernels 3.19.0-25, 3.19.0-26, and 3.19.0-28 are installed, and /etc/apt/ apt.conf. d/01autoremove- kernels is set to prevent 3.19.0-26 and 3.19.0-28 from being removed.
When you run the autoremove in this situation, 3.19.0-25 is removed. If /etc/kernel/ postinst. d/apt-auto- removal is triggered during this removal, it will mark as not-for-autoremoval (according to the comments at the top of apt-auto-removal):
* the currently booted version - this will be 3.19.0-28
* the kernel version we've been called for - this will be 3.19.0-25 (which is the kernel we've just removed - why are we marking for non-auto-removal a kernel which has already been removed?)
* the latest kernel version - this will be 3.19.0-28
* the second-latest kernel version, if the booted kernel version is already the latest and this script is called for that same version - this doesn't apply, as this script isn't being called for the latest version, it's being called for the version we're deleting.
The result is that /etc/apt/ apt.conf. d/01autoremove- kernels is set to prevent 3.19.0-25 and 3.19.0-28 from being autoremoved, while the system actually contains 3.19.0-26 and 3.19.0-28. Autoremove will, in this case, want to remove 3.19.0-26, leaving only 3.19.0-28.
I think the fact that having a kernel removal trigger /etc/kernel/ postinst. d/apt-auto- removal will result in marking for non-auto-removal a kernel that is already removed alone indicates that something is not being done correctly. Either don't trigger /etc/kernel/ postinst. d/apt-auto- removal on a kernel remove, or tweak /etc/kernel/ postinst. d/apt-auto- removal to handle the fact that the kernel it's being called for is being deleted, not added (perhaps don't do criteria 2 (the kernel version we've been called for) and do criteria 4 (tweak criteria 4's conditions to have it apply on a deletion).