how to build grub2 bootloader from it's source and test it with qemu emulator

14,065

Of course you can.

As shown on the GRUB website, the grub source code is available via git from git.savannah.gnu.org.

Then it is theoretically just a question of

$ ./autogen.sh
$ ./configure
$ make
$ sudo make install

However, depending on your platform, grub's default target platform may or may not be what you want. So you will need to decide which firmware platform you want to use in QEMU, which depending on your architecture can be something like

  • (pc) BIOS
  • coreboot
  • (U)EFI
  • ieee1275 (open firmware)
  • u-boot

Your mentioning of Ubuntu matches at least 3 possible options from the above, but I'm going to be boring and assume you mean x86_64/amd64. Since you will be running GRUB under QEMU, it does not really matter which of the two likely platforms ("pc" or "efi") your physical computer is running. So let's live a little and go for the (U)EFI variant.

You will need some prerequisites installed before configuring and building, so

$ sudo apt-get install build-essential autoconf automake
$ sudo apt-get build-dep grub-efi-amd64

So a practical build may look a bit like this:

$ # Next command is optionnal (languages):
$ ./linguas.sh
$ ./autogen.sh
$ # Next parameters are optionnal:
$ ./configure --prefix=$HOME/local --platform=efi
$ make
$ # Next command is optionnal:
$ make check
$ make install

The easiest way to get a functioning GRUB image is probably with the grub-mkstandalone command:

$ $HOME/local/bin/grub-mkstandalone -O x86_64-efi -o mygrub.efi

Note: To install grub on /dev/sda disk (instead of QEMU), use:

$ sudo grub-install /dev/sda

Note: If you don't see GRUB menu when booting, check this question. It involves pressing Shift when booting or editing /etc/default/grub to comment GRUB_HIDDEN_TIMEOUT.

Then you need some kind of UEFI image to run under your QEMU. The default choice for x86 is called OVMF and is part of Tianocore EDK2 - the de facto open source implementation of UEFI. Due to legal technicalities with regards to redistribution of the FAT filesystem driver, many Linux distributions (including Ubuntu) do not include a pre-built one. But have no fear, it is pretty straightforward to build one yourself.

However, I am not going to take this answer further off-topic than I already have, so all I am going to say is have a read through the OVMF README and look through one or two only slightly outdated blog posts about it.

Share:
14,065
Project-A
Author by

Project-A

Updated on June 12, 2022

Comments

  • Project-A
    Project-A almost 2 years

    I want to know how to build grub 2 bootloader from it's source in ubuntu and test it with qemu emulator.

    I would also like to change the default background image of grub2 bootloader in the new build?

    Is this possible? If yes, how ?

  • Project-A
    Project-A over 8 years
    Thank you for your answer. But I would also like to know, if it is possible to customize the grub code, like removing the multi-boot functionality. If yes, give me a relative source.
  • KrisWebDev
    KrisWebDev about 8 years
    Forgetting to set the --prefix leads to error ./grub-install : error : /usr/local/lib/grub/i386-pc/modinfo.sh or similar.
  • unixsmurf
    unixsmurf over 7 years
    @KrisWebDev: only noticed your edits now. Would those not be more suitable as a separate answer?
  • WinEunuuchs2Unix
    WinEunuuchs2Unix almost 6 years
    Glad I found this. I'm on Ubuntu 64-bit with UEFI boot. My goal is to change the time out from seconds to 10/th of a second. So GRUB_TIMEOUT=25 is no longer 25 seconds but 2.5 seconds. It's for a smoother "tick" countdown with circular progress display under gfxmenu. Since you are so knowledgeable I wonder if there is a Grub Forum somewhere I can lurk or read up on some tips to help in the journey?
  • unixsmurf
    unixsmurf almost 6 years
    @WinEunuuchs2Unix: well, there's always the grub development mailing list archives - lists.gnu.org/archive/html/grub-devel.
  • Yamian Quintero
    Yamian Quintero about 5 years
    Just one small detail, it should be: ./configure --prefix=$PWD/_install --**with-**platform=efi