Is a hybrid Linux USB-Stick for UEFI & legacy BIOS possible?

my idea is to create an USB-Boot-Stick with Lubuntu that is able to boot on an older BIOS Laptop and an newer UEFI Systems.

This would be quite easy, when the Stick would be setup as a legacy boot device, but I want it to be an UEFI-able device.

  1. Does that make even sense?! Or did I understand the UEFI concept horrobly wrong?
  2. If necessary there can be two diffrent Linux installs (one for UEFI, one for legacy but both on the same drive)
  3. Boot a GPT Device on legacy Bios - will that work?

I can't see clear, it might be just a better idea to have a seperate legacy Linux boot stick, but I am curious to explore the boundries :)

So, is my idea of a hybrid linux-uefi-boot-usb-stick that would work with legacy bios possible?


(3) seems to be possible for linux with grub http://www.rodsbooks.com/gdisk/booting.html

Further, I do have an EF00 Partition ready on that stick, aside a Swap, Fat32 and two Ext4 partitions.


(1) seems to be possible too http://www.rodsbooks.com/bios2uefi/

This instructions are however from 2012, some time passed since - does anybody have a working example that is easier?


For other researchers of this topic:

Boot of Windows 7 from GPT disk on non EFI motherboard Is there any way to boot Windows 7/8 using BIOS on GPT?

http://www.borncity.com/blog/2012/07/25/uefi-emulation-auf-pcs/


UPDATE:

I have managed to create the UEFI part inlcuding secure Boot setup with rEFInd.

Following the given advice, I have used dd to copy gptmbr.bin to my drive, which now seems to do at least something on a BIOS-System: "This is not a bootable disk"

However, I have followed the advice and set the pmbr flag for the disk and the gpt legacy boot flag - still gettin this error, any ideas?

My ESP partition is the second partition on the disk and is setup with efi files.

The Setup:

  • 64GB Space
  • GPT Partitiontable
  • Disk has pmbr flag set
  • 1st Partition starts at 16MiB and is about 45GB, a fat32 primary for data
  • 2nd Partition starts around 45GB and is the EFI System(fat32) with a working rEFInd setup
  • 3rd - 5th Partition is home (ext4), swap and root(ext4) of my working Lubuntu 14.04.1

Using dd I have copied the gptmbr.bin of my compiled 6.02 Syslinux to the first 440bytes.

Under parted 3.2 I can see that my partition 2 has a boot and legacy_boot flag.

During boot I get No bootable disk found - if I set my 5th instead of the 2nd partition to be legacy_boot I get Missing OS

It's alive!

