8

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 ?

1
  • pls tell us your ubuntu version
    – d1bro
    Nov 3 '16 at 2:30
11

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:

6
  • 1
    thx this post helped me again!
    – Oliver G.
    Jan 8 '18 at 19:57
  • 2
    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. Apr 18 '18 at 13:21
  • 1
    I love you man. Sep 23 '19 at 11:45
  • 1
    Btw how do you copy/paste in a shell? I resorted to taking a pic of the uuid. Sep 23 '19 at 11:56
  • 3
    I would run sudo grub-install /dev/sda instead of purging grub-common and reinstalling grub-pc. Oct 2 '19 at 13:27
0

I had to do this to rescue an encrypted Manjaro install, and I had to modify the other answer's commands slightly for them to work.

1️⃣ Instead of mounting the drive's boot partition to /mnt/boot, I had to mount it to /mnt/boot/efi. Otherwise, GRUB would complain that it can't find the EFI partition.

2️⃣ I had to add /sys/firmware/efi/efivars to the for loop (credit to this answer). Without this, GRUB would not recognize the chrooted system as being booted in UEFI mode, though it was:

# grub-install /dev/drive
Installing for x86_64-efi platform.
EFI variables are not supported on this system.
EFI variables are not supported on this system.
grub-install: error: efibootmgr failed to register the boot entry: No such file or directory.

The drive details:

# "drive" is the encrypted Manjaro drive we need to reinstall GRUB on

$ lsblk -o NAME,UUID,SIZE,FSTYPE,MOUNTPOINT,MODEL,SERIAL
drive             931.5G
├─drivep1 UUID1   300M     vfat          # EFI partition
├─drivep2 UUID2   922.4G   crypto_LUKS   # /
└─drivep3 UUID3   8.8G     crypto_LUKS   # SWAP

🔑 The version of the commands that worked for me:

cryptsetup luksOpen /dev/drivep2 drive_crypt
vgscan --mknodes
vgchange -ay
mount /dev/mapper/drive_crypt /mnt
mount /dev/drivep1 /mnt/boot/efi
for i in /dev /dev/pts /proc /sys /sys/firmware/efi/efivars /run; do sudo mount -B $i /mnt$i; done
chroot /mnt

Once chrooted in, run:

grub-install /dev/drive
pacman -S linux?? # replace "??" with whatever kernel version already exists 
                  # or is desirable

Exit chroot (press Ctrl + d or run exit), reboot (systemctl reboot), and try to boot in to the rescued drive by selecting its entry in the UEFI! 🤞

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.