61faef6
#!/bin/bash
61faef6
db5dccf
if ! [[ $KERNEL_INSTALL_MACHINE_ID ]]; then
db5dccf
    exit 0
db5dccf
fi
db5dccf
61faef6
[[ -f /etc/default/grub ]] && . /etc/default/grub
61faef6
[[ -f /etc/os-release ]] && . /etc/os-release
61faef6
61faef6
COMMAND="$1"
61faef6
KERNEL_VERSION="$2"
61faef6
BOOT_DIR_ABS="$3"
61faef6
KERNEL_IMAGE="$4"
61faef6
61faef6
KERNEL_DIR="${KERNEL_IMAGE%/*}"
61faef6
61faef6
MACHINE_ID=$KERNEL_INSTALL_MACHINE_ID
61faef6
30b139c
# If ${BOOT_DIR_ABS} exists, some other boot loader is active.
30b139c
[[ -d "${BOOT_DIR_ABS}" ]] && exit 0
61faef6
61faef6
BLS_DIR="/boot/loader/entries"
61faef6
78e1a10
mkbls() {
78e1a10
    local kernelver=$1 && shift
78e1a10
    local datetime=$1 && shift
78e1a10
78e1a10
    local debugname=""
dc178ac
    local debugid=""
78e1a10
    local flavor=""
78e1a10
dc178ac
    if [[ "$kernelver" == *\+* ]] ; then
dc178ac
        local flavor=-"${kernelver##*+}"
78e1a10
        if [[ "${flavor}" == "-debug" ]]; then
78e1a10
            local debugname=" with debugging"
dc178ac
            local debugid="-debug"
78e1a10
        fi
78e1a10
    fi
78e1a10
78e1a10
    cat <
78e1a10
title ${NAME} (${kernelver}) ${VERSION}${debugname}
dc178ac
version ${kernelver}${debugid}
78e1a10
linux /vmlinuz-${kernelver}
78e1a10
initrd /initramfs-${kernelver}.img
78e1a10
options \$kernelopts
78e1a10
id ${ID}-${datetime}-${kernelver}
78e1a10
grub_users \$grub_users
78e1a10
grub_arg --unrestricted
78e1a10
grub_class kernel${flavor}
78e1a10
EOF
78e1a10
}
78e1a10
61faef6
[[ "$KERNEL_VERSION" == *\+* ]] && flavor=-"${KERNEL_VERSION##*+}"
61faef6
case "$COMMAND" in
61faef6
    add)
61faef6
        if [[ "${KERNEL_DIR}" != "/boot" ]]; then
61faef6
            for i in \
61faef6
                "$KERNEL_IMAGE" \
61faef6
                    "$KERNEL_DIR"/System.map \
61faef6
                    "$KERNEL_DIR"/config \
61faef6
                    "$KERNEL_DIR"/zImage.stub \
78e1a10
                    "$KERNEL_DIR"/dtb
78e1a10
            do
61faef6
                [[ -e "$i" ]] || continue
c9b8b10
                rm -f "/boot/${i##*/}-${KERNEL_VERSION}"
61faef6
                cp -aT "$i" "/boot/${i##*/}-${KERNEL_VERSION}"
61faef6
                command -v restorecon &>/dev/null && \
61faef6
                    restorecon -R "/boot/${i##*/}-${KERNEL_VERSION}"
61faef6
            done
61faef6
            # hmac is .vmlinuz-<version>.hmac so needs a special treatment
61faef6
            i="$KERNEL_DIR/.${KERNEL_IMAGE##*/}.hmac"
61faef6
            if [[ -e "$i" ]]; then
c9b8b10
                rm -f "/boot/.${KERNEL_IMAGE##*/}-${KERNEL_VERSION}.hmac"
61faef6
                cp -a "$i" "/boot/.${KERNEL_IMAGE##*/}-${KERNEL_VERSION}.hmac"
61faef6
                command -v restorecon &>/dev/null && \
61faef6
                    restorecon "/boot/.${KERNEL_IMAGE##*/}-${KERNEL_VERSION}.hmac"
61faef6
            fi
61faef6
        fi
61faef6
78e1a10
        if [[ "x${GRUB_ENABLE_BLSCFG}" = "xtrue" ]] || [[ ! -f /sbin/new-kernel-pkg ]]; then
66078fc
            eval "$(grub2-get-kernel-settings)" || true
78e1a10
            [[ -d "$BLS_DIR" ]] || mkdir -m 0700 -p "$BLS_DIR"
66078fc
            BLS_ID="${MACHINE_ID}-${KERNEL_VERSION}"
66078fc
            BLS_TARGET="${BLS_DIR}/${BLS_ID}.conf"
78e1a10
            if [[ -f "${KERNEL_DIR}/bls.conf" ]]; then
78e1a10
                cp -aT "${KERNEL_DIR}/bls.conf" "${BLS_TARGET}" || exit $?
78e1a10
            else
78e1a10
                mkbls "${KERNEL_VERSION}" \
78e1a10
                    "$(date -u +%Y%m%d%H%M%S -d "$(stat -c '%y' "${KERNEL_DIR}")")" \
78e1a10
                    >"${BLS_TARGET}"
78e1a10
            fi
d8bbf03
            command -v restorecon &>/dev/null && \
d8bbf03
                restorecon -R "${BLS_TARGET}"
ed50db3
6442061
            LINUX="$(grep '^linux[ \t]' "${BLS_TARGET}" | sed -e 's,^linux[ \t]*,,')"
6442061
            INITRD="$(grep '^initrd[ \t]' "${BLS_TARGET}" | sed -e 's,^initrd[ \t]*,,')"
