19db5c4
#!/bin/bash
19db5c4
#
19db5c4
# grubby wrapper to manage BootLoaderSpec files
19db5c4
#
19db5c4
#
19db5c4
# Copyright 2018 Red Hat, Inc.  All rights reserved.
19db5c4
#
19db5c4
# This program is free software; you can redistribute it and/or modify
19db5c4
# it under the terms of the GNU General Public License as published by
19db5c4
# the Free Software Foundation; either version 2 of the License, or
19db5c4
# (at your option) any later version.
19db5c4
#
19db5c4
# This program is distributed in the hope that it will be useful,
19db5c4
# but WITHOUT ANY WARRANTY; without even the implied warranty of
19db5c4
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19db5c4
# GNU General Public License for more details.
19db5c4
#
19db5c4
# You should have received a copy of the GNU General Public License
19db5c4
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
19db5c4
19db5c4
readonly SCRIPTNAME="${0##*/}"
19db5c4
19db5c4
CMDLINE_LINUX_DEBUG=" systemd.log_level=debug systemd.log_target=kmsg"
19db5c4
LINUX_DEBUG_VERSION_POSTFIX="_with_debugging"
19db5c4
LINUX_DEBUG_TITLE_POSTFIX=" with debugging"
19db5c4
19db5c4
declare -a bls_file
19db5c4
declare -a bls_title
19db5c4
declare -a bls_version
19db5c4
declare -a bls_linux
19db5c4
declare -a bls_initrd
19db5c4
declare -a bls_options
19db5c4
declare -a bls_id
19db5c4
19db5c4
[[ -f /etc/sysconfig/kernel ]] && . /etc/sysconfig/kernel
19db5c4
[[ -f /etc/os-release ]] && . /etc/os-release
19db5c4
read MACHINE_ID < /etc/machine-id
19db5c4
arch=$(uname -m)
19db5c4
19db5c4
if [[ $arch = 's390' || $arch = 's390x' ]]; then
19db5c4
    bootloader="zipl"
19db5c4
else
19db5c4
    bootloader="grub2"
19db5c4
fi
19db5c4
19db5c4
print_error() {
19db5c4
    echo "$1" >&2
19db5c4
    exit 1
19db5c4
}
19db5c4
33ab171
print_info() {
33ab171
    echo "$1" >&2
33ab171
}
33ab171
19db5c4
if [[ ${#} = 0 ]]; then
19db5c4
    print_error "no action specified"
19db5c4
fi
19db5c4
19db5c4
get_bls_value() {
19db5c4
    local bls="$1" && shift
19db5c4
    local key="$1" && shift
19db5c4
28952a6
    echo "$(grep "^${key}[ \t]" "${bls}" | sed -e "s!^${key}[ \t]*!!")"
19db5c4
}
19db5c4
19db5c4
set_bls_value() {
19db5c4
    local bls="$1" && shift
19db5c4
    local key="$1" && shift
19db5c4
    local value="$1" && shift
19db5c4
7d53b5e
    value=$(echo $value | sed -e 's/\//\\\//g')
7d53b5e
    sed -i -e "s/^${key}.*/${key} ${value}/" "${bls}"
19db5c4
}
19db5c4
19db5c4
append_bls_value() {
19db5c4
    local bls="$1" && shift
19db5c4
    local key="$1" && shift
19db5c4
    local value="$1" && shift
19db5c4
a5e56a6
    old_value="$(get_bls_value "${bls}" ${key})"
19db5c4
    set_bls_value "${bls}" "${key}" "${old_value}${value}"
19db5c4
}
19db5c4
19db5c4
get_bls_values() {
19db5c4
    count=0
a5e56a6
    local -a files
a5e56a6
    local IFS=$'\n'
a5e56a6
    files=($(for bls in ${blsdir}/*.conf ; do
a5e56a6
        if ! [[ -e "${bls}" ]] ; then
a5e56a6
            continue
a5e56a6
        fi
a5e56a6
        bls="${bls%.conf}"
a5e56a6
        bls="${bls##*/}"
a5e56a6
        echo "${bls}"
77a8c0b
    done | /usr/libexec/grubby/rpm-sort -c rpmnvrcmp 2>/dev/null | tac)) || :
a5e56a6
a5e56a6
    for bls in "${files[@]}" ; do
a5e56a6
        blspath="${blsdir}/${bls}.conf"
a5e56a6
        bls_file[$count]="${blspath}"
a5e56a6
        bls_title[$count]="$(get_bls_value ${blspath} title)"
a5e56a6
        bls_version[$count]="$(get_bls_value ${blspath} version)"
a5e56a6
        bls_linux[$count]="$(get_bls_value ${blspath} linux)"
a5e56a6
        bls_initrd[$count]="$(get_bls_value ${blspath} initrd)"
a5e56a6
        bls_options[$count]="$(get_bls_value ${blspath} options)"
a5e56a6
        bls_id[$count]="${bls}"
19db5c4
19db5c4
        count=$((count+1))
19db5c4
    done
19db5c4
}
19db5c4
19db5c4
get_default_index() {
19db5c4
    local default=""
19db5c4
    local index="-1"
19db5c4
    local title=""
19db5c4
    local version=""
19db5c4
    if [[ $bootloader = "grub2" ]]; then
19db5c4
	default="$(grep '^saved_entry=' ${env} | sed -e 's/^saved_entry=//')"
19db5c4
    else
19db5c4
	default="$(grep '^default=' ${zipl_config} | sed -e 's/^default=//')"
19db5c4
    fi
19db5c4
19db5c4
    if [[ -z $default ]]; then
19db5c4
	index=0
19db5c4
    elif [[ $default =~ ^[0-9]+$ ]]; then
19db5c4
	index="$default"
19db5c4
    fi
19db5c4
19db5c4
    for i in ${!bls_file[@]}; do
2ef6823
        if [[ $i -eq $index ]]; then
2ef6823
            echo $i
2ef6823
            return
2ef6823
        fi
2ef6823
6b2ae0e
        if [[ $default = ${bls_id[$i]} || $default = ${bls_title[$i]} ]]; then
19db5c4
            echo $i
19db5c4
            return
19db5c4
        fi
19db5c4
    done
19db5c4
}
19db5c4
19db5c4
display_default_value() {
d1dd184
    local prefix=$(get_prefix)
d1dd184
19db5c4
    case "$display_default" in
19db5c4
        kernel)
d1dd184
            echo "${prefix}${bls_linux[$default_index]}"
19db5c4
            exit 0
19db5c4
            ;;
19db5c4
        index)
19db5c4
            echo "$default_index"
19db5c4
            exit 0
19db5c4
            ;;
19db5c4
        title)
