Properly quote strings, remove duplicated echo / cat, and separate some
variable declaration and assignment, remove unused variable and update
legacy syntax.
Signed-off-by: Kairui Song <kasong(a)redhat.com>
---
kdump-lib.sh | 199 ++++++++++++++++++++++++++-------------------------
1 file changed, 100 insertions(+), 99 deletions(-)
diff --git a/kdump-lib.sh b/kdump-lib.sh
index 2f5b17ad..b89bb294 100755
--- a/kdump-lib.sh
+++ b/kdump-lib.sh
@@ -14,8 +14,7 @@ is_fadump_capable()
# Check if firmware-assisted dump is enabled
# if no, fallback to kdump check
if [[ -f $FADUMP_ENABLED_SYS_NODE ]]; then
- rc=$(cat $FADUMP_ENABLED_SYS_NODE)
- [[ $rc -eq 1 ]] && return 0
+ [[ $(cat $FADUMP_ENABLED_SYS_NODE) -eq 1 ]] && return 0
fi
return 1
}
@@ -25,7 +24,7 @@ is_squash_available() {
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
+ modprobe -S "$KDUMP_KERNELVER" --dry-run $kmodule
&>/dev/null || return 1
fi
done
}
@@ -65,7 +64,7 @@ to_dev_name() {
dev=$(blkid -L "${dev#LABEL=}")
;;
esac
- echo $dev
+ echo "$dev"
}
is_user_configured_dump_target()
@@ -78,10 +77,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()
@@ -93,12 +92,12 @@ get_block_dump_target()
fi
_target=$(get_user_configured_dump_disk)
- [[ -n "$_target" ]] && echo $(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" ]] && echo $(to_dev_name $_target)
+ _target=$(get_target_from_path "$_path")
+ [[ -b "$_target" ]] && to_dev_name "$_target"
}
is_dump_to_rootfs()
@@ -113,7 +112,7 @@ get_failure_action_target()
if is_dump_to_rootfs; then
# Get rootfs device name
_target=$(get_root_fs_device)
- [[ -b "$_target" ]] && echo $(to_dev_name $_target) &&
return
+ [[ -b "$_target" ]] && to_dev_name "$_target"
&& return
# Then, must be nfs root
echo "nfs"
fi
@@ -157,27 +156,29 @@ get_kdump_targets()
# part is the bind mounted directory which quotes by bracket "[]".
get_bind_mount_source()
{
- local _mnt=$(df $1 | tail -1 | awk '{print $NF}')
- local _path=${1#$_mnt}
+ local _mnt _src _opt _fstype _path _src_nofsroot
- local _src=$(get_mount_info SOURCE target $_mnt -f)
- local _opt=$(get_mount_info OPTIONS target $_mnt -f)
- local _fstype=$(get_mount_info FSTYPE target $_mnt -f)
+ _mnt=$(df $1 | tail -1 | awk '{print $NF}')
+ _path=${1#$_mnt}
+
+ _src=$(get_mount_info SOURCE target "$_mnt" -f)
+ _opt=$(get_mount_info OPTIONS target "$_mnt" -f)
+ _fstype=$(get_mount_info FSTYPE target "$_mnt" -f)
# bind mount in fstab
if [[ -d "$_src" ]] && [[ "$_fstype" = none ]] &&
(echo "$_opt" | grep -q "\bbind\b"); then
- echo $_src$_path && return
+ echo "$_src$_path" && return
fi
# direct mount
- local _src_nofsroot=$(get_mount_info SOURCE target $_mnt -v -f)
- if [[ $_src_nofsroot = $_src ]]; then
- echo $_mnt$_path && return
+ _src_nofsroot=$(get_mount_info SOURCE target "$_mnt" -v -f)
+ if [[ $_src_nofsroot = "$_src" ]]; then
+ echo "$_mnt$_path" && return
fi
- local _fsroot=${_src#$_src_nofsroot[}
+ local _fsroot=${_src#${_src_nofsroot}[}
_fsroot=${_fsroot%]}
- _mnt=$(get_mount_info TARGET source $_src_nofsroot -f)
+ _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
@@ -186,19 +187,21 @@ get_bind_mount_source()
_subvol=${_subvol%,*}
_fsroot=${_fsroot#$_subvol}
fi
- echo $_mnt$_fsroot$_path
+ echo "$_mnt$_fsroot$_path"
}
get_mntopt_from_target()
{
- get_mount_info OPTIONS source $1 -f
+ get_mount_info OPTIONS source "$1" -f
}
# Get the path where the target will be mounted in kdump kernel
# $1: kdump target device
get_kdump_mntpoint_from_target()
{
- local _mntpoint=$(get_mntpoint_from_target $1)
+ local _mntpoint
+
+ _mntpoint=$(get_mntpoint_from_target "$1")
# mount under /sysroot if dump to root disk or mount under
# mount under /kdumproot if dump target is not mounted in first kernel
@@ -229,7 +232,8 @@ kdump_get_persistent_dev() {
dev=$(blkid -L "${dev#LABEL=}")
;;
esac
- echo $(get_persistent_dev "$dev")
+
+ get_persistent_dev "$dev"
}
is_atomic()
@@ -248,26 +252,24 @@ get_remote_host()
_config_val=${_config_val%:/*}
_config_val=${_config_val#[}
_config_val=${_config_val%]}
- echo $_config_val
+ echo "$_config_val"
}
is_hostname()
{
- local _hostname=$(echo $1 | grep ":")
-
- if [[ -n "$_hostname" ]]; then
+ if echo "$1" | grep -q ":"; then
return 1
fi
- echo $1 | grep -q "[a-zA-Z]"
+ echo "$1" | grep -q "[a-zA-Z]"
}
# Copied from "/etc/sysconfig/network-scripts/network-functions"
get_hwaddr()
{
if [[ -f "/sys/class/net/${1}/address" ]]; then
- awk '{ print toupper($0) }' < /sys/class/net/${1}/address
+ 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
@@ -368,7 +370,7 @@ get_ifcfg_nmcli()
# $1: netdev name
get_ifcfg_legacy()
{
- local ifcfg_file
+ local ifcfg_file hwaddr
ifcfg_file="/etc/sysconfig/network-scripts/ifcfg-${1}"
[[ -f "${ifcfg_file}" ]] && echo -n "${ifcfg_file}"
&& return
@@ -376,7 +378,7 @@ get_ifcfg_legacy()
ifcfg_file=$(get_ifcfg_by_name "${1}")
[[ -f "${ifcfg_file}" ]] && echo -n "${ifcfg_file}"
&& return
- local hwaddr=$(get_hwaddr "${1}")
+ hwaddr=$(get_hwaddr "${1}")
if [[ -n "$hwaddr" ]]; then
ifcfg_file=$(get_ifcfg_by_hwaddr "${hwaddr}")
[[ -f "${ifcfg_file}" ]] && echo -n "${ifcfg_file}"
&& return
@@ -403,7 +405,7 @@ get_ifcfg_filename() {
# returns 0 when omission of a module is desired in dracut_args
# returns 1 otherwise
is_dracut_mod_omitted() {
- local dracut_args dracut_mod=$1
+ local dracut_mod=$1
set -- $(kdump_get_conf_val dracut_args)
while [[ $# -gt 0 ]]; do
@@ -483,14 +485,14 @@ remove_cmdline_param()
local cmdline=$1
shift
- for arg in $@; do
- cmdline=`echo $cmdline | \
- sed -e "s/\b$arg=[^ ]*//g" \
- -e "s/^$arg\b//g" \
- -e "s/[[:space:]]$arg\b//g" \
- -e "s/\s\+/ /g"`
+ for arg in "$@"; do
+ cmdline=$(echo "$cmdline" | \
+ sed -e "s/\b$arg=[^ ]*//g" \
+ -e "s/^$arg\b//g" \
+ -e "s/[[:space:]]$arg\b//g" \
+ -e "s/\s\+/ /g")
done
- echo $cmdline
+ echo "$cmdline"
}
#
@@ -521,15 +523,14 @@ append_cmdline()
cmdline="${cmdline} ${2}=${3}"
fi
- echo $cmdline
+ echo "$cmdline"
}
# This function check iomem and determines if we have more than
# 4GB of ram available. Returns 1 if we do, 0 if we dont
need_64bit_headers()
{
- return `tail -n 1 /proc/iomem | awk '{ split ($1, r, "-"); \
- print (strtonum("0x" r[2]) > strtonum("0xffffffff")); }'`
+ return "$(tail -n 1 /proc/iomem | awk '{ split ($1, r, "-"); print
(strtonum("0x" r[2]) > strtonum("0xffffffff")); }')"
}
# Check if secure boot is being enforced.
@@ -558,12 +559,12 @@ is_secure_boot_enforced()
fi
# Detect secure boot on x86 and arm64
- 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)
+ 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
- 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)
+ 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
return 0
@@ -593,7 +594,7 @@ prepare_kexec_args()
need_64bit_headers
if [[ $? == 1 ]]
then
- found_elf_args=$(echo $kexec_args | grep elf32-core-headers)
+ found_elf_args=$(echo "$kexec_args" | grep elf32-core-headers)
if [[ -n "$found_elf_args" ]]
then
dwarn "Warning: elf32-core-headers overrides correct elf64
setting"
@@ -601,14 +602,14 @@ prepare_kexec_args()
kexec_args="$kexec_args --elf64-core-headers"
fi
else
- found_elf_args=$(echo $kexec_args | grep elf64-core-headers)
+ found_elf_args=$(echo "$kexec_args" | grep elf64-core-headers)
if [[ -z "$found_elf_args" ]]
then
kexec_args="$kexec_args --elf32-core-headers"
fi
fi
fi
- echo $kexec_args
+ echo "$kexec_args"
}
#
@@ -620,19 +621,19 @@ prepare_kexec_args()
#
prepare_kdump_bootinfo()
{
- local boot_imglist boot_dirlist boot_initrdlist curr_kver="$(uname -r)"
+ local boot_img boot_imglist boot_dirlist boot_initrdlist
local machine_id
if [[ -z "$KDUMP_KERNELVER" ]]; then
KDUMP_KERNELVER="$(uname -r)"
fi
- read machine_id < /etc/machine-id
+ read -r machine_id < /etc/machine-id
boot_dirlist=${KDUMP_BOOTDIR:-"/boot /boot/efi /efi /"}
boot_imglist="$KDUMP_IMG-$KDUMP_KERNELVER$KDUMP_IMG_EXT
$machine_id/$KDUMP_KERNELVER/$KDUMP_IMG"
# Use BOOT_IMAGE as reference if possible, strip the GRUB root device prefix in
(hd0,gpt1) format
- local boot_img="$(cat /proc/cmdline | sed "s/^BOOT_IMAGE=\((\S*)\)\?\(\S*\)
.*/\2/")"
+ boot_img="$(sed "s/^BOOT_IMAGE=\((\S*)\)\?\(\S*\) .*/\2/"
/proc/cmdline)"
if [[ -n "$boot_img" ]]; then
boot_imglist="$boot_img $boot_imglist"
fi
@@ -640,7 +641,7 @@ prepare_kdump_bootinfo()
for dir in $boot_dirlist; do
for img in $boot_imglist; do
if [[ -f "$dir/$img" ]]; then
- KDUMP_KERNEL=$(echo $dir/$img | tr -s '/')
+ KDUMP_KERNEL=$(echo "$dir/$img" | tr -s '/')
break 2
fi
done
@@ -652,7 +653,7 @@ prepare_kdump_bootinfo()
fi
# Set KDUMP_BOOTDIR to where kernel image is stored
- KDUMP_BOOTDIR=$(dirname $KDUMP_KERNEL)
+ KDUMP_BOOTDIR=$(dirname "$KDUMP_KERNEL")
# Default initrd should just stay aside of kernel image, try to find it in
KDUMP_BOOTDIR
boot_initrdlist="initramfs-$KDUMP_KERNELVER.img initrd"
@@ -693,7 +694,7 @@ get_watchdog_drvs()
# device/modalias will return driver of this device
[[ -f "$_dir/device/modalias" ]] || continue
_drv=$(< "$_dir/device/modalias")
- _drv=$(modprobe --set-version "$KDUMP_KERNELVER" -R $_drv
2>/dev/null)
+ _drv=$(modprobe --set-version "$KDUMP_KERNELVER" -R "$_drv"
2>/dev/null)
for i in $_drv; do
if ! [[ " $_wdtdrvs " == *" $i "* ]]; then
_wdtdrvs="$_wdtdrvs $i"
@@ -701,7 +702,7 @@ get_watchdog_drvs()
done
done
- echo $_wdtdrvs
+ echo "$_wdtdrvs"
}
#
@@ -742,8 +743,8 @@ prepare_cmdline()
cmdline="${cmdline} $3"
id=$(get_bootcpu_apicid)
- if [[ ! -z ${id} ]] ; then
- cmdline=$(append_cmdline "${cmdline}" disable_cpu_apicid ${id})
+ if [[ -n ${id} ]] ; then
+ cmdline=$(append_cmdline "${cmdline}" disable_cpu_apicid
"${id}")
fi
# If any watchdog is used, set it's pretimeout to 0. pretimeout let
@@ -765,18 +766,18 @@ prepare_cmdline()
cmdline=$(remove_cmdline_param "$cmdline" trace_buf_size trace_event)
cmdline="${cmdline} trace_buf_size=1"
- echo ${cmdline}
+ echo "${cmdline}"
}
#get system memory size in the unit of GB
get_system_size()
{
- result=$(cat /proc/iomem | grep "System RAM" | awk -F ":" '{
print $1 }' | tr [:lower:] [:upper:] | paste -sd+)
+ result=$(grep "System RAM" /proc/iomem | awk -F ":" '{ print
$1 }' | tr "[:lower:]" "[:upper:]" | paste -sd+)
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)))
- let size=$size/1024/1024/1024
+ size=$(printf "%d\n" $((sum)))
+ size=$((size/1024/1024/1024))
echo $size
}
@@ -787,9 +788,6 @@ get_recommend_size()
local _ck_cmdline=$2
local OLDIFS="$IFS"
- last_sz=""
- last_unit=""
-
start=${_ck_cmdline: :1}
if [[ $mem_size -lt $start ]]; then
echo "0M"
@@ -797,15 +795,15 @@ get_recommend_size()
fi
IFS=','
for i in $_ck_cmdline; do
- end=$(echo $i | awk -F "-" '{ print $2 }' | awk -F
":" '{ print $1 }')
- recommend=$(echo $i | awk -F "-" '{ print $2 }' | awk -F
":" '{ print $2 }')
+ end=$(echo "$i" | awk -F "-" '{ print $2 }' | awk -F
":" '{ print $1 }')
+ recommend=$(echo "$i" | awk -F "-" '{ print $2 }' |
awk -F ":" '{ print $2 }')
size=${end: : -1}
unit=${end: -1}
if [[ $unit == 'T' ]]; then
- let size=$size*1024
+ size=$((size*1024))
fi
if [[ $mem_size -lt $size ]]; then
- echo $recommend
+ echo "$recommend"
IFS="$OLDIFS"
return
fi
@@ -844,7 +842,7 @@ kdump_get_arch_recommend_size()
fi
fi
- ck_cmdline=$(echo $ck_cmdline | sed -e 's/-:/-102400T:/g')
+ ck_cmdline=$(echo "$ck_cmdline" | sed -e 's/-:/-102400T:/g')
sys_mem=$(get_system_size)
get_recommend_size "$sys_mem" "$ck_cmdline"
@@ -855,12 +853,13 @@ kdump_get_arch_recommend_size()
# $1: the block device to be checked in maj:min format
get_luks_crypt_dev()
{
+ local _type
[[ -b /dev/block/$1 ]] || return 1
- local _type=$(eval "$(blkid -u filesystem,crypto -o export -- /dev/block/$1);
echo \$TYPE")
- [[ $_type == "crypto_LUKS" ]] && echo $1
+ _type=$(eval "$(blkid -u filesystem,crypto -o export --
"/dev/block/$1"); echo \$TYPE")
+ [[ $_type == "crypto_LUKS" ]] && echo "$1"
- for _x in /sys/dev/block/$1/slaves/*; do
+ for _x in "/sys/dev/block/$1/slaves/"*; do
[[ -f $_x/dev ]] || continue
[[ $_x/subsystem -ef /sys/class/block ]] || continue
get_luks_crypt_dev "$(< "$_x/dev")"
@@ -883,24 +882,24 @@ get_all_kdump_crypt_dev()
local _dev _crypt
for _dev in $(get_block_dump_target); do
- _crypt=$(get_luks_crypt_dev $(kdump_get_maj_min "$_dev"))
- [[ -n "$_crypt" ]] && echo $_crypt
+ _crypt=$(get_luks_crypt_dev "$(kdump_get_maj_min "$_dev")")
+ [[ -n "$_crypt" ]] && echo "$_crypt"
done
}
check_vmlinux()
{
# Use readelf to check if it's a valid ELF
- readelf -h $1 &>/dev/null || return 1
+ readelf -h "$1" &>/dev/null || return 1
}
get_vmlinux_size()
{
local size=0
- while read _type _offset _virtaddr _physaddr _fsize _msize _flg _aln; do
- size=$(( $size + $_msize ))
- done <<< $(readelf -l -W $1 | grep "^ LOAD" 2>/dev/stderr)
+ while read -r _type _offset _virtaddr _physaddr _fsize _msize _flg _aln; do
+ size=$(( size + _msize ))
+ done <<< "$(readelf -l -W "$1" | grep "^ LOAD"
2>/dev/stderr)"
echo $size
}
@@ -913,14 +912,14 @@ try_decompress()
# Try to find the header ($1) and decompress from here
for pos in $(tr "$1\n$2" "\n$2=" < "$4" | grep -abo
"^$2")
do
- if ! type -P $3 > /dev/null; then
+ if ! type -P "$3" > /dev/null; then
ddebug "Signiature detected but '$3' is missing, skip this
decompressor"
break
fi
pos=${pos%%:*}
- tail -c+$pos "$img" | $3 > $5 2> /dev/null
- if check_vmlinux $5; then
+ tail -c+$pos "$img" | $3 > "$5" 2> /dev/null
+ if check_vmlinux "$5"; then
ddebug "Kernel is extracted with '$3'"
return 0
fi
@@ -933,28 +932,30 @@ try_decompress()
get_kernel_size()
{
# Prepare temp files:
- local img=$1 tmp=$(mktemp /tmp/vmlinux-XXX)
- trap "rm -f $tmp" 0
+ local img=$1 tmp
+
+ tmp=$(mktemp /tmp/vmlinux-XXX)
+ trap 'rm -f $tmp' 0
# Try to check if it's a vmlinux already
- check_vmlinux $img && get_vmlinux_size $img && return 0
+ 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 '(\265/\375' xxx unzstd $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:
- [[ $? -eq 0 ]] && get_vmlinux_size $tmp && return 0
+ [[ $? -eq 0 ]] && get_vmlinux_size "$tmp" && return 0
# Fallback to use iomem
local _size=0
- for _seg in $(cat /proc/iomem | grep -E "Kernel (code|rodata|data|bss)" |
cut -d ":" -f 1); do
- _size=$(( $_size + 0x${_seg#*-} - 0x${_seg%-*} ))
- done
+ while read -r _seg; do
+ _size=$(( _size + 0x${_seg#*-} - 0x${_seg%-*} ))
+ done <<< "$(grep -E "Kernel (code|rodata|data|bss)"
/proc/iomem | cut -d ":" -f 1)"
echo $_size
}
--
2.31.1