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