ab224c
#!/bin/sh
ab224c
#
765b01
# Kdump common variables and functions
ab224c
#
ab224c
1b417c
DEFAULT_PATH="/var/crash/"
1b417c
FENCE_KDUMP_CONFIG_FILE="/etc/sysconfig/fence_kdump"
765b01
FENCE_KDUMP_SEND="/usr/libexec/fence_kdump_send"
06c2a2
FADUMP_ENABLED_SYS_NODE="/sys/kernel/fadump_enabled"
06c2a2
06c2a2
is_fadump_capable()
06c2a2
{
06c2a2
    # Check if firmware-assisted dump is enabled
06c2a2
    # if no, fallback to kdump check
06c2a2
    if [ -f $FADUMP_ENABLED_SYS_NODE ]; then
06c2a2
        rc=`cat $FADUMP_ENABLED_SYS_NODE`
06c2a2
        [ $rc -eq 1 ] && return 0
06c2a2
    fi
06c2a2
    return 1
06c2a2
}
765b01
e35838
perror_exit() {
e35838
    echo $@ >&2
e35838
    exit 1
e35838
}
e35838
e35838
perror() {
e35838
    echo $@ >&2
e35838
}
e35838
ab224c
is_ssh_dump_target()
ab224c
{
ab224c
    grep -q "^ssh[[:blank:]].*@" /etc/kdump.conf
ab224c
}
ab224c
ab224c
is_nfs_dump_target()
ab224c
{
e35838
    grep -q "^nfs" /etc/kdump.conf || \
e35838
        [[ $(get_dracut_args_fstype "$(grep "^dracut_args .*\-\-mount" /etc/kdump.conf)") = nfs* ]]
ab224c
}
ab224c
ab224c
is_raw_dump_target()
ab224c
{
ab224c
    grep -q "^raw" /etc/kdump.conf
ab224c
}
ab224c
1b417c
is_fs_type_nfs()
1b417c
{
1b417c
    local _fstype=$1
1b417c
    [ $_fstype = "nfs" ] || [ $_fstype = "nfs4" ] && return 0
1b417c
    return 1
1b417c
}
1b417c
1b417c
is_fs_dump_target()
1b417c
{
1b417c
    egrep -q "^ext[234]|^xfs|^btrfs|^minix" /etc/kdump.conf
1b417c
}
1b417c
ab224c
strip_comments()
ab224c
{
ab224c
    echo $@ | sed -e 's/\(.*\)#.*/\1/'
ab224c
}
765b01
1b417c
# Check if fence kdump is configured in Pacemaker cluster
1b417c
is_pcs_fence_kdump()
765b01
{
765b01
    # no pcs or fence_kdump_send executables installed?
765b01
    type -P pcs > /dev/null || return 1
765b01
    [ -x $FENCE_KDUMP_SEND ] || return 1
765b01
765b01
    # fence kdump not configured?
766e0d
    (pcs cluster cib | grep 'type="fence_kdump"') &> /dev/null || return 1
765b01
}
765b01
1b417c
# Check if fence_kdump is configured using kdump options
1b417c
is_generic_fence_kdump()
1b417c
{
1b417c
    [ -x $FENCE_KDUMP_SEND ] || return 1
1b417c
1b417c
    grep -q "^fence_kdump_nodes" /etc/kdump.conf
1b417c
}
1b417c
e35838
to_dev_name() {
e35838
    local dev="${1//\"/}"
e35838
e35838
    case "$dev" in
e35838
    UUID=*)
e35838
        dev=`blkid -U "${dev#UUID=}"`
e35838
        ;;
e35838
    LABEL=*)
e35838
        dev=`blkid -L "${dev#LABEL=}"`
e35838
        ;;
e35838
    esac
e35838
    echo $dev
