Blame SOURCES/rt-setup-kdump

876737
#!/bin/sh
876737
#
876737
# script to update /etc/sysconfig/kdump to use the latest 
876737
# kernel package as the dump kernel
876737
#
876737
# optional argument --grub causes kdump kernel cmdline to
876737
# be added to rt kernel grub entries
876737
#
876737
876737
me=$(basename $0)
876737
rpmcmd='rpm -q --last'
876737
876737
function fatal () {
876737
    echo "$me: " $1
876737
    exit -1
876737
}
876737
876737
function usage () {
876737
    echo "usage: $me [-g|--grub] [-r|--rhel] [-v|--verbose] [-h|--help]"
876737
    echo "       --grub    - add crashkernel arg to rt grub entries"
876737
    echo "       --rhel    - use the RHEL-7.1 kernel as the kdump kernel"
876737
    echo "                   (the default is to use the RHEL-RT kernel)"
876737
    echo "       --verbose - print out actions"
876737
    echo "       --help    - print this message"
876737
    exit -1
876737
}
876737
876737
function report() {
876737
    [ $verbose -eq 1 ] && echo $1
876737
}
876737
876737
# return the latest package version of specified package name
876737
function latest_package_ver() {
876737
    local pkg=$1
876737
    local ver=$($rpmcmd $pkg | head -1 | awk '{print $1}')
876737
    
876737
    if [ $? -ne 0 ]; then
876737
	fatal "  error fetching version for $pkg"
876737
    fi
876737
    echo ${ver#$pkg-}
876737
    return 0
876737
}
876737
876737
# get the kernel version of hhe latest installed kernel
876737
function vmlinux_ver() {
876737
    local ver=$1
876737
    local vmver=''
876737
    for i in $(cd /boot; echo vmlinuz-*); do
876737
	if [ "${i#vmlinuz-$ver}" != "$i" ]; then
876737
	    vmver=${i#vmlinuz-}
876737
	    echo $vmver
876737
	    return 0
876737
	fi
876737
    done
876737
    return 1
876737
}
876737
876737
# find all the grub indexs for installed rhel-rt kernels
876737
# returns a comma-separated list of indices for use
876737
# by the grubby command
876737
function find_rt_kernel_indexes_rhel_rt() {
876737
    local awkscript='BEGIN{FS="="; ORS=","} $1 ~ /^index/{idx=$2;}
876737
		$2 ~ /.rt.*.el7.x86_64/ &&
876737
		$1 ~ /^kernel/ {print idx}'
876737
    local rt_idx_list=$(/sbin/grubby --info=ALL | /usr/bin/awk "$awkscript")
876737
    
876737
    echo $rt_idx_list | sed -e 's/,$//'
876737
    return 0
876737
}
876737
876737
#############################################################################
876737
876737
# make sure we're root
876737
if [ $UID -ne 0 ]; then
876737
    echo "  must be root to run $me!"
876737
    usage
876737
fi
876737
876737
# process options
876737
dogrub=0
876737
userhel=0
876737
verbose=0
876737
TEMP=$(getopt --options "grvh" --longoptions="grub,rhel,verbose,help" -- "$@")
876737
if [ $? -ne 0 ]; then
876737
    usage
876737
fi
876737
eval set -- "$TEMP"
876737
while true; do
876737
    case "$1" in
876737
	-g|--grub)
876737
	    dogrub=1
876737
	    shift
876737
	    ;;
876737
	-r|--rhel)
876737
	    userhel=1
876737
	    shift
876737
	    ;;
876737
	-v|--verbose)
876737
	    verbose=1
876737
	    shift
876737
	    ;;
876737
	-h|--help)
876737
	    usage
876737
	    ;;
876737
	--) shift ; break ;;
876737
	*)
876737
	    echo "internal error!"
876737
	    usage
876737
	    ;;
876737
    esac
876737
done
876737
876737
# warn if /etc/sysconfig/kdump does not exist
876737
if [ ! -f /etc/sysconfig/kdump ]; then
876737
    echo "  File /etc/sysconfig/kdump not found."
876737
    echo "  Please, check your kexec-tools installation."
876737
    exit 1
876737
fi
876737
876737
if [ $dogrub = 0 ]; then
876737
    echo "Not performing changes to /etc/grub.conf"
876737
    echo
876737
    # check if there is memory reserved for the kexec kernel
876737
    if ! cat /proc/cmdline | grep -e crashkernel > /dev/null; then
876737
        echo "  Kernel DOES NOT have memory reserved for kdump kernel..."
876737
        echo "  Use --grub option to enable crashkernel option on kernel command line"
876737
	echo
876737
    fi
876737
fi
876737
876737
# select the right kdump kernel
876737
if [ $userhel -eq 1 ]; then
876737
	KDUMP_KERNEL="kernel"
876737
else
876737
	KDUMP_KERNEL="kernel-rt"
876737
fi
876737
# get the version of the latest installed kernel
876737
kver=$(latest_package_ver $KDUMP_KERNEL)
876737
if [ -z "$kver" ]; then
876737
    fatal "  Can't find $KDUMP_KERNEL package information!"
876737
fi
876737
876737
report "  making kernel-$kver the kdump kernel"
876737
876737
# find the vmlinux version info for the latest kernel package
876737
vmlinux_version=$(vmlinux_ver $kver)
876737
if [ -z "$vmlinux_version" ]; then
876737
    fatal "  Can't get vmlinux version!"
876737
fi
876737
876737
# now edit the /etc/sysconfig/kdump file
876737
sed -e "s/^KDUMP_KERNELVER.*$/KDUMP_KERNELVER=\"$vmlinux_version\"/" \
876737
    /etc/sysconfig/kdump >/tmp/kdump.$$
876737
mv /etc/sysconfig/kdump /etc/sysconfig/kdump.save && \
876737
    mv /tmp/kdump.$$ /etc/sysconfig/kdump
876737
876737
# if requested, update the grub entries for the rt kernels
876737
if [ $dogrub = 1 ]; then
876737
    rtver=$(latest_package_ver kernel-rt)
876737
    if [ -z "$rtver" ]; then
876737
	fatal "  Can't find kernel-rt package information!"
876737
    fi
876737
    # RHEL-RT kernel
876737
    kernels=$(find_rt_kernel_indexes_rhel_rt)
876737
    if [ ! -z $kernels ]; then
876737
        report "  adding 'crashkernel=auto' arg to grub entries: $kernels"
876737
        /sbin/grubby --update-kernel=$kernels --args="crashkernel=auto"
876737
    fi
876737
fi
876737
876737
exit $?