24fce8
#!/bin/bash
24fce8
#
24fce8
# grubby wrapper to manage BootLoaderSpec files
24fce8
#
24fce8
#
24fce8
# Copyright 2018 Red Hat, Inc.  All rights reserved.
24fce8
#
24fce8
# This program is free software; you can redistribute it and/or modify
24fce8
# it under the terms of the GNU General Public License as published by
24fce8
# the Free Software Foundation; either version 2 of the License, or
24fce8
# (at your option) any later version.
24fce8
#
24fce8
# This program is distributed in the hope that it will be useful,
24fce8
# but WITHOUT ANY WARRANTY; without even the implied warranty of
24fce8
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24fce8
# GNU General Public License for more details.
24fce8
#
24fce8
# You should have received a copy of the GNU General Public License
24fce8
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
24fce8
24fce8
readonly SCRIPTNAME="${0##*/}"
24fce8
24fce8
CMDLINE_LINUX_DEBUG=" systemd.log_level=debug systemd.log_target=kmsg"
24fce8
LINUX_DEBUG_VERSION_POSTFIX="_with_debugging"
24fce8
LINUX_DEBUG_TITLE_POSTFIX=" with debugging"
24fce8
24fce8
declare -a bls_file
24fce8
declare -a bls_title
24fce8
declare -a bls_version
24fce8
declare -a bls_linux
24fce8
declare -a bls_initrd
24fce8
declare -a bls_options
24fce8
declare -a bls_id
24fce8
24fce8
[[ -f /etc/sysconfig/kernel ]] && . /etc/sysconfig/kernel
24fce8
[[ -f /etc/os-release ]] && . /etc/os-release
24fce8
read MACHINE_ID < /etc/machine-id
24fce8
arch=$(uname -m)
24fce8
24fce8
if [[ $arch = 's390' || $arch = 's390x' ]]; then
24fce8
    bootloader="zipl"
24fce8
else
24fce8
    bootloader="grub2"
24fce8
fi
24fce8
24fce8
print_error() {
24fce8
    echo "$1" >&2
24fce8
    exit 1
24fce8
}
24fce8
24fce8
print_info() {
24fce8
    echo "$1" >&2
24fce8
}
24fce8
24fce8
if [[ ${#} = 0 ]]; then
24fce8
    print_error "no action specified"
24fce8
fi
24fce8
24fce8
get_bls_value() {
24fce8
    local bls="$1" && shift
24fce8
    local key="$1" && shift
24fce8
24fce8
    echo "$(grep "^${key}[ \t]" "${bls}" | sed -e "s!^${key}[ \t]*!!")"
24fce8
}
24fce8
24fce8
set_bls_value() {
24fce8
    local bls="$1" && shift
24fce8
    local key="$1" && shift
24fce8
    local value="$1" && shift
24fce8
24fce8
    value=$(echo $value | sed -e 's/\//\\\//g')
24fce8
    sed -i -e "s/^${key}.*/${key} ${value}/" "${bls}"
24fce8
}
24fce8
24fce8
append_bls_value() {
24fce8
    local bls="$1" && shift
24fce8
    local key="$1" && shift
24fce8
    local value="$1" && shift
24fce8
24fce8
    old_value="$(get_bls_value "${bls}" ${key})"
24fce8
    set_bls_value "${bls}" "${key}" "${old_value}${value}"
24fce8
}
24fce8
24fce8
get_bls_values() {
24fce8
    count=0
24fce8
    local -a files
24fce8
    local IFS=$'\n'
24fce8
    files=($(for bls in ${blsdir}/*.conf ; do
24fce8
        if ! [[ -e "${bls}" ]] ; then
24fce8
            continue
24fce8
        fi
24fce8
        bls="${bls%.conf}"
24fce8
        bls="${bls##*/}"
24fce8
        echo "${bls}"
69c48d
    done | /usr/libexec/grubby/rpm-sort -c rpmnvrcmp 2>/dev/null | tac)) || :
24fce8
24fce8
    for bls in "${files[@]}" ; do
24fce8
        blspath="${blsdir}/${bls}.conf"
24fce8
        bls_file[$count]="${blspath}"
24fce8
        bls_title[$count]="$(get_bls_value ${blspath} title)"
24fce8
        bls_version[$count]="$(get_bls_value ${blspath} version)"
24fce8
        bls_linux[$count]="$(get_bls_value ${blspath} linux)"
24fce8
        bls_initrd[$count]="$(get_bls_value ${blspath} initrd)"
24fce8
        bls_options[$count]="$(get_bls_value ${blspath} options)"
24fce8
        bls_id[$count]="${bls}"
24fce8
24fce8
        count=$((count+1))
24fce8
    done
24fce8
}
24fce8
24fce8
get_default_index() {
24fce8
    local default=""
24fce8
    local index="-1"
24fce8
    local title=""
24fce8
    local version=""
24fce8
    if [[ $bootloader = "grub2" ]]; then
24fce8
	default="$(grep '^saved_entry=' ${env} | sed -e 's/^saved_entry=//')"
24fce8
    else
24fce8
	default="$(grep '^default=' ${zipl_config} | sed -e 's/^default=//')"
24fce8
    fi
24fce8
24fce8
    if [[ -z $default ]]; then
24fce8
	index=0
24fce8
    elif [[ $default =~ ^[0-9]+$ ]]; then
24fce8
	index="$default"
24fce8
    fi
24fce8
24fce8
    for i in ${!bls_file[@]}; do
24fce8
        if [[ $i -eq $index ]]; then
24fce8
            echo $i
24fce8
            return
24fce8
        fi
24fce8
24fce8
        if [[ $default = ${bls_id[$i]} || $default = ${bls_title[$i]} ]]; then
24fce8
            echo $i
24fce8
            return
24fce8
        fi
24fce8
    done
24fce8
}
24fce8
24fce8
display_default_value() {
24fce8
    local prefix=$(get_prefix)
24fce8
24fce8
    case "$display_default" in
24fce8
        kernel)
24fce8
            echo "${prefix}${bls_linux[$default_index]}"
24fce8
            exit 0
24fce8
            ;;
24fce8
        index)
24fce8
            echo "$default_index"
24fce8
            exit 0
24fce8
            ;;
24fce8
        title)
