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