Is it possible?
Well, most trivial and important utilities are installed in /bin
, and now you lost access to all of them. In fact, if you reboot, your system will not be able to boot-up anymore.
Anyway, we are going to fix the issue and make /bin
's contents as close as is possible to where it was. The only difference would be some symbolic links which we will fix too.
How?
First, we should chroot
into your broken system, but with a minor difference! After that we will get a list of installed packages on your system which have any installed file in /bin
directory, then we are going to only download the needed packages and extract the necessary files into /bin
. Then we'll be done.
For example, after chroot
, we can get a list of packages which have installed files in /bin
using:
dpkg --search /bin | cut -f1 -d: | tr ',' '\n'
And we can also use:
dpkg --listfiles PACKAGE-NAME | grep "^/bin/" # or awk '$0 ~ "^/bin/
to list installed files by these packages in /bin
.
Then we simply create a list of all packages that are necessary to us, then download them and extract them to /bin
with something like:
xargs apt download < list-packages
dpkg-deb -x PACKAGE .
mv ./bin/* /bin
However we must use a script to check all installed packages on our system, because doing it manually is just madness.
So I wrote a script which does everything we need. It finds all necessary packages for us to restore /bin
, shows us the name of each package and their related files that belongs to /bin
. Here is a screenshot:
At the end we choose to reinstall all packages or only download and extract the necessary files to /bin
(which is the recommended option):
You can grab a copy of this script or download it directly.
Lets start
chroot
Boot your system with a live disk which has same architecture as your installed Ubuntu, open a terminal and get root access:
sudo -i
Mount your root
file system (for me it's /dev/sda1
):
mount /dev/sda1 /mnt
We will need connectivity to the Internet, so copy resolv.conf
from live Ubuntu to your mounted root partition:
cp /etc/resolv.conf /mnt/etc/resolv.conf
Now copy the script to somewhere on the mounted partition, e.g:
cp /media/ubuntu/usb/restore-bin.sh /mnt/restore-bin.sh
or you can download it using wget
, etc. like:
wget https://git.io/v9fRm -O /mnt/restore-bin.sh
Mount other necessary paths:
mount --bind /dev /mnt/dev
mount --bind /sys /mnt/sys
mount -t proc /proc /mnt/proc
And here is the minor difference: how can we chroot
to a broken system when there is no /bin
directory in there? Which shell should we run?
So create a temporary bin directory. e.g: named bintmp
within your broken system root:
mkdir /mnt/bintmp
Then bind the live /bin
into that:
mount --bind /bin /mnt/bintmp
Chroot into the system while setting the /bintmp/bash
as your login shell:
chroot /mnt /bintmp/bash
Export the /bintmp
as your PATH
environment variable:
export PATH=/bintmp:$PATH
Give the script the executable bit:
chmod +x restore-bin.sh
Run the script:
./restore-bin.sh
Wait for the search to be completed then answer the question we saw in the screenshot. It will start to restore the /bin
and we are almost done.
After it's done, use CTRL+D to get out of chroot
environment and unmount the mounted paths:
umount -R /mnt
Reboot the system.
Restoring the links within /bin
Now almost all of files within /bin
directory are back, except around 5 symbolic links which are managed by update-alternatives
.
In your running system, run:
sudo update-alternatives --all
It asks you some questions; you can simply press ENTER to accept them all.
And now we are done.