(As my old Question got deleted https://askubuntu.com/q/516730/319747)


My guess is, that I have to copy some of the *.c32 and other files to a syslinux folder eighter on my EFI (where?!) or on my root partition (/boot/syslinux?!) to get it to work - am I right? What files are essential?

Further, I guess I will need that syslinucx.cfg file too - right?


My goal would be at least a direct boot of the lubuntu system on the root ext4 partition.

Update:

Although I have no idea why, I got it working - but not with a manual install.

  • the pmbr flag was bad and prohibited my UEFI system from booting the stick as UEFI
  • the legacy_boot flag was necessary for my 5th partition (linux root)
  • I had to use extlinux --install /path/to/root/parition
  • I had to create a syslinux.cfg in the syslinux folder under boot of my root partition

I did all of this on a second smaller stick, then tried to copy just the syslinux folder, but had no luck - the extlinux --install seems to be mandatory.

Anybody knows what exactly extlinux --install does? Can it be done manually or how else could you install a newer version like 6.02 without installing it on to your system?

Got my stick working with the partitions mentioned on BIOS, UEFI and UEFI SecureBoot, hell of a ride, learned a lot, nearly killed 2TB with parted, be careful and good luck.


It's possible, and it's very frequently done with both external USB sticks and internal drives.

Regarding partition table types:

  • BIOS normally doesn't need any partition table. It is only interested in the bootstrap code part that is the first 440 bytes of your MBR. (Although there are exceptions. Some BIOS implementations actually do break if they cannot find a MBR with one of the usual partition types. Hopefully those are rare.)

    The GPT partition table doesn't physically replace a MBR – it always starts at sector 1, while the MBR is in sector 0 – so it's possible for a disk to have both. In fact, most GPT disks do have a "protective MBR" containing just a single partition that covers the entire disk, to prevent older MBR-only partitioning tools from accidentally destroying the data.

    So you can install, for example, the Syslinux boot sector into your MBR, and it will boot. (The syslinux-install_update script will do this for you, but you can look for gptmbr.bin in your syslinux package to do it manually.)

    Much like the "active" flag on MBR partitions, Syslinux will look for the "legacy BIOS bootable" flag on GPT partitions (bit 2 – UEFI spec 2.4 section 5.3.3 table 20); the aforementioned script also sets that flag automatically if you have sgdisk (from gptfdisk) installed.

    GRUB can also be used, but it's also a bit more annoying. Since GRUB 2 wants to "embed" parts of itself in the traditionally-unused sectors 1-62, it would overwrite a GPT that's stored in the same location. So you might need to create a dedicated partition of 2-4 MB and mark it as BIOS-bootable, so that grub-install would find itself an embedding place.

  • On the other hand, the UEFI specification does require support for MBR partition types (section 12.3.1) and assign the MBR partition type 0xEF to the EFI system partition (section 5.2.2).

    So if you for some reason cannot get your disk working with GPT, you still can create an EFI system partition on MBR, and it will be used as long as it has the correct filesystem and all the necessary files (section 12.3.1.3):

    For removable media devices there must be only one UEFI-compliant system partition, and that partition must contain an UEFI-defined directory in the root directory. The directory will be named EFI. All OS loaders and applications will be stored in a subdirectory below EFI called BOOT. There must only be one executable EFI image for each supported processor architecture in the BOOT directory. For removable media to be bootable under EFI, it must be built in accordance with the rules laid out in Section 3.4.1.1.

Regarding operating systems, Linux generally does not care the slightest bit about whether it's booted from BIOS today and from UEFI tomorrow. I've had to reboot my laptop from UEFI mode to "BIOS compatibility" mode and back maybe a hundred times this week, while trying to find a bug in the 3.17 UEFI support...


Note that the "BIOS to UEFI Transformation" article is about a quite different thing. It describes how to install UEFI itself – an UEFI implementation called "DUET" – to be started from a BIOS-only system. It has nothing to do with booting an operating system directly from BIOS.


Yes, this is possible.

The basic idea is to GPT partition your USB stick like this:

  1. BIOS boot partition (GPT type 1686148-6449-6E6F-744E-656564454649) - 1 MiB, no filesystem
  2. EFI system partition (GPT type C12A7328-F81F-11D2-BA4B-00A0C93EC93B) - 200 MiB, VFAT filesystem
  3. Linux boot partition (GPT type 0FC63DAF-8483-4772-8E79-3D69D8477DE4) - 1 GiB, ext4 filesystem
  4. Linux root/home file system (GPT type 0FC63DAF-8483-4772-8E79-3D69D8477DE4) - remaining space, XFS or Btrfs filesystem

This partitioning allows us to boot on Legacy systems because with GPT there is still space for an MBR (for backwards compatibility reasons) and Grub2 has enough space to install some middle stage into the BIOS boot partition. Legacy BIOSes don't care about the partitioning scheme and the Grub2/Linux Kernel have no issues understanding GPT on Legacy systems.

On UEFI systems, the UEFI firmware ignores the MBR and uses the EFI system partition as entry point.

For example, with Fedora, you basically have to do the following:

Make sure that the Linux Boot filesystem is mounted under /boot and that EFI system partition filesystem is mounted under /boot/efi.

(Re-)Install both legacy and UEFI boot loaders:

grub2-pc grub2-efi-x64 shim-x64 efibootmgr

(Re-)Generate the grub config files both for legacy and UEFI boot:

# grub2-mkconfig -o /boot/efi/EFI/fedora/grub.cfg
# grub2-mkconfig -o /boot/grub2/grub.cfg

Depending whether your system booted in legacy or UEFI mode you have to fix either the first or second file and replace (linux|initrd)16 with (linux|initrd)efi or the other way around.

Install grub into the MBR:

# grub2-install --target=i386-pc /dev/sd_your_usb_stick_device

Voila.