This is how I installed grub unto my external hard drive (GPT) which had Ubuntu 17.04 installed. When you do this it allows you to boot your Ubuntu on any PC which supports UEFI by using the grub on the external without relying on the PC grub. It can also serve as backup grub in case the is problem with EFI of Windows.
Installing Grub to an external hard drive that has Ubuntu installed
Boot into an Ubuntu live USB and connect the external hard drive.
List partitions of all devices:
From the results of
lsblk, identify the linux partition and also the efi partition of the external hard drive, i.e.
XY2with your own partition names.
Special mount the linux partition:
sudo mount /dev/sdXY1 /mnt
Mount critical virtual filesystems:
for i in /dev /dev/pts /proc /sys /run; do sudo mount -B $i /mnt$i; done
chrootinto the Linux partition you mounted:
sudo chroot /mnt
You are now in the external hard drive's linux filesystem.
Create the directory where grub would install its files:
mkdir -p /boot/efi
If it already exists, then skip to step 8.
Mount the efi partition from step 3:
mount /dev/sdXY2 /boot/efi
Install grub to the external hard drive:
Find the UUID of the efi partition (aka 'vfat') and note it down:
ls -l /dev/disk/by-uuid
Now we need to tell fstab to mount that efi partition on boot:
cp /etc/fstab /etc/fstab.bak sudo nano /etc/fstab
Add the below two lines to fstab replacing xxxx-xxxx with the UUID from step 11:
#my modified fstab to mount external hard drive's esp UUID=xxxx-xxxx /boot/efi vfat umask=0077 0 1
Make sure to comment out the fstab entry of the Windows esp so it doesn't conflict.
Exit the chroot:
Reboot the PC:
At this point when you reboot and choose your external device from the EFI boot manager, it will boot to grub.
To make Ubuntu automatically boot when you insert the external and Windows to automatically boot when the external is not inserted you would have to create a custom boot entry for Ubuntu to point to the efi on the external drive:
Creating a custom boot entry for Ubuntu to point to efi on the external HD
From Windows EasyUEFI can add or remove boot entries.
From Linux efibootmgr can add or remove boot entries.
Alternative #1 – using EasyUEFI
In Windows download EasyUEFI, install it and run.
Choose the EFI options manager
Choose create 'new entry' with + sign
Choose Linux or other OS and give it a name in the description box
Select the FAT32 EFI partition on the external drive that contains the grub bootloader
Navigate to /efi/ubuntu/shimx64.efi or /efi/ubuntu/grubx64
Move it to the top of the boot entries list
Alternative #2 – using
List boot menu entries:
Create a boot entry:
efibootmgr -c [-L label] [-d /dev/sdX]
efibootmgr -c -L myubuntu -d /dev/sdX
This boot entry would automatically be the first entry.
/dev/sdXis the external hard drive with the grub bootloader.
You can now reboot and without your intervention Ubuntu would automatically boot when external hard drive is plugged in. When the external hard drive is not plugged in, the PC would skip our Ubuntu boot entry to the next entry which is probably Windows Boot Manager.