0

So i did a mistake of using Grub Customizer and then did another while trying to restore previous config by following instructions in /etc/grub.d/backup/RESTORE_INSTRUCTIONS, which led to corrupting my grub config files.

Now when i try to run sudo update-grup i get

Sourcing file `/etc/default/grub'
Sourcing file `/etc/default/grub.d/init-select.cfg'
Generating grub configuration file ...
Script `/boot/grub/grub.cfg.new' contains no commands and will do nothing
Syntax errors are detected in generated GRUB config file.
Ensure that there are no errors in /etc/default/grub
and /etc/grub.d/* files or please file a bug report with
/boot/grub/grub.cfg.new file attached.

The resulting grub.cfg.new is empty (it contains only the initial comment about it being generated).

I have tried to reinstall the grub according to this answer by

sudo apt-get purge grub-pc grub-common

but it no longer works. First the apt complained about unmet dependencies and when i added grub2-common grub-pc-bin to the list to be removed, it told me it needs to install these instead

... grub-common:i386 grub-efi-amd64 grub2-common:i386 ...

After some hesitation i accepted it, and let those packages install with the intention to replace them back aftewards, but i ran into this again during installation

Script `/boot/grub/grub.cfg.new' contains no commands and will do nothing
...
dpkg: error processing package grub-pc (--configure):
 installed grub-pc package post-installation script subprocess returned error exit status 1

So apparently the update-group is now part of the post-install script, and if it fails the package fails to install. So now i'm in a state where dependencies are broken in both ways and neither of the grub versions is properly installed and cannot be fixed.

