Page 1 of 1

Bootable USB with GRUB2

Posted: Sat Dec 12, 2020 9:54 pm
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?

Re: Bootable USB with GRUB2

Posted: Sat Dec 12, 2020 10:07 pm
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.

Re: Bootable USB with GRUB2

Posted: Sat Dec 12, 2020 10:11 pm
by jamesread
What if I want to support both legacy and UEFI booting?

Re: Bootable USB with GRUB2

Posted: Sat Dec 12, 2020 10:14 pm
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.

Re: Bootable USB with GRUB2

Posted: Sat Dec 12, 2020 10:18 pm
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.

Re: Bootable USB with GRUB2

Posted: Sat Dec 12, 2020 10:27 pm
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.

Re: Bootable USB with GRUB2

Posted: Sat Dec 12, 2020 10:29 pm
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.

Re: Bootable USB with GRUB2

Posted: Sat Dec 12, 2020 10:53 pm
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...

Re: Bootable USB with GRUB2

Posted: Sat Dec 12, 2020 11:52 pm
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.

Re: Bootable USB with GRUB2

Posted: Sun Dec 13, 2020 3:24 pm
by Octocontrabass
jamesread wrote:Then I ran:
Wouldn't it be easier to use grub-mkrescue to do all of that?

Re: Bootable USB with GRUB2

Posted: Tue Dec 15, 2020 12:22 pm
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.

Re: Bootable USB with GRUB2

Posted: Tue Dec 15, 2020 12:42 pm
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.

Re: Bootable USB with GRUB2

Posted: Tue Dec 15, 2020 1:17 pm
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?

Re: Bootable USB with GRUB2

Posted: Tue Dec 15, 2020 1:53 pm
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

Re: Bootable USB with GRUB2

Posted: Tue Dec 15, 2020 2:28 pm
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.