#!/bin/bash
set -x
set -e
export PS4=+{$LINENO:${FUNCNAME[0]}}
trap 'echo "---NEWKERNARGS=$NEWKERNARGS xx=$xx------"' DEBUG NEWKERNARGS=""
grubby --info=`/sbin/grubby --default-kernel` 2>/dev/null | grep -q crashkernel
if [ $? -ne 0 ];then
NEWKERNARGS="--kernel-args=\"crashkernel=auto\""
fi grubby --info=`/sbin/grubby --default-kernel` 2>/dev/null | grep -q "nopti noibrs noibpb"
if [ $? -ne 0 ];then
NEWKERNARGS="--kernel-args=\"crashkernel=auto nopti noibrs noibpb\""
fi /sbin/new-kernel-pkg --package kernel%{?1:-%{1}} %{!?1:--make-default} --install %{KVERREL}%{?1:.%{1}}

上面的这种做法其实存在3个问题:

1 缺失一种场景,即是grub的内核入口参数中包含"crashkernel=auto"但不包含"nopti noibrs noibpb",

2 转义字符使用过多,不便理解且容易出错,可参照--install将--kernel-args参数选项移出变量的赋值过程,大大简化原有逻辑和复杂度;

3 NEWKERNARGS参数的赋值其实可以采用字符串加方式,这样可以减少grubby查询次数

具体做法如下:

#!/bin/bash
set -x
set -e
export PS4=+{$LINENO:${FUNCNAME[0]}}
trap 'echo "---NEWKERNARGS=$NEWKERNARGS xx=$xx------"' DEBUG NEWKERNARGS=""
grubby --info=`/sbin/grubby --default-kernel` 2>/dev/null | grep -q crashkernel
if [ $? -ne 0 ];then
NEWKERNARGS="crashkernel=auto"
fi grubby --info=`/sbin/grubby --default-kernel` 2>/dev/null | grep -q "nopti noibrs noibpb"
if [ $? -ne 0 ];then
NEWKERNARGS=${NEWKERNARGS}"nopti noibrs noibpb"
fi /sbin/new-kernel-pkg --package kernel%{?1:-%{1}} %{!?1:--make-default} --install %{KVERREL}%{?1:.%{1}} --kernel-args="$NEWKERNARGS"

  

  

05-11 13:27