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