how to build grub2 bootloader from it's source

2019-05-10 15:13发布

问题:

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 ?

回答1:

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.