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