Adding and Using Files To/From the GRUB Memdisk (GRUB Standalone)

Published by Philipp Schuster on

TLDR: GRUB can reference internal files via the volume specifier (memdisk), similar to external drives (hd0).

GRUB has an undocumented but really convenient feature to include files/images (e.g., bootable kernels) directly into the GRUB installation itself. If you use $ grub-mkstandalone to store GRUB on a USB stick or a QEMU volume for example, you can bundle GRUB as EFI-application together with a configuration and payload/files. These files can be stored in the internal volume (memdisk), that is built-in into the GRUB EFI application. There is no official documentation link I found, but it allows you to reference and boot files without having to access a real file system (like on a HDD or SSD). Let’s take a look at the following command, which creates a GRUB standalone application in the local directory ./volume-out. This directory could then be copied to a bootable USB stick.

grub-mkstandalone \ 
  -O x86_64-efi \
  -o ".volume-out/EFI/BOOT/BOOTX64.EFI" \
  "/boot/grub/grub.cfg=./grub.cfg" \
  "/boot/multiboot2-binary.elf=./multiboot2-kernel.elf"

All arguments in the form /path/inside/grub/memdisk=/path/in/host/filesystem will be bundled into the (memdisk). From the GRUB man page ($ man grub-mkstandalone) we can find grub-mkstandalone [OPTION…] [OPTION] SOURCE…. The man page tells about SOURCE, that Graft point syntax (E.g. /boot/grub/grub.cfg=./grub.cfg) is accepted. (I never heart of Graft Point Syntax before :D) To reference files from there, a minimal GRUB configuration file might look like this:

set timeout=0
set default=0
# if on GRUB shell, this might be useful
# set root=(memdisk)

menuentry "My Multiboot2-kernel" {
    # Test if the file supports Multiboot2 and loads the file into GRUB
    # It gets booted automatically
    # "/" automatically references the (memdisk)-volume
    # for other volumes, the path would be "(hd0)/boot/..." for example
    multiboot2 /boot/multiboot2-binary.elf

    # if on GRUB shell, this is required
    # boot
}

This way you can easily boot your Multiboot2 kernel for example during develoment in QEMU or your personal PC from GRUB.


Philipp Schuster

Hi, I'm Philipp and interested in Computer Science. I especially like low level development, making ugly things nice, and de-mystify "low level magic".

0 Comments

Leave a Reply

Your email address will not be published.