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

1

11

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 ?

Chanson answered 4/8, 2015 at 1:36 Comment(2)
Changing the background image is not a programming task, so you can ask on superuser.com for that part of the question.Banting
You can use commands like these in your /etc/default/grub: GRUB_BACKGROUND="/usr/share/grub/themes/Tuxkiller2/1600x900-TuxRestingOnWindowsTB.png" will give a background image. GRUB_THEME="/usr/share/grub/themes/Tuxkiller2/theme.txt" will give you graphical menu with background image, icons, custom fonts, menu borders, item borders, etc.Ghazi
B
14

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.

Banting answered 11/8, 2015 at 19:34 Comment(6)
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.Chanson
Forgetting to set the --prefix leads to error ./grub-install : error : /usr/local/lib/grub/i386-pc/modinfo.sh or similar.Contributor
@KrisWebDev: only noticed your edits now. Would those not be more suitable as a separate answer?Banting
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?Ghazi
@WinEunuuchs2Unix: well, there's always the grub development mailing list archives - lists.gnu.org/archive/html/grub-devel.Banting
Just one small detail, it should be: ./configure --prefix=$PWD/_install --**with-**platform=efiUs

© 2022 - 2024 — McMap. All rights reserved.