Score:2

Error 'update-initramfs: 175: awk: Too many levels of symbolic links' when using APT

zw flag

I'm on Ubuntu 22.10, when trying to run apt or apt-get with {install|remove|auto-remove} I got the following error:

Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
0 upgraded, 0 newly installed, 0 to remove and 1 not upgraded.
1 not fully installed or removed.
After this operation, 0 B of additional disk space will be used.
Setting up initramfs-tools (0.140ubuntu17) ...
update-initramfs: deferring update (trigger activated)
Processing triggers for initramfs-tools (0.140ubuntu17) ...
/usr/sbin/update-initramfs: 175: awk: Too many levels of symbolic links
dpkg: error processing package initramfs-tools (--configure):
 installed initramfs-tools package post-installation script subprocess returned 
error exit status 127
Errors were encountered while processing:
 initramfs-tools
E: Sub-process /usr/bin/dpkg returned an error code (1)

the error message shows:

/usr/sbin/update-initramfs: 175: awk: Too many levels of symbolic links

so I tried to run update-initramfs separately

ya22y@IYB:~$ sudo update-initramfs -u
/usr/sbin/update-initramfs: 175: awk: Too many levels of symbolic links

to ensure file permissions

    ya22y@IYB:~$ stat ../../usr/sbin/update-initramfs
  File: ../../usr/sbin/update-initramfs
  Size: 6906        Blocks: 16         IO Block: 4096   regular file
Device: 803h/2051d  Inode: 6553706     Links: 1
Access: (0755/-rwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)

as far as I know, there's an infinite loop of file A referencing to file B leading to this error. I checked to update-initramfs code but I have no clue how to resolve this error.

EDIT I checked update-initramfs again, the error source was boot_opts

ro_boot_check()
{
        # check irrelevant inside of a chroot
        if [ ! -r /proc/mounts ] || ischroot; then
                return 0
        fi

        #shellcheck disable=SC1004
        boot_opts=$(awk '/boot/{if ((match($4, /^ro/) || match($4, /,ro/)) \
                && $2 == "/boot") print "ro"}' /proc/mounts)

        if [ -n "${boot_opts}" ]; then
                echo "W: /boot is ro mounted." >&2
                echo "W: update-initramfs: Not updating ${initramfs}" >&2
                exit 0
        fi
}

boot directory

ya22y@IYB:~$ ls ../../boot
config-5.19.0-35-generic      memtest86+.elf
efi                           memtest86+_multiboot.bin
grub                          System.map-5.19.0-35-generic
initrd.img                    vmlinuz
initrd.img-5.19.0-35-generic  vmlinuz-5.19.0-35-generic
memtest86+.bin

I tried

awk
Command 'awk' not found, but can be installed with:
sudo apt install gawk          # version 1:5.1.0-1build3, or
sudo apt install mawk          # version 1.3.4.20200120-3.1
sudo apt install original-awk  # version 2018-08-27-1

as @Bodo mentionned

ya22y@IYB:~$ ls -l ../../usr/bin/awk
lrwxrwxrwx 1 root root 12 mars   6 23:17 ../../usr/bin/awk -> /usr/bin/awk

also tried

sudo sh -c 'awk'
sh: 1: awk: Too many levels of symbolic links

so i decided to change awk to mawk inside /usr/sbin/update-initramfs

 
 #shellcheck disable=SC1004
       #changing awk to mawk 
       boot_opts=$(mawk '/boot/{if ((match($4, /^ro/) || match($4, /,ro/)) \
                && $2 == "/boot") print "ro"}' /proc/mounts)

checking

ya22y@IYB:~$ sudo update-initramfs -u
update-initramfs: Generating /boot/initrd.img-5.19.0-35-generic
ya22y@IYB:~$ 

Now i got a different error

sudo apt autoremove
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
0 upgraded, 0 newly installed, 0 to remove and 1 not upgraded.
2 not fully installed or removed.
After this operation, 0 B of additional disk space will be used.
Setting up initramfs-tools (0.140ubuntu17) ...
update-initramfs: deferring update (trigger activated)
Setting up original-awk (2018-08-27-1) ...
update-alternatives: error: cannot stat file '/usr/bin/awk': Too many levels of 
symbolic links
dpkg: error processing package original-awk (--configure):
 installed original-awk package post-installation script subprocess returned err
or exit status 2

Mainly, the awk link is broken, how can i repaire that ?