24fce8
            echo "${bls_title[$default_index]}"
24fce8
            exit 0
24fce8
            ;;
24fce8
    esac
24fce8
}
24fce8
24fce8
param_to_indexes() {
24fce8
    local param="$1"
24fce8
    local indexes=""
24fce8
24fce8
    if [[ $param = "ALL" ]]; then
24fce8
        for i in ${!bls_file[@]}; do
24fce8
            indexes="$indexes $i"
24fce8
        done
24fce8
        echo -n $indexes
24fce8
        return
24fce8
    fi
24fce8
24fce8
    if [[ $param = "DEFAULT" ]]; then
24fce8
        echo -n $default_index
24fce8
        return
24fce8
    fi
24fce8
24fce8
    for i in ${!bls_file[@]}; do
24fce8
        if [[ $param = "${bls_linux[$i]}" || "/${param##*/}" = "${bls_linux[$i]}" ]]; then
24fce8
            indexes="$indexes $i"
24fce8
        fi
24fce8
24fce8
        if [[ $param = "TITLE=${bls_title[$i]}" ]]; then
24fce8
            indexes="$indexes $i"
24fce8
        fi
24fce8
24fce8
	if [[ $param = $i ]]; then
24fce8
	    indexes="$indexes $i"
24fce8
	fi
24fce8
    done
24fce8
24fce8
    if [[ -n $indexes ]]; then
24fce8
        echo -n $indexes
24fce8
        return
24fce8
    fi
24fce8
24fce8
    echo -n "-1"
24fce8
}
24fce8
24fce8
get_prefix() {
24fce8
    if [[ $bootloader = grub2 ]] && mountpoint -q /boot; then
24fce8
	echo "/boot"
24fce8
    else
24fce8
	echo ""
24fce8
    fi
24fce8
}
24fce8
24fce8
expand_var() {
24fce8
    local var=$1
24fce8
24fce8
    if [[ $bootloader == "grub2" ]]; then
24fce8
        local value="$(grub2-editenv "${env}" list | grep ${var##$} | sed -e "s/${var##$}=//")"
24fce8
        value="$(echo ${value} | sed -e 's/\//\\\//g')"
24fce8
        if [[ -n $value ]]; then
24fce8
            var="$value"
24fce8
        fi
24fce8
    fi
24fce8
24fce8
    echo $var
24fce8
}
24fce8
24fce8
has_kernelopts()
24fce8
{
24fce8
    local args=${bls_options[$1]}
24fce8
    local opts=(${args})
24fce8
24fce8
    for opt in ${opts[*]}; do
69c48d
        [[ $opt = "\$kernelopts" ]] && echo "true"
24fce8
    done
24fce8
69c48d
    echo "false"
24fce8
}
24fce8
24fce8
get_bls_args() {
24fce8
    local args=${bls_options[$1]}
24fce8
    local opts=(${args})
24fce8
24fce8
    for opt in ${opts[*]}; do
220f94
        if [[ $opt = "\$kernelopts" ]]; then
24fce8
            value="$(expand_var $opt)"
24fce8
            args="$(echo ${args} | sed -e "s/${opt}/${value}/")"
24fce8
        fi
24fce8
    done
24fce8
24fce8
    echo ${args}
24fce8
}
24fce8
24fce8
display_info_values() {
24fce8
    local indexes=($(param_to_indexes "$1"))
24fce8
    local prefix=$(get_prefix)
24fce8
24fce8
    if [[ $indexes = "-1" ]]; then
24fce8
        print_error "The param $1 is incorrect"
24fce8
    fi
24fce8
24fce8
    for i in ${indexes[*]}; do
24fce8
        local root=""
24fce8
        local value=""
24fce8
        local args="$(get_bls_args "$i")"
24fce8
24fce8
        local opts=(${args})
24fce8
24fce8
        for opt in ${opts[*]}; do
24fce8
            if echo $opt | grep -q "^root="; then
24fce8
                root="$(echo $opt | sed -e 's/root=//')"
24fce8
                value="$(echo ${opt} | sed -e 's/\//\\\//g')"
24fce8
                args="$(echo ${args} | sed -e "s/${value}[ \t]*//")"
24fce8
                break
24fce8
            fi
24fce8
        done
24fce8
24fce8
        echo "index=$i"
24fce8
        echo "kernel=\"${prefix}${bls_linux[$i]}\""
24fce8
        echo "args=\"${args}\""
24fce8
24fce8
        if [[ -n $root ]]; then
24fce8
            echo "root=\"${root}\""
24fce8
        fi
24fce8
24fce8
        echo "initrd=\"${prefix}${bls_initrd[$i]}\""
24fce8
        echo "title=\"${bls_title[$i]}\""
24fce8
        echo "id=\"${bls_id[$i]}\""
24fce8
    done
24fce8
    exit 0
24fce8
}
24fce8
24fce8
mkbls() {
24fce8
    local kernel=$1 && shift
24fce8
    local kernelver=$1 && shift
24fce8
    local datetime=$1 && shift
24fce8
24fce8
    local debugname=""
24fce8
    local flavor=""
24fce8
    local prefix=""
24fce8
24fce8
    if [[ $(get_prefix) = "" ]]; then
24fce8
	prefix="/boot"
24fce8
    fi
24fce8
24fce8
    if [[ $kernelver == *\+* ]] ; then
24fce8
        local flavor=-"${kernelver##*+}"
24fce8
        if [[ $flavor == "-debug" ]]; then
24fce8
            local debugname="with debugging"
24fce8
	    local debugid="-debug"
24fce8
        fi
24fce8
    fi
24fce8
24fce8
    cat <
24fce8
title ${NAME} (${kernelver}) ${VERSION}${debugname}
24fce8
version ${kernelver}${debugid}
24fce8
linux ${kernel}
24fce8
initrd ${prefix}/initramfs-${kernelver}.img
24fce8
options \$kernelopts
24fce8
id ${ID}-${datetime}-${kernelver}${debugid}
24fce8
grub_users \$grub_users
24fce8
grub_arg --unrestricted
24fce8
grub_class kernel${flavor}
24fce8
EOF
24fce8
}
24fce8
24fce8
unset_default_bls()
24fce8
{
24fce8
    if [[ $bootloader = grub2 ]]; then
24fce8
        grub2-editenv "${env}" unset saved_entry
24fce8
    else
24fce8
        sed -i -e "/^default=.*/d" "${zipl_config}"
24fce8
    fi
24fce8
}
24fce8
24fce8
remove_bls_fragment() {
24fce8
    local indexes=($(param_to_indexes "$1"))
24fce8
24fce8
    if [[ $indexes = "-1" ]]; then
24fce8
	print_error "The param $(get_prefix)$1 is incorrect"
24fce8
    fi
24fce8
24fce8
    for i in "${indexes[@]}"; do
24fce8
        if [[ $default_index = $i ]]; then
24fce8
            unset_default_bls
24fce8
        fi
24fce8
        rm -f "${bls_file[$i]}"
24fce8
    done
24fce8
24fce8
    get_bls_values
24fce8
24fce8
    update_grubcfg
24fce8
}
24fce8
24fce8
get_custom_bls_filename() {
24fce8
    local kernelver=$1
24fce8
    local bls_target="${blsdir}/${MACHINE_ID}-${kernelver}.conf"
24fce8
    count=0
24fce8
    local -a files
24fce8
    local IFS=$'\n'
24fce8
24fce8
    prefix="${bls_target%%.conf}"
24fce8
    prefix="${bls_target%%${arch}}"
24fce8
    prefix="${prefix%.*}"
24fce8
24fce8
    last=($(for bls in ${prefix}.*~custom*.conf ; do
24fce8
        if ! [[ -e "${bls}" ]] ; then
24fce8
            continue
24fce8
        fi
24fce8
        bls="${bls##${prefix}.}"
24fce8
        bls="${bls%%~custom*}"
24fce8
        echo "${bls}"
24fce8
    done | tail -n1)) || :
24fce8
24fce8
    if [[ -z $last ]]; then
24fce8
        last="0"
24fce8
    else
24fce8
        last=$((last+1))
24fce8
    fi
24fce8
24fce8
    echo "${bls_target}" | sed -e "s!${prefix}!${prefix}.${last}~custom!"
24fce8
}
24fce8
24fce8
add_bls_fragment() {
24fce8
    local kernel="$1" && shift
24fce8
    local title="$1" && shift
24fce8
    local options="$1" && shift
24fce8
    local initrd="$1" && shift
24fce8
    local extra_initrd="$1" && shift
24fce8
24fce8
    if [[ $kernel = *"vmlinuz-"* ]]; then
24fce8
	kernelver="${kernel##*/vmlinuz-}"
24fce8
	prefix="vmlinuz-"
24fce8
    else
24fce8
	kernelver="${kernel##*/}"
24fce8
    fi
24fce8
24fce8
    if [[ ! -f "/boot/${prefix}${kernelver}" ]] &&
24fce8
       [[ $bad_image != "true" ]]; then
24fce8
        print_error "The ${kernelver} kernel isn't installed in the machine"
24fce8
    fi
24fce8
24fce8
    if [[ -z $title ]]; then
24fce8
	print_error "The kernel title must be specified"
24fce8
    fi
24fce8
24fce8
    if [[ ! -d $blsdir ]]; then
24fce8
        install -m 700 -d "${blsdir}"
24fce8
    fi
24fce8
24fce8
    bls_target="${blsdir}/${MACHINE_ID}-${kernelver}.conf"
24fce8
24fce8
    if [[ -e ${bls_target} ]]; then
24fce8
        bls_target="$(get_custom_bls_filename "${kernelver}")"
24fce8
        print_info "An entry for kernel ${kernelver} already exists, adding ${bls_target}"
24fce8
    fi
24fce8
24fce8
    kernel_dir="/lib/modules/${kernelver}"
24fce8
    if [[ -d $kernel_dir ]]; then
24fce8
        datetime="$(date -u +%Y%m%d%H%M%S -d "$(stat -c '%y' "${kernel_dir}")")"
24fce8
    else
24fce8
        datetime=0
24fce8
    fi
24fce8
    mkbls "${kernel}" "${kernelver}" "${datetime}" > "${bls_target}"
24fce8
24fce8
    if [[ -n $title ]]; then
24fce8
        set_bls_value "${bls_target}" "title" "${title}"
24fce8
    fi
24fce8
24fce8
    if [[ -n $options ]]; then
24fce8
        set_bls_value "${bls_target}" "options" "${options}"
24fce8
    fi
24fce8
24fce8
    if [[ -n $initrd ]]; then
24fce8
        set_bls_value "${bls_target}" "initrd" "${initrd}"
24fce8
    fi
24fce8
24fce8
    if [[ -n $extra_initrd ]]; then
24fce8
        append_bls_value "${bls_target}" "initrd" "${extra_initrd}"
24fce8
    fi
24fce8
24fce8
    if [[ $MAKEDEBUG = "yes" ]]; then
24fce8
        bls_debug="$(echo ${bls_target} | sed -e "s/${kernelver}/${kernelver}~debug/")"
24fce8
        cp -aT  "${bls_target}" "${bls_debug}"
24fce8
        append_bls_value "${bls_debug}" "title" "${LINUX_DEBUG_TITLE_POSTFIX}"
24fce8
        append_bls_value "${bls_debug}" "version" "${LINUX_DEBUG_VERSION_POSTFIX}"
24fce8
        append_bls_value "${bls_debug}" "options" "${CMDLINE_LINUX_DEBUG}"
24fce8
        blsid="$(get_bls_value ${bls_debug} "id" | sed -e "s/${kernelver}/${kernelver}~debug/")"
24fce8
        set_bls_value "${bls_debug}" "id" "${blsid}"
24fce8
    fi
24fce8
24fce8
    get_bls_values
24fce8
24fce8
    if [[ $make_default = "true" ]]; then
24fce8
        set_default_bls "TITLE=${title}"
24fce8
    fi
24fce8
24fce8
    update_grubcfg
24fce8
24fce8
    exit 0
24fce8
}
24fce8
24fce8
update_args() {
24fce8
    local args=$1 && shift
24fce8
    local remove_args=($1) && shift
24fce8
    local add_args=($1) && shift
24fce8
24fce8
    for arg in ${remove_args[*]}; do
1c6adf
        arg="$(echo $arg | sed -e 's/\//\\\//g')"
24fce8
        if [[ $arg = *"="* ]]; then
24fce8
            args="$(echo $args | sed -E "s/(^|[[:space:]])$arg([[:space:]]|$)/ /")"
24fce8
        else
24fce8
            args="$(echo $args | sed -E "s/(^|[[:space:]])$arg(([[:space:]]|$)|([=][^ ]*([$]*)))/ /g")"
24fce8
        fi
24fce8
    done
24fce8
24fce8
    for arg in ${add_args[*]}; do
1c6adf
        arg="${arg%%=*}"
1c6adf
        arg="$(echo $arg | sed -e 's/\//\\\//g')"
24fce8
        args="$(echo $args | sed -E "s/(^|[[:space:]])$arg(([[:space:]]|$)|([=][^ ]*([$]*)))/ /g")"
24fce8
    done
24fce8
24fce8
    for arg in ${add_args[*]}; do
24fce8
        args="$args $arg"
24fce8
    done
24fce8
24fce8
    echo ${args}
24fce8
}
24fce8
24fce8
update_bls_fragment() {
24fce8
    local param="$1"
24fce8
    local indexes=($(param_to_indexes "$1")) && shift
24fce8
    local remove_args=$1 && shift
24fce8
    local add_args=$1 && shift
24fce8
    local initrd=$1 && shift
24fce8
    local opts
24fce8
24fce8
    if [[ $indexes = "-1" ]]; then
24fce8
        print_error "The param $(get_prefix)${param} is incorrect"
24fce8
    fi
24fce8
24fce8
    if [[ $param = "ALL" && $bootloader = grub2 ]] && [[ -n $remove_args || -n $add_args ]]; then
1c6adf
        local old_args=""
1c6adf
1c6adf
        if [[ -z $no_etc_update ]] && [[ -e ${grub_etc_default} ]]; then
1c6adf
            old_args="$(source ${grub_etc_default}; echo ${GRUB_CMDLINE_LINUX})"
1c6adf
            if [[ -n $old_args ]]; then
1c6adf
                opts="$(update_args "${old_args}" "${remove_args}" "${add_args}")"
1c6adf
                opts="$(echo "$opts" | sed -e 's/\//\\\//g')"
1c6adf
                sed -i -e "s/^GRUB_CMDLINE_LINUX.*/GRUB_CMDLINE_LINUX=\\\"${opts}\\\"/" "${grub_etc_default}"
1c6adf
            fi
1c6adf
        fi
1c6adf
1c6adf
        old_args="$(grub2-editenv "${env}" list | grep kernelopts | sed -e "s/kernelopts=//")"
1c6adf
        if [[ -n $old_args ]]; then
1c6adf
            opts="$(update_args "${old_args}" "${remove_args}" "${add_args}")"
1c6adf
            grub2-editenv "${env}" set kernelopts="${opts}"
1c6adf
        fi
24fce8
    elif [[ $bootloader = grub2 ]]; then
24fce8
        opts="$(grub2-editenv "${env}" list | grep kernelopts | sed -e "s/kernelopts=//")"
32c05e
    elif [[ $bootloader = zipl ]]; then
32c05e
	if [[ ! -f /etc/kernel/cmdline ]]; then
32c05e
	    # anaconda could create this, but we'd just end up in the same
32c05e
	    # place.
32c05e
	    sed 's/BOOT_IMAGE=[^ ]*//' /proc/cmdline > /etc/kernel/cmdline
32c05e
	fi
32c05e
	if [[ $param = "ALL" ]] && [[ -n $remove_args || -n $add_args ]]; then
32c05e
	    read old_args < /etc/kernel/cmdline
32c05e
	    opts="$(update_args "${old_args}" "${remove_args}" "${add_args}")"
32c05e
	    echo "$opts" > /etc/kernel/cmdline
32c05e
	fi
24fce8
    fi
24fce8
7aade2
    if [[ $bootloader = grub2 ]] && [[ ! -f /etc/kernel/cmdline ]]; then
7aade2
	opts=$(grub2-editenv "${env}" list | grep kernelopts | sed 's/kernelopts=//')
7aade2
	if [[ -n $opts ]]; then
7aade2
	    echo "opts" > /etc/kernel/cmdline
7aade2
	fi
7aade2
    fi
7aade2
24fce8
    for i in ${indexes[*]}; do
24fce8
	if [[ -n $remove_args || -n $add_args ]]; then
24fce8
            local old_args="$(get_bls_args "$i")"
24fce8
            local new_args="$(update_args "${old_args}" "${remove_args}" "${add_args}")"
24fce8
69c48d
            if [[ $param != "ALL" || "$(has_kernelopts "$i")" = "false" ]]; then
24fce8
                set_bls_value "${bls_file[$i]}" "options" "${new_args}"
24fce8
            fi
24fce8
69c48d
            if [[ $bootloader = grub2 && "$(has_kernelopts "$i")" = "false" && $opts = $new_args ]]; then
24fce8
                set_bls_value "${bls_file[$i]}" "options" "\$kernelopts"
24fce8
            fi
24fce8
	fi
24fce8
24fce8
	if [[ -n $initrd ]]; then
24fce8
	    set_bls_value "${bls_file[$i]}" "initrd" "${initrd}"
24fce8
	fi
24fce8
    done
24fce8
24fce8
    update_grubcfg
24fce8
}
24fce8
24fce8
set_default_bls() {
24fce8
    local index=($(param_to_indexes "$1"))
24fce8
24fce8
    if [[ $index = "-1" ]]; then
24fce8
        print_error "The param $1 is incorrect"
24fce8
    fi
24fce8
24fce8
    if [[ $bootloader = grub2 ]]; then
24fce8
        grub2-editenv "${env}" set saved_entry="${bls_id[$index]}"
24fce8
    else
24fce8
        local default="${bls_title[$index]}"
24fce8
        local current="$(grep '^default=' ${zipl_config} | sed -e 's/^default=//')"
24fce8
        if [[ -n $current ]]; then
24fce8
            sed -i -e "s,^default=.*,default=${default}," "${zipl_config}"
24fce8
        else
24fce8
            echo "default=${default}" >> "${zipl_config}"
24fce8
        fi
24fce8
    fi
24fce8
24fce8
    print_info "The default is ${bls_file[$index]} with index $index and kernel $(get_prefix)${bls_linux[$index]}"
24fce8
}
24fce8
24fce8
remove_var_prefix() {
1c6adf
    local prefix="$1"
1c6adf
1c6adf
    [ -z "${prefix}" ] && return
1c6adf
24fce8
    if [[ -n $remove_kernel && $remove_kernel =~ ^/ ]]; then
1c6adf
       remove_kernel="/${remove_kernel##${prefix}/}"
24fce8
    fi
24fce8
24fce8
    if [[ -n $initrd ]]; then
1c6adf
	initrd="/${initrd##${prefix}/}"
24fce8
    fi
24fce8
24fce8
    if [[ -n $extra_initrd ]]; then
1c6adf
	extra_initrd=" /${extra_initrd##${prefix}/}"
24fce8
    fi
24fce8
24fce8
    if [[ -n $kernel ]]; then
1c6adf
	kernel="/${kernel##${prefix}/}"
24fce8
    fi
24fce8
24fce8
    if [[ -n $update_kernel && $update_kernel =~ ^/ ]]; then
1c6adf
	update_kernel="/${update_kernel##${prefix}/}"
24fce8
    fi
24fce8
}
24fce8
24fce8
update_grubcfg()
24fce8
{
24fce8
    if [[ $arch = 'ppc64' || $arch = 'ppc64le' ]]; then
69c48d
	grub2-mkconfig --no-grubenv-update -o "${grub_config}" >& /dev/null
24fce8
    fi
24fce8
}
24fce8
24fce8
print_usage()
24fce8
{
24fce8
    cat <
24fce8
Usage: grubby [OPTION...]
24fce8
      --add-kernel=kernel-path            add an entry for the specified kernel
24fce8
      --args=args                         default arguments for the new kernel or new arguments for kernel being updated)
24fce8
      --bad-image-okay                    don't sanity check images in boot entries (for testing only)
24fce8
  -c, --config-file=path                  path to grub config file to update ("-" for stdin)
24fce8
      --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
24fce8
                                          first linux entry whose kernel does exist is used as the template
24fce8
      --default-kernel                    display the path of the default kernel
24fce8
      --default-index                     display the index of the default kernel
24fce8
      --default-title                     display the title of the default kernel
24fce8
      --env=path                          path for environment data
24fce8
      --grub2                             configure grub2 bootloader
24fce8
      --info=kernel-path                  display boot information for specified kernel
24fce8
      --initrd=initrd-path                initrd image for the new kernel
24fce8
  -i, --extra-initrd=initrd-path          auxiliary initrd image for things other than the new kernel
24fce8
      --make-default                      make the newly added entry the default boot entry
24fce8
      --remove-args=STRING                remove kernel arguments
24fce8
      --remove-kernel=kernel-path         remove all entries for the specified kernel
24fce8
      --set-default=kernel-path           make the first entry referencing the specified kernel the default
24fce8
      --set-default-index=entry-index     make the given entry index the default entry
24fce8
      --title=entry-title                 title to use for the new kernel entry
24fce8
      --update-kernel=kernel-path         updated information for the specified kernel
24fce8
      --zipl                              configure zipl bootloader
24fce8
  -b, --bls-directory                     path to directory containing the BootLoaderSpec fragment files
1c6adf
      --no-etc-grub-update                don't update the GRUB_CMDLINE_LINUX variable in /etc/default/grub
24fce8
24fce8
Help options:
24fce8
  -?, --help                              Show this help message
24fce8
24fce8
EOF
24fce8
}
24fce8
69c48d
OPTS="$(getopt -o c:i:b:? --long help,add-kernel:,args:,bad-image-okay,\
24fce8
config-file:,copy-default,default-kernel,default-index,default-title,env:,\
69c48d
grub2,info:,initrd:,extra-initrd:,make-default,remove-args:,\
24fce8
remove-kernel:,set-default:,set-default-index:,title:,update-kernel:,zipl,\
1c6adf
bls-directory:,no-etc-grub-update,add-multiboot:,mbargs:,mounts:,boot-filesystem:,\
24fce8
bootloader-probe,debug,devtree,devtreedir:,elilo,efi,extlinux,grub,lilo,\
24fce8
output-file:,remove-mbargs:,remove-multiboot:,silo,yaboot -n ${SCRIPTNAME} -- "$@")"
24fce8
24fce8
[[ $? = 0 ]] || exit 1
24fce8
24fce8
eval set -- "$OPTS"
24fce8
24fce8
while [ ${#} -gt 0 ]; do
24fce8
    case "$1" in
24fce8
        --help|-h)
24fce8
            print_usage
24fce8
            exit 0
24fce8
            ;;
24fce8
        --add-kernel)
