35519c
#!/bin/bash
Petr Šabata f5bf49
#
Petr Šabata f5bf49
# Kdump common variables and functions
Petr Šabata f5bf49
#
Petr Šabata f5bf49
35519c
. /usr/lib/kdump/kdump-lib-initramfs.sh
35519c
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
60e5a1
    if [[ -f $FADUMP_ENABLED_SYS_NODE ]]; then
60e5a1
        rc=$(cat $FADUMP_ENABLED_SYS_NODE)
60e5a1
        [[ $rc -eq 1 ]] && return 0
Petr Šabata f5bf49
    fi
Petr Šabata f5bf49
    return 1
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Kairui Song 96a3fc
is_squash_available() {
Kairui Song 96a3fc
    for kmodule in squashfs overlay loop; do
60e5a1
        if [[ -z "$KDUMP_KERNELVER" ]]; then
Kairui Song 96a3fc
            modprobe --dry-run $kmodule &>/dev/null || return 1
Kairui Song 96a3fc
        else
Kairui Song 96a3fc
            modprobe -S $KDUMP_KERNELVER --dry-run $kmodule &>/dev/null || return 1
Kairui Song 96a3fc
        fi
Kairui Song 96a3fc
    done
Kairui Song 96a3fc
}
Kairui Song 96a3fc
Petr Šabata f5bf49
perror_exit() {
DistroBaker 5cac7c
    derror "$@"
Petr Šabata f5bf49
    exit 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
60e5a1
    [[ -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
{
60e5a1
    [[ -x $FENCE_KDUMP_SEND ]] || return 1
Petr Šabata f5bf49
9a937b
    [[ $(kdump_get_conf_val fence_kdump_nodes) ]]
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=*)
60e5a1
        dev=$(blkid -U "${dev#UUID=}")
Petr Šabata f5bf49
        ;;
Petr Šabata f5bf49
    LABEL=*)
60e5a1
        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
{
9a937b
    [[ $(kdump_get_conf_val "ext[234]\|xfs\|btrfs\|minix\|raw\|nfs\|ssh") ]] || 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
9a937b
    _target=$(kdump_get_conf_val "ext[234]\|xfs\|btrfs\|minix\|raw")
60e5a1
    [[ -n "$_target" ]] && echo $_target && return
Petr Šabata f5bf49
9a937b
    _target=$(get_dracut_args_target "$(kdump_get_conf_val "dracut_args")")
60e5a1
    [[ -b "$_target" ]] && echo $_target
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)
60e5a1
    [[ -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)
60e5a1
    [[ -b "$_target" ]] && echo $(to_dev_name $_target)
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
is_dump_to_rootfs()
Petr Šabata f5bf49
{
9a937b
    [[ $(kdump_get_conf_val "failure_action|default") == dump_to_rootfs ]]
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)
60e5a1
        [[ -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)
60e5a1
    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)
60e5a1
    if [[ -n "$_root" ]] && [[ "$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
get_mntopt_from_target()
Petr Šabata f5bf49
{
Petr Šabata f5bf49
    get_mount_info OPTIONS 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.
60e5a1
    if [[ -z "$_mntpoint" ]];then
Petr Šabata f5bf49
        _mntpoint="/kdumproot"
Petr Šabata f5bf49
    else
60e5a1
        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
kdump_get_persistent_dev() {
Petr Šabata f5bf49
    local dev="${1//\"/}"
Petr Šabata f5bf49
Petr Šabata f5bf49
    case "$dev" in
Petr Šabata f5bf49
    UUID=*)
60e5a1
        dev=$(blkid -U "${dev#UUID=}")
Petr Šabata f5bf49
        ;;
Petr Šabata f5bf49
    LABEL=*)
60e5a1
        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
# 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
{
60e5a1
    local _hostname=$(echo $1 | grep ":")
Petr Šabata f5bf49
60e5a1
    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
{
60e5a1
    if [[ -f "/sys/class/net/${1}/address" ]]; then
Petr Šabata f5bf49
        awk '{ print toupper($0) }' < /sys/class/net/${1}/address
60e5a1
    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
{
60e5a1
    [[ "$(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}")
60e5a1
        [[ -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}"
60e5a1
    [[ -f "${ifcfg_file}" ]] && echo -n "${ifcfg_file}" && return
Petr Šabata f5bf49
Petr Šabata f5bf49
    ifcfg_file=$(get_ifcfg_by_name "${1}")
60e5a1
    [[ -f "${ifcfg_file}" ]] && echo -n "${ifcfg_file}" && return
Petr Šabata f5bf49
Petr Šabata f5bf49
    local hwaddr=$(get_hwaddr "${1}")
60e5a1
    if [[ -n "$hwaddr" ]]; then
Petr Šabata f5bf49
        ifcfg_file=$(get_ifcfg_by_hwaddr "${hwaddr}")
60e5a1
        [[ -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}")
60e5a1
    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
9a937b
    set -- $(kdump_get_conf_val dracut_args)
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
60e5a1
    [[ -d /sys/class/watchdog ]] || return 1
DistroBaker 17a515
    for dir in /sys/class/watchdog/*; do
60e5a1
        [[ -f "$dir/state" ]] || continue
DistroBaker 17a515
        active=$(< "$dir/state")
60e5a1
        [[ "$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
{
9a937b
    [[ " $(kdump_get_conf_val dracut_args)" =~ .*[[:space:]]--mount[=[:space:]].* ]]
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)
60e5a1
    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
{
60e5a1
    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
{
60e5a1
    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
60e5a1
    rc=$(cat /sys/kernel/kexec_crash_loaded)
60e5a1
    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
60e5a1
        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" \
60e5a1
                 -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
60e5a1
    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
{
60e5a1
    return "$(tail -n 1 /proc/iomem | awk '{ split ($1, r, "-");
60e5a1
        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.
60e5a1
    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
60e5a1
    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
60e5a1
        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
60e5a1
    ARCH=$(uname -m)
60e5a1
    if [[ "$ARCH" == "i686" ]] || [[ "$ARCH" == "i386" ]]
Petr Šabata f5bf49
    then
Petr Šabata f5bf49
        need_64bit_headers
60e5a1
        if [[ $? == 1 ]]
Petr Šabata f5bf49
        then
60e5a1
            found_elf_args=$(echo $kexec_args | grep elf32-core-headers)
60e5a1
            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
60e5a1
            found_elf_args=$(echo $kexec_args | grep elf64-core-headers)
60e5a1
            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
60e5a1
    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/")"
60e5a1
    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
60e5a1
            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
60e5a1
    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
60e5a1
        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
60e5a1
    # 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
60e5a1
    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)
60e5a1
    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}
60e5a1
    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}
60e5a1
        if [[ $unit == 'T' ]]; then
Pingfan Liu 540d33
            let size=$size*1024
Pingfan Liu 540d33
        fi
60e5a1
        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
60e5a1
# $1: kernel version, if not set, will defaults to $(uname -r)
Pingfan Liu 540d33
kdump_get_arch_recommend_size()
Pingfan Liu 540d33
{
Kairui Song abb0c3
    local kernel=$1 arch
Kairui Song abb0c3
60e5a1
    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
60e5a1
    [[ -z "$kernel" ]] && kernel=$(uname -r)
Kairui Song abb0c3
    ck_cmdline=$(cat "/usr/lib/modules/$kernel/crashkernel.default" 2>/dev/null)
Kairui Song abb0c3
60e5a1
    if [[ -n "$ck_cmdline" ]]; then
Kairui Song abb0c3
        ck_cmdline=${ck_cmdline#crashkernel=}
Kairui Song abb0c3
    else
Kairui Song abb0c3
        arch=$(lscpu | grep Architecture | awk -F ":" '{ print $2 }' | tr '[:lower:]' '[:upper:]')
60e5a1
        if [[ "$arch" = "X86_64" ]] || [[ "$arch" = "S390X" ]]; then
Kairui Song abb0c3
            ck_cmdline="1G-4G:160M,4G-64G:192M,64G-1T:256M,1T-:512M"
60e5a1
        elif [[ "$arch" = "AARCH64" ]]; then
Kairui Song abb0c3
            ck_cmdline="2G-:448M"
60e5a1
        elif [[ "$arch" = "PPC64LE" ]]; then
Kairui Song abb0c3
            if is_fadump_capable; then
Kairui Song abb0c3
                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"
Kairui Song abb0c3
            else
Kairui Song abb0c3
                ck_cmdline="2G-4G:384M,4G-16G:512M,16G-64G:1G,64G-128G:2G,128G-:4G"
Kairui Song abb0c3
            fi
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)
Kairui Song abb0c3
Kairui Song abb0c3
    get_recommend_size "$sys_mem" "$ck_cmdline"
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
60e5a1
    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
}