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() {
8f4abc
    derror "$@"
893e0b
    exit 1
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
603de6
# Read from kdump config file stripping all comments
603de6
read_strip_comments()
603de6
{
603de6
    # strip heading spaces, and print any content starting with
603de6
    # neither space or #, and strip everything after #
603de6
    sed -n -e "s/^\s*\([^# \t][^#]\+\).*/\1/gp" $1
603de6
}
603de6
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
{
f8bec6
    findmnt -k -f -n -o SOURCE /
893e0b
}
893e0b
893e0b
get_save_path()
893e0b
{
f8bec6
    local _save_path=$(awk '$1 == "path" {print $2}' /etc/kdump.conf)
f8bec6
    [ -z "$_save_path" ] && _save_path=$DEFAULT_PATH
893e0b
f8bec6
    # strip the duplicated "/"
f8bec6
    echo $_save_path | tr -s /
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
{
603de6
    grep -E "^(failure_action|default)[[:space:]]dump_to_rootfs" /etc/kdump.conf >/dev/null
893e0b
}
893e0b
603de6
get_failure_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.
603de6
    _root=$(get_failure_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
f8bec6
# Return the bind mount source path, return the path itself if it's not bind mounted
f8bec6
# Eg. if /path/to/src is bind mounted to /mnt/bind, then:
f8bec6
# /mnt/bind -> /path/to/src, /mnt/bind/dump -> /path/to/src/dump
f8bec6
#
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.
f8bec6
#
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 "[]".
f8bec6
get_bind_mount_source()
893e0b
{
f8bec6
    local _path=$1
f8bec6
    # In case it's a sub path in a mount point, get the mount point first
f8bec6
    local _mnt_top=$(df $_path | tail -1 | awk '{print $NF}')
f8bec6
    local _mntpoint=$(findmnt $_mnt_top | tail -n 1 | awk '{print $2}')
f8bec6
    local _mntpoint_nofsroot=$(findmnt -v $_mnt_top | tail -n 1 | awk '{print $2}')
893e0b
f8bec6
    if [[ "$_mntpoint" = $_mntpoint_nofsroot ]]; then
f8bec6
        echo $_path && return
f8bec6
    fi
893e0b
f8bec6
    _mntpoint=${_mntpoint#*$_mntpoint_nofsroot}
893e0b
    _mntpoint=${_mntpoint#[}
893e0b
    _mntpoint=${_mntpoint%]}
f8bec6
    _path=${_path#$_mnt_top}
893e0b
f8bec6
    echo $_mntpoint$_path
893e0b
}
893e0b
f8bec6
# Return the current underlaying device of a path, ignore bind mounts
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
f8bec6
is_mounted()
893e0b
{
f8bec6
    findmnt -k -n $1 &>/dev/null
893e0b
}
893e0b
f8bec6
get_mount_info()
f8bec6
{
f8bec6
    local _info_type=$1 _src_type=$2 _src=$3; shift 3
f8bec6
    local _info=$(findmnt -k -n -r -o $_info_type --$_src_type $_src $@)
f8bec6
f8bec6
    [ -z "$_info" ] && [ -e "/etc/fstab" ] && _info=$(findmnt -s -n -r -o $_info_type --$_src_type $_src $@)
f8bec6
f8bec6
    echo $_info
f8bec6
}
f8bec6
f8bec6
get_fs_type_from_target()
f8bec6
{
f8bec6
    get_mount_info FSTYPE source $1 -f
f8bec6
}
f8bec6
f8bec6
get_mntopt_from_target()
f8bec6
{
f8bec6
    get_mount_info OPTIONS source $1 -f
f8bec6
}
f8bec6
# Find the general mount point of a dump target, not the bind mount point
893e0b
get_mntpoint_from_target()
893e0b
{
f8bec6
    # Expcilitly specify --source to findmnt could ensure non-bind mount is returned
f8bec6
    get_mount_info TARGET source $1 -f
f8bec6
}
f8bec6
f8bec6
# Get the path where the target will be mounted in kdump kernel
f8bec6
# $1: kdump target device
f8bec6
get_kdump_mntpoint_from_target()
f8bec6
{
f8bec6
    local _mntpoint=$(get_mntpoint_from_target $1)
893e0b
f8bec6
    # mount under /sysroot if dump to root disk or mount under
f8bec6
    # mount under /kdumproot if dump target is not mounted in first kernel
f8bec6
    # mount under /kdumproot/$_mntpoint in other cases in 2nd kernel.
f8bec6
    # systemd will be in charge to umount it.
f8bec6
    if [ -z "$_mntpoint" ];then
f8bec6
        _mntpoint="/kdumproot"
893e0b
    else
f8bec6
        if [ "$_mntpoint" = "/" ];then
f8bec6
            _mntpoint="/sysroot"
f8bec6
        else
f8bec6
            _mntpoint="/kdumproot/$_mntpoint"
f8bec6
        fi
893e0b
    fi
f8bec6
f8bec6
    # strip duplicated "/"
f8bec6
    echo $_mntpoint | tr -s "/"
893e0b
}
893e0b
893e0b
# get_option_value <option_name>
893e0b
# retrieves value of option defined in kdump.conf
893e0b
get_option_value() {
f8bec6
    strip_comments `grep "^$1[[:space:]]\+" /etc/kdump.conf | tail -1 | cut -d\  -f2-`
893e0b
}
893e0b
f8bec6
kdump_get_persistent_dev() {
f8bec6
    local dev="${1//\"/}"
893e0b
f8bec6
    case "$dev" in
f8bec6
    UUID=*)
f8bec6
        dev=`blkid -U "${dev#UUID=}"`
f8bec6
        ;;
f8bec6
    LABEL=*)
f8bec6
        dev=`blkid -L "${dev#LABEL=}"`
f8bec6
        ;;
f8bec6
    esac
f8bec6
    echo $(get_persistent_dev "$dev")
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
16c266
is_atomic()
16c266
{
16c266
    grep -q "ostree" /proc/cmdline
16c266
}
16c266
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
8f4abc
# returns 0 when omission of a module is desired in dracut_args
893e0b
# returns 1 otherwise
8f4abc
is_dracut_mod_omitted() {
8f4abc
    local dracut_args dracut_mod=$1
8f4abc
8f4abc
    set -- $(grep  "^dracut_args" /etc/kdump.conf)
8f4abc
    while [ $# -gt 0 ]; do
8f4abc
        case $1 in
8f4abc
            -o|--omit)
8f4abc
                [[ " ${2//[^[:alnum:]]/ } " ==  *" $dracut_mod "* ]] && return 0
8f4abc
        esac
8f4abc
        shift
8f4abc
    done
893e0b
8f4abc
    return 1
8f4abc
}
893e0b
8f4abc
is_wdt_active() {
8f4abc
    local active
893e0b
8f4abc
    [ -d /sys/class/watchdog ] || return 1
8f4abc
    for dir in /sys/class/watchdog/*; do
8f4abc
        [ -f "$dir/state" ] || continue
8f4abc
        active=$(< "$dir/state")
8f4abc
        [ "$active" =  "active" ] && return 0
8f4abc
    done
8f4abc
    return 1
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
8f4abc
        derror "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
8f4abc
        derror "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
8f4abc
        derror "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
8f4abc
    # On powerpc, secure boot is enforced if:
8f4abc
    #   host secure boot: /ibm,secure-boot/os-secureboot-enforcing DT property exists
8f4abc
    #   guest secure boot: /ibm,secure-boot >= 2
f8bec6
    if [ -f /proc/device-tree/ibm,secureboot/os-secureboot-enforcing ]; then
f8bec6
		return 0
f8bec6
    fi
8f4abc
    if [ -f /proc/device-tree/ibm,secure-boot ] && \
8f4abc
       [ $(lsprop /proc/device-tree/ibm,secure-boot | tail -1) -ge 2 ]; then
8f4abc
		return 0
8f4abc
    fi
f8bec6
6bb61d
    # Detect secure boot on x86 and arm64
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
6bb61d
    # Detect secure boot on s390x
6bb61d
    if [[ -e "/sys/firmware/ipl/secure" && "$(cat /sys/firmware/ipl/secure)" == "1" ]]; then
6bb61d
        return 0
6bb61d
    fi
6bb61d
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
8f4abc
                dwarn "Warning: elf32-core-headers overrides correct elf64 setting"
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
16c266
#
16c266
# Detect initrd and kernel location, results are stored in global enviromental variables:
16c266
# KDUMP_BOOTDIR, KDUMP_KERNELVER, KDUMP_KERNEL, DEFAULT_INITRD, and KDUMP_INITRD
16c266
#
16c266
# Expectes KDUMP_BOOTDIR, KDUMP_IMG, KDUMP_IMG_EXT, KDUMP_KERNELVER to be loaded from config already
16c266
# and will prefer already set values so user can specify custom kernel/initramfs location
16c266
#
16c266
prepare_kdump_bootinfo()
893e0b
{
16c266
    local boot_imglist boot_dirlist boot_initrdlist curr_kver="$(uname -r)"
16c266
    local machine_id
16c266
16c266
    if [ -z "$KDUMP_KERNELVER"]; then
16c266
        KDUMP_KERNELVER="$(uname -r)"
893e0b
    fi
893e0b
16c266
    read machine_id < /etc/machine-id
16c266
    boot_dirlist=${KDUMP_BOOTDIR:-"/boot /boot/efi /efi /"}
16c266
    boot_imglist="$KDUMP_IMG-$KDUMP_KERNELVER$KDUMP_IMG_EXT $machine_id/$KDUMP_KERNELVER/$KDUMP_IMG"
16c266
16c266
    # Use BOOT_IMAGE as reference if possible, strip the GRUB root device prefix in (hd0,gpt1) format
16c266
    local boot_img="$(cat /proc/cmdline | sed "s/^BOOT_IMAGE=\((\S*)\)\?\(\S*\) .*/\2/")"
16c266
    if [ -n "$boot_img" ]; then
16c266
        boot_imglist="$boot_img $boot_imglist"
16c266
    fi
16c266
16c266
    for dir in $boot_dirlist; do
16c266
        for img in $boot_imglist; do
16c266
            if [ -f "$dir/$img" ]; then
16c266
                KDUMP_KERNEL=$(echo $dir/$img | tr -s '/')
16c266
                break 2
16c266
            fi
16c266
        done
16c266
    done
16c266
16c266
    if ! [ -e "$KDUMP_KERNEL" ]; then
8f4abc
        derror "Failed to detect kdump kernel location"
16c266
        return 1
16c266
    fi
16c266
16c266
    # Set KDUMP_BOOTDIR to where kernel image is stored
16c266
    KDUMP_BOOTDIR=$(dirname $KDUMP_KERNEL)
16c266
16c266
    # Default initrd should just stay aside of kernel image, try to find it in KDUMP_BOOTDIR
16c266
    boot_initrdlist="initramfs-$KDUMP_KERNELVER.img initrd"
16c266
    for initrd in $boot_initrdlist; do
16c266
        if [ -f "$KDUMP_BOOTDIR/$initrd" ]; then
4efe6f
            defaut_initrd_base="$initrd"
4efe6f
            DEFAULT_INITRD="$KDUMP_BOOTDIR/$defaut_initrd_base"
16c266
            break
16c266
        fi
16c266
    done
16c266
4efe6f
    # Create kdump initrd basename from default initrd basename
16c266
    # initramfs-5.7.9-200.fc32.x86_64.img => initramfs-5.7.9-200.fc32.x86_64kdump.img
16c266
    # initrd => initrdkdump
4efe6f
    if [[ -z "$defaut_initrd_base" ]]; then
4efe6f
        kdump_initrd_base=initramfs-${KDUMP_KERNELVER}kdump.img
4efe6f
    elif [[ $defaut_initrd_base == *.* ]]; then
4efe6f
        kdump_initrd_base=${defaut_initrd_base%.*}kdump.${DEFAULT_INITRD##*.}
893e0b
    else
4efe6f
        kdump_initrd_base=${defaut_initrd_base}kdump
4efe6f
    fi
4efe6f
4efe6f
    # Place kdump initrd in `/var/lib/kdump` if `KDUMP_BOOTDIR` not writable
4efe6f
    if [[ ! -w "$KDUMP_BOOTDIR" ]];then
4efe6f
        var_target_initrd_dir="/var/lib/kdump"
4efe6f
        mkdir -p "$var_target_initrd_dir"
4efe6f
        KDUMP_INITRD="$var_target_initrd_dir/$kdump_initrd_base"
4efe6f
    else
4efe6f
        KDUMP_INITRD="$KDUMP_BOOTDIR/$kdump_initrd_base"
893e0b
    fi
893e0b
}
893e0b
8f4abc
get_watchdog_drvs()
8f4abc
{
8f4abc
    local _wdtdrvs _drv _dir
8f4abc
8f4abc
    for _dir in /sys/class/watchdog/*; do
8f4abc
        # device/modalias will return driver of this device
8f4abc
        [[ -f "$_dir/device/modalias" ]] || continue
8f4abc
        _drv=$(< "$_dir/device/modalias")
8f4abc
        _drv=$(modprobe --set-version "$KDUMP_KERNELVER" -R $_drv 2>/dev/null)
8f4abc
        for i in $_drv; do
8f4abc
            if ! [[ " $_wdtdrvs " == *" $i "* ]]; then
8f4abc
                _wdtdrvs="$_wdtdrvs $i"
8f4abc
            fi
8f4abc
        done
8f4abc
    done
8f4abc
8f4abc
    echo $_wdtdrvs
8f4abc
}
8f4abc
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
f8bec6
8f4abc
    # If any watchdog is used, set it's pretimeout to 0. pretimeout let
8f4abc
    # watchdog panic the kernel first, and reset the system after the
8f4abc
    # panic. If the system is already in kdump, panic is not helpful
8f4abc
    # and only increase the chance of watchdog failure.
8f4abc
    for i in $(get_watchdog_drvs); do
8f4abc
        cmdline+=" $i.pretimeout=0"
8f4abc
8f4abc
        if [[ $i == hpwdt ]]; then
8f4abc
            # hpwdt have a special parameter kdumptimeout, is's only suppose
8f4abc
            # to be set to non-zero in first kernel. In kdump, non-zero
8f4abc
            # value could prevent the watchdog from resetting the system.
8f4abc
            cmdline+=" $i.kdumptimeout=0"
8f4abc
        fi
8f4abc
    done
f8bec6
893e0b
    echo ${cmdline}
893e0b
}