dpkg-query: error: package 'grub-legacy-ec2' is not installed

root@sites:~# apt-get update
Reading package lists... Done
root@sites:~# apt-get upgrade
Reading package lists... Done
Building dependency tree
Reading state information... Done
Calculating upgrade... Done
The following packages were automatically installed and are no longer required:
  linux-modules-4.15.0-62-generic linux-modules-4.15.0-64-generic linux-modules-4.15.0-65-generic
  linux-modules-4.15.0-66-generic linux-modules-4.15.0-70-generic
Use 'apt autoremove' to remove them.
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
2 not fully installed or removed.
After this operation, 0 B of additional disk space will be used.
Do you want to continue? [Y/n] y
Setting up linux-image-4.15.0-72-generic (4.15.0-72.81) ...
Setting up linux-image-unsigned-5.4.2-050402-generic (5.4.2-050402.201912042231) ...
Processing triggers for linux-image-4.15.0-72-generic (4.15.0-72.81) ...
/etc/kernel/postinst.d/initramfs-tools:
update-initramfs: Generating /boot/initrd.img-4.15.0-72-generic
cryptsetup: WARNING: failed to detect canonical device of /dev/sda1
cryptsetup: WARNING: could not determine root device from /etc/fstab
/etc/kernel/postinst.d/x-grub-legacy-ec2:
Searching for GRUB installation directory ... found: /boot/grub
Searching for default file ... Generating /boot/grub/default file and setting the default boot entry to 0
Testing for an existing GRUB menu.lst file ...

Could not find /boot/grub/menu.lst file. Would you like /boot/grub/menu.lst generated for you? (y/N) /usr/sbin/update-grub-legacy-ec2: line 1101: read: read error: 0: Bad file descriptor
run-parts: /etc/kernel/postinst.d/x-grub-legacy-ec2 exited with return code 1
dpkg: error processing package linux-image-4.15.0-72-generic (--configure):
 installed linux-image-4.15.0-72-generic package post-installation script subprocess returned error exit status 1
Processing triggers for linux-image-unsigned-5.4.2-050402-generic (5.4.2-050402.201912042231) ...
/etc/kernel/postinst.d/initramfs-tools:
update-initramfs: Generating /boot/initrd.img-5.4.2-050402-generic
cryptsetup: WARNING: failed to detect canonical device of /dev/sda1
cryptsetup: WARNING: could not determine root device from /etc/fstab
/etc/kernel/postinst.d/x-grub-legacy-ec2:
Searching for GRUB installation directory ... found: /boot/grub
Searching for default file ... Generating /boot/grub/default file and setting the default boot entry to 0
Testing for an existing GRUB menu.lst file ...

Could not find /boot/grub/menu.lst file. Would you like /boot/grub/menu.lst generated for you? (y/N) /usr/sbin/update-grub-legacy-ec2: line 1101: read: read error: 0: Bad file descriptor
run-parts: /etc/kernel/postinst.d/x-grub-legacy-ec2 exited with return code 1
dpkg: error processing package linux-image-unsigned-5.4.2-050402-generic (--configure):
 installed linux-image-unsigned-5.4.2-050402-generic package post-installation script subprocess returned error exit status 1
Errors were encountered while processing:
 linux-image-4.15.0-72-generic
 linux-image-unsigned-5.4.2-050402-generic
E: Sub-process /usr/bin/dpkg returned an error code (1)

root@sites:/boot/grub# cp * backup
cp: -r not specified; omitting directory 'backup'
cp: -r not specified; omitting directory 'fonts'
cp: -r not specified; omitting directory 'i386-pc'
cp: -r not specified; omitting directory 'locale'
root@sites:/boot/grub# rm menu.lst
root@sites:/boot/grub# rm default
root@sites:/boot/grub# mv /etc/kernel/postrm.d/x-grub-legacy-ec2 backup
root@sites:/boot/grub# dpkg --configure -a
Setting up linux-image-4.15.0-72-generic (4.15.0-72.81) ...
Setting up linux-image-unsigned-5.4.2-050402-generic (5.4.2-050402.201912042231) ...
Processing triggers for linux-image-4.15.0-72-generic (4.15.0-72.81) ...
/etc/kernel/postinst.d/initramfs-tools:
update-initramfs: Generating /boot/initrd.img-4.15.0-72-generic
cryptsetup: WARNING: failed to detect canonical device of /dev/sda1
cryptsetup: WARNING: could not determine root device from /etc/fstab
/etc/kernel/postinst.d/x-grub-legacy-ec2:
Searching for GRUB installation directory ... found: /boot/grub
Searching for default file ... Generating /boot/grub/default file and setting the default boot entry to 0
/usr/sbin/update-grub-legacy-ec2: line 556: grub-set-default: command not found
run-parts: /etc/kernel/postinst.d/x-grub-legacy-ec2 exited with return code 127
dpkg: error processing package linux-image-4.15.0-72-generic (--configure):
 installed linux-image-4.15.0-72-generic package post-installation script subprocess returned error exit status 1
