2023-01-09 23:09:18 -08:00
|
|
|
{{- if (eq .host.distro.family "linux") -}}
|
|
|
|
#!/usr/bin/env bash
|
2023-04-11 20:26:25 -07:00
|
|
|
# @file GRUB2 Theme / Configuration
|
|
|
|
# @brief Configures GRUB2 to use a custom theme
|
|
|
|
# @description
|
|
|
|
# This script configures GRUB2 with a custom theme.
|
2023-01-09 23:09:18 -08:00
|
|
|
|
|
|
|
{{ includeTemplate "universal/profile" }}
|
|
|
|
{{ includeTemplate "universal/logg" }}
|
|
|
|
|
|
|
|
### Fix Qubes issue
|
|
|
|
if command -v qubesctl > /dev/null && [ -f /boot/grub2/grubenv ] && [ -d /boot/efi/EFI/qubes ]; then
|
|
|
|
sudo cp -f /boot/grub2/grubenv /boot/efi/EFI/qubes/grubenv
|
|
|
|
logg success 'Copied /boot/grub2/grubenv to /boot/efi/EFI/qubes/grubenv'
|
|
|
|
fi
|
|
|
|
|
|
|
|
### Ensure /boot/grub2/themes is directory
|
|
|
|
if [ ! -d /boot/grub2/themes ]; then
|
|
|
|
sudo mkdir -p /boot/grub2/themes
|
|
|
|
logg success 'Created /boot/grub2/themes'
|
|
|
|
fi
|
|
|
|
|
|
|
|
### Copy GRUB theme to /boot/grub2/themes
|
|
|
|
if [ -d /usr/local/share/grub/themes ]; then
|
2023-01-10 01:27:32 -08:00
|
|
|
sudo cp -rf /usr/local/share/grub/themes /boot/grub2/
|
2023-01-09 23:09:18 -08:00
|
|
|
logg success 'Copied GRUB themes in /usr/local/share/grub/themes to /boot/grub2/themes'
|
|
|
|
else
|
2023-01-11 22:20:13 -08:00
|
|
|
logg warn '/usr/local/share/grub/themes is missing'
|
2023-01-09 23:09:18 -08:00
|
|
|
fi
|
|
|
|
|
|
|
|
### Set default GRUB screen resolution variables
|
|
|
|
SCREEN_RATIO_ULTRAWIDE="2100"
|
|
|
|
GRUB_RESOLUTION_TYPE="1080p"
|
|
|
|
|
|
|
|
### Determine screen size ratio (used for picking GRUB2 theme resolution)
|
2023-01-10 01:02:52 -08:00
|
|
|
if command -v xrandr > /dev/null && command -v uniq > /dev/null; then
|
2023-01-09 23:09:18 -08:00
|
|
|
SCREEN_WIDTH="$(xrandr --current | grep '*' | uniq | awk '{print $1}' | cut -d 'x' -f1)"
|
|
|
|
SCREEN_HEIGHT="$(xrandr --current | grep '*' | uniq | awk '{print $1}' | cut -d 'x' -f2)"
|
|
|
|
SCREEN_RATIO="$(awk -v height="$SCREEN_HEIGHT" -v width="$SCREEN_WIDTH" 'BEGIN { print ((height / width) * 1000) }')"
|
|
|
|
SCREEN_RATIO="${SCREEN_RATIO%.*}"
|
|
|
|
logg success "Screen detected as $SCREEN_WIDTH x $SCREEN_HEIGHT (ratio of $SCREEN_RATIO)"
|
|
|
|
if (( $(echo "$SCREEN_RATIO $SCREEN_RATIO_ULTRAWIDE" | awk '{print ($1 > $2)}') )); then
|
|
|
|
GRUB_RESOLUTION_TYPE="ultrawide"
|
2023-01-11 22:20:13 -08:00
|
|
|
logg info 'GRUB resolution registered as ultrawide'
|
2023-01-09 23:09:18 -08:00
|
|
|
fi
|
|
|
|
else
|
2023-01-11 22:20:13 -08:00
|
|
|
logg warn 'Missing either xrandr or uniq (required for calculating screen size ratio)'
|
2023-01-09 23:09:18 -08:00
|
|
|
fi
|
|
|
|
|
|
|
|
### Optimize the GRUB resolution
|
|
|
|
if [ -f /etc/default/grub ]; then
|
|
|
|
### GRUB_GFXMODE
|
|
|
|
logg info 'Setting GRUB_GFXMODE=auto in /etc/default/grub'
|
2023-01-11 14:44:05 -08:00
|
|
|
if cat /etc/default/grub | grep GRUB_GFX_MODE > /dev/null; then
|
2023-01-10 01:11:20 -08:00
|
|
|
sudo sed -i 's/.*GRUB_GFXMODE.*/GRUB_GFXMODE=auto/' /etc/default/grub
|
2023-01-09 23:09:18 -08:00
|
|
|
else
|
2023-02-01 13:06:44 -08:00
|
|
|
echo "GRUB_GFXMODE=auto" | sudo tee -a /etc/default/grub > /dev/null
|
2023-01-09 23:09:18 -08:00
|
|
|
fi
|
|
|
|
|
|
|
|
### GRUB_GFXPAYLOAD_LINUX
|
|
|
|
logg info 'Setting GRUB_GFXPAYLOAD_LINUX=keep in /etc/default/grub'
|
2023-01-11 14:44:05 -08:00
|
|
|
if cat /etc/default/grub | grep GRUB_GFXPAYLOAD_LINUX > /dev/null; then
|
2023-01-10 01:11:20 -08:00
|
|
|
sudo sed -i 's/.*GRUB_GFXPAYLOAD_LINUX.*/GRUB_GFXPAYLOAD_LINUX="keep"/' /etc/default/grub
|
2023-01-09 23:09:18 -08:00
|
|
|
else
|
2023-02-01 13:06:44 -08:00
|
|
|
echo 'GRUB_GFXPAYLOAD_LINUX="keep"' | sudo tee -a /etc/default/grub > /dev/null
|
2023-01-09 23:09:18 -08:00
|
|
|
fi
|
|
|
|
|
|
|
|
### GRUB_THEME
|
|
|
|
logg info 'Setting GRUB_THEME={{ .theme }} in /etc/default/grub'
|
2023-01-11 14:44:05 -08:00
|
|
|
if cat /etc/default/grub | grep GRUB_THEME > /dev/null; then
|
2023-01-10 01:27:32 -08:00
|
|
|
sudo sed -i 's/.*GRUB_THEME.*/GRUB_THEME="{{ .theme }}-'"$GRUB_RESOLUTION_TYPE"'"/' /etc/default/grub
|
2023-01-09 23:09:18 -08:00
|
|
|
else
|
2023-02-01 13:06:44 -08:00
|
|
|
echo 'GRUB_THEME="{{ .theme }}-'"$GRUB_RESOLUTION_TYPE"'"' | sudo tee -a /etc/default/grub > /dev/null
|
2023-01-09 23:09:18 -08:00
|
|
|
fi
|
|
|
|
|
|
|
|
### GRUB_BACKGROUND
|
2023-01-24 06:26:16 -08:00
|
|
|
# Removed since the background should be flat black which is configurable
|
|
|
|
# Leaving this code here in case we need to add a flat black image background for some reason
|
|
|
|
# logg info 'Setting GRUB_BACKGROUND=/usr/local/share/grub/{{ .theme }}-blue.png in /etc/default/grub'
|
|
|
|
# if cat /etc/default/grub | grep GRUB_BACKGROUND > /dev/null; then
|
|
|
|
# sudo sed -i 's/.*GRUB_BACKGROUND.*/GRUB_BACKGROUND="\/usr\/local\/share\/grub\/{{ .theme }}-blue.png"/' /etc/default/grub
|
|
|
|
# else
|
2023-02-01 13:06:44 -08:00
|
|
|
# echo 'GRUB_BACKGROUND="/usr/local/share/grub/{{ .theme }}-blue.png"' | sudo tee -a /etc/default/grub > /dev/null
|
2023-01-24 06:26:16 -08:00
|
|
|
# fi
|
2023-01-09 23:09:18 -08:00
|
|
|
|
|
|
|
### GRUB_TIMEOUT
|
|
|
|
logg info 'Setting GRUB_TIMEOUT={{ .grub.timeout }} in /etc/default/grub'
|
2023-01-11 14:44:05 -08:00
|
|
|
if cat /etc/default/grub | grep GRUB_TIMEOUT > /dev/null; then
|
2023-01-10 01:11:20 -08:00
|
|
|
sudo sed -i 's/.*GRUB_TIMEOUT.*/GRUB_TIMEOUT="{{ .grub.timeout }}"/' /etc/default/grub
|
2023-01-09 23:09:18 -08:00
|
|
|
else
|
2023-02-01 13:06:44 -08:00
|
|
|
echo 'GRUB_TIMEOUT="{{ .grub.timeout }}"' | sudo tee -a /etc/default/grub > /dev/null
|
2023-01-09 23:09:18 -08:00
|
|
|
fi
|
|
|
|
|
|
|
|
### GRUB_FORCE_HIDDEN_MENU
|
|
|
|
logg info 'Setting GRUB_FORCE_HIDDEN_MENU={{ .grub.shiftToSee }} in /etc/default/grub'
|
2023-01-10 01:27:32 -08:00
|
|
|
sudo sed -i '/GRUB_FORCE_HIDDEN_MENU/d' /etc/default/grub
|
2023-02-01 13:06:44 -08:00
|
|
|
echo "GRUB_FORCE_HIDDEN_MENU={{ .grub.shiftToSee }}" | sudo tee -a /etc/default/grub > /dev/null
|
2023-01-09 23:09:18 -08:00
|
|
|
|
|
|
|
### Remove duplicate lines in /etc/default/grub
|
|
|
|
logg info 'Ensuring there are no duplicate entries in /etc/default/grub'
|
2023-01-10 01:27:32 -08:00
|
|
|
cat /etc/default/grub | uniq | sudo tee /etc/default/grub > /dev/null
|
2023-01-09 23:09:18 -08:00
|
|
|
else
|
|
|
|
logg warn '/etc/default/grub is missing'
|
|
|
|
fi
|
|
|
|
|
|
|
|
### Determine platform-specific icon to use
|
|
|
|
if command -v qubesctl > /dev/null; then
|
|
|
|
GRUB_ICON='qubes'
|
2023-02-04 01:19:31 -08:00
|
|
|
elif [ -f "/usr/local/share/grub/themes/{{ .theme }}-$GRUB_RESOLUTION_TYPE/icons/{{ .host.distro.id }}.png" ]; then
|
2023-01-09 23:09:18 -08:00
|
|
|
GRUB_ICON='{{ .host.distro.id }}'
|
2023-02-04 01:19:31 -08:00
|
|
|
elif [ -f "/usr/local/share/grub/themes/{{ .theme }}-$GRUB_RESOLUTION_TYPE/icons/{{ .host.distro.family }}.png" ]; then
|
2023-01-09 23:09:18 -08:00
|
|
|
GRUB_ICON='{{ .host.distro.family }}'
|
|
|
|
else
|
|
|
|
GRUB_ICON='linux'
|
|
|
|
fi
|
|
|
|
|
|
|
|
### Copy icon to GRUB2 theme folder
|
2023-02-04 01:19:31 -08:00
|
|
|
# Check looks in /usr/local/share/grub because on some systems the /boot folder is behind permissions for non-root users
|
|
|
|
if [ -f "/usr/local/share/grub/themes/{{ .theme }}-$GRUB_RESOLUTION_TYPE/icons/$GRUB_ICON.png" ]; then
|
2023-01-11 14:18:05 -08:00
|
|
|
sudo cp -f /boot/grub2/themes/{{ .theme }}-$GRUB_RESOLUTION_TYPE/icons/$GRUB_ICON.png /boot/grub2/themes/{{ .theme }}-$GRUB_RESOLUTION_TYPE/icon.png
|
2023-01-09 23:09:18 -08:00
|
|
|
logg success 'Copied platform-specific icon to GRUB2 theme folder'
|
|
|
|
else
|
|
|
|
logg warn "/boot/grub2/themes/{{ .theme }}-$GRUB_RESOLUTION_TYPE/icons/$GRUB_ICON.png is missing"
|
|
|
|
fi
|
2023-01-11 22:32:51 -08:00
|
|
|
|
2023-02-06 03:46:34 -08:00
|
|
|
### Hide unnecessary Boot messages and Bliking cursor
|
|
|
|
GRUB_DEFAULT_CMDLINE=$(grep 'GRUB_CMDLINE_LINUX_DEFAULT' /etc/default/grub)
|
|
|
|
if [[ -n $GRUB_DEFAULT_CMDLINE ]]; then
|
|
|
|
KERNEL_PARAMS_QUIET=$(echo "$GRUB_DEFAULT_CMDLINE" | grep 'quiet')
|
|
|
|
logg info 'Updating GRUB_CMDLINE_LINUX_DEFAULT to hide log messages'
|
|
|
|
if [[ -z $KERNEL_PARAMS_QUIET ]]; then
|
|
|
|
sudo sed -i 's/^GRUB_CMDLINE_LINUX_DEFAULT="/GRUB_CMDLINE_LINUX_DEFAULT="quiet loglevel=3 systemd.show_status=auto rd.udev.log_level=3 vt.global_cursor_default=0 /' /etc/default/grub
|
|
|
|
else
|
|
|
|
NEW_KERNEL_PARAMS=$(echo $KERNEL_PARAMS_QUIET | sed -e "s/quiet/quiet loglevel=3 systemd.show_status=auto rd.udev.log_level=3 vt.global_cursor_default=0/")
|
|
|
|
sudo sed -i "s/^GRUB_CMDLINE_LINUX_DEFAULT.*/${NEW_KERNEL_PARAMS}/" /etc/default/grub
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
logg info 'GRUB_CMDLINE_LINUX_DEFAULT was not present, adding one with parameters to hide log messages'
|
|
|
|
echo 'GRUB_CMDLINE_LINUX_DEFAULT="quiet loglevel=3 systemd.show_status=auto rd.udev.log_level=3 vt.global_cursor_default=0"' | sudo tee -a /etc/default/grub > /dev/null
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
2023-01-11 14:59:22 -08:00
|
|
|
### Ensure grub2-mkconfig is available
|
|
|
|
if ! command -v grub2-mkconfig > /dev/null; then
|
|
|
|
if command -v grub-mkconfig > /dev/null; then
|
|
|
|
sudo ln -s "$(which grub-mkconfig)" /usr/bin/grub2-mkconfig
|
2023-01-30 00:31:07 -08:00
|
|
|
elif sudo which grub-mkconfig > /dev/null; then
|
|
|
|
sudo ln -s "$(sudo which grub-mkconfig)" /usr/bin/grub2-mkconfig
|
2023-01-11 14:59:22 -08:00
|
|
|
else
|
2023-01-11 22:32:13 -08:00
|
|
|
logg warn 'Neither grub2-mkconfig or grub-mkconfig are available'
|
2023-01-11 14:59:22 -08:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2023-01-09 23:09:18 -08:00
|
|
|
### Apply GRUB2 theme
|
2023-01-11 22:20:13 -08:00
|
|
|
# Set export DEBUG_MODE=true to bypass GRUB2 / Plymouth application
|
2023-01-11 14:59:22 -08:00
|
|
|
if [ "$DEBUG_MODE" != 'true' ]; then
|
2023-01-30 00:31:07 -08:00
|
|
|
if command -v grub2-mkconfig > /dev/null; then
|
|
|
|
if [ -d /sys/firmware/efi ]; then
|
|
|
|
logg info 'Assuming system is UEFI-enabled since /sys/firmware/efi is present'
|
|
|
|
if [ -f /boot/efi/EFI/qubes/grub.cfg ]; then
|
|
|
|
logg info 'Running sudo grub2-mkconfig -o /boot/efi/EFI/qubes/grub.cfg'
|
|
|
|
sudo grub2-mkconfig -o /boot/efi/EFI/qubes/grub.cfg
|
|
|
|
logg success 'Applied GRUB2 theme'
|
|
|
|
elif [ -f /boot/efi/EFI/grub.cfg ]; then
|
|
|
|
logg info 'Running sudo grub2-mkconfig -o /boot/efi/EFI/grub.cfg'
|
|
|
|
sudo grub2-mkconfig -o /boot/efi/EFI/grub.cfg
|
|
|
|
logg success 'Applied GRUB2 theme'
|
|
|
|
else
|
|
|
|
logg warn 'Unknown GRUB2 configuration - not applying GRUB2 theme'
|
|
|
|
fi
|
2023-01-11 14:59:22 -08:00
|
|
|
else
|
2023-01-30 00:31:07 -08:00
|
|
|
logg info 'Assuming system is non-UEFI since /sys/firmware/efi is not present'
|
|
|
|
logg info 'Running sudo grub2-mkconfig -o /boot/grub2/grub.cfg'
|
|
|
|
sudo grub2-mkconfig -o /boot/grub2/grub.cfg
|
|
|
|
logg success 'Applied GRUB2 theme'
|
2023-01-11 14:59:22 -08:00
|
|
|
fi
|
2023-01-30 00:31:07 -08:00
|
|
|
elif [ -f /usr/sbin/update-grub ]; then
|
2023-11-04 18:46:18 -07:00
|
|
|
logg info 'Running sudo update-grub'
|
2023-01-30 00:31:07 -08:00
|
|
|
sudo update-grub
|
2023-01-09 23:09:18 -08:00
|
|
|
else
|
2023-01-30 00:31:07 -08:00
|
|
|
logg warn 'Unable to find appropriate GRUB mkconfig command'
|
2023-01-09 23:09:18 -08:00
|
|
|
fi
|
|
|
|
else
|
2023-01-11 14:59:22 -08:00
|
|
|
logg info 'Skipping GRUB2 theme application because DEBUG_MODE is set to true'
|
2023-01-09 23:09:18 -08:00
|
|
|
fi
|
|
|
|
|
|
|
|
{{ end -}}
|