#!/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="root=/dev/loop0 ro" 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_LINUX="$(sed 's|^/boot/||' <<< "$DEFAULT_LINUX")" 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 DEFAULT_INITRAMFS="$(sed 's|^/boot/||' <<< "$DEFAULT_INITRAMFS")" 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 $LINUX_CMDLINE 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"