Processing triggers for linux-image-unsigned-5.4.2-050402-generic (5.4.2-050402.201912042231) ...
/etc/kernel/postinst.d/initramfs-tools:
update-initramfs: Generating /boot/initrd.img-5.4.2-050402-generic
cryptsetup: WARNING: failed to detect canonical device of /dev/sda1
cryptsetup: WARNING: could not determine root device from /etc/fstab
/etc/kernel/postinst.d/x-grub-legacy-ec2:
Searching for GRUB installation directory ... found: /boot/grub
Searching for default file ... Generating /boot/grub/default file and setting the default boot entry to 0
/usr/sbin/update-grub-legacy-ec2: line 556: grub-set-default: command not found
run-parts: /etc/kernel/postinst.d/x-grub-legacy-ec2 exited with return code 127
dpkg: error processing package linux-image-unsigned-5.4.2-050402-generic (--configure):
 installed linux-image-unsigned-5.4.2-050402-generic package post-installation script subprocess returned error exit status 1
Errors were encountered while processing:
 linux-image-4.15.0-72-generic
 linux-image-unsigned-5.4.2-050402-generic
root@sites:/boot/grub#

 dpkg -l | grep grub
ic  grub-common                                2.02-2ubuntu8.14                                         amd64        GRand Unified Bootloader (common files)
ic  grub-pc                                    2.02-2ubuntu8.14                                         amd64        GRand Unified Bootloader, version 2 (PC/BIOS version)

Can't remove or install or anything... I'm kinda stuck.

root@sites:/# apt install --reinstall initramfs-tools
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
  linux-modules-4.15.0-62-generic linux-modules-4.15.0-64-generic linux-modules-4.15.0-65-generic
  linux-modules-4.15.0-66-generic linux-modules-4.15.0-70-generic
Use 'apt autoremove' to remove them.
The following packages will be REMOVED:
  linux-image-4.15.0-62-generic linux-image-4.15.0-64-generic linux-image-4.15.0-65-generic linux-image-4.15.0-66-generic
  linux-image-4.15.0-70-generic
0 upgraded, 0 newly installed, 1 reinstalled, 5 to remove and 0 not upgraded.
7 not fully installed or removed.
Need to get 9,568 B of archives.
After this operation, 41.9 MB disk space will be freed.
Do you want to continue? [Y/n] y
Get:1 http://us.archive.ubuntu.com/ubuntu bionic-updates/main amd64 initramfs-tools all 0.130ubuntu3.9 [9,568 B]
Fetched 9,568 B in 0s (56.6 kB/s)
(Reading database ... 211144 files and directories currently installed.)
Removing linux-image-4.15.0-62-generic (4.15.0-62.69) ...
/etc/kernel/postrm.d/initramfs-tools:
update-initramfs: Deleting /boot/initrd.img-4.15.0-62-generic
/etc/kernel/postrm.d/x-grub-legacy-ec2:
Searching for GRUB installation directory ... found: /boot/grub
Searching for default file ... found: /boot/grub/default
Testing for an existing GRUB menu.lst file ... found: /boot/grub/menu.lst
Searching for splash image ... none found, skipping ...
Found kernel: /boot/vmlinuz-5.4.2-050402-generic
Found kernel: /boot/vmlinuz-5.2.14-050214-generic
Found kernel: /boot/vmlinuz-4.15.0-72-generic
Found kernel: /boot/vmlinuz-4.15.0-70-generic
dpkg-query: error: package 'grub-legacy-ec2' is not installed
run-parts: /etc/kernel/postrm.d/x-grub-legacy-ec2 exited with return code 10
dpkg: error processing package linux-image-4.15.0-62-generic (--remove):
 installed linux-image-4.15.0-62-generic package post-removal script subprocess returned error exit status 1
Removing linux-image-4.15.0-64-generic (4.15.0-64.73) ...
/var/lib/dpkg/info/linux-image-4.15.0-64-generic.postrm ... removing pending trigger
/etc/kernel/postrm.d/initramfs-tools:
update-initramfs: Deleting /boot/initrd.img-4.15.0-64-generic
/etc/kernel/postrm.d/x-grub-legacy-ec2:
Searching for GRUB installation directory ... found: /boot/grub
Searching for default file ... found: /boot/grub/default
Testing for an existing GRUB menu.lst file ... found: /boot/grub/menu.lst
Searching for splash image ... none found, skipping ...
Found kernel: /boot/vmlinuz-5.4.2-050402-generic
Found kernel: /boot/vmlinuz-5.2.14-050214-generic
Found kernel: /boot/vmlinuz-4.15.0-72-generic
Found kernel: /boot/vmlinuz-4.15.0-70-generic
dpkg-query: error: package 'grub-legacy-ec2' is not installed
run-parts: /etc/kernel/postrm.d/x-grub-legacy-ec2 exited with return code 10
dpkg: error processing package linux-image-4.15.0-64-generic (--remove):
 installed linux-image-4.15.0-64-generic package post-removal script subprocess returned error exit status 1
