How to install Grub in an external hard drive?
Solution 1:
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:
lsblk
-
From the results of
lsblk
, identify the linux partition and also the efi partition of the external hard drive, i.e./dev/sdXY1
and/dev/sdXY2
respectively. ReplaceXY1
andXY2
with 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
-
chroot
into 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:
grub-install /dev/sdX
-
Update grub:
update-grub
-
Find the UUID of the efi partition (aka 'vfat') and note it down:
blkid
or
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:
exit
-
Reboot the PC:
sudo reboot
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
-
Choose browse
-
Navigate to /efi/ubuntu/shimx64.efi or /efi/ubuntu/grubx64
-
Save
-
Move it to the top of the boot entries list
-
Restart
Alternative #2 – using efibootmgr
-
List boot menu entries:
efibootmgr
-
Create a boot entry:
efibootmgr -c [-L label] [-d /dev/sdX]
e.g.:
efibootmgr -c -L myubuntu -d /dev/sdX
This boot entry would automatically be the first entry.
/dev/sdX
is 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.
Solution 2:
I eventually found the solution to my problem. I'll post it here in case someone may need it. Also, I'm not entirely sure what worked and if all the steps I took were really necessary, however this worked.
- Make sure you have an EFI partition on your external drive using gparted or something similar. The partition should be FAT32, have the boot and esp flags, and be mounted at /boot/efi. The size should be at least 200 Mb although I made it 1Gb since I hade space available on my HD. If you do not have this partition, then make it.
- Check that the UUID of the EFI BOOT partition on your external hard drive matches the one in the fstab file.
to check partitions' UUID just type in the terminal
ls -l /dev/disk/by-uuid
while to edit the fstab file
sudo nano /etc/fstab
-
Now install Grub. (You can also try to copy grub from the internal drive to the external drive partition but it did not work for me).
sudo grub-install /dev/sdX
replace sdX with the actual drive.
- In case you haven't already done it, enable boot from USB drive and make sure external boot loader is loaded before the internal one from your UEFI setup menu. Now your machine should boot first from USB then (if no boot loader has been found) from the internal hard drive.