Introduction: Unlocking GRUB2’s Dynamic Potential
GRUB2, the Grand Unified Bootloader, is a powerful and highly configurable boot manager. While most users interact with its static menu entries, GRUB2 possesses a robust scripting engine, including support for environment variables. These variables are the cornerstone for creating dynamic, intelligent boot menus that can adapt to system states, user input, or even pseudo-real-time data. This article will deep dive into GRUB2 environment variables, demonstrating how to leverage them to build sophisticated and responsive boot experiences.
Understanding GRUB2 Environment Variables
GRUB2 environment variables function much like shell variables, storing strings that can be referenced and manipulated within GRUB’s scripting environment. They are crucial for controlling boot behavior, customizing appearance, and enabling conditional logic.
Variable Scope: Persistent vs. Transient
- Transient Variables: These variables exist only for the current GRUB session. They are defined during the boot process (e.g., in
grub.cfgor interactively at the GRUB prompt) and are lost once the operating system boots or GRUB restarts. Most variables you define directly in/boot/grub/grub.cfgor custom scripts are transient. - Persistent Variables: GRUB2 also supports persistent environment variables, which are stored in a dedicated file (typically
/boot/grub/grubenv) and persist across reboots. These are invaluable for user-defined defaults or states that need to be remembered.
Setting and Using Variables
Variables are set using the set command and referenced with a dollar sign prefix ($ or ${}).
set my_variable="Hello World" # Set a transient variableecho ${my_variable} # Output: Hello World
For persistent variables, GRUB2 provides the save_env and load_env commands. However, direct manipulation of grubenv is discouraged; instead, modify it through the GRUB prompt or by utilizing the grub-editenv utility from a running Linux system.
The Power of Custom Variables for Conditional Booting
Custom variables allow you to introduce sophisticated logic into your boot process. Let’s imagine a scenario where you want to offer a ‘recovery mode’ option only if a specific condition is met, such as a flag set by a system administrator.
Example: Conditional Recovery Mode
First, we need a way to set a persistent flag. From your running Linux system, use grub-editenv:
sudo grub-editenv - set boot_recovery="true" # Sets a persistent variable
Now, we can use this in a custom GRUB configuration file. Create a file named /etc/grub.d/41_custom_recovery and make it executable:
sudo nano /etc/grub.d/41_custom_recovery
Add the following content:
#!/bin/shset -e# Load persistent variables from grubenv so 'boot_recovery' is available.grub_editenv - read boot_recoveryif [ "x${boot_recovery}" = "xtrue" ] ; thenecho "menuentry 'System Recovery Mode (Conditional)' --class ubuntu --class gnu-linux --class gnu --class os {"echo " recordfail"echo " set gfxpayload=keep"echo " linux /vmlinuz-recovery root=/dev/sda1 ro single"echo " initrd /initrd.img-recovery"echo "}"fi# Unset the variable after use, or reset it if it was for a one-time recovery.grub_editenv - unset boot_recovery
Make the script executable and update GRUB:
sudo chmod +x /etc/grub.d/41_custom_recoverysudo update-grub
Upon reboot, if boot_recovery was set to true, you will see the
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 →