Bootable USB with GRUB2

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
jamesread
Member
Member
Posts: 49
Joined: Wed Dec 09, 2020 11:32 am

Bootable USB with GRUB2

Post by jamesread »

Hi,

I am working through https://wiki.osdev.org/GRUB#USB_instructions and have hit a problem. I managed to format my USB stick with

Code: Select all

sudo mkfs.vfat -F 32 -n YourLabel -I /dev/sdb
But when running

Code: Select all

sudo grub-install --root-directory=/media/YourLabel --no-floppy --recheck --force /dev/sdb
I get the error

Code: Select all

Installing for x86_64-efi platform.
grub-install: error: cannot find EFI directory.
Any idea how to fix this error?
Octocontrabass
Member
Member
Posts: 5568
Joined: Mon Mar 25, 2013 7:01 pm

Re: Bootable USB with GRUB2

Post by Octocontrabass »

Assuming you want to install the 64-bit UEFI version of GRUB, you need to add the "--efi-directory" parameter to tell grub-install which directory will be used as the EFI system partition. (For a USB flash drive, it would be the same as the root directory, so use "--efi-directory=/media/YourLabel".)

If you actually want to install the legacy BIOS version of GRUB, add the "--target=i386-pc" parameter to your grub-install command instead.
jamesread
Member
Member
Posts: 49
Joined: Wed Dec 09, 2020 11:32 am

Re: Bootable USB with GRUB2

Post by jamesread »

What if I want to support both legacy and UEFI booting?
jamesread
Member
Member
Posts: 49
Joined: Wed Dec 09, 2020 11:32 am

Re: Bootable USB with GRUB2

Post by jamesread »

Octocontrabass wrote:Assuming you want to install the 64-bit UEFI version of GRUB, you need to add the "--efi-directory" parameter to tell grub-install which directory will be used as the EFI system partition. (For a USB flash drive, it would be the same as the root directory, so use "--efi-directory=/media/YourLabel".)
This produces the following error:

Code: Select all

Installing for x86_64-efi platform.
grub-install: error: /media/YourLabel doesn't look like an EFI partition.
Octocontrabass
Member
Member
Posts: 5568
Joined: Mon Mar 25, 2013 7:01 pm

Re: Bootable USB with GRUB2

Post by Octocontrabass »

jamesread wrote:What if I want to support both legacy and UEFI booting?
Run grub-install twice, once for each version. (Three times if you also want to support 32-bit UEFI.)
jamesread wrote:This produces the following error:
Whoops! I forgot, you need the "--removable" parameter to install the UEFI version on a removable drive.
jamesread
Member
Member
Posts: 49
Joined: Wed Dec 09, 2020 11:32 am

Re: Bootable USB with GRUB2

Post by jamesread »

Octocontrabass wrote:
jamesread wrote:This produces the following error:
Whoops! I forgot, you need the "--removable" parameter to install the UEFI version on a removable drive.
This produces the following error:

Code: Select all

sudo grub-install --root-directory=/media/YourLabel --efi-directory=/media/YourLabel --removable --no-floppy --recheck --force /dev/sdb
Installing for x86_64-efi platform.
grub-install: error: /media/YourLabel doesn't look like an EFI partition.
jamesread
Member
Member
Posts: 49
Joined: Wed Dec 09, 2020 11:32 am

Re: Bootable USB with GRUB2

Post by jamesread »

Octocontrabass wrote:Assuming you want to install the 64-bit UEFI version of GRUB, you need to add the "--efi-directory" parameter to tell grub-install which directory will be used as the EFI system partition. (For a USB flash drive, it would be the same as the root directory, so use "--efi-directory=/media/YourLabel".)

If you actually want to install the legacy BIOS version of GRUB, add the "--target=i386-pc" parameter to your grub-install command instead.
I tried to make a legacy and this is what happened:

Code: Select all

