Advanced OS Customizations & Bootloaders

Automating rEFInd Configuration: Shell Scripting for Dynamic Boot Manager Management

Google AdSense Native Placement - Horizontal Top-Post banner

Introduction: Streamlining rEFInd Customization

rEFInd is a powerful, graphical boot manager for UEFI-based systems, offering a highly customizable interface and robust control over the boot process. While its configuration file, refind.conf, is straightforward, manually applying changes, especially themes or advanced boot options across multiple systems or after reinstallations, can become repetitive and error-prone. This article delves into leveraging shell scripting to automate rEFInd’s configuration, focusing on dynamic theme management and advanced boot entry customizations. By the end, you’ll have a solid foundation for creating scripts that ensure consistent and efficient rEFInd setups.

The primary benefit of automation lies in efficiency and reproducibility. Imagine deploying a new system or recovering an existing one; a well-crafted script can restore your preferred rEFInd look and functionality with a single command, saving significant time and ensuring uniformity.

Prerequisites

Before diving into the scripts, ensure you have the following:

  • A system running rEFInd on a UEFI-based motherboard.
  • Basic familiarity with Linux shell scripting (Bash, Zsh, etc.).
  • Access to the EFI System Partition (ESP). This usually means running commands as root or with sudo.
  • Common Linux utilities: mount, sed, grep, awk, cp, mkdir, wget/curl.

Understanding rEFInd’s Configuration Landscape

rEFInd’s core configuration resides on the EFI System Partition (ESP), typically mounted at /boot/efi on Linux systems. Within the rEFInd directory (e.g., /boot/efi/EFI/refind), you’ll find:

  • refind.conf: The main configuration file, controlling boot entries, timeouts, graphics, and theme paths.
  • themes/: An optional subdirectory where rEFInd themes are stored. Each theme usually gets its own subdirectory within themes/, containing theme.conf and assets.

Our scripts will primarily interact with refind.conf and the themes/ directory.

Automating Theme Management

Theme management involves downloading a theme, placing it in the correct location, and then instructing rEFInd to use it by modifying refind.conf.

Step 1: Mount the EFI System Partition (ESP)

First, ensure your ESP is mounted. If it’s not mounted at /boot/efi, you’ll need to mount it manually.

#!/bin/bashESP_MOUNT_POINT="/boot/efi"REFIND_DIR="${ESP_MOUNT_POINT}/EFI/refind"# Check if ESP is mountedif ! mountpoint -q "${ESP_MOUNT_POINT}"; then    echo "Mounting ESP to ${ESP_MOUNT_POINT}..."    sudo mount "/dev/disk/by-partlabel/EFI System Partition" "${ESP_MOUNT_POINT}" || sudo mount "/dev/sda1" "${ESP_MOUNT_POINT}" # Adjust device as needed    if ! mountpoint -q "${ESP_MOUNT_POINT}"; then        echo "Error: Could not mount ESP. Exiting."        exit 1    fielse    echo "ESP already mounted at ${ESP_MOUNT_POINT}."fi

Step 2: Download and Install a rEFInd Theme

Let’s assume we want to download a theme from GitHub. We’ll use wget or curl and then extract it to the themes directory.

THEME_NAME="refind-minimal" # Example theme from GitHubTHEME_URL="https://github.com/evan-goode/refind-minimal/archive/refs/heads/master.zip"THEME_ZIP="/tmp/${THEME_NAME}.zip"THEME_DIR="${REFIND_DIR}/themes/${THEME_NAME}"echo "Downloading ${THEME_NAME} theme..."wget -q --show-progress "${THEME_URL}" -O "${THEME_ZIP}"if [ $? -ne 0 ]; then    echo "Error: Failed to download theme. Exiting."    exit 1fiecho "Extracting theme to ${THEME_DIR}..."sudo mkdir -p "${REFIND_DIR}/themes"sudo unzip -q "${THEME_ZIP}" -d "${REFIND_DIR}/themes/"# The zip often contains a subdirectory like 'refind-minimal-master', so we need to rename itsudo mv "${REFIND_DIR}/themes/${THEME_NAME}-master" "${THEME_DIR}"rm "${THEME_ZIP}"echo "Theme ${THEME_NAME} installed."

Step 3: Activate the Theme in refind.conf

We need to add or modify the include line in refind.conf to point to our new theme. We’ll use sed for this, handling cases where the line already exists or needs to be added.

REFIND_CONF="${REFIND_DIR}/refind.conf"THEME_CONFIG_PATH="EFI/refind/themes/${THEME_NAME}/theme.conf"if grep -q "^include ${THEME_CONFIG_PATH}" "${REFIND_CONF}"; then    echo "Theme include already present in ${REFIND_CONF}."elif grep -q "^include.*.conf" "${REFIND_CONF}"; then    # Replace an existing include line    echo "Updating existing theme include in ${REFIND_CONF}..."    sudo sed -i "s|^include.*.conf|include ${THEME_CONFIG_PATH}|" "${REFIND_CONF}"else    # Add the include line if none exist    echo "Adding theme include to ${REFIND_CONF}..."    # Find a good place to insert, e.g., after 'scanfor' or at the end    if grep -q "^scanfor" "${REFIND_CONF}"; then        sudo sed -i "/^[tool]/i\ninclude ${THEME_CONFIG_PATH}" "${REFIND_CONF}"    else        echo "include ${THEME_CONFIG_PATH}" | sudo tee -a "${REFIND_CONF}" > /dev/null    fi    echo "Theme activated."fi

