893e0b
#!/bin/sh
893e0b
#
893e0b
# Kdump common variables and functions
893e0b
#
893e0b
893e0b
DEFAULT_PATH="/var/crash/"
893e0b
FENCE_KDUMP_CONFIG_FILE="/etc/sysconfig/fence_kdump"
893e0b
FENCE_KDUMP_SEND="/usr/libexec/fence_kdump_send"
893e0b
FADUMP_ENABLED_SYS_NODE="/sys/kernel/fadump_enabled"
893e0b
893e0b
is_fadump_capable()
893e0b
{
893e0b
    # Check if firmware-assisted dump is enabled
893e0b
    # if no, fallback to kdump check
893e0b
    if [ -f $FADUMP_ENABLED_SYS_NODE ]; then
893e0b
        rc=`cat $FADUMP_ENABLED_SYS_NODE`
893e0b
        [ $rc -eq 1 ] && return 0
893e0b
    fi
893e0b
    return 1
893e0b
}
893e0b
893e0b
perror_exit() {
893e0b
    echo $@ >&2
893e0b
    exit 1
893e0b
}
893e0b
893e0b
perror() {
893e0b
    echo $@ >&2
893e0b
}
893e0b
893e0b
is_ssh_dump_target()
893e0b
{
893e0b
    grep -q "^ssh[[:blank:]].*@" /etc/kdump.conf
893e0b
}
893e0b
893e0b
is_nfs_dump_target()
893e0b
{
893e0b
    grep -q "^nfs" /etc/kdump.conf || \
893e0b
        [[ $(get_dracut_args_fstype "$(grep "^dracut_args .*\-\-mount" /etc/kdump.conf)") = nfs* ]]
893e0b
}
893e0b
893e0b
is_raw_dump_target()
893e0b
{
893e0b
    grep -q "^raw" /etc/kdump.conf
893e0b
}
893e0b
893e0b
is_fs_type_nfs()
893e0b
{
893e0b
    local _fstype=$1
893e0b
    [ $_fstype = "nfs" ] || [ $_fstype = "nfs4" ] && return 0
893e0b
    return 1
893e0b
}
893e0b
893e0b
is_fs_dump_target()
893e0b
{
893e0b
    egrep -q "^ext[234]|^xfs|^btrfs|^minix" /etc/kdump.conf
893e0b
}
893e0b
893e0b
strip_comments()
893e0b
{
893e0b
    echo $@ | sed -e 's/\(.*\)#.*/\1/'
893e0b
}
893e0b
893e0b
# Check if fence kdump is configured in Pacemaker cluster
893e0b
is_pcs_fence_kdump()
893e0b
{
893e0b
    # no pcs or fence_kdump_send executables installed?
893e0b
    type -P pcs > /dev/null || return 1
893e0b
    [ -x $FENCE_KDUMP_SEND ] || return 1
893e0b
893e0b
    # fence kdump not configured?
893e0b
    (pcs cluster cib | grep 'type="fence_kdump"') &> /dev/null || return 1
893e0b
}
893e0b
893e0b
# Check if fence_kdump is configured using kdump options
893e0b
is_generic_fence_kdump()
893e0b
{
893e0b
    [ -x $FENCE_KDUMP_SEND ] || return 1
893e0b
893e0b
    grep -q "^fence_kdump_nodes" /etc/kdump.conf
893e0b
}
893e0b
893e0b
to_dev_name() {
893e0b
    local dev="${1//\"/}"
893e0b
893e0b
    case "$dev" in
893e0b
    UUID=*)
893e0b
        dev=`blkid -U "${dev#UUID=}"`
893e0b
        ;;
893e0b
    LABEL=*)
893e0b
        dev=`blkid -L "${dev#LABEL=}"`
893e0b
        ;;
893e0b
    esac
893e0b
    echo $dev
