From edb0c96796cbbf300d10b056ac9bd316cda696ef Mon Sep 17 00:00:00 2001 From: BodgeMaster <> Date: Tue, 20 Jun 2023 13:17:45 +0200 Subject: [PATCH] grub-config: new script --- opt/grub-config | 102 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 opt/grub-config diff --git a/opt/grub-config b/opt/grub-config new file mode 100644 index 0000000..98dda5d --- /dev/null +++ b/opt/grub-config @@ -0,0 +1,102 @@ +#!/usr/bin/env bash + +OS_NAME="Void Linux USB" +GRUB_PREFIX="/boot/efi/LOADER/grub" +CFG_CUSTOM_BEFORE="custom_before.cfg" +CFG_CUSTOM_AFTER="custom_after.cfg" +LINUX_CMDLINE="" + +DEFAULT_LINUX=/boot/vmlinu? +if [ ! -f "$DEFAULT_LINUX" ]; then + # in cases where it’s a .gz or .xz or whatever + DEFAULT_LINUX=$(find /boot -type l -name "vmlinu*" 2>/dev/null | sort | head -n1) +fi + +DEFAULT_INITRAMFS=/boot/initramfs.img +if [ ! -f "$DEFAULT_INITRAMFS" ]; then + # hope to find anything named initramfs or initrd + DEFAULT_INITRAMFS=$(find /boot -type l -name "init*" 2>/dev/null | sort | head -n1) +fi + +function make_menuentry { + # $1 is the kernel path + + # We don’t want duplicate entries for /boot/vmlinuz symlink + if [ -L $1 ]; then + return 0 + fi + + # exclude garbage globbing results + if [ ! -f $1 ]; then + return 0 + fi + + LINUX="$(basename "$1")" + LINUX_VERSION="$(sed 's/^kernel//;s/^vm//;s/^linuz//;s/^linux//;s/^-//' <<< "$LINUX")" + if grep -e ".gz$" -e ".xz$" -e ".bz$" -e ".bz2$" -e ".lzma$" -e ".lz$" -e ".img$" >/dev/null 2>&1 <<< "$LINUX"; then + LINUX_VERSION="$(sed 's/[^.]*$//;s/.$//' <<< "$LINUX_VERSION")" + fi + + INITRAMFS="$(basename "$(find /boot -name "init*$LINUX_VERSION*" 2>/dev/null | sort | head -n1)")" + + # user output + echo " -> Found $LINUX (version $LINUX_VERSION, initramfs $INITRAMFS)" + + # grub.cfg + echo " + menuentry 'Linux $LINUX_VERSION' { + echo 'Loading $LINUX...' + linux /$LINUX $LINUX_CMDLINE + echo 'Loading initial ramdisk...' + initrd /$INITRAMFS + }" >> "$GRUB_PREFIX/grub.cfg" +} + +# user output +echo "Generating grub config..." + +# grub.cfg +echo "# This file is auto-generated by $0. +# Changes will be overwritten automatically. +# If you want to add custom entries or other changes, add them to +# $GRUB_PREFIX/$CFG_CUSTOM_BEFORE or +# $GRUB_PREFIX/$CFG_CUSTOM_AFTER + +search --fs-uuid --set=root $(blkid --output value --match-tag UUID "$(grep " /boot " /proc/mounts | sed -e 's/ .*//')") + +set menu_color_normal=white/black +set menu_color_highlight=black/light-gray + +timeout=5 + +if [ -f \${config_directory}/$CFG_CUSTOM_BEFORE ]; then + source \${config_directory}/$CFG_CUSTOM_BEFORE +elif [ -z \"\${config_directory}\" -a -f \$prefix/$CFG_CUSTOM_BEFORE ]; then + source \$prefix/$CFG_CUSTOM_BEFORE +fi + +menuentry '$OS_NAME' { + echo 'Loading Linux...' + linux $DEFAULT_LINUX + echo 'Loading initial ramdisk...' + initrd $DEFAULT_INITRAMFS +} + +submenu 'Choose kernel version...' {" > "$GRUB_PREFIX/grub.cfg" + +for FILE in /boot/{vm,}linu{x,z}* /boot/kernel*; do + make_menuentry "$FILE" +done + +# grub.cfg +echo " +} + +if [ -f \${config_directory}/$CFG_CUSTOM_AFTER ]; then + source \${config_directory}/$CFG_CUSTOM_AFTER +elif [ -z \"\${config_directory}\" -a -f \$prefix/$CFG_CUSTOM_AFTER ]; then + source \$prefix/$CFG_CUSTOM_AFTER +fi" >> "$GRUB_PREFIX/grub.cfg" + +# user output +echo "done"