24fce8
            kernel="${2}"
24fce8
            shift
24fce8
            ;;
24fce8
        --args)
24fce8
            args="${2}"
24fce8
            shift
24fce8
            ;;
24fce8
        --bad-image-okay)
24fce8
            bad_image=true
24fce8
            ;;
24fce8
        --config-file|-c)
69c48d
            grub_config="${2}"
24fce8
            zipl_config="${2}"
24fce8
            shift
24fce8
            ;;
24fce8
        --copy-default)
24fce8
            copy_default=true
24fce8
            ;;
24fce8
        --default-kernel)
24fce8
            display_default="kernel"
24fce8
            ;;
24fce8
        --default-index)
24fce8
            display_default="index"
24fce8
            ;;
24fce8
        --default-title)
24fce8
            display_default="title"
24fce8
            ;;
24fce8
        --env)
24fce8
            env="${2}"
24fce8
            shift
24fce8
            ;;
24fce8
        --grub2)
24fce8
            bootloader="grub2"
24fce8
            ;;
24fce8
        --info)
24fce8
            display_info="${2}"
24fce8
            shift
24fce8
            ;;
24fce8
        --initrd)
24fce8
            initrd="${2}"
24fce8
            shift
24fce8
            ;;
24fce8
        --extra-initrd|-i)