893e0b
}
893e0b
893e0b
is_user_configured_dump_target()
893e0b
{
893e0b
    return $(is_mount_in_dracut_args || is_ssh_dump_target || is_nfs_dump_target || \
893e0b
             is_raw_dump_target || is_fs_dump_target)
893e0b
}
893e0b
893e0b
get_user_configured_dump_disk()
893e0b
{
893e0b
    local _target
893e0b
893e0b
    _target=$(egrep "^ext[234]|^xfs|^btrfs|^minix|^raw" /etc/kdump.conf 2>/dev/null |awk '{print $2}')
893e0b
    [ -n "$_target" ] && echo $_target && return
893e0b
893e0b
    _target=$(get_dracut_args_target "$(grep "^dracut_args .*\-\-mount" /etc/kdump.conf)")
893e0b
    [ -b "$_target" ] && echo $_target
893e0b
}
893e0b
893e0b
get_root_fs_device()
893e0b
{
893e0b
    local _target
893e0b
    _target=$(findmnt -k -f -n -o SOURCE /)
893e0b
    [ -n "$_target" ] && echo $_target
893e0b
893e0b
    return
893e0b
}
893e0b
893e0b
get_save_path()
893e0b
{
893e0b
	local _save_path=$(grep "^path" /etc/kdump.conf|awk '{print $2}')
893e0b
	if [ -z "$_save_path" ]; then
893e0b
		_save_path=$DEFAULT_PATH
893e0b
	fi
893e0b
893e0b
	echo $_save_path
893e0b
}
893e0b
893e0b
get_block_dump_target()
893e0b
{
893e0b
    local _target _path
893e0b
893e0b
    if is_ssh_dump_target || is_nfs_dump_target; then
893e0b
        return
893e0b
    fi
893e0b
893e0b
    _target=$(get_user_configured_dump_disk)
893e0b
    [ -n "$_target" ] && echo $(to_dev_name $_target) && return
893e0b
893e0b
    # Get block device name from local save path
893e0b
    _path=$(get_save_path)
893e0b
    _target=$(get_target_from_path $_path)
893e0b
    [ -b "$_target" ] && echo $(to_dev_name $_target)
893e0b
}
893e0b
893e0b
is_dump_to_rootfs()
893e0b
{
893e0b
    grep "^default[[:space:]]dump_to_rootfs" /etc/kdump.conf >/dev/null
893e0b
}
893e0b
893e0b
get_default_action_target()
893e0b
{
893e0b
    local _target
893e0b
893e0b
    if is_dump_to_rootfs; then
893e0b
        # Get rootfs device name
893e0b
        _target=$(get_root_fs_device)
893e0b
        [ -b "$_target" ] && echo $(to_dev_name $_target) && return
893e0b
        # Then, must be nfs root
893e0b
        echo "nfs"
893e0b
    fi
893e0b
}
893e0b
893e0b
# Get kdump targets(including root in case of dump_to_rootfs).
893e0b
get_kdump_targets()
893e0b
{
893e0b
    local _target _root
893e0b
    local kdump_targets
893e0b
893e0b
    _target=$(get_block_dump_target)
893e0b
    if [ -n "$_target" ]; then
893e0b
        kdump_targets=$_target
893e0b
    elif is_ssh_dump_target; then
893e0b
        kdump_targets="ssh"
893e0b
    else
893e0b
        kdump_targets="nfs"
893e0b
    fi
893e0b
893e0b
    # Add the root device if dump_to_rootfs is specified.
893e0b
    _root=$(get_default_action_target)
893e0b
    if [ -n "$_root" -a "$kdump_targets" != "$_root" ]; then
893e0b
        kdump_targets="$kdump_targets $_root"
893e0b
    fi
893e0b
893e0b
    echo "$kdump_targets"
893e0b
}
893e0b
893e0b
893e0b
# findmnt uses the option "-v, --nofsroot" to exclusive the [/dir]
893e0b
# in the SOURCE column for bind-mounts, then if $_mntpoint equals to
893e0b
# $_mntpoint_nofsroot, the mountpoint is not bind mounted directory.
893e0b
is_bind_mount()
893e0b
{
893e0b
    local _mntpoint=$(findmnt $1 | tail -n 1 | awk '{print $2}')
893e0b
    local _mntpoint_nofsroot=$(findmnt -v $1 | tail -n 1 | awk '{print $2}')
893e0b
893e0b
    if [[ $_mntpoint = $_mntpoint_nofsroot ]]; then
893e0b
        return 1
893e0b
    else
893e0b
        return 0
893e0b
    fi
893e0b
}
893e0b
893e0b
# Below is just an example for mount info
893e0b
# /dev/mapper/atomicos-root[/ostree/deploy/rhel-atomic-host/var], if the
893e0b
# directory is bind mounted. The former part represents the device path, rest
893e0b
# part is the bind mounted directory which quotes by bracket "[]".
893e0b
get_bind_mount_directory()
893e0b
{
893e0b
    local _mntpoint=$(findmnt $1 | tail -n 1 | awk '{print $2}')
893e0b
    local _mntpoint_nofsroot=$(findmnt -v $1 | tail -n 1 | awk '{print $2}')
893e0b
893e0b
    _mntpoint=${_mntpoint#*$_mntpoint_nofsroot}
893e0b
893e0b
    _mntpoint=${_mntpoint#[}
893e0b
    _mntpoint=${_mntpoint%]}
893e0b
893e0b
    echo $_mntpoint
893e0b
}
893e0b
893e0b
get_mntpoint_from_path() 
893e0b
{
893e0b
    echo $(df $1 | tail -1 |  awk '{print $NF}')
893e0b
}
893e0b
893e0b
get_target_from_path()
893e0b
{
893e0b
    local _target
893e0b
893e0b
    _target=$(df $1 2>/dev/null | tail -1 |  awk '{print $1}')
893e0b
    [[ "$_target" == "/dev/root" ]] && [[ ! -e /dev/root ]] && _target=$(get_root_fs_device)
893e0b
    echo $_target
893e0b
}
893e0b
893e0b
get_fs_type_from_target() 
893e0b
{
893e0b
    echo $(findmnt -k -f -n -r -o FSTYPE $1)
893e0b
}
893e0b
893e0b
# input: device path
893e0b
# output: the general mount point
893e0b
# find the general mount point, not the bind mounted point in atomic
893e0b
# As general system, Use the previous code
893e0b
#
893e0b
# ERROR and EXIT:
893e0b
# the device can be umounted the general mount point, if one of the mount point is bind mounted
893e0b
# For example:
893e0b
# mount /dev/sda /mnt/
893e0b
# mount -o bind /mnt/var /var
893e0b
# umount /mnt
893e0b
get_mntpoint_from_target()
893e0b
{
893e0b
    if is_atomic; then
893e0b
        for _mnt in $(findmnt -k -n -r -o TARGET $1)
893e0b
        do
893e0b
            if ! is_bind_mount $_mnt; then
893e0b
                echo $_mnt
893e0b
                return
893e0b
            fi
893e0b
        done
893e0b
893e0b
        echo "Mount $1 firstly, without the bind mode" >&2
893e0b
        exit 1
893e0b
    else
893e0b
        echo $(findmnt -k -f -n -r -o TARGET $1)
893e0b
    fi
893e0b
}
893e0b
893e0b
# get_option_value <option_name>
893e0b
# retrieves value of option defined in kdump.conf
893e0b
get_option_value() {
893e0b
    echo $(strip_comments `grep "^$1[[:space:]]\+" /etc/kdump.conf | tail -1 | cut -d\  -f2-`)
893e0b
}
893e0b
893e0b
#This function compose a absolute path with the mount
893e0b
#point and the relative $SAVE_PATH.
893e0b
#target is passed in as argument, could be UUID, LABEL,
893e0b
#block device or even nfs server export of the form of
893e0b
#"my.server.com:/tmp/export"?
893e0b
#And possibly this could be used for both default case
893e0b
#as well as when dump taret is specified. When dump
893e0b
#target is not specified, then $target would be null.
893e0b
make_absolute_save_path()
893e0b
{
893e0b
    local _target=$1
893e0b
    local _mnt
893e0b
893e0b
    [ -n $_target ] && _mnt=$(get_mntpoint_from_target $1)
893e0b
    _mnt="${_mnt}/$SAVE_PATH"
893e0b
893e0b
    # strip the duplicated "/"
893e0b
    echo "$_mnt" | tr -s /
893e0b
}
893e0b
893e0b
check_save_path_fs()
893e0b
{
893e0b
    local _path=$1
893e0b
893e0b
    if [ ! -d $_path ]; then
893e0b
        perror_exit "Dump path $_path does not exist."
893e0b
    fi
893e0b
}
893e0b
893e0b
is_atomic()
893e0b
{
893e0b
    grep -q "ostree" /proc/cmdline
893e0b
}
893e0b
893e0b
# fixme, try the best to decide whether the ipv6 addr is allocated by slaac or dhcp6
893e0b
is_ipv6_auto()
893e0b
{
893e0b
    local _netdev=$1
893e0b
    local _auto=$(cat /proc/sys/net/ipv6/conf/$_netdev/autoconf)
893e0b
    if [ $_auto -eq 1 ]; then
893e0b
        return 0
893e0b
    else
893e0b
        return 1
893e0b
    fi
893e0b
}
893e0b
893e0b
is_ipv6_address()
893e0b
{
893e0b
    echo $1 | grep -q ":"
893e0b
}
893e0b
893e0b
# get ip address or hostname from nfs/ssh config value
893e0b
get_remote_host()
893e0b
{
893e0b
    local _config_val=$1
893e0b
893e0b
    # ipv6 address in kdump.conf is around with "[]",
893e0b
    # factor out the ipv6 address
893e0b
    _config_val=${_config_val#*@}
893e0b
    _config_val=${_config_val%:/*}
893e0b
    _config_val=${_config_val#[}
893e0b
    _config_val=${_config_val%]}
893e0b
    echo $_config_val
893e0b
}
893e0b
893e0b
is_hostname()
893e0b
{
893e0b
    local _hostname=`echo $1 | grep ":"`
893e0b
893e0b
    if [ -n "$_hostname" ]; then
893e0b
        return 1
893e0b
    fi
893e0b
    echo $1 | grep -q "[a-zA-Z]"
893e0b
}
893e0b
893e0b
# Copied from "/etc/sysconfig/network-scripts/network-functions"
893e0b
get_hwaddr()
893e0b
{
893e0b
    if [ -f "/sys/class/net/${1}/address" ]; then
893e0b
        awk '{ print toupper($0) }' < /sys/class/net/${1}/address
893e0b
    elif [ -d "/sys/class/net/${1}" ]; then
893e0b
       LC_ALL= LANG= ip -o link show ${1} 2>/dev/null | \
893e0b
            awk '{ print toupper(gensub(/.*link\/[^ ]* ([[:alnum:]:]*).*/,
893e0b
                                        "\\1", 1)); }'
893e0b
    fi
893e0b
}
893e0b
893e0b
get_ifcfg_by_device()
893e0b
{
893e0b
    grep -E -i -l "^[[:space:]]*DEVICE=\"*${1}\"*[[:space:]]*$" \
893e0b
         /etc/sysconfig/network-scripts/ifcfg-* 2>/dev/null | head -1
893e0b
}
893e0b
893e0b
get_ifcfg_by_hwaddr()
893e0b
{
893e0b
    grep -E -i -l "^[[:space:]]*HWADDR=\"*${1}\"*[[:space:]]*$" \
893e0b
         /etc/sysconfig/network-scripts/ifcfg-* 2>/dev/null | head -1
893e0b
}
893e0b
893e0b
get_ifcfg_by_uuid()
893e0b
{
893e0b
    grep -E -i -l "^[[:space:]]*UUID=\"*${1}\"*[[:space:]]*$" \
893e0b
         /etc/sysconfig/network-scripts/ifcfg-* 2>/dev/null | head -1
893e0b
}
893e0b
893e0b
get_ifcfg_by_name()
893e0b
{
893e0b
    grep -E -i -l "^[[:space:]]*NAME=\"*${1}\"*[[:space:]]*$" \
893e0b
         /etc/sysconfig/network-scripts/ifcfg-* 2>/dev/null | head -1
893e0b
}
893e0b
893e0b
is_nm_running()
893e0b
{
893e0b
    [ "$(LANG=C nmcli -t --fields running general status 2>/dev/null)" = "running" ]
893e0b
}
893e0b
893e0b
is_nm_handling()
893e0b
{
893e0b
    LANG=C nmcli -t --fields device,state  dev status 2>/dev/null \
893e0b
          | grep -q "^\(${1}:connected\)\|\(${1}:connecting.*\)$"
893e0b
}
893e0b
893e0b
# $1: netdev name
893e0b
get_ifcfg_nmcli()
893e0b
{
893e0b
    local nm_uuid nm_name
893e0b
    local ifcfg_file
893e0b
893e0b
    # Get the active nmcli config name of $1
893e0b
    if is_nm_running && is_nm_handling "${1}" ; then
893e0b
        # The configuration "uuid" and "name" generated by nm is wrote to
893e0b
        # the ifcfg file as "UUID=<nm_uuid>" and "NAME=<nm_name>".
893e0b
        nm_uuid=$(LANG=C nmcli -t --fields uuid,device c show --active 2>/dev/null \
893e0b
                  | grep "${1}" | head -1 | cut -d':' -f1)
893e0b
        nm_name=$(LANG=C nmcli -t --fields name,device c show --active 2>/dev/null \
893e0b
                  | grep "${1}" | head -1 | cut -d':' -f1)
893e0b
        ifcfg_file=$(get_ifcfg_by_uuid "${nm_uuid}")
893e0b
        [ -z "${ifcfg_file}" ] && ifcfg_file=$(get_ifcfg_by_name "${nm_name}")
893e0b
    fi
893e0b
893e0b
    echo -n "${ifcfg_file}"
893e0b
}
893e0b
893e0b
# $1: netdev name
893e0b
get_ifcfg_legacy()
893e0b
{
893e0b
    local ifcfg_file
893e0b
893e0b
    ifcfg_file="/etc/sysconfig/network-scripts/ifcfg-${1}"
893e0b
    [ -f "${ifcfg_file}" ] && echo -n "${ifcfg_file}" && return
893e0b
893e0b
    ifcfg_file=$(get_ifcfg_by_name "${1}")
893e0b
    [ -f "${ifcfg_file}" ] && echo -n "${ifcfg_file}" && return
893e0b
893e0b
    local hwaddr=$(get_hwaddr "${1}")
893e0b
    if [ -n "$hwaddr" ]; then
893e0b
        ifcfg_file=$(get_ifcfg_by_hwaddr "${hwaddr}")
893e0b
        [ -f "${ifcfg_file}" ] && echo -n "${ifcfg_file}" && return
893e0b
    fi
893e0b
893e0b
    ifcfg_file=$(get_ifcfg_by_device "${1}")
893e0b
893e0b
    echo -n "${ifcfg_file}"
893e0b
}
893e0b
893e0b
# $1: netdev name
893e0b
# Return the ifcfg file whole name(including the path) of $1 if any.
893e0b
get_ifcfg_filename() {
893e0b
    local ifcfg_file
893e0b
893e0b
    ifcfg_file=$(get_ifcfg_nmcli "${1}")
893e0b
    if [ -z "${ifcfg_file}" ]; then
893e0b
        ifcfg_file=$(get_ifcfg_legacy "${1}")
893e0b
    fi
893e0b
893e0b
    echo -n "${ifcfg_file}"
893e0b
}
893e0b
893e0b
# returns 0 when omission of watchdog module is desired in dracut_args
893e0b
# returns 1 otherwise
893e0b
is_wdt_mod_omitted() {
893e0b
	local dracut_args
893e0b
	local ret=1
893e0b
893e0b
	dracut_args=$(grep  "^dracut_args" /etc/kdump.conf)
893e0b
	[[ -z $dracut_args ]] && return $ret
893e0b
893e0b
	eval set -- $dracut_args
893e0b
	while :; do
893e0b
		[[ -z $1 ]] && break
893e0b
		case $1 in
893e0b
			-o|--omit)
