Do you use let's encrypt?





4990 votes ~ 18 comments

 

XML logo

Bootable grub USB stick (EFI and BIOS for Intel)
Posted by dkg on Mon 16 Mar 2015 at 23:12
Tags: , , ,

I'm using grub version 2.02~beta2-2.

I want to make a USB stick that's capable of booting Intel architecture EFI machines, both 64-bit (x86_64) and 32-bit (ia32). I'm starting from a USB stick which is attached to a running debian system as /dev/sdX. I have nothing that i care about on that USB stick, and all data on it will be destroyed by this process.

I'm also going to try to make it bootable for traditional Intel BIOS machines, since that seems handy.

I'm documenting what I did here, in case it's useful to other people.

Set up the USB stick's partition table:

parted /dev/sdX -- mktable gpt
parted /dev/sdX -- mkpart biosgrub fat32 1MiB 4MiB
parted /dev/sdX -- mkpart efi fat32 4MiB -1
parted /dev/sdX -- set 1 bios_grub on
parted /dev/sdX -- set 2 esp on
After this, my 1GiB USB stick looks like:
0 root@foo:~# parted /dev/sdX -- print
Model:  USB FLASH DRIVE (scsi)
Disk /dev/sdX: 1032MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name      Flags
 1      1049kB  4194kB  3146kB  fat32        biosgrub  bios_grub
 2      4194kB  1031MB  1027MB               efi       boot, esp

0 root@foo:~# 
make a filesystem and mount it temporarily at /mnt:
mkfs -t vfat -n GRUB /dev/sdX2
mount /dev/sdX2 /mnt
ensure we have the binaries needed, and add three grub targets for the different platforms:
apt install grub-efi-ia32-bin grub-efi-amd64-bin grub-pc-bin grub2-common

grub-install --removable --no-nvram --no-uefi-secure-boot \
     --efi-directory=/mnt --boot-directory=/mnt \
     --target=i386-efi

grub-install --removable --no-nvram --no-uefi-secure-boot \
     --efi-directory=/mnt --boot-directory=/mnt \
     --target=x86_64-efi

grub-install --removable --boot-directory=/mnt \
     --target=i386-pc /dev/sdX
At this point, you should add anything else you want to /mnt here! For example: And don't forget to cleanup:
umount /mnt
sync

 

Comments on this Entry

Re: Bootable grub USB stick (EFI and BIOS for Intel)
Posted by Anonymous (217.155.xx.xx) on Tue 17 Mar 2015 at 01:40
Thanks! I've learned many Gnu things from reading this.

[ Parent | Reply to this comment ]

Re: Bootable grub USB stick (EFI and BIOS for Intel)
Posted by Anonymous (149.7.xx.xx) on Tue 17 Mar 2015 at 10:13
I am afraid that will not work in all cases. Some BIOSes will only boot devices with one MBR partition marked as active, and the active flag is forbidden in the protective MBR when you use the GPT scheme.

The solution I found to that is to use a regular MBR partitionning rather than a GPT. Yes, there is an MBR partition type for EFI System Partition! There is no MBR partition type for BIOS boot area, but there is no need for it, as GRUB will then be able to install itself to the intersticial space between the MBR and the first cylinder, as usual in the old days.

[ Parent | Reply to this comment ]

