Can I install grub2 on a flash drive to boot both BIOS and UEFI
Solution 1
Yes, you can just install Grub2 EFI and Legacy side by side. They don't clobber each other and the ordering doesn't matter.
The precondition for this to work is that you use GPT partitioning and that you have an BIOS boot partition (1 MiB is enough).
Otherwise, the legacy MBR Grub2 install will fail since there isn't enough space for its intermediate stage.
The Grub2 install commands look like this - to install into the MBR:
grub2-install --target=i386-pc /dev/sdb
And to install into the EFI system partition (ESP) it's either (after the ESP is mounted under /boot/efi
)
grub2-install --target x86_64-efi
or, on Fedora-like distributions it's instead:
dnf reinstall grub2-efi-x64 shim-x64
(grub2-install works there too, but needs an additional package and breaks secure boot)
Solution 2
You can try this tuto_by_sysmatck_ubuntuforums.org
First Step: Format USB drive
To create a EFI firmware compatible boot drive, you need a GPT partition table and at least one FAT32 partition. Do as follows...
sudo apt-get install gdisk
sudo sgdisk --zap-all /dev/sdb
You probably need to remove and insert the USB drive again at this point for the kernel to update information about it...
sudo sgdisk --new=1:0:0 --typecode=1:ef00 /dev/sdb
sudo mkfs.vfat -F32 -n GRUB2EFI /dev/sdb1
Second Step: Copy files and Set directory structure
Let's mount the usb drive
sudo mount -t vfat /dev/sdb1 /mnt -o uid=1000,gid=1000,umask=022
To make life easier, I created a pack with all necessary files for you to modify as you need usb-pack_efi. If you don't trust my files, create yours using this page as reference.
Extract the zip file and paste those inner files using Command Line Interface or a file manager you like.
cd ~/Downloads/
unzip usb-pack_efi.zip
rsync -auv usb-pack_efi/ /mnt
The most important files are bootia32.efi to boot on 32bit machines, bootx64.efi to boot on 64bit machines and grub.cfg to setup grub to load ISOs or chainload to other paths. In the end, you might get a directory tree like this:
Third Step: Install GRUB2 on the drive
sudo grub-install --removable --boot-directory=/mnt/boot --efi-directory=/mnt/EFI/BOOT /dev/sdb
Fourth Step: Setup ISOs to be loaded
Put (copy) the .iso files you want to load in /mnt/iso/
and setup grub.cfg
like the existing examples...
Note that the most important variable to set is isofile. There is lots of examples on the web about how to configure grub2 menu. Use #
to comment those lines you don't want to use, e.g. to hide a configuration of absent .iso at /iso.
Last Step: Configure firmware and Test First and most important, deactivate secure boot on your computer's firmware. Search on the web if you don't know how.
To boot the USB drive you can set your machine firmware to search first for the USB device (boot order). Or you can choose what drive to boot as soon as you turn on your computer. Each manufacturer has its own keys to do it. Search for your machine's manual if needed.
thank you sysmatck
Related videos on Youtube
deitch
Updated on September 18, 2022Comments
-
deitch over 1 year
Well, obviously I can, because all of the Linux distros are delivered as hybrid ISOs that can boot both BIOS and UEFI.
The question is how? I had thought I could run
grub-install
twice:grub-install --target=i386-pc --recheck --boot-directory=/mnt/boot /dev/sdX grub-install --target x86_64-efi --efi-directory /mnt --boot-directory=/mnt/boot --removable
and that it would install both. However, doesn't one clobber the other?
When it comes down to it, I don't understand enough of what grub installs in the MBR (BIOS or UEFI mode) to know. In theory, UEFI shouldn't care about the MBR itself, but just unsure.
FYI, running
grub-install
off of an Ubuntu LiveCD in VirtualBox because, well, because it was there.-
Admin about 8 yearsTo answer my own question, it does just work. I think you need to run BIOS first, then EFI, but it does work.
-
-
deitch about 8 yearsThanks for the detail... but you didn't answer about booting both BIOS and UEFI
-
deitch about 6 yearsBeen a long time since I posted this question. Since then, I have gotten very deep into the structures of filesystems, bootloaders, etc. I long since stopped using grub whenever possible, and just do simplistic loaders, except when I really need to multi-boot. Thanks!
-
SebMa about 4 years@maxschlepzip Aren't you forgetting the
--boot-directory
option ? I just copied 'n' pasted your command with-v
and noticed it copied the files into the/boot/grub
directory. -
maxschlepzig about 4 years@SebMa - I don't use the
--boot-directory
option. I execute these commands in a chroot when I target a flash drive. -
SebMa about 4 years@maxschlepzip Can you please give your
pwd
output and give thechroot
argument you use so I can understand how the files got copied in/mnt/boot
? -
maxschlepzig about 4 yearsUsing a chroot basically requires a few setup steps like illustrated here: gist.github.com/gsauthof/7c0b65ffe4da38b83c8c61d79b71c6d7 - in that example
/boot
inside the chroot is located under/mnt/root/root/boot
outside of the chroot. That means you would execute the grub install commands after entering the chroot, i.e. inside the chroot. -
SebMa about 4 years@maxschlepzig Why do you need a chroot environment in the first place to install grub into a flash drive ? Why not simply call
grub-install
with the--boot-directory=/mnt/boot
argument ? -
maxschlepzig about 4 years@SebMa Because the grub on my host system isn't necessarily compatible with the one I install on a flash drive