====== Restoring After Deleting Libraries ======
Whilst performing the [upgrade to 17.1 profile]() I stupidly deleted some directories under `/usr/lib32/` rather than deleting the symbolic link `/usr/lib32` itself. This included `/usr/lib32/gcc` which meant I could no longer emerge anything and some programs no longer ran which were linked against `libstdc++.so.6`. Not being able to emerge anything is a bit of a problem under Gentoo given everything is compiled from source!
Fortunately my system was still bootable, although either xdm or Xorg failed to start (either way no Desktop Environment) but I had a command line login so all was (relatively) good. A search of the [Gentoo Forums](https://forums.gentoo.org/) lead to a really useful thread with a solution from none other than the hero of Gentoo Forums NeddySeagoon who explained that a [stage3 chrooted to ](https://forums.gentoo.org/viewtopic-p-7276926.html) with your existing `/usr/portage` as per the [Gentoo Handbook](https://wiki.gentoo.org/wiki/Handbook:AMD64/Installation/Base) and from here it is, after having synced the portage tree `emerge --sync`, possible to install any package and build a `binpkg` that is saved under `/usr/portage/packages`. These can then be installed under the normal non-chroot environment using `emerge -K =category/package-version`, or in the case of gcc you treat the created package as a mini Stage3 tarball and extract it to `/`.
Here are the *exact* steps I took after hosing my system to recover it.
===== Download a Stage 3 =====
Download the [latest stage3](https://www.gentoo.org/downloads/), I use a no-multib profile so grabbed that.
cd /tmp
wget http://distfiles.gentoo.org/releases/amd64/autobuilds/20190605T214501Z/stage3-amd64-nomultilib-20190605T214501Z.tar.xz
===== Make a mount point for stage3 and extract =====
mkdir /mnt/stage3
mv /tmp/stage3-amd64-nomultilib-20190605T214501Z.tar.xz /mnt/stage3/.
cd /mnt/stage3
tar xvf stage3-amd64-nomultilib-20190605T214501Z.tar.xz
===== Copy DNS =====
cp --dereference /etc/resolv.conf /mnt/stage3/etc/
===== Mount /proc and /dev =====
mount --types proc /proc /mnt/stage3/proc
mount --rbind /sys /mnt/stage3/sys
mount --make-rslave /mnt/stage3/sys
mount --rbind /dev /mnt/stage3/dev
mount --make-rslave /mnt/stage3/dev
===== Mount bind additional locations =====
mkdir -p /mnt/stage3/var/cache/distfiles /mnt/stage3/var/db/repos /mnt/stage3/var/cache/binpkgs
mount -o bind /var/db/repos/gentoo /mnt/stage3/var/db/repos/gentoo
# mount -o bind /var/cache/distfiles /mnt/stage3/var/cache/distfiles
mount -o bind /var/cache/binpkgs /mnt/stage3/var/cache/binpkgs
===== Copy your /etc to /mnt/stage3 =====
cp -r /etc/portage /mnt/stage3/etc/portage
===== chroot to stage3 =====
chroot /mnt/stage3 /bin/bash
source /etc/profile
export PS1="(stage3) ${PS1}"
===== Modified ACCEPT_KEYWORDS =====
On my system I run `ACCEPT_KEYWORDS="~amd64` so I added the line to `/etc/portage/make.conf` (in the chrooted environment)
echo 'ACCEPT_KEYWORDS="~amd64' >> /etc/portage/make.conf
===== Synced portage and installed eix and genlop =====
emerge --sync && emerge -q genlop eix && eix-update
===== Emerged gcc =====
Check what version of gcc you are running with `gcc-config -l` and emerge it...
gcc-config -l
emerge -qv =sys-devel/gcc-9.1.0-r1
===== Switch to new gcc =====
gcc-config -l
gcc-config 2 # Be sure to select the new one you want
. /etc/profile
export PS1="(stage3) ${PS1}"
===== Package gcc =====
**NB** Despite switching to the new gcc BOTH installed versions were packaged by the following.
quickpkg gcc
===== Install newly packaged gcc in main system =====
Switch to a non-chroot environment, if you are still in a desktop environment and haven't rebooted then this is as simple as starting a new terminal and `su`, if you rebooted then use `Ctrl+F2` to go to a new console, login and su. Install the newly packaged gcc with
tar xpf /mnt/stage3/usr/portage/packages/gcc-9.1.0-r1.tar.xz -C /
===== Test gcc on your main system =====
Try compiling a simple program on your main system
#include
void main()
{
printf("Hello world\n");
}
gcc test.c
./a.out
If that works try re-emerging gcc on your main system, if there are complaints about missing libraries compile them under the Stage3 environment, package them and then install on the main system. I was missing `libmpfr.so.4` part of dev-lib/mpfr and needed to build and package `=dev-libs/mpfr-4.0.2`
==== Stage 3 ====
emerge =dev-libs/mpfr-4.0.2
quickpkg dev-libs/mpfr-4.0.2
==== Main System ====
rsync -av /mnt/stage3/usr/portage/packages/* /var/cache/binpkgs/.
emerge -Kv =dev-libs/mpfr-4.0.2
===== Re-emerge missing packages or @world =====
You can now install in your normal install any packages for which you deleted their libraries.
time emerge -ev @world
===== Unmount /mnt/stage3 =====
umount -l /mnt/stage3/dev{/shm,/pts,/proc}
umount -l /mnt/stage3/proc
umount /mnt/stage3
====== Links ======
* [Fix My Gentoo - Gentoo Wiki](https://wiki.gentoo.org/wiki/Fix_My_Gentoo#The_Rescue_Install) - an excellent guide by the wonderful NeddySeagoon.
* [Gentoo Linux amd64 Handbook: Installing Gentoo - Gentoo Wiki](https://wiki.gentoo.org/wiki/Handbook:AMD64/Full/Installation)