From 6abeb6fc77bf8831ce74c7b3b8499259280ddf5a Mon Sep 17 00:00:00 2001 From: Javier Martinez Canillas Date: Thu, 28 Feb 2019 13:01:40 +0100 Subject: [PATCH] grubby-bls: make --update-kernel ALL to update kernelopts var in grubenv Currently there's no way to update the kernelopts variable defined in the grubenv by using the grubby wrapper. Updating it with grub2-editenv - set is tedious and error prone so let's use --update-kernel=ALL option for it. This command currently iterates over all the BLS entries and update them individually, but makes more sense to update the kernelopts grubenv var and only update the BLS entries that don't have the $kernelopts var in their options field. If after an update one of these BLS entries have exactly the same params in their options field than the ones set in $kernelopts, set the options field to $kernelopts again since the parameters have converged again. That way the modified BLS entries will only have a custom options field while its value diverged from the $kernelopts defined in grubenv. Signed-off-by: Javier Martinez Canillas --- grubby-bls | 32 ++++++++++++++++++++++++++++++-- grubby.spec | 5 ++++- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/grubby-bls b/grubby-bls index c1ea4cf..d4a543e 100755 --- a/grubby-bls +++ b/grubby-bls @@ -218,8 +218,20 @@ expand_var() { echo $var } +has_kernelopts() +{ + local args=${bls_options[$1]} + local opts=(${args}) + + for opt in ${opts[*]}; do + [[ $opt = "\$kernelopts" ]] && return 0 + done + + return 1 +} + get_bls_args() { - local args=${bls_options[$i]} + local args=${bls_options[$1]} local opts=(${args}) for opt in ${opts[*]}; do @@ -473,16 +485,32 @@ update_bls_fragment() { local remove_args=$1 && shift local add_args=$1 && shift local initrd=$1 && shift + local opts if [[ $indexes = "-1" ]]; then print_error "The param $(get_prefix)${param} is incorrect" fi + if [[ $param = "ALL" && $bootloader = grub2 ]] && [[ -n $remove_args || -n $add_args ]]; then + local old_args="$(grub2-editenv "${env}" list | grep kernelopts | sed -e "s/kernelopts=//")" + opts="$(update_args "${old_args}" "${remove_args}" "${add_args}")" + grub2-editenv "${env}" set kernelopts="${opts}" + elif [[ $bootloader = grub2 ]]; then + opts="$(grub2-editenv "${env}" list | grep kernelopts | sed -e "s/kernelopts=//")" + fi + for i in ${indexes[*]}; do if [[ -n $remove_args || -n $add_args ]]; then local old_args="$(get_bls_args "$i")" local new_args="$(update_args "${old_args}" "${remove_args}" "${add_args}")" - set_bls_value "${bls_file[$i]}" "options" "${new_args}" + + if [[ $param != "ALL" || ! "$(has_kernelopts "$i")" ]]; then + set_bls_value "${bls_file[$i]}" "options" "${new_args}" + fi + + if [[ $bootloader = grub2 && ! "$(has_kernelopts "$i")" && $opts = $new_args ]]; then + set_bls_value "${bls_file[$i]}" "options" "\$kernelopts" + fi fi if [[ -n $initrd ]]; then diff --git a/grubby.spec b/grubby.spec index 5606b2c..9df01e6 100644 --- a/grubby.spec +++ b/grubby.spec @@ -1,6 +1,6 @@ Name: grubby Version: 8.40 -Release: 27%{?dist} +Release: 28%{?dist} Summary: Command line tool for updating bootloader configs License: GPLv2+ URL: https://github.com/rhinstaller/grubby @@ -132,6 +132,9 @@ current boot environment. %{_mandir}/man8/*.8* %changelog +* Fri Mar 01 2019 Javier Martinez Canillas - 8.40-28 +- grubby-bls: make --update-kernel ALL to update kernelopts var in grubenv + * Thu Feb 14 2019 Javier Martinez Canillas - 8.40-27 - grubby-bls: error if args or remove-args is used without update-kernel