Petr Šabata f5bf49
#!/bin/sh
Petr Šabata f5bf49
#
Petr Šabata f5bf49
# Kdump common variables and functions
Petr Šabata f5bf49
#
Petr Šabata f5bf49
Petr Šabata f5bf49
DEFAULT_PATH="/var/crash/"
Petr Šabata f5bf49
FENCE_KDUMP_CONFIG_FILE="/etc/sysconfig/fence_kdump"
Petr Šabata f5bf49
FENCE_KDUMP_SEND="/usr/libexec/fence_kdump_send"
Petr Šabata f5bf49
FADUMP_ENABLED_SYS_NODE="/sys/kernel/fadump_enabled"
Petr Šabata f5bf49
Petr Šabata f5bf49
is_fadump_capable()
Petr Šabata f5bf49
{
Petr Šabata f5bf49
    # Check if firmware-assisted dump is enabled
Petr Šabata f5bf49
    # if no, fallback to kdump check
Petr Šabata f5bf49
    if [ -f $FADUMP_ENABLED_SYS_NODE ]; then
Petr Šabata f5bf49
        rc=`cat $FADUMP_ENABLED_SYS_NODE`
Petr Šabata f5bf49
        [ $rc -eq 1 ] && return 0
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
    return 1
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
perror_exit() {
DistroBaker 5cac7c
    derror "$@"
Petr Šabata f5bf49
    exit 1
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
is_fs_type_nfs()
Petr Šabata f5bf49
{
Petr Šabata f5bf49
    [ "$1" = "nfs" ] || [ "$1" = "nfs4" ]
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
is_ssh_dump_target()
Petr Šabata f5bf49
{
Petr Šabata f5bf49
    grep -q "^ssh[[:blank:]].*@" /etc/kdump.conf
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
is_nfs_dump_target()
Petr Šabata f5bf49
{
Petr Šabata f5bf49
    if grep -q "^nfs" /etc/kdump.conf; then
Petr Šabata f5bf49
        return 0;
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
Petr Šabata f5bf49
    if is_fs_type_nfs $(get_dracut_args_fstype "$(grep "^dracut_args .*\-\-mount" /etc/kdump.conf)"); then
Petr Šabata f5bf49
        return 0
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
Petr Šabata f5bf49
    local _save_path=$(get_save_path)
Petr Šabata f5bf49
    local _target=$(get_target_from_path $_save_path)
Petr Šabata f5bf49
    local _fstype=$(get_fs_type_from_target $_target)
Petr Šabata f5bf49
Petr Šabata f5bf49
    if is_fs_type_nfs $_fstype; then
Petr Šabata f5bf49
        return 0
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
Petr Šabata f5bf49
    return 1
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
is_raw_dump_target()
Petr Šabata f5bf49
{
Petr Šabata f5bf49
    grep -q "^raw" /etc/kdump.conf
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
is_fs_dump_target()
Petr Šabata f5bf49
{
Petr Šabata f5bf49
    egrep -q "^ext[234]|^xfs|^btrfs|^minix" /etc/kdump.conf
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
strip_comments()
Petr Šabata f5bf49
{
Petr Šabata f5bf49
    echo $@ | sed -e 's/\(.*\)#.*/\1/'
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
# Read from kdump config file stripping all comments
Petr Šabata f5bf49
read_strip_comments()
Petr Šabata f5bf49
{
Petr Šabata f5bf49
    # strip heading spaces, and print any content starting with
Petr Šabata f5bf49
    # neither space or #, and strip everything after #
Petr Šabata f5bf49
    sed -n -e "s/^\s*\([^# \t][^#]\+\).*/\1/gp" $1
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
# Check if fence kdump is configured in Pacemaker cluster
Petr Šabata f5bf49
is_pcs_fence_kdump()
Petr Šabata f5bf49
{
Petr Šabata f5bf49
    # no pcs or fence_kdump_send executables installed?
Petr Šabata f5bf49
    type -P pcs > /dev/null || return 1
Petr Šabata f5bf49
    [ -x $FENCE_KDUMP_SEND ] || return 1
Petr Šabata f5bf49
Petr Šabata f5bf49
    # fence kdump not configured?
Petr Šabata f5bf49
    (pcs cluster cib | grep 'type="fence_kdump"') &> /dev/null || return 1
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
# Check if fence_kdump is configured using kdump options
Petr Šabata f5bf49
is_generic_fence_kdump()
Petr Šabata f5bf49
{
Petr Šabata f5bf49
    [ -x $FENCE_KDUMP_SEND ] || return 1
Petr Šabata f5bf49
Petr Šabata f5bf49
    grep -q "^fence_kdump_nodes" /etc/kdump.conf
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
to_dev_name() {
Petr Šabata f5bf49
    local dev="${1//\"/}"
Petr Šabata f5bf49
Petr Šabata f5bf49
    case "$dev" in
Petr Šabata f5bf49
    UUID=*)
Petr Šabata f5bf49
        dev=`blkid -U "${dev#UUID=}"`
Petr Šabata f5bf49
        ;;
Petr Šabata f5bf49
    LABEL=*)
Petr Šabata f5bf49
        dev=`blkid -L "${dev#LABEL=}"`
Petr Šabata f5bf49
        ;;
Petr Šabata f5bf49
    esac
Petr Šabata f5bf49
    echo $dev
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
is_user_configured_dump_target()
Petr Šabata f5bf49
{
Petr Šabata f5bf49
    grep -E -q "^ext[234]|^xfs|^btrfs|^minix|^raw|^nfs|^ssh" /etc/kdump.conf || is_mount_in_dracut_args;
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
get_user_configured_dump_disk()
Petr Šabata f5bf49
{
Petr Šabata f5bf49
    local _target
Petr Šabata f5bf49
Petr Šabata f5bf49
    _target=$(egrep "^ext[234]|^xfs|^btrfs|^minix|^raw" /etc/kdump.conf 2>/dev/null |awk '{print $2}')
Petr Šabata f5bf49
    [ -n "$_target" ] && echo $_target && return
Petr Šabata f5bf49
Petr Šabata f5bf49
    _target=$(get_dracut_args_target "$(grep "^dracut_args .*\-\-mount" /etc/kdump.conf)")
Petr Šabata f5bf49
    [ -b "$_target" ] && echo $_target
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
get_root_fs_device()
Petr Šabata f5bf49
{
Petr Šabata f5bf49
    findmnt -k -f -n -o SOURCE /
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
get_save_path()
Petr Šabata f5bf49
{
Petr Šabata f5bf49
    local _save_path=$(awk '$1 == "path" {print $2}' /etc/kdump.conf)
Petr Šabata f5bf49
    [ -z "$_save_path" ] && _save_path=$DEFAULT_PATH
Petr Šabata f5bf49
Petr Šabata f5bf49
    # strip the duplicated "/"
Petr Šabata f5bf49
    echo $_save_path | tr -s /
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
get_block_dump_target()
Petr Šabata f5bf49
{
Petr Šabata f5bf49
    local _target _path
Petr Šabata f5bf49
Petr Šabata f5bf49
    if is_ssh_dump_target || is_nfs_dump_target; then
Petr Šabata f5bf49
        return
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
Petr Šabata f5bf49
    _target=$(get_user_configured_dump_disk)
Petr Šabata f5bf49
    [ -n "$_target" ] && echo $(to_dev_name $_target) && return
Petr Šabata f5bf49
Petr Šabata f5bf49
    # Get block device name from local save path
Petr Šabata f5bf49
    _path=$(get_save_path)
Petr Šabata f5bf49
    _target=$(get_target_from_path $_path)
Petr Šabata f5bf49
    [ -b "$_target" ] && echo $(to_dev_name $_target)
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
is_dump_to_rootfs()
Petr Šabata f5bf49
{
Petr Šabata f5bf49
    grep -E "^(failure_action|default)[[:space:]]dump_to_rootfs" /etc/kdump.conf >/dev/null
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
get_failure_action_target()
Petr Šabata f5bf49
{
Petr Šabata f5bf49
    local _target
Petr Šabata f5bf49
Petr Šabata f5bf49
    if is_dump_to_rootfs; then
Petr Šabata f5bf49
        # Get rootfs device name
Petr Šabata f5bf49
        _target=$(get_root_fs_device)
Petr Šabata f5bf49
        [ -b "$_target" ] && echo $(to_dev_name $_target) && return
Petr Šabata f5bf49
        # Then, must be nfs root
Petr Šabata f5bf49
        echo "nfs"
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
# Get kdump targets(including root in case of dump_to_rootfs).
Petr Šabata f5bf49
get_kdump_targets()
Petr Šabata f5bf49
{
Petr Šabata f5bf49
    local _target _root
Petr Šabata f5bf49
    local kdump_targets
Petr Šabata f5bf49
Petr Šabata f5bf49
    _target=$(get_block_dump_target)
Petr Šabata f5bf49
    if [ -n "$_target" ]; then
Petr Šabata f5bf49
        kdump_targets=$_target
Petr Šabata f5bf49
    elif is_ssh_dump_target; then
Petr Šabata f5bf49
        kdump_targets="ssh"
Petr Šabata f5bf49
    else
Petr Šabata f5bf49
        kdump_targets="nfs"
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
Petr Šabata f5bf49
    # Add the root device if dump_to_rootfs is specified.
Petr Šabata f5bf49
    _root=$(get_failure_action_target)
Petr Šabata f5bf49
    if [ -n "$_root" -a "$kdump_targets" != "$_root" ]; then
Petr Šabata f5bf49
        kdump_targets="$kdump_targets $_root"
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
Petr Šabata f5bf49
    echo "$kdump_targets"
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
# Return the bind mount source path, return the path itself if it's not bind mounted
Petr Šabata f5bf49
# Eg. if /path/to/src is bind mounted to /mnt/bind, then:
Petr Šabata f5bf49
# /mnt/bind -> /path/to/src, /mnt/bind/dump -> /path/to/src/dump
Petr Šabata f5bf49
#
Petr Šabata f5bf49
# findmnt uses the option "-v, --nofsroot" to exclusive the [/dir]
DistroBaker a10140
# in the SOURCE column for bind-mounts, then if $_src equals to
DistroBaker a10140
# $_src_nofsroot, the mountpoint is not bind mounted directory.
Petr Šabata f5bf49
#
Petr Šabata f5bf49
# Below is just an example for mount info
Petr Šabata f5bf49
# /dev/mapper/atomicos-root[/ostree/deploy/rhel-atomic-host/var], if the
Petr Šabata f5bf49
# directory is bind mounted. The former part represents the device path, rest
Petr Šabata f5bf49
# part is the bind mounted directory which quotes by bracket "[]".
Petr Šabata f5bf49
get_bind_mount_source()
Petr Šabata f5bf49
{
DistroBaker a10140
    local _mnt=$(df $1 | tail -1 | awk '{print $NF}')
DistroBaker a10140
    local _path=${1#$_mnt}
Petr Šabata f5bf49
DistroBaker a10140
    local _src=$(get_mount_info SOURCE target $_mnt -f)
DistroBaker a10140
    local _opt=$(get_mount_info OPTIONS target $_mnt -f)
DistroBaker a10140
    local _fstype=$(get_mount_info FSTYPE target $_mnt -f)
DistroBaker a10140
DistroBaker a10140
    # bind mount in fstab
DistroBaker a10140
    if [[ -d "$_src" ]] && [[ "$_fstype" = none ]] && (echo "$_opt" | grep -q "\bbind\b"); then
DistroBaker a10140
        echo $_src$_path && return
DistroBaker a10140
    fi
DistroBaker a10140
DistroBaker a10140
    # direct mount
DistroBaker a10140
    local _src_nofsroot=$(get_mount_info SOURCE target $_mnt -v -f)
DistroBaker a10140
    if [[ $_src_nofsroot = $_src ]]; then
DistroBaker a10140
        echo $_mnt$_path && return
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
DistroBaker a10140
    local _fsroot=${_src#$_src_nofsroot[}
DistroBaker a10140
    _fsroot=${_fsroot%]}
DistroBaker a10140
    _mnt=$(get_mount_info TARGET source $_src_nofsroot -f)
Petr Šabata f5bf49
DistroBaker a10140
    # for btrfs, _fsroot will also contain the subvol value as well, strip it
DistroBaker a10140
    if [[ "$_fstype" = btrfs ]]; then
DistroBaker a10140
        local _subvol
DistroBaker a10140
        _subvol=${_opt#*subvol=}
DistroBaker a10140
        _subvol=${_subvol%,*}
DistroBaker a10140
        _fsroot=${_fsroot#$_subvol}
DistroBaker a10140
    fi
DistroBaker a10140
    echo $_mnt$_fsroot$_path
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
# Return the current underlaying device of a path, ignore bind mounts
Petr Šabata f5bf49
get_target_from_path()
Petr Šabata f5bf49
{
Petr Šabata f5bf49
    local _target
Petr Šabata f5bf49
Petr Šabata f5bf49
    _target=$(df $1 2>/dev/null | tail -1 |  awk '{print $1}')
Petr Šabata f5bf49
    [[ "$_target" == "/dev/root" ]] && [[ ! -e /dev/root ]] && _target=$(get_root_fs_device)
Petr Šabata f5bf49
    echo $_target
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
is_mounted()
Petr Šabata f5bf49
{
Petr Šabata f5bf49
    findmnt -k -n $1 &>/dev/null
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
get_mount_info()
Petr Šabata f5bf49
{
Petr Šabata f5bf49
    local _info_type=$1 _src_type=$2 _src=$3; shift 3
DistroBaker a10140
    local _info=$(findmnt -k -n -r -o $_info_type --$_src_type $_src $@)
Petr Šabata f5bf49
DistroBaker a10140
    [ -z "$_info" ] && [ -e "/etc/fstab" ] && _info=$(findmnt -s -n -r -o $_info_type --$_src_type $_src $@)
Petr Šabata f5bf49
Petr Šabata f5bf49
    echo $_info
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
get_fs_type_from_target()
Petr Šabata f5bf49
{
Petr Šabata f5bf49
    get_mount_info FSTYPE source $1 -f
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
get_mntopt_from_target()
Petr Šabata f5bf49
{
Petr Šabata f5bf49
    get_mount_info OPTIONS source $1 -f
Petr Šabata f5bf49
}
Petr Šabata f5bf49
# Find the general mount point of a dump target, not the bind mount point
Petr Šabata f5bf49
get_mntpoint_from_target()
Petr Šabata f5bf49
{
Petr Šabata f5bf49
    # Expcilitly specify --source to findmnt could ensure non-bind mount is returned
Petr Šabata f5bf49
    get_mount_info TARGET source $1 -f
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
# Get the path where the target will be mounted in kdump kernel
Petr Šabata f5bf49
# $1: kdump target device
Petr Šabata f5bf49
get_kdump_mntpoint_from_target()
Petr Šabata f5bf49
{
Petr Šabata f5bf49
    local _mntpoint=$(get_mntpoint_from_target $1)
Petr Šabata f5bf49
Petr Šabata f5bf49
    # mount under /sysroot if dump to root disk or mount under
Petr Šabata f5bf49
    # mount under /kdumproot if dump target is not mounted in first kernel
Petr Šabata f5bf49
    # mount under /kdumproot/$_mntpoint in other cases in 2nd kernel.
Petr Šabata f5bf49
    # systemd will be in charge to umount it.
Petr Šabata f5bf49
    if [ -z "$_mntpoint" ];then
Petr Šabata f5bf49
        _mntpoint="/kdumproot"
Petr Šabata f5bf49
    else
Petr Šabata f5bf49
        if [ "$_mntpoint" = "/" ];then
Petr Šabata f5bf49
            _mntpoint="/sysroot"
Petr Šabata f5bf49
        else
Petr Šabata f5bf49
            _mntpoint="/kdumproot/$_mntpoint"
Petr Šabata f5bf49
        fi
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
Petr Šabata f5bf49
    # strip duplicated "/"
Petr Šabata f5bf49
    echo $_mntpoint | tr -s "/"
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
# get_option_value <option_name>
Petr Šabata f5bf49
# retrieves value of option defined in kdump.conf
Petr Šabata f5bf49
get_option_value() {
Petr Šabata f5bf49
    strip_comments `grep "^$1[[:space:]]\+" /etc/kdump.conf | tail -1 | cut -d\  -f2-`
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
kdump_get_persistent_dev() {
Petr Šabata f5bf49
    local dev="${1//\"/}"
Petr Šabata f5bf49
Petr Šabata f5bf49
    case "$dev" in
Petr Šabata f5bf49
    UUID=*)
Petr Šabata f5bf49
        dev=`blkid -U "${dev#UUID=}"`
Petr Šabata f5bf49
        ;;
Petr Šabata f5bf49
    LABEL=*)
Petr Šabata f5bf49
        dev=`blkid -L "${dev#LABEL=}"`
Petr Šabata f5bf49
        ;;
Petr Šabata f5bf49
    esac
Petr Šabata f5bf49
    echo $(get_persistent_dev "$dev")
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Kairui Song 80c191
is_atomic()
Kairui Song 80c191
{
Kairui Song 80c191
    grep -q "ostree" /proc/cmdline
Kairui Song 80c191
}
Kairui Song 80c191
Petr Šabata f5bf49
is_ipv6_address()
Petr Šabata f5bf49
{
Petr Šabata f5bf49
    echo $1 | grep -q ":"
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
# get ip address or hostname from nfs/ssh config value
Petr Šabata f5bf49
get_remote_host()
Petr Šabata f5bf49
{
Petr Šabata f5bf49
    local _config_val=$1
Petr Šabata f5bf49
Petr Šabata f5bf49
    # ipv6 address in kdump.conf is around with "[]",
Petr Šabata f5bf49
    # factor out the ipv6 address
Petr Šabata f5bf49
    _config_val=${_config_val#*@}
Petr Šabata f5bf49
    _config_val=${_config_val%:/*}
Petr Šabata f5bf49
    _config_val=${_config_val#[}
Petr Šabata f5bf49
    _config_val=${_config_val%]}
Petr Šabata f5bf49
    echo $_config_val
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
is_hostname()
Petr Šabata f5bf49
{
Petr Šabata f5bf49
    local _hostname=`echo $1 | grep ":"`
Petr Šabata f5bf49
Petr Šabata f5bf49
    if [ -n "$_hostname" ]; then
Petr Šabata f5bf49
        return 1
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
    echo $1 | grep -q "[a-zA-Z]"
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
# Copied from "/etc/sysconfig/network-scripts/network-functions"
Petr Šabata f5bf49
get_hwaddr()
Petr Šabata f5bf49
{
Petr Šabata f5bf49
    if [ -f "/sys/class/net/${1}/address" ]; then
Petr Šabata f5bf49
        awk '{ print toupper($0) }' < /sys/class/net/${1}/address
Petr Šabata f5bf49
    elif [ -d "/sys/class/net/${1}" ]; then
Petr Šabata f5bf49
       LC_ALL= LANG= ip -o link show ${1} 2>/dev/null | \
Petr Šabata f5bf49
            awk '{ print toupper(gensub(/.*link\/[^ ]* ([[:alnum:]:]*).*/,
Petr Šabata f5bf49
                                        "\\1", 1)); }'
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Coiby Xu d09dc2
Coiby Xu d09dc2
# Get value by a field using "nmcli -g"
Coiby Xu d09dc2
#
Coiby Xu d09dc2
# "nmcli --get-values" allows us to retrive value(s) by field, for example,
Coiby Xu d09dc2
# nmcli --get-values <field> connection show /org/freedesktop/NetworkManager/ActiveConnection/1
Coiby Xu d09dc2
# returns the following value for the corresponding field respectively,
Coiby Xu d09dc2
#   Field                                  Value
Coiby Xu d09dc2
#   IP4.DNS                                "10.19.42.41 | 10.11.5.19 | 10.5.30.160"
Coiby Xu d09dc2
#   802-3-ethernet.s390-subchannels        ""
Coiby Xu d09dc2
#   bond.options                           "mode=balance-rr"
Coiby Xu d09dc2
get_nmcli_value_by_field()
Coiby Xu d09dc2
{
Coiby Xu d09dc2
    local _nm_show_cmd=$1
Coiby Xu d09dc2
    local _field=$2
Coiby Xu d09dc2
Coiby Xu d09dc2
    local val=$(LANG=C nmcli --get-values $_field $_nm_show_cmd)
Coiby Xu d09dc2
Coiby Xu d09dc2
    echo -n "$val"
Coiby Xu d09dc2
}
Coiby Xu d09dc2
Coiby Xu 2f9fc8
# Get nmcli connection apath (a D-Bus active connection path ) by ifname
Coiby Xu 2f9fc8
#
Coiby Xu 2f9fc8
# apath is used for nmcli connection operations, e.g.
Coiby Xu 2f9fc8
#  $ nmcli connection show $apath
Coiby Xu 2f9fc8
get_nmcli_connection_apath_by_ifname()
Coiby Xu 2f9fc8
{
Coiby Xu 2f9fc8
    local _ifname=$1
Coiby Xu 2f9fc8
    local _nm_show_cmd="device show $_ifname"
Coiby Xu 2f9fc8
Coiby Xu 2f9fc8
    local _apath=$(get_nmcli_value_by_field "$_nm_show_cmd" "GENERAL.CON-PATH")
Coiby Xu 2f9fc8
Coiby Xu 2f9fc8
    echo -n "$_apath"
Coiby Xu 2f9fc8
}
Coiby Xu 2f9fc8
Coiby Xu b15a09
# Get nmcli connection show cmd by ifname
Coiby Xu b15a09
#
Coiby Xu b15a09
# "$_apath" is supposed to not contain any chracter that
Coiby Xu b15a09
# need to be escapded, e.g. space. Otherwise get_nmcli_value_by_field
Coiby Xu b15a09
# would fail.
Coiby Xu b15a09
get_nmcli_connection_show_cmd_by_ifname()
Coiby Xu b15a09
{
Coiby Xu b15a09
    local _ifname="$1"
Coiby Xu b15a09
    local _apath=$(get_nmcli_connection_apath_by_ifname "$_ifname")
Coiby Xu b15a09
    local _nm_show_cmd="connection show $_apath"
Coiby Xu b15a09
Coiby Xu b15a09
    echo -n "$_nm_show_cmd"
Coiby Xu b15a09
}
Coiby Xu b15a09
Petr Šabata f5bf49
get_ifcfg_by_device()
Petr Šabata f5bf49
{
Petr Šabata f5bf49
    grep -E -i -l "^[[:space:]]*DEVICE=\"*${1}\"*[[:space:]]*$" \
Petr Šabata f5bf49
         /etc/sysconfig/network-scripts/ifcfg-* 2>/dev/null | head -1
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
get_ifcfg_by_hwaddr()
Petr Šabata f5bf49
{
Petr Šabata f5bf49
    grep -E -i -l "^[[:space:]]*HWADDR=\"*${1}\"*[[:space:]]*$" \
Petr Šabata f5bf49
         /etc/sysconfig/network-scripts/ifcfg-* 2>/dev/null | head -1
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
get_ifcfg_by_uuid()
Petr Šabata f5bf49
{
Petr Šabata f5bf49
    grep -E -i -l "^[[:space:]]*UUID=\"*${1}\"*[[:space:]]*$" \
Petr Šabata f5bf49
         /etc/sysconfig/network-scripts/ifcfg-* 2>/dev/null | head -1
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
get_ifcfg_by_name()
Petr Šabata f5bf49
{
Petr Šabata f5bf49
    grep -E -i -l "^[[:space:]]*NAME=\"*${1}\"*[[:space:]]*$" \
Petr Šabata f5bf49
         /etc/sysconfig/network-scripts/ifcfg-* 2>/dev/null | head -1
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
is_nm_running()
Petr Šabata f5bf49
{
Petr Šabata f5bf49
    [ "$(LANG=C nmcli -t --fields running general status 2>/dev/null)" = "running" ]
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
is_nm_handling()
Petr Šabata f5bf49
{
Petr Šabata f5bf49
    LANG=C nmcli -t --fields device,state  dev status 2>/dev/null \
Petr Šabata f5bf49
          | grep -q "^\(${1}:connected\)\|\(${1}:connecting.*\)$"
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
# $1: netdev name
Petr Šabata f5bf49
get_ifcfg_nmcli()
Petr Šabata f5bf49
{
Petr Šabata f5bf49
    local nm_uuid nm_name
Petr Šabata f5bf49
    local ifcfg_file
Petr Šabata f5bf49
Petr Šabata f5bf49
    # Get the active nmcli config name of $1
Petr Šabata f5bf49
    if is_nm_running && is_nm_handling "${1}" ; then
Petr Šabata f5bf49
        # The configuration "uuid" and "name" generated by nm is wrote to
Petr Šabata f5bf49
        # the ifcfg file as "UUID=<nm_uuid>" and "NAME=<nm_name>".
Petr Šabata f5bf49
        nm_uuid=$(LANG=C nmcli -t --fields uuid,device c show --active 2>/dev/null \
Petr Šabata f5bf49
                  | grep "${1}" | head -1 | cut -d':' -f1)
Petr Šabata f5bf49
        nm_name=$(LANG=C nmcli -t --fields name,device c show --active 2>/dev/null \
Petr Šabata f5bf49
                  | grep "${1}" | head -1 | cut -d':' -f1)
Petr Šabata f5bf49
        ifcfg_file=$(get_ifcfg_by_uuid "${nm_uuid}")
Petr Šabata f5bf49
        [ -z "${ifcfg_file}" ] && ifcfg_file=$(get_ifcfg_by_name "${nm_name}")
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
Petr Šabata f5bf49
    echo -n "${ifcfg_file}"
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
# $1: netdev name
Petr Šabata f5bf49
get_ifcfg_legacy()
Petr Šabata f5bf49
{
Petr Šabata f5bf49
    local ifcfg_file
Petr Šabata f5bf49
Petr Šabata f5bf49
    ifcfg_file="/etc/sysconfig/network-scripts/ifcfg-${1}"
Petr Šabata f5bf49
    [ -f "${ifcfg_file}" ] && echo -n "${ifcfg_file}" && return
Petr Šabata f5bf49
Petr Šabata f5bf49
    ifcfg_file=$(get_ifcfg_by_name "${1}")
Petr Šabata f5bf49
    [ -f "${ifcfg_file}" ] && echo -n "${ifcfg_file}" && return
Petr Šabata f5bf49
Petr Šabata f5bf49
    local hwaddr=$(get_hwaddr "${1}")
Petr Šabata f5bf49
    if [ -n "$hwaddr" ]; then
Petr Šabata f5bf49
        ifcfg_file=$(get_ifcfg_by_hwaddr "${hwaddr}")
Petr Šabata f5bf49
        [ -f "${ifcfg_file}" ] && echo -n "${ifcfg_file}" && return
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
Petr Šabata f5bf49
    ifcfg_file=$(get_ifcfg_by_device "${1}")
Petr Šabata f5bf49
Petr Šabata f5bf49
    echo -n "${ifcfg_file}"
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
# $1: netdev name
Petr Šabata f5bf49
# Return the ifcfg file whole name(including the path) of $1 if any.
Petr Šabata f5bf49
get_ifcfg_filename() {
Petr Šabata f5bf49
    local ifcfg_file
Petr Šabata f5bf49
Petr Šabata f5bf49
    ifcfg_file=$(get_ifcfg_nmcli "${1}")
Petr Šabata f5bf49
    if [ -z "${ifcfg_file}" ]; then
Petr Šabata f5bf49
        ifcfg_file=$(get_ifcfg_legacy "${1}")
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
Petr Šabata f5bf49
    echo -n "${ifcfg_file}"
Petr Šabata f5bf49
}
Petr Šabata f5bf49
DistroBaker a10140
# returns 0 when omission of a module is desired in dracut_args
Petr Šabata f5bf49
# returns 1 otherwise
DistroBaker a10140
is_dracut_mod_omitted() {
DistroBaker a10140
    local dracut_args dracut_mod=$1
DistroBaker a10140
DistroBaker a10140
    set -- $(grep  "^dracut_args" /etc/kdump.conf)
DistroBaker a10140
    while [ $# -gt 0 ]; do
DistroBaker a10140
        case $1 in
DistroBaker a10140
            -o|--omit)
DistroBaker a10140
                [[ " ${2//[^[:alnum:]]/ } " ==  *" $dracut_mod "* ]] && return 0
DistroBaker a10140
        esac
DistroBaker a10140
        shift
DistroBaker a10140
    done
Petr Šabata f5bf49
DistroBaker a10140
    return 1
Petr Šabata f5bf49
}
Petr Šabata f5bf49
DistroBaker 17a515
is_wdt_active() {
DistroBaker 17a515
    local active
DistroBaker 17a515
DistroBaker 17a515
    [ -d /sys/class/watchdog ] || return 1
DistroBaker 17a515
    for dir in /sys/class/watchdog/*; do
DistroBaker 17a515
        [ -f "$dir/state" ] || continue
DistroBaker 17a515
        active=$(< "$dir/state")
DistroBaker 17a515
        [ "$active" =  "active" ] && return 0
DistroBaker 17a515
    done
DistroBaker 17a515
    return 1
DistroBaker 17a515
}
DistroBaker 17a515
Petr Šabata f5bf49
# If "dracut_args" contains "--mount" information, use it
Petr Šabata f5bf49
# directly without any check(users are expected to ensure
Petr Šabata f5bf49
# its correctness).
Petr Šabata f5bf49
is_mount_in_dracut_args()
Petr Šabata f5bf49
{
Petr Šabata f5bf49
    grep -q "^dracut_args .*\-\-mount" /etc/kdump.conf
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
# If $1 contains dracut_args "--mount", return <filesystem type>
Petr Šabata f5bf49
get_dracut_args_fstype()
Petr Šabata f5bf49
{
Petr Šabata f5bf49
    echo $1 | grep "\-\-mount" | sed "s/.*--mount .\(.*\)/\1/" | cut -d' ' -f3
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
# If $1 contains dracut_args "--mount", return <device>
Petr Šabata f5bf49
get_dracut_args_target()
Petr Šabata f5bf49
{
Petr Šabata f5bf49
    echo $1 | grep "\-\-mount" | sed "s/.*--mount .\(.*\)/\1/" | cut -d' ' -f1
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
check_crash_mem_reserved()
Petr Šabata f5bf49
{
Petr Šabata f5bf49
    local mem_reserved
Petr Šabata f5bf49
Petr Šabata f5bf49
    mem_reserved=$(cat /sys/kernel/kexec_crash_size)
Petr Šabata f5bf49
    if [ $mem_reserved -eq 0 ]; then
DistroBaker 5cac7c
        derror "No memory reserved for crash kernel"
Petr Šabata f5bf49
        return 1
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
Petr Šabata f5bf49
    return 0
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
check_kdump_feasibility()
Petr Šabata f5bf49
{
Petr Šabata f5bf49
    if [ ! -e /sys/kernel/kexec_crash_loaded ]; then
DistroBaker 5cac7c
        derror "Kdump is not supported on this kernel"
Petr Šabata f5bf49
        return 1
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
    check_crash_mem_reserved
Petr Šabata f5bf49
    return $?
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
check_current_kdump_status()
Petr Šabata f5bf49
{
Petr Šabata f5bf49
    if [ ! -f /sys/kernel/kexec_crash_loaded ];then
DistroBaker 5cac7c
        derror "Perhaps CONFIG_CRASH_DUMP is not enabled in kernel"
Petr Šabata f5bf49
        return 1
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
Petr Šabata f5bf49
    rc=`cat /sys/kernel/kexec_crash_loaded`
Petr Šabata f5bf49
    if [ $rc == 1 ]; then
Petr Šabata f5bf49
        return 0
Petr Šabata f5bf49
    else
Petr Šabata f5bf49
        return 1
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
# remove_cmdline_param <kernel cmdline> <param1> [<param2>] ... [<paramN>]
Petr Šabata f5bf49
# Remove a list of kernel parameters from a given kernel cmdline and print the result.
Petr Šabata f5bf49
# For each "arg" in the removing params list, "arg" and "arg=xxx" will be removed if exists.
Petr Šabata f5bf49
remove_cmdline_param()
Petr Šabata f5bf49
{
Petr Šabata f5bf49
    local cmdline=$1
Petr Šabata f5bf49
    shift
Petr Šabata f5bf49
Petr Šabata f5bf49
    for arg in $@; do
Petr Šabata f5bf49
        cmdline=`echo $cmdline | \
Petr Šabata f5bf49
                 sed -e "s/\b$arg=[^ ]*//g" \
Petr Šabata f5bf49
                 -e "s/^$arg\b//g" \
Petr Šabata f5bf49
                 -e "s/[[:space:]]$arg\b//g" \
Petr Šabata f5bf49
                 -e "s/\s\+/ /g"`
Petr Šabata f5bf49
    done
Petr Šabata f5bf49
    echo $cmdline
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
#
Petr Šabata f5bf49
# This function returns the "apicid" of the boot
Petr Šabata f5bf49
# cpu (cpu 0) if present.
Petr Šabata f5bf49
#
Petr Šabata f5bf49
get_bootcpu_apicid()
Petr Šabata f5bf49
{
Petr Šabata f5bf49
    awk '                                                       \
Petr Šabata f5bf49
        BEGIN { CPU = "-1"; }                                   \
Petr Šabata f5bf49
        $1=="processor" && $2==":"      { CPU = $NF; }          \
Petr Šabata f5bf49
        CPU=="0" && /^apicid/           { print $NF; }          \
Petr Šabata f5bf49
        '                                                       \
Petr Šabata f5bf49
        /proc/cpuinfo
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
#
Petr Šabata f5bf49
# append_cmdline <kernel cmdline> <parameter name> <parameter value>
Petr Šabata f5bf49
# This function appends argument "$2=$3" to string ($1) if not already present.
Petr Šabata f5bf49
#
Petr Šabata f5bf49
append_cmdline()
Petr Šabata f5bf49
{
Petr Šabata f5bf49
    local cmdline=$1
Petr Šabata f5bf49
    local newstr=${cmdline/$2/""}
Petr Šabata f5bf49
Petr Šabata f5bf49
    # unchanged str implies argument wasn't there
Petr Šabata f5bf49
    if [ "$cmdline" == "$newstr" ]; then
Petr Šabata f5bf49
        cmdline="${cmdline} ${2}=${3}"
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
Petr Šabata f5bf49
    echo $cmdline
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
# This function check iomem and determines if we have more than
Petr Šabata f5bf49
# 4GB of ram available. Returns 1 if we do, 0 if we dont
Petr Šabata f5bf49
need_64bit_headers()
Petr Šabata f5bf49
{
Petr Šabata f5bf49
    return `tail -n 1 /proc/iomem | awk '{ split ($1, r, "-"); \
Petr Šabata f5bf49
    print (strtonum("0x" r[2]) > strtonum("0xffffffff")); }'`
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
# Check if secure boot is being enforced.
Petr Šabata f5bf49
#
Petr Šabata f5bf49
# Per Peter Jones, we need check efivar SecureBoot-$(the UUID) and
Petr Šabata f5bf49
# SetupMode-$(the UUID), they are both 5 bytes binary data. The first four
Petr Šabata f5bf49
# bytes are the attributes associated with the variable and can safely be
Petr Šabata f5bf49
# ignored, the last bytes are one-byte true-or-false variables. If SecureBoot
Petr Šabata f5bf49
# is 1 and SetupMode is 0, then secure boot is being enforced.
Petr Šabata f5bf49
#
Petr Šabata f5bf49
# Assume efivars is mounted at /sys/firmware/efi/efivars.
Petr Šabata f5bf49
is_secure_boot_enforced()
Petr Šabata f5bf49
{
Petr Šabata f5bf49
    local secure_boot_file setup_mode_file
Petr Šabata f5bf49
    local secure_boot_byte setup_mode_byte
Petr Šabata f5bf49
Petr Šabata f5bf49
    # On powerpc, os-secureboot-enforcing DT property indicates whether secureboot
Petr Šabata f5bf49
    # is enforced. Return success, if it is found.
Petr Šabata f5bf49
    if [ -f /proc/device-tree/ibm,secureboot/os-secureboot-enforcing ]; then
Petr Šabata f5bf49
		return 0
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
DistroBaker 5cac7c
    # Detect secure boot on x86 and arm64
Petr Šabata f5bf49
    secure_boot_file=$(find /sys/firmware/efi/efivars -name SecureBoot-* 2>/dev/null)
Petr Šabata f5bf49
    setup_mode_file=$(find /sys/firmware/efi/efivars -name SetupMode-* 2>/dev/null)
Petr Šabata f5bf49
Petr Šabata f5bf49
    if [ -f "$secure_boot_file" ] && [ -f "$setup_mode_file" ]; then
Petr Šabata f5bf49
        secure_boot_byte=$(hexdump -v -e '/1 "%d\ "' $secure_boot_file|cut -d' ' -f 5)
Petr Šabata f5bf49
        setup_mode_byte=$(hexdump -v -e '/1 "%d\ "' $setup_mode_file|cut -d' ' -f 5)
Petr Šabata f5bf49
Petr Šabata f5bf49
        if [ "$secure_boot_byte" = "1" ] && [ "$setup_mode_byte" = "0" ]; then
Petr Šabata f5bf49
            return 0
Petr Šabata f5bf49
        fi
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
DistroBaker 5cac7c
    # Detect secure boot on s390x
DistroBaker 5cac7c
    if [[ -e "/sys/firmware/ipl/secure" && "$(cat /sys/firmware/ipl/secure)" == "1" ]]; then
DistroBaker 5cac7c
        return 0
DistroBaker 5cac7c
    fi
DistroBaker 5cac7c
Petr Šabata f5bf49
    return 1
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
#
Petr Šabata f5bf49
# prepare_kexec_args <kexec args>
Petr Šabata f5bf49
# This function prepares kexec argument.
Petr Šabata f5bf49
#
Petr Šabata f5bf49
prepare_kexec_args()
Petr Šabata f5bf49
{
Petr Šabata f5bf49
    local kexec_args=$1
Petr Šabata f5bf49
    local found_elf_args
Petr Šabata f5bf49
Petr Šabata f5bf49
    ARCH=`uname -m`
Petr Šabata f5bf49
    if [ "$ARCH" == "i686" -o "$ARCH" == "i386" ]
Petr Šabata f5bf49
    then
Petr Šabata f5bf49
        need_64bit_headers
Petr Šabata f5bf49
        if [ $? == 1 ]
Petr Šabata f5bf49
        then
Petr Šabata f5bf49
            found_elf_args=`echo $kexec_args | grep elf32-core-headers`
Petr Šabata f5bf49
            if [ -n "$found_elf_args" ]
Petr Šabata f5bf49
            then
DistroBaker 5cac7c
                dwarn "Warning: elf32-core-headers overrides correct elf64 setting"
Petr Šabata f5bf49
            else
Petr Šabata f5bf49
                kexec_args="$kexec_args --elf64-core-headers"
Petr Šabata f5bf49
            fi
Petr Šabata f5bf49
        else
Petr Šabata f5bf49
            found_elf_args=`echo $kexec_args | grep elf64-core-headers`
Petr Šabata f5bf49
            if [ -z "$found_elf_args" ]
Petr Šabata f5bf49
            then
Petr Šabata f5bf49
                kexec_args="$kexec_args --elf32-core-headers"
Petr Šabata f5bf49
            fi
Petr Šabata f5bf49
        fi
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
    echo $kexec_args
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
#
Petr Šabata f5bf49
# Detect initrd and kernel location, results are stored in global enviromental variables:
Petr Šabata f5bf49
# KDUMP_BOOTDIR, KDUMP_KERNELVER, KDUMP_KERNEL, DEFAULT_INITRD, and KDUMP_INITRD
Petr Šabata f5bf49
#
Petr Šabata f5bf49
# Expectes KDUMP_BOOTDIR, KDUMP_IMG, KDUMP_IMG_EXT, KDUMP_KERNELVER to be loaded from config already
Petr Šabata f5bf49
# and will prefer already set values so user can specify custom kernel/initramfs location
Petr Šabata f5bf49
#
Petr Šabata f5bf49
prepare_kdump_bootinfo()
Petr Šabata f5bf49
{
Petr Šabata f5bf49
    local boot_imglist boot_dirlist boot_initrdlist curr_kver="$(uname -r)"
Petr Šabata f5bf49
    local machine_id
Petr Šabata f5bf49
Pingfan Liu 8ce1f3
    if [ -z "$KDUMP_KERNELVER" ]; then
Petr Šabata f5bf49
        KDUMP_KERNELVER="$(uname -r)"
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
Petr Šabata f5bf49
    read machine_id < /etc/machine-id
Petr Šabata f5bf49
    boot_dirlist=${KDUMP_BOOTDIR:-"/boot /boot/efi /efi /"}
Petr Šabata f5bf49
    boot_imglist="$KDUMP_IMG-$KDUMP_KERNELVER$KDUMP_IMG_EXT $machine_id/$KDUMP_KERNELVER/$KDUMP_IMG"
Petr Šabata f5bf49
Petr Šabata f5bf49
    # Use BOOT_IMAGE as reference if possible, strip the GRUB root device prefix in (hd0,gpt1) format
Petr Šabata f5bf49
    local boot_img="$(cat /proc/cmdline | sed "s/^BOOT_IMAGE=\((\S*)\)\?\(\S*\) .*/\2/")"
Petr Šabata f5bf49
    if [ -n "$boot_img" ]; then
Petr Šabata f5bf49
        boot_imglist="$boot_img $boot_imglist"
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
Petr Šabata f5bf49
    for dir in $boot_dirlist; do
Petr Šabata f5bf49
        for img in $boot_imglist; do
Petr Šabata f5bf49
            if [ -f "$dir/$img" ]; then
Petr Šabata f5bf49
                KDUMP_KERNEL=$(echo $dir/$img | tr -s '/')
Petr Šabata f5bf49
                break 2
Petr Šabata f5bf49
            fi
Petr Šabata f5bf49
        done
Petr Šabata f5bf49
    done
Petr Šabata f5bf49
Petr Šabata f5bf49
    if ! [ -e "$KDUMP_KERNEL" ]; then
DistroBaker 5cac7c
        derror "Failed to detect kdump kernel location"
Petr Šabata f5bf49
        return 1
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
Petr Šabata f5bf49
    # Set KDUMP_BOOTDIR to where kernel image is stored
Petr Šabata f5bf49
    KDUMP_BOOTDIR=$(dirname $KDUMP_KERNEL)
Petr Šabata f5bf49
Petr Šabata f5bf49
    # Default initrd should just stay aside of kernel image, try to find it in KDUMP_BOOTDIR
Petr Šabata f5bf49
    boot_initrdlist="initramfs-$KDUMP_KERNELVER.img initrd"
Petr Šabata f5bf49
    for initrd in $boot_initrdlist; do
Petr Šabata f5bf49
        if [ -f "$KDUMP_BOOTDIR/$initrd" ]; then
5f4c5f
            defaut_initrd_base="$initrd"
5f4c5f
            DEFAULT_INITRD="$KDUMP_BOOTDIR/$defaut_initrd_base"
Petr Šabata f5bf49
            break
Petr Šabata f5bf49
        fi
Petr Šabata f5bf49
    done
Petr Šabata f5bf49
5f4c5f
    # Create kdump initrd basename from default initrd basename
Petr Šabata f5bf49
    # initramfs-5.7.9-200.fc32.x86_64.img => initramfs-5.7.9-200.fc32.x86_64kdump.img
Petr Šabata f5bf49
    # initrd => initrdkdump
5f4c5f
    if [[ -z "$defaut_initrd_base" ]]; then
5f4c5f
        kdump_initrd_base=initramfs-${KDUMP_KERNELVER}kdump.img
5f4c5f
    elif [[ $defaut_initrd_base == *.* ]]; then
5f4c5f
        kdump_initrd_base=${defaut_initrd_base%.*}kdump.${DEFAULT_INITRD##*.}
Petr Šabata f5bf49
    else
5f4c5f
        kdump_initrd_base=${defaut_initrd_base}kdump
5f4c5f
    fi
5f4c5f
5f4c5f
    # Place kdump initrd in `/var/lib/kdump` if `KDUMP_BOOTDIR` not writable
5f4c5f
    if [[ ! -w "$KDUMP_BOOTDIR" ]];then
5f4c5f
        var_target_initrd_dir="/var/lib/kdump"
5f4c5f
        mkdir -p "$var_target_initrd_dir"
5f4c5f
        KDUMP_INITRD="$var_target_initrd_dir/$kdump_initrd_base"
5f4c5f
    else
5f4c5f
        KDUMP_INITRD="$KDUMP_BOOTDIR/$kdump_initrd_base"
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
}
Petr Šabata f5bf49
DistroBaker a10140
get_watchdog_drvs()
DistroBaker a10140
{
DistroBaker a10140
    local _wdtdrvs _drv _dir
DistroBaker a10140
DistroBaker a10140
    for _dir in /sys/class/watchdog/*; do
DistroBaker a10140
        # device/modalias will return driver of this device
DistroBaker a10140
        [[ -f "$_dir/device/modalias" ]] || continue
DistroBaker a10140
        _drv=$(< "$_dir/device/modalias")
DistroBaker a10140
        _drv=$(modprobe --set-version "$KDUMP_KERNELVER" -R $_drv 2>/dev/null)
DistroBaker a10140
        for i in $_drv; do
DistroBaker a10140
            if ! [[ " $_wdtdrvs " == *" $i "* ]]; then
DistroBaker a10140
                _wdtdrvs="$_wdtdrvs $i"
DistroBaker a10140
            fi
DistroBaker a10140
        done
DistroBaker a10140
    done
DistroBaker a10140
DistroBaker a10140
    echo $_wdtdrvs
DistroBaker a10140
}
DistroBaker a10140
Petr Šabata f5bf49
#
Petr Šabata f5bf49
# prepare_cmdline <commandline> <commandline remove> <commandline append>
Petr Šabata f5bf49
# This function performs a series of edits on the command line.
Petr Šabata f5bf49
# Store the final result in global $KDUMP_COMMANDLINE.
Petr Šabata f5bf49
prepare_cmdline()
Petr Šabata f5bf49
{
Petr Šabata f5bf49
    local cmdline id
Petr Šabata f5bf49
Petr Šabata f5bf49
    if [ -z "$1" ]; then
Petr Šabata f5bf49
        cmdline=$(cat /proc/cmdline)
Petr Šabata f5bf49
    else
Petr Šabata f5bf49
        cmdline="$1"
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
Petr Šabata f5bf49
    # These params should always be removed
Petr Šabata f5bf49
    cmdline=$(remove_cmdline_param "$cmdline" crashkernel panic_on_warn)
Petr Šabata f5bf49
    # These params can be removed configurably
Petr Šabata f5bf49
    cmdline=$(remove_cmdline_param "$cmdline" "$2")
Petr Šabata f5bf49
Petr Šabata f5bf49
    # Always remove "root=X", as we now explicitly generate all kinds
Petr Šabata f5bf49
    # of dump target mount information including root fs.
Petr Šabata f5bf49
    #
Petr Šabata f5bf49
    # We do this before KDUMP_COMMANDLINE_APPEND, if one really cares
Petr Šabata f5bf49
    # about it(e.g. for debug purpose), then can pass "root=X" using
Petr Šabata f5bf49
    # KDUMP_COMMANDLINE_APPEND.
Petr Šabata f5bf49
    cmdline=$(remove_cmdline_param "$cmdline" root)
Petr Šabata f5bf49
Petr Šabata f5bf49
    # With the help of "--hostonly-cmdline", we can avoid some interitage.
Petr Šabata f5bf49
    cmdline=$(remove_cmdline_param "$cmdline" rd.lvm.lv rd.luks.uuid rd.dm.uuid rd.md.uuid fcoe)
Petr Šabata f5bf49
Petr Šabata f5bf49
    # Remove netroot, rd.iscsi.initiator and iscsi_initiator since
Petr Šabata f5bf49
    # we get duplicate entries for the same in case iscsi code adds
Petr Šabata f5bf49
    # it as well.
Petr Šabata f5bf49
    cmdline=$(remove_cmdline_param "$cmdline" netroot rd.iscsi.initiator iscsi_initiator)
Petr Šabata f5bf49
Petr Šabata f5bf49
    cmdline="${cmdline} $3"
Petr Šabata f5bf49
Petr Šabata f5bf49
    id=$(get_bootcpu_apicid)
Petr Šabata f5bf49
    if [ ! -z ${id} ] ; then
Petr Šabata f5bf49
        cmdline=$(append_cmdline "${cmdline}" disable_cpu_apicid ${id})
Petr Šabata f5bf49
    fi
DistroBaker a10140
DistroBaker a10140
    # If any watchdog is used, set it's pretimeout to 0. pretimeout let
DistroBaker a10140
    # watchdog panic the kernel first, and reset the system after the
DistroBaker a10140
    # panic. If the system is already in kdump, panic is not helpful
DistroBaker a10140
    # and only increase the chance of watchdog failure.
DistroBaker a10140
    for i in $(get_watchdog_drvs); do
DistroBaker a10140
        cmdline+=" $i.pretimeout=0"
DistroBaker a10140
DistroBaker a10140
        if [[ $i == hpwdt ]]; then
DistroBaker a10140
            # hpwdt have a special parameter kdumptimeout, is's only suppose
DistroBaker a10140
            # to be set to non-zero in first kernel. In kdump, non-zero
DistroBaker a10140
            # value could prevent the watchdog from resetting the system.
DistroBaker a10140
            cmdline+=" $i.kdumptimeout=0"
DistroBaker a10140
        fi
DistroBaker a10140
    done
DistroBaker a10140
Petr Šabata f5bf49
    echo ${cmdline}
Petr Šabata f5bf49
}
Pingfan Liu 540d33
Pingfan Liu 540d33
#get system memory size in the unit of GB
Pingfan Liu 540d33
get_system_size()
Pingfan Liu 540d33
{
Pingfan Liu 540d33
    result=$(cat /proc/iomem  | grep "System RAM" | awk -F ":" '{ print $1 }' | tr [:lower:] [:upper:] | paste -sd+)
Pingfan Liu 540d33
    result="+$result"
Pingfan Liu 540d33
    # replace '-' with '+0x' and '+' with '-0x'
Pingfan Liu 540d33
    sum=$( echo $result | sed -e 's/-/K0x/g' | sed -e 's/+/-0x/g' | sed -e 's/K/+/g' )
Pingfan Liu 540d33
    size=$(printf "%d\n" $(($sum)))
Pingfan Liu 540d33
    let size=$size/1024/1024/1024
Pingfan Liu 540d33
Pingfan Liu 540d33
    echo $size
Pingfan Liu 540d33
}
Pingfan Liu 540d33
Pingfan Liu 540d33
get_recommend_size()
Pingfan Liu 540d33
{
Pingfan Liu 540d33
    local mem_size=$1
Pingfan Liu 540d33
    local _ck_cmdline=$2
Pingfan Liu 540d33
    local OLDIFS="$IFS"
Pingfan Liu 540d33
Pingfan Liu 540d33
    last_sz=""
Pingfan Liu 540d33
    last_unit=""
Pingfan Liu 540d33
Pingfan Liu 73929f
    start=${_ck_cmdline: :1}
Pingfan Liu 73929f
    if [ $mem_size -lt $start ]; then
Pingfan Liu 73929f
        echo "0M"
Pingfan Liu 73929f
        return
Pingfan Liu 73929f
    fi
Pingfan Liu 540d33
    IFS=','
Pingfan Liu 540d33
    for i in $_ck_cmdline; do
Pingfan Liu 540d33
        end=$(echo $i | awk -F "-" '{ print $2 }' | awk -F ":" '{ print $1 }')
Pingfan Liu 540d33
        recommend=$(echo $i | awk -F "-" '{ print $2 }' | awk -F ":" '{ print $2 }')
Pingfan Liu 540d33
        size=${end: : -1}
Pingfan Liu 540d33
        unit=${end: -1}
Pingfan Liu 540d33
        if [ $unit == 'T' ]; then
Pingfan Liu 540d33
            let size=$size*1024
Pingfan Liu 540d33
        fi
Pingfan Liu 540d33
        if [ $mem_size -lt $size ]; then
Pingfan Liu 540d33
            echo $recommend
Pingfan Liu 540d33
            IFS="$OLDIFS"
Pingfan Liu 540d33
            return
Pingfan Liu 540d33
        fi
Pingfan Liu 540d33
    done
Pingfan Liu 540d33
    IFS="$OLDIFS"
Pingfan Liu 540d33
}
Pingfan Liu 540d33
Pingfan Liu 540d33
# return recommended size based on current system RAM size
Pingfan Liu 540d33
kdump_get_arch_recommend_size()
Pingfan Liu 540d33
{
Pingfan Liu 540d33
    if ! [[ -r "/proc/iomem" ]] ; then
Pingfan Liu 540d33
        echo "Error, can not access /proc/iomem."
Pingfan Liu 540d33
        return 1
Pingfan Liu 540d33
    fi
Pingfan Liu 540d33
    arch=$(lscpu | grep Architecture | awk -F ":" '{ print $2 }' | tr [:lower:] [:upper:])
Pingfan Liu 540d33
Pingfan Liu 73929f
    if [ $arch == "X86_64" ] || [ $arch == "S390X" ]; then
Pingfan Liu 540d33
        ck_cmdline="1G-4G:160M,4G-64G:192M,64G-1T:256M,1T-:512M"
Pingfan Liu 73929f
    elif [ $arch == "AARCH64" ]; then
Pingfan Liu 540d33
        ck_cmdline="2G-:448M"
Pingfan Liu 540d33
    elif [ $arch == "PPC64LE" ]; then
Pingfan Liu 540d33
        if is_fadump_capable; then
Pingfan Liu 540d33
            ck_cmdline="4G-16G:768M,16G-64G:1G,64G-128G:2G,128G-1T:4G,1T-2T:6G,2T-4T:12G,4T-8T:20G,8T-16T:36G,16T-32T:64G,32T-64T:128G,64T-:180G"
Pingfan Liu 540d33
        else
Pingfan Liu 540d33
            ck_cmdline="2G-4G:384M,4G-16G:512M,16G-64G:1G,64G-128G:2G,128G-:4G"
Pingfan Liu 540d33
        fi
Pingfan Liu 540d33
    fi
Pingfan Liu 540d33
Pingfan Liu 540d33
    ck_cmdline=$(echo $ck_cmdline | sed -e 's/-:/-102400T:/g')
Pingfan Liu 540d33
    sys_mem=$(get_system_size)
Pingfan Liu 540d33
    result=$(get_recommend_size $sys_mem "$ck_cmdline")
Pingfan Liu 540d33
    echo $result
Pingfan Liu 540d33
    return 0
Pingfan Liu 540d33
}
Kairui Song ccdd4f
Kairui Song ccdd4f
# Print all underlying crypt devices of a block device
Kairui Song ccdd4f
# print nothing if device is not on top of a crypt device
Kairui Song ccdd4f
# $1: the block device to be checked in maj:min format
Kairui Song ccdd4f
get_luks_crypt_dev()
Kairui Song ccdd4f
{
Kairui Song ccdd4f
    [[ -b /dev/block/$1 ]] || return 1
Kairui Song ccdd4f
Kairui Song ccdd4f
    local _type=$(eval "$(blkid -u filesystem,crypto -o export -- /dev/block/$1); echo \$TYPE")
Kairui Song ccdd4f
    [[ $_type == "crypto_LUKS" ]] && echo $1
Kairui Song ccdd4f
Kairui Song ccdd4f
    for _x in /sys/dev/block/$1/slaves/*; do
Kairui Song ccdd4f
        [[ -f $_x/dev ]] || continue
Kairui Song ccdd4f
        [[ $_x/subsystem -ef /sys/class/block ]] || continue
Kairui Song ccdd4f
        get_luks_crypt_dev "$(< "$_x/dev")"
Kairui Song ccdd4f
    done
Kairui Song ccdd4f
}
Kairui Song 8387b5
Kairui Song 8387b5
# kdump_get_maj_min <device>
Kairui Song 8387b5
# Prints the major and minor of a device node.
Kairui Song 8387b5
# Example:
Kairui Song 8387b5
# $ get_maj_min /dev/sda2
Kairui Song 8387b5
# 8:2
Kairui Song 8387b5
kdump_get_maj_min() {
Kairui Song 8387b5
    local _majmin
Kairui Song 8387b5
    _majmin="$(stat -L -c '%t:%T' "$1" 2> /dev/null)"
Kairui Song 8387b5
    printf "%s" "$((0x${_majmin%:*})):$((0x${_majmin#*:}))"
Kairui Song 8387b5
}
Kairui Song 8387b5
Kairui Song 8387b5
get_all_kdump_crypt_dev()
Kairui Song 8387b5
{
Kairui Song 8387b5
    local _dev _crypt
Kairui Song 8387b5
Kairui Song 8387b5
    for _dev in $(get_block_dump_target); do
Kairui Song 8387b5
        _crypt=$(get_luks_crypt_dev $(kdump_get_maj_min "$_dev"))
Kairui Song 8387b5
        [[ -n "$_crypt" ]] && echo $_crypt
Kairui Song 8387b5
    done
Kairui Song 8387b5
}
Kairui Song a9fda5
Kairui Song a9fda5
check_vmlinux()
Kairui Song a9fda5
{
Kairui Song a9fda5
    # Use readelf to check if it's a valid ELF
Kairui Song a9fda5
    readelf -h $1 &>/dev/null || return 1
Kairui Song a9fda5
}
Kairui Song a9fda5
Kairui Song a9fda5
get_vmlinux_size()
Kairui Song a9fda5
{
Kairui Song a9fda5
    local size=0
Kairui Song a9fda5
Kairui Song a9fda5
    while read _type _offset _virtaddr _physaddr _fsize _msize _flg _aln; do
Kairui Song a9fda5
        size=$(( $size + $_msize ))
Kairui Song a9fda5
    done <<< $(readelf -l -W $1 | grep "^  LOAD" 2>/dev/stderr)
Kairui Song a9fda5
Kairui Song a9fda5
    echo $size
Kairui Song a9fda5
}
Kairui Song a9fda5
Kairui Song a9fda5
try_decompress()
Kairui Song a9fda5
{
Kairui Song a9fda5
    # The obscure use of the "tr" filter is to work around older versions of
Kairui Song a9fda5
    # "grep" that report the byte offset of the line instead of the pattern.
Kairui Song a9fda5
Kairui Song a9fda5
    # Try to find the header ($1) and decompress from here
Kairui Song a9fda5
    for pos in `tr "$1\n$2" "\n$2=" < "$4" | grep -abo "^$2"`
Kairui Song a9fda5
    do
Kairui Song a9fda5
        if ! type -P $3 > /dev/null; then
Kairui Song a9fda5
            ddebug "Signiature detected but '$3' is missing, skip this decompressor"
Kairui Song a9fda5
            break
Kairui Song a9fda5
        fi
Kairui Song a9fda5
Kairui Song a9fda5
        pos=${pos%%:*}
Kairui Song a9fda5
        tail -c+$pos "$img" | $3 > $5 2> /dev/null
Kairui Song a9fda5
        if check_vmlinux $5; then
Kairui Song a9fda5
            ddebug "Kernel is extracted with '$3'"
Kairui Song a9fda5
            return 0
Kairui Song a9fda5
        fi
Kairui Song a9fda5
    done
Kairui Song a9fda5
Kairui Song a9fda5
    return 1
Kairui Song a9fda5
}
Kairui Song a9fda5
Kairui Song a9fda5
# Borrowed from linux/scripts/extract-vmlinux
Kairui Song a9fda5
get_kernel_size()
Kairui Song a9fda5
{
Kairui Song a9fda5
    # Prepare temp files:
Kairui Song a9fda5
    local img=$1 tmp=$(mktemp /tmp/vmlinux-XXX)
Kairui Song a9fda5
    trap "rm -f $tmp" 0
Kairui Song a9fda5
Kairui Song a9fda5
    # Try to check if it's a vmlinux already
Kairui Song a9fda5
    check_vmlinux $img && get_vmlinux_size $img && return 0
Kairui Song a9fda5
Kairui Song a9fda5
    # That didn't work, so retry after decompression.
Kairui Song a9fda5
    try_decompress '\037\213\010' xy    gunzip    $img $tmp || \
Kairui Song a9fda5
    try_decompress '\3757zXZ\000' abcde unxz      $img $tmp || \
Kairui Song a9fda5
    try_decompress 'BZh'          xy    bunzip2   $img $tmp || \
Kairui Song a9fda5
    try_decompress '\135\0\0\0'   xxx   unlzma    $img $tmp || \
Kairui Song a9fda5
    try_decompress '\211\114\132' xy    'lzop -d' $img $tmp || \
Kairui Song a9fda5
    try_decompress '\002!L\030'   xxx   'lz4 -d'  $img $tmp || \
Kairui Song a9fda5
    try_decompress '(\265/\375'   xxx   unzstd    $img $tmp
Kairui Song a9fda5
Kairui Song a9fda5
    # Finally check for uncompressed images or objects:
Kairui Song a9fda5
    [[ $? -eq 0 ]] && get_vmlinux_size $tmp && return 0
Kairui Song a9fda5
Kairui Song a9fda5
    # Fallback to use iomem
Kairui Song a9fda5
    local _size=0
Kairui Song a9fda5
    for _seg in $(cat /proc/iomem  | grep -E "Kernel (code|rodata|data|bss)" | cut -d ":" -f 1); do
Kairui Song a9fda5
	    _size=$(( $_size + 0x${_seg#*-} - 0x${_seg%-*} ))
Kairui Song a9fda5
    done
Kairui Song a9fda5
    echo $_size
Kairui Song a9fda5
}