kdump-lib.sh: reformat with shfmt

upstream: fedora
resolves: bz2003832
conflict:
    Regenerated with shfmt command because of
    too much hunk patches. When comparing with the original
    patch, only patch "kdumpctl: enable secure boot on ppc64le LPARs"
    related hunks are different.

commit 4cdce1f489
Author: Kairui Song <kasong@redhat.com>
Date:   Tue Sep 14 03:09:30 2021 +0800

    kdump-lib.sh: reformat with shfmt

    This is a batch update done with:
    shfmt -s -w kdump-lib.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 22:38:12 +08:00
parent 040a2e259f
commit d53e0655f1

View File

@ -20,9 +20,10 @@ is_fadump_capable()
return 1
}
is_squash_available() {
is_squash_available()
{
for kmodule in squashfs overlay loop; do
if [[ -z "$KDUMP_KERNELVER" ]]; then
if [[ -z $KDUMP_KERNELVER ]]; then
modprobe --dry-run $kmodule &> /dev/null || return 1
else
modprobe -S "$KDUMP_KERNELVER" --dry-run $kmodule &> /dev/null || return 1
@ -30,7 +31,8 @@ is_squash_available() {
done
}
perror_exit() {
perror_exit()
{
derror "$@"
exit 1
}
@ -54,7 +56,8 @@ is_generic_fence_kdump()
[[ $(kdump_get_conf_val fence_kdump_nodes) ]]
}
to_dev_name() {
to_dev_name()
{
local dev="${1//\"/}"
case "$dev" in
@ -66,6 +69,7 @@ to_dev_name() {
;;
*)
echo "$dev"
;;
esac
}
@ -79,10 +83,10 @@ get_user_configured_dump_disk()
local _target
_target=$(kdump_get_conf_val "ext[234]\|xfs\|btrfs\|minix\|raw")
[[ -n "$_target" ]] && echo "$_target" && return
[[ -n $_target ]] && echo "$_target" && return
_target=$(get_dracut_args_target "$(kdump_get_conf_val "dracut_args")")
[[ -b "$_target" ]] && echo "$_target"
[[ -b $_target ]] && echo "$_target"
}
get_block_dump_target()
@ -94,12 +98,12 @@ get_block_dump_target()
fi
_target=$(get_user_configured_dump_disk)
[[ -n "$_target" ]] && to_dev_name "$_target" && return
[[ -n $_target ]] && to_dev_name "$_target" && return
# Get block device name from local save path
_path=$(get_save_path)
_target=$(get_target_from_path "$_path")
[[ -b "$_target" ]] && to_dev_name "$_target"
[[ -b $_target ]] && to_dev_name "$_target"
}
is_dump_to_rootfs()
@ -114,7 +118,7 @@ get_failure_action_target()
if is_dump_to_rootfs; then
# Get rootfs device name
_target=$(get_root_fs_device)
[[ -b "$_target" ]] && to_dev_name "$_target" && return
[[ -b $_target ]] && to_dev_name "$_target" && return
# Then, must be nfs root
echo "nfs"
fi
@ -127,7 +131,7 @@ get_kdump_targets()
local kdump_targets
_target=$(get_block_dump_target)
if [[ -n "$_target" ]]; then
if [[ -n $_target ]]; then
kdump_targets=$_target
elif is_ssh_dump_target; then
kdump_targets="ssh"
@ -137,7 +141,7 @@ get_kdump_targets()
# Add the root device if dump_to_rootfs is specified.
_root=$(get_failure_action_target)
if [[ -n "$_root" ]] && [[ "$kdump_targets" != "$_root" ]]; then
if [[ -n $_root ]] && [[ $kdump_targets != "$_root" ]]; then
kdump_targets="$kdump_targets $_root"
fi
@ -169,13 +173,13 @@ get_bind_mount_source()
_fstype=$(get_mount_info FSTYPE target "$_mnt" -f)
# bind mount in fstab
if [[ -d "$_src" ]] && [[ "$_fstype" = none ]] && (echo "$_opt" | grep -q "\bbind\b"); then
if [[ -d $_src ]] && [[ $_fstype == none ]] && (echo "$_opt" | grep -q "\bbind\b"); then
echo "$_src$_path" && return
fi
# direct mount
_src_nofsroot=$(get_mount_info SOURCE target "$_mnt" -v -f)
if [[ $_src_nofsroot = "$_src" ]]; then
if [[ $_src_nofsroot == "$_src" ]]; then
echo "$_mnt$_path" && return
fi
@ -184,7 +188,7 @@ get_bind_mount_source()
_mnt=$(get_mount_info TARGET source "$_src_nofsroot" -f)
# for btrfs, _fsroot will also contain the subvol value as well, strip it
if [[ "$_fstype" = btrfs ]]; then
if [[ $_fstype == btrfs ]]; then
local _subvol
_subvol=${_opt#*subvol=}
_subvol=${_subvol%,*}
@ -209,10 +213,10 @@ get_kdump_mntpoint_from_target()
# mount under /kdumproot if dump target is not mounted in first kernel
# mount under /kdumproot/$_mntpoint in other cases in 2nd kernel.
# systemd will be in charge to umount it.
if [[ -z "$_mntpoint" ]];then
if [[ -z $_mntpoint ]]; then
_mntpoint="/kdumproot"
else
if [[ "$_mntpoint" = "/" ]];then
if [[ $_mntpoint == "/" ]]; then
_mntpoint="/sysroot"
else
_mntpoint="/kdumproot/$_mntpoint"
@ -223,7 +227,8 @@ get_kdump_mntpoint_from_target()
echo $_mntpoint | tr -s "/"
}
kdump_get_persistent_dev() {
kdump_get_persistent_dev()
{
local dev="${1//\"/}"
case "$dev" in
@ -261,7 +266,7 @@ is_hostname()
local _hostname
_hostname=$(echo "$1" | grep ":")
if [[ -n "$_hostname" ]]; then
if [[ -n $_hostname ]]; then
return 1
fi
echo "$1" | grep -q "[a-zA-Z]"
@ -273,13 +278,12 @@ get_hwaddr()
if [[ -f "/sys/class/net/$1/address" ]]; then
awk '{ print toupper($0) }' < "/sys/class/net/$1/address"
elif [[ -d "/sys/class/net/$1" ]]; then
LC_ALL="" LANG="" ip -o link show "$1" 2>/dev/null | \
LC_ALL="" LANG="" ip -o link show "$1" 2> /dev/null |
awk '{ print toupper(gensub(/.*link\/[^ ]* ([[:alnum:]:]*).*/,
"\\1", 1)); }'
fi
}
# Get value by a field using "nmcli -g"
# Usage: get_nmcli_value_by_field <field> <nmcli command>
#
@ -341,13 +345,13 @@ get_ifcfg_by_name()
is_nm_running()
{
[[ "$(LANG=C nmcli -t --fields running general status 2>/dev/null)" = "running" ]]
[[ "$(LANG=C nmcli -t --fields running general status 2> /dev/null)" == "running" ]]
}
is_nm_handling()
{
LANG=C nmcli -t --fields device,state dev status 2>/dev/null \
| grep -q "^\(${1}:connected\)\|\(${1}:connecting.*\)$"
LANG=C nmcli -t --fields device,state dev status 2> /dev/null |
grep -q "^\(${1}:connected\)\|\(${1}:connecting.*\)$"
}
# $1: netdev name
@ -360,12 +364,12 @@ get_ifcfg_nmcli()
if is_nm_running && is_nm_handling "${1}"; then
# The configuration "uuid" and "name" generated by nm is wrote to
# the ifcfg file as "UUID=<nm_uuid>" and "NAME=<nm_name>".
nm_uuid=$(LANG=C nmcli -t --fields uuid,device c show --active 2>/dev/null \
| grep "${1}" | head -1 | cut -d':' -f1)
nm_name=$(LANG=C nmcli -t --fields name,device c show --active 2>/dev/null \
| grep "${1}" | head -1 | cut -d':' -f1)
nm_uuid=$(LANG=C nmcli -t --fields uuid,device c show --active 2> /dev/null |
grep "${1}" | head -1 | cut -d':' -f1)
nm_name=$(LANG=C nmcli -t --fields name,device c show --active 2> /dev/null |
grep "${1}" | head -1 | cut -d':' -f1)
ifcfg_file=$(get_ifcfg_by_uuid "${nm_uuid}")
[[ -z "${ifcfg_file}" ]] && ifcfg_file=$(get_ifcfg_by_name "${nm_name}")
[[ -z ${ifcfg_file} ]] && ifcfg_file=$(get_ifcfg_by_name "${nm_name}")
fi
echo -n "${ifcfg_file}"
@ -377,15 +381,15 @@ get_ifcfg_legacy()
local ifcfg_file hwaddr
ifcfg_file="/etc/sysconfig/network-scripts/ifcfg-${1}"
[[ -f "${ifcfg_file}" ]] && echo -n "${ifcfg_file}" && return
[[ -f ${ifcfg_file} ]] && echo -n "${ifcfg_file}" && return
ifcfg_file=$(get_ifcfg_by_name "${1}")
[[ -f "${ifcfg_file}" ]] && echo -n "${ifcfg_file}" && return
[[ -f ${ifcfg_file} ]] && echo -n "${ifcfg_file}" && return
hwaddr=$(get_hwaddr "${1}")
if [[ -n "$hwaddr" ]]; then
if [[ -n $hwaddr ]]; then
ifcfg_file=$(get_ifcfg_by_hwaddr "${hwaddr}")
[[ -f "${ifcfg_file}" ]] && echo -n "${ifcfg_file}" && return
[[ -f ${ifcfg_file} ]] && echo -n "${ifcfg_file}" && return
fi
ifcfg_file=$(get_ifcfg_by_device "${1}")
@ -395,11 +399,12 @@ get_ifcfg_legacy()
# $1: netdev name
# Return the ifcfg file whole name(including the path) of $1 if any.
get_ifcfg_filename() {
get_ifcfg_filename()
{
local ifcfg_file
ifcfg_file=$(get_ifcfg_nmcli "${1}")
if [[ -z "${ifcfg_file}" ]]; then
if [[ -z ${ifcfg_file} ]]; then
ifcfg_file=$(get_ifcfg_legacy "${1}")
fi
@ -408,7 +413,8 @@ get_ifcfg_filename() {
# returns 0 when omission of a module is desired in dracut_args
# returns 1 otherwise
is_dracut_mod_omitted() {
is_dracut_mod_omitted()
{
local dracut_args dracut_mod=$1
set -- $(kdump_get_conf_val dracut_args)
@ -416,6 +422,7 @@ is_dracut_mod_omitted() {
case $1 in
-o | --omit)
[[ " ${2//[^[:alnum:]]/ } " == *" $dracut_mod "* ]] && return 0
;;
esac
shift
done
@ -423,14 +430,15 @@ is_dracut_mod_omitted() {
return 1
}
is_wdt_active() {
is_wdt_active()
{
local active
[[ -d /sys/class/watchdog ]] || return 1
for dir in /sys/class/watchdog/*; do
[[ -f "$dir/state" ]] || continue
active=$(< "$dir/state")
[[ "$active" = "active" ]] && return 0
[[ $active == "active" ]] && return 0
done
return 1
}
@ -490,7 +498,7 @@ remove_cmdline_param()
shift
for arg in "$@"; do
cmdline=$(echo "$cmdline" | \
cmdline=$(echo "$cmdline" |
sed -e "s/\b$arg=[^ ]*//g" \
-e "s/^$arg\b//g" \
-e "s/[[:space:]]$arg\b//g" \
@ -523,7 +531,7 @@ append_cmdline()
local newstr=${cmdline/$2/""}
# unchanged str implies argument wasn't there
if [[ "$cmdline" == "$newstr" ]]; then
if [[ $cmdline == "$newstr" ]]; then
cmdline="${cmdline} ${2}=${3}"
fi
@ -562,11 +570,11 @@ is_secure_boot_enforced()
secure_boot_file=$(find /sys/firmware/efi/efivars -name "SecureBoot-*" 2> /dev/null)
setup_mode_file=$(find /sys/firmware/efi/efivars -name "SetupMode-*" 2> /dev/null)
if [[ -f "$secure_boot_file" ]] && [[ -f "$setup_mode_file" ]]; then
if [[ -f $secure_boot_file ]] && [[ -f $setup_mode_file ]]; then
secure_boot_byte=$(hexdump -v -e '/1 "%d\ "' "$secure_boot_file" | cut -d' ' -f 5)
setup_mode_byte=$(hexdump -v -e '/1 "%d\ "' "$setup_mode_file" | cut -d' ' -f 5)
if [[ "$secure_boot_byte" = "1" ]] && [[ "$setup_mode_byte" = "0" ]]; then
if [[ $secure_boot_byte == "1" ]] && [[ $setup_mode_byte == "0" ]]; then
return 0
fi
fi
@ -589,22 +597,18 @@ prepare_kexec_args()
local found_elf_args
ARCH=$(uname -m)
if [[ "$ARCH" == "i686" ]] || [[ "$ARCH" == "i386" ]]
then
if [[ $ARCH == "i686" ]] || [[ $ARCH == "i386" ]]; then
need_64bit_headers
if [[ $? == 1 ]]
then
if [[ $? == 1 ]]; then
found_elf_args=$(echo "$kexec_args" | grep elf32-core-headers)
if [[ -n "$found_elf_args" ]]
then
if [[ -n $found_elf_args ]]; then
dwarn "Warning: elf32-core-headers overrides correct elf64 setting"
else
kexec_args="$kexec_args --elf64-core-headers"
fi
else
found_elf_args=$(echo "$kexec_args" | grep elf64-core-headers)
if [[ -z "$found_elf_args" ]]
then
if [[ -z $found_elf_args ]]; then
kexec_args="$kexec_args --elf32-core-headers"
fi
fi
@ -624,7 +628,7 @@ prepare_kdump_bootinfo()
local boot_img boot_imglist boot_dirlist boot_initrdlist
local machine_id
if [[ -z "$KDUMP_KERNELVER" ]]; then
if [[ -z $KDUMP_KERNELVER ]]; then
KDUMP_KERNELVER="$(uname -r)"
fi
@ -634,7 +638,7 @@ prepare_kdump_bootinfo()
# Use BOOT_IMAGE as reference if possible, strip the GRUB root device prefix in (hd0,gpt1) format
boot_img="$(sed "s/^BOOT_IMAGE=\((\S*)\)\?\(\S*\) .*/\2/" /proc/cmdline)"
if [[ -n "$boot_img" ]]; then
if [[ -n $boot_img ]]; then
boot_imglist="$boot_img $boot_imglist"
fi
@ -647,7 +651,7 @@ prepare_kdump_bootinfo()
done
done
if ! [[ -e "$KDUMP_KERNEL" ]]; then
if ! [[ -e $KDUMP_KERNEL ]]; then
derror "Failed to detect kdump kernel location"
return 1
fi
@ -668,7 +672,7 @@ prepare_kdump_bootinfo()
# Create kdump initrd basename from default initrd basename
# initramfs-5.7.9-200.fc32.x86_64.img => initramfs-5.7.9-200.fc32.x86_64kdump.img
# initrd => initrdkdump
if [[ -z "$defaut_initrd_base" ]]; then
if [[ -z $defaut_initrd_base ]]; then
kdump_initrd_base=initramfs-${KDUMP_KERNELVER}kdump.img
elif [[ $defaut_initrd_base == *.* ]]; then
kdump_initrd_base=${defaut_initrd_base%.*}kdump.${DEFAULT_INITRD##*.}
@ -677,7 +681,7 @@ prepare_kdump_bootinfo()
fi
# Place kdump initrd in $(/var/lib/kdump) if $(KDUMP_BOOTDIR) not writable
if [[ ! -w "$KDUMP_BOOTDIR" ]];then
if [[ ! -w $KDUMP_BOOTDIR ]]; then
var_target_initrd_dir="/var/lib/kdump"
mkdir -p "$var_target_initrd_dir"
KDUMP_INITRD="$var_target_initrd_dir/$kdump_initrd_base"
@ -713,7 +717,7 @@ prepare_cmdline()
{
local cmdline id arg
if [[ -z "$1" ]]; then
if [[ -z $1 ]]; then
cmdline=$(< /proc/cmdline)
else
cmdline="$1"
@ -778,7 +782,7 @@ get_system_size()
result="+$result"
# replace '-' with '+0x' and '+' with '-0x'
sum=$(echo "$result" | sed -e 's/-/K0x/g' -e 's/+/-0x/g' -e 's/K/+/g')
size=$(printf "%d\n" $(($sum)))
size=$(printf "%d\n" $((sum)))
size=$((size / 1024 / 1024 / 1024))
echo "$size"
@ -824,18 +828,18 @@ kdump_get_arch_recommend_size()
return 1
fi
[[ -z "$kernel" ]] && kernel=$(uname -r)
[[ -z $kernel ]] && kernel=$(uname -r)
ck_cmdline=$(cat "/usr/lib/modules/$kernel/crashkernel.default" 2> /dev/null)
if [[ -n "$ck_cmdline" ]]; then
if [[ -n $ck_cmdline ]]; then
ck_cmdline=${ck_cmdline#crashkernel=}
else
arch=$(lscpu | grep Architecture | awk -F ":" '{ print $2 }' | tr '[:lower:]' '[:upper:]')
if [[ "$arch" = "X86_64" ]] || [[ "$arch" = "S390X" ]]; then
if [[ $arch == "X86_64" ]] || [[ $arch == "S390X" ]]; then
ck_cmdline="1G-4G:160M,4G-64G:192M,64G-1T:256M,1T-:512M"
elif [[ "$arch" = "AARCH64" ]]; then
elif [[ $arch == "AARCH64" ]]; then
ck_cmdline="2G-:448M"
elif [[ "$arch" = "PPC64LE" ]]; then
elif [[ $arch == "PPC64LE" ]]; then
if is_fadump_capable; then
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"
else
@ -874,7 +878,8 @@ get_luks_crypt_dev()
# Example:
# $ get_maj_min /dev/sda2
# 8:2
kdump_get_maj_min() {
kdump_get_maj_min()
{
local _majmin
_majmin="$(stat -L -c '%t:%T' "$1" 2> /dev/null)"
printf "%s" "$((0x${_majmin%:*})):$((0x${_majmin#*:}))"
@ -912,8 +917,7 @@ try_decompress()
# "grep" that report the byte offset of the line instead of the pattern.
# Try to find the header ($1) and decompress from here
for pos in $(tr "$1\n$2" "\n$2=" < "$4" | grep -abo "^$2")
do
for pos in $(tr "$1\n$2" "\n$2=" < "$4" | grep -abo "^$2"); do
if ! type -P "$3" > /dev/null; then
ddebug "Signiature detected but '$3' is missing, skip this decompressor"
break
@ -943,12 +947,12 @@ get_kernel_size()
check_vmlinux "$img" && get_vmlinux_size "$img" && return 0
# That didn't work, so retry after decompression.
try_decompress '\037\213\010' xy gunzip "$img" "$tmp" || \
try_decompress '\3757zXZ\000' abcde unxz "$img" "$tmp" || \
try_decompress 'BZh' xy bunzip2 "$img" "$tmp" || \
try_decompress '\135\0\0\0' xxx unlzma "$img" "$tmp" || \
try_decompress '\211\114\132' xy 'lzop -d' "$img" "$tmp" || \
try_decompress '\002!L\030' xxx 'lz4 -d' "$img" "$tmp" || \
try_decompress '\037\213\010' xy gunzip "$img" "$tmp" ||
try_decompress '\3757zXZ\000' abcde unxz "$img" "$tmp" ||
try_decompress 'BZh' xy bunzip2 "$img" "$tmp" ||
try_decompress '\135\0\0\0' xxx unlzma "$img" "$tmp" ||
try_decompress '\211\114\132' xy 'lzop -d' "$img" "$tmp" ||
try_decompress '\002!L\030' xxx 'lz4 -d' "$img" "$tmp" ||
try_decompress '(\265/\375' xxx unzstd "$img" "$tmp"
# Finally check for uncompressed images or objects: