logo

grub-nomagic-scripts

Automagic-free replacement scripts for GRUB grub-mkconfig and grub-install.git clone https://hacktivis.me/git/grub-nomagic-scripts.git
commit: 494dc05140063d13d3336fc24f1b50416a277e74
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Wed,  7 Feb 2024 17:23:43 +0100

init

Diffstat:

ALICENSES/MIT.txt9+++++++++
AREADME.md8++++++++
Aexample.env18++++++++++++++++++
Agrub-bios-install.sh47+++++++++++++++++++++++++++++++++++++++++++++++
Agrub-cfg.sh60++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Agrub.cfg.head14++++++++++++++
6 files changed, 156 insertions(+), 0 deletions(-)

diff --git a/LICENSES/MIT.txt b/LICENSES/MIT.txt @@ -0,0 +1,9 @@ +MIT License + +Copyright (c) <year> <copyright holders> + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md @@ -0,0 +1,8 @@ +# grub-nomagic-scripts + +Automagic-free replacement scripts for GRUB `grub-mkconfig` and `grub-install`. + +As no guesses are made, manual configuration is required, see the `example.env` file. + +Copyright ⓒ 2024 Haelwenn (lanodan) Monnier <contact+grub-nomagic-scripts@hacktivis.me> +SPDX-License-Identifier: MIT diff --git a/example.env b/example.env @@ -0,0 +1,18 @@ +# ~/.config/grub-nomagic/env +# Copyright ⓒ 2024 Haelwenn (lanodan) Monnier <contact+grub-nomagic-scripts@hacktivis.me> +# SPDX-License-Identifier: MIT + +# On gentoo this is provided by sys-kernel/linux-firmware[initramfs] +MICROCODE="amd-uc.img" + +# boottime grub root relative to the boot disk +#GRUB_ROOT="/ROOT/gentoo/@/boot" + +# grub modules required to decrypt /boot +#GRUB_CORE_MODULES="zfs cryptodisk luks luks2 gcry_rijndael gcry_sha512 part_msdos part_gpt biosdisk" + +# Kernel command line +#CMDLINE="iommu=soft amdgpu.gpu_recovery=1 nopti root=NightmareMoon/ROOT/gentoo rd.luks.uuid=a11ed797-cb45-496e-967d-a625b28740b6 crypt_root=UUID=a11ed797-cb45-496e-967d-a625b28740b6 rootfstype=zfs dozfs=cache apparmor=1" + +# Kernel initramfs, ONLY if you have an universal/multi-version one +#INITRD="initramfs-all.cpio.xz" diff --git a/grub-bios-install.sh b/grub-bios-install.sh @@ -0,0 +1,47 @@ +#!/bin/sh +# grub-install replacement for BIOS systems, assumes an encrypted /boot +# Copyright ⓒ 2024 Haelwenn (lanodan) Monnier <contact+grub-nomagic-scripts@hacktivis.me> +# SPDX-License-Identifier: MIT + +die() { + echo "$@" + exit 1 +} + +load_cfg() { + if [ -e "$1" ] ; then + . "$1" + else + die "grub-bios-install: Error, couldn't load configuration, file not found: $1" + fi +} + +command -v lsblk || die 'grub-bios-install: Error, command not found: lsblk' + +load_cfg ~/.config/grub-nomagic/env + +test -z "${DISK}" && die '$DISK needs to be set to a disk blockdevice, for example: /dev/sda' +test -z "${ROOT_BLOCK}" && die '$ROOT_BLOCK needs to be set to your root filesystem blockdevice, for example: /dev/sda2' + +test -z "${GRUB_ROOT}" && die '$GRUB_ROOT needs to be set to the boottime grub root relative to the boot disk, for example: /ROOT/gentoo/@/boot' +test -z "${GRUB_CORE_MODULES}" && die '$GRUB_CORE_MODULES needs to be set to the grub modules required to decrypt /boot' + +set -x -e + +: ${ROOT_UUID:=$(lsblk -n -o uuid ${ROOT_BLOCK} | head -1)} +: ${GRUB_PREFIX:=(cryptouuid/${ROOT_UUID//-/}${GRUB_ROOT}/grub/} + +echo "cryptomount -u ${ROOT_UUID//-/}" > /boot/grub/i386-pc/load.cfg +#echo 'cryptomount hd0,msdos2' > /boot/grub/i386-pc/load.cfg + +grub-mkimage \ + --format 'i386-pc' \ + --output '/boot/grub/i386-pc/core.img' \ + --config '/boot/grub/i386-pc/load.cfg' \ + --prefix "${GRUB_PREFIX}" \ + ${GRUB_CORE_MODULES} + +grub-bios-setup \ + --directory='/boot/grub/i386-pc' \ + --device-map='/boot/grub/device.map' \ + "${DISK}" diff --git a/grub-cfg.sh b/grub-cfg.sh @@ -0,0 +1,60 @@ +#!/bin/sh +# /boot/grub/grub.cfg config manager +# Copyright ⓒ 2024 Haelwenn (lanodan) Monnier <contact+grub-nomagic-scripts@hacktivis.me> +# SPDX-License-Identifier: MIT + +die() { + echo "$@" + exit 1 +} + +load_cfg() { + if [ -e "$1" ] ; then + . "$1" + else + die "grub-bios-install: Error, couldn't load configuration, file not found: $1" + fi +} + +set -e + +load_cfg ~/.config/grub-nomagic/env + +cd /boot + +test -z "${CMDLINE}" && die '$CMDLINE needs to be set to the kernel cmdline, for example the root variable for the kernel' +test -z "${MICROCODE}" && die '$MICROCODE needs to be set, for example: amc-uc.img' +test -z "${GRUB_ROOT}" && die '$GRUB_ROOT needs to be set to the boottime grub root relative to the boot disk, for example: /ROOT/gentoo/@/boot' + +cp grub/grub.cfg.head grub/grub.cfg || die 'grub/grub.cfg.head is required for initial configuration (such as modules and variables)' + +if test ! -f "/boot/${MICROCODE}" +then + echo "/boot/${MICROCODE}: File not found (microcode)" + exit 1 +fi + +: ${INITRD_EXT:=.img} + +for kernel in $(ls -t vmlinuz*) +do + version="${kernel#vmlinuz}" + : ${INITRD:=initramfs${version}${INITRD_EXT}} + + if test -e "${INITRD}" + then + echo "${kernel} + ${MICROCODE} + ${INITRD}" + else + echo "${kernel} ignored" + continue + fi + + cat >>grub/grub.cfg <<EOF +menuentry 'Linux${version:- Default}' { + linux ${grub_root}/${kernel} ${CMDLINE} + initrd ${grub_root}/${MICROCODE} ${grub_root}/${INITRD} +} +EOF +done + +test -f grub/grub.cfg.tail && cat grub/grub.cfg.tail >> grub/grub.cfg diff --git a/grub.cfg.head b/grub.cfg.head @@ -0,0 +1,14 @@ +# /boot/grub/grub.cfg.head +# Copyright ⓒ 2024 Haelwenn (lanodan) Monnier <contact+grub-nomagic-scripts@hacktivis.me> +# SPDX-License-Identifier: MIT +insmod part_gpt +insmod cryptodisk +insmod luks +insmod gcry_rijndael +insmod gcry_sha512 +insmod fat + +#set prefix=(cryptouuid/0d92d1bb4f464f0995f5fb4545973f8c)/ROOT/gentoo-2/@/boot/grub/ +#set root=(cryptouuid/0d92d1bb4f464f0995f5fb4545973f8c) + +insmod gzio