e35838
}
e35838
e35838
kdump_get_persistent_dev() {
e35838
    local i _tmp _dev _lookup_dirs
e35838
e35838
    _dev=$(udevadm info --query=name --name="$1" 2>/dev/null)
e35838
    [ -z "$_dev" ] && {
e35838
        perror_exit "Kernel dev name of $1 is not found."
e35838
    }
e35838
e35838
    if [[ $2 = "raw" ]];then
26a7a5
	_lookup_dirs="/dev/mapper/* /dev/disk/by-id/* /dev/disk/by-path/*"
e35838
    else
e35838
	_lookup_dirs="/dev/mapper/* /dev/disk/by-uuid/* /dev/disk/by-id/*"
e35838
    fi
e35838
e35838
    for i in $_lookup_dirs; do
e35838
        _tmp=$(udevadm info --query=name --name="$i" 2>/dev/null)
e35838
        if [ "$_tmp" = "$_dev" ]; then
e35838
            echo $i
e35838
            return
e35838
        fi
e35838
    done
e35838
e35838
    perror "WARNING: Persistent device name of $1 not found. Using $1 as dump target name"
e35838
    echo $1
e35838
}
e35838
06c2a2
is_user_configured_dump_target()
06c2a2
{
06c2a2
    return $(is_mount_in_dracut_args || is_ssh_dump_target || is_nfs_dump_target || \
06c2a2
             is_raw_dump_target || is_fs_dump_target)
06c2a2
}
06c2a2
765b01
get_user_configured_dump_disk()
765b01
{
765b01
    local _target
765b01
765b01
    _target=$(egrep "^ext[234]|^xfs|^btrfs|^minix|^raw" /etc/kdump.conf 2>/dev/null |awk '{print $2}')
06c2a2
    [ -n "$_target" ] && echo $_target && return
765b01
06c2a2
    _target=$(get_dracut_args_target "$(grep "^dracut_args .*\-\-mount" /etc/kdump.conf)")
06c2a2
    [ -b "$_target" ] && echo $_target
765b01
}
765b01
1b417c
get_root_fs_device()
765b01
{
765b01
    local _target
1b417c
    _target=$(findmnt -k -f -n -o SOURCE /)
1b417c
    [ -n "$_target" ] && echo $_target
765b01
1b417c
    return
1b417c
}
765b01
06c2a2
get_save_path()
06c2a2
{
06c2a2
	local _save_path=$(grep "^path" /etc/kdump.conf|awk '{print $2}')
06c2a2
	if [ -z "$_save_path" ]; then
06c2a2
		_save_path=$DEFAULT_PATH
06c2a2
	fi
06c2a2
06c2a2
	echo $_save_path
06c2a2
}
06c2a2
06c2a2
get_block_dump_target()
06c2a2
{
06c2a2
    local _target _path
06c2a2
06c2a2
    if is_ssh_dump_target || is_nfs_dump_target; then
06c2a2
        return
06c2a2
    fi
06c2a2
06c2a2
    _target=$(get_user_configured_dump_disk)
06c2a2
    [ -n "$_target" ] && echo $(to_dev_name $_target) && return
06c2a2
06c2a2
    # Get block device name from local save path
06c2a2
    _path=$(get_save_path)
06c2a2
    _target=$(get_target_from_path $_path)
06c2a2
    [ -b "$_target" ] && echo $(to_dev_name $_target)
06c2a2
}
06c2a2
06c2a2
is_dump_to_rootfs()
06c2a2
{
06c2a2
    grep "^default[[:space:]]dump_to_rootfs" /etc/kdump.conf >/dev/null
06c2a2
}
06c2a2
06c2a2
get_default_action_target()
06c2a2
{
06c2a2
    local _target
06c2a2
06c2a2
    if is_dump_to_rootfs; then
06c2a2
        # Get rootfs device name
06c2a2
        _target=$(get_root_fs_device)
06c2a2
        [ -b "$_target" ] && echo $(to_dev_name $_target) && return
06c2a2
        # Then, must be nfs root
06c2a2
        echo "nfs"
06c2a2
    fi
06c2a2
}
06c2a2
06c2a2
# Get kdump targets(including root in case of dump_to_rootfs).
06c2a2
get_kdump_targets()
06c2a2
{
06c2a2
    local _target _root
06c2a2
    local kdump_targets
06c2a2
06c2a2
    _target=$(get_block_dump_target)
06c2a2
    if [ -n "$_target" ]; then
06c2a2
        kdump_targets=$_target
06c2a2
    elif is_ssh_dump_target; then
06c2a2
        kdump_targets="ssh"
06c2a2
    else
06c2a2
        kdump_targets="nfs"
06c2a2
    fi
06c2a2
06c2a2
    # Add the root device if dump_to_rootfs is specified.
06c2a2
    _root=$(get_default_action_target)
06c2a2
    if [ -n "$_root" -a "$kdump_targets" != "$_root" ]; then
06c2a2
        kdump_targets="$kdump_targets $_root"
06c2a2
    fi
06c2a2
06c2a2
    echo "$kdump_targets"
06c2a2
}
06c2a2
06c2a2
b6bfec
# findmnt uses the option "-v, --nofsroot" to exclusive the [/dir]
b6bfec
# in the SOURCE column for bind-mounts, then if $_mntpoint equals to
b6bfec
# $_mntpoint_nofsroot, the mountpoint is not bind mounted directory.
b6bfec
is_bind_mount()
b6bfec
{
b6bfec
    local _mntpoint=$(findmnt $1 | tail -n 1 | awk '{print $2}')
b6bfec
    local _mntpoint_nofsroot=$(findmnt -v $1 | tail -n 1 | awk '{print $2}')
b6bfec
b6bfec
    if [[ $_mntpoint = $_mntpoint_nofsroot ]]; then
b6bfec
        return 1
b6bfec
    else
b6bfec
        return 0
b6bfec
    fi
b6bfec
}
b6bfec
b6bfec
# Below is just an example for mount info
b6bfec
# /dev/mapper/atomicos-root[/ostree/deploy/rhel-atomic-host/var], if the
b6bfec
# directory is bind mounted. The former part represents the device path, rest
b6bfec
# part is the bind mounted directory which quotes by bracket "[]".
b6bfec
get_bind_mount_directory()
b6bfec
{
b6bfec
    local _mntpoint=$(findmnt $1 | tail -n 1 | awk '{print $2}')
b6bfec
    local _mntpoint_nofsroot=$(findmnt -v $1 | tail -n 1 | awk '{print $2}')
b6bfec
b6bfec
    _mntpoint=${_mntpoint#*$_mntpoint_nofsroot}
b6bfec
b6bfec
    _mntpoint=${_mntpoint#[}
b6bfec
    _mntpoint=${_mntpoint%]}
b6bfec
b6bfec
    echo $_mntpoint
b6bfec
}
b6bfec
1b417c
get_mntpoint_from_path() 
1b417c
{
1b417c
    echo $(df $1 | tail -1 |  awk '{print $NF}')
1b417c
}
765b01
1b417c
get_target_from_path()
1b417c
{
1b417c
    echo $(df $1 | tail -1 |  awk '{print $1}')
765b01
}
765b01
1b417c
get_fs_type_from_target() 
765b01
{
1b417c
    echo $(findmnt -k -f -n -r -o FSTYPE $1)
1b417c
}
765b01
b6bfec
# input: device path
b6bfec
# output: the general mount point
b6bfec
# find the general mount point, not the bind mounted point in atomic
b6bfec
# As general system, Use the previous code
b6bfec
#
b6bfec
# ERROR and EXIT:
b6bfec
# the device can be umounted the general mount point, if one of the mount point is bind mounted
b6bfec
# For example:
b6bfec
# mount /dev/sda /mnt/
b6bfec
# mount -o bind /mnt/var /var
b6bfec
# umount /mnt
1b417c
get_mntpoint_from_target()
1b417c
{
b6bfec
    if is_atomic; then
b6bfec
        for _mnt in $(findmnt -k -n -r -o TARGET $1)
b6bfec
        do
b6bfec
            if ! is_bind_mount $_mnt; then
b6bfec
                echo $_mnt
b6bfec
                return
b6bfec
            fi
b6bfec
        done
b6bfec
b6bfec
        echo "Mount $1 firstly, without the bind mode" >&2
b6bfec
        exit 1
b6bfec
    else
b6bfec
        echo $(findmnt -k -f -n -r -o TARGET $1)
b6bfec
    fi
1b417c
}
1b417c
1b417c
# get_option_value <option_name>
1b417c
# retrieves value of option defined in kdump.conf
1b417c
get_option_value() {
1b417c
    echo $(strip_comments `grep ^$1 /etc/kdump.conf | tail -1 | cut -d\  -f2-`)
1b417c
}
1b417c
1b417c
#This function compose a absolute path with the mount
1b417c
#point and the relative $SAVE_PATH.
1b417c
#target is passed in as argument, could be UUID, LABEL,
1b417c
#block device or even nfs server export of the form of
1b417c
#"my.server.com:/tmp/export"?
1b417c
#And possibly this could be used for both default case
1b417c
#as well as when dump taret is specified. When dump
1b417c
#target is not specified, then $target would be null.
1b417c
make_absolute_save_path()
1b417c
{
1b417c
    local _target=$1
1b417c
    local _mnt
1b417c
1b417c
    [ -n $_target ] && _mnt=$(get_mntpoint_from_target $1)
b6bfec
    _mnt="${_mnt}/$SAVE_PATH"
b6bfec
b6bfec
    # strip the duplicated "/"
b6bfec
    echo "$_mnt" | tr -s /
1b417c
}
1b417c
1b417c
check_save_path_fs()
1b417c
{
1b417c
    local _path=$1
1b417c
1b417c
    if [ ! -d $_path ]; then
1b417c
        perror_exit "Dump path $_path does not exist."
1b417c
    fi
765b01
}
765b01
b6bfec
is_atomic()
b6bfec
{
b6bfec
    grep -q "ostree" /proc/cmdline
b6bfec
}
a6d77e
a6d77e
is_ipv6_address()
a6d77e
{
a6d77e
    echo $1 | grep -q ":"
a6d77e
}
a6d77e
a6d77e
# get ip address or hostname from nfs/ssh config value
a6d77e
get_remote_host()
a6d77e
{
a6d77e
    local _config_val=$1
a6d77e
a6d77e
    # ipv6 address in kdump.conf is around with "[]",
a6d77e
    # factor out the ipv6 address
a6d77e
    _config_val=${_config_val#*@}
a6d77e
    _config_val=${_config_val%:/*}
a6d77e
    _config_val=${_config_val#[}
a6d77e
    _config_val=${_config_val%]}
a6d77e
    echo $_config_val
a6d77e
}
a6d77e
a6d77e
is_hostname()
a6d77e
{
a6d77e
    local _hostname=`echo $1 | grep ":"`
a6d77e
a6d77e
    if [ -n "$_hostname" ]; then
a6d77e
        return 1
a6d77e
    fi
a6d77e
    echo $1 | grep -q "[a-zA-Z]"
a6d77e
}
e35838
e35838
# Copied from "/etc/sysconfig/network-scripts/network-functions"
e35838
get_hwaddr()
e35838
{
e35838
    if [ -f "/sys/class/net/${1}/address" ]; then
e35838
        awk '{ print toupper($0) }' < /sys/class/net/${1}/address
e35838
    elif [ -d "/sys/class/net/${1}" ]; then
e35838
       LC_ALL= LANG= ip -o link show ${1} 2>/dev/null | \
e35838
            awk '{ print toupper(gensub(/.*link\/[^ ]* ([[:alnum:]:]*).*/,
e35838
                                        "\\1", 1)); }'
e35838
    fi
e35838
}
e35838
e35838
get_ifcfg_by_device()
e35838
{
e35838
    grep -E -i -l "^[[:space:]]*DEVICE=\"*${1}\"*[[:space:]]*$" \
e35838
         /etc/sysconfig/network-scripts/ifcfg-* 2>/dev/null | head -1
e35838
}
e35838
e35838
get_ifcfg_by_hwaddr()
e35838
{
e35838
    grep -E -i -l "^[[:space:]]*HWADDR=\"*${1}\"*[[:space:]]*$" \
e35838
         /etc/sysconfig/network-scripts/ifcfg-* 2>/dev/null | head -1
e35838
}
e35838
e35838
get_ifcfg_by_uuid()
e35838
{
e35838
    grep -E -i -l "^[[:space:]]*UUID=\"*${1}\"*[[:space:]]*$" \
e35838
         /etc/sysconfig/network-scripts/ifcfg-* 2>/dev/null | head -1
e35838
}
e35838
e35838
get_ifcfg_by_name()
e35838
{
e35838
    grep -E -i -l "^[[:space:]]*NAME=\"*${1}\"*[[:space:]]*$" \
e35838
         /etc/sysconfig/network-scripts/ifcfg-* 2>/dev/null | head -1
e35838
}
e35838
e35838
is_nm_running()
e35838
{
e35838
    [ "$(LANG=C nmcli -t --fields running general status 2>/dev/null)" = "running" ]
e35838
}
e35838
e35838
is_nm_handling()
e35838
{
e35838
    LANG=C nmcli -t --fields device,state  dev status 2>/dev/null \
e35838
          | grep -q "^\(${1}:connected\)\|\(${1}:connecting.*\)$"
e35838
}
e35838
e35838
# $1: netdev name
e35838
get_ifcfg_nmcli()
e35838
{
e35838
    local nm_uuid nm_name
e35838
    local ifcfg_file
e35838
e35838
    # Get the active nmcli config name of $1
e35838
    if is_nm_running && is_nm_handling "${1}" ; then
e35838
        # The configuration "uuid" and "name" generated by nm is wrote to
e35838
        # the ifcfg file as "UUID=<nm_uuid>" and "NAME=<nm_name>".
e35838
        nm_uuid=$(LANG=C nmcli -t --fields uuid,device c show --active 2>/dev/null \
e35838
                  | grep "${1}" | head -1 | cut -d':' -f1)
e35838
        nm_name=$(LANG=C nmcli -t --fields name,device c show --active 2>/dev/null \
e35838
                  | grep "${1}" | head -1 | cut -d':' -f1)
e35838
        ifcfg_file=$(get_ifcfg_by_uuid "${nm_uuid}")
e35838
        [ -z "${ifcfg_file}" ] && ifcfg_file=$(get_ifcfg_by_name "${nm_name}")
e35838
    fi
e35838
e35838
    echo -n "${ifcfg_file}"
e35838
}
e35838
e35838
# $1: netdev name
e35838
get_ifcfg_legacy()
e35838
{
e35838
    local ifcfg_file
e35838
e35838
    ifcfg_file="/etc/sysconfig/network-scripts/ifcfg-${1}"
e35838
    [ -f "${ifcfg_file}" ] && echo -n "${ifcfg_file}" && return
e35838
e35838
    ifcfg_file=$(get_ifcfg_by_name "${1}")
e35838
    [ -f "${ifcfg_file}" ] && echo -n "${ifcfg_file}" && return
e35838
e35838
    local hwaddr=$(get_hwaddr "${1}")
e35838
    if [ -n "$hwaddr" ]; then
e35838
        ifcfg_file=$(get_ifcfg_by_hwaddr "${hwaddr}")
e35838
        [ -f "${ifcfg_file}" ] && echo -n "${ifcfg_file}" && return
e35838
    fi
e35838
e35838
    ifcfg_file=$(get_ifcfg_by_device "${1}")
e35838
e35838
    echo -n "${ifcfg_file}"
e35838
}
e35838
e35838
# $1: netdev name
e35838
# Return the ifcfg file whole name(including the path) of $1 if any.
e35838
get_ifcfg_filename() {
e35838
    local ifcfg_file
e35838
e35838
    ifcfg_file=$(get_ifcfg_nmcli "${1}")
e35838
    if [ -z "${ifcfg_file}" ]; then
e35838
        ifcfg_file=$(get_ifcfg_legacy "${1}")
e35838
    fi
e35838
e35838
    echo -n "${ifcfg_file}"
e35838
}
e35838
e35838
# returns 0 when omission of watchdog module is desired in dracut_args
e35838
# returns 1 otherwise
e35838
is_wdt_mod_omitted() {
e35838
	local dracut_args
e35838
	local ret=1
e35838
e35838
	dracut_args=$(grep  "^dracut_args" /etc/kdump.conf)
e35838
	[[ -z $dracut_args ]] && return $ret
e35838
e35838
	eval set -- $dracut_args
e35838
	while :; do
e35838
		[[ -z $1 ]] && break
e35838
		case $1 in
e35838
			-o|--omit)