24fce8
            extra_initrd=" /${2}"
24fce8
            shift
24fce8
            ;;
24fce8
        --make-default)
24fce8
            make_default=true
24fce8
            ;;
24fce8
        --remove-args)
24fce8
            remove_args="${2}"
24fce8
            shift
24fce8
            ;;
24fce8
        --remove-kernel)
24fce8
            remove_kernel="${2}"
24fce8
            shift
24fce8
            ;;
24fce8
        --set-default)
24fce8
            set_default="${2}"
24fce8
            shift
24fce8
            ;;
24fce8
        --set-default-index)
24fce8
            set_default="${2}"
24fce8
            shift
24fce8
            ;;
24fce8
        --title)
24fce8
            title="${2}"
24fce8
            shift
24fce8
            ;;
24fce8
        --update-kernel)
24fce8
            update_kernel="${2}"
24fce8
            shift
24fce8
            ;;
24fce8
        --zipl)
24fce8
            bootloader="zipl"
24fce8
            ;;
24fce8
        --bls-directory|-b)
24fce8
            blsdir="${2}"
24fce8
	    shift
24fce8
	    ;;
1c6adf
        --no-etc-grub-update)
1c6adf
            no_etc_update=true
1c6adf
            shift
1c6adf
            ;;
1c6adf
        --add-multiboot|--mbargs|--mounts|--boot-filesystem|\
