Fix your broken or old portage in Gentoo, fix the emerge command

Using Gentoo for servers and chroots it might happen to deal with old and not regularly updated Gentoo environment, which in some cases is completely non-updatable using the regular process with emerge command. And no emerge means no installs from the Gentoo packaging system. Or you maybe have a broken portage after failed update with missing emerge command or missing or broken portage modules

In general, this article is for those, who cannot update their “sys-apps/portage” or even the emerge command exits with errors and is of no use.

In cases such as:

  • Missing portage files (broken portage)
  • Broken emerge command (broken portage)
  • no update of new portage is possible – very very old portage, which does not support EAPI of all current “sys-apps/portage” (now is EAPI=5) and you do not have old portage ebuild files
  • Easy update the portage to the newest in an old Gentoo system.

A real world errors are included at the bottom for each case above.

You need just a working python 2.7.x and git to clone “https://anongit.gentoo.org/git/proj/portage.git” or at least some tool to download the portage package.

Unpack to a globally accessible directory, DO NOT USE “/root“, because emerge changes user to non-privileges and needs execute permission for other for the all the path to the directory of emerge. Look below for more details.

Our example is to repair a completely broken portage with error:

srv ~ # emerge -va portage
Traceback (most recent call last):
  File "/usr/lib/python-exec/python2.7/emerge", line 42, in <module>
    import portage
ImportError: No module named portage

STEP 1) Clone the portage package from the git or download the portage tarball.

If you do not have a git client you can always check the URL in the sys-app/portage ebuild and download the URL. Still, if you cannot find the tarball’s URL, you can just simply get a mirror such as https://mirror.leaseweb.com/gentoo/distfiles, wait for the loading of the page (12Mbytes, it’s huge, all Gentoo tarball files’ names are here) and grep in it by the name “portage”. Or you can check here https://dev.gentoo.org/~zmedico/portage/archives/. Download the latest tarball and unpack it in the /opt directory.
Using git is the easiest method, but it is the same with downloading and unpacking the tarball in the “/opt”.

srv opt # git clone https://anongit.gentoo.org/git/proj/portage.git
Cloning into 'portage'...
remote: Enumerating objects: 151654, done.
remote: Counting objects: 100% (151654/151654), done.
remote: Compressing objects: 100% (39915/39915), done.
remote: Total 151654 (delta 112542), reused 149864 (delta 111349)
Receiving objects: 100% (151654/151654), 22.38 MiB | 550.00 KiB/s, done.
Resolving deltas: 100% (112542/112542), done.
srv opt # cd portage/
srv portage # ls -al
total 232
drwxr-xr-x. 11 root root  4096 Oct  6 04:52 .
drwx------.  4 root root  4096 Oct  6 04:51 ..
drwxr-xr-x.  6 root root  4096 Oct  6 04:52 bin
drwxr-xr-x.  5 root root  4096 Oct  6 04:52 cnf
-rw-r--r--.  1 root root  1840 Oct  6 04:52 COPYING
-rw-r--r--.  1 root root  6646 Oct  6 04:52 DEVELOPING
drwxr-xr-x.  5 root root  4096 Oct  6 04:52 doc
-rw-r--r--.  1 root root   200 Oct  6 04:52 .editorconfig
drwxr-xr-x.  7 root root  4096 Oct  6 04:52 .git
-rw-r--r--.  1 root root    80 Oct  6 04:52 .gitignore
drwxr-xr-x.  4 root root  4096 Oct  6 04:52 lib
-rw-r--r--.  1 root root 18092 Oct  6 04:52 LICENSE
-rwxr-xr-x.  1 root root  1272 Oct  6 04:52 make.conf.example-repatch.sh
drwxr-xr-x.  3 root root  4096 Oct  6 04:52 man
-rw-r--r--.  1 root root   290 Oct  6 04:52 MANIFEST.in
drwxr-xr-x.  2 root root  4096 Oct  6 04:52 misc
-rw-r--r--.  1 root root 17845 Oct  6 04:52 NEWS
-rw-r--r--.  1 root root     0 Oct  6 04:52 .portage_not_installed
-rw-r--r--.  1 root root  2246 Oct  6 04:52 README
-rw-r--r--.  1 root root 68122 Oct  6 04:52 RELEASE-NOTES
drwxr-xr-x.  6 root root  4096 Oct  6 04:52 repoman
-rwxr-xr-x.  1 root root  4889 Oct  6 04:52 runtests
-rwxr-xr-x.  1 root root 19743 Oct  6 04:52 setup.py
drwxr-xr-x.  2 root root  4096 Oct  6 04:52 src
-rwxr-xr-x.  1 root root    81 Oct  6 04:52 tabcheck.py
-rw-r--r--.  1 root root  1864 Oct  6 04:52 TEST-NOTES
-rw-r--r--.  1 root root   571 Oct  6 04:52 testpath
-rw-r--r--.  1 root root   348 Oct  6 04:52 tox.ini
-rw-r--r--.  1 root root   584 Oct  6 04:52 .travis.yml