Advanced Configuration Automation

Beyond themes, shell scripts can automate other critical refind.conf settings.

Hiding Unwanted Boot Entries

rEFInd automatically detects many bootable entries. You might want to hide specific ones (e.g., redundant EFI entries, recovery partitions). The hideui and dont_scan_volumes directives are useful.

# Hide specific UI elements or drivers (e.g., mouse pointer, firmware tools)UNWANTED_UI_ELEMENTS="firmware,shell,memtest,optical"if ! grep -q "^hideui ${UNWANTED_UI_ELEMENTS}" "${REFIND_CONF}"; then    echo "Adding/updating hideui directive..."    sudo sed -i "s|^#*hideui.*|hideui ${UNWANTED_UI_ELEMENTS}|" "${REFIND_CONF}"fi# Hide boot entries from a specific volume (e.g., a data partition)UNWANTED_VOLUME_UUID="ABCDEF01-2345-6789-ABCD-EF0123456789"if ! grep -q "^dont_scan_volumes.*${UNWANTED_VOLUME_UUID}" "${REFIND_CONF}"; then    echo "Adding/updating dont_scan_volumes directive..."    # Append to existing if present, otherwise add    if grep -q "^dont_scan_volumes" "${REFIND_CONF}"; then        sudo sed -i "s|(^dont_scan_volumes.*)|1,${UNWANTED_VOLUME_UUID}|" "${REFIND_CONF}"    else        echo "dont_scan_volumes ${UNWANTED_VOLUME_UUID}" | sudo tee -a "${REFIND_CONF}" > /dev/null    fi    echo "Volume ${UNWANTED_VOLUME_UUID} hidden."fi

Setting Default Boot Entry and Timeout

You can set a default boot option and a timeout for automatic booting.

DEFAULT_BOOT_LABEL="Arch Linux" # Or the UUID/GUID of the boot entryBOOT_TIMEOUT=5 # Secondsif ! grep -q "^default_selection" "${REFIND_CONF}"; then    echo "Setting default boot selection..."    echo "default_selection "${DEFAULT_BOOT_LABEL}"" | sudo tee -a "${REFIND_CONF}" > /dev/nullelse    echo "Updating default boot selection..."    sudo sed -i "s|^#*default_selection.*|default_selection "${DEFAULT_BOOT_LABEL}"|" "${REFIND_CONF}"fiif ! grep -q "^timeout" "${REFIND_CONF}"; then    echo "Setting boot timeout..."    echo "timeout ${BOOT_TIMEOUT}" | sudo tee -a "${REFIND_CONF}" > /dev/nullelse    echo "Updating boot timeout..."    sudo sed -i "s|^#*timeout.*|timeout ${BOOT_TIMEOUT}|" "${REFIND_CONF}"fi

Scripting Best Practices for rEFInd Automation

  • Idempotency: Ensure your scripts can be run multiple times without causing unintended side effects. Using grep -q before sed -i helps prevent duplicate entries.
  • Error Handling: Check the exit status of commands (if [ $? -ne 0 ]; then ... fi) and provide informative error messages.
  • User Interaction: For more complex scripts, consider using read -p for user input (e.g., theme choice, confirmation).
  • Backup: Always back up refind.conf before making changes.
# Backup refind.confsudo cp "${REFIND_CONF}" "${REFIND_CONF}.bak_$(date +%Y%m%d%H%M%S)"echo "Backed up ${REFIND_CONF} to ${REFIND_CONF}.bak_..."

Putting It All Together: A Comprehensive Script Skeleton

Combining the snippets above, you can build a robust script. Remember to customize paths, URLs, and specific configuration values to match your environment and preferences.