sudo grub-install --root-directory=/media/YourLabel --target=i386-pc --no-floppy --recheck --force /dev/sdb
Installing for i386-pc platform.
grub-install: warning: Filesystem `fat' doesn't support embedding.
grub-install: error: embedding is not possible, but this is required for cross-disk install.
Octocontrabass
Member
Member
Posts: 5568
Joined: Mon Mar 25, 2013 7:01 pm

Re: Bootable USB with GRUB2

Post by Octocontrabass »

It sounds like the wiki page needs an update. The legacy BIOS version of GRUB requires a partitioned disk to install on a FAT volume.

UEFI requires partitioned media as well, so that might be why the UEFI version of GRUB won't install. I'll have to dig up a flash drive to test...
jamesread
Member
Member
Posts: 49
Joined: Wed Dec 09, 2020 11:32 am

Re: Bootable USB with GRUB2

Post by jamesread »

I've had partial success with the following:

First I made a myos/grub.cfg file with the following contents:

Code: Select all

set timeout=15
set default=0 # Set the default menu entry
 
menuentry "OS Name" {
   multiboot /boot/kernel-file   # The multiboot command replaces the kernel command
   boot
}
Then I ran:

Code: Select all

$ mkdir myos
$ grub-mkstandalone    --format=x86_64-efi    --output=myos/bootx64.efi    --locales=""    --fonts=""    "boot/grub/grub.cfg=myos/grub.cfg"
$ (    cd myos &&    dd if=/dev/zero of=efiboot.img bs=1M count=10 &&    sudo mkfs.vfat efiboot.img &&    LC_CTYPE=C mmd -i efiboot.img efi efi/boot &&    LC_CTYPE=C mcopy -i efiboot.img ./bootx64.efi ::efi/boot/; )
$ grub-mkstandalone    --format=i386-pc    --output=myos/core.img    --install-modules="linux16 linux normal iso9660 biosdisk memdisk search tar ls"    --modules="linux16 linux normal iso9660 biosdisk search"    --locales=""    --fonts=""    "boot/grub/grub.cfg=myos/grub.cfg"
$ cat /usr/lib/grub/i386-pc/cdboot.img myos/core.img > myos/bios.img
$ sudo xorriso    -as mkisofs    -iso-level 3    -full-iso9660-filenames    -volid "MYOS"    -eltorito-boot boot/grub/bios.img    -no-emul-boot    -boot-load-size 4    -boot-info-table    --eltorito-catalog boot/grub/boot.cat    --grub2-boot-info    --grub2-mbr /usr/lib/grub/i386-pc/boot_hybrid.img    -eltorito-alt-boot    -e EFI/efiboot.img    -no-emul-boot    -append_partition 2 0xef myos/efiboot.img    -output "./myos.iso"    -graft-points       "."       /boot/grub/bios.img=myos/bios.img       /EFI/efiboot.img=myos/efiboot.img
$ qemu-system-x86_64 -bios ~/osdev002/OVMF-pure-efi.fd -m 64 -cdrom myos.iso -serial stdio
This gave me a grub menu so I'm imagining partial success. I just need a "Hello World" kernel image that I can boot now.
Octocontrabass
Member
Member
Posts: 5568
Joined: Mon Mar 25, 2013 7:01 pm

Re: Bootable USB with GRUB2

Post by Octocontrabass »

jamesread wrote:Then I ran:
Wouldn't it be easier to use grub-mkrescue to do all of that?
jamesread
Member
Member
Posts: 49
Joined: Wed Dec 09, 2020 11:32 am

Re: Bootable USB with GRUB2

Post by jamesread »

Octocontrabass wrote:
jamesread wrote:Then I ran:
Wouldn't it be easier to use grub-mkrescue to do all of that?
What exact command would you use?

Is the tutorial going to be updated? The instructions clearly don't work.
Octocontrabass
Member
Member
Posts: 5568
Joined: Mon Mar 25, 2013 7:01 pm

Re: Bootable USB with GRUB2

Post by Octocontrabass »

jamesread wrote:What exact command would you use?
Assuming the appropriate GRUB targets are already installed:

Code: Select all

grub-mkrescue -o myos.iso myos
jamesread wrote:Is the tutorial going to be updated?
Are you offering to update it? We could really use the help.
jamesread
Member
Member
Posts: 49
Joined: Wed Dec 09, 2020 11:32 am

Re: Bootable USB with GRUB2

Post by jamesread »

Octocontrabass wrote:
jamesread wrote:What exact command would you use?
Assuming the appropriate GRUB targets are already installed:

Code: Select all

grub-mkrescue -o myos.iso myos
jamesread wrote:Is the tutorial going to be updated?
Are you offering to update it? We could really use the help.

I made the iso image with grub-mkrescue and ran the image in qemu. It drops immediately to a grub command line without showing any kind of menu.

Regarding updating the wiki. Of course, I would love to help once I've got sufficient knowledge and experience to do so. You would think some form of grub-install command would work but alas up to now I haven't managed to find one that does. Have you managed to get anything working?
User avatar
bzt
Member
Member
Posts: 1584
Joined: Thu Oct 13, 2016 4:55 pm
Contact:

Re: Bootable USB with GRUB2

Post by bzt »

jamesread wrote:I made the iso image with grub-mkrescue and ran the image in qemu. It drops immediately to a grub command line without showing any kind of menu.
It sounds like you have missed step 4 on the wiki, copying grub.cfg over.
jamesread wrote:You would think some form of grub-install command would work but alas up to now I haven't managed to find one that does. Have you managed to get anything working?
As long as CDROM concerned, here's what I use. As for the USB boot, well, I've found figuring out Grub so much difficult and overcomplicated, that I ended up writing my own loader and image creator instead...

Cheers,
bzt
jamesread
Member
Member
Posts: 49
Joined: Wed Dec 09, 2020 11:32 am

Re: Bootable USB with GRUB2

Post by jamesread »

bzt wrote:
jamesread wrote:I made the iso image with grub-mkrescue and ran the image in qemu. It drops immediately to a grub command line without showing any kind of menu.
It sounds like you have missed step 4 on the wiki, copying grub.cfg over.
Yep. That was it. It works now. Now I just need to figure out how to make a hello world kernel to get me started.
Post Reply