17

I mistakenly deleted the /boot folder from my filesystem, rebooted, and all I get now is this:

error: file '/boot/grub/i368-pc/linux.mod' not found.

grub rescue>
Braiam
  • 35,380
  • 25
  • 108
  • 167
Imad Nouari
  • 301
  • 2
  • 3
  • 7
  • 1
    possible duplicate of [I deleted files from my Linux box's /boot directory and now it won't boot](http://unix.stackexchange.com/questions/13654/i-deleted-files-from-my-linux-boxs-boot-directory-and-now-it-wont-boot) – Cthulhu Tentacles Jan 01 '15 at 18:01
  • it's different, in this case, the /boot folder was completely deleted. not only some files from it, thanks. – Imad Nouari Jan 01 '15 at 18:34
  • I don't thing any of the answers on the other question are sufficient to fix this.. – Graeme Jan 01 '15 at 18:36
  • Fair enough, didn't really read all the answers. I would always suggest rebuilding the whole thing rather than fiddling about trying to work out what was missing so assumed someone there would have too. Takes half the time. – Cthulhu Tentacles Jan 01 '15 at 18:59
  • 7
    The far bigger mistake compared to deleting `/boot` is, of course, rebooting the system before this was fixed. – Anthon Jan 01 '15 at 19:26
  • the worst case scenario was the power cut. – Imad Nouari Jan 01 '15 at 19:49
  • I experienced a similar problem a while back and wrote up [my recovery which you may find useful](http://unix.stackexchange.com/a/148042/27437). – DocSalvager Jan 09 '15 at 11:33

1 Answers1

38

Oops!

Here's what to do:

  1. Boot a live distro. This can be your Ubuntu installation disc or another one such as Knoppix.

  2. Find the drive/partition where you have installed your root filesystem. To do this you can open a terminal and run either sudo parted -l or sudo fdisk -l. If you can't tell, then edit your question and add the output.

  3. Assuming that your root partition that you found from the last step is /dev/sdxy (x should be another letter and y should be a number), then run the following commands in a terminal:

    mkdir mnt
    sudo mount /dev/sdxy mnt
    sudo mount --bind /dev /mnt/dev
    sudo mount --bind /proc /mnt/proc
    sudo mount --bind /sys /mnt/sys
    sudo chroot mnt
    
  4. You will now be inside a chroot environment meaning that running commands here is equivalent to running them on your installed system. The first thing you want to do is reinstall GRUB2 to the device so that it copies the correct files into the /boot folder. To do this run the following with the drive that your root partition is on (ie /dev/sdxy with the number (y) removed):

    grub-install /dev/sdx
    
  5. You now want to find out which packages you have installed that have files in the boot directory and reinstall them. This will replace the kernel images that have been deleted among other things. The command to find the packages is:

    dpkg -S /boot
    

    And to reinstall them:

    apt-get --reinstall install ...
    

    Where ... is replaced with the names of packages from the first command. Do not include the commas, just have the package names with spaces to separate.

    This step will probably require internet access (unless the packages are already in the cache), so make sure you are connected if there is an problem.

  6. Since you will have deleted your kernels and reinstalled them, this should have triggered a GRUB2 update automatically. But just in case they haven't, you can run:

    update-grub
    
  7. Reboot and things should now be fixed. One issue that I had the last time I did something similar was that Windows installs where not found by update-grub when run in the chroot due to a bug in os-prober. If this is an issue, just run sudo update-grub again in the repaired system.

Graeme
  • 33,607
  • 8
  • 85
  • 110
  • @Olivier, thanks I originally had `/dev/sdax` everywhere then changed it to `/dev/sdxy`. – Graeme Jan 02 '15 at 09:12
  • Thanx for the great info - it saved me, with one addition. Since I have a software RAID, I had to also follow the instructions here: https://help.ubuntu.com/community/Grub2/Installing under the reinstall using chroot method to start mdadm and assemble the RAID members and do the mounts accordingly. Then, after chroot, reinstall the packages as described in this answer and all is well! – bitfiddler Jul 30 '15 at 06:08