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.
How to use Linux to Install Grub2 on USB
This Grub2 USB flash drive Installation tutorial assumes that Ubuntu is being used to perform all of the steps that follow. However, it may work to create a Grub2 enabled flash drive from other Linux operating environments as well. Upon completion, your device should be able to boot from both UEFI and BIOS machines using 32 or 64bit architectures.
Grub2 on USB Prerequisites:
- A running Linux OS (I.E. Ubuntu)
- Computer that can boot from USB
- USB flash drive (Fat32 formatted)
- No fear of the terminal
NOTE: This Guide assumes you are starting with a freshly Fat32 formatted USB thumb drive. Be aware you will be overwriting any existing bootloader and grub.cfg.
Get the latest Grub2 Source Code and Bootstrap it
Before proceeding, boot into your Linux desktop (Ubuntu was used here). Once running from your desktop, insert your flash drive. First we need to install required packages, grab the latest Grub2 source code from the repository, and then bootstrap it.
- To gain admin (root) access, open a terminal ctrl+alt+t, then type the following and press Enter.
- Optional Step: To update before proceeding, type
apt update && apt upgrade
- 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,
c. Then to bootstrap it,
Compiling Grub2 for BIOS and UEFI support
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.
- First create a separate working container for each platform,
mkdir EFI64 EFI32 BIOS
- Next to configure and then build for Grub2 64bit UEFI,
a. First change to the EFI64 directory,
b. Then to configure and make Grub2 for 64Bit UEFI,
../configure --target=x86_64 --with-platform=efi && make
- Now to configure and then make for Grub2 32 bit UEFI,
a. Change to the EFI32 directory,
b. Then to configure and make Grub2 for 32bit UEFI,
../configure --target=i386 --with-platform=efi && make
- Finally to configure and build for Grub2 BIOS
a. Change to the BIOS directory,
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.
- First, you'll need to find out which dev is your USB. To do this type,
- Note which device belongs to your USB.
- Now, to make a directory to be used for the mount point type,
- 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/sdx /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
--efi-directory. In addition we'll include the
--force option to allow buggy BIOS systems to detect the USB as a Hard Disk.
- To install Grub2 64 bit UEFI x86_64-efi,
a. First change to the EFI64 container,
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
- Then to install Grub2 32 bit UEFI i386-efi,a. First change to the EFI32 container,
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
- Finally to put Grub2 BIOS i386-pc,
(replacing x with your device).a. First change to the BIOS container,
b. Then install i386-pc,
grub-install -d $PWD --force --no-floppy --target=i386-pc --boot-directory=/mnt/USB/boot /dev/sdx
Loopback USB Boot an Ubuntu ISO from the Stick
The next steps are optional, but will compliment your efforts. In these steps, you will add a simple example configuration file to your bootable 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.
- First, type the following to change to the grub directory on your USB.
- Next, to download an grub2 loopback configuration file, type
- Then, to navigate back to the root of the USB, type
- Now to download the Ubuntu.iso, type
wget "http://releases.ubuntu.com/groovy/ubuntu-20.10-desktop-amd64.iso" -O ubuntu.iso
- 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.
Optionally with a Grub2 Boot Menu to Loopback USB Boot from the Ubuntu ISO copied to your flash drive.