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