The configuration files of the grub (/etc/default/grub and /etc/grub.d/*) were never removed and overwritten during the purge. And if i delete them manually, they are never installed again.

Is there anything i can do to save my installation of the OS now?

Youda008
  • 153
  • Could somebody who has dual-boot with Windows 10 and Ubuntu 22 possibly send me his /etc/grub.d folder? Maybe copying in the correctly formed files will help me. – Youda008 Nov 12 '22 at 23:18
  • First, run sudo apt update and sudo apt -f install to fix any loose ends. Then, run sudo apt install --reinstall -o Dpkg::Options::="--force-confmiss" grub-pc grub-common grub2-common grub-pc-bin – mchid Nov 12 '22 at 23:24
  • Also and if that fails with the same error, move the offending file sudo mv /boot/grub/grub.cfg.new /boot/grub/grubdotcfgdotnewdotbackup so that you have a backup of the file if needed. The force confmiss option in the reinstall command I provided should cause the reinstall to overwrite and/or replace any existing and/or missing configuration files. – mchid Nov 12 '22 at 23:28
  • 1
    Simply sudo rm /boot/grub/grub.cfg.new. It's empty, and contains no information. – waltinator Nov 12 '22 at 23:30
  • @mchid So sudo apt -f install ends with the same post-install script error from grub-pc. The second command ends with Internal Error, No file name for grub-pc:amd64 – Youda008 Nov 12 '22 at 23:32
  • @Youda008 See this answer to fix that error. – mchid Nov 12 '22 at 23:36
  • However, the original syntax error problem was probably something you changed in /etc/default/grub. Did you make any changes to that file or any other grub files? If so, which file did you change? You can easily download a replacement file if you know which file you need (which file you edited). – mchid Nov 12 '22 at 23:44
  • 1
    @mchid I did not manually edit anything. I just restored the files from /etc/grub.d/backup folder created by Grub Customizer as was in the RESTORE-INSTRUCTIONS, and it broke. Where can i download a replacement? I would like to see whole directory structure. – Youda008 Nov 12 '22 at 23:52
  • You can download the deb files for packages apt download packagename and then you have to extract the files. However, you should probably remove (purge) grub-customizer and get the original amd64 versions reinstalled and the i386 versions uninstalled first and foremost. Also, move or remove any grub.cfg files if needed (if it gives you an error during the reinstall). – mchid Nov 13 '22 at 00:13
  • Also, using sudo apt install --reinstall -o Dpkg::Options::="--force-confnew" packagename should replace old configuration files with new ones (of course, replace "packagename" with the actual names of the packages). – mchid Nov 13 '22 at 00:19
  • The grub.cfg.new is created if you have to typo in grub or grub scripts. It probably is best to just do a total reinstall of grub. You can chroot into it, if you cannot boot, or use Boot-Repair. Or if you can boot, sudo grub-install For Boot-Repair: https://help.ubuntu.com/community/Boot-Repair – oldfred Nov 13 '22 at 03:43
  • 1
    @mchid That command with --force-confnew ended up with the same post-install error. However, manually downloading the package file and extracting the config files from it into /etc fixed the problem and now even apt -f install finally finishes. – Youda008 Nov 13 '22 at 08:36

2 Answers2

1

apt purge is almost always the wrong solution for this kind of situation. That route is likely to remove stuff that is critical and if you remove enough stuff you'll back yourself into a corner that will require a reinstall to fix. The only time removing packages helps is if they replaced other packages that you are trying to reinstall.

Instead, I would recommend using dpkg -V to find corrupted files and permissions, and use apt install --reinstall to reinstall packages that have been corrupted. This will replace core corrupted files, but may leave config files corrupted. But it's a start.

When you find corrupted config files, you can try fixing them or renaming them, and use dpkg --reconfigure for the relevant package to try to rebuild the config files. dpkg --configure might help if the configs are completely messed up or deleted.

Another possibility is that there are extra files in the config directories that have corrupted your configuration. Again, apt purge will not remove those and won't fix anything. Finding those extra files needs to be done by process of elimination, but isn't impossible.

user10489
  • 4,051
  • 1
    You may need to add -o Dpkg::Options::="--force-confmiss" to your apt install --reinstall command in order to overwrite and replace any corrupted and/or missing configuration files. – mchid Nov 12 '22 at 23:48
  • Ooh, that sounds handy. I haven't tried that. – user10489 Nov 12 '22 at 23:49
  • 1
    and Dpkg::Options::="--force-confnew" to replace any changed configuration files with the default version. – mchid Nov 13 '22 at 00:20
  • The other neat trick is to create a git repo in /etc and selectively check in changed config files. Then you can do something like --force-confnew and use git to merge differences. Hopefully, you also check in the original so you can go back and compare if things get messed up. But this is a bit advanced and takes a lot of work to maintain. – user10489 Nov 13 '22 at 04:51
  • I found the **** problem. It's permissions. Not missing files, not bad syntax, but permissions. The files inside /etc/grub.d/backup/etc_grub_d did not have an execute flag, and the update-grub just couldn't tell me. I will write an anwer myself. – Youda008 Nov 13 '22 at 09:13
  • I'll just add that to remove all of that accidental i386 bloat, i had to run sudo dpkg -r --force-depends --force-remove-protected {all the installed packages delimited by comma}:i386 followed by sudo apt -f install – Youda008 Nov 13 '22 at 09:37
0

The problem was the permissions, not missing/corrupted files or bad syntax, but wrong permissions. The backup files created by Grub Customizer in /etc/grub.d/backup/etc_grub_d do not have the execute flag. That's why empty grub.cfg was generated. And i couldn't see it because the error message of update-grub is misleading.

So it's not enough to just follow RESTORE_INSTRUCTIONS in /etc/grub.d/backup/ and copy /etc/grub.d/backup/etc_grub_d back to /etc/grub.d. You also have to

sudo chmod +x 0* 1* 2* 3* 4*

But i also think this should be reported as issue to the package maintainers. It's not ok that successful installation of a package requires running a script that depends on breakable user config. And it's not ok that trying to temporarily uninstall and reinstall that package results in a i386 subsystem being installed.

End of rant, thanks to all who helped.

Youda008
  • 153