#!/bin/bash
#
# Copyright 2014 Red Hat, Inc.
#
# NAME
#     rht-add-kernel-args - create another GRUB2 boot menu item
#
# SYNOPSIS
#     rht-add-kernel-args "arguments"
#
# DESCRIPTION
#     This script creates another boot menu item. It takes the
#     current default GRUB menu item, copy it with additional
#     kernel parameters (specified as an argument to this script),
#     and makes the new stanza the default boot menu item.
#
# CHANGELOG
#   * Fri Jun 20 2014 George Hacker <ghacker@redhat.com>
#   - original code

PATH=/usr/bin:/usr/sbin

# Initialize and set some variables
grubby=/sbin/grubby
grub2Config=/boot/grub2/grub.cfg

new_options="${1}"
if [[ -z "${new_options}" ]]; then
  echo 'Warning: new kernel arguments not specified - nothing done.'
  exit 1
fi

# Get the path of the current default kernel.
kernel=$(${grubby} --grub2 -c ${grub2Config} --default-kernel)

# Use grubby to get the following values for the default kernel:
#
#   initial ramdisk (initrd)
#   kernel arguments (args)
#   root file system (root)
#
# "title" had to be excluded because it has unquoted spaces.
eval $(${grubby} --grub2 -c ${grub2Config} --info=${kernel} | grep -v '^title=')
# Here is how to get the existing title.
title="$(${grubby} --grub2 -c ${grub2Config} --default-title)"

# Create the new default GRUB menu item with additional arguments.
${grubby} --grub2 -c ${grub2Config} \
	  --add-kernel=${kernel} \
	  --initrd=${initrd} \
	  --copy-default \
	  --make-default \
	  --title "${title} fixed" \
	  --args="root=${root} ${args} ${new_options}"

exit 0
