16aa69
#!/bin/sh
16aa69
16aa69
######################################################################
16aa69
# Prune undesired systemd.debug entries in kernels that are not
16aa69
# marked for debugging. See Red Hat bugzilla 1285601.
16aa69
######################################################################
16aa69
16aa69
PATH=/usr/bin:/usr/sbin
16aa69
EPOCH_DATE=$(date +"%s")
16aa69
LOG_DIR="/var/log"
16aa69
LOG_FILE="${LOG_DIR}/grubby_prune_debug"
16aa69
16aa69
display_event()
16aa69
{
16aa69
    EVENT_LABEL="$1"
16aa69
    EVENT_DATA="$2"
16aa69
    JUSTIFIED_LABEL=$(printf "%-12s" "${EVENT_LABEL}")
16aa69
    LOG_TIMESTAMP=$(date +"%s")
16aa69
    echo "[${LOG_TIMESTAMP}] ${JUSTIFIED_LABEL}: ${EVENT_DATA}" >> "${LOG_FILE}"
16aa69
}
16aa69
16aa69
exit_event()
16aa69
{
16aa69
    display_event "Exit" "Exiting script"
16aa69
    exit $1
16aa69
}
16aa69
16aa69
find_entry_by_title()
16aa69
{
16aa69
    SEARCH_TITLE=$1
16aa69
16aa69
    display_event "Examine" "Searching for entry title - ${SEARCH_TITLE}"
16aa69
16aa69
    for current_index in $(grubby --info=ALL|grep "^index="| sed 's/^index=//') ; do
16aa69
        CURRENT_TITLE=$(grubby --info="${current_index}" 2> /dev/null |grep "^title=" | sed 's/^title=//')
16aa69
16aa69
        # do not do anything if this was empty
16aa69
        [ -z "${CURRENT_TITLE}" ] && continue
16aa69
16aa69
        if [ "${CURRENT_TITLE}" = "${SEARCH_TITLE}" ]; then
16aa69
            echo "${current_index}"
16aa69
            break;
16aa69
        fi
16aa69
    done
16aa69
}
16aa69
16aa69
[ -d "${LOG_DIR}" ] || mkdir -p -m 0755 "${LOG_DIR}"
16aa69
16aa69
display_event "Start" "Begin search for extraneous debug arguments"
16aa69
16aa69
if [ "$(id -u)" -ne 0 ]; then
16aa69
    display_event "Error" "This script may only run as root."
16aa69
    exit_event 0
16aa69
fi
16aa69
16aa69
######################################################################
16aa69
# Back up the grub.cfg for sanity
16aa69
######################################################################
16aa69
BOOTLOADER_CFG=
16aa69
BOOTLOADER_CFG_BK=
16aa69
16aa69
if [ -f "/etc/grub2.cfg" ]; then
16aa69
    BOOTLOADER_CFG=$(realpath "/etc/grub2.cfg")
16aa69
    BOOTLOADER_CFG_BK="${BOOTLOADER_CFG}.${EPOCH_DATE}.rpmsave"
16aa69
elif [ -f "/etc/grub2-efi.cfg" ]; then
16aa69
    BOOTLOADER_CFG=$(realpath "/etc/grub2-efi.cfg")
16aa69
    BOOTLOADER_CFG_BK="${BOOTLOADER_CFG}.${EPOCH_DATE}.rpmsave"
16aa69
elif [ -f "/etc/zipl.conf" ]; then
16aa69
    BOOTLOADER_CFG=$(realpath "/etc/zipl.conf")
16aa69
    BOOTLOADER_CFG_BK="${BOOTLOADER_CFG}.${EPOCH_DATE}.rpmsave"
16aa69
elif [ -f "/etc/yaboot.conf" ]; then
16aa69
    BOOTLOADER_CFG=$(realpath "/etc/yaboot.conf")
16aa69
    BOOTLOADER_CFG_BK="${BOOTLOADER_CFG}.${EPOCH_DATE}.rpmsave"
16aa69
fi
16aa69
16aa69
if [ -z "${BOOTLOADER_CFG}" ]; then
16aa69
    display_event "Error" "Could not find a bootloader configuration to back up"
16aa69
    exit_event 0
16aa69
fi
16aa69
16aa69
if [ -f "${BOOTLOADER_CFG}" ]; then
16aa69
    if cp -a "${BOOTLOADER_CFG}" "${BOOTLOADER_CFG_BK}" 2> /dev/null; then
16aa69
        display_event "Copied" \
16aa69
            "Current configuration backed up to ${BOOTLOADER_CFG_BK}"
16aa69
    else
16aa69
        display_event "Error" "Could not write ${BOOTLOADER_CFG_BK}"
16aa69
        exit_event 0
16aa69
    fi
16aa69
fi
16aa69
16aa69
######################################################################
16aa69
#  Figure out what the debugging tag will look like
16aa69
######################################################################
16aa69
ARCH=$(uname -m)
16aa69
if [ $ARCH = 's390' -o $ARCH = 's390x' ]; then
16aa69
    DEBUGGING_TAG="_with_debugging"
