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