diff --git a/.gitignore b/.gitignore index 243d67f..ec445f0 100644 --- a/.gitignore +++ b/.gitignore @@ -4,5 +4,5 @@ *.zst.* pkg/ src/ -.idea/ -refind-menu-generator + + diff --git a/etc/refind-menu-generator/menu-end.txt b/etc/refind-menu-generator/menu-end.txt new file mode 100644 index 0000000..883c1b4 --- /dev/null +++ b/etc/refind-menu-generator/menu-end.txt @@ -0,0 +1,46 @@ +menuentry "rEFInd HARD" { + icon /EFI/refind/themes/{THEME}/icons/os_systemd-boot.png + ostype Linux + graphics on + loader /EFI/refind_hard/refind_x64.efi +} + + +menuentry "Windows 10" { + icon /EFI/refind/themes/{THEME}/icons/os_win10.png + ostype Windows + loader /EFI/ms10/Boot/bootmgfw.efi +} + + +menuentry "Windows 11" { + icon /EFI/refind/themes/{THEME}/icons/os_win11.png + ostype Windows + loader /EFI/ms11/Microsoft/Boot/bootmgfw.efi +} + + +menuentry "Memtest86+ from EFI" { + icon /EFI/refind/themes/{THEME}/icons/tool_memtest.png + loader /EFI/memtest/bootx64.efi +} + +menuentry "GRUB" { + icon /EFI/refind/themes/{THEME}/icons/os_systemd-boot.png + ostype Linux + graphics on + loader /EFI/boot/grubx64.efi + + disabled +} + +menuentry "Systemd Boot" { + icon /EFI/refind/themes/{THEME}/icons/os_systemd-boot.png + ostype Linux + graphics on + loader /EFI/systemd/systemd-bootx64.efi + + disabled +} + +default_selection "linux" diff --git a/etc/refind-menu-generator/menu-template-efi-first.txt b/etc/refind-menu-generator/menu-template-efi-first.txt new file mode 100644 index 0000000..7d81aff --- /dev/null +++ b/etc/refind-menu-generator/menu-template-efi-first.txt @@ -0,0 +1,28 @@ +menuentry "EFI {OSNAME} ({KERNEL})" { + icon /EFI/refind/themes/{THEME}/icons/os_{ICON}.png + ostype Linux + graphics on + + loader /EFI/Linux/{KERNEL_EFI}.efi + + submenuentry "EFI {OSNAME} ({KERNEL}) fallback" { + loader /EFI/Linux/{KERNEL_EFI}-fallback.efi + } + + submenuentry "Terminal" { + add_options "systemd.unit=multi-user.target" + } + + submenuentry "{OSNAME} ({KERNEL})" { + loader /EFI/Linux/{KERNEL_VMLINUZ} + initrd /EFI/Linux/{INITRAMFS} + options "{CMDLINE} initrd=EFI\Linux\intel-ucode.img initrd=EFI\Linux\amd-ucode.img" + } + + submenuentry "{OSNAME} ({KERNEL}) fallback" { + loader /EFI/Linux/{KERNEL_VMLINUZ} + initrd /EFI/Linux/{INITRAMFS_FALLBACK} + options "{CMDLINE} initrd=EFI\Linux\intel-ucode.img initrd=EFI\Linux\amd-ucode.img" + } +} + diff --git a/etc/refind-menu-generator/menu-template-vmlinuz-first.txt b/etc/refind-menu-generator/menu-template-vmlinuz-first.txt new file mode 100644 index 0000000..5f1c24f --- /dev/null +++ b/etc/refind-menu-generator/menu-template-vmlinuz-first.txt @@ -0,0 +1,32 @@ +menuentry "{OSNAME} ({KERNEL})" { + icon /EFI/refind/themes/{THEME}/icons/os_{ICON}.png + ostype Linux + graphics on + + loader /EFI/Linux/{KERNEL_VMLINUZ} + initrd /EFI/Linux/{INITRAMFS} + options "{CMDLINE} initrd=EFI\Linux\intel-ucode.img initrd=EFI\Linux\amd-ucode.img" + + submenuentry "{OSNAME} ({KERNEL}) fallback" { + loader /EFI/Linux/{KERNEL_VMLINUZ} + initrd /EFI/Linux/{INITRAMFS_FALLBACK} + options "{CMDLINE} initrd=EFI\Linux\intel-ucode.img initrd=EFI\Linux\amd-ucode.img" + } + + submenuentry "EFI {OSNAME} ({KERNEL})" { + loader /EFI/Linux/{KERNEL_EFI}.efi + initrd + options + } + + submenuentry "EFI {OSNAME} ({KERNEL}) fallback" { + loader /EFI/Linux/{KERNEL_EFI}-fallback.efi + initrd + options + } + + submenuentry "Terminal" { + add_options "systemd.unit=multi-user.target" + } +} + diff --git a/etc/refind-menu-generator/menu-template.txt b/etc/refind-menu-generator/menu-template.txt new file mode 100644 index 0000000..7d81aff --- /dev/null +++ b/etc/refind-menu-generator/menu-template.txt @@ -0,0 +1,28 @@ +menuentry "EFI {OSNAME} ({KERNEL})" { + icon /EFI/refind/themes/{THEME}/icons/os_{ICON}.png + ostype Linux + graphics on + + loader /EFI/Linux/{KERNEL_EFI}.efi + + submenuentry "EFI {OSNAME} ({KERNEL}) fallback" { + loader /EFI/Linux/{KERNEL_EFI}-fallback.efi + } + + submenuentry "Terminal" { + add_options "systemd.unit=multi-user.target" + } + + submenuentry "{OSNAME} ({KERNEL})" { + loader /EFI/Linux/{KERNEL_VMLINUZ} + initrd /EFI/Linux/{INITRAMFS} + options "{CMDLINE} initrd=EFI\Linux\intel-ucode.img initrd=EFI\Linux\amd-ucode.img" + } + + submenuentry "{OSNAME} ({KERNEL}) fallback" { + loader /EFI/Linux/{KERNEL_VMLINUZ} + initrd /EFI/Linux/{INITRAMFS_FALLBACK} + options "{CMDLINE} initrd=EFI\Linux\intel-ucode.img initrd=EFI\Linux\amd-ucode.img" + } +} + diff --git a/etc/refind-menu-generator/refind.conf b/etc/refind-menu-generator/refind.conf new file mode 100755 index 0000000..6cdbbee --- /dev/null +++ b/etc/refind-menu-generator/refind.conf @@ -0,0 +1,18 @@ +timeout 5 +use_nvram false +screensaver 60 +resolution max +enable_touch +enable_mouse +mouse_speed 16 +use_graphics_for osx,linux,grub,windows +showtools shutdown, reboot, exit, firmware, fwupdate +scan_driver_dirs /EFI/refind/drivers_x64,EFI/tools/drivers,drivers +#scanfor manual,internal,external,optical +scanfor manual +also_scan_dirs boot,ESP:EFI/Linux,ESP2:EFI/Linux +scan_all_linux_kernels true +extra_kernel_version_strings linux-lts,linux-zen,linux-xanmod,linux-hybrid,linux-cachyos-pds,linux-cachyos-tt,linux +write_systemd_vars true +include manual.conf +include themes/melawy-nier-a2/theme.conf diff --git a/usr/bin/refind-menu-generator b/usr/bin/refind-menu-generator new file mode 100755 index 0000000..2bb2973 --- /dev/null +++ b/usr/bin/refind-menu-generator @@ -0,0 +1,134 @@ +#!/bin/bash + +function check_root { + [ $EUID -eq 0 ] && return + echo "refind-menu-generator requires root privileges to work" >&2 + exit 1 +} + +check_root + +__find_esp__() { + local parttype + local fstype + local device + + while read -r device; do + read -r parttype fstype ESP <<<"$(lsblk -o "PARTTYPE,FSTYPE,MOUNTPOINT" "$device" 2>/dev/null | awk 'NR==2')" + + [[ "${parttype,,}" != "c12a7328-f81f-11d2-ba4b-00a0c93ec93b" ]] && continue + [[ "${fstype,,}" != "vfat" ]] && continue + [[ -z $(findmnt -sn "${ESP}") ]] && continue + + done <<<"$(fdisk -l 2>/dev/null | grep -i efi | cut -d " " -f 1)" + + if [ -z "${ESP}" ]; then + ESP=$(grep -i "/efi" /etc/fstab | awk '{print $2}') + if [ -z "${ESP}" ]; then + ESP=$(grep -i "/boot" /etc/fstab | awk '{print $2}') + if [ -z "${ESP}" ]; then + ESP=$(bootctl --print-esp-path) + if [ -z "${ESP}" ]; then + exit 1 + fi + fi + fi + fi + + echo "Found ${ESP}" + readonly ESP +} + +# Copied from https://raw.githubusercontent.com/jaltuna/refind-theme-nord/main/setup.sh +# Verify EFI System Partition +__has_esp__() { + __find_esp__ + + mount "${ESP}" &>/dev/null + [[ -d "${ESP}/EFI" ]] && return 0 || return 1 +} + +echo "Searching rEFInd installation in EFI partition..." +if ! __has_esp__; then + echo "EFI partition not found" >&2 + exit 1 +fi + + +REFIND_DIR=$(find "${ESP}" -type d -iname refind) +if ! [[ -d "${REFIND_DIR}" ]]; then + echo "rEFInd not installed in ${ESP}" >&2 + exit 1 +fi +echo "Found rEFInd in ${REFIND_DIR}" + + +if [ -z "${ESP}" ]; then + exit 1 +else + + if [ ! -d "${ESP}/EFI/Linux" ]; then + mkdir -p "${ESP}/EFI/Linux" + if [ -d "${ESP}/EFI/linux" ]; then + KERNEL_DIR="${ESP}/EFI/linux" + fi + else + KERNEL_DIR="${ESP}/EFI/Linux" + fi + +fi + +echo "Found $KERNEL_DIR" + +theme=$(cat "${ESP}/EFI/refind/refind.conf" | grep themes | cut -d"/" -f2) + +if [ -d $KERNEL_DIR ]; then + + if [ -f "${ESP}/EFI/Linux/cmdline.txt" ]; then + CMDLINE=$(sed -e 's/^[[:space:]]//g' -e 's/[[:space:]]$//g' "${ESP}/EFI/Linux/cmdline.txt") + elif [ -f "/etc/kernel/cmdline" ]; then + CMDLINE=$(sed -e 's/^[[:space:]]//g' -e 's/[[:space:]]$//g' "/etc/kernel/cmdline") + else + CMDLINE=$(sed -e 's/^[[:space:]]//g' -e 's/[[:space:]]$//g' -e 's/initrd.*$//g' "/proc/cmdline") + fi + + echo $CMDLINE + + manual_conf=$(mktemp) + + template="/etc/refind-menu-generator/menu-template.txt" + + for i in $(ls -1 "${ESP}/EFI/Linux/" | grep 'linu' | grep -v 'fallback' | grep '\.efi$'); + do + kernel=$(echo $i | sed 's/\.efi//g') + kernel_efi=$kernel + kernel_vmlinuz="vmlinuz-$kernel" + + osname=$(cat /etc/os-release | grep "^NAME=" | cut -d'=' -f2 | sed -e 's/"//g') + icon=$(cat /etc/os-release | grep "^ID=" | cut -d'=' -f2 | sed -e 's/"//g') + + initramfs_tpl=$(echo "initramfs-$kernel" | sed "s/vmlinuz-//g") + initramfs=$(echo "$initramfs_tpl.img") + initramfs_fallback=$(echo "$initramfs_tpl-fallback.img") + + echo "OSNAME: $osname; THEME: $theme; ICON: $icon; KERNEL: $kernel; KERNEL_EFI: $kernel_efi; KERNEL_VMLINUZ: $kernel_vmlinuz; INITRAMFS: $initramfs; INITRAMFS_FALLBACK: $initramfs_fallback; CMDLINE: $CMDLINE" + sed -e "s|{OSNAME}|$osname|g" -e "s|{THEME}|$theme|g" -e "s|{ICON}|$icon|g" -e "s|{KERNEL}|$kernel|g" -e "s|{KERNEL_EFI}|$kernel_efi|g" -e "s|{KERNEL_VMLINUZ}|$kernel_vmlinuz|g" -e "s|{INITRAMFS}|$initramfs|g" -e "s|{INITRAMFS_FALLBACK}|$initramfs_fallback|g" -e "s|{CMDLINE}|$CMDLINE|g" $template >> $manual_conf + done + + manual_end_conf=$(mktemp) + + template_end="/etc/refind-menu-generator/menu-end.txt" + + sed -e "s/{THEME}/$theme/g" $template_end >> $manual_end_conf + + cat $manual_end_conf >> $manual_conf + + cp -vf $manual_conf "${ESP}/EFI/refind/manual.conf" + + if [ -d "${ESP}/EFI/boot" ]; then + cp -vf $manual_conf "${ESP}/EFI/boot/manual.conf" + fi + + rm -f $manual_conf + rm -f $manual_end_conf +fi