19db5c4
            echo "${bls_title[$default_index]}"
19db5c4
            exit 0
19db5c4
            ;;
19db5c4
    esac
19db5c4
}
19db5c4
19db5c4
param_to_indexes() {
19db5c4
    local param="$1"
19db5c4
    local indexes=""
19db5c4
19db5c4
    if [[ $param = "ALL" ]]; then
19db5c4
        for i in ${!bls_file[@]}; do
19db5c4
            indexes="$indexes $i"
19db5c4
        done
19db5c4
        echo -n $indexes
19db5c4
        return
19db5c4
    fi
19db5c4
19db5c4
    if [[ $param = "DEFAULT" ]]; then
19db5c4
        echo -n $default_index
19db5c4
        return
19db5c4
    fi
19db5c4
19db5c4
    for i in ${!bls_file[@]}; do
f988064
        if [[ $param = "${bls_linux[$i]}" || "/${param##*/}" = "${bls_linux[$i]}" ]]; then
19db5c4
            indexes="$indexes $i"
19db5c4
        fi
19db5c4
19db5c4
        if [[ $param = "TITLE=${bls_title[$i]}" ]]; then
19db5c4
            indexes="$indexes $i"
19db5c4
        fi
19db5c4
19db5c4
	if [[ $param = $i ]]; then
19db5c4
	    indexes="$indexes $i"
19db5c4
	fi
19db5c4
    done
19db5c4
19db5c4
    if [[ -n $indexes ]]; then
19db5c4
        echo -n $indexes
19db5c4
        return
19db5c4
    fi
19db5c4
19db5c4
    echo -n "-1"
19db5c4
}
19db5c4
6cdfe45
get_prefix() {
73b809b
    if [[ $bootloader = grub2 ]] && mountpoint -q /boot; then
6cdfe45
	echo "/boot"
6cdfe45
    else
6cdfe45
	echo ""
6cdfe45
    fi
6cdfe45
}
6cdfe45
b84a214
expand_var() {
b84a214
    local var=$1
b84a214
b84a214
    if [[ $bootloader == "grub2" ]]; then
b84a214
        local value="$(grub2-editenv "${env}" list | grep ${var##$} | sed -e "s/${var##$}=//")"
b84a214
        value="$(echo ${value} | sed -e 's/\//\\\//g')"
b84a214
        if [[ -n $value ]]; then
b84a214
            var="$value"
b84a214
        fi
b84a214
    fi
b84a214
b84a214
    echo $var
b84a214
}
b84a214
6abeb6f
has_kernelopts()
6abeb6f
{
6abeb6f
    local args=${bls_options[$1]}
6abeb6f
    local opts=(${args})
6abeb6f
6abeb6f
    for opt in ${opts[*]}; do
08153fc
        [[ $opt = "\$kernelopts" ]] && echo "true"
6abeb6f
    done
6abeb6f
08153fc
    echo "false"
6abeb6f
}
6abeb6f
290786e
get_bls_args() {
6abeb6f
    local args=${bls_options[$1]}
290786e
    local opts=(${args})
290786e
290786e
    for opt in ${opts[*]}; do
2b59d13
        if [[ $opt = "\$kernelopts" ]]; then
290786e
            value="$(expand_var $opt)"
290786e
            args="$(echo ${args} | sed -e "s/${opt}/${value}/")"
290786e
        fi
290786e
    done
290786e
290786e
    echo ${args}
290786e
}
290786e
19db5c4
display_info_values() {
19db5c4
    local indexes=($(param_to_indexes "$1"))
6cdfe45
    local prefix=$(get_prefix)
19db5c4
b40ea5b
    if [[ $indexes = "-1" ]]; then
b40ea5b
        print_error "The param $1 is incorrect"
b40ea5b
    fi
b40ea5b
19db5c4
    for i in ${indexes[*]}; do
695377d
        local root=""
b84a214
        local value=""
290786e
        local args="$(get_bls_args "$i")"
b84a214
b84a214
        local opts=(${args})
b84a214
b84a214
        for opt in ${opts[*]}; do
695377d
            if echo $opt | grep -q "^root="; then
695377d
                root="$(echo $opt | sed -e 's/root=//')"
695377d
                value="$(echo ${opt} | sed -e 's/\//\\\//g')"
695377d
                args="$(echo ${args} | sed -e "s/${value}[ \t]*//")"
695377d
                break
695377d
            fi
695377d
        done
695377d
19db5c4
        echo "index=$i"
6cdfe45
        echo "kernel=\"${prefix}${bls_linux[$i]}\""
695377d
        echo "args=\"${args}\""
695377d
695377d
        if [[ -n $root ]]; then
695377d
            echo "root=\"${root}\""
695377d
        fi
695377d
6cdfe45
        echo "initrd=\"${prefix}${bls_initrd[$i]}\""
a5e56a6
        echo "title=\"${bls_title[$i]}\""
4aa0918
        echo "id=\"${bls_id[$i]}\""
19db5c4
    done
19db5c4
    exit 0
19db5c4
}
19db5c4
19db5c4
mkbls() {
19db5c4
    local kernel=$1 && shift
19db5c4
    local kernelver=$1 && shift
19db5c4
    local datetime=$1 && shift
19db5c4
19db5c4
    local debugname=""
19db5c4
    local flavor=""
6cdfe45
    local prefix=""
6cdfe45
6cdfe45
    if [[ $(get_prefix) = "" ]]; then
6cdfe45
	prefix="/boot"
6cdfe45
    fi
19db5c4
19db5c4
    if [[ $kernelver == *\+* ]] ; then
19db5c4
        local flavor=-"${kernelver##*+}"
19db5c4
        if [[ $flavor == "-debug" ]]; then
19db5c4
            local debugname="with debugging"
19db5c4
	    local debugid="-debug"
19db5c4
        fi
19db5c4
    fi
19db5c4
19db5c4
    cat <
19db5c4
title ${NAME} (${kernelver}) ${VERSION}${debugname}
19db5c4
version ${kernelver}${debugid}
19db5c4
linux ${kernel}
6cdfe45
initrd ${prefix}/initramfs-${kernelver}.img
19db5c4
options \$kernelopts
19db5c4
id ${ID}-${datetime}-${kernelver}${debugid}
19db5c4
grub_users \$grub_users
19db5c4
grub_arg --unrestricted
19db5c4
grub_class kernel${flavor}
19db5c4
EOF
19db5c4
}
19db5c4
a772214
unset_default_bls()
a772214
{
a772214
    if [[ $bootloader = grub2 ]]; then
a772214
        grub2-editenv "${env}" unset saved_entry
a772214
    else
a772214
        sed -i -e "/^default=.*/d" "${zipl_config}"
a772214
    fi
a772214
}
a772214
19db5c4
remove_bls_fragment() {
19db5c4
    local indexes=($(param_to_indexes "$1"))
19db5c4
19db5c4
    if [[ $indexes = "-1" ]]; then
f742fa1
	print_error "The param $(get_prefix)$1 is incorrect"
19db5c4
    fi
19db5c4
a5e56a6
    for i in "${indexes[@]}"; do
a772214
        if [[ $default_index = $i ]]; then
a772214
            unset_default_bls
a772214
        fi
19db5c4
        rm -f "${bls_file[$i]}"
19db5c4
    done
19db5c4
19db5c4
    get_bls_values
b8c8cdc
b8c8cdc
    update_grubcfg
19db5c4
}
19db5c4
9a80bb6
get_custom_bls_filename() {
9a80bb6
    local kernelver=$1
9a80bb6
    local bls_target="${blsdir}/${MACHINE_ID}-${kernelver}.conf"
9a80bb6
    count=0
9a80bb6
    local -a files
9a80bb6
    local IFS=$'\n'
9a80bb6
9a80bb6
    prefix="${bls_target%%.conf}"
9a80bb6
    prefix="${bls_target%%${arch}}"
9a80bb6
    prefix="${prefix%.*}"
9a80bb6
9a80bb6
    last=($(for bls in ${prefix}.*~custom*.conf ; do
9a80bb6
        if ! [[ -e "${bls}" ]] ; then
9a80bb6
            continue
9a80bb6
        fi
9a80bb6
        bls="${bls##${prefix}.}"
9a80bb6
        bls="${bls%%~custom*}"
9a80bb6
        echo "${bls}"
9a80bb6
    done | tail -n1)) || :
9a80bb6
9a80bb6
    if [[ -z $last ]]; then
9a80bb6
        last="0"
9a80bb6
    else
9a80bb6
        last=$((last+1))
9a80bb6
    fi
9a80bb6
9a80bb6
    echo "${bls_target}" | sed -e "s!${prefix}!${prefix}.${last}~custom!"
9a80bb6
}
9a80bb6
19db5c4
add_bls_fragment() {
19db5c4
    local kernel="$1" && shift
19db5c4
    local title="$1" && shift
19db5c4
    local options="$1" && shift
19db5c4
    local initrd="$1" && shift
19db5c4
    local extra_initrd="$1" && shift
19db5c4
19db5c4
    if [[ $kernel = *"vmlinuz-"* ]]; then
19db5c4
	kernelver="${kernel##*/vmlinuz-}"
f988064
	prefix="vmlinuz-"
19db5c4
    else
19db5c4
	kernelver="${kernel##*/}"
19db5c4
    fi
19db5c4
f988064
    if [[ ! -f "/boot/${prefix}${kernelver}" ]] &&
19db5c4
       [[ $bad_image != "true" ]]; then
19db5c4
        print_error "The ${kernelver} kernel isn't installed in the machine"
19db5c4
    fi
19db5c4
19db5c4
    if [[ -z $title ]]; then
19db5c4
	print_error "The kernel title must be specified"
19db5c4
    fi
19db5c4
19db5c4
    if [[ ! -d $blsdir ]]; then
19db5c4
        install -m 700 -d "${blsdir}"
19db5c4
    fi
19db5c4
19db5c4
    bls_target="${blsdir}/${MACHINE_ID}-${kernelver}.conf"
9a80bb6
9a80bb6
    if [[ -e ${bls_target} ]]; then
9a80bb6
        bls_target="$(get_custom_bls_filename "${kernelver}")"
9a80bb6
        print_info "An entry for kernel ${kernelver} already exists, adding ${bls_target}"
9a80bb6
    fi
9a80bb6
19db5c4
    kernel_dir="/lib/modules/${kernelver}"
f1aafa6
    if [[ -d $kernel_dir ]]; then
f1aafa6
        datetime="$(date -u +%Y%m%d%H%M%S -d "$(stat -c '%y' "${kernel_dir}")")"
19db5c4
    else
f1aafa6
        datetime=0
19db5c4
    fi
f1aafa6
    mkbls "${kernel}" "${kernelver}" "${datetime}" > "${bls_target}"
19db5c4
19db5c4
    if [[ -n $title ]]; then
19db5c4
        set_bls_value "${bls_target}" "title" "${title}"
19db5c4
    fi
19db5c4
19db5c4
    if [[ -n $options ]]; then
19db5c4
        set_bls_value "${bls_target}" "options" "${options}"
19db5c4
    fi
19db5c4
19db5c4
    if [[ -n $initrd ]]; then
19db5c4
        set_bls_value "${bls_target}" "initrd" "${initrd}"
19db5c4
    fi
19db5c4
19db5c4
    if [[ -n $extra_initrd ]]; then
a5c9c3a
        append_bls_value "${bls_target}" "initrd" " ${extra_initrd}"
19db5c4
    fi
19db5c4
19db5c4
    if [[ $MAKEDEBUG = "yes" ]]; then
1c34519
        bls_debug="$(echo ${bls_target} | sed -e "s/${kernelver}/${kernelver}~debug/")"
19db5c4
        cp -aT  "${bls_target}" "${bls_debug}"
19db5c4
        append_bls_value "${bls_debug}" "title" "${LINUX_DEBUG_TITLE_POSTFIX}"
19db5c4
        append_bls_value "${bls_debug}" "version" "${LINUX_DEBUG_VERSION_POSTFIX}"
19db5c4
        append_bls_value "${bls_debug}" "options" "${CMDLINE_LINUX_DEBUG}"
1c34519
        blsid="$(get_bls_value ${bls_debug} "id" | sed -e "s/${kernelver}/${kernelver}~debug/")"
19db5c4
        set_bls_value "${bls_debug}" "id" "${blsid}"
19db5c4
    fi
19db5c4
19db5c4
    get_bls_values
19db5c4
19db5c4
    if [[ $make_default = "true" ]]; then
19db5c4
        set_default_bls "TITLE=${title}"
19db5c4
    fi
19db5c4
b8c8cdc
    update_grubcfg
b8c8cdc
19db5c4
    exit 0
19db5c4
}
19db5c4
19db5c4
update_args() {
19db5c4
    local args=$1 && shift
19db5c4
    local remove_args=($1) && shift
19db5c4
    local add_args=($1) && shift
19db5c4
19db5c4
    for arg in ${remove_args[*]}; do
b1eee22
        arg="$(echo $arg | sed -e 's/\//\\\//g')"
19db5c4
        if [[ $arg = *"="* ]]; then
4b1d63c
            args="$(echo $args | sed -E "s/(^|[[:space:]])$arg([[:space:]]|$)/ /")"
19db5c4
        else
4b1d63c
            args="$(echo $args | sed -E "s/(^|[[:space:]])$arg(([[:space:]]|$)|([=][^ ]*([$]*)))/ /g")"
19db5c4
        fi
19db5c4
    done
19db5c4
4b1d63c
    for arg in ${add_args[*]}; do
f57406f
        arg="${arg%%=*}"
b1eee22
        arg="$(echo $arg | sed -e 's/\//\\\//g')"
4b1d63c
        args="$(echo $args | sed -E "s/(^|[[:space:]])$arg(([[:space:]]|$)|([=][^ ]*([$]*)))/ /g")"
4b1d63c
    done
4b1d63c
4b1d63c
    for arg in ${add_args[*]}; do
4b1d63c
        args="$args $arg"
4b1d63c
    done
4b1d63c
19db5c4
    echo ${args}
19db5c4
}
19db5c4
19db5c4
update_bls_fragment() {
f742fa1
    local param="$1"
19db5c4
    local indexes=($(param_to_indexes "$1")) && shift
19db5c4
    local remove_args=$1 && shift
19db5c4
    local add_args=$1 && shift
19db5c4
    local initrd=$1 && shift
6abeb6f
    local opts
19db5c4
b40ea5b
    if [[ $indexes = "-1" ]]; then
f742fa1
        print_error "The param $(get_prefix)${param} is incorrect"
b40ea5b
    fi
b40ea5b
6abeb6f
    if [[ $param = "ALL" && $bootloader = grub2 ]] && [[ -n $remove_args || -n $add_args ]]; then
f24781c
        local old_args=""
f24781c
770392f
        if [[ -z $no_etc_update ]] && [[ -e ${grub_etc_default} ]]; then
f24781c
            old_args="$(source ${grub_etc_default}; echo ${GRUB_CMDLINE_LINUX})"
770392f
            if [[ -n $old_args ]]; then
770392f
                opts="$(update_args "${old_args}" "${remove_args}" "${add_args}")"
770392f
                opts="$(echo "$opts" | sed -e 's/\//\\\//g')"
770392f
                sed -i -e "s/^GRUB_CMDLINE_LINUX.*/GRUB_CMDLINE_LINUX=\\\"${opts}\\\"/" "${grub_etc_default}"
770392f
            fi
f24781c
        fi
8c7d866
8c7d866
        old_args="$(grub2-editenv "${env}" list | grep kernelopts | sed -e "s/kernelopts=//")"
770392f
        if [[ -n $old_args ]]; then
770392f
            opts="$(update_args "${old_args}" "${remove_args}" "${add_args}")"
770392f
            grub2-editenv "${env}" set kernelopts="${opts}"
770392f
        fi
6abeb6f
    elif [[ $bootloader = grub2 ]]; then
6abeb6f
        opts="$(grub2-editenv "${env}" list | grep kernelopts | sed -e "s/kernelopts=//")"
6abeb6f
    fi
6abeb6f
19db5c4
    for i in ${indexes[*]}; do
19db5c4
	if [[ -n $remove_args || -n $add_args ]]; then
84881fb
            local old_args="$(get_bls_args "$i")"
84881fb
            local new_args="$(update_args "${old_args}" "${remove_args}" "${add_args}")"
6abeb6f
08153fc
            if [[ $param != "ALL" || "$(has_kernelopts "$i")" = "false" ]]; then
6abeb6f
                set_bls_value "${bls_file[$i]}" "options" "${new_args}"
6abeb6f
            fi
19db5c4
	fi
19db5c4
19db5c4
	if [[ -n $initrd ]]; then
19db5c4
	    set_bls_value "${bls_file[$i]}" "initrd" "${initrd}"
19db5c4
	fi
19db5c4
    done
b8c8cdc
b8c8cdc
    update_grubcfg
19db5c4
}
19db5c4
19db5c4
set_default_bls() {
19db5c4
    local index=($(param_to_indexes "$1"))
19db5c4
b40ea5b
    if [[ $index = "-1" ]]; then
b40ea5b
        print_error "The param $1 is incorrect"
b40ea5b
    fi
b40ea5b
19db5c4
    if [[ $bootloader = grub2 ]]; then
cf42506
        grub2-editenv "${env}" set saved_entry="${bls_id[$index]}"
19db5c4
    else
791d554
        local default="${bls_title[$index]}"
19db5c4
        local current="$(grep '^default=' ${zipl_config} | sed -e 's/^default=//')"
19db5c4
        if [[ -n $current ]]; then
19db5c4
            sed -i -e "s,^default=.*,default=${default}," "${zipl_config}"
19db5c4
        else
19db5c4
            echo "default=${default}" >> "${zipl_config}"
19db5c4
        fi
19db5c4
    fi
33ab171
6cdfe45
    print_info "The default is ${bls_file[$index]} with index $index and kernel $(get_prefix)${bls_linux[$index]}"
19db5c4
}
19db5c4
19db5c4
remove_var_prefix() {
Yuval Turgeman 9baffd0
    local prefix="$1"
Yuval Turgeman 9baffd0
Yuval Turgeman 9baffd0
    [ -z "${prefix}" ] && return
Yuval Turgeman 9baffd0
19db5c4
    if [[ -n $remove_kernel && $remove_kernel =~ ^/ ]]; then
Yuval Turgeman 9baffd0
       remove_kernel="/${remove_kernel##${prefix}/}"
19db5c4
    fi
19db5c4
19db5c4
    if [[ -n $initrd ]]; then
Yuval Turgeman 9baffd0
	initrd="/${initrd##${prefix}/}"
19db5c4
    fi
19db5c4
19db5c4
    if [[ -n $extra_initrd ]]; then
a5c9c3a
	extra_initrd="/${extra_initrd##${prefix}/}"
19db5c4
    fi
19db5c4
19db5c4
    if [[ -n $kernel ]]; then
Yuval Turgeman 9baffd0
	kernel="/${kernel##${prefix}/}"
19db5c4
    fi
19db5c4
19db5c4
    if [[ -n $update_kernel && $update_kernel =~ ^/ ]]; then
Yuval Turgeman 9baffd0
	update_kernel="/${update_kernel##${prefix}/}"
19db5c4
    fi
19db5c4
}
19db5c4
b8c8cdc
update_grubcfg()
b8c8cdc
{
5d39638
    # Older ppc64le OPAL firmware (petitboot version < 1.8.0) don't have BLS support
5d39638
    # so grub2-mkconfig has to be run to generate a config with menuentry commands.
5d39638
    if [ "${arch}" = "ppc64le" ] && [ -d /sys/firmware/opal ]; then
5d39638
        RUN_MKCONFIG="true"
5d39638
        petitboot_path="/sys/firmware/devicetree/base/ibm,firmware-versions/petitboot"
5d39638
5d39638
        if test -e ${petitboot_path}; then
5d39638
            read -r -d '' petitboot_version < ${petitboot_path}
5d39638
            petitboot_version="$(echo ${petitboot_version//v})"
5d39638
5d39638
            if test -n ${petitboot_version}; then
5d39638
                major_version="$(echo ${petitboot_version} | cut -d . -f1)"
5d39638
                minor_version="$(echo ${petitboot_version} | cut -d . -f2)"
5d39638
5d39638
                re='^[0-9]+$'
5d39638
                if [[ $major_version =~ $re ]] && [[ $minor_version =~ $re ]] &&
5d39638
                       ([[ ${major_version} -gt 1 ]] ||
5d39638
                            [[ ${major_version} -eq 1 &&
5d39638
                                 ${minor_version} -ge 8  ]]); then
5d39638
                    RUN_MKCONFIG="false"
5d39638
                fi
5d39638
            fi
5d39638
        fi
5d39638
    fi
5d39638
5d39638
    if [[ $RUN_MKCONFIG = "true" ]]; then
5d39638
        grub2-mkconfig --no-grubenv-update -o "${grub_config}" >& /dev/null
b8c8cdc
    fi
b8c8cdc
}
b8c8cdc
19db5c4
print_usage()
19db5c4
{
19db5c4
    cat <
19db5c4
Usage: grubby [OPTION...]
19db5c4
      --add-kernel=kernel-path            add an entry for the specified kernel
19db5c4
      --args=args                         default arguments for the new kernel or new arguments for kernel being updated)
19db5c4
      --bad-image-okay                    don't sanity check images in boot entries (for testing only)
19db5c4
  -c, --config-file=path                  path to grub config file to update ("-" for stdin)
19db5c4
      --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
19db5c4
                                          first linux entry whose kernel does exist is used as the template
19db5c4
      --default-kernel                    display the path of the default kernel
19db5c4
      --default-index                     display the index of the default kernel
19db5c4
      --default-title                     display the title of the default kernel
19db5c4
      --env=path                          path for environment data
19db5c4
      --grub2                             configure grub2 bootloader
19db5c4
      --info=kernel-path                  display boot information for specified kernel
19db5c4
      --initrd=initrd-path                initrd image for the new kernel
19db5c4
  -i, --extra-initrd=initrd-path          auxiliary initrd image for things other than the new kernel
19db5c4
      --make-default                      make the newly added entry the default boot entry
19db5c4
      --remove-args=STRING                remove kernel arguments
19db5c4
      --remove-kernel=kernel-path         remove all entries for the specified kernel
19db5c4
      --set-default=kernel-path           make the first entry referencing the specified kernel the default
19db5c4
      --set-default-index=entry-index     make the given entry index the default entry
19db5c4
      --title=entry-title                 title to use for the new kernel entry
19db5c4
      --update-kernel=kernel-path         updated information for the specified kernel
19db5c4
      --zipl                              configure zipl bootloader
19db5c4
  -b, --bls-directory                     path to directory containing the BootLoaderSpec fragment files
f24781c
      --no-etc-grub-update                don't update the GRUB_CMDLINE_LINUX variable in /etc/default/grub
19db5c4
19db5c4
Help options:
19db5c4
  -?, --help                              Show this help message
19db5c4
19db5c4
EOF
19db5c4
}
19db5c4
5dafc37
OPTS="$(getopt -o c:i:b:? --long help,add-kernel:,args:,bad-image-okay,\
19db5c4
config-file:,copy-default,default-kernel,default-index,default-title,env:,\
5dafc37
grub2,info:,initrd:,extra-initrd:,make-default,remove-args:,\
19db5c4
remove-kernel:,set-default:,set-default-index:,title:,update-kernel:,zipl,\
f24781c
bls-directory:,no-etc-grub-update,add-multiboot:,mbargs:,mounts:,boot-filesystem:,\
19db5c4
bootloader-probe,debug,devtree,devtreedir:,elilo,efi,extlinux,grub,lilo,\
19db5c4
output-file:,remove-mbargs:,remove-multiboot:,silo,yaboot -n ${SCRIPTNAME} -- "$@")"
19db5c4
19db5c4
[[ $? = 0 ]] || exit 1
19db5c4
19db5c4
eval set -- "$OPTS"
19db5c4
19db5c4
while [ ${#} -gt 0 ]; do
19db5c4
    case "$1" in
19db5c4
        --help|-h)
19db5c4
            print_usage
19db5c4
            exit 0
19db5c4
            ;;
19db5c4
        --add-kernel)
