Use kernel-install to manage BLS (rhbz#2209779)

- Use KERNEL_INSTALL_PLUGINS to define configuration logic
 - Require systemd > 252 that has support for KERNEL_INSTALL_PLUGINS
This commit is contained in:
Jonathan Steffan 2023-06-30 15:12:20 -06:00
parent 3afed5255b
commit e01efa4e1c
2 changed files with 119 additions and 22 deletions

View File

@ -0,0 +1,98 @@
#!/usr/bin/bash
# Sanity Checks
if ! [[ $KERNEL_INSTALL_MACHINE_ID ]]; then
exit 0
fi
if ! [[ $KERNEL_INSTALL_BOOT_ROOT ]]; then
exit 0
fi
# Setup variables
[[ -f /etc/default/grub ]] && . /etc/default/grub
[[ -f /etc/os-release ]] && . /etc/os-release
COMMAND="$1"
MEMTEST_VERSION="$2"
BOOT_DIR_ABS="$3"
MEMTEST_IMAGE="$4"
IMAGE_DIR="${MEMTEST_IMAGE%/*}"
BOOT_ROOT="${KERNEL_INSTALL_BOOT_ROOT}"
MACHINE_ID="${KERNEL_INSTALL_MACHINE_ID}"
BLS_DIR="${BOOT_ROOT}/loader/entries"
BLS_ENTRY="${BLS_DIR}/${MACHINE_ID}-0-memtest86+.conf"
# Setup functions
mkbls_grub() {
local memtestver=$1 && shift
local memtestimg=$1 && shift
cat <<EOF
title Memtest86+ (${memtestimg}-${memtestver})
version 0-${memtestver}-memtest86+
linux /${memtestimg}
grub_users \$grub_users
grub_arg --unrestricted
grub_class ${ID}
EOF
}
# If ${BOOT_DIR_ABS} exists, some other boot loader is active.
[[ -d "${BOOT_DIR_ABS}" ]] && exit 0
case "$COMMAND" in
add)
# Install into BOOT_ROOT, if not already there
if [[ "${MEMTEST_DIR}" != "${BOOT_ROOT}" ]]; then
for i in \
"$MEMTEST_IMAGE"
do
[[ -e "$i" ]] || continue
rm -f "${BOOT_ROOT}/${i##*/}"
cp -aT "$i" "${BOOT_ROOT}/${i##*/}"
command -v restorecon &>/dev/null && \
restorecon -R "${BOOT_ROOT}/${i##*/}"
done
fi
# Generate GRUB BLS, if enabled
if [[ "x${GRUB_ENABLE_BLSCFG}" = "xtrue" ]]; then
[[ -d "$BLS_DIR" ]] || mkdir -m 0700 -p "$BLS_DIR"
mkbls_grub "${MEMTEST_VERSION}" "${MEMTEST_IMAGE##*/}" > "${BLS_ENTRY}"
command -v restorecon &>/dev/null && restorecon -R "${BLS_ENTRY}"
MEMTEST_LOC="$(grep '^linux[ \t]' "${BLS_ENTRY}" | sed -e 's,^linux[ \t]*,,')"
if [[ "$(grub2-probe --device $(grub2-probe --target=device /) --target=fs)" == "btrfs" &&
"${SUSE_BTRFS_SNAPSHOT_BOOTING}" == "true" ]]; then
MEMTEST_RELPATH="$(grub2-mkrelpath -r ${BOOT_ROOT}${MEMTEST_LOC})"
else
MEMTEST_RELPATH="$(grub2-mkrelpath ${BOOT_ROOT}${MEMTEST_LOC})"
fi
BOOTPREFIX="$(dirname ${MEMTEST_RELPATH})"
if [[ "${MEMTEST_LOC}" != "${MEMTEST_RELPATH}" ]]; then
sed -i -e "s,^linux.*,linux ${BOOTPREFIX}${MEMTEST_LOC},g" "${BLS_ENTRY}"
fi
exit 77
fi
;;
remove)
# Find MEMTEST_IMAGE location in BLS_ENTRY, delete both MEMTEST_IMAGE and BLS_ENTRY
if [[ "x${GRUB_ENABLE_BLSCFG}" = "xtrue" ]]; then
if [[ -f "${BLS_ENTRY}" ]]; then
MEMTEST_IMAGE="$(grep '^linux[ \t]' "${BLS_ENTRY}" | sed -e 's,^linux[ \t]*,,')"
if [[ -f "${BOOT_ROOT}${MEMTEST_IMAGE}" ]]; then
rm -f "${BOOT_ROOT}${MEMTEST_IMAGE}"
fi
rm -f "${BLS_ENTRY}"
fi
exit 77
fi
;;
*)
;;
esac