Removing linux-image-4.15.0-65-generic (4.15.0-65.74) ...
/var/lib/dpkg/info/linux-image-4.15.0-65-generic.postrm ... removing pending trigger
/etc/kernel/postrm.d/initramfs-tools:
update-initramfs: Deleting /boot/initrd.img-4.15.0-65-generic
/etc/kernel/postrm.d/x-grub-legacy-ec2:
Searching for GRUB installation directory ... found: /boot/grub
Searching for default file ... found: /boot/grub/default
Testing for an existing GRUB menu.lst file ... found: /boot/grub/menu.lst
Searching for splash image ... none found, skipping ...
Found kernel: /boot/vmlinuz-5.4.2-050402-generic
Found kernel: /boot/vmlinuz-5.2.14-050214-generic
Found kernel: /boot/vmlinuz-4.15.0-72-generic
Found kernel: /boot/vmlinuz-4.15.0-70-generic
dpkg-query: error: package 'grub-legacy-ec2' is not installed
run-parts: /etc/kernel/postrm.d/x-grub-legacy-ec2 exited with return code 10
dpkg: error processing package linux-image-4.15.0-65-generic (--remove):
 installed linux-image-4.15.0-65-generic package post-removal script subprocess returned error exit status 1
Removing linux-image-4.15.0-66-generic (4.15.0-66.75) ...
/var/lib/dpkg/info/linux-image-4.15.0-66-generic.postrm ... removing pending trigger
/etc/kernel/postrm.d/initramfs-tools:
update-initramfs: Deleting /boot/initrd.img-4.15.0-66-generic
/etc/kernel/postrm.d/x-grub-legacy-ec2:
Searching for GRUB installation directory ... found: /boot/grub
Searching for default file ... found: /boot/grub/default
Testing for an existing GRUB menu.lst file ... found: /boot/grub/menu.lst
Searching for splash image ... none found, skipping ...
Found kernel: /boot/vmlinuz-5.4.2-050402-generic
Found kernel: /boot/vmlinuz-5.2.14-050214-generic
Found kernel: /boot/vmlinuz-4.15.0-72-generic
Found kernel: /boot/vmlinuz-4.15.0-70-generic
dpkg-query: error: package 'grub-legacy-ec2' is not installed
run-parts: /etc/kernel/postrm.d/x-grub-legacy-ec2 exited with return code 10
dpkg: error processing package linux-image-4.15.0-66-generic (--remove):
 installed linux-image-4.15.0-66-generic package post-removal script subprocess returned error exit status 1
Removing linux-image-4.15.0-70-generic (4.15.0-70.79) ...
/var/lib/dpkg/info/linux-image-4.15.0-70-generic.postrm ... removing pending trigger
/etc/kernel/postrm.d/initramfs-tools:
update-initramfs: Deleting /boot/initrd.img-4.15.0-70-generic
/etc/kernel/postrm.d/x-grub-legacy-ec2:
Searching for GRUB installation directory ... found: /boot/grub
Searching for default file ... found: /boot/grub/default
Testing for an existing GRUB menu.lst file ... found: /boot/grub/menu.lst
Searching for splash image ... none found, skipping ...
Found kernel: /boot/vmlinuz-5.4.2-050402-generic
Found kernel: /boot/vmlinuz-5.2.14-050214-generic
Found kernel: /boot/vmlinuz-4.15.0-72-generic
Found kernel: /boot/vmlinuz-4.15.0-70-generic
dpkg-query: error: package 'grub-legacy-ec2' is not installed
run-parts: /etc/kernel/postrm.d/x-grub-legacy-ec2 exited with return code 10
dpkg: error processing package linux-image-4.15.0-70-generic (--remove):
 installed linux-image-4.15.0-70-generic package post-removal script subprocess returned error exit status 1
Errors were encountered while processing:
 linux-image-4.15.0-62-generic
 linux-image-4.15.0-64-generic
 linux-image-4.15.0-65-generic
 linux-image-4.15.0-66-generic
 linux-image-4.15.0-70-generic
E: Sub-process /usr/bin/dpkg returned an error code (1)

Solution 1:

Safety first. Make backup, make backup, make backup.

sudo rm /boot/grub/menu.lst 

and sudo rm /etc/kernel/postrm.d/x-grub-legacy-ec2, also

sudo rm /boot/grub/default

Then try sudo dpkg --configure -a No ,error?

 sudo apt install --reinstall grub-common grub-gfxpayload-lists  grub-pc grub-pc-bin grub2-common

Afteer that, we install the boot code in the MBR sudo dpkg-reconfigure grub-pc If you have only one harddrive /dev/sda is the right place.