19db5c4
            kernel="${2}"
19db5c4
            shift
19db5c4
            ;;
19db5c4
        --args)
19db5c4
            args="${2}"
19db5c4
            shift
19db5c4
            ;;
19db5c4
        --bad-image-okay)
19db5c4
            bad_image=true
19db5c4
            ;;
19db5c4
        --config-file|-c)
5dafc37
            grub_config="${2}"
19db5c4
            zipl_config="${2}"
19db5c4
            shift
19db5c4
            ;;
19db5c4
        --copy-default)
19db5c4
            copy_default=true
19db5c4
            ;;
19db5c4
        --default-kernel)
19db5c4
            display_default="kernel"
19db5c4
            ;;
19db5c4
        --default-index)
19db5c4
            display_default="index"
19db5c4
            ;;
19db5c4
        --default-title)
19db5c4
            display_default="title"
19db5c4
            ;;
19db5c4
        --env)
19db5c4
            env="${2}"
19db5c4
            shift
19db5c4
            ;;
19db5c4
        --grub2)
19db5c4
            bootloader="grub2"
19db5c4
            ;;
19db5c4
        --info)
19db5c4
            display_info="${2}"
19db5c4
            shift
19db5c4
            ;;
19db5c4
        --initrd)
19db5c4
            initrd="${2}"