893e0b
				echo $2 | grep -qw "watchdog"
893e0b
				[[ $? == 0 ]] && ret=0
893e0b
				break
893e0b
		esac
893e0b
		shift
893e0b
	done
893e0b
893e0b
	return $ret
893e0b
}
893e0b
893e0b
# If "dracut_args" contains "--mount" information, use it
893e0b
# directly without any check(users are expected to ensure
893e0b
# its correctness).
893e0b
is_mount_in_dracut_args()
893e0b
{
893e0b
    grep -q "^dracut_args .*\-\-mount" /etc/kdump.conf
893e0b
}
893e0b
893e0b
# If $1 contains dracut_args "--mount", return <filesystem type>
893e0b
get_dracut_args_fstype()
893e0b
{
893e0b
    echo $1 | grep "\-\-mount" | sed "s/.*--mount .\(.*\)/\1/" | cut -d' ' -f3
893e0b
}
893e0b
893e0b
# If $1 contains dracut_args "--mount", return <device>
893e0b
get_dracut_args_target()
893e0b
{
893e0b
    echo $1 | grep "\-\-mount" | sed "s/.*--mount .\(.*\)/\1/" | cut -d' ' -f1
893e0b
}
893e0b
893e0b
check_crash_mem_reserved()
893e0b
{
893e0b
    local mem_reserved
893e0b
893e0b
    mem_reserved=$(cat /sys/kernel/kexec_crash_size)
893e0b
    if [ $mem_reserved -eq 0 ]; then
893e0b
        echo "No memory reserved for crash kernel"
893e0b
        return 1
893e0b
    fi
893e0b
893e0b
    return 0
893e0b
}
893e0b
893e0b
check_kdump_feasibility()
893e0b
{
893e0b
    if [ ! -e /sys/kernel/kexec_crash_loaded ]; then
893e0b
        echo "Kdump is not supported on this kernel"
893e0b
        return 1
893e0b
    fi
893e0b
    check_crash_mem_reserved
893e0b
    return $?
893e0b
}
893e0b
893e0b
check_current_kdump_status()
893e0b
{
893e0b
    if [ ! -f /sys/kernel/kexec_crash_loaded ];then
893e0b
        echo "Perhaps CONFIG_CRASH_DUMP is not enabled in kernel"
893e0b
        return 1
893e0b
    fi
893e0b
893e0b
    rc=`cat /sys/kernel/kexec_crash_loaded`
893e0b
    if [ $rc == 1 ]; then
893e0b
        return 0
893e0b
    else
893e0b
        return 1
893e0b
    fi
893e0b
}
893e0b
893e0b
# remove_cmdline_param <kernel cmdline> <param1> [<param2>] ... [<paramN>]
893e0b
# Remove a list of kernel parameters from a given kernel cmdline and print the result.
893e0b
# For each "arg" in the removing params list, "arg" and "arg=xxx" will be removed if exists.
893e0b
remove_cmdline_param()
893e0b
{
893e0b
    local cmdline=$1
893e0b
    shift
893e0b
893e0b
    for arg in $@; do
893e0b
        cmdline=`echo $cmdline | \
893e0b
                 sed -e "s/\b$arg=[^ ]*//g" \
893e0b
                 -e "s/^$arg\b//g" \
893e0b
                 -e "s/[[:space:]]$arg\b//g" \
893e0b
                 -e "s/\s\+/ /g"`
893e0b
    done
893e0b
    echo $cmdline
893e0b
}
893e0b
893e0b
#
893e0b
# This function returns the "apicid" of the boot
893e0b
# cpu (cpu 0) if present.
893e0b
#
893e0b
get_bootcpu_apicid()
893e0b
{
893e0b
    awk '                                                       \
893e0b
        BEGIN { CPU = "-1"; }                                   \
893e0b
        $1=="processor" && $2==":"      { CPU = $NF; }          \
893e0b
        CPU=="0" && /^apicid/           { print $NF; }          \
893e0b
        '                                                       \
893e0b
        /proc/cpuinfo
893e0b
}
893e0b
893e0b
#
893e0b
# append_cmdline <kernel cmdline> <parameter name> <parameter value>
893e0b
# This function appends argument "$2=$3" to string ($1) if not already present.
893e0b
#
893e0b
append_cmdline()
893e0b
{
893e0b
    local cmdline=$1
893e0b
    local newstr=${cmdline/$2/""}
893e0b
893e0b
    # unchanged str implies argument wasn't there
893e0b
    if [ "$cmdline" == "$newstr" ]; then
893e0b
        cmdline="${cmdline} ${2}=${3}"
893e0b
    fi
893e0b
893e0b
    echo $cmdline
893e0b
}
893e0b
893e0b
# This function check iomem and determines if we have more than
893e0b
# 4GB of ram available. Returns 1 if we do, 0 if we dont
893e0b
need_64bit_headers()
893e0b
{
893e0b
    return `tail -n 1 /proc/iomem | awk '{ split ($1, r, "-"); \
893e0b
    print (strtonum("0x" r[2]) > strtonum("0xffffffff")); }'`
893e0b
}
893e0b
893e0b
# Check if secure boot is being enforced.
893e0b
#
893e0b
# Per Peter Jones, we need check efivar SecureBoot-$(the UUID) and
893e0b
# SetupMode-$(the UUID), they are both 5 bytes binary data. The first four
893e0b
# bytes are the attributes associated with the variable and can safely be
893e0b
# ignored, the last bytes are one-byte true-or-false variables. If SecureBoot
893e0b
# is 1 and SetupMode is 0, then secure boot is being enforced.
893e0b
#
893e0b
# Assume efivars is mounted at /sys/firmware/efi/efivars.
893e0b
is_secure_boot_enforced()
893e0b
{
893e0b
    local secure_boot_file setup_mode_file
893e0b
    local secure_boot_byte setup_mode_byte
893e0b
893e0b
    secure_boot_file=$(find /sys/firmware/efi/efivars -name SecureBoot-* 2>/dev/null)
893e0b
    setup_mode_file=$(find /sys/firmware/efi/efivars -name SetupMode-* 2>/dev/null)
893e0b
893e0b
    if [ -f "$secure_boot_file" ] && [ -f "$setup_mode_file" ]; then
893e0b
        secure_boot_byte=$(hexdump -v -e '/1 "%d\ "' $secure_boot_file|cut -d' ' -f 5)
893e0b
        setup_mode_byte=$(hexdump -v -e '/1 "%d\ "' $setup_mode_file|cut -d' ' -f 5)
893e0b
893e0b
        if [ "$secure_boot_byte" = "1" ] && [ "$setup_mode_byte" = "0" ]; then
893e0b
            return 0
893e0b
        fi
893e0b
    fi
893e0b
893e0b
    return 1
893e0b
}
893e0b
893e0b
#
893e0b
# prepare_kexec_args <kexec args>
893e0b
# This function prepares kexec argument.
893e0b
#
893e0b
prepare_kexec_args()
893e0b
{
893e0b
    local kexec_args=$1
893e0b
    local found_elf_args
893e0b
893e0b
    ARCH=`uname -m`
893e0b
    if [ "$ARCH" == "i686" -o "$ARCH" == "i386" ]
893e0b
    then
893e0b
        need_64bit_headers
893e0b
        if [ $? == 1 ]
893e0b
        then
893e0b
            found_elf_args=`echo $kexec_args | grep elf32-core-headers`
893e0b
            if [ -n "$found_elf_args" ]
893e0b
            then
893e0b
                echo -n "Warning: elf32-core-headers overrides correct elf64 setting"
893e0b
                echo
893e0b
            else
893e0b
                kexec_args="$kexec_args --elf64-core-headers"
893e0b
            fi
893e0b
        else
893e0b
            found_elf_args=`echo $kexec_args | grep elf64-core-headers`
893e0b
            if [ -z "$found_elf_args" ]
893e0b
            then
893e0b
                kexec_args="$kexec_args --elf32-core-headers"
893e0b
            fi
893e0b
        fi
893e0b
    fi
893e0b
    echo $kexec_args
893e0b
}
893e0b
893e0b
check_boot_dir()
893e0b
{
893e0b
    local kdump_bootdir=$1
893e0b
    #If user specify a boot dir for kdump kernel, let's use it. Otherwise
893e0b
    #check whether it's a atomic host. If yes parse the subdirectory under
893e0b
    #/boot; If not just find it under /boot.
893e0b
    if [ -n "$kdump_bootdir" ]; then
893e0b
        echo "$kdump_bootdir"
893e0b
        return
893e0b
    fi
893e0b
893e0b
    if ! is_atomic || [ "$(uname -m)" = "s390x" ]; then
893e0b
        kdump_bootdir="/boot"
893e0b
    else
893e0b
        eval $(cat /proc/cmdline| grep "BOOT_IMAGE" | cut -d' ' -f1)
893e0b
        kdump_bootdir="/boot"$(dirname $BOOT_IMAGE)
893e0b
    fi
893e0b
    echo $kdump_bootdir
893e0b
}
893e0b
893e0b
#
893e0b
# prepare_cmdline <commandline> <commandline remove> <commandline append>
893e0b
# This function performs a series of edits on the command line.
893e0b
# Store the final result in global $KDUMP_COMMANDLINE.
893e0b
prepare_cmdline()
893e0b
{
893e0b
    local cmdline id
893e0b
893e0b
    if [ -z "$1" ]; then
893e0b
        cmdline=$(cat /proc/cmdline)
893e0b
    else
893e0b
        cmdline="$1"
893e0b
    fi
893e0b
893e0b
    # These params should always be removed
893e0b
    cmdline=$(remove_cmdline_param "$cmdline" crashkernel panic_on_warn)
893e0b
    # These params can be removed configurably
893e0b
    cmdline=$(remove_cmdline_param "$cmdline" "$2")
893e0b
893e0b
    # Always remove "root=X", as we now explicitly generate all kinds
893e0b
    # of dump target mount information including root fs.
893e0b
    #
893e0b
    # We do this before KDUMP_COMMANDLINE_APPEND, if one really cares
893e0b
    # about it(e.g. for debug purpose), then can pass "root=X" using
893e0b
    # KDUMP_COMMANDLINE_APPEND.
893e0b
    cmdline=$(remove_cmdline_param "$cmdline" root)
893e0b
893e0b
    # With the help of "--hostonly-cmdline", we can avoid some interitage.
893e0b
    cmdline=$(remove_cmdline_param "$cmdline" rd.lvm.lv rd.luks.uuid rd.dm.uuid rd.md.uuid fcoe)
893e0b
893e0b
    # Remove netroot, rd.iscsi.initiator and iscsi_initiator since
893e0b
    # we get duplicate entries for the same in case iscsi code adds
893e0b
    # it as well.
893e0b
    cmdline=$(remove_cmdline_param "$cmdline" netroot rd.iscsi.initiator iscsi_initiator)
893e0b
893e0b
    cmdline="${cmdline} $3"
893e0b
893e0b
    id=$(get_bootcpu_apicid)
893e0b
    if [ ! -z ${id} ] ; then
893e0b
        cmdline=$(append_cmdline "${cmdline}" disable_cpu_apicid ${id})
893e0b
    fi
893e0b
    echo ${cmdline}
893e0b
}