This is the latest portage with working emerge command in the bin sub-directory.

STEP 2) Use the emerge command from the portage git/tarball to update or reinstall portage.

srv ~ # /opt/portage/bin/emerge --sync
>>> Syncing repository 'gentoo' into '/var/db/repos/gentoo'...
 * Using keys from /usr/share/openpgp-keys/gentoo-release.asc
 * Refreshing keys via WKD ...                                                                                                                                       [ ok ]
>>> Starting rsync with rsync://91.186.30.235/gentoo-portage...
>>> Checking server timestamp ...
Welcome to boobie.gentoo.org / rsync.gentoo.org
.....
.....
srv ~ # /opt/portage/bin/emerge -v portage

These are the packages that would be merged, in order:

Calculating dependencies... done!
[ebuild   R    ] sys-apps/portage-2.3.76::gentoo  USE="(ipc) native-extensions rsync-verify xattr -build -doc -epydoc -gentoo-dev (-selinux)" PYTHON_TARGETS="python2_7 python3_6 (-pypy) -python3_5 (-python3_7)" 0 KiB

Total: 1 package (1 reinstall), Size of downloads: 0 KiB


>>> Verifying ebuild manifests
>>> Running pre-merge checks for sys-apps/portage-2.3.76
 * Determining the location of the kernel source code
 * Unable to find kernel sources at /usr/src/linux
 * Please make sure that /usr/src/linux points at your running kernel, 
 * (or the kernel you wish to build against).
 * Alternatively, set the KERNEL_DIR environment variable to the kernel sources location
 * Unable to calculate Linux Kernel version for build, attempting to use running version
 * Checking for suitable kernel configuration options...                                                                                                             [ ok ]

>>> Emerging (1 of 1) sys-apps/portage-2.3.76::gentoo
 * portage-2.3.76.tar.bz2 BLAKE2B SHA512 size ;-) ...                                                                                                                [ ok ]
>>> Unpacking source...
>>> Unpacking portage-2.3.76.tar.bz2 to /var/tmp/portage/sys-apps/portage-2.3.76/work
....
....

The portage in your system will be reinstalled or upgraded and it will work. Of course, you might need to sync a new portage tree before the actual emerging of the sys-app/portage package as we have shown here.

Real world examples