24fce8
        --bootloader-probe|--debug|--devtree|--devtreedir|--elilo|--efi|\
24fce8
        --extlinux|--grub|--lilo|--output-file|--remove-mbargs|--silo|\
24fce8
        --remove-multiboot|--slilo|--yaboot)
24fce8
            echo
24fce8
            echo "${SCRIPTNAME}: the option \"${1}\" was deprecated" >&2
24fce8
            echo "Try '${SCRIPTNAME} --help' to list supported options" >&2
24fce8
            echo
24fce8
	    exit 1
24fce8
	    ;;
24fce8
        --)
24fce8
            shift
24fce8
            break
24fce8
            ;;
24fce8
        *)
24fce8
            echo
24fce8
            echo "${SCRIPTNAME}: invalid option \"${1}\"" >&2
24fce8
            echo "Try '${SCRIPTNAME} --help' for more information" >&2
24fce8
            echo
24fce8
            exit 1
24fce8
            ;;
24fce8
    esac
24fce8
    shift
24fce8
done
24fce8
24fce8
if [[ -z $update_kernel && -z $kernel ]] && [[ -n $args || -n $remove_args ]]; then
24fce8
    print_error "no action specified"
24fce8
fi
24fce8
24fce8
if [[ -z $blsdir ]]; then
24fce8
    blsdir="/boot/loader/entries"
