memtest86plus/memtest86+.kernel-install-plugin
Jonathan Steffan e01efa4e1c 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
2023-06-30 15:12:20 -06:00

99 lines
2.9 KiB
Bash

#!/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