Install GRUB2 on USB from Linux

How to Install Grub2 on USB from Linux to make a BIOS an UEFI bootable USB that can boot on all Machines. In the following tutorial, I'll show you one way of easily installing Grub2 Bootloader to a USB Flash Drive from an up and running Linux Operating Environment. Ubuntu was used to perform all of the steps in this guide. Upon completion, your device should be able to boot from both UEFI and BIOS machines using 32 or 64bit architectures.

Note: This Grub2 USB Flash Drive Installation tutorial assumes you will be using Ubuntu to create your Grub2 enabled Flash Drive. It may or may not work from other Linux operating environments as well.

Prerequisites:

  • A running Linux OS (I.E. Ubuntu)
  • Computer that can boot from USB
  • USB Flash Drive (Fat32 formatted)
  • No fear of the terminal

Warning: This Guide assumes you are starting with a Fat32 formatted USB. Be aware you will be overwriting any existing bootloader and grub.cfg.

Before proceeding, boot into your Linux desktop (Ubuntu was used here). Once running from your desktop, insert your Flash Drive.

Get the latest Grub2 Source Code and Bootstrap it

First we need to install required packages, grab the latest Grub2 source code from the repository, and then bootstrap it.

1.) To gain admin (root) access, open a terminal ctrl+alt+t, then type the following and press Enter.

sudo su

2.) Optional Step: To update before proceeding, type

apt update && apt upgrade

3.) To install the required packages,

apt install git bison libopts25 libselinux1-dev m4 help2man libopts25-dev flex libfont-freetype-perl automake make autotools-dev autopoint libfreetype6-dev texinfo python autogen autoconf libtool libfuse3-3 unifont gettext binutils pkg-config liblzma5 libdevmapper-dev

a. To get the latest Grub2 source code,

git clone git://git.savannah.gnu.org/grub.git

b. Now change to the grub directory,

cd grub

c. Then to bootstrap it,

./bootstrap

Compiling Grub2 for BIOS and UEFI support on 32/64bit platforms

In order to support BIOS or UEFI booting from both 32 and 64 bit platforms, Grub2 must be compiled for each architecture. To accomplish this, we will create separate containers for each build.

1.) First create a separate working container for each platform,

mkdir EFI64 EFI32 BIOS

2.) Next to configure and then build for Grub2 64bit UEFI,

a. First change to the EFI64 directory,

cd EFI64

b. Then to configure and make Grub2 for 64Bit UEFI,

../configure --target=x86_64 --with-platform=efi && make

3.) Now to configure and then make for Grub2 32 bit UEFI,

a. Change to the EFI32 directory,

cd ../EFI32

b. Then to configure and make Grub2 for 32bit UEFI,

../configure --target=i386 --with-platform=efi && make

4.) Finally to configure and build for Grub2 BIOS

a. Change to the BIOS directory,

cd ../BIOS

b. Then to configure and make for BIOS systems,

../configure --target=i386 --with-platform=pc --disable-nls && make

Locating and mounting your USB Device

Before going any further, you'll need to find your USB device and then mount it.

1.) First, you'll need to find out which dev is your USB. To do this type,

fdisk -l

2.) Note which device belongs to your USB.
Use fdisk -l to locate usb device
3.) Now, to make a directory to be used for the mount point type,

mkdir /mnt/USB

4.) Then you'll need to mount your USB device, so that you can install Grub2 on it. To do this, type the following (replacing x with your actual USB device).

mount /dev/sdx1 /mnt/USB

Grub2 USB Flash Drive Installation

This next section covers the Grub2 installation of all three architectures (64bit UEFI, 32bit UEFI, BIOS) to your USB flash drive.

According to the Grub manual, removable installs require the --removable option (with the exception of i386-pc). When using this, you must also specify --boot-directory and --efi-directory. In addition we'll include the --force option to allow buggy BIOS systems to detect the USB as a Hard Disk.

1.) To install Grub2 64 bit UEFI x86_64-efi,

a. First change to the EFI64 container,

cd ../EFI64/grub-core

b. Then install x86_64-efi,

grub-install -d $PWD --force --removable --no-floppy --target=x86_64-efi --boot-directory=/mnt/USB/boot --efi-directory=/mnt/USB

2.) Then to install Grub2 32 bit UEFI i386-efi,

a. First change to the EFI32 container,

cd ../../EFI32/grub-core

b. Then install i386-efi,

grub-install -d $PWD --force --removable --no-floppy --target=i386-efi --boot-directory=/mnt/USB/boot --efi-directory=/mnt/USB

3.) Finally to put Grub2 BIOS i386-pc,
(replacing  x with your device).

a. First change to the BIOS container,

cd ../../BIOS/grub-core

b. Then install i386-pc,

grub-install -d $PWD --force --no-floppy --target=i386-pc --boot-directory=/mnt/USB/boot /dev/sdx

Loopback Boot an Ubuntu ISO from your Grub2 USB (Optional)

The next steps are optional, but will compliment your efforts. In these steps, you will add a simple example configuration file to your USB that enables you to directly boot from an Ubuntu ISO file using the loopback feature. Then you download an ubuntu.iso onto your USB drive. And finally, reboot to test functionality.

1.) First, type the following to change to the grub directory on your USB.

cd /mnt/USB/boot/grub

2.) Next, to download an grub2 loopback configuration file, type

wget pendrivelinux.com/downloads/grub.cfg

3.) Then, to navigate back to the root of the USB, type

cd /mnt/USB

4.) Now to download the Ubuntu.iso, type

wget "http://releases.ubuntu.com/groovy/ubuntu-20.10-desktop-amd64.iso" -O ubuntu.iso

5.) Reboot your PC, setting your BIOS or Boot Menu to boot from the USB device.

If all goes well, you should now be booting into your Grub2 prepared USB Flash Drive.
GRUB2
Optionally with a Grub2 Boot Menu to Loopback Boot from the Ubuntu ISO copied to your USB.
Ubuntu 20.04 Desktop

Install GRUB2 on USB from Linux published under Flash drive installs using Linux, Using and Configuring Linux 

Disclaimer