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