16aa69
else
16aa69
    DEBUGGING_TAG=" with debugging"
16aa69
fi
16aa69
16aa69
######################################################################
16aa69
# Remove the systemd.debug kernel arguments from standard non-debug
16aa69
# kernel entries only
16aa69
######################################################################
16aa69
display_event "Examine" "Searching for distribution provided debugging entries"
16aa69
for entry_index in $(grubby --info=ALL|grep "^index="| sed 's/^index=//') ; do
16aa69
    INDEX_TITLE=$(grubby --info="${entry_index}" 2> /dev/null |grep "^title=" | sed 's/^title=//')
16aa69
16aa69
    # do not do anything if this was empty
16aa69
    [ -z "${INDEX_TITLE}" ] && continue
16aa69
16aa69
    if echo "${INDEX_TITLE}" | grep -q "${DEBUGGING_TAG}$"; then
16aa69
        display_event "Found" "Found distribution provided debugging entry - ${INDEX_TITLE}"
16aa69
        NON_DEBUG_TITLE=$(echo "${INDEX_TITLE}" | sed "s/${DEBUGGING_TAG}$//")
16aa69
        NON_DEBUG_INDEX=$(find_entry_by_title "${NON_DEBUG_TITLE}")
16aa69
16aa69
        if [ -n "${NON_DEBUG_INDEX}" ]; then
16aa69
            display_event "Found" "Found matching non-debugging entry - ${NON_DEBUG_TITLE}"
16aa69
            KERNEL_ARGS=$(grubby --info="${NON_DEBUG_INDEX}" 2> /dev/null |grep "^args=")
16aa69
            if echo "${KERNEL_ARGS}" | grep -E -q 'systemd.debug|systemd.log_level=debug|systemd.log_target=kmsg'; then
16aa69
                if grubby --update-kernel="${NON_DEBUG_INDEX}" --remove-args="systemd.debug systemd.log_level=debug systemd.log_target=kmsg"; then
16aa69
                    display_event "Update" "Removed debugging arguments for ${NON_DEBUG_TITLE}"
16aa69
                else
16aa69
                    display_event "Error" "grubby failed to remove debug argument from ${NON_DEBUG_TITLE}"
16aa69
                fi
16aa69
            else
16aa69
                display_event "Skip" "No debugging arguments to remove for ${NON_DEBUG_TITLE}"
16aa69
            fi
16aa69
        else
16aa69
            display_event "Error" "Could not find the matching non-debugging entry - ${NON_DEBUG_TITLE}"
16aa69
        fi
16aa69
    fi
16aa69
done
16aa69
16aa69
######################################################################
16aa69
# The default entry should not have debugging statements
16aa69
######################################################################
16aa69
16aa69
# source of ALLOW_DEBUGGING_DEFAULT
16aa69
[ -f /etc/sysconfig/kernel ] && . /etc/sysconfig/kernel
16aa69
16aa69
CURRENT_DEFAULT_INDEX=$(grubby --default-index)
16aa69
CURRENT_TITLE=$(grubby --info="${CURRENT_DEFAULT_INDEX}" 2> /dev/null |grep "^title=" | sed 's/^title=//')
16aa69
if echo "${CURRENT_TITLE}" | grep -q "${DEBUGGING_TAG}$"; then
16aa69
    case "${ALLOW_DEBUGGING_DEFAULT}" in
16aa69
        [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|1)
16aa69
            display_event "Examine" "Detected user preference to allow debugging default entries if present"
16aa69
            exit_event 0
16aa69
            ;;
16aa69
    esac
16aa69
16aa69
    display_event "Examine" "Search for entry without debugging to replace default entry - ${CURRENT_TITLE}"
16aa69
16aa69
    unset NON_DEBUG_INDEX
16aa69
    unset NON_DEBUG_TITLE
16aa69
16aa69
    ARCH=$(uname -m)
16aa69
    NON_DEBUG_TITLE=$(echo "${CURRENT_TITLE}" | sed "s/${DEBUGGING_TAG}$//")
16aa69
16aa69
    NON_DEBUG_INDEX=$(find_entry_by_title "${NON_DEBUG_TITLE}")
16aa69
16aa69
    if [ -n "${NON_DEBUG_INDEX}" ]; then
16aa69
        if grubby --set-default-index="${NON_DEBUG_INDEX}"; then
16aa69
            display_event "Update" "Selected ${NON_DEBUG_TITLE} as the new default entry"
16aa69
        else
16aa69
            display_event "Error" "Could not set ${NON_DEBUG_TITLE} as default"
16aa69
        fi
16aa69
    else
16aa69
        display_event "Error" "Could not find the matching non-debugging entry"
16aa69
    fi
16aa69
fi
16aa69
16aa69
exit_event 0