35519c
#!/bin/sh
35519c
#
35519c
# Function and variables used in initramfs environment, POSIX compatible
35519c
#
Petr Šabata f5bf49
35519c
DEFAULT_PATH="/var/crash/"
35519c
KDUMP_CONFIG_FILE="/etc/kdump.conf"
Petr Šabata f5bf49
35519c
# Read kdump config in well formated style
35519c
kdump_read_conf()
35519c
{
35519c
    # Following steps are applied in order: strip trailing comment, strip trailing space,
35519c
    # strip heading space, match non-empty line, remove duplicated spaces between conf name and value
35519c
    [ -f "$KDUMP_CONFIG_FILE" ] && sed -n -e "s/#.*//;s/\s*$//;s/^\s*//;s/\(\S\+\)\s*\(.*\)/\1 \2/p" $KDUMP_CONFIG_FILE
35519c
}
35519c
35519c
# Retrieves config value defined in kdump.conf
35519c
# $1: config name, sed regexp compatible
35519c
kdump_get_conf_val() {
35519c
    # For lines matching "^\s*$1\s+", remove matched part (config name including space),
35519c
    # remove tailing comment, space, then store in hold space. Print out the hold buffer on last line.
35519c
    [ -f "$KDUMP_CONFIG_FILE" ] && \
35519c
        sed -n -e "/^\s*\($1\)\s\+/{s/^\s*\($1\)\s\+//;s/#.*//;s/\s*$//;h};\${x;p}" $KDUMP_CONFIG_FILE
35519c
}
35519c
35519c
is_mounted()
35519c
{
35519c
    findmnt -k -n $1 &>/dev/null
35519c
}
35519c
35519c
get_mount_info()
35519c
{
35519c
    local _info_type=$1 _src_type=$2 _src=$3; shift 3
35519c
    local _info=$(findmnt -k -n -r -o $_info_type --$_src_type $_src $@)
35519c
35519c
    [ -z "$_info" ] && [ -e "/etc/fstab" ] && _info=$(findmnt -s -n -r -o $_info_type --$_src_type $_src $@)
35519c
35519c
    echo $_info
35519c
}
35519c
35519c
is_ipv6_address()
35519c
{
35519c
    echo $1 | grep -q ":"
35519c
}
35519c
35519c
is_fs_type_nfs()
35519c
{
35519c
    [ "$1" = "nfs" ] || [ "$1" = "nfs4" ]
35519c
}
35519c
35519c
# If $1 contains dracut_args "--mount", return <filesystem type>
35519c
get_dracut_args_fstype()
35519c
{
35519c
    echo $1 | grep "\-\-mount" | sed "s/.*--mount .\(.*\)/\1/" | cut -d' ' -f3
35519c
}
35519c
35519c
# If $1 contains dracut_args "--mount", return <device>
35519c
get_dracut_args_target()
35519c
{
35519c
    echo $1 | grep "\-\-mount" | sed "s/.*--mount .\(.*\)/\1/" | cut -d' ' -f1
35519c
}
35519c
35519c
get_save_path()
35519c
{
35519c
    local _save_path=$(kdump_get_conf_val path)
35519c
    [ -z "$_save_path" ] && _save_path=$DEFAULT_PATH
35519c
35519c
    # strip the duplicated "/"
35519c
    echo $_save_path | tr -s /
35519c
}
35519c
35519c
get_root_fs_device()
35519c
{
35519c
    findmnt -k -f -n -o SOURCE /
35519c
}
35519c
35519c
# Return the current underlying device of a path, ignore bind mounts
35519c
get_target_from_path()
35519c
{
35519c
    local _target
35519c
35519c
    _target=$(df $1 2>/dev/null | tail -1 |  awk '{print $1}')
35519c
    [[ "$_target" == "/dev/root" ]] && [[ ! -e /dev/root ]] && _target=$(get_root_fs_device)
35519c
    echo $_target
35519c
}
35519c
35519c
get_fs_type_from_target()
35519c
{
35519c
    get_mount_info FSTYPE source $1 -f
35519c
}
35519c
35519c
get_mntpoint_from_target()
35519c
{
35519c
    # --source is applied to ensure non-bind mount is returned
35519c
    get_mount_info TARGET source $1 -f
35519c
}
35519c
35519c
is_ssh_dump_target()
35519c
{
35519c
    [[ $(kdump_get_conf_val ssh) == *@* ]]
35519c
}
35519c
35519c
is_raw_dump_target()
35519c
{
35519c
    [[ $(kdump_get_conf_val raw) ]]
35519c
}
35519c
35519c
is_nfs_dump_target()
35519c
{
35519c
    if [[ $(kdump_get_conf_val nfs) ]]; then
35519c
        return 0;
35519c
    fi
35519c
35519c
    if is_fs_type_nfs $(get_dracut_args_fstype "$(kdump_get_conf_val dracut_args)"); then
35519c
        return 0
35519c
    fi
35519c
35519c
    local _save_path=$(get_save_path)
35519c
    local _target=$(get_target_from_path $_save_path)
35519c
    local _fstype=$(get_fs_type_from_target $_target)
35519c
35519c
    if is_fs_type_nfs $_fstype; then
35519c
        return 0
35519c
    fi
35519c
35519c
    return 1
35519c
}
35519c
35519c
is_fs_dump_target()
35519c
{
35519c
    [[ $(kdump_get_conf_val "ext[234]\|xfs\|btrfs\|minix") ]]
35519c
}