19db5c4
            shift
19db5c4
            ;;
19db5c4
        --extra-initrd|-i)
a5c9c3a
            extra_initrd="${2}"
19db5c4
            shift
19db5c4
            ;;
19db5c4
        --make-default)
19db5c4
            make_default=true
19db5c4
            ;;
19db5c4
        --remove-args)
19db5c4
            remove_args="${2}"
19db5c4
            shift
19db5c4
            ;;
19db5c4
        --remove-kernel)
19db5c4
            remove_kernel="${2}"
19db5c4
            shift
19db5c4
            ;;
19db5c4
        --set-default)
19db5c4
            set_default="${2}"
19db5c4
            shift
19db5c4
            ;;
19db5c4
        --set-default-index)
19db5c4
            set_default="${2}"
19db5c4
            shift
19db5c4
            ;;
19db5c4
        --title)
19db5c4
            title="${2}"
19db5c4
            shift
19db5c4
            ;;
19db5c4
        --update-kernel)
19db5c4
            update_kernel="${2}"
19db5c4
            shift
19db5c4
            ;;
19db5c4
        --zipl)
19db5c4
            bootloader="zipl"
19db5c4
            ;;
19db5c4
        --bls-directory|-b)
19db5c4
            blsdir="${2}"
19db5c4
	    shift
