The actual answer to this question is a little more annoying than I anticipated. Yes, yes, this is dangerous, and the maintainers make it exceptionally hard to do this for precisely this reason. But if you're trying to automate your infrastructure to do something like this, I trust you actually know what you're doing. In my case, it's because I'm trying to purge the (currently running) kernel inside another script which verifies that there is indeed a newer kernel available.
By checking through the output of debconf-get-selections
, there is indeed an option to preselect this value for you:
$ sudo debconf-get-selections | grep -B1 linux-base
# Abort kernel removal?
linux-base linux-base/removing-running-kernel boolean true
If you set this to "false" using debconf-set-selections
, however, nothing changes. You are still prompted.
This let to some digging into exactly what is calling this. It turns out, at least on relatively current (Buster) versions of Debian and likely Ubuntu, there is a separate Perl script called linux-check-removal
whos purpose seems to be to ignore, reset, and then prompt for this warning.
http://manpages.ubuntu.com/manpages/cosmic/man1/linux-check-removal.1.html
This script is called from the prerm
dpkg
script for the given kernel, e.g.
$ cat /var/lib/dpkg/info/linux-image-4.19.0-13-amd64.prerm
#!/bin/sh -e
version=4.19.0-13-amd64
image_path=/boot/vmlinuz-$version
if [ "$1" != remove ]; then
exit 0
fi
linux-check-removal $version
if [ -d /etc/kernel/prerm.d ]; then
DEB_MAINT_PARAMS="$*" run-parts --report --exit-on-error --arg=$version
--arg=$image_path /etc/kernel/prerm.d
fi
exit 0
And verifying its operation:
$ sudo linux-check-removal $(uname -r)
# "Yes" selected
E: Aborting removal of the running kernel
$ echo $?
1
$ sudo linux-check-removal $(uname -r)
# "No" selected
W: Removing the running kernel
$ echo $?
0
So, the actual solution is to override this command with something that just returns 0 instead.
However, despite the command using a relative call, the $PATH
inside this script when called from dpkg
does not include /usr/local
directories. So a simple override of the script in /usr/local/bin
, as you might expect to be able to do, does not work. To demonstrate, I modified the prerm
to print the $PATH
and which
values and then called the apt remove
command:
Removing linux-image-4.19.0-13-amd64 (4.19.160-2) ...
PATH: /usr/sbin:/usr/bin:/sbin:/bin
WHICH: /usr/bin/linux-check-removal
W: Removing the running kernel
Thus, we are forced to temporarily move the real /usr/bin/linux-check-removal
out of the way and install a dummy placeholder script that just returns 0 instead. The simplest way I can think to do this would be:
$ sudo mv /usr/bin/linux-check-removal /usr/bin/linux-check-removal.orig
$ echo -e '#!/bin/sh\necho "Overriding default linux-check-removal script!"\nexit 0' | sudo tee /usr/bin/linux-check-removal
$ sudo chmod +x /usr/bin/linux-check-removal
You can probably remove the echo
line if you wish, but I include it for the demonstration.
Now you can successfully purge the running kernel version non-interactively:
$ sudo apt purge -y linux-image-4.19.0-13-amd64
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
linux-headers-4.19.0-12-amd64 linux-headers-4.19.0-12-common linux-image-4.19.0-12-amd64
Use 'sudo apt autoremove' to remove them.
The following packages will be REMOVED:
linux-image-4.19.0-13-amd64*
0 upgraded, 0 newly installed, 1 to remove and 0 not upgraded.
After this operation, 270 MB disk space will be freed.
(Reading database ... 104395 files and directories currently installed.)
Removing linux-image-4.19.0-13-amd64 (4.19.160-2) ...
Overriding default linux-check-removal script!
I: /vmlinuz.old is now a symlink to boot/vmlinuz-4.19.0-12-amd64
I: /initrd.img.old is now a symlink to boot/initrd.img-4.19.0-12-amd64
/etc/kernel/postrm.d/initramfs-tools:
update-initramfs: Deleting /boot/initrd.img-4.19.0-13-amd64
/etc/kernel/postrm.d/zz-update-grub:
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-4.19.0-14-amd64
Found initrd image: /boot/initrd.img-4.19.0-14-amd64
Found linux image: /boot/vmlinuz-4.19.0-12-amd64
Found initrd image: /boot/initrd.img-4.19.0-12-amd64
done
(Reading database ... 99993 files and directories currently installed.)
Purging configuration files for linux-image-4.19.0-13-amd64 (4.19.160-2) ...
And then restore the original linux-check-removal
:
$ sudo mv /usr/bin/linux-check-removal.orig /usr/bin/linux-check-removal
sudo apt remove linux-image-$(uname -r)
) and I did get the dialog you mentioned. I moved to NO with the arrow key and pressed enter and the kernel packages were uninstalled. To install it again, I had to runsudo apt install linux-image-$(uname -r) linux-modules-extra-$(uname -r)
because the modules package got removed but didn't automatically get pulled in. I wonder why you want to uninstall the running kernel though. – Zanna Nov 05 '18 at 09:15