Advanced OS Customizations & Bootloaders

GRUB2 Hooks & External Scripts: Integrating Python/Bash for Supercharged Dynamic Menus

Google AdSense Native Placement - Horizontal Top-Post banner

Introduction: Beyond Static Boot Menus

GRUB2, the Grand Unified Bootloader, is a powerful and flexible boot manager for Linux and other operating systems. While its default configuration typically provides static entries, its true power lies in its extensibility. Imagine a boot menu that dynamically updates based on system state, new kernels, or even external data sources. This is achievable by leveraging GRUB2 hooks and integrating external scripting languages like Bash and Python.

This article will guide you through the process of creating dynamic GRUB2 menu entries using custom scripts. We’ll explore how GRUB2’s configuration generation process works, how to write and integrate Bash scripts, and finally, how to harness the advanced logic of Python to create truly supercharged, intelligent boot menus.

Understanding GRUB2’s Configuration Generation

GRUB2’s primary configuration file, /boot/grub/grub.cfg, is not meant for direct manual editing. Instead, it’s generated by the grub-mkconfig utility, which aggregates configuration snippets from various scripts located in the /etc/grub.d/ directory. This directory is where the magic happens for custom dynamic entries.

The /etc/grub.d/ Structure

The files in /etc/grub.d/ are essentially shell scripts that are executed in alphanumeric order by grub-mkconfig. Each script is responsible for printing a specific part of the grub.cfg file to standard output. Some key scripts include:

  • 00_header: Sets up global GRUB parameters.
  • 10_linux: Detects installed Linux kernels and creates menu entries.
  • 20_linux_xen: Handles Xen hypervisor entries.
  • 30_os-prober: Detects other operating systems (Windows, macOS, other Linux distributions).
  • 40_custom: An empty template for user-defined static entries.
  • 40_custom_proxy: A hook point for more advanced custom entries, often used as a template.

By creating your own script within this directory, you can inject custom menu entries or even modify existing ones during the GRUB configuration generation process.

Creating a Basic External Script Hook

Let’s start by creating a simple Bash script that adds a custom menu entry. We’ll place it in /etc/grub.d/ to ensure it’s picked up by grub-mkconfig.

Step 1: Create the Script File

Use a number that ensures your script runs at the desired time. For custom entries, a number between 40 and 49 is usually appropriate. Let’s use 45_my_dynamic_entry.

sudo nano /etc/grub.d/45_my_dynamic_entry

Step 2: Add Script Content (Bash Example)

Inside the file, add the following Bash script:

#!/bin/shset -e# This script adds a custom GRUB2 menu entry.cat << EOFmenuentry 'My Custom Dynamic Entry (Bash)' {    insmod part_gpt    insmod ext2    set root='hd0,gpt2' # Adjust to your root partition    linux /boot/vmlinuz-$(uname -r) root=/dev/sda2 ro quiet splash    initrd /boot/initrd.img-$(uname -r)}EOF

Explanation:

  • #!/bin/sh: Shebang line indicating it’s a shell script.
  • set -e: Exits immediately if a command exits with a non-zero status.
  • cat << EOF ... EOF: This is a ‘here document’ that prints the enclosed text directly to standard output, which grub-mkconfig then captures into grub.cfg.
  • menuentry '...' { ... }: The standard GRUB2 syntax for a menu entry.
  • insmod part_gpt, insmod ext2: Load necessary GRUB modules for your partition type.
  • set root='hd0,gpt2': Sets the root device for GRUB. Adjust hd0,gpt2 to your actual root partition (e.g., hd0,msdos1 for MBR).
  • linux /boot/vmlinuz-...: Specifies the kernel image and kernel parameters. $(uname -r) dynamically fetches the current running kernel version.
  • initrd /boot/initrd.img-...: Specifies the initial RAM disk image.

Step 3: Make the Script Executable

sudo chmod +x /etc/grub.d/45_my_dynamic_entry

Step 4: Update GRUB Configuration

Now, regenerate grub.cfg:

sudo update-grub

On some systems, the command might be sudo grub-mkconfig -o /boot/grub/grub.cfg. After running this, reboot your system, and you should see

Android Mobile Specs & Compare Directory

Are you researching mobile hardware properties, processor SoCs, GPU chipsets, or RAM configurations? Access our complete specs catalog to compare up to 5 devices side-by-side!

Compare Devices Specs →
Google AdSense Inline Placement - Content Footer banner