How to create an Hybrid UEFI GPT + BIOS GPT/MBR boot USB disk

An Hybrid UEFI GPT + BIOS GPT/MBR boot USB disk is a disk that works on both UEFI and BIOS PCs

Requisites:

  • A working Linux installation with the following packages: grub2, grub2-efi, gdisk (on Ubuntu: grub-pc-bin, grub-efi, grub-efi-amd64, gdisk)

  • An USB drive
  • Some Linux ISO.

Step 1: get the right /dev

From now on we will use a root shell because almost all commands we will use require it.

root@buntu:~# fdisk -l

...............

Disk /dev/sdb: 14.9 GiB, 16005464064 bytes, 31260672 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x70707573

Device     Boot Start      End  Sectors  Size Id Type
/dev/sdb1           2 31260671 31260670 14.9G af HPFS / NTFS / exFAT

Find the right dev (ie: mine was /dev/sdb and I WILL USE IT IN THE GUIDE, you have to use your dev).

Step 2: make GUID partition table and partitions

root@buntu:~# gdisk /dev/sdb
And then we are in gdisk prompt; from now on just give the commands in bold and the press enter.

GPT fdisk (gdisk) version 0.8.10

Partition table scan:

  • MBR: MBR only
    BSD: not present
    APM: not present
    GPT: not present

***************************************************************
Found invalid GPT and valid MBR; converting MBR to GPT format
in memory. THIS OPERATION IS POTENTIALLY DESTRUCTIVE! Exit by
typing 'q' if you don't want to convert your MBR partitions
to GPT format!
***************************************************************

Warning! Main partition table overlaps the first partition by 32 blocks!
You will need to delete this partition or resize it in another utility.

Warning! Secondary partition table overlaps the last partition by
33 blocks!
You will need to delete this partition or resize it in another utility.

Command (? for help): o
This option deletes all partitions and creates a new protective MBR.
Proceed? (Y/N): y

Command (? for help): n Partition number (1-128, default 1): 
First sector (34-31260638, default = 2048) or {+-}size{KMGTP}:
Last sector (2048-31260638, default = 31260638) or {+-}size{KMGTP}: +1M
Current type is 'Linux filesystem' Hex code or GUID (L to show codes, Enter = 8300): EF02
Changed type of partition to 'BIOS boot partition'

Command (? for help): n
Partition number (2-128, default 2):
First sector (34-31260638, default = 4096) or {+-}size{KMGTP}:
Last sector (4096-31260638, default = 31260638) or {+-}size{KMGTP}: +50M
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300): EF00
Changed type of partition to 'EFI System'

Command (? for help): n
Partition number (3-128, default 3):
First sector (34-31260638, default = 106496) or {+-}size{KMGTP}:
Last sector (106496-31260638, default = 31260638) or {+-}size{KMGTP}: 
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300): 0700
Changed type of partition to 'Microsoft basic data'

Command (? for help): w

Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!

Do you want to proceed? (Y/N): y
OK; writing new GUID partition table (GPT) to /dev/sdb.
The operation has completed successfully.
Now gdisk will quit, it’s ok.

Step 3: make hybrid GUID/MBR

root@buntu:~# gdisk /dev/sdb
GPT fdisk (gdisk) version 0.8.10

Partition table scan:
  MBR: protective
  BSD: not present
  APM: not present
  GPT: present

Found valid GPT with protective MBR; using GPT.

Command (? for help): r

Recovery/transformation command (? for help): h

WARNING! Hybrid MBRs are flaky and dangerous! If you decide not to use one,
just hit the Enter key at the below prompt and your MBR partition table will
be untouched.

Type from one to three GPT partition numbers, separated by spaces, to be
added to the hybrid MBR, in sequence: 1 2 3
Place EFI GPT (0xEE) partition first in MBR (good for GRUB)? (Y/N): n

Creating entry for GPT partition #1 (MBR partition #1)
Enter an MBR hex code (default EF): EF02
Set the bootable flag? (Y/N): n

Creating entry for GPT partition #2 (MBR partition #2)
Enter an MBR hex code (default EF): EF00
Set the bootable flag? (Y/N): n

