17

The system does not start. Did it by using rm - R accidentally. How can I recover the system?

αғsнιη
  • 35,660
Mouvre
  • 307
  • are you sure you removed /usr/share folder.. give a try.. logon with live session.. copy and paste the /usr/share folder from live session to your root partitions /usr/share – PRATAP Apr 29 '19 at 08:35
  • 1
    What @PRATAP suggested may get it so you can log back in, but many user programs store application data there, so that'll at best fix only programs used by your 'live' (install media) system. You'll then have to install --reinstall other programs; or you could just re-install using 'something else', no format & it'll take note of your installed apps, wipe system directories then install, then re-install your additional programs (if in Ubuntu repos); but this option can be a fallback anyway as you'll probably learn more trying to fix it first yourself. There's always restoring your backups. – guiverc Apr 29 '19 at 08:43
  • -> with just rm -R in /usr/share NOTHING gets deleted. Contents are owned by root so a normal user can't delete it – Rinzwind Apr 29 '19 at 10:50
  • @Rinzwind And if so, why system is not loading? How can I get the root to fix it? – Mouvre Apr 29 '19 at 11:54
  • did you perhaps do sudo rm -R? The answer n0rbert posted looks good to me. That is the way to go – Rinzwind Apr 29 '19 at 11:56
  • No suggestion to use a recovery solution? The system hasn't been run, the files will only have been flagged as deleted, they should be able to be recovered with relative ease. e.g. https://www.linux.com/learn/get-your-data-back-linux-based-data-recovery-tools (not an answer as I'm not providing instructions) – Baldrickk Apr 29 '19 at 17:11

2 Answers2

49

You need to reinstall all applications which have files in /usr/share.

  1. Boot system from Recovery Mode with root prompt.

  2. Remount rootfs to read-write mode with mount -o rw,remount /.

  3. Raise up ethernet interface and get IP address from DHCP server: dhclient enp0s3 (check correct interface name in ip a or ifconfig -a).

  4. Specify DNS server by

    rm /etc/resolv.conf
    echo "nameserver 192.168.1.1" > /etc/resolv.conf
    

    (change 192.168.1.1 to yours gateway/router)

  5. You need to fix the dpkg package as it misses the /usr/share/dpkg/cputable file (otherwise you will face the "E: error reading the cpu table" on any apt/apt-get command)

    wget http://mirrors.kernel.org/ubuntu/pool/main/d/dpkg/dpkg_1.19.0.5ubuntu2.1_amd64.deb
    dpkg -i dpkg_1.19.0.5ubuntu2.1_amd64.deb
    

    or installing it from cache:

    dpkg -i /var/cache/apt/archives/dpkg_*.deb
    
  6. update package cache:

    apt-get update
    
  7. reinstall essential componenets

    apt-get install --reinstall debconf linux-base
    mkdir /usr/share/python
    apt-get install --reinstall python2.7-minimal python3-minimal
    
  8. and then use one-liner below:

    apt-get install --reinstall $(dpkg -S /usr/share/ | sed 's/,//g' | sed 's/: \/usr\/share//g')
    

    Above:

    • dpkg -S /usr/share/ shows the list of comma-separated packages
    • sed 's/,//g' - removes commas
    • sed 's/: \/usr\/share//g' - removes : /usr/share in the end

    This part may fail with messages about some packages. For example on my VM I have had a problem with bsdmainutils, so I reinstalled them with:

    dpkg -i /var/cache/apt/archives/bsdmainutils_*.deb
    

    and then reran one-liner above.

  9. fix broken packages

    dpkg --configure -a
    apt-get install -f
    
  10. finally fix /etc/resolv.conf link by

    rm /etc/resolv.conf
    ln -s /run/systemd/resolve/stub-resolv.conf /etc/resolv.conf
    
  11. reboot.

Note: the resulting fixed system do not show any problems while running sudo apt-get check or sudo debsums --all --changed.

N0rbert
  • 99,918
  • this command shall be used from live session? – PRATAP Apr 29 '19 at 09:05
  • @PRATAP from recovery mode is enough. – N0rbert Apr 29 '19 at 10:03
  • OK, thats good.. but even after deleting /usr/share will grub work normally and show the grub menu? i think i have to delete it and see in my practical pc..nice answer..Thank you.. – PRATAP Apr 29 '19 at 10:05
  • 1
    I have just tested this on VM. It is almost working solution. – N0rbert Apr 29 '19 at 10:07
  • Oh, thanks a lot.. you saved lot of time.. appriciated.. – PRATAP Apr 29 '19 at 10:08
  • @N0rbert Thank you for answer, i did everything as you said, but after reboot system loads for a long time, and then just a gray screen. Maybe you now what to do? – Mouvre May 08 '19 at 06:56
  • I'll boot the system in recovery mode again and then install/reinstall the desktop environment with something like sudo apt-get install --reinstall ubuntu-desktop or maybe just LightDM alone with sudo apt-get install --reinstall lightdm. Then resume normal system boot. Without system logs it is difficult to say what is currently broken. Also you can try to boot in normal mode, login to the text console with <Ctrl>+<Alt>+<F1> and try to restart lightdm from it with sudo service lightdm restart and the read its error output by service lightdm status. What desktop environment you use? – N0rbert May 08 '19 at 07:52
  • @N0rbert I have Gnome3 – Mouvre May 08 '19 at 07:59
  • Then commands from previous comment may help. – N0rbert May 08 '19 at 08:14
  • 1
    @N0rbert Thank you for help, its really works! – Mouvre May 11 '19 at 17:46
2

Short answer: you can't and you have to reinstall the apps, as suggested by a previous answer.

However, before that, I would mount the drive in another computer and test undelete utilities which are available for ext3/ext4. A quick search in Google points to these ones:

extundelete: http://extundelete.sourceforge.net/

easeus: https://www.easeus.com/resource/drive/ext3.htm

TestDisk: https://www.cgsecurity.org/wiki/TestDisk

How to guide: http://translatedby.com/you/howto-recover-deleted-files-on-an-ext3-file-system/original/

etc.