View File

@ -15,16 +15,18 @@ Memtest86+.
%global mt_isa ia32 %global mt_isa ia32
%endif %endif
Name: memtest86+ Name: memtest86+
Version: 6.20 Version: 6.20
Release: %autorelease Release: %autorelease
Summary: Stand-alone memory tester for x86-64 computers Summary: Stand-alone memory tester for x86-64 computers
License: GPL-2.0-only License: GPL-2.0-only
URL: https://www.memtest.org/ URL: https://www.memtest.org/
Source0: https://github.com/memtest86plus/memtest86plus/archive/v%{version}/memtest86-plus-%{version}.tar.gz Source0: https://github.com/memtest86plus/memtest86plus/archive/v%{version}/memtest86-plus-%{version}.tar.gz
Source1: memtest86+.kernel-install-plugin
BuildRequires: gcc, make, xorriso, dosfstools, mtools BuildRequires: gcc, make, xorriso, dosfstools, mtools
ExclusiveArch: x86_64 %{ix86} Requires(pre): systemd-udev >= 252
ExclusiveArch: x86_64 %{ix86}
%description %description
%wordwrap -v common_description %wordwrap -v common_description
@ -49,6 +51,7 @@ pushd build%{__isa_bits}
install -m 0644 memtest.efi %{buildroot}%{_libdir}/%{name}/memtest86+%{mt_isa}.efi install -m 0644 memtest.efi %{buildroot}%{_libdir}/%{name}/memtest86+%{mt_isa}.efi
install -m 0644 memtest.bin %{buildroot}%{_libdir}/%{name}/memtest86+%{mt_isa}.bin install -m 0644 memtest.bin %{buildroot}%{_libdir}/%{name}/memtest86+%{mt_isa}.bin
install -m 0644 memtest.iso %{buildroot}%{_datarootdir}/%{name}/memtest86+%{mt_isa}.iso install -m 0644 memtest.iso %{buildroot}%{_datarootdir}/%{name}/memtest86+%{mt_isa}.iso
install -m 0755 %{SOURCE1} %{buildroot}%{_libdir}/%{name}/memtest86+.kernel-install-plugin
popd popd
@ -56,27 +59,23 @@ popd
%license LICENSE %license LICENSE
%doc README.md %doc README.md
%{_libdir}/%{name}/memtest86+%{mt_isa}.* %{_libdir}/%{name}/memtest86+%{mt_isa}.*
%{_libdir}/%{name}/memtest86+.kernel-install-plugin
%{_datarootdir}/%{name}/memtest86+%{mt_isa}.iso %{_datarootdir}/%{name}/memtest86+%{mt_isa}.iso
%posttrans %posttrans
install -m 0644 %{_libdir}/%{name}/memtest86+%{mt_isa}.* /boot/ MEMTEST_IMAGE="memtest86+%{mt_isa}.bin"
if [ -d /sys/firmware/efi/ ]; then if [ -d /sys/firmware/efi/ ]; then
cat << EOBLSEFI > /boot/loader/entries/`cat /etc/machine-id`-0-memtest86+-%{version}-uefi.%{mt_isa}.conf MEMTEST_IMAGE="memtest86+%{mt_isa}.efi"
title Memtest86+ v%{version} %{mt_isa} UEFI
linux /memtest86+%{mt_isa}.efi
EOBLSEFI
else
cat << EOBLSBIN > /boot/loader/entries/`cat /etc/machine-id`-0-memtest86+-%{version}-bios.%{mt_isa}.conf
title Memtest86+ v%{version} %{mt_isa} BIOS
linux /memtest86+%{mt_isa}.bin
EOBLSBIN
fi fi
KERNEL_INSTALL_PLUGINS=%{_libdir}/%{name}/memtest86+.kernel-install-plugin \
kernel-install add %{version} %{_libdir}/%{name}/${MEMTEST_IMAGE}
exit 0 exit 0
%postun
%preun
if [ $1 -eq 0 ]; then if [ $1 -eq 0 ]; then
rm -f /boot/memtest86+%{mt_isa}.* KERNEL_INSTALL_PLUGINS=%{_libdir}/%{name}/memtest86+.kernel-install-plugin \
rm -f /boot/loader/entries/`cat /etc/machine-id`-0-memtest86+-%{version}-*.%{mt_isa}.conf kernel-install remove %{version}
fi fi
exit 0 exit 0