8

I accidentally overwrote the /boot filesystem on a running Ubuntu host where the root fs and swap are LVs, and the kernel, initramfs, etc. are under /boot with grub modules and config under /boot/grub.

How would one go about recreating all the files needed to successfully boot?

# mkfs.ext2 /dev/sda1
# mount /dev/sda1 /boot
# apt-get install --reinstall linux-image-`uname -r` linux-image memtest86+
# mkdir /boot/grub
# grub-install /dev/sda

That seems to have recreated most everything, bit is that enough? I don't want to chance a reboot without some assurance it will complete.

For the paranoid, this may also be a way of creating a backup boot partition on a flash drive if, for example, your boot partition isn't mirrored but root is.

petiepooo
  • 338

2 Answers2

10

If you have a working system, you can just skip part 1 to 5.

  1. Boot up a Ubuntu live-cd macthing the version you are using.

  2. Mount your normal system partition. X is the drive letter. Y is the partition number:
    sudo mount /dev/sdXY /mnt

  3. Only if you have a separate boot partition (where sdYY is the /boot partition designation):
    sudo mount /dev/sdYY /mnt/boot

  4. Mount the critical virtual filesystems.
    for i in /dev /dev/pts /proc /sys /run; do sudo mount -B $i /mnt$i; done

  5. Chroot into your normal system device:
    sudo chroot /mnt

  6. Reinstall GRUB 2 (substitute the correct device with sda, sdb, etc. Do not specify a partition number):
    grub-install --recheck /dev/sdX

  7. Install ubuntu kernel (Internet is required)
    apt-get install --reinstall linux-image-$(uname -r)

  8. Recreate the GRUB 2 menu file (grub.cfg)
    update-grub

  9. Exit chroot:
    CTRL-D on keyboard
    sudo reboot

https://help.ubuntu.com/community/Grub2/Installing#via_ChRoot
Reinstall latest linux kernel on Ubuntu 10.04

Diblo Dk
  • 738
-1

I suggest using those fine backups you have made and restore them.

If no backups (bad answer)...your easiest route might be a reinstall.

If you have not destroyed the filesystem metadata, even after a reboot you can go in and use a Linux rescue mode boot to get to the information. Your process looks ok but I can provide no guarantees, have you compared the contents of your resulting /boot file with another running system? That might be interesting to be sure you have everything.

mdpc
  • 4,489