Fix your portage with the errors like:

  • Missing portage files (broken portage)
    srv ~ # emerge -va portage
    
    These are the packages that would be merged, in order:
    
    Calculating dependencies... done!
    [ebuild   R    ] sys-apps/portage-2.3.76::gentoo  USE="(ipc) native-extensions rsync-verify xattr -build -doc -epydoc -gentoo-dev (-selinux)" PYTHON_TARGETS="python2_7 python3_6 (-pypy) -python3_5 (-python3_7)" 0 KiB
    
    Total: 1 package (1 reinstall), Size of downloads: 0 KiB
    
    Would you like to merge these packages? [Yes/No] yes
    
    >>> Verifying ebuild manifests
    >>> Running pre-merge checks for sys-apps/portage-2.3.76
    /var/tmp/portage/._portage_reinstall_.DHn_k0/bin/ebuild.sh: line 57: /var/tmp/portage/._portage_reinstall_.DHn_k0/bin/phase-helpers.sh: No such file or directory
     * ERROR: sys-apps/portage-2.3.76::gentoo failed (clean phase):
     *   (no error message)
     * 
     * Call stack:
     *   ebuild.sh, line 57:  Called die
     * The specific snippet of code:
     *      source "${PORTAGE_BIN_PATH}/phase-helpers.sh" || die
     * 
     * If you need support, post the output of `emerge --info '=sys-apps/portage-2.3.76::gentoo'`,
     * the complete build log and the output of `emerge -pqv '=sys-apps/portage-2.3.76::gentoo'`.
     * The ebuild environment file is located at '/var/tmp/portage/sys-apps/portage-2.3.76/temp/die.env'.
     * Working directory: '/opt'
     * S: ''
    
    srv ~ # emerge -va portage
    
    These are the packages that would be merged, in order:
    
    Calculating dependencies... done!
    [ebuild   R    ] sys-apps/portage-2.3.76::gentoo  USE="(ipc) native-extensions rsync-verify xattr -build -doc -epydoc -gentoo-dev (-selinux)" PYTHON_TARGETS="python2_7 python3_6 (-pypy) -python3_5 (-python3_7)" 0 KiB
    
    Total: 1 package (1 reinstall), Size of downloads: 0 KiB
    
    Would you like to merge these packages? [Yes/No] yes
    Traceback (most recent call last):
      File "/usr/lib/python-exec/python2.7/emerge", line 53, in <module>
        retval = emerge_main()
      File "/usr/lib64/python2.7/site-packages/_emerge/main.py", line 1289, in emerge_main
        return run_action(emerge_config)
      File "/usr/lib64/python2.7/site-packages/_emerge/actions.py", line 3330, in run_action
        retval = action_build(emerge_config, spinner=spinner)
      File "/usr/lib64/python2.7/site-packages/_emerge/actions.py", line 541, in action_build
        retval = mergetask.merge()
      File "/usr/lib64/python2.7/site-packages/_emerge/Scheduler.py", line 949, in merge
        rval = self._handle_self_update()
      File "/usr/lib64/python2.7/site-packages/_emerge/Scheduler.py", line 325, in _handle_self_update
        _prepare_self_update(self.settings)
      File "/usr/lib64/python2.7/site-packages/portage/package/ebuild/doebuild.py", line 2507, in _prepare_self_update
        shutil.copytree(orig_bin_path, portage._bin_path, symlinks=True)
      File "/usr/lib64/python2.7/site-packages/portage/__init__.py", line 246, in __call__
        rval = self._func(*wrapped_args, **wrapped_kwargs)
      File "/usr/lib64/python2.7/shutil.py", line 185, in copytree
        names = os.listdir(src)
    OSError: [Errno 2] No such file or directory: '/usr/lib/portage/python2.7'
    
  • Broken emerge command (broken portage)
    srv ~ # emerge -va portage
    Traceback (most recent call last):
      File "/usr/lib/python-exec/python2.7/emerge", line 42, in <module>
        import portage
    ImportError: No module named portage
    
  • no update of new portage is possible – very very old portage, which does not support EAPI of all current “sys-apps/portage” (now is EAPI=5) and you do not have old portage ebuild files
    srv ~ # emerge -va portage
    !!! Unable to parse profile: '/etc/make.profile'
    !!! ParseError: Profile contains unsupported EAPI '5': '/usr/portage/profiles/default/linux/amd64/17.0/eapi'
    !!! If you have just changed your profile configuration, you should revert
    !!! back to the previous configuration. Due to your current profile being
    !!! invalid, allowed actions are limited to --help, --info, --sync, and
    !!! --version.
    

Upgrading to profile 17.1

Upgrading to Gentoo profile 17.1 there are additional steps to do to have a healthy environment. There is an additional article on the subject – emerge – ERROR: 17.1 migration has not been performed – upgrade from 13 to 17.1 because we do not want to add additional complexity in this article.

Additional error if /root is used for the downloaded or cloned portage

If you use the /root to unpack or clone the https://anongit.gentoo.org/git/proj/portage.git, the emerge command will be under /root/portage/bin/emerge, which could lead to:

>>> Emerging (1 of 1) dev-lang/python-3.6.6::gentoo
 * Python-3.6.6.tar.xz BLAKE2B SHA512 size ;-) ...                                                                                                                                    [ ok ]
 * python-gentoo-patches-3.6.6.tar.xz BLAKE2B SHA512 size ;-) ...                                                                                                                     [ ok ]
/bin/bash: /root/portage-2.3.103/bin/ebuild.sh: Permission denied
 * The ebuild phase 'unpack' has exited unexpectedly. This type of behavior
 * is known to be triggered by things such as failed variable assignments
 * (bug #190128) or bad substitution errors (bug #200313). Normally, before
 * exiting, bash should have displayed an error message above. If bash did
 * not produce an error message above, it's possible that the ebuild has
 * called `exit` when it should have called `die` instead. This behavior
 * may also be triggered by a corrupt bash binary or a hardware problem
 * such as memory or cpu malfunction. If the problem is not reproducible or
 * it appears to occur randomly, then it is likely to be triggered by a
 * hardware problem. If you suspect a hardware problem then you should try
 * some basic hardware diagnostics such as memtest. Please do not report
 * this as a bug unless it is consistently reproducible and you are sure
 * that your bash binary and hardware are functioning properly.

git clone or unpack anywhere and move the directory to /opt, for example! To be sure all groups could access and execute the binaries! Under /root, the only root can read and execute, but there is a portage user, which must have read and execute binaries from the portage directory!

Leave a Reply

Your email address will not be published. Required fields are marked *