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