ChrisTitusTech / linutil

Chris Titus Tech's Linux Toolbox - Linutil is a distro-agnostic toolbox designed to simplify everyday Linux tasks.
https://christitus.com
MIT License
1.75k stars 158 forks source link

Grub theme configuration #149

Open kobewijnants opened 1 month ago

kobewijnants commented 1 month ago

Describe the solution you'd like Add an option to configure a grub theme

Describe alternatives you've considered A clear and concise description of any alternative solutions or features you've considered.

Additional context

fam007e commented 1 week ago

Solution for Configuring a GRUB Theme by taking inspiration from ChrisTitusTech's GRUB project

Description of the Solution You Might be Interested Into

To provide a solution for configuring a GRUB theme, a script can be created that allows users to select a theme from the themes available in the /boot/grub/themes directory. This script would automate the process of applying a GRUB theme by backing up the current configuration, installing the selected theme, and updating the GRUB configuration.

Script Overview

The following script dynamically lists the available themes in the specified directory and allows the user to select one. Once a theme is selected, it configures GRUB to use the chosen theme, enabling the GRUB menu with a 60-second timeout and setting the graphics mode to auto. The script also backs up the existing GRUB configuration before making any changes, ensuring that the original settings can be restored if needed.

Implementation

#!/bin/bash

# GRUB Theme Installer Script (Distro-Agnostic)

# Theme directory
THEME_DIR='/boot/grub/themes'
THEME_NAME=''
DISTRO=''

# Function to detect the Linux distribution
function detect_distro() {
    if [ -f /etc/os-release ]; then
        . /etc/os-release
        DISTRO=$ID
    else
        echo "Unknown distribution. Exiting..."
        exit 1
    fi
}

# Function to display menu and install selected theme
function select_theme() {
    # Get the list of theme directories in THEME_DIR
    themes=($(ls -d "${THEME_DIR}"/*/ | xargs -n 1 basename))
    themes+=("Quit")  # Add a Quit option at the end

    if [ ${#themes[@]} -eq 1 ]; then
        echo "No themes found in ${THEME_DIR}. Please add themes before running the script."
        exit 1
    fi

    PS3='Choose The Theme You Want: '
    select THEME_NAME in "${themes[@]}"; do
        if [ "${THEME_NAME}" == "Quit" ]; then
            echo 'Exiting...'
            exit 0
        elif [[ " ${themes[@]} " =~ " ${THEME_NAME} " ]]; then
            echo "Installing ${THEME_NAME} Theme..."
            break
        else
            echo "Invalid option"
        fi
    done
}

# Function to check for root access
function check_root() {
    if [[ $EUID -ne 0 ]]; then
        echo "This script must be run as root. Please use sudo."
        exit 1
    fi
}

# Function to backup the current GRUB config
function backup_grub() {
    cp -an /etc/default/grub /etc/default/grub.bak
    echo "Backup of GRUB config created at /etc/default/grub.bak"
}

# Function to install the selected theme
function install_theme() {
    # Create the theme directory if it doesn't exist
    if [[ ! -d "${THEME_DIR}/${THEME_NAME}" ]]; then
        echo "Error: Theme ${THEME_NAME} does not exist."
        exit 1
    fi

    # Set the theme in the GRUB config
    sed -i '/GRUB_THEME=/d' /etc/default/grub
    echo "GRUB_THEME=\"${THEME_DIR}/${THEME_NAME}/theme.txt\"" >> /etc/default/grub
    echo "Theme ${THEME_NAME} installed."
}

# Function to configure GRUB
function config_grub() {
    # Enable GRUB menu
    sed -i '/GRUB_TIMEOUT_STYLE=/d' /etc/default/grub
    echo 'GRUB_TIMEOUT_STYLE="menu"' >> /etc/default/grub

    # Set GRUB timeout to 60 seconds
    sed -i '/GRUB_TIMEOUT=/d' /etc/default/grub
    echo 'GRUB_TIMEOUT=60' >> /etc/default/grub

    # Set GRUB graphics mode to auto
    sed -i '/GRUB_GFXMODE=/d' /etc/default/grub
    echo 'GRUB_GFXMODE="auto"' >> /etc/default/grub
}

# Function to update GRUB based on distro
function update_grub() {
    case "$DISTRO" in
        ubuntu|debian)
            update-grub
            ;;
        arch|manjaro)
            grub-mkconfig -o /boot/grub/grub.cfg
            ;;
        fedora|centos)
            grub2-mkconfig -o /boot/grub2/grub.cfg
            ;;
        *)
            echo "Unsupported distribution: $DISTRO"
            exit 1
            ;;
    esac
    echo "GRUB configuration updated."
}

# Main function to run the theme installation process
function main() {
    detect_distro
    check_root
    select_theme
    backup_grub
    install_theme
    config_grub
    update_grub
    echo "GRUB Theme Update Successful!"
}

main

Explanation

  1. detect_distro() Function: This function checks the /etc/os-release file to determine the Linux distribution and assigns it to the DISTRO variable.

  2. update_grub() Function: Based on the detected distribution, this function chooses the appropriate command to update GRUB. For example, it uses update-grub for Ubuntu/Debian, grub-mkconfig for Arch/Manjaro, and grub2-mkconfig for Fedora/CentOS.

  3. Distro-Agnostic Logic: The script adjusts commands and paths to be compatible with multiple distributions, making it more versatile and aligned with the goal of creating a distro-agnostic utility.

Describe Alternatives You've Considered

  1. Standard Themes: Including a set of standard themes within the script itself, but this could limit customization and would require regular updates to keep the themes current.

  2. Downloading Themes from URLs: The script could be extended to allow users to paste a URL from theme repositories like GNOME-Look or Gorgeous-GRUB. The script would then download and extract the theme automatically. This approach would provide users with greater flexibility and access to a broader range of themes but would require additional functionality for handling downloads, verifying theme files, and managing dependencies.

Additional Context

This script is designed to make it easy for users to apply a GRUB theme by selecting one from the available directories. The focus is on simplicity and reliability, ensuring that users can update their GRUB theme with minimal effort. Future versions of the script could incorporate additional features like theme downloads or a library of standard themes to further enhance the user experience.