Advanced OS Customizations & Bootloaders

Performance Hacking GRUB2: Optimizing Dynamic Boot Menu Load Times with Lean Scripts

Google AdSense Native Placement - Horizontal Top-Post banner

Introduction: The Hidden Cost of a Dynamic Boot Menu

GRUB2, the Grand Unified Bootloader, is a powerful and highly flexible boot manager for Linux and other operating systems. Its dynamic nature, driven by an array of scripts in /etc/grub.d/, allows it to automatically detect and configure boot entries for various OS installations. While incredibly convenient, this dynamism comes at a price: slower boot menu generation, especially on systems with multiple operating systems, complex partition layouts, or slower storage. This article delves into optimizing GRUB2’s dynamic boot menu load times by crafting lean, custom scripts, thereby reducing the overhead associated with the default configuration generation process.

Understanding GRUB2’s Configuration Generation

The primary GRUB2 configuration file, /boot/grub/grub.cfg, is not meant for direct manual editing. Instead, it’s generated by the update-grub (or grub-mkconfig) command, which executes scripts found in /etc/grub.d/. These scripts, prefixed with numbers (e.g., 00_header, 10_linux, 30_os-prober), are executed in alphanumeric order. Each script contributes fragments to the final grub.cfg. The default scripts often perform extensive system scans, such as:

  • Probing all mounted filesystems for Linux kernels (10_linux).
  • Detecting other operating systems on all disks (30_os-prober).
  • Checking for memtest utilities (20_memtest86+).

These operations, especially 30_os-prober, can be resource-intensive, leading to significant delays during update-grub execution and, consequently, a bloated and slow-loading boot menu.

Identifying Bottlenecks and Crafting Leaner Alternatives

The Problem with 30_os-prober

The 30_os-prober script is often the biggest culprit for slow GRUB2 updates. It exhaustively scans all available partitions for known operating systems (Windows, macOS, other Linux distributions). If you only ever boot a specific set of OSes or want to manually define them, this script is overkill.

Minimizing Shell Utility Overhead

Many default scripts rely heavily on shell utilities like grep, awk, and sed for parsing configuration files or command outputs. While powerful, repeated invocation of these external commands within loops or complex pipelines adds overhead. Custom scripts should aim for simpler logic or pre-calculate static values where possible.

Strategy for Optimization: Disable Defaults, Enable Custom

  1. Disable Unnecessary Default Scripts: The simplest performance gain comes from disabling scripts you don’t need.
  2. Create Custom, Leaner Scripts: For entries you still need (e.g., a specific Windows boot or a custom Linux kernel entry), write a minimal script that directly adds the GRUB commands without exhaustive probing.
  3. Hardcode Static Values: Use UUIDs or explicit device paths (e.g., (hd0,gpt1)) instead of relying on dynamic detection wherever possible.

Step-by-Step Optimization Guide

1. Backup Your Existing GRUB Configuration

Always start by backing up your critical files:

sudo cp /etc/default/grub /etc/default/grub.bak sudo cp -r /etc/grub.d/ /etc/grub.d.bak/ sudo cp /boot/grub/grub.cfg /boot/grub/grub.cfg.bak

2. Disable Unwanted Default Scripts

Navigate to /etc/grub.d/ and make non-executable any script you don’t need. A common candidate for multi-boot systems is 30_os-prober:

cd /etc/grub.d/ sudo chmod -x 30_os-prober

You might also consider disabling 20_memtest86+ if you don’t use it, or even 10_linux if you intend to manage all Linux entries manually (though this is less common and requires careful attention).

3. Crafting Custom Boot Entries (Example: Windows)

Instead of 30_os-prober, you can create a custom script, for example, 40_custom_windows, to directly add a Windows boot entry. This avoids the entire OS probing process.

sudo nano /etc/grub.d/40_custom_windows

Add the following content (adjust hd0,gpt1 and UUID to your specific Windows partition):

#!/bin/sh exec tail -n +3 $0 # This line makes the script self-executing for GRUB (leave as is) menuentry 'Windows Boot Manager (on /dev/sda1)' --class windows --class os { insmod part_gpt insmod fat if [ x$feature_platform_search_hint = xy ]; then   search --no-floppy --fs-uuid --set=root --hint-bios=hd0,gpt1 --hint-efi=hd0,gpt1 --hint-baremetal=ahci0,gpt1  YOUR_WINDOWS_PARTITION_UUID fi chainloader /efi/Microsoft/Boot/bootmgfw.efi }

Replace YOUR_WINDOWS_PARTITION_UUID with the actual UUID of your Windows EFI System Partition. You can find this using sudo blkid.

Make the script executable:

sudo chmod +x /etc/grub.d/40_custom_windows

4. Customizing Linux Entries (Advanced)

For even finer control over Linux entries, you could disable 10_linux and create your own 40_custom_linux. This is typically done to remove recovery entries, specific kernels, or to simplify the menu structure. A basic custom Linux entry might look like this:

#!/bin/sh exec tail -n +3 $0 menuentry 'My Custom Arch Linux' --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-simple-YOUR_ROOT_UUID' { insmod part_gpt insmod ext2 set root='hd0,gpt2' # Adjust to your root partition linux /boot/vmlinuz-linux root=UUID=YOUR_ROOT_UUID rw initrd /boot/initramfs-linux.img }

Remember to replace YOUR_ROOT_UUID and hd0,gpt2 with your actual root partition’s UUID and path. This approach gives you absolute control but requires manual updates for new kernels.

5. Adjusting GRUB Default Settings

Edit /etc/default/grub to further fine-tune performance and appearance. Common adjustments include:

  • GRUB_TIMEOUT_STYLE=menu: Ensures the menu is always displayed, preventing delays from hidden menus.
  • GRUB_TIMEOUT=5: Sets the menu timeout in seconds. Reduce it for faster booting if you often select the first entry.
  • GRUB_DISABLE_LINUX_RECOVERY=

    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