e35838
				echo $2 | grep -qw "watchdog"
e35838
				[[ $? == 0 ]] && ret=0
e35838
				break
e35838
		esac
e35838
		shift
e35838
	done
e35838
e35838
	return $ret
e35838
}
e35838
e35838
# If "dracut_args" contains "--mount" information, use it
e35838
# directly without any check(users are expected to ensure
e35838
# its correctness).
e35838
is_mount_in_dracut_args()
e35838
{
e35838
    grep -q "^dracut_args .*\-\-mount" /etc/kdump.conf
e35838
}
e35838
e35838
# If $1 contains dracut_args "--mount", return <filesystem type>
e35838
get_dracut_args_fstype()
e35838
{
e35838
    echo $1 | grep "\-\-mount" | sed "s/.*--mount .\(.*\)/\1/" | cut -d' ' -f3
e35838
}
e35838
e35838
# If $1 contains dracut_args "--mount", return <device>
e35838
get_dracut_args_target()
e35838
{
e35838
    echo $1 | grep "\-\-mount" | sed "s/.*--mount .\(.*\)/\1/" | cut -d' ' -f1
e35838
}
26a7a5
26a7a5
# Get currently loaded modules
26a7a5
# sorted, and delimited by newline
26a7a5
get_loaded_kernel_modules()
26a7a5
{
26a7a5
    local modules=( )
26a7a5
    while read _module _size _used _used_by; do
26a7a5
        modules+=( "$_module" )
26a7a5
    done <<< "$(lsmod | sed -n '1!p')"
26a7a5
    printf '%s\n' "${modules[@]}" | sort
26a7a5
}