As you request a clear illustration of how to do it, here it is. I assume you have your live Linux booted.
Partition the USB thumb drive
I recommend the command line tool gdisk. It produces very clean results. Alternatively, you can use gparted.
Create a new partiton table. Use GPT with a protective MBR.
Define these partitions:
- (optional) A data partition
- A Linux partition
- A legacy BIOS boot partition (1MB is enough)
- A EFI System partition (at least 32MB)
Example: On my 64GB thumb drive, the result looks like this:
Number Start (sector) End (sector) Size Code Name
1 2048 107632639 51.3 GiB 0700 DATA
2 107632640 124411903 8.0 GiB 8300 Linux filesystem
3 124411904 124413951 1024.0 KiB EF02 BIOS boot partition
4 124413952 124822453 199.5 MiB EF00 EFI System
Install Linux to the USB thumb drive using any method.
During the process, format the Linux partition with a filesystem of your choice, preferably ext4. Use this partition as root /.
Format the EFI System partition with FAT16.
The BIOS boot partition remains unformatted.
Install GRUB twice
In a final step, install GRUB for both boot methods, UEFI style booting and legacy BIOS booting.
grub-install --target=x86_64-efi --removable /dev/sdx
grub-install --target=i386-pc /dev/sdx
Where /dev/sdx is your USB thumb drive, obviously.
That --removable is important. Took me three hours to realize I need it on a removable USB thumb drive...
If you install GRUB to the thumb drive from the "outside" (not having booted the Linux from the USB thumb drive), you need to mount the Linux partition first. Maybe you mount it into /mnt. Then you mount the EFI System partition into the Linux partitions /mnt/boot/efi directory. Use --root=/mnt as parameter for grub-install. Only then grub-install finds all necessary directories.
Done.
For further reading:
We did not really define partitions in the MBR. So you may ask, why it does work on legacy BIOS machines. Keep in mind, we installed GRUB into the MBR. During the legacy BIOS boot process, the bootloader stored in the MBR is executed. This loads stage 1 of GRUB, which then proceeds to load stage 2 from the legacy BIOS boot partition. But at this time, GRUB does not actually know anything about partitions MBR or otherwise. For this reason, the information about the position of the BIOS boot partition has been embedded into GRUB stage 1. Consequently, if the BIOS boot partition is moved, you need to reinstall GRUB. After GRUB stage 2 is loaded, GRUB understands GPT and can continue with booting the Linux kernel.