Re: Bootable grub USB stick (EFI and BIOS for Intel)
Posted by dkg (38.109.xx.xx) on Tue 17 Mar 2015 at 15:48
[ View dkg's Scratchpad | View Weblogs ]
Thanks, this is a useful tip. I've never run into those kind of BIOSes; the ones i've seen just scrape the MBR from the first 512B sector of the USB stick and take it from there.

Can you point toward a specific BIOS that behaves this way?

[ Parent | Reply to this comment ]

Re: Bootable grub USB stick (EFI and BIOS for Intel)
Posted by Anonymous (149.7.xx.xx) on Fri 20 Mar 2015 at 12:33
I think I remember an old Packard-Bell laptop that has this kind of problem: for that computer, an USB stick without an active partition was "not bootable".

And I think current HP servers suffer from the same problem.

[ Parent | Reply to this comment ]

Re: Bootable grub USB stick (EFI and BIOS for Intel)
Posted by Anonymous (217.155.xx.xx) on Tue 17 Mar 2015 at 19:49
I've tons of non-EFI hardware lying around (which I hope to use for some d-i and jessie install image testing soon) so I could maybe test this out too, if it would be useful information.

For that matter, I've seen some super-old BIOSes (non- LBA capable) that can't read past a certain Cylinder/Head/Sector value, typically around 2GB from the beginning of the disk. That's a problem if loading any GRUB stage, the kernel image or initrd requires reading beyond that.

[ Parent | Reply to this comment ]

Re: Bootable grub USB stick (EFI and BIOS for Intel)
Posted by dkg (38.109.xx.xx) on Fri 20 Mar 2015 at 01:15
[ View dkg's Scratchpad | View Weblogs ]
> I could maybe test this out too, if it would be useful information.
Sure, that would certainly be useful.
> I've seen some super-old BIOSes (non- LBA capable) that can't read past a certain Cylinder/Head/Sector value, typically around 2GB from the beginning of the disk.
Are any of those BIOSes capable of booting from USB? The approach i describe above should work fine for PATA internal drives as well, so i suppose it's still relevant, though. you could resolve this by just capping the size of the first partition on the disk at 2GiB, though. That makes the mkpart command for the large partition:
parted /dev/sdX -- mkpart efi fat32 4MiB 2GiB
(in my example, my USB stick was only 1GiB, so it would make no difference.

[ Parent | Reply to this comment ]

Re: Bootable grub USB stick (EFI and BIOS for Intel)
Posted by Anonymous (217.155.xx.xx) on Mon 23 Mar 2015 at 20:30
Great, I'll reply back in a week or two after lots of testing.

I was referring to USB booting, yes. I saw this with an embedded system manufactured circa 2006.

The typical geometry of USB flash drives makes them more prone to this - they often reduce Heads and Sectors/Track to powers of two, like 128 and 32 respectively - non-LBA capable BIOS can thus only read the first 1024 cylinders. That might be 2 GiB (1024x128x32x512-byte sectors), or only 1 GiB at 64 heads.

Magnetic drives tend to use maximum H and S values, allowing to read the first 1024x255x63x512 =~ 8032 MiB instead.

[ Parent | Reply to this comment ]

Re: Bootable grub USB stick (EFI and BIOS for Intel)
Posted by Anonymous (72.22.xx.xx) on Thu 19 Mar 2015 at 13:24
A bunch of these tools are not available in the versions in Wheezy.

[ Parent | Reply to this comment ]

Re: Bootable grub USB stick (EFI and BIOS for Intel)
Posted by jerome (212.198.xx.xx) on Thu 17 Sep 2015 at 14:41
Thanks for your useful article!

I've followed it to make a 4GB USB stick bootable. It works on almost every computer I could try on - both EFI and BIOS - except one... It's a HP Compaq 6005 Pro with a HP 3047h motherboard and an AMD Athlon II X2 B22 processor.
I tried to resize the second partition - as you were talking about - but without changes... Do you have any idea please?

By the way, what the goal of the first partition?

Thank you very much in advance! :)

[ Parent | Reply to this comment ]

Re: Bootable grub USB stick (EFI and BIOS for Intel)
Posted by dkg (108.58.xx.xx) on Thu 17 Sep 2015 at 15:51
[ View dkg's Scratchpad | View Weblogs ]
The first partition is a designated area for GRUB to store its early stage code.

Looks like this can also be done with an MSDOS partition table (instead of GPT) and one explicit partition, leaving a large enough gap between the MBR and the partition that grub will use this "embedded zone" instead.

Maybe you could try to make such a bootable stick and see if it works on the system that the GPT stick doesn't work on?

[ Parent | Reply to this comment ]

Re: Bootable grub USB stick (EFI and BIOS for Intel)
Posted by jerome (212.198.xx.xx) on Thu 17 Sep 2015 at 16:36

Thanks for your answer!

I tried with only one partition on an MSDOS partition table and it worked on that computer - and also on EFI ones. I execute those commands for the partitioning:

parted /dev/sdX -- mktable msdos
parted /dev/sdX -- mkpart primary fat32 4MiB -1
parted /dev/sdX -- set 1 boot on

Is that what you were suggesting? Any ameliorations? :)

[ Parent | Reply to this comment ]

Re: Bootable grub USB stick (EFI and BIOS for Intel)
Posted by dkg (199.102.xx.xx) on Thu 17 Sep 2015 at 21:58
[ View dkg's Scratchpad | View Weblogs ]
Excellent, thanks for the reportback. What you did was just what i was recommending.

It sounds like the 1-partition/msdos-table approach is probably more the more portable approach. Thanks for the clarifications!

btw, what version of the BIOS was the HP 6005 Pro running? Sometimes an upgrade to the BIOS of the system will enable booting from a USB GPT image. It's possible that the older BIOS can only handle MSDOS partition tables.

[ Parent | Reply to this comment ]

Re: Bootable grub USB stick (EFI and BIOS for Intel)
Posted by jerome (212.198.xx.xx) on Tue 22 Sep 2015 at 09:16

Thanks for this idea, it's effectively not the last BIOS version. Unfortunately it's not mine so I don't want to upgrade it in case of any possible issue...

So, I tried another thing - since I were sad to fail with the GPT-table as you nicely suggest, an hybrid MBR with gdisk: http://www.rodsbooks.com/gdisk/hybrid.html

Regarding your partitioning commands, I added:

sgdisk --hybrid=2 /dev/sdX
sfdisk --change-id /dev/sdX 2 0c
sfdisk --activate=2 /dev/sdX

And... It's working - at least on the HP 6005 Pro and other computers I could try on!

[ Parent | Reply to this comment ]

Re: Bootable grub USB stick (EFI and BIOS for Intel)
Posted by Anonymous (188.174.xx.xx) on Tue 8 Dec 2015 at 12:38
Thanks for the detailed infos in this discussion! Trying to create a UEFI/BIOS bootable USB stick myself, but can't get the partitioning to work.

jerome, would you please write all the commands you've been using to create your single-partition hybrid MBR stick? Or anybody else who got the partitioning down? Thanks! (;

[ Parent | Reply to this comment ]

Re: Bootable grub USB stick (EFI and BIOS for Intel)
Posted by Anonymous (81.193.xx.xx) on Sun 24 Jan 2016 at 19:21
Yes, i'd love to have UEFI and BIOS boot on the same partition, and then i can boot to WinPE or any other boot.
Is it possible to someone make a simple guide please?
thanks

[ Parent | Reply to this comment ]