#!/bin/bash# Configuration VariablesESP_MOUNT_POINT="/boot/efi"REFIND_DIR="${ESP_MOUNT_POINT}/EFI/refind"REFIND_CONF="${REFIND_DIR}/refind.conf"THEME_NAME="refind-minimal"THEME_URL="https://github.com/evan-goode/refind-minimal/archive/refs/heads/master.zip"DEFAULT_BOOT_LABEL="Arch Linux"BOOT_TIMEOUT=5UNWANTED_UI_ELEMENTS="firmware,shell,memtest,optical"UNWANTED_VOLUME_UUID="" # Example: "ABCDEF01-2345-6789-ABCD-EF0123456789"# --- Functions ---mount_esp() {    if ! mountpoint -q "${ESP_MOUNT_POINT}"; then        echo "Mounting ESP to ${ESP_MOUNT_POINT}..."        # Attempt to mount by label or common device path        sudo mount "/dev/disk/by-partlabel/EFI System Partition" "${ESP_MOUNT_POINT}" ||         sudo mount "/dev/sda1" "${ESP_MOUNT_POINT}" || { echo "Error: Could not mount ESP." && exit 1; }    else        echo "ESP already mounted at ${ESP_MOUNT_POINT}."    fi}backup_refind_conf() {    sudo cp "${REFIND_CONF}" "${REFIND_CONF}.bak_$(date +%Y%m%d%H%M%S)"    echo "Backed up ${REFIND_CONF} to ${REFIND_CONF}.bak_..."}install_theme() {    local theme_zip="/tmp/${THEME_NAME}.zip"    local theme_dir="${REFIND_DIR}/themes/${THEME_NAME}"    echo "Downloading ${THEME_NAME} theme..."    wget -q --show-progress "${THEME_URL}" -O "${theme_zip}" || { echo "Error: Failed to download theme." && exit 1; }    echo "Extracting theme to ${theme_dir}..."    sudo mkdir -p "${REFIND_DIR}/themes"    sudo unzip -q "${theme_zip}" -d "${REFIND_DIR}/themes/"    # Handle common zip structure of 'themename-master'    if [ -d "${REFIND_DIR}/themes/${THEME_NAME}-master" ]; then        sudo mv "${REFIND_DIR}/themes/${THEME_NAME}-master" "${theme_dir}"    fi    rm -f "${theme_zip}"    echo "Theme ${THEME_NAME} installed."    # Activate theme    local theme_config_path="EFI/refind/themes/${THEME_NAME}/theme.conf"    if ! grep -q "^include ${theme_config_path}" "${REFIND_CONF}"; then        echo "Activating theme in ${REFIND_CONF}..."        if grep -q "^include.*.conf" "${REFIND_CONF}"; then            sudo sed -i "s|^include.*.conf|include ${theme_config_path}|" "${REFIND_CONF}"        else            sudo sed -i "/^[tool]/i\ninclude ${theme_config_path}" "${REFIND_CONF}"        fi    else        echo "Theme already active in ${REFIND_CONF}."    fi}configure_refind_settings() {    echo "Configuring rEFInd settings..."    # Hide UI elements    if ! grep -q "^hideui ${UNWANTED_UI_ELEMENTS}" "${REFIND_CONF}"; then        sudo sed -i "s|^#*hideui.*|hideui ${UNWANTED_UI_ELEMENTS}|" "${REFIND_CONF}" ||         echo "hideui ${UNWANTED_UI_ELEMENTS}" | sudo tee -a "${REFIND_CONF}" > /dev/null        echo "Updated hideui."    fi    # Set default selection    if ! grep -q "^default_selection" "${REFIND_CONF}"; then        echo "default_selection "${DEFAULT_BOOT_LABEL}"" | sudo tee -a "${REFIND_CONF}" > /dev/null    else        sudo sed -i "s|^#*default_selection.*|default_selection "${DEFAULT_BOOT_LABEL}"|" "${REFIND_CONF}"    fi    echo "Set default boot to '${DEFAULT_BOOT_LABEL}'."    # Set timeout    if ! grep -q "^timeout" "${REFIND_CONF}"; then        echo "timeout ${BOOT_TIMEOUT}" | sudo tee -a "${REFIND_CONF}" > /dev/null    else        sudo sed -i "s|^#*timeout.*|timeout ${BOOT_TIMEOUT}|" "${REFIND_CONF}"    fi    echo "Set boot timeout to ${BOOT_TIMEOUT} seconds."    # Dont scan volumes (if UUID provided)    if [ -n "${UNWANTED_VOLUME_UUID}" ]; then        if ! grep -q "^dont_scan_volumes.*${UNWANTED_VOLUME_UUID}" "${REFIND_CONF}"; then            if grep -q "^dont_scan_volumes" "${REFIND_CONF}"; then                sudo sed -i "s|(^dont_scan_volumes.*)|1,${UNWANTED_VOLUME_UUID}|" "${REFIND_CONF}"            else                echo "dont_scan_volumes ${UNWANTED_VOLUME_UUID}" | sudo tee -a "${REFIND_CONF}" > /dev/null            fi            echo "Added volume ${UNWANTED_VOLUME_UUID} to dont_scan_volumes."        else            echo "Volume ${UNWANTED_VOLUME_UUID} already in dont_scan_volumes."        fi    fi}# --- Main Script Execution ---mount_espbackup_refind_confinstall_themeconfigure_refind_settingsecho "rEFInd configuration script completed. Please reboot to see changes."

Conclusion

Automating rEFInd configuration with shell scripting transforms a potentially tedious manual process into a streamlined, reproducible workflow. We’ve covered mounting the ESP, dynamically installing and activating themes, and managing advanced settings like hiding boot entries and setting defaults. The provided script snippets and the comprehensive skeleton demonstrate how to use standard Linux utilities like sed, grep, and awk to intelligently modify configuration files. By adopting these practices, you can maintain a consistent and personalized rEFInd experience across all your UEFI systems with minimal effort, significantly enhancing your system administration efficiency.

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