Creating entry for GPT partition #3 (MBR partition #3)
Enter an MBR hex code (default 07): 0700
Set the bootable flag? (Y/N): y

Recovery/transformation command (? for help): w

Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!

Do you want to proceed? (Y/N): y
OK; writing new GUID partition table (GPT) to /dev/sdb.
The operation has completed successfully.
gdisk will quit (again) and it’s ok.

Step 4: format the partitons

We have to format in FAT32 two of the three partitions, /dev/sdb2 and /dev/sdb3. Umount the partitions if mounted and then:

root@buntu:~# mkfs.fat -F32 -n GRUB2EFI /dev/sdb2
mkfs.fat 3.0.27 (2014-11-12)

root@buntu:~# mkfs.fat -F32 -n DATA /dev/sdb3
mkfs.fat 3.0.27 (2014-11-12)

Now the partitons are ready, we will mount /devsdb2 to /mnt/efi and /dev/sdb3 to /mnt/data.

root@buntu:~# mkdir /mnt/efi
root@buntu:~# mkdir /mnt/data
root@buntu:~# mount /dev/sdb2 /mnt/efi/
root@buntu:~# mount /dev/sdb3 /mnt/data/

Step 5: install grub

In this step we will install grub in EFI partition (mounted in /mnt/efi) and in MBR.

root@buntu:~# grub-install --target=x86_64-efi --efi-directory=/mnt/efi --boot-directory=/mnt/data/boot --removable --recheck
Installing for x86_64-efi platform.
Installation finished. No error reported.


root@buntu:~# grub-install --target=i386-pc --recheck --boot-directory=/mnt/data/boot /dev/sdb
Installing for i386-pc platform.
Installation finished. No error reported.

Step 6: create folders for ISOs

In /mnt/data we should now have only one directory, boot; let’s create a directory for the ISOs.

root@buntu:~# mkdir /mnt/data/iso
In /mnt/data we should now have
/mnt/data/
– – – – – – – /boot/
– – – – – – – – – – – /grub/
– – – – – – – /iso/
Now we add 2 folders in /iso, one for the other ISOs and one for Debian (this is a workaround for a known bug).
root@buntu:~# mkdir /mnt/data/iso/debian
root@dbuntu:~# mkdir /mnt/data/iso/others

Let’s download some ISOs, here i will explain ho to configure grub to boot Debian XFCE amd64 Installer and Tails i386; here there are instructions for other distros.

Debian

Download:

And put these files into /mnt/data/debian/

Tails

Download the iso torrent. Then create a folder for tails and copy there the iso.

root@buntu:~# mkdir /mnt/data/others/tails

In /mnt/data we should now have
/mnt/data/
– – – – – – – /boot/
– – – – – – – – – – – /grub/
– – – – – – – /iso/
– – – – – – – – – – – /debian/
– – – – – – – – – – – – – – – – debian-8.5.0-amd64-xfce-CD-1.iso
– – – – – – – – – – – – – – – – vmlinuz
– – – – – – – – – – – – – – – – initrd.gz
– – – – – – – – – – – /others/
– – – – – – – – – – – – – – – – /tails/
– – – – – – – – – – – – – – – – – – – – tails-i386-2.5.iso

Step 7: configure grub

Now we need to configure grub; grub at boot reads its config from a config file (grub.cfg). Create a grub.cfg file with the following content and copy it into /mnt/data/boot/grub/:

# path to the partition holding ISO images (using UUID)
probe -u $root --set=rootuuid
set imgdevpath="/dev/disk/by-uuid/$rootuuid"

insmod exfat

# Setup video
if [ "$grub_platform" = "efi" ]
then
    insmod efi_gop
    insmod efi_uga
fi

if [ "$grub_platform" = "pc" ]
then
    insmod vbe
fi

# Setup font
insmod font

if loadfont ${prefix}/fonts/unicode.pf2
then
    insmod gfxterm
    set gfxmode=auto
    set gfxpayload=keep
    terminal_output gfxterm
fi

# Setup GRUB Screen
insmod jpeg
if background_image /boot/grub/grub_bg.jpg ; then
    set color_normal=white/black
    set color_highlight=white/green
else
    set menu_color_normal=white/black
    set menu_color_highlight=black/light-gray
