Ubuntu – How to install Grub in an external hard drive

bootgrub2usb

So, I've used Ubuntu for a month or two in dualboot with Windows 8, it looked and ran fine, however it broke down for some reason and I had to restore the entire PC. Due to this bad experience, I decided to reinstall Ubuntu on an external hard drive separated from my main Windows machine. This option seems to be good for both the operating systems.

However, when installing Ubuntu, for some reason, the bootloader Grub was not installed on the external drive, while at the same time, the "old" Grub bootloader was never erased from the PC internal hard drive. As a result, I am in a weird situation: when I want to run Windows, I just power on the pc and go. When I want to run Linux, I have to plug in the external drive, go in the startup menu, and change bootloader from Windows' to Grub. The external hard drive is not bootable (as a usb stick for example) and this long sequence of operations needs to be repeated each time.

Quick recap:

  1. PC internal hard drive contains Windows OS, Windows bootloader and Grub from the old linux installation.
  2. External hard drive contains Ubuntu 15.10, possibly Grub, but is not bootable as a USB stick is.
  3. Every time I want to switch OS I need to change bootloader manually in the startup menu which is tedious.
  4. Boot devices are ordered as follows: 1. USB 2. External drive 3. Internal drive

My question:
How can I make the external drive bootable so that when I want to switch from Windows to Linux I just power off the pc, plug in the USB, power on the pc and go? (without loosing the data I already stored on it and all the preferences)

PS.
For some reason Windows is terribly slow when loaded via the Grub bootloader available in the internal drive. Furthermore, if the external hard drive is not plugged in, Grub bootloader shows the Grub rescue panel only. So using the already installed Grub as the only bootloader is not an option.

Best Answer

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

  1. Boot into an Ubuntu live USB and connect the external hard drive.

  2. List partitions of all devices:

     lsblk
    
  3. 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. Replace XY1 and XY2 with your own partition names.

  4. Special mount the linux partition:

      sudo mount /dev/sdXY1 /mnt
    
  5. Mount critical virtual filesystems:

     for i in /dev /dev/pts /proc /sys /run; do sudo mount -B $i /mnt$i; done
    
  6. chroot into the Linux partition you mounted:

     sudo chroot /mnt
    

    You are now in the external hard drive's linux filesystem.

  7. Create the directory where grub would install its files:

     mkdir -p /boot/efi
    

    If it already exists, then skip to step 8.

  8. Mount the efi partition from step 3:

     mount /dev/sdXY2 /boot/efi
    
  9. Install grub to the external hard drive:

     grub-install /dev/sdX
    
  10. Update grub:

    update-grub
    
  11. Find the UUID of the efi partition (aka 'vfat') and note it down:

    blkid
    

    or

    ls -l /dev/disk/by-uuid
    
  12. 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.

  13. Exit the chroot:

    exit
    
  14. 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

  1. In Windows download EasyUEFI, install it and run.

  2. Choose the EFI options manager

  3. Choose create 'new entry' with + sign

  4. Choose Linux or other OS and give it a name in the description box

  5. Select the FAT32 EFI partition on the external drive that contains the grub bootloader

  6. Choose browse

  7. Navigate to /efi/ubuntu/shimx64.efi or /efi/ubuntu/grubx64

  8. Save

  9. Move it to the top of the boot entries list

  10. Restart

Alternative #2 – using efibootmgr

  1. List boot menu entries:

     efibootmgr
    
  2. 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.

  3. 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.

Related Question