Bodo avatar
pt flag
Please [edit] your question and show the output of `type awk`. This will print something like `awk is /usr/bin/awk`. Show the output of `ls -l /usr/bin/awk` (or whatever the `type` command printed). I would expect something like ...`/usr/bin/awk -> /etc/alternatives/awk`. Repeat with the link target to get soemthing like ...`/etc/alternatives/awk -> /usr/bin/gawk`. Show all this in your question. It could also be a symlink loop for some of the directories involved.
Bodo avatar
pt flag
Please add details how you "found that awk is not found". Copy&paste the command you used and the resulting output. I suspect that the shell found a symlink loop when it tried to execute `awk` in `boot_opts=$(awk '...' /proc/mounts)`. If `type awk` shows `awk: not found` retry with `sudo type awk`.
hr flag
@Bodo `type` is a shell builtin - you'd need something like `sudo sh -c 'type awk'`
yazzy avatar
zw flag
@Bodo @steeldriver thanks for mentionning, i've edited my question, ```awk`` link is broken as the link target is ```/usr/bin/awk`` and the parent folder is ```/usr/bin/``` so its looping and thats what causing the error. How can i fix it ?
Score:1
jp flag

Manually altering files under /etc/alternatives/ will break the Ubuntu Alternatives system and result in errors ... Please see for example: error: cannot stat file '/etc/alternatives/my.cnf': Invalid argument

The default awk variant installed on Ubuntu is mawk ... You should be able to, correctly, fix the broken/unusable file/link for awk under /etc/alternatives/ by removing it first, like so:

sudo rm /etc/alternatives/awk

Then, recreating it the right way, like so:

sudo dpkg-reconfigure mawk

Notice: If gawk is installed on your system, then it will take precedence and that link should read:

$ ls -l /etc/alternatives/awk
lrwxrwxrwx 1 root root 13 Mar 17 11:44 /etc/alternatives/awk -> /usr/bin/gawk

or otherwise, it should read:

$ ls -l /etc/alternatives/awk
lrwxrwxrwx 1 root root 13 Mar 17 11:44 /etc/alternatives/awk -> /usr/bin/mawk

Also notice: That /usr/bin/awk is merely a symbolic link managed by the Ubuntu Alternatives system as well that leads back to /etc/alternatives/awk and it should be like:

$ ls -l /usr/bin/awk 
lrwxrwxrwx 1 root root 21 Jan  8  2022 /usr/bin/awk -> /etc/alternatives/awk

So, you shouldn't manually alter that one either or you will need to fix both by removing them first, like so:

$ sudo rm /etc/alternatives/awk /usr/bin/awk

Then, recreate them both the right way like so:

$ sudo dpkg-reconfigure mawk

And, afterwords, they should be fixed and look something like this:

$ ls -l /etc/alternatives/awk /usr/bin/awk 
lrwxrwxrwx 1 root root 13 Mar 17 14:02 /etc/alternatives/awk -> /usr/bin/gawk
lrwxrwxrwx 1 root root 21 Mar 17 14:02 /usr/bin/awk -> /etc/alternatives/awk
Score:0
zw flag

Simply deleting the broken link will resolve the problem.

ya22y@IYB:/usr/bin$ sudo rm awk

(Edit)

Then created a new link

sudo ln -s /etc/alternatives/awk /usr/bin/awk

Bodo avatar
pt flag
This will fix remove the recursive link, but probably there should be a different link. You could try to reinstall the `gawk` package. If there is correct link `/etc/alternatives/awk -> /usr/bin/gawk` (or to a different alternative), you can create a link `/usr/bin/awk -> /etc/alternatives/awk` by `sudo ln -s /etc/alternatives/awk /usr/bin/awk` after deleting the broken link.
yazzy avatar
zw flag
Actually i forget to mention, after removing the recursive link and running apt one more time, for some reason ```/usr/bin/awk -> /etc/alternatives/awk``` was created automatically.
Raffa avatar
jp flag
Manually altering files under `/etc/alternatives/` will break the Ubuntu Alternatives system and result in errors ... Please see for example: [error: cannot stat file '/etc/alternatives/my.cnf': Invalid argument](https://askubuntu.com/q/1458756/968501) @Bodo
Bodo avatar
pt flag
@Raffa I did not propose to modify anything **in** `/etc/alternatives` but to re-create a link pointing **to** it.
Raffa avatar
jp flag
@Bodo I know ... However, this includes `/usr/bin/awk` as well ... I thought I made it clear the first time ... Given the error message provided in the question `update-alternatives: error: cannot stat file '/usr/bin/awk' ...` ... But, clearly, I didn't :-) ... So, kindly see my updated answer now ... It should be clear now.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.