0

Suppose we have an empty drive /dev/sda with lots of space. We also have a partition of a previous Linux backup /dev/sdb1. This contains the usual linux filesystem /home, /usr, etc. What is notably missing is the EFI boot partition.

From this setup, how would we

  1. Copy the partition over to /dev/sda, leaving space for a boot partition
  2. Create a boot partition that boots into this file system
  3. Install grub to boot into this from BIOS

1 Answer 1

1

First off, mount /dev/sdb1 on /src. I know you don't have a /boot/efi in your image, but I don't know whether your /boot was separate. Check whether there's an entry for /boot in /src/etc/fstab. If there is, we'll have to recreate that as well.

Copy the partition over to /dev/sda, leaving space for a boot partition

Instead, create the EFI boot partition first (GPT table, make the first partition a 4 GB FAT32 partition, label "EFIboot" for identification).

Make sure you've formatted the partition (sudo mkfs.vfat /dev/sda1, assuming sda1 is the partition name), then note down the UUID you get with sudo lsblk -o UUID /dev/sda1 (we'll call it uuid_efi).

If there was an entry for /boot in /src/etc/fstab, also make a 4GB partition (ext4, nothing special) for that. Note down the UUID of that as well (we'll call it uuid_boot).

Add an appropriately sized partition on sda for the contents of /dev/sdb1, format it (hint: using the same file system type as the original enables the default configuration of your system to still use it) and mount it on /dst and cp --archive /src/* /dst/. This might take a while!

Adjust /dst/etc/fstab so that /boot/efi is a mount of UUID=${uuid_efi}. If there was a /boot entry itself, adjust it to be UUID=${uuid_boot}, both as noted above.

Bind-mount /proc, /sys, /dev and /run into /dst:

for mnt in {proc,sys,run}; do
  sudo mount -o bind "/${mnt}" "/dst/${mnt}"
done

And finally open a shell that sees the /dst directory as /:

sudo chroot /dst /bin/bash

If present, mount /boot; then mount /boot/efi from within that shell

mount /boot
mount /boot/efi

Tada. Now you have a shell that sees everything as if it was running on the booted target system.

The usual distro-specific methods for reinstalling the boot-loader apply here. For debianoids, that's this way, for redhatoids, it's probably yum reinstall grub2-efi. For other distros, you need to do your own research (Search engine for distroname reinstall grub should work). Then use the distro-specific way of reinstalling the recent kernel package, to populate /boot/ properly and regenerate the initrds.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .