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