bash scripts: reformat with shfmt

upstream: fedora
resolves: bz2003832
conflict:
    function load_kdump_kernel_key() not exist in rhel9,
    so related patch hunk is removed.

commit 0e4b66b1ab
Author: Kairui Song <kasong@redhat.com>
Date:   Tue Sep 14 02:25:40 2021 +0800

    bash scripts: reformat with shfmt

    This is a batch update done with:
    shfmt -s -w mkfadumprd mkdumprd kdumpctl *-module-setup.sh

    Clean up code style and reduce code base size, no behaviour change.

    Signed-off-by: Kairui Song <kasong@redhat.com>
    Acked-by: Philipp Rudo <prudo@redhat.com>

Signed-off-by: Tao Liu <ltao@redhat.com>
This commit is contained in:
Tao Liu 2021-11-09 21:42:45 +08:00
parent 63308480fc
commit b494b7f193
5 changed files with 563 additions and 552 deletions

View File

@ -6,9 +6,8 @@ KDUMP_KERNEL=""
KDUMP_INITRD=""
check() {
if [[ ! -f /etc/sysconfig/kdump ]] || [[ ! -f /lib/kdump/kdump-lib.sh ]]\
|| [[ -n "${IN_KDUMP}" ]]
then
if [[ ! -f /etc/sysconfig/kdump ]] || [[ ! -f /lib/kdump/kdump-lib.sh ]] \
|| [[ -n ${IN_KDUMP} ]]; then
return 1
fi
return 255
@ -25,7 +24,7 @@ prepare_kernel_initrd() {
prepare_kdump_bootinfo
# $kernel is a variable from dracut
if [[ "$KDUMP_KERNELVER" != "$kernel" ]]; then
if [[ $KDUMP_KERNELVER != "$kernel" ]]; then
dwarn "Using kernel version '$KDUMP_KERNELVER' for early kdump," \
"but the initramfs is generated for kernel version '$kernel'"
fi
@ -33,12 +32,12 @@ prepare_kernel_initrd() {
install() {
prepare_kernel_initrd
if [[ ! -f "$KDUMP_KERNEL" ]]; then
if [[ ! -f $KDUMP_KERNEL ]]; then
derror "Could not find required kernel for earlykdump," \
"earlykdump will not work!"
return 1
fi
if [[ ! -f "$KDUMP_INITRD" ]]; then
if [[ ! -f $KDUMP_INITRD ]]; then
derror "Could not find required kdump initramfs for earlykdump," \
"please ensure kdump initramfs is generated first," \
"earlykdump will not work!"

View File

@ -11,8 +11,7 @@ kdump_module_init() {
check() {
[[ $debug ]] && set -x
#kdumpctl sets this explicitly
if [[ -z "$IN_KDUMP" ]] || [[ ! -f /etc/kdump.conf ]]
then
if [[ -z $IN_KDUMP ]] || [[ ! -f /etc/kdump.conf ]]; then
return 1
fi
return 0
@ -41,11 +40,11 @@ depends() {
_dep="$_dep ssh-client"
fi
if [[ "$(uname -m)" = "s390x" ]]; then
if [[ "$(uname -m)" == "s390x" ]]; then
_dep="$_dep znet"
fi
if [[ -n "$( ls -A /sys/class/drm 2>/dev/null )" ]] || [[ -d /sys/module/hyperv_fb ]]; then
if [[ -n "$(ls -A /sys/class/drm 2> /dev/null)" ]] || [[ -d /sys/module/hyperv_fb ]]; then
add_opt_module drm
fi
@ -78,7 +77,7 @@ source_ifcfg_file() {
dwarning "Network Scripts are deprecated. You are encouraged to set up network by NetworkManager."
ifcfg_file=$(get_ifcfg_filename "$1")
if [[ -f "${ifcfg_file}" ]]; then
if [[ -f ${ifcfg_file} ]]; then
. "${ifcfg_file}"
else
dwarning "The ifcfg file of $1 is not found!"
@ -94,28 +93,26 @@ kdump_setup_dns() {
_tmp=$(get_nmcli_value_by_field "$_nm_show_cmd" "IP4.DNS")
# shellcheck disable=SC2206
array=( ${_tmp//|/ } )
array=(${_tmp//|/ })
if [[ ${array[*]} ]]; then
for _dns in "${array[@]}"
do
for _dns in "${array[@]}"; do
echo "nameserver=$_dns" >> "$_dnsfile"
done
else
dwarning "Failed to get DNS info via nmcli output. Now try sourcing ifcfg script"
source_ifcfg_file "$_netdev"
[[ -n "$DNS1" ]] && echo "nameserver=$DNS1" > "$_dnsfile"
[[ -n "$DNS2" ]] && echo "nameserver=$DNS2" >> "$_dnsfile"
[[ -n $DNS1 ]] && echo "nameserver=$DNS1" > "$_dnsfile"
[[ -n $DNS2 ]] && echo "nameserver=$DNS2" >> "$_dnsfile"
fi
while read -r content;
do
while read -r content; do
_nameserver=$(echo "$content" | grep ^nameserver)
[[ -z "$_nameserver" ]] && continue
[[ -z $_nameserver ]] && continue
_dns=$(echo "$_nameserver" | awk '{print $2}')
[[ -z "$_dns" ]] && continue
[[ -z $_dns ]] && continue
if [[ ! -f $_dnsfile ]] || ! grep -q "$_dns" "$_dnsfile" ; then
if [[ ! -f $_dnsfile ]] || ! grep -q "$_dns" "$_dnsfile"; then
echo "nameserver=$_dns" >> "$_dnsfile"
fi
done < "/etc/resolv.conf"
@ -130,7 +127,7 @@ repeatedly_join_str() {
local _separator="$3"
local i _res
if [[ "$_count" -le 0 ]]; then
if [[ $_count -le 0 ]]; then
echo -n ""
return
fi
@ -139,7 +136,7 @@ repeatedly_join_str() {
_res="$_str"
((_count--))
while [[ "$i" -lt "$_count" ]]; do
while [[ $i -lt $_count ]]; do
((i++))
_res="${_res}${_separator}${_str}"
done
@ -160,14 +157,14 @@ cal_netmask_by_prefix() {
local _count _res _octets_per_group _octets_total _seperator _total_groups
local _max_group_value _max_group_value_repr _bits_per_group _tmp _zero_bits
if [[ "$_ipv6_flag" == "-6" ]]; then
if [[ $_ipv6_flag == "-6" ]]; then
_ipv6=1
else
_ipv6=0
fi
if [[ "$_prefix" -lt 0 || "$_prefix" -gt 128 ]] || \
( ((!_ipv6)) && [[ "$_prefix" -gt 32 ]] ); then
if [[ $_prefix -lt 0 || $_prefix -gt 128 ]] \
|| ( ((!_ipv6)) && [[ $_prefix -gt 32 ]]); then
derror "Bad prefix:$_prefix for calculating netmask"
exit 1
fi
@ -182,7 +179,7 @@ cal_netmask_by_prefix() {
_seperator="."
fi
_total_groups=$((_octets_total/_octets_per_group))
_total_groups=$((_octets_total / _octets_per_group))
_bits_per_group=$((_octets_per_group * _bits_per_octet))
_max_group_value=$(((1 << _bits_per_group) - 1))
@ -192,39 +189,39 @@ cal_netmask_by_prefix() {
_max_group_value_repr="$_max_group_value"
fi
_count=$((_prefix/_octets_per_group/_bits_per_octet))
_count=$((_prefix / _octets_per_group / _bits_per_octet))
_first_part=$(repeatedly_join_str "$_count" "$_max_group_value_repr" "$_seperator")
_res="$_first_part"
_tmp=$((_octets_total*_bits_per_octet-_prefix))
_tmp=$((_octets_total * _bits_per_octet - _prefix))
_zero_bits=$((_tmp % _bits_per_group))
if [[ "$_zero_bits" -ne 0 ]]; then
if [[ $_zero_bits -ne 0 ]]; then
_second_part=$((_max_group_value >> _zero_bits << _zero_bits))
if ((_ipv6)); then
_second_part=$(printf "%x" $_second_part)
fi
((_count++))
if [[ -z "$_first_part" ]]; then
if [[ -z $_first_part ]]; then
_res="$_second_part"
else
_res="${_first_part}${_seperator}${_second_part}"
fi
fi
_count=$((_total_groups-_count))
if [[ "$_count" -eq 0 ]]; then
_count=$((_total_groups - _count))
if [[ $_count -eq 0 ]]; then
echo -n "$_res"
return
fi
if ((_ipv6)) && [[ "$_count" -gt 1 ]] ; then
if ((_ipv6)) && [[ $_count -gt 1 ]]; then
# use condensed notion for IPv6
_third_part=":"
else
_third_part=$(repeatedly_join_str "$_count" "0" "$_seperator")
fi
if [[ -z "$_res" ]] && ((!_ipv6)) ; then
if [[ -z $_res ]] && ((!_ipv6)); then
echo -n "${_third_part}"
else
echo -n "${_res}${_seperator}${_third_part}"
@ -244,9 +241,9 @@ kdump_static_ip() {
_ipv6_flag="-6"
fi
if [[ -n "$_ipaddr" ]]; then
_gateway=$(ip $_ipv6_flag route list dev "$_netdev" | \
awk '/^default /{print $3}' | head -n 1)
if [[ -n $_ipaddr ]]; then
_gateway=$(ip $_ipv6_flag route list dev "$_netdev" \
| awk '/^default /{print $3}' | head -n 1)
if [[ "x" != "x"$_ipv6_flag ]]; then
# _ipaddr="2002::56ff:feb6:56d5/64", _netmask is the number after "/"
@ -263,9 +260,9 @@ kdump_static_ip() {
echo -n "${_srcaddr}::${_gateway}:${_netmask}::"
fi
/sbin/ip $_ipv6_flag route show | grep -v default |\
grep ".*via.* $_netdev " | grep -v "^[[:space:]]*nexthop" |\
while read -r _route; do
/sbin/ip $_ipv6_flag route show | grep -v default \
| grep ".*via.* $_netdev " | grep -v "^[[:space:]]*nexthop" \
| while read -r _route; do
_target=$(echo "$_route" | awk '{print $1}')
_nexthop=$(echo "$_route" | awk '{print $3}')
if [[ "x" != "x"$_ipv6_flag ]]; then
@ -287,14 +284,14 @@ kdump_handle_mulitpath_route() {
fi
while IFS="" read -r _route; do
if [[ "$_route" =~ [[:space:]]+nexthop ]]; then
if [[ $_route =~ [[:space:]]+nexthop ]]; then
_route=${_route##[[:space:]]}
# Parse multipath route, using previous _target
[[ "$_target" == 'default' ]] && continue
[[ "$_route" =~ .*via.*\ $_netdev ]] || continue
[[ $_target == 'default' ]] && continue
[[ $_route =~ .*via.*\ $_netdev ]] || continue
_weight=$(echo "$_route" | cut -d ' ' -f7)
if [[ "$_weight" -gt "$_max_weight" ]]; then
if [[ $_weight -gt $_max_weight ]]; then
_nexthop=$(echo "$_route" | cut -d ' ' -f3)
_max_weight=$_weight
if [[ "x" != "x"$_ipv6_flag ]]; then
@ -304,11 +301,11 @@ kdump_handle_mulitpath_route() {
fi
fi
else
[[ -n "$_rule" ]] && echo "$_rule"
[[ -n $_rule ]] && echo "$_rule"
_target=$(echo "$_route" | cut -d ' ' -f1)
_rule="" _max_weight=0 _weight=0
fi
done >> "${initdir}/etc/cmdline.d/45route-static.conf"\
done >> "${initdir}/etc/cmdline.d/45route-static.conf" \
<<< "$(/sbin/ip $_ipv6_flag route show)"
[[ -n $_rule ]] && echo "$_rule" >> "${initdir}/etc/cmdline.d/45route-static.conf"
@ -323,8 +320,7 @@ kdump_get_mac_addr() {
kdump_get_perm_addr() {
local addr
addr=$(ethtool -P "$1" | sed -e 's/Permanent address: //')
if [[ -z "$addr" ]] || [[ "$addr" = "00:00:00:00:00:00" ]]
then
if [[ -z $addr ]] || [[ $addr == "00:00:00:00:00:00" ]]; then
derror "Can't get the permanent address of $1"
else
echo "$addr"
@ -391,13 +387,13 @@ kdump_setup_bond() {
_bondoptions=$(get_nmcli_value_by_field "$_nm_show_cmd" "bond.options")
if [[ -z "$_bondoptions" ]]; then
if [[ -z $_bondoptions ]]; then
dwarning "Failed to get bond configuration via nmlci output. Now try sourcing ifcfg script."
source_ifcfg_file "$_netdev"
_bondoptions="$(echo "$BONDING_OPTS" | xargs echo | tr " " ",")"
fi
if [[ -z "$_bondoptions" ]]; then
if [[ -z $_bondoptions ]]; then
derror "Get empty bond options"
exit 1
fi
@ -456,10 +452,9 @@ find_online_znet_device() {
local CCWGROUPBUS_DEVICEDIR="/sys/bus/ccwgroup/devices"
local NETWORK_DEVICES d ifname ONLINE
[[ ! -d "$CCWGROUPBUS_DEVICEDIR" ]] && return
[[ ! -d $CCWGROUPBUS_DEVICEDIR ]] && return
NETWORK_DEVICES=$(find $CCWGROUPBUS_DEVICEDIR)
for d in $NETWORK_DEVICES
do
for d in $NETWORK_DEVICES; do
[[ ! -f "$d/online" ]] && continue
read -r ONLINE < "$d/online"
if [[ $ONLINE -ne 1 ]]; then
@ -467,14 +462,12 @@ find_online_znet_device() {
fi
# determine interface name, if there (only for qeth and if
# device is online)
if [[ -f $d/if_name ]]
then
if [[ -f $d/if_name ]]; then
read -r ifname < "$d/if_name"
elif [[ -d $d/net ]]
then
elif [[ -d $d/net ]]; then
ifname=$(ls "$d/net/")
fi
[[ -n "$ifname" ]] && break
[[ -n $ifname ]] && break
done
echo -n "$ifname"
}
@ -494,7 +487,7 @@ kdump_setup_znet() {
SUBCHANNELS=$(get_nmcli_value_by_field "$_nmcli_cmd" "${s390_prefix}subchannels")
_options=$(get_nmcli_value_by_field "$_nmcli_cmd" "${s390_prefix}options")
if [[ -z "$NETTYPE" || -z "$SUBCHANNELS" || -z "$_options" ]]; then
if [[ -z $NETTYPE || -z $SUBCHANNELS || -z $_options ]]; then
dwarning "Failed to get znet configuration via nmlci output. Now try sourcing ifcfg script."
source_ifcfg_file "$_netdev"
for i in $OPTIONS; do
@ -502,15 +495,14 @@ kdump_setup_znet() {
done
fi
if [[ -z "$NETTYPE" || -z "$SUBCHANNELS" || -z "$_options" ]]; then
if [[ -z $NETTYPE || -z $SUBCHANNELS || -z $_options ]]; then
exit 1
fi
echo "rd.znet=${NETTYPE},${SUBCHANNELS},${_options} rd.znet_ifname=$_netdev:${SUBCHANNELS}" > "${initdir}/etc/cmdline.d/30znet.conf"
}
kdump_get_ip_route()
{
kdump_get_ip_route() {
local _route
if ! _route=$(/sbin/ip -o route get to "$1" 2>&1); then
derror "Bad kdump network destination: $1"
@ -519,18 +511,16 @@ kdump_get_ip_route()
echo "$_route"
}
kdump_get_ip_route_field()
{
kdump_get_ip_route_field() {
echo "$1" | sed -n -e "s/^.*\<$2\>\s\+\(\S\+\).*$/\1/p"
}
kdump_get_remote_ip()
{
kdump_get_remote_ip() {
local _remote _remote_temp
_remote=$(get_remote_host "$1")
if is_hostname "$_remote"; then
_remote_temp=$(getent ahosts "$_remote" | grep -v : | head -n 1)
if [[ -z "$_remote_temp" ]]; then
if [[ -z $_remote_temp ]]; then
_remote_temp=$(getent ahosts "$_remote" | head -n 1)
fi
_remote=$(echo "$_remote_temp" | awk '{print $1}')
@ -555,7 +545,7 @@ kdump_install_net() {
kdumpnic=$(kdump_setup_ifname "$_netdev")
_znet_netdev=$(find_online_znet_device)
if [[ -n "$_znet_netdev" ]]; then
if [[ -n $_znet_netdev ]]; then
_nm_show_cmd_znet=$(get_nmcli_connection_show_cmd_by_ifname "$_znet_netdev")
if ! (kdump_setup_znet "$_znet_netdev" "$_nm_show_cmd_znet"); then
derror "Failed to set up znet"
@ -564,7 +554,7 @@ kdump_install_net() {
fi
_static=$(kdump_static_ip "$_netdev" "$_srcaddr" "$kdumpnic")
if [[ -n "$_static" ]]; then
if [[ -n $_static ]]; then
_proto=none
elif is_ipv6_address "$_srcaddr"; then
_proto=auto6
@ -579,8 +569,8 @@ kdump_install_net() {
# so we have to avoid adding duplicates
# We should also check /proc/cmdline for existing ip=xx arg.
# For example, iscsi boot will specify ip=xxx arg in cmdline.
if [[ ! -f $_ip_conf ]] || ! grep -q "$_ip_opts" "$_ip_conf" &&\
! grep -q "ip=[^[:space:]]*$_netdev" /proc/cmdline; then
if [[ ! -f $_ip_conf ]] || ! grep -q "$_ip_opts" "$_ip_conf" \
&& ! grep -q "ip=[^[:space:]]*$_netdev" /proc/cmdline; then
echo "$_ip_opts" >> "$_ip_conf"
fi
@ -611,8 +601,8 @@ kdump_install_net() {
# the default gate way for network dump, eth1 in the fence kdump path will
# call kdump_install_net again and we don't want eth1 to be the default
# gateway.
if [[ ! -f ${initdir}/etc/cmdline.d/60kdumpnic.conf ]] &&
[[ ! -f ${initdir}/etc/cmdline.d/70bootdev.conf ]]; then
if [[ ! -f ${initdir}/etc/cmdline.d/60kdumpnic.conf ]] \
&& [[ ! -f ${initdir}/etc/cmdline.d/70bootdev.conf ]]; then
echo "kdumpnic=$kdumpnic" > "${initdir}/etc/cmdline.d/60kdumpnic.conf"
echo "bootdev=$kdumpnic" > "${initdir}/etc/cmdline.d/70bootdev.conf"
fi
@ -622,7 +612,7 @@ kdump_install_net() {
kdump_install_pre_post_conf() {
if [[ -d /etc/kdump/pre.d ]]; then
for file in /etc/kdump/pre.d/*; do
if [[ -x "$file" ]]; then
if [[ -x $file ]]; then
dracut_install "$file"
elif [[ $file != "/etc/kdump/pre.d/*" ]]; then
echo "$file is not executable"
@ -632,7 +622,7 @@ kdump_install_pre_post_conf() {
if [[ -d /etc/kdump/post.d ]]; then
for file in /etc/kdump/post.d/*; do
if [[ -x "$file" ]]; then
if [[ -x $file ]]; then
dracut_install "$file"
elif [[ $file != "/etc/kdump/post.d/*" ]]; then
echo "$file is not executable"
@ -641,8 +631,7 @@ kdump_install_pre_post_conf() {
fi
}
default_dump_target_install_conf()
{
default_dump_target_install_conf() {
local _target _fstype
local _mntpoint _save_path
@ -663,7 +652,7 @@ default_dump_target_install_conf()
echo "$_fstype $_target" >> "${initdir}/tmp/$$-kdump.conf"
# don't touch the path under root mount
if [[ "$_mntpoint" != "/" ]]; then
if [[ $_mntpoint != "/" ]]; then
_save_path=${_save_path##"$_mntpoint"}
fi
@ -678,27 +667,26 @@ kdump_install_conf() {
kdump_read_conf > "${initdir}/tmp/$$-kdump.conf"
while read -r _opt _val;
do
while read -r _opt _val; do
# remove inline comments after the end of a directive.
case "$_opt" in
raw)
_pdev=$(persistent_policy="by-id" kdump_get_persistent_dev "$_val")
sed -i -e "s#^${_opt}[[:space:]]\+$_val#$_opt $_pdev#" "${initdir}/tmp/$$-kdump.conf"
;;
ext[234]|xfs|btrfs|minix)
ext[234] | xfs | btrfs | minix)
_pdev=$(kdump_get_persistent_dev "$_val")
sed -i -e "s#^${_opt}[[:space:]]\+$_val#$_opt $_pdev#" "${initdir}/tmp/$$-kdump.conf"
;;
ssh|nfs)
ssh | nfs)
kdump_install_net "$_val"
;;
dracut_args)
if [[ $(get_dracut_args_fstype "$_val") = nfs* ]] ; then
if [[ $(get_dracut_args_fstype "$_val") == nfs* ]]; then
kdump_install_net "$(get_dracut_args_target "$_val")"
fi
;;
kdump_pre|kdump_post|extra_bins)
kdump_pre | kdump_post | extra_bins)
dracut_install "$_val"
;;
core_collector)
@ -744,16 +732,17 @@ kdump_get_iscsi_initiator() {
local _initiator
local initiator_conf="/etc/iscsi/initiatorname.iscsi"
[[ -f "$initiator_conf" ]] || return 1
[[ -f $initiator_conf ]] || return 1
while read -r _initiator; do
[[ -z "${_initiator%%#*}" ]] && continue # Skip comment lines
[[ -z ${_initiator%%#*} ]] && continue # Skip comment lines
case $_initiator in
InitiatorName=*)
initiator=${_initiator#InitiatorName=}
echo "rd.iscsi.initiator=${initiator}"
return 0;;
return 0
;;
*) ;;
esac
done < ${initiator_conf}
@ -763,15 +752,21 @@ kdump_get_iscsi_initiator() {
# Figure out iBFT session according to session type
is_ibft() {
[[ "$(kdump_iscsi_get_rec_val "$1" "node.discovery_type")" = fw ]]
[[ "$(kdump_iscsi_get_rec_val "$1" "node.discovery_type")" == fw ]]
}
kdump_setup_iscsi_device() {
local path=$1
local tgt_name; local tgt_ipaddr;
local username; local password; local userpwd_str;
local username_in; local password_in; local userpwd_in_str;
local netroot_str ; local initiator_str;
local tgt_name
local tgt_ipaddr
local username
local password
local userpwd_str
local username_in
local password_in
local userpwd_in_str
local netroot_str
local initiator_str
local netroot_conf="${initdir}/etc/cmdline.d/50iscsi.conf"
local initiator_conf="/etc/iscsi/initiatorname.iscsi"
@ -779,7 +774,7 @@ kdump_setup_iscsi_device() {
# Check once before getting explicit values, so we can bail out early,
# e.g. in case of pure-hardware(all-offload) iscsi.
if ! /sbin/iscsiadm -m session -r "$path" &>/dev/null ; then
if ! /sbin/iscsiadm -m session -r "$path" &> /dev/null; then
return 1
fi
@ -796,18 +791,18 @@ kdump_setup_iscsi_device() {
# get and set username and password details
username=$(kdump_iscsi_get_rec_val "$path" "node.session.auth.username")
[[ "$username" == "<empty>" ]] && username=""
[[ $username == "<empty>" ]] && username=""
password=$(kdump_iscsi_get_rec_val "$path" "node.session.auth.password")
[[ "$password" == "<empty>" ]] && password=""
[[ $password == "<empty>" ]] && password=""
username_in=$(kdump_iscsi_get_rec_val "$path" "node.session.auth.username_in")
[[ -n "$username" ]] && userpwd_str="$username:$password"
[[ -n $username ]] && userpwd_str="$username:$password"
# get and set incoming username and password details
[[ "$username_in" == "<empty>" ]] && username_in=""
[[ $username_in == "<empty>" ]] && username_in=""
password_in=$(kdump_iscsi_get_rec_val "$path" "node.session.auth.password_in")
[[ "$password_in" == "<empty>" ]] && password_in=""
[[ $password_in == "<empty>" ]] && password_in=""
[[ -n "$username_in" ]] && userpwd_in_str=":$username_in:$password_in"
[[ -n $username_in ]] && userpwd_in_str=":$username_in:$password_in"
kdump_install_net "$tgt_ipaddr"
@ -841,9 +836,9 @@ kdump_setup_iscsi_device() {
fi
}
kdump_check_iscsi_targets () {
kdump_check_iscsi_targets() {
# If our prerequisites are not met, fail anyways.
type -P iscsistart >/dev/null || return 1
type -P iscsistart > /dev/null || return 1
kdump_check_setup_iscsi() (
local _dev
@ -869,8 +864,7 @@ get_alias() {
local alias_set
ips=$(hostname -I)
for ip in $ips
do
for ip in $ips; do
# in /etc/hosts, alias can come at the 2nd column
if entries=$(grep "$ip" /etc/hosts | awk '{ $1=""; print $0 }'); then
alias_set="$alias_set $entries"
@ -892,7 +886,7 @@ is_localhost() {
hostnames="$hostnames $shortnames $aliasname"
for name in ${hostnames}; do
if [[ "$name" == "$nodename" ]]; then
if [[ $name == "$nodename" ]]; then
return 0
fi
done
@ -948,7 +942,7 @@ get_generic_fence_kdump_nodes() {
# setup fence_kdump in cluster
# setup proper network and install needed files
kdump_configure_fence_kdump () {
kdump_configure_fence_kdump() {
local kdump_cfg_file=$1
local nodes
local args
@ -963,7 +957,7 @@ kdump_configure_fence_kdump () {
echo "fence_kdump_nodes $nodes" >> "${kdump_cfg_file}"
args=$(get_pcs_fence_kdump_args)
if [[ -n "$args" ]]; then
if [[ -n $args ]]; then
echo "fence_kdump_args $args" >> "${kdump_cfg_file}"
fi
@ -987,7 +981,7 @@ kdump_configure_fence_kdump () {
kdump_install_random_seed() {
local poolsize
poolsize=$(</proc/sys/kernel/random/poolsize)
poolsize=$(< /proc/sys/kernel/random/poolsize)
if [[ ! -d "${initdir}/var/lib/" ]]; then
mkdir -p "${initdir}/var/lib/"
@ -1072,7 +1066,7 @@ install() {
# actually does nothing.
sed -i -e \
's/\(^[[:space:]]*reserved_memory[[:space:]]*=\)[[:space:]]*[[:digit:]]*/\1 1024/' \
"${initdir}/etc/lvm/lvm.conf" &>/dev/null
"${initdir}/etc/lvm/lvm.conf" &> /dev/null
# Save more memory by dropping switch root capability
dracut_no_switch_root

216
kdumpctl
View File

@ -46,7 +46,7 @@ single_instance_lock()
{
local rc timeout=5
if ! exec 9>/var/lock/kdump; then
if ! exec 9> /var/lock/kdump; then
derror "Create file lock failed"
exit 1
fi
@ -90,9 +90,9 @@ save_core()
# https://fedorahosted.org/abrt/
if [[ -x /usr/bin/dumpoops ]]; then
ddebug "makedumpfile --dump-dmesg $coredir/vmcore $coredir/dmesg"
makedumpfile --dump-dmesg "$coredir/vmcore" "$coredir/dmesg" >/dev/null 2>&1
makedumpfile --dump-dmesg "$coredir/vmcore" "$coredir/dmesg" > /dev/null 2>&1
ddebug "dumpoops -d $coredir/dmesg"
if dumpoops -d "$coredir/dmesg" >/dev/null 2>&1; then
if dumpoops -d "$coredir/dmesg" > /dev/null 2>&1; then
dinfo "kernel oops has been collected by abrt tool"
fi
fi
@ -131,7 +131,7 @@ rebuild_kdump_initrd()
rebuild_initrd()
{
if [[ ! -w $(dirname "$TARGET_INITRD") ]];then
if [[ ! -w $(dirname "$TARGET_INITRD") ]]; then
derror "$(dirname "$TARGET_INITRD") does not have write permission. Cannot rebuild $TARGET_INITRD"
return 1
fi
@ -149,7 +149,7 @@ rebuild_initrd()
check_exist()
{
for file in $1; do
if [[ ! -e "$file" ]]; then
if [[ ! -e $file ]]; then
derror "Error: $file not found."
return 1
fi
@ -160,7 +160,7 @@ check_exist()
check_executable()
{
for file in $1; do
if [[ ! -x "$file" ]]; then
if [[ ! -x $file ]]; then
derror "Error: $file is not executable."
return 1
fi
@ -171,7 +171,7 @@ backup_default_initrd()
{
ddebug "backup default initrd: $DEFAULT_INITRD"
if [[ ! -f "$DEFAULT_INITRD" ]]; then
if [[ ! -f $DEFAULT_INITRD ]]; then
return
fi
@ -190,7 +190,7 @@ restore_default_initrd()
{
ddebug "restore default initrd: $DEFAULT_INITRD"
if [[ ! -f "$DEFAULT_INITRD" ]]; then
if [[ ! -f $DEFAULT_INITRD ]]; then
return
fi
@ -200,7 +200,7 @@ restore_default_initrd()
# verify checksum before restoring
backup_checksum=$(sha1sum "$DEFAULT_INITRD_BAK" | awk '{ print $1 }')
default_checksum=$(awk '{ print $1 }' "$INITRD_CHECKSUM_LOCATION")
if [[ "$default_checksum" != "$backup_checksum" ]]; then
if [[ $default_checksum != "$backup_checksum" ]]; then
dwarn "WARNING: checksum mismatch! Can't restore original initrd.."
else
rm -f $INITRD_CHECKSUM_LOCATION
@ -220,7 +220,7 @@ check_config()
dracut_args)
if [[ $config_val == *--mount* ]]; then
if [[ $(echo "$config_val" | grep -o "\-\-mount" | wc -l) -ne 1 ]]; then
derror "Multiple mount targets specified in one \"dracut_args\"."
derror 'Multiple mount targets specified in one "dracut_args".'
return 1
fi
config_opt=_target
@ -232,12 +232,12 @@ check_config()
fi
config_opt=_target
;;
ext[234]|minix|btrfs|xfs|nfs|ssh)
ext[234] | minix | btrfs | xfs | nfs | ssh)
config_opt=_target
;;
sshkey|path|core_collector|kdump_post|kdump_pre|extra_bins|extra_modules|failure_action|default|final_action|force_rebuild|force_no_rebuild|fence_kdump_args|fence_kdump_nodes)
;;
net|options|link_delay|disk_timeout|debug_mem_level|blacklist)
sshkey | path | core_collector | kdump_post | kdump_pre | extra_bins | extra_modules | failure_action | default | final_action | force_rebuild | force_no_rebuild | fence_kdump_args | fence_kdump_nodes) ;;
net | options | link_delay | disk_timeout | debug_mem_level | blacklist)
derror "Deprecated kdump config option: $config_opt. Refer to kdump.conf manpage for alternatives."
return 1
;;
@ -250,12 +250,12 @@ check_config()
;;
esac
if [[ -z "$config_val" ]]; then
if [[ -z $config_val ]]; then
derror "Invalid kdump config value for option '$config_opt'"
return 1
fi
if [[ -n "${_opt_rec[$config_opt]}" ]]; then
if [[ -n ${_opt_rec[$config_opt]} ]]; then
if [[ $config_opt == _target ]]; then
derror "More than one dump targets specified"
else
@ -291,7 +291,7 @@ get_pcs_cluster_modified_files()
if [[ -f $FENCE_KDUMP_CONFIG_FILE ]]; then
time_stamp=$(stat -c "%Y" "$FENCE_KDUMP_CONFIG_FILE")
if [[ "$time_stamp" -gt "$image_time" ]]; then
if [[ $time_stamp -gt $image_time ]]; then
modified_files="$modified_files $FENCE_KDUMP_CONFIG_FILE"
fi
fi
@ -335,14 +335,14 @@ check_files_modified()
HOOKS="/etc/kdump/post.d/ /etc/kdump/pre.d/"
if [[ -d /etc/kdump/post.d ]]; then
for file in /etc/kdump/post.d/*; do
if [[ -x "$file" ]]; then
if [[ -x $file ]]; then
POST_FILES="$POST_FILES $file"
fi
done
fi
if [[ -d /etc/kdump/pre.d ]]; then
for file in /etc/kdump/pre.d/*; do
if [[ -x "$file" ]]; then
if [[ -x $file ]]; then
PRE_FILES="$PRE_FILES $file"
fi
done
@ -359,19 +359,19 @@ check_files_modified()
# Check for any updated extra module
EXTRA_MODULES="$(kdump_get_conf_val extra_modules)"
if [[ -n "$EXTRA_MODULES" ]]; then
if [[ -n $EXTRA_MODULES ]]; then
if [[ -e /lib/modules/$KDUMP_KERNELVER/modules.dep ]]; then
files="$files /lib/modules/$KDUMP_KERNELVER/modules.dep"
fi
for _module in $EXTRA_MODULES; do
if _module_file="$(modinfo --set-version "$KDUMP_KERNELVER" --filename "$_module" 2>/dev/null)"; then
if _module_file="$(modinfo --set-version "$KDUMP_KERNELVER" --filename "$_module" 2> /dev/null)"; then
files="$files $_module_file"
for _dep_modules in $(modinfo -F depends "$_module" | tr ',' ' '); do
files="$files $(modinfo --set-version "$KDUMP_KERNELVER" --filename "$_dep_modules" 2>/dev/null)"
files="$files $(modinfo --set-version "$KDUMP_KERNELVER" --filename "$_dep_modules" 2> /dev/null)"
done
else
# If it's not a module nor builtin, give an error
if ! ( modprobe --set-version "$KDUMP_KERNELVER" --dry-run "$_module" &>/dev/null ); then
if ! (modprobe --set-version "$KDUMP_KERNELVER" --dry-run "$_module" &> /dev/null); then
dwarn "Module $_module not found"
fi
fi
@ -383,15 +383,15 @@ check_files_modified()
check_exist "$files" && check_executable "$EXTRA_BINS" || return 2
for file in $files; do
if [[ -e "$file" ]]; then
if [[ -e $file ]]; then
time_stamp=$(stat -c "%Y" "$file")
if [[ "$time_stamp" -gt "$image_time" ]]; then
if [[ $time_stamp -gt $image_time ]]; then
modified_files="$modified_files $file"
fi
if [[ -L "$file" ]]; then
if [[ -L $file ]]; then
file=$(readlink -m "$file")
time_stamp=$(stat -c "%Y" "$file")
if [[ "$time_stamp" -gt "$image_time" ]]; then
if [[ $time_stamp -gt $image_time ]]; then
modified_files="$modified_files $file"
fi
fi
@ -400,7 +400,7 @@ check_files_modified()
fi
done
if [[ -n "$modified_files" ]]; then
if [[ -n $modified_files ]]; then
dinfo "Detected change(s) in the following file(s): $modified_files"
return 1
fi
@ -414,8 +414,9 @@ check_drivers_modified()
# If it's dump target is on block device, detect the block driver
_target=$(get_block_dump_target)
if [[ -n "$_target" ]]; then
_record_block_drivers() {
if [[ -n $_target ]]; then
_record_block_drivers()
{
local _drivers
_drivers=$(udevadm info -a "/dev/block/$1" | sed -n 's/\s*DRIVERS=="\(\S\+\)"/\1/p')
for _driver in $_drivers; do
@ -431,7 +432,7 @@ check_drivers_modified()
# Include watchdog drivers if watchdog module is not omitted
is_dracut_mod_omitted watchdog || _new_drivers+=" $(get_watchdog_drvs)"
[[ -z "$_new_drivers" ]] && return 0
[[ -z $_new_drivers ]] && return 0
if is_fadump_capable; then
_old_drivers="$(lsinitrd "$TARGET_INITRD" -f /usr/lib/dracut/fadump-kernel-modules.txt | tr '\n' ' ')"
@ -443,9 +444,9 @@ check_drivers_modified()
ddebug "Modules included in old initramfs: '$_old_drivers'"
for _driver in $_new_drivers; do
# Skip deprecated/invalid driver name or built-in module
_module_name=$(modinfo --set-version "$KDUMP_KERNELVER" -F name "$_driver" 2>/dev/null)
_module_filename=$(modinfo --set-version "$KDUMP_KERNELVER" -n "$_driver" 2>/dev/null)
if [[ -z "$_module_name" ]] || [[ -z "$_module_filename" ]] || [[ "$_module_filename" = *"(builtin)"* ]]; then
_module_name=$(modinfo --set-version "$KDUMP_KERNELVER" -F name "$_driver" 2> /dev/null)
_module_filename=$(modinfo --set-version "$KDUMP_KERNELVER" -n "$_driver" 2> /dev/null)
if [[ -z $_module_name ]] || [[ -z $_module_filename ]] || [[ $_module_filename == *"(builtin)"* ]]; then
continue
fi
if ! [[ " $_old_drivers " == *" $_module_name "* ]]; then
@ -474,21 +475,21 @@ check_fs_modified()
_target=$(get_block_dump_target)
_new_fstype=$(get_fs_type_from_target "$_target")
if [[ -z "$_target" ]] || [[ -z "$_new_fstype" ]];then
if [[ -z $_target ]] || [[ -z $_new_fstype ]]; then
derror "Dump target is invalid"
return 2
fi
ddebug "_target=$_target _new_fstype=$_new_fstype"
_new_dev=$(kdump_get_persistent_dev "$_target")
if [[ -z "$_new_dev" ]]; then
if [[ -z $_new_dev ]]; then
perror "Get persistent device name failed"
return 2
fi
_new_mntpoint="$(get_kdump_mntpoint_from_target "$_target")"
_dracut_args=$(lsinitrd "$TARGET_INITRD" -f usr/lib/dracut/build-parameter.txt)
if [[ -z "$_dracut_args" ]];then
if [[ -z $_dracut_args ]]; then
dwarn "Warning: No dracut arguments found in initrd"
return 0
fi
@ -501,10 +502,10 @@ check_fs_modified()
_old_dev=$1
_old_mntpoint=$2
_old_fstype=$3
[[ $_new_dev = "$_old_dev" && $_new_mntpoint = "$_old_mntpoint" && $_new_fstype = "$_old_fstype" ]] && return 0
[[ $_new_dev == "$_old_dev" && $_new_mntpoint == "$_old_mntpoint" && $_new_fstype == "$_old_fstype" ]] && return 0
# otherwise rebuild if target device is not a root device
else
[[ "$_target" = "$(get_root_fs_device)" ]] && return 0
[[ $_target == "$(get_root_fs_device)" ]] && return 0
fi
dinfo "Detected change in File System"
@ -551,36 +552,36 @@ check_rebuild()
force_no_rebuild=$(kdump_get_conf_val force_no_rebuild)
force_no_rebuild=${force_no_rebuild:-0}
if [[ "$force_no_rebuild" != "0" ]] && [[ "$force_no_rebuild" != "1" ]];then
if [[ $force_no_rebuild != "0" ]] && [[ $force_no_rebuild != "1" ]]; then
derror "Error: force_no_rebuild value is invalid"
return 1
fi
force_rebuild=$(kdump_get_conf_val force_rebuild)
force_rebuild=${force_rebuild:-0}
if [[ "$force_rebuild" != "0" ]] && [[ "$force_rebuild" != "1" ]];then
if [[ $force_rebuild != "0" ]] && [[ $force_rebuild != "1" ]]; then
derror "Error: force_rebuild value is invalid"
return 1
fi
if [[ "$force_no_rebuild" == "1" && "$force_rebuild" == "1" ]]; then
if [[ $force_no_rebuild == "1" && $force_rebuild == "1" ]]; then
derror "Error: force_rebuild and force_no_rebuild are enabled simultaneously in kdump.conf"
return 1
fi
# Will not rebuild kdump initrd
if [[ "$force_no_rebuild" == "1" ]]; then
if [[ $force_no_rebuild == "1" ]]; then
return 0
fi
#check to see if dependent files has been modified
#since last build of the image file
if [[ -f $TARGET_INITRD ]]; then
image_time=$(stat -c "%Y" "$TARGET_INITRD" 2>/dev/null)
image_time=$(stat -c "%Y" "$TARGET_INITRD" 2> /dev/null)
#in case of fadump mode, check whether the default/target
#initrd is already built with dump capture capability
if [[ "$DEFAULT_DUMP_MODE" == "fadump" ]]; then
if [[ $DEFAULT_DUMP_MODE == "fadump" ]]; then
capture_capable_initrd=$(lsinitrd -f $DRACUT_MODULES_FILE "$TARGET_INITRD" | grep -c -e ^kdumpbase$ -e ^zz-fadumpinit$)
fi
fi
@ -589,17 +590,17 @@ check_rebuild()
ret=$?
if [[ $ret -eq 2 ]]; then
return 1
elif [[ $ret -eq 1 ]];then
elif [[ $ret -eq 1 ]]; then
system_modified="1"
fi
if [[ $image_time -eq 0 ]]; then
dinfo "No kdump initial ramdisk found."
elif [[ "$capture_capable_initrd" == "0" ]]; then
elif [[ $capture_capable_initrd == "0" ]]; then
dinfo "Rebuild $TARGET_INITRD with dump capture support"
elif [[ "$force_rebuild" != "0" ]]; then
elif [[ $force_rebuild != "0" ]]; then
dinfo "Force rebuild $TARGET_INITRD"
elif [[ "$system_modified" != "0" ]]; then
elif [[ $system_modified != "0" ]]; then
:
else
return 0
@ -665,7 +666,7 @@ check_ssh_config()
case "$config_opt" in
sshkey)
# remove inline comments after the end of a directive.
if [[ -f "$config_val" ]]; then
if [[ -f $config_val ]]; then
# canonicalize the path
SSH_KEY_LOCATION=$(/usr/bin/readlink -m "$config_val")
else
@ -678,14 +679,14 @@ check_ssh_config()
ssh)
DUMP_TARGET=$config_val
;;
*)
;;
*) ;;
esac
done <<< "$(kdump_read_conf)"
#make sure they've configured kdump.conf for ssh dumps
SSH_TARGET=$(echo -n "$DUMP_TARGET" | sed -n '/.*@/p')
if [[ -z "$SSH_TARGET" ]]; then
if [[ -z $SSH_TARGET ]]; then
return 1
fi
return 0
@ -732,7 +733,7 @@ check_and_wait_network_ready()
diff=$((cur - start_time))
# 60s time out
if [[ $diff -gt 180 ]]; then
break;
break
fi
sleep 1
done
@ -786,7 +787,7 @@ show_reserved_mem()
local mem
local mem_mb
mem=$(</sys/kernel/kexec_crash_size)
mem=$(< /sys/kernel/kexec_crash_size)
mem_mb=$((mem / 1024 / 1024))
dinfo "Reserved ${mem_mb}MB memory for crash kernel"
@ -795,7 +796,7 @@ show_reserved_mem()
check_current_fadump_status()
{
# Check if firmware-assisted dump has been registered.
rc=$(<$FADUMP_REGISTER_SYS_NODE)
rc=$(< $FADUMP_REGISTER_SYS_NODE)
[[ $rc -eq 1 ]] && return 0
return 1
}
@ -817,8 +818,8 @@ save_raw()
local raw_target
raw_target=$(kdump_get_conf_val raw)
[[ -z "$raw_target" ]] && return 0
[[ -b "$raw_target" ]] || {
[[ -z $raw_target ]] && return 0
[[ -b $raw_target ]] || {
derror "raw partition $raw_target not found"
return 1
}
@ -828,22 +829,22 @@ save_raw()
return 0
fi
kdump_dir=$(kdump_get_conf_val path)
if [[ -z "${kdump_dir}" ]]; then
if [[ -z ${kdump_dir} ]]; then
coredir="/var/crash/$(date +"%Y-%m-%d-%H:%M")"
else
coredir="${kdump_dir}/$(date +"%Y-%m-%d-%H:%M")"
fi
mkdir -p "$coredir"
[[ -d "$coredir" ]] || {
[[ -d $coredir ]] || {
derror "failed to create $coredir"
return 1
}
if makedumpfile -R "$coredir/vmcore" < "$raw_target" >/dev/null 2>&1; then
if makedumpfile -R "$coredir/vmcore" < "$raw_target" > /dev/null 2>&1; then
# dump found
dinfo "Dump saved to $coredir/vmcore"
# wipe makedumpfile header
dd if=/dev/zero of="$raw_target" bs=1b count=1 2>/dev/null
dd if=/dev/zero of="$raw_target" bs=1b count=1 2> /dev/null
else
rm -rf "$coredir"
fi
@ -866,11 +867,11 @@ path_to_be_relabeled()
if is_user_configured_dump_target; then
if is_mount_in_dracut_args; then
return;
return
fi
_target=$(local_fs_dump_target)
if [[ -n "$_target" ]]; then
if [[ -n $_target ]]; then
_mnt=$(get_mntpoint_from_target "$_target")
if ! is_mounted "$_mnt"; then
return
@ -882,8 +883,8 @@ path_to_be_relabeled()
_path=$(get_save_path)
# if $_path is masked by other mount, we will not relabel it.
_rmnt=$(df "$_mnt/$_path" 2>/dev/null | tail -1 | awk '{ print $NF }')
if [[ "$_rmnt" == "$_mnt" ]]; then
_rmnt=$(df "$_mnt/$_path" 2> /dev/null | tail -1 | awk '{ print $NF }')
if [[ $_rmnt == "$_mnt" ]]; then
echo "$_mnt/$_path"
fi
}
@ -893,14 +894,14 @@ selinux_relabel()
local _path _i _attr
_path=$(path_to_be_relabeled)
if [[ -z "$_path" ]] || ! [[ -d "$_path" ]] ; then
if [[ -z $_path ]] || ! [[ -d $_path ]]; then
return
fi
while IFS= read -r -d '' _i; do
_attr=$(getfattr -m "security.selinux" "$_i" 2>/dev/null)
if [[ -z "$_attr" ]]; then
restorecon "$_i";
_attr=$(getfattr -m "security.selinux" "$_i" 2> /dev/null)
if [[ -z $_attr ]]; then
restorecon "$_i"
fi
done < <(find "$_path" -print0)
}
@ -916,7 +917,7 @@ check_fence_kdump_config()
nodes=$(kdump_get_conf_val "fence_kdump_nodes")
for node in $nodes; do
if [[ "$node" = "$hostname" ]]; then
if [[ $node == "$hostname" ]]; then
derror "Option fence_kdump_nodes cannot contain $hostname"
return 1
fi
@ -972,25 +973,26 @@ check_failure_action_config()
default_option=$(kdump_get_conf_val default)
failure_action=$(kdump_get_conf_val failure_action)
if [[ -z "$failure_action" ]] && [[ -z "$default_option" ]]; then
if [[ -z $failure_action ]] && [[ -z $default_option ]]; then
return 0
elif [[ -n "$failure_action" ]] && [[ -n "$default_option" ]]; then
elif [[ -n $failure_action ]] && [[ -n $default_option ]]; then
derror "Cannot specify 'failure_action' and 'default' option together"
return 1
fi
if [[ -n "$default_option" ]]; then
if [[ -n $default_option ]]; then
option="default"
failure_action="$default_option"
fi
case "$failure_action" in
reboot|halt|poweroff|shell|dump_to_rootfs)
reboot | halt | poweroff | shell | dump_to_rootfs)
return 0
;;
*)
dinfo $"Usage kdump.conf: $option {reboot|halt|poweroff|shell|dump_to_rootfs}"
return 1
;;
esac
}
@ -999,16 +1001,17 @@ check_final_action_config()
local final_action
final_action=$(kdump_get_conf_val final_action)
if [[ -z "$final_action" ]]; then
if [[ -z $final_action ]]; then
return 0
else
case "$final_action" in
reboot|halt|poweroff)
reboot | halt | poweroff)
return 0
;;
*)
dinfo $"Usage kdump.conf: final_action {reboot|halt|poweroff}"
return 1
;;
esac
fi
}
@ -1025,7 +1028,7 @@ start()
return 1
fi
if sestatus 2>/dev/null | grep -q "SELinux status.*enabled"; then
if sestatus 2> /dev/null | grep -q "SELinux status.*enabled"; then
selinux_relabel
fi
@ -1156,7 +1159,8 @@ stop()
return 0
}
rebuild() {
rebuild()
{
check_config || return 1
if check_ssh_config; then
@ -1172,33 +1176,34 @@ rebuild() {
return $?
}
do_estimate() {
do_estimate()
{
local kdump_mods
local -A large_mods
local baseline
local kernel_size mod_size initrd_size baseline_size runtime_size reserved_size estimated_size recommended_size
local size_mb=$(( 1024 * 1024 ))
local size_mb=$((1024 * 1024))
setup_initrd
if [[ ! -f "$TARGET_INITRD" ]]; then
if [[ ! -f $TARGET_INITRD ]]; then
derror "kdumpctl estimate: kdump initramfs is not built yet."
exit 1
fi
kdump_mods="$(lsinitrd "$TARGET_INITRD" -f /usr/lib/dracut/hostonly-kernel-modules.txt | tr '\n' ' ')"
baseline=$(kdump_get_arch_recommend_size)
if [[ "${baseline: -1}" == "M" ]]; then
if [[ ${baseline: -1} == "M" ]]; then
baseline=${baseline%M}
elif [[ "${baseline: -1}" == "G" ]]; then
baseline=$(( ${baseline%G} * 1024 ))
elif [[ "${baseline: -1}" == "T" ]]; then
baseline=$(( ${baseline%Y} * 1048576 ))
elif [[ ${baseline: -1} == "G" ]]; then
baseline=$((${baseline%G} * 1024))
elif [[ ${baseline: -1} == "T" ]]; then
baseline=$((${baseline%Y} * 1048576))
fi
# The default pre-reserved crashkernel value
baseline_size=$((baseline * size_mb))
# Current reserved crashkernel size
reserved_size=$(</sys/kernel/kexec_crash_size)
reserved_size=$(< /sys/kernel/kexec_crash_size)
# A pre-estimated value for userspace usage and kernel
# runtime allocation, 64M should good for most cases
runtime_size=$((64 * size_mb))
@ -1209,7 +1214,7 @@ do_estimate() {
# Kernel modules static size after loaded
mod_size=0
while read -r _name _size _; do
if [[ ! " $kdump_mods " == *" $_name "* ]]; then
if [[ " $kdump_mods " != *" $_name "* ]]; then
continue
fi
mod_size=$((mod_size + _size))
@ -1224,8 +1229,8 @@ do_estimate() {
crypt_size=0
for _dev in $(get_all_kdump_crypt_dev); do
_crypt_info=$(cryptsetup luksDump "/dev/block/$_dev")
[[ $(echo "$_crypt_info" | sed -n "s/^Version:\s*\(.*\)/\1/p" ) == "2" ]] || continue
for _mem in $(echo "$_crypt_info" | sed -n "s/\sMemory:\s*\(.*\)/\1/p" | sort -n ); do
[[ $(echo "$_crypt_info" | sed -n "s/^Version:\s*\(.*\)/\1/p") == "2" ]] || continue
for _mem in $(echo "$_crypt_info" | sed -n "s/\sMemory:\s*\(.*\)/\1/p" | sort -n); do
crypt_size=$((crypt_size + _mem * 1024))
break
done
@ -1246,10 +1251,10 @@ do_estimate() {
echo "Kernel modules size: $((mod_size / size_mb))M"
echo "Initramfs size: $((initrd_size / size_mb))M"
echo "Runtime reservation: $((runtime_size / size_mb))M"
[[ $crypt_size -ne 0 ]] && \
[[ $crypt_size -ne 0 ]] &&
echo "LUKS required size: $((crypt_size / size_mb))M"
echo -n "Large modules:"
if [[ "${#large_mods[@]}" -eq 0 ]]; then
if [[ ${#large_mods[@]} -eq 0 ]]; then
echo " <none>"
else
echo ""
@ -1263,14 +1268,15 @@ do_estimate() {
fi
}
reset_crashkernel() {
reset_crashkernel()
{
local kernel=$1 entry crashkernel_default
local grub_etc_default="/etc/default/grub"
[[ -z "$kernel" ]] && kernel=$(uname -r)
crashkernel_default=$(cat "/usr/lib/modules/$kernel/crashkernel.default" 2>/dev/null)
[[ -z $kernel ]] && kernel=$(uname -r)
crashkernel_default=$(cat "/usr/lib/modules/$kernel/crashkernel.default" 2> /dev/null)
if [[ -z "$crashkernel_default" ]]; then
if [[ -z $crashkernel_default ]]; then
derror "$kernel doesn't have a crashkernel.default"
exit 1
fi
@ -1287,7 +1293,7 @@ reset_crashkernel() {
entry=${entry#\"}
entry=${entry%\"}
if [[ -f "$grub_etc_default" ]]; then
if [[ -f $grub_etc_default ]]; then
sed -i -e "s/^\(GRUB_CMDLINE_LINUX=.*\)crashkernel=[^\ \"]*\([\ \"].*\)$/\1$crashkernel_default\2/" "$grub_etc_default"
fi
@ -1297,12 +1303,12 @@ reset_crashkernel() {
fi
}
if [[ ! -f "$KDUMP_CONFIG_FILE" ]]; then
if [[ ! -f $KDUMP_CONFIG_FILE ]]; then
derror "Error: No kdump config file found!"
exit 1
fi
main ()
main()
{
# Determine if the dump mode is kdump or fadump
determine_dump_mode
@ -1344,8 +1350,8 @@ main ()
rebuild)
rebuild
;;
condrestart)
;;
condrestart) ;;
propagate)
propagate_ssh_key
;;
@ -1361,6 +1367,7 @@ main ()
*)
dinfo $"Usage: $0 {estimate|start|stop|status|restart|reload|rebuild|reset-crashkernel|propagate|showmem}"
exit 1
;;
esac
}
@ -1369,6 +1376,9 @@ single_instance_lock
# To avoid fd 9 leaking, we invoke a subshell, close fd 9 and call main.
# So that fd isn't leaking when main is invoking a subshell.
(exec 9<&-; main "$@")
(
exec 9<&-
main "$@"
)
exit $?

View File

@ -27,7 +27,7 @@ SAVE_PATH=$(get_save_path)
OVERRIDE_RESETTABLE=0
extra_modules=""
dracut_args=( --add kdumpbase --quiet --hostonly --hostonly-cmdline --hostonly-i18n --hostonly-mode strict -o "plymouth dash resume ifcfg earlykdump" )
dracut_args=(--add kdumpbase --quiet --hostonly --hostonly-cmdline --hostonly-i18n --hostonly-mode strict -o "plymouth dash resume ifcfg earlykdump")
MKDUMPRD_TMPDIR="$(mktemp -d -t mkdumprd.XXXXXX)"
[ -d "$MKDUMPRD_TMPDIR" ] || perror_exit "dracut: mktemp -p -d -t dracut.XXXXXX failed."
@ -43,20 +43,24 @@ trap '
# clean up after ourselves no matter how we die.
trap 'exit 1;' SIGINT
add_dracut_arg() {
dracut_args+=( "$@" )
add_dracut_arg()
{
dracut_args+=("$@")
}
add_dracut_mount() {
add_dracut_mount()
{
add_dracut_arg "--mount" "$1"
}
add_dracut_sshkey() {
add_dracut_sshkey()
{
add_dracut_arg "--sshkey" "$1"
}
# caller should ensure $1 is valid and mounted in 1st kernel
to_mount() {
to_mount()
{
local _target=$1 _fstype=$2 _options=$3 _sed_cmd _new_mntpoint _pdev
_new_mntpoint=$(get_kdump_mntpoint_from_target "$_target")
@ -64,7 +68,7 @@ to_mount() {
_options="${_options:-$(get_mntopt_from_target "$_target")}"
_options="${_options:-defaults}"
if [[ "$_fstype" == "nfs"* ]]; then
if [[ $_fstype == "nfs"* ]]; then
_pdev=$_target
_sed_cmd+='s/,addr=[^,]*//;'
_sed_cmd+='s/,proto=[^,]*//;'
@ -94,7 +98,8 @@ to_mount() {
#Function: get_ssh_size
#$1=dump target
#called from while loop and shouldn't read from stdin, so we're using "ssh -n"
get_ssh_size() {
get_ssh_size()
{
local _out
local _opt=("-i" "$SSH_KEY_LOCATION" "-o" "BatchMode=yes" "-o" "StrictHostKeyChecking=yes")
@ -113,11 +118,11 @@ mkdir_save_path_ssh()
{
local _opt _dir
_opt=(-i "$SSH_KEY_LOCATION" -o BatchMode=yes -o StrictHostKeyChecking=yes)
ssh -qn "${_opt[@]}" "$1" mkdir -p "$SAVE_PATH" &>/dev/null || \
ssh -qn "${_opt[@]}" "$1" mkdir -p "$SAVE_PATH" &> /dev/null ||
perror_exit "mkdir failed on $1:$SAVE_PATH"
# check whether user has write permission on $1:$SAVE_PATH
_dir=$(ssh -qn "${_opt[@]}" "$1" mktemp -dqp "$SAVE_PATH" 2>/dev/null) || \
_dir=$(ssh -qn "${_opt[@]}" "$1" mktemp -dqp "$SAVE_PATH" 2> /dev/null) ||
perror_exit "Could not create temporary directory on $1:$SAVE_PATH. Make sure user has write permission on destination"
ssh -qn "${_opt[@]}" "$1" rmdir "$_dir"
@ -126,20 +131,23 @@ mkdir_save_path_ssh()
#Function: get_fs_size
#$1=dump target
get_fs_size() {
get_fs_size()
{
df --output=avail "$(get_mntpoint_from_target "$1")/$SAVE_PATH" | tail -1
}
#Function: get_raw_size
#$1=dump target
get_raw_size() {
get_raw_size()
{
fdisk -s "$1"
}
#Function: check_size
#$1: dump type string ('raw', 'fs', 'ssh')
#$2: dump target
check_size() {
check_size()
{
local avail memtotal
memtotal=$(awk '/MemTotal/{print $2}' /proc/meminfo)
@ -155,9 +163,10 @@ check_size() {
;;
*)
return
;;
esac || perror_exit "Check dump target size failed"
if [[ "$avail" -lt "$memtotal" ]]; then
if [[ $avail -lt $memtotal ]]; then
dwarn "Warning: There might not be enough space to save a vmcore."
dwarn " The size of $2 should be greater than $memtotal kilo bytes."
fi
@ -179,13 +188,13 @@ mount_failure()
local _fstype=$3
local msg="Failed to mount $_target"
if [[ -n "$_mnt" ]]; then
if [[ -n $_mnt ]]; then
msg="$msg on $_mnt"
fi
msg="$msg for kdump preflight check."
if [[ $_fstype = "nfs" ]]; then
if [[ $_fstype == "nfs" ]]; then
msg="$msg Please make sure nfs-utils has been installed."
fi
@ -201,11 +210,11 @@ check_user_configured_target()
_opt=$(get_mntopt_from_target "$_target")
_fstype=$(get_fs_type_from_target "$_target")
if [[ -n "$_fstype" ]]; then
if [[ -n $_fstype ]]; then
# In case of nfs4, nfs should be used instead, nfs* options is deprecated in kdump.conf
[[ $_fstype = "nfs"* ]] && _fstype=nfs
[[ $_fstype == "nfs"* ]] && _fstype=nfs
if [[ -n "$_cfg_fs_type" ]] && [[ "$_fstype" != "$_cfg_fs_type" ]]; then
if [[ -n $_cfg_fs_type ]] && [[ $_fstype != "$_cfg_fs_type" ]]; then
perror_exit "\"$_target\" have a wrong type config \"$_cfg_fs_type\", expected \"$_fstype\""
fi
else
@ -215,9 +224,9 @@ check_user_configured_target()
# For noauto mount, mount it inplace with default value.
# Else use the temporary target directory
if [[ -n "$_mnt" ]]; then
if [[ -n $_mnt ]]; then
if ! is_mounted "$_mnt"; then
if [[ $_opt = *",noauto"* ]]; then
if [[ $_opt == *",noauto"* ]]; then
mount "$_mnt" || mount_failure "$_target" "$_mnt" "$_fstype"
_mounted=$_mnt
else
@ -239,17 +248,18 @@ check_user_configured_target()
check_size fs "$_target"
# Unmount it early, if function is interrupted and didn't reach here, the shell trap will clear it up anyway
if [[ -n "$_mounted" ]]; then
if [[ -n $_mounted ]]; then
umount -f -- "$_mounted"
fi
}
# $1: core_collector config value
verify_core_collector() {
verify_core_collector()
{
local _cmd="${1%% *}"
local _params="${1#* }"
if [[ "$_cmd" != "makedumpfile" ]]; then
if [[ $_cmd != "makedumpfile" ]]; then
if is_raw_dump_target; then
dwarn "Warning: specifying a non-makedumpfile core collector, you will have to recover the vmcore manually."
fi
@ -258,7 +268,7 @@ verify_core_collector() {
if is_ssh_dump_target || is_raw_dump_target; then
if ! strstr "$_params" "-F"; then
perror_exit "The specified dump target needs makedumpfile \"-F\" option."
perror_exit 'The specified dump target needs makedumpfile "-F" option.'
fi
_params="$_params vmcore"
else
@ -271,7 +281,8 @@ verify_core_collector() {
fi
}
add_mount() {
add_mount()
{
local _mnt
_mnt=$(to_mount "$@") || exit 1
@ -304,7 +315,7 @@ for_each_block_target()
local dev majmin
for dev in $(get_kdump_targets); do
[[ -b "$dev" ]] || continue
[[ -b $dev ]] || continue
majmin=$(get_maj_min "$dev")
check_block_and_slaves "$1" "$majmin" && return 1
done
@ -319,9 +330,9 @@ is_unresettable()
local path device resettable=1
path="/sys/$(udevadm info --query=all --path="/sys/dev/block/$1" | awk '/^P:/ {print $2}' | sed -e 's/\(cciss[0-9]\+\/\).*/\1/g' -e 's/\/block\/.*$//')/resettable"
if [[ -f "$path" ]]; then
resettable="$(<"$path")"
[[ $resettable -eq 0 ]] && [[ "$OVERRIDE_RESETTABLE" -eq 0 ]] && {
if [[ -f $path ]]; then
resettable="$(< "$path")"
[[ $resettable -eq 0 ]] && [[ $OVERRIDE_RESETTABLE -eq 0 ]] && {
device=$(udevadm info --query=all --path="/sys/dev/block/$1" | awk -F= '/DEVNAME/{print $2}')
derror "Error: Can not save vmcore because device $device is unresettable"
return 0
@ -339,7 +350,7 @@ check_resettable()
_override_resettable=$(kdump_get_conf_val override_resettable)
OVERRIDE_RESETTABLE=${_override_resettable:-$OVERRIDE_RESETTABLE}
if [ "$OVERRIDE_RESETTABLE" != "0" ] && [ "$OVERRIDE_RESETTABLE" != "1" ];then
if [ "$OVERRIDE_RESETTABLE" != "0" ] && [ "$OVERRIDE_RESETTABLE" != "1" ]; then
perror_exit "override_resettable value '$OVERRIDE_RESETTABLE' is invalid"
fi
@ -369,19 +380,18 @@ fi
# firstly get right SSH_KEY_LOCATION
keyfile=$(kdump_get_conf_val sshkey)
if [[ -f "$keyfile" ]]; then
if [[ -f $keyfile ]]; then
# canonicalize the path
SSH_KEY_LOCATION=$(/usr/bin/readlink -m "$keyfile")
fi
while read -r config_opt config_val;
do
while read -r config_opt config_val; do
# remove inline comments after the end of a directive.
case "$config_opt" in
extra_modules)
extra_modules="$extra_modules $config_val"
;;
ext[234]|xfs|btrfs|minix|nfs)
ext[234] | xfs | btrfs | minix | nfs)
check_user_configured_target "$config_val" "$config_opt"
add_mount "$config_val" "$config_opt"
;;
@ -398,8 +408,7 @@ do
check_size raw "$config_val"
;;
ssh)
if strstr "$config_val" "@";
then
if strstr "$config_val" "@"; then
mkdir_save_path_ssh "$config_val"
check_size ssh "$config_val"
add_dracut_sshkey "$SSH_KEY_LOCATION"
@ -415,15 +424,14 @@ do
add_dracut_arg "$dracut_arg"
done <<< "$(echo "$config_val" | xargs -n 1 echo)"
;;
*)
;;
*) ;;
esac
done <<< "$(kdump_read_conf)"
handle_default_dump_target
if [[ -n "$extra_modules" ]]
then
if [[ -n $extra_modules ]]; then
add_dracut_arg "--add-drivers" "$extra_modules"
fi

View File

@ -44,22 +44,22 @@ fi
### Unpack the initramfs having dump capture capability
mkdir -p "$MKFADUMPRD_TMPDIR/fadumproot"
if ! (pushd "$MKFADUMPRD_TMPDIR/fadumproot" > /dev/null && lsinitrd --unpack "$FADUMP_INITRD" && \
if ! (pushd "$MKFADUMPRD_TMPDIR/fadumproot" > /dev/null && lsinitrd --unpack "$FADUMP_INITRD" &&
popd > /dev/null); then
derror "mkfadumprd: failed to unpack '$MKFADUMPRD_TMPDIR'"
exit 1
fi
### Pack it into the normal boot initramfs with zz-fadumpinit module
_dracut_isolate_args=(\
--rebuild "$REBUILD_INITRD" --add zz-fadumpinit \
_dracut_isolate_args=(
--rebuild "$REBUILD_INITRD" --add zz-fadumpinit
-i "$MKFADUMPRD_TMPDIR/fadumproot" /fadumproot
-i "$MKFADUMPRD_TMPDIR/fadumproot/usr/lib/dracut/hostonly-kernel-modules.txt"
/usr/lib/dracut/fadump-kernel-modules.txt
)
if is_squash_available; then
_dracut_isolate_args+=( --add squash )
_dracut_isolate_args+=(--add squash)
fi
if ! dracut --force --quiet "${_dracut_isolate_args[@]}" "$@" "$TARGET_INITRD"; then