19db5c4
	    ;;
f24781c
        --no-etc-grub-update)
f24781c
            no_etc_update=true
f24781c
            shift
f24781c
            ;;
f24781c
        --add-multiboot|--mbargs|--mounts|--boot-filesystem|\
19db5c4
        --bootloader-probe|--debug|--devtree|--devtreedir|--elilo|--efi|\
19db5c4
        --extlinux|--grub|--lilo|--output-file|--remove-mbargs|--silo|\
19db5c4
        --remove-multiboot|--slilo|--yaboot)
19db5c4
            echo
19db5c4
            echo "${SCRIPTNAME}: the option \"${1}\" was deprecated" >&2
19db5c4
            echo "Try '${SCRIPTNAME} --help' to list supported options" >&2
19db5c4
            echo
19db5c4
	    exit 1
19db5c4
	    ;;
19db5c4
        --)
19db5c4
            shift
19db5c4
            break
19db5c4
            ;;
19db5c4
        *)
19db5c4
            echo
19db5c4
            echo "${SCRIPTNAME}: invalid option \"${1}\"" >&2
19db5c4
            echo "Try '${SCRIPTNAME} --help' for more information" >&2
19db5c4
            echo
19db5c4
            exit 1
19db5c4
            ;;
19db5c4
    esac