24fce8
fi
24fce8
24fce8
if [[ -z $env ]]; then
24fce8
    env="/boot/grub2/grubenv"
24fce8
fi
24fce8
24fce8
if [[ -z $zipl_config ]]; then
24fce8
    zipl_config="/etc/zipl.conf"
24fce8
fi
24fce8
69c48d
if [[ -z $grub_config ]]; then
69c48d
    grub_config="/boot/grub2/grub.cfg"
69c48d
fi
69c48d
1c6adf
if [[ -z $grub_etc_default ]]; then
1c6adf
    grub_etc_default="/etc/default/grub"
1c6adf
fi
1c6adf
24fce8
get_bls_values
24fce8
24fce8
default_index="$(get_default_index)"
24fce8
24fce8
if [[ -n $display_default ]]; then
24fce8
    display_default_value
24fce8
fi
24fce8
24fce8
if [[ -n $display_info ]]; then
24fce8
    display_info_values "${display_info}"
24fce8
fi
24fce8
1c6adf
remove_var_prefix "$(get_prefix)"
24fce8
24fce8
if [[ -n $kernel ]]; then
24fce8
    if [[ $copy_default = "true" ]]; then
24fce8
	opts="${bls_options[$default_index]}"
24fce8
	if [[ -n $args ]]; then
24fce8
	    opts="${opts} ${args}"
24fce8
	fi
24fce8
    else
24fce8
	opts="${args}"
24fce8
    fi
24fce8
24fce8
    add_bls_fragment "${kernel}" "${title}" "${opts}" "${initrd}" \
24fce8
                     "${extra_initrd}"
24fce8
fi
24fce8
24fce8
if [[ -n $remove_kernel ]]; then
24fce8
    remove_bls_fragment "${remove_kernel}"
24fce8
fi
24fce8
24fce8
if [[ -n $update_kernel ]]; then
24fce8
    update_bls_fragment "${update_kernel}" "${remove_args}" "${args}" "${initrd}"
24fce8
fi
24fce8
24fce8
if [[ -n $set_default ]]; then
24fce8
    set_default_bls "${set_default}"
24fce8
fi
24fce8
24fce8
exit 0