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"
765b01
ab224c
is_ssh_dump_target()
ab224c
{
ab224c
    grep -q "^ssh[[:blank:]].*@" /etc/kdump.conf
ab224c
}
ab224c
ab224c
is_nfs_dump_target()
ab224c
{
ab224c
    grep -q "^nfs" /etc/kdump.conf
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
1b417c
is_user_configured_dump_target()
1b417c
{
1b417c
    return $(is_ssh_dump_target || is_nfs_dump_target || is_raw_dump_target || is_fs_dump_target)
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?
765b01
    (pcs cluster cib | grep -q '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
765b01
get_user_configured_dump_disk()
765b01
{
765b01
    local _target
765b01
765b01
    if is_ssh_dump_target || is_nfs_dump_target; then
765b01
        return
765b01
    fi
765b01
765b01
    _target=$(egrep "^ext[234]|^xfs|^btrfs|^minix|^raw" /etc/kdump.conf 2>/dev/null |awk '{print $2}')
765b01
    [ -n "$_target" ] && echo $_target
765b01
765b01
    return
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
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
}