6442061
            LINUX_RELPATH="$(grub2-mkrelpath /boot${LINUX})"
6442061
            BOOTPREFIX="$(dirname ${LINUX_RELPATH})"
c9b8b10
            ROOTPREFIX="$(dirname "/boot${LINUX}")"
6442061
6442061
            if [[ $LINUX != $LINUX_RELPATH ]]; then
6442061
                sed -i -e "s,^linux.*,linux ${BOOTPREFIX}${LINUX},g" "${BLS_TARGET}"
6442061
                sed -i -e "s,^initrd.*,initrd ${BOOTPREFIX}${INITRD},g" "${BLS_TARGET}"
6442061
            fi
6442061
ff9dc1e
            if [[ "$KERNEL_VERSION" == *\+* ]] && [ "x$GRUB_DEFAULT_TO_DEBUG" != "xtrue" ]; then
ff9dc1e
                GRUB_UPDATE_DEFAULT_KERNEL=false
ff9dc1e
            fi
ff9dc1e
ff9dc1e
            if [ "x$GRUB_UPDATE_DEFAULT_KERNEL" = "xtrue" ]; then
ff9dc1e
                NEWDEFAULT="${BLS_ID}"
ff9dc1e
            fi
ff9dc1e
ed50db3
            if [ "x$GRUB_LINUX_MAKE_DEBUG" = "xtrue" ]; then
ed50db3
                ARCH="$(uname -m)"
efb46df
                BLS_DEBUG_ID="$(echo ${BLS_ID} | sed -e "s/${KERNEL_VERSION}/${KERNEL_VERSION}~debug/")"
efb46df
                BLS_DEBUG="$(echo ${BLS_TARGET} | sed -e "s/${KERNEL_VERSION}/${KERNEL_VERSION}~debug/")"
ed50db3
                cp -aT  "${BLS_TARGET}" "${BLS_DEBUG}"
ed50db3
                TITLE="$(grep '^title[ \t]' "${BLS_DEBUG}" | sed -e 's/^title[ \t]*//')"
ed50db3
                sed -i -e "s/^title.*/title ${TITLE}${GRUB_LINUX_DEBUG_TITLE_POSTFIX}/" "${BLS_DEBUG}"
66078fc
                sed -i -e "s/^id.*/id ${BLS_DEBUG_ID}/" "${BLS_DEBUG}"
ed50db3
                sed -i -e "s/^options.*/options \$kernelopts ${GRUB_CMDLINE_LINUX_DEBUG}/" "${BLS_DEBUG}"
ff9dc1e
                if [ -n "$NEWDEFAULT" -a "x$GRUB_DEFAULT_TO_DEBUG" = "xtrue" ]; then
66078fc
                    NEWDEFAULT="${BLS_DEBUG_ID}"
66078fc
                fi
66078fc
            fi
ff9dc1e
            if [ -n "$NEWDEFAULT" ]; then
ff9dc1e
                grub2-editenv - set "saved_entry=${NEWDEFAULT}"
ff9dc1e
            fi
ed50db3
67aea4f
            # this probably isn't the best place to do this, but it will do for now.
c9b8b10
            if [ -e "${ROOTPREFIX}${INITRD}" -a -e "${ROOTPREFIX}${LINUX}" -a \
c9b8b10
                 "${ROOTPREFIX}${INITRD}" -ot "${ROOTPREFIX}${LINUX}" -a \
67aea4f
                 -x /usr/lib/kernel/install.d/50-dracut.install ]; then
c9b8b10
                rm -f "${ROOTPREFIX}${INITRD}"
67aea4f
            fi
78e1a10
            exit 0
61faef6
        fi
61faef6
61faef6
        /sbin/new-kernel-pkg --package "kernel${flavor}" --install "$KERNEL_VERSION" || exit $?
61faef6
        /sbin/new-kernel-pkg --package "kernel${flavor}" --mkinitrd --dracut --depmod --update "$KERNEL_VERSION" || exit $?
61faef6
        /sbin/new-kernel-pkg --package "kernel${flavor}" --rpmposttrans "$KERNEL_VERSION" || exit $?
78e1a10
        # If grubby is used there's no need to run other installation plugins
78e1a10
        exit 77
61faef6
        ;;
61faef6
    remove)
61faef6
78e1a10
        if [[ "x${GRUB_ENABLE_BLSCFG}" = "xtrue" ]] || [[ ! -f /sbin/new-kernel-pkg ]]; then
ed50db3
            ARCH="$(uname -m)"
ed50db3
            BLS_TARGET="${BLS_DIR}/${MACHINE_ID}-${KERNEL_VERSION}.conf"
efb46df
            BLS_DEBUG="$(echo ${BLS_TARGET} | sed -e "s/${KERNEL_VERSION}/${KERNEL_VERSION}~debug/")"
ed50db3
            rm -f "${BLS_TARGET}" "${BLS_DEBUG}"
61faef6
61faef6
            for i in vmlinuz System.map config zImage.stub dtb; do
61faef6
                rm -rf "/boot/${i}-${KERNEL_VERSION}"
61faef6
            done
61faef6
            # hmac is .vmlinuz-<version>.hmac so needs a special treatment
61faef6
            rm -f "/boot/.vmlinuz-${KERNEL_VERSION}.hmac"
61faef6
61faef6
            exit 0
61faef6
        fi
61faef6
61faef6
        /sbin/new-kernel-pkg --package "kernel${flavor+-$flavor}" --rminitrd --rmmoddep --remove "$KERNEL_VERSION" || exit $?
61faef6
        # If grubby is used there's no need to run other installation plugins
61faef6
        exit 77
61faef6
        ;;
61faef6
    *)
61faef6
        ;;
61faef6
esac