19db5c4
    shift
19db5c4
done
19db5c4
dc24f2c
if [[ -z $update_kernel && -z $kernel ]] && [[ -n $args || -n $remove_args ]]; then
40d7338
    print_error "no action specified"
40d7338
fi
40d7338
19db5c4
if [[ -z $blsdir ]]; then
19db5c4
    blsdir="/boot/loader/entries"
19db5c4
fi
19db5c4
19db5c4
if [[ -z $env ]]; then
19db5c4
    env="/boot/grub2/grubenv"
19db5c4
fi
19db5c4
19db5c4
if [[ -z $zipl_config ]]; then
19db5c4
    zipl_config="/etc/zipl.conf"
19db5c4
fi
19db5c4
5dafc37
if [[ -z $grub_config ]]; then
5dafc37
    grub_config="/boot/grub2/grub.cfg"
5dafc37
fi
5dafc37
8c7d866
if [[ -z $grub_etc_default ]]; then
8c7d866
    grub_etc_default="/etc/default/grub"
8c7d866
fi
8c7d866
19db5c4
get_bls_values
19db5c4
19db5c4
default_index="$(get_default_index)"
19db5c4
19db5c4
if [[ -n $display_default ]]; then
19db5c4
    display_default_value
19db5c4
fi
19db5c4
19db5c4
if [[ -n $display_info ]]; then
19db5c4
    display_info_values "${display_info}"