fi

menuentry "Tails 2.5 i386" {
    set isofile='/iso/others/tails/tails-i386-2.5.iso'
    loopback loop $isofile
    linux (loop)/live/vmlinuz2 boot=live config findiso=${isofile} live-media=removable apparmor=1 security=apparmor nopersistent noprompt timezone=Etc/UTC block.events_dfl_poll_msecs=1000 noautologin module=Tails i2p
    initrd (loop)/live/initrd2.img
}

menuentry 'Debian 8.5.0 amd64 CD 1 XFCE' {
        set isofile='/iso/debian/debian-8.5.0-amd64-xfce-CD-1.iso'
        set initrdfile='/iso/debian/initrd.gz'
        loopback loop $isofile
        linux (loop)/install.amd/vmlinuz iso-scan/ask_second_pass=true
        initrd $initrdfile
}
menuentry "Reboot" {
        echo "System rebooting..."
        reboot
}

menuentry "Shutdown" {
        echo "System shutting down..."
        halt
}

It’s done! If you want to customize grub’s background copy a .jpg into /mnt/boot/grub/ and rename it grub_bg.jpg. Now unmount the USB drive and test it.
root@buntu:~# umount /mnt/efi
root@buntu:~# umount /mnt/data

Sources: arch linux wiki and many other sites like Ninux

19 commenti

  1. ciao,
    when i try to install grub on both /mnt/efi/ or /mnt/data/, i get the error : grub-probe failed to get canonical path of “aufs”
    grazzie

  2. when i try usb key with uefi or bios, the same message appears :

    Minimal BASH-like line editing is supported. For the first word, TAB lists…..

    grub>

    I’ve copied exactly your grub.conf in /mnt/data/boot/grub/.

    1. Hi Steph. Took me a while to figure this out too. It is because GRUB cant find its config file. He has called it “grub.conf” but if you rename if to “grub.cfg” you should find it works. Hope this helps. (Huge thanks to Slivermetal for this excellent giude!)

  3. I’m guessing all of this has to be done from a Linux distro that is not the usb you want to make hybrid…? I have Linux on an external hdd that I want to be able to run on uefi and bios PC’s and it the only Linux OS I have. I’m supossing I can’t do it from the hdd I want to make hybrid?

  4. Any update on STEP 5? Whenever and however I try to punch it in I get
    bash: grub-install: command not found…
    Green as hell to Linux, so any noob help would be appreciated. Googled the s**t out of it for the last 3 days with no avail …

    1. which distro do you use?
      you wrote grub-install –target=x86_64-efi –efi-directory=/mnt/efi –boot-directory=/mnt/data/boot –removable –recheck and nothing happened?

  5. Sorry for lack of detail.
    I’ve tried this procedure on Debian 10 Buser.
    My step5:

    # grub-install –target=x86_64-efi –efi-directory=/mnt/efi –boot-directory=/mnt/data/boot –removable –recheck
    grub-install: error: /usr/lib/grub/x86_86-efi/modinfo.sh doesn’t exist. Please specify –target or –directory.

    I realize that this probably is some rookie mistake on my part…

  6. Step 6: found a slight error (10 minutes for noob like me to figure out, so no biggy)
    —–
    Download:
    iso from http or torrent
    vmlinuz
    initrd
    And put these files into /mnt/data/debian/
    —–
    Files should be put int /mnt/data/iso/debian/ as per table in the next step.
    Also found an issiu with the lin to vmlinuz . Chromium blures out something like
    FatalFailure: WebGL1 blacklisted
    Any advise?

    Another question. Any referrals on how to make this dual boot usb disk to launch not the installer but the OS itself? Making it boot full Debian solution for exmaple?

    P.S. While step-by-stepping-on-my-toes thru this manual, learned a great deal of new stuff. Thank you very mutch for that.

  7. sudo grub-install –target=x86_64-efi –efi-directory=/mnt/efi –boot

    Get error in kali linux:
    grub-install: option ‘–boot’ is ambiguous; possibilities: ‘–boot-directory’ ‘–bootloader-id’
    Try ‘grub-install –help’ or ‘grub-install –usage’ for more information.

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *