Kernel – Can’t Remove or Install Kernel

aptgrub-efikernel

I accidentally deleted my /boot partition but I was able to get it back with grub-install

I'm trying to chroot and install the kernels again via this guide:How to restore a system after accidentally removing all kernels?

However when I tried to remove or install the package I get the following error:

root@ubuntu:/# apt-get install -f

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following package was automatically installed and is no longer required:
  thermald
Use 'apt-get autoremove' to remove it.
The following packages will be REMOVED:
  linux-image-extra-4.2.0-25-generic
0 upgraded, 0 newly installed, 1 to remove and 33 not upgraded.
2 not fully installed or removed.
After this operation, 162 MB disk space will be freed.
Do you want to continue? [Y/n] y
(Reading database ... 411666 files and directories currently installed.)
Removing linux-image-extra-4.2.0-25-generic (4.2.0-25.30) ...
depmod: FATAL: could not load /boot/System.map-4.2.0-25-generic: No such file or directory
run-parts: executing /etc/kernel/postinst.d/apt-auto-removal 4.2.0-25-generic /boot/vmlinuz-4.2.0-25-generic
run-parts: executing /etc/kernel/postinst.d/dkms 4.2.0-25-generic /boot/vmlinuz-4.2.0-25-generic
Running in chroot, ignoring request.
run-parts: executing /etc/kernel/postinst.d/initramfs-tools 4.2.0-25-generic /boot/vmlinuz-4.2.0-25-generic
update-initramfs: Generating /boot/initrd.img-4.2.0-25-generic
mv: ‘/boot/initrd.img-4.2.0-25-generic.new’ and ‘/boot/initrd.img-4.2.0-25-generic’ are the same file
run-parts: /etc/kernel/postinst.d/initramfs-tools exited with return code 1
dpkg: error processing package linux-image-extra-4.2.0-25-generic (--remove):
 subprocess installed post-removal script returned error exit status 1
Errors were encountered while processing:
 linux-image-extra-4.2.0-25-generic
E: Sub-process /usr/bin/dpkg returned an error code (1)

Update: It seems to be stuck on the following command

root@ubuntu:/# update-initramfs -c -t -k 4.2.0-25-generic -b /boot
update-initramfs: Generating /boot/initrd.img-4.2.0-25-generic
mv: ‘/boot/initrd.img-4.2.0-25-generic.new’ and ‘/boot/initrd.img-4.2.0-25-generic’ are the same file

root@ubuntu:/# ls -l /boot

ubuntu@ubuntu:~$ ls -l /boot

total 5430
-rw-r--r-- 1 root root 1268815 Apr 17  2015 abi-3.19.0-15-generic
-rw-r--r-- 1 root root  177656 Apr 17  2015 config-3.19.0-15-generic
drwxr-xr-x 1 root root      60 Jan 20 21:39 grub
-rw-r--r-- 1 root root  164216 Mar  6  2015 memtest86+.bin
-rw-r--r-- 1 root root  165892 Mar  6  2015 memtest86+.elf
-rw-r--r-- 1 root root  166396 Mar  6  2015 memtest86+_multiboot.bin
-rw------- 1 root root 3615358 Apr 17  2015 System.map-3.19.0-15-generic

root@ubuntu:/# apt-get install –reinstall linux-image-4.2.0-25-generic

Reading package lists... Done
Building dependency tree       
Reading state information... Done
0 upgraded, 0 newly installed, 1 reinstalled, 0 to remove and 33 not upgraded.
2 not fully installed or removed.
After this operation, 0 B of additional disk space will be used.
E: Internal Error, No file name for linux-image-4.2.0-25-generic:amd64

root@ubuntu:/# dpkg –configure linux-image-4.2.0-25-generic

Setting up linux-image-4.2.0-25-generic (4.2.0-25.30) ...
Running depmod.
update-initramfs: deferring update (hook will be called later)
initrd.img(/boot/initrd.img-4.2.0-25-generic
) points to /boot/initrd.img-4.2.0-25-generic
 (/boot/initrd.img-4.2.0-25-generic) -- doing nothing at /var/lib/dpkg/info/linux-image-4.2.0-25-generic.postinst line 491.
vmlinuz(/boot/vmlinuz-4.2.0-25-generic
) points to /boot/vmlinuz-4.2.0-25-generic
 (/boot/vmlinuz-4.2.0-25-generic) -- doing nothing at /var/lib/dpkg/info/linux-image-4.2.0-25-generic.postinst line 491.
Examining /etc/kernel/postinst.d.
run-parts: executing /etc/kernel/postinst.d/apt-auto-removal 4.2.0-25-generic /boot/vmlinuz-4.2.0-25-generic
run-parts: executing /etc/kernel/postinst.d/dkms 4.2.0-25-generic /boot/vmlinuz-4.2.0-25-generic
Running in chroot, ignoring request.
run-parts: executing /etc/kernel/postinst.d/initramfs-tools 4.2.0-25-generic /boot/vmlinuz-4.2.0-25-generic
update-initramfs: Generating /boot/initrd.img-4.2.0-25-generic
mv: ‘/boot/initrd.img-4.2.0-25-generic.new’ and ‘/boot/initrd.img-4.2.0-25-generic’ are the same file
run-parts: /etc/kernel/postinst.d/initramfs-tools exited with return code 1
Failed to process /etc/kernel/postinst.d at /var/lib/dpkg/info/linux-image-4.2.0-25-generic.postinst line 1025.
dpkg: error processing package linux-image-4.2.0-25-generic (--configure):
 subprocess installed post-installation script returned error exit status 2
Errors were encountered while processing:
 linux-image-4.2.0-25-generic

How can I fix this?

Best Answer

There was a hint above at not being able to process something about postinst.d so I looked at what scripts it was running and found them.

I did a rm -rf /var/lib/dpkg/info/linux-image-4.2.0-25-generic.* which had some .postrm and .list files that were being used and those were faulty for whatever reason. I removed them, ran dpkg --configure -a and no more reinstall errors! Yay. Thanks all for pointing me in the right direction. There was another hint about reinstalling grub which I did with Boot Repair and after that I was able to pinpoint better where things were broken.

Related Question