How to repair /boot on LUKS encrypted harddrive?

12,428

Hello I tested the following commands in my laptop - yeah I deleted everything in /dev/sda1 and I got it working again - so here it is:

  • Get a live-image and boot from it.

- First lets get a clean /dev/sda1 - open GParted; reformat /dev/sda1 with ext2 and don't forget to confirm the changes and then set the "boot" flag (right click on the partition --> select "Manage Flags" --> check the box next to "boot" [this automatically causes the "esp" flag to be set to] --> click the "Close" button). (this was due to "dd-ed a DSL image to sda1" in OP post)

  • Now we will prepare everything to chroot into the installed system and then we will switch into it (via chroot):

     sudo cryptsetup luksOpen /dev/sda5 sda5_crypt
     sudo vgscan --mknodes
     sudo vgchange -ay
     sudo mount /dev/mapper/ubuntu--vg-root /mnt
     sudo mount /dev/sda1 /mnt/boot
     for i in /dev /dev/pts /proc /sys /run; do sudo mount -B $i /mnt$i; done
     sudo chroot /mnt
    
  • Okay let's delete and reinstall GRUB: grub-install /dev/sda apt purge grub-common (have your terminal in fullscreen-mode due to ncurses), this might ask you if it shall delete everything - select yes; now lets reinstall it with apt install grub-pc here select /dev/sda when asked.

  • Lastly we need to reinstall a kernel to get the needed initrd.img-* and vmlinuz-* images into "/boot/". We get currently-installed kernels with apt list --installed linux-image-* and now we reinstall this kernel with apt install linux-image-[version-numbers]-generic --reinstall - don't forget to exchange the brackets with an actual version number.

  • Almost done; exit chroot with Ctrl + d, or just type exit, and then reboot (via GUI menus or with sudo reboot)!

Used resources:

Share:
12,428

Related videos on Youtube

Pentagon98
Author by

Pentagon98

Updated on September 18, 2022

Comments

  • Pentagon98
    Pentagon98 over 1 year

    I have a laptop with Xubuntu installed.

    At install I chose the full disk encryption option.

    But yesterday i accidentally dd-ed a DSL image to sda1 (/boot partition) instead to sdb1 (pendrive).

    How can i repair my /boot ?

    • d1bro
      d1bro over 7 years
      pls tell us your ubuntu version
  • Oliver G.
    Oliver G. over 6 years
    thx this post helped me again!
  • Stephen Friedrich
    Stephen Friedrich about 6 years
    Great answer! One additional issue that I needed to fix after that: The reformat of sda1 lead to a different uuid for that partition. So I needed to change the uuid in fstab after this procedure.
  • Lauri Elias
    Lauri Elias over 4 years
    I love you man.
  • Lauri Elias
    Lauri Elias over 4 years
    Btw how do you copy/paste in a shell? I resorted to taking a pic of the uuid.
  • Marius Gedminas
    Marius Gedminas over 4 years
    I would run sudo grub-install /dev/sda instead of purging grub-common and reinstalling grub-pc.
  • phipsgabler
    phipsgabler over 3 years
    What Marius says works; I tried this answer on Xubuntu 20.04. This is another good resource.