19db5c4
fi
19db5c4
Yuval Turgeman 9baffd0
remove_var_prefix "$(get_prefix)"
19db5c4
19db5c4
if [[ -n $kernel ]]; then
19db5c4
    if [[ $copy_default = "true" ]]; then
19db5c4
	opts="${bls_options[$default_index]}"
19db5c4
	if [[ -n $args ]]; then
19db5c4
	    opts="${opts} ${args}"
19db5c4
	fi
19db5c4
    else
19db5c4
	opts="${args}"
19db5c4
    fi
19db5c4
19db5c4
    add_bls_fragment "${kernel}" "${title}" "${opts}" "${initrd}" \
19db5c4
                     "${extra_initrd}"
19db5c4
fi
19db5c4
19db5c4
if [[ -n $remove_kernel ]]; then
19db5c4
    remove_bls_fragment "${remove_kernel}"
19db5c4
fi
19db5c4
19db5c4
if [[ -n $update_kernel ]]; then
19db5c4
    update_bls_fragment "${update_kernel}" "${remove_args}" "${args}" "${initrd}"
19db5c4
fi
19db5c4
19db5c4
if [[ -n $set_default ]]; then
19db5c4
    set_default_bls "${set_default}"
19db5c4
fi
19db5c4
19db5c4
exit 0