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