2018-06-18 09:14:35 +00:00
#!/bin/bash
#
# grubby wrapper to manage BootLoaderSpec files
#
#
# Copyright 2018 Red Hat, Inc. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
readonly SCRIPTNAME="${0##*/}"
CMDLINE_LINUX_DEBUG=" systemd.log_level=debug systemd.log_target=kmsg"
LINUX_DEBUG_VERSION_POSTFIX="_with_debugging"
LINUX_DEBUG_TITLE_POSTFIX=" with debugging"
declare -a bls_file
declare -a bls_title
declare -a bls_version
declare -a bls_linux
declare -a bls_initrd
declare -a bls_options
declare -a bls_id
[[ -f /etc/sysconfig/kernel ]] && . /etc/sysconfig/kernel
[[ -f /etc/os-release ]] && . /etc/os-release
read MACHINE_ID < /etc/machine-id
arch=$(uname -m)
if [[ $arch = 's390' || $arch = 's390x' ]]; then
bootloader="zipl"
else
bootloader="grub2"
fi
print_error() {
echo "$1" >&2
exit 1
}
2018-10-05 12:31:13 +00:00
print_info() {
echo "$1" >&2
}
2018-06-18 09:14:35 +00:00
if [[ ${#} = 0 ]]; then
print_error "no action specified"
fi
get_bls_value() {
local bls="$1" && shift
local key="$1" && shift
2018-10-04 15:54:42 +00:00
echo "$(grep "^${key}[ \t]" "${bls}" | sed -e "s!^${key}[ \t]*!!")"
2018-06-18 09:14:35 +00:00
}
set_bls_value() {
local bls="$1" && shift
local key="$1" && shift
local value="$1" && shift
2018-10-17 18:36:08 +00:00
value=$(echo $value | sed -e 's/\//\\\//g')
sed -i -e "s/^${key}.*/${key} ${value}/" "${bls}"
2018-06-18 09:14:35 +00:00
}
append_bls_value() {
local bls="$1" && shift
local key="$1" && shift
local value="$1" && shift
2018-10-23 13:38:55 +00:00
old_value="$(get_bls_value "${bls}" ${key})"
2018-06-18 09:14:35 +00:00
set_bls_value "${bls}" "${key}" "${old_value}${value}"
}
get_bls_values() {
count=0
2018-10-23 13:38:55 +00:00
local -a files
local IFS=$'\n'
files=($(for bls in ${blsdir}/*.conf ; do
if ! [[ -e "${bls}" ]] ; then
continue
fi
bls="${bls%.conf}"
bls="${bls##*/}"
echo "${bls}"
2022-11-01 17:40:38 +00:00
done | sort -Vr 2>/dev/null)) || :
2018-10-23 13:38:55 +00:00
for bls in "${files[@]}" ; do
blspath="${blsdir}/${bls}.conf"
bls_file[$count]="${blspath}"
bls_title[$count]="$(get_bls_value ${blspath} title)"
bls_version[$count]="$(get_bls_value ${blspath} version)"
bls_linux[$count]="$(get_bls_value ${blspath} linux)"
bls_initrd[$count]="$(get_bls_value ${blspath} initrd)"
bls_options[$count]="$(get_bls_value ${blspath} options)"
bls_id[$count]="${bls}"
2018-06-18 09:14:35 +00:00
count=$((count+1))
done
}
get_default_index() {
local default=""
local index="-1"
local title=""
local version=""
if [[ $bootloader = "grub2" ]]; then
default="$(grep '^saved_entry=' ${env} | sed -e 's/^saved_entry=//')"
else
default="$(grep '^default=' ${zipl_config} | sed -e 's/^default=//')"
fi
if [[ -z $default ]]; then
index=0
elif [[ $default =~ ^[0-9]+$ ]]; then
index="$default"
fi
for i in ${!bls_file[@]}; do
2018-11-01 23:44:51 +00:00
if [[ $i -eq $index ]]; then
echo $i
return
fi
2018-12-11 15:21:45 +00:00
if [[ $default = ${bls_id[$i]} || $default = ${bls_title[$i]} ]]; then
2018-06-18 09:14:35 +00:00
echo $i
return
fi
done
}
display_default_value() {
2018-12-01 00:57:07 +00:00
local prefix=$(get_prefix)
2018-06-18 09:14:35 +00:00
case "$display_default" in
kernel)
2018-12-01 00:57:07 +00:00
echo "${prefix}${bls_linux[$default_index]}"
2018-06-18 09:14:35 +00:00
exit 0
;;
index)
echo "$default_index"
exit 0
;;
title)
echo "${bls_title[$default_index]}"
exit 0
;;
esac
}
param_to_indexes() {
local param="$1"
local indexes=""
if [[ $param = "ALL" ]]; then
for i in ${!bls_file[@]}; do
indexes="$indexes $i"
done
echo -n $indexes
return
fi
if [[ $param = "DEFAULT" ]]; then
echo -n $default_index
return
fi
for i in ${!bls_file[@]}; do
2018-10-04 15:35:33 +00:00
if [[ $param = "${bls_linux[$i]}" || "/${param##*/}" = "${bls_linux[$i]}" ]]; then
2018-06-18 09:14:35 +00:00
indexes="$indexes $i"
fi
if [[ $param = "TITLE=${bls_title[$i]}" ]]; then
indexes="$indexes $i"
fi
if [[ $param = $i ]]; then
indexes="$indexes $i"
fi
done
if [[ -n $indexes ]]; then
echo -n $indexes
return
fi
echo -n "-1"
}
2018-11-16 23:03:02 +00:00
get_prefix() {
2019-05-03 16:21:23 +00:00
if [[ $bootloader = grub2 ]] && mountpoint -q /boot; then
2018-11-16 23:03:02 +00:00
echo "/boot"
else
echo ""
fi
}
2018-11-23 00:22:56 +00:00
expand_var() {
local var=$1
if [[ $bootloader == "grub2" ]]; then
local value="$(grub2-editenv "${env}" list | grep ${var##$} | sed -e "s/${var##$}=//")"
value="$(echo ${value} | sed -e 's/\//\\\//g')"
if [[ -n $value ]]; then
var="$value"
fi
fi
echo $var
}
2019-02-28 12:01:40 +00:00
has_kernelopts()
{
local args=${bls_options[$1]}
local opts=(${args})
for opt in ${opts[*]}; do
2019-11-27 16:08:03 +00:00
[[ $opt = "\$kernelopts" ]] && echo "true"
2019-02-28 12:01:40 +00:00
done
2019-11-27 16:08:03 +00:00
echo "false"
2019-02-28 12:01:40 +00:00
}
2019-01-14 08:56:05 +00:00
get_bls_args() {
2019-02-28 12:01:40 +00:00
local args=${bls_options[$1]}
2019-01-14 08:56:05 +00:00
local opts=(${args})
for opt in ${opts[*]}; do
2021-06-09 21:38:54 +00:00
if [[ $opt = "\$kernelopts" ]]; then
2019-01-14 08:56:05 +00:00
value="$(expand_var $opt)"
args="$(echo ${args} | sed -e "s/${opt}/${value}/")"
fi
done
echo ${args}
}
2018-06-18 09:14:35 +00:00
display_info_values() {
local indexes=($(param_to_indexes "$1"))
2018-11-16 23:03:02 +00:00
local prefix=$(get_prefix)
2018-06-18 09:14:35 +00:00
2018-10-15 13:25:43 +00:00
if [[ $indexes = "-1" ]]; then
print_error "The param $1 is incorrect"
fi
2018-06-18 09:14:35 +00:00
for i in ${indexes[*]}; do
2018-11-17 02:35:14 +00:00
local root=""
2018-11-23 00:22:56 +00:00
local value=""
2019-01-14 08:56:05 +00:00
local args="$(get_bls_args "$i")"
2018-11-23 00:22:56 +00:00
local opts=(${args})
2018-11-17 02:35:14 +00:00
for opt in ${opts[*]}; do
if echo $opt | grep -q "^root="; then
root="$(echo $opt | sed -e 's/root=//')"
value="$(echo ${opt} | sed -e 's/\//\\\//g')"
args="$(echo ${args} | sed -e "s/${value}[ \t]*//")"
break
fi
done
2018-06-18 09:14:35 +00:00
echo "index=$i"
2018-11-16 23:03:02 +00:00
echo "kernel=\"${prefix}${bls_linux[$i]}\""
2018-11-17 02:35:14 +00:00
echo "args=\"${args}\""
if [[ -n $root ]]; then
echo "root=\"${root}\""
fi
2018-11-16 23:03:02 +00:00
echo "initrd=\"${prefix}${bls_initrd[$i]}\""
2018-10-23 13:38:55 +00:00
echo "title=\"${bls_title[$i]}\""
2018-10-23 13:08:30 +00:00
echo "id=\"${bls_id[$i]}\""
2018-06-18 09:14:35 +00:00
done
exit 0
}
mkbls() {
local kernel=$1 && shift
local kernelver=$1 && shift
local datetime=$1 && shift
local debugname=""
local flavor=""
2018-11-16 23:03:02 +00:00
local prefix=""
if [[ $(get_prefix) = "" ]]; then
prefix="/boot"
fi
2018-06-18 09:14:35 +00:00
if [[ $kernelver == *\+* ]] ; then
local flavor=-"${kernelver##*+}"
if [[ $flavor == "-debug" ]]; then
local debugname="with debugging"
local debugid="-debug"
fi
fi
cat <<EOF
title ${NAME} (${kernelver}) ${VERSION}${debugname}
version ${kernelver}${debugid}
linux ${kernel}
2018-11-16 23:03:02 +00:00
initrd ${prefix}/initramfs-${kernelver}.img
2018-06-18 09:14:35 +00:00
options \$kernelopts
id ${ID}-${datetime}-${kernelver}${debugid}
grub_users \$grub_users
grub_arg --unrestricted
grub_class kernel${flavor}
EOF
}
2019-02-01 23:59:16 +00:00
unset_default_bls()
{
if [[ $bootloader = grub2 ]]; then
grub2-editenv "${env}" unset saved_entry
else
sed -i -e "/^default=.*/d" "${zipl_config}"
fi
}
2018-06-18 09:14:35 +00:00
remove_bls_fragment() {
local indexes=($(param_to_indexes "$1"))
if [[ $indexes = "-1" ]]; then
2019-02-01 23:37:15 +00:00
print_error "The param $(get_prefix)$1 is incorrect"
2018-06-18 09:14:35 +00:00
fi
2018-10-23 13:38:55 +00:00
for i in "${indexes[@]}"; do
2019-02-01 23:59:16 +00:00
if [[ $default_index = $i ]]; then
unset_default_bls
fi
2018-06-18 09:14:35 +00:00
rm -f "${bls_file[$i]}"
done
get_bls_values
2018-10-05 10:13:49 +00:00
update_grubcfg
2018-06-18 09:14:35 +00:00
}
2018-11-01 23:51:22 +00:00
get_custom_bls_filename() {
local kernelver=$1
local bls_target="${blsdir}/${MACHINE_ID}-${kernelver}.conf"
count=0
local -a files
local IFS=$'\n'
prefix="${bls_target%%.conf}"
prefix="${bls_target%%${arch}}"
prefix="${prefix%.*}"
last=($(for bls in ${prefix}.*~custom*.conf ; do
if ! [[ -e "${bls}" ]] ; then
continue
fi
bls="${bls##${prefix}.}"
bls="${bls%%~custom*}"
echo "${bls}"
done | tail -n1)) || :
if [[ -z $last ]]; then
last="0"
else
last=$((last+1))
fi
echo "${bls_target}" | sed -e "s!${prefix}!${prefix}.${last}~custom!"
}
2018-06-18 09:14:35 +00:00
add_bls_fragment() {
local kernel="$1" && shift
local title="$1" && shift
local options="$1" && shift
local initrd="$1" && shift
local extra_initrd="$1" && shift
if [[ $kernel = *"vmlinuz-"* ]]; then
kernelver="${kernel##*/vmlinuz-}"
2018-10-04 15:35:33 +00:00
prefix="vmlinuz-"
2018-06-18 09:14:35 +00:00
else
kernelver="${kernel##*/}"
fi
2018-10-04 15:35:33 +00:00
if [[ ! -f "/boot/${prefix}${kernelver}" ]] &&
2018-06-18 09:14:35 +00:00
[[ $bad_image != "true" ]]; then
print_error "The ${kernelver} kernel isn't installed in the machine"
fi
if [[ -z $title ]]; then
print_error "The kernel title must be specified"
fi
if [[ ! -d $blsdir ]]; then
install -m 700 -d "${blsdir}"
fi
bls_target="${blsdir}/${MACHINE_ID}-${kernelver}.conf"
2018-11-01 23:51:22 +00:00
if [[ -e ${bls_target} ]]; then
bls_target="$(get_custom_bls_filename "${kernelver}")"
print_info "An entry for kernel ${kernelver} already exists, adding ${bls_target}"
fi
2018-06-18 09:14:35 +00:00
kernel_dir="/lib/modules/${kernelver}"
2018-11-26 17:06:24 +00:00
if [[ -d $kernel_dir ]]; then
datetime="$(date -u +%Y%m%d%H%M%S -d "$(stat -c '%y' "${kernel_dir}")")"
2018-06-18 09:14:35 +00:00
else
2018-11-26 17:06:24 +00:00
datetime=0
2018-06-18 09:14:35 +00:00
fi
2018-11-26 17:06:24 +00:00
mkbls "${kernel}" "${kernelver}" "${datetime}" > "${bls_target}"
2018-06-18 09:14:35 +00:00
if [[ -n $title ]]; then
set_bls_value "${bls_target}" "title" "${title}"
fi
if [[ -n $options ]]; then
set_bls_value "${bls_target}" "options" "${options}"
fi
if [[ -n $initrd ]]; then
set_bls_value "${bls_target}" "initrd" "${initrd}"
fi
if [[ -n $extra_initrd ]]; then
2020-06-26 07:38:06 +00:00
append_bls_value "${bls_target}" "initrd" " ${extra_initrd}"
2018-06-18 09:14:35 +00:00
fi
if [[ $MAKEDEBUG = "yes" ]]; then
2018-10-19 18:51:29 +00:00
bls_debug="$(echo ${bls_target} | sed -e "s/${kernelver}/${kernelver}~debug/")"
2018-06-18 09:14:35 +00:00
cp -aT "${bls_target}" "${bls_debug}"
append_bls_value "${bls_debug}" "title" "${LINUX_DEBUG_TITLE_POSTFIX}"
append_bls_value "${bls_debug}" "version" "${LINUX_DEBUG_VERSION_POSTFIX}"
append_bls_value "${bls_debug}" "options" "${CMDLINE_LINUX_DEBUG}"
2018-10-19 18:51:29 +00:00
blsid="$(get_bls_value ${bls_debug} "id" | sed -e "s/${kernelver}/${kernelver}~debug/")"
2018-06-18 09:14:35 +00:00
set_bls_value "${bls_debug}" "id" "${blsid}"
fi
get_bls_values
if [[ $make_default = "true" ]]; then
set_default_bls "TITLE=${title}"
fi
2018-10-05 10:13:49 +00:00
update_grubcfg
2018-06-18 09:14:35 +00:00
exit 0
}
update_args() {
local args=$1 && shift
local remove_args=($1) && shift
local add_args=($1) && shift
for arg in ${remove_args[*]}; do
2020-05-04 12:57:52 +00:00
arg="$(echo $arg | sed -e 's/\//\\\//g')"
2018-06-18 09:14:35 +00:00
if [[ $arg = *"="* ]]; then
2018-11-22 19:35:13 +00:00
args="$(echo $args | sed -E "s/(^|[[:space:]])$arg([[:space:]]|$)/ /")"
2018-06-18 09:14:35 +00:00
else
2018-11-22 19:35:13 +00:00
args="$(echo $args | sed -E "s/(^|[[:space:]])$arg(([[:space:]]|$)|([=][^ ]*([$]*)))/ /g")"
2018-06-18 09:14:35 +00:00
fi
done
2018-11-22 19:35:13 +00:00
for arg in ${add_args[*]}; do
2020-04-29 10:05:04 +00:00
arg="${arg%%=*}"
2020-05-04 12:57:52 +00:00
arg="$(echo $arg | sed -e 's/\//\\\//g')"
2018-11-22 19:35:13 +00:00
args="$(echo $args | sed -E "s/(^|[[:space:]])$arg(([[:space:]]|$)|([=][^ ]*([$]*)))/ /g")"
done
for arg in ${add_args[*]}; do
args="$args $arg"
done
2018-06-18 09:14:35 +00:00
echo ${args}
}
update_bls_fragment() {
2019-02-01 23:37:15 +00:00
local param="$1"
2018-06-18 09:14:35 +00:00
local indexes=($(param_to_indexes "$1")) && shift
local remove_args=$1 && shift
local add_args=$1 && shift
local initrd=$1 && shift
2019-02-28 12:01:40 +00:00
local opts
2018-06-18 09:14:35 +00:00
2018-10-15 13:25:43 +00:00
if [[ $indexes = "-1" ]]; then
2019-02-01 23:37:15 +00:00
print_error "The param $(get_prefix)${param} is incorrect"
2018-10-15 13:25:43 +00:00
fi
2019-02-28 12:01:40 +00:00
if [[ $param = "ALL" && $bootloader = grub2 ]] && [[ -n $remove_args || -n $add_args ]]; then
2020-05-05 09:40:57 +00:00
local old_args=""
2020-05-07 08:15:39 +00:00
if [[ -z $no_etc_update ]] && [[ -e ${grub_etc_default} ]]; then
2020-05-05 09:40:57 +00:00
old_args="$(source ${grub_etc_default}; echo ${GRUB_CMDLINE_LINUX})"
2020-05-07 08:15:39 +00:00
if [[ -n $old_args ]]; then
opts="$(update_args "${old_args}" "${remove_args}" "${add_args}")"
opts="$(echo "$opts" | sed -e 's/\//\\\//g')"
sed -i -e "s/^GRUB_CMDLINE_LINUX.*/GRUB_CMDLINE_LINUX=\\\"${opts}\\\"/" "${grub_etc_default}"
fi
2020-05-05 09:40:57 +00:00
fi
2020-03-30 08:54:20 +00:00
old_args="$(grub2-editenv "${env}" list | grep kernelopts | sed -e "s/kernelopts=//")"
2020-05-07 08:15:39 +00:00
if [[ -n $old_args ]]; then
opts="$(update_args "${old_args}" "${remove_args}" "${add_args}")"
grub2-editenv "${env}" set kernelopts="${opts}"
fi
2019-02-28 12:01:40 +00:00
elif [[ $bootloader = grub2 ]]; then
opts="$(grub2-editenv "${env}" list | grep kernelopts | sed -e "s/kernelopts=//")"
fi
2018-06-18 09:14:35 +00:00
for i in ${indexes[*]}; do
if [[ -n $remove_args || -n $add_args ]]; then
2018-10-16 18:06:33 +00:00
local old_args="$(get_bls_args "$i")"
local new_args="$(update_args "${old_args}" "${remove_args}" "${add_args}")"
2019-02-28 12:01:40 +00:00
2019-11-27 16:08:03 +00:00
if [[ $param != "ALL" || "$(has_kernelopts "$i")" = "false" ]]; then
2019-02-28 12:01:40 +00:00
set_bls_value "${bls_file[$i]}" "options" "${new_args}"
fi
2018-06-18 09:14:35 +00:00
fi
if [[ -n $initrd ]]; then
set_bls_value "${bls_file[$i]}" "initrd" "${initrd}"
fi
done
2018-10-05 10:13:49 +00:00
2022-08-22 21:47:49 +00:00
if [[ $param = "ALL" ]] && [[ -n $remove_args || -n $add_args ]]; then
2022-08-02 20:09:04 +00:00
if [[ ! -f /etc/kernel/cmdline ]]; then
# anaconda could pre-populate this file, but until then, most of
# the time we'll just want the most recent one. This is pretty
# close to the current almost-correct behavior of falling back to
# /proc/cmdline anyhow.
echo "$(get_bls_args -1)" > /etc/kernel/cmdline
fi
read old_args < /etc/kernel/cmdline
local new_args="$(update_args "${old_args}" "${remove_args}" "${add_args}")"
echo "$new_args" > /etc/kernel/cmdline
fi
2018-10-05 10:13:49 +00:00
update_grubcfg
2018-06-18 09:14:35 +00:00
}
set_default_bls() {
local index=($(param_to_indexes "$1"))
2018-10-15 13:25:43 +00:00
if [[ $index = "-1" ]]; then
print_error "The param $1 is incorrect"
fi
2018-06-18 09:14:35 +00:00
if [[ $bootloader = grub2 ]]; then
2018-10-15 12:56:02 +00:00
grub2-editenv "${env}" set saved_entry="${bls_id[$index]}"
2018-06-18 09:14:35 +00:00
else
2018-11-21 09:44:33 +00:00
local default="${bls_title[$index]}"
2018-06-18 09:14:35 +00:00
local current="$(grep '^default=' ${zipl_config} | sed -e 's/^default=//')"
if [[ -n $current ]]; then
sed -i -e "s,^default=.*,default=${default}," "${zipl_config}"
else
echo "default=${default}" >> "${zipl_config}"
fi
fi
2018-10-05 12:31:13 +00:00
2018-11-16 23:03:02 +00:00
print_info "The default is ${bls_file[$index]} with index $index and kernel $(get_prefix)${bls_linux[$index]}"
2018-06-18 09:14:35 +00:00
}
remove_var_prefix() {
2019-08-05 18:01:52 +00:00
local prefix="$1"
[ -z "${prefix}" ] && return
2018-06-18 09:14:35 +00:00
if [[ -n $remove_kernel && $remove_kernel =~ ^/ ]]; then
2019-08-05 18:01:52 +00:00
remove_kernel="/${remove_kernel##${prefix}/}"
2018-06-18 09:14:35 +00:00
fi
if [[ -n $initrd ]]; then
2019-08-05 18:01:52 +00:00
initrd="/${initrd##${prefix}/}"
2018-06-18 09:14:35 +00:00
fi
if [[ -n $extra_initrd ]]; then
2020-06-26 07:38:06 +00:00
extra_initrd="/${extra_initrd##${prefix}/}"
2018-06-18 09:14:35 +00:00
fi
if [[ -n $kernel ]]; then
2019-08-05 18:01:52 +00:00
kernel="/${kernel##${prefix}/}"
2018-06-18 09:14:35 +00:00
fi
if [[ -n $update_kernel && $update_kernel =~ ^/ ]]; then
2019-08-05 18:01:52 +00:00
update_kernel="/${update_kernel##${prefix}/}"
2018-06-18 09:14:35 +00:00
fi
}
2018-10-05 10:13:49 +00:00
update_grubcfg()
{
2021-04-26 16:43:51 +00:00
# Older ppc64le OPAL firmware (petitboot version < 1.8.0) don't have BLS support
# so grub2-mkconfig has to be run to generate a config with menuentry commands.
if [ "${arch}" = "ppc64le" ] && [ -d /sys/firmware/opal ]; then
RUN_MKCONFIG="true"
petitboot_path="/sys/firmware/devicetree/base/ibm,firmware-versions/petitboot"
if test -e ${petitboot_path}; then
read -r -d '' petitboot_version < ${petitboot_path}
petitboot_version="$(echo ${petitboot_version//v})"
if test -n ${petitboot_version}; then
major_version="$(echo ${petitboot_version} | cut -d . -f1)"
minor_version="$(echo ${petitboot_version} | cut -d . -f2)"
re='^[0-9]+$'
if [[ $major_version =~ $re ]] && [[ $minor_version =~ $re ]] &&
([[ ${major_version} -gt 1 ]] ||
[[ ${major_version} -eq 1 &&
${minor_version} -ge 8 ]]); then
RUN_MKCONFIG="false"
fi
fi
fi
fi
if [[ $RUN_MKCONFIG = "true" ]]; then
grub2-mkconfig --no-grubenv-update -o "${grub_config}" >& /dev/null
2018-10-05 10:13:49 +00:00
fi
}
2018-06-18 09:14:35 +00:00
print_usage()
{
cat <<EOF
Usage: grubby [OPTION...]
--add-kernel=kernel-path add an entry for the specified kernel
--args=args default arguments for the new kernel or new arguments for kernel being updated)
--bad-image-okay don't sanity check images in boot entries (for testing only)
-c, --config-file=path path to grub config file to update ("-" for stdin)
--copy-default use the default boot entry as a template for the new entry being added; if the default is not a linux image, or if the kernel referenced by the default image does not exist, the
first linux entry whose kernel does exist is used as the template
--default-kernel display the path of the default kernel
--default-index display the index of the default kernel
--default-title display the title of the default kernel
--env=path path for environment data
--grub2 configure grub2 bootloader
--info=kernel-path display boot information for specified kernel
--initrd=initrd-path initrd image for the new kernel
-i, --extra-initrd=initrd-path auxiliary initrd image for things other than the new kernel
--make-default make the newly added entry the default boot entry
--remove-args=STRING remove kernel arguments
--remove-kernel=kernel-path remove all entries for the specified kernel
--set-default=kernel-path make the first entry referencing the specified kernel the default
--set-default-index=entry-index make the given entry index the default entry
--title=entry-title title to use for the new kernel entry
--update-kernel=kernel-path updated information for the specified kernel
--zipl configure zipl bootloader
-b, --bls-directory path to directory containing the BootLoaderSpec fragment files
2020-05-05 09:40:57 +00:00
--no-etc-grub-update don't update the GRUB_CMDLINE_LINUX variable in /etc/default/grub
2018-06-18 09:14:35 +00:00
Help options:
2022-02-07 21:42:54 +00:00
-h, --help Show this help message
2018-06-18 09:14:35 +00:00
EOF
}
2022-02-07 21:42:54 +00:00
OPTS="$(getopt -o hc:i:b:? --long help,add-kernel:,args:,bad-image-okay,\
2018-06-18 09:14:35 +00:00
config-file:,copy-default,default-kernel,default-index,default-title,env:,\
2019-11-22 14:52:17 +00:00
grub2,info:,initrd:,extra-initrd:,make-default,remove-args:,\
2018-06-18 09:14:35 +00:00
remove-kernel:,set-default:,set-default-index:,title:,update-kernel:,zipl,\
2020-05-05 09:40:57 +00:00
bls-directory:,no-etc-grub-update,add-multiboot:,mbargs:,mounts:,boot-filesystem:,\
2018-06-18 09:14:35 +00:00
bootloader-probe,debug,devtree,devtreedir:,elilo,efi,extlinux,grub,lilo,\
output-file:,remove-mbargs:,remove-multiboot:,silo,yaboot -n ${SCRIPTNAME} -- "$@")"
[[ $? = 0 ]] || exit 1
eval set -- "$OPTS"
while [ ${#} -gt 0 ]; do
case "$1" in
--help|-h)
print_usage
exit 0
;;
--add-kernel)
kernel="${2}"
shift
;;
--args)
args="${2}"
shift
;;
--bad-image-okay)
bad_image=true
;;
--config-file|-c)
2019-11-22 14:52:17 +00:00
grub_config="${2}"
2018-06-18 09:14:35 +00:00
zipl_config="${2}"
shift
;;
--copy-default)
copy_default=true
;;
--default-kernel)
display_default="kernel"
;;
--default-index)
display_default="index"
;;
--default-title)
display_default="title"
;;
--env)
env="${2}"
shift
;;
--grub2)
bootloader="grub2"
;;
--info)
display_info="${2}"
shift
;;
--initrd)
initrd="${2}"
shift
;;
--extra-initrd|-i)
2020-06-26 07:38:06 +00:00
extra_initrd="${2}"
2018-06-18 09:14:35 +00:00
shift
;;
--make-default)
make_default=true
;;
--remove-args)
remove_args="${2}"
shift
;;
--remove-kernel)
remove_kernel="${2}"
shift
;;
--set-default)
set_default="${2}"
shift
;;
--set-default-index)
set_default="${2}"
shift
;;
--title)
title="${2}"
shift
;;
--update-kernel)
update_kernel="${2}"
shift
;;
--zipl)
bootloader="zipl"
;;
--bls-directory|-b)
blsdir="${2}"
shift
;;
2020-05-05 09:40:57 +00:00
--no-etc-grub-update)
no_etc_update=true
shift
;;
--add-multiboot|--mbargs|--mounts|--boot-filesystem|\
2018-06-18 09:14:35 +00:00
--bootloader-probe|--debug|--devtree|--devtreedir|--elilo|--efi|\
--extlinux|--grub|--lilo|--output-file|--remove-mbargs|--silo|\
--remove-multiboot|--slilo|--yaboot)
echo
echo "${SCRIPTNAME}: the option \"${1}\" was deprecated" >&2
echo "Try '${SCRIPTNAME} --help' to list supported options" >&2
echo
exit 1
;;
--)
shift
break
;;
*)
echo
echo "${SCRIPTNAME}: invalid option \"${1}\"" >&2
echo "Try '${SCRIPTNAME} --help' for more information" >&2
echo
exit 1
;;
esac
shift
done
2019-03-20 16:15:00 +00:00
if [[ -z $update_kernel && -z $kernel ]] && [[ -n $args || -n $remove_args ]]; then
2019-02-14 09:33:36 +00:00
print_error "no action specified"
fi
2018-06-18 09:14:35 +00:00
if [[ -z $blsdir ]]; then
blsdir="/boot/loader/entries"
fi
if [[ -z $env ]]; then
env="/boot/grub2/grubenv"
fi
if [[ -z $zipl_config ]]; then
zipl_config="/etc/zipl.conf"
fi
2019-11-22 14:52:17 +00:00
if [[ -z $grub_config ]]; then
grub_config="/boot/grub2/grub.cfg"
fi
2020-03-30 08:54:20 +00:00
if [[ -z $grub_etc_default ]]; then
grub_etc_default="/etc/default/grub"
fi
2018-06-18 09:14:35 +00:00
get_bls_values
default_index="$(get_default_index)"
if [[ -n $display_default ]]; then
display_default_value
fi
if [[ -n $display_info ]]; then
display_info_values "${display_info}"
fi
2019-08-05 18:01:52 +00:00
remove_var_prefix "$(get_prefix)"
2018-06-18 09:14:35 +00:00
if [[ -n $kernel ]]; then
2023-02-21 18:33:02 +00:00
if [[ $copy_default = "true" ]]; then
opts="${bls_options[$default_index]}"
if [[ -n $args ]]; then
opts="${opts} ${args}"
fi
else
opts="${opts} ${args}"
remove_args="$kernelopts"
update_args "${opts}" "${remove_args}" ""
2018-06-18 09:14:35 +00:00
fi
add_bls_fragment "${kernel}" "${title}" "${opts}" "${initrd}" \
"${extra_initrd}"
fi
if [[ -n $remove_kernel ]]; then
remove_bls_fragment "${remove_kernel}"
fi
if [[ -n $update_kernel ]]; then
update_bls_fragment "${update_kernel}" "${remove_args}" "${args}" "${initrd}"
fi
if [[ -n $set_default ]]; then
set_default_bls "${set_default}"
fi
exit 0