How to restore a virtual system after accidentally removing all kernels?

5,681

You can solve this problem very easily, so go through the steps below one by one:

  1. Insert a Live CD ISO image into the Virtualbox disk drive.
  2. Change the boot order for the Virtualbox drives to boot from this ISO image and boot into it.
  3. choose 'Try Ubuntu' and open a terminal by pressing Ctrl+Alt+T.
  4. Run lsblk to find the proper device name for your VM main drive.
  5. Mount the drive into /mnt where XX is the proper drive label for the drive you want to rescue:

    mount /dev/sdXX /mnt
    
  6. Now mount and bind the following to make a proper install possible:

    sudo mount -o bind /dev /mnt/dev
    sudo mount -o bind /dev/pts /mnt/dev/pts
    sudo mount -t sysfs /sys /mnt/sys
    sudo mount -t proc /proc /mnt/proc
    sudo cp /proc/mounts /mnt/etc/mtab
    sudo cp /etc/resolv.conf /mnt/etc/resolv.conf
    
  7. Chroot into the mountpoint with sudo chroot /mnt.

  8. Run an update of the packet-lists:

    apt-get update
    # you might want to do an upgrade as well if you like
    apt-get upgrade
    
  9. Now you can install the kernel again with:

    apt-get install --reinstall linux-image-generic
    
  10. Run update-grub to make sure grub points again to a working installed kernel.

  11. Now you can exit the chroot environment by pressing Ctrl+D and wrap up before you shut down the VM and reboot it on the normal drive:

    sudo umount /mnt/dev/pts
    sudo umount /mnt/dev
    sudo umount /mnt/sys
    sudo umount /mnt/proc
    sudo umount /mnt
    

This should have done it and your VM now again has a kernel to boot from

Share:
5,681

Related videos on Youtube

gwalker
Author by

gwalker

Updated on September 18, 2022

Comments

  • gwalker
    gwalker over 1 year

    I have seen answers for actual installs but am not sure this would work for Virtual.

    Like a fool, I had not backed up the data on a VM I used at work. When I tried to upgrade I was out of space and like a bigger fool, wiped out all kernels instead of just old ones when trying to free up space. Now I boot into Memtest86 and can go no further because grub(?) sees no other kernel.

    The VM was Ubuntu 14.04 running in Oracle VM VirtualBox. Is there a way to mount the image from another Ubuntu VM and reinstall the kernel? The directory has an Ubuntu.vbox file and a Ubuntu.vdi file as well as a Ubuntu.vbox.prev file that seems to just be XML data


    UPDATE: Mounted from desktop in "Try Ubuntu" mode, chrooted in to /media/ubuntu/<diskname> and had to manually override to write a new /etc/resolv.conf. When I did the apt-get install linux-image-generic it seemed to have write issues. Did I mount incorrectly?

    I mounted just by selecting the HD listed in "Files" and after

    sudo chroot /media/ubuntu/<diskname>
    

    I confirmed that the existing users were listed in /home/ so I am pretty sure I was on the right drive, but when I tried to mount it the way other how-to show, I get:

    sudo mount /media/ubuntu/<long HD name>
    mount: /media/ubuntu/<long HD name> is not a block device
    
    • Thomas Ward
      Thomas Ward over 6 years
      Comments are not for extended discussion; this conversation has been moved to chat.
    • Thomas Ward
      Thomas Ward over 6 years
      The above was done at one of the commentor's requests.
  • gwalker
    gwalker over 6 years
    Worked! Many thanks for your patience and help!