c78952
From 764506b89942103c3a511d7933edef351fb594b8 Mon Sep 17 00:00:00 2001
9d15b4
From: Peter Jones <pjones@redhat.com>
9d15b4
Date: Thu, 23 Jun 2016 11:01:39 -0400
31cddd
Subject: [PATCH] Add grub-get-kernel-settings and use it in 10_linux
9d15b4
9d15b4
This patch adds grub-get-kernel-settings, which reads the system kernel
9d15b4
installation configuration from /etc/sysconfig/kernel, and outputs
9d15b4
${GRUB_...} variables suitable for evaluation by grub-mkconfig.  Those
9d15b4
variables are then used by 10_linux to choose whether or not to create
9d15b4
debug stanzas.
9d15b4
9d15b4
Resolves: rhbz#1226325
9d15b4
---
9d15b4
 configure.ac                                   |  2 +
ec4acb
 Makefile.util.def                              |  7 +++
ec4acb
 .gitignore                                     |  2 +
9d15b4
 util/bash-completion.d/grub-completion.bash.in | 22 ++++++++
9d15b4
 util/grub-get-kernel-settings.3                | 20 +++++++
9d15b4
 util/grub-get-kernel-settings.in               | 78 ++++++++++++++++++++++++++
9d15b4
 util/grub-mkconfig.in                          |  3 +
9d15b4
 util/grub.d/10_linux.in                        | 23 ++++++--
bbc6a8
 8 files changed, 152 insertions(+), 5 deletions(-)
9d15b4
 create mode 100644 util/grub-get-kernel-settings.3
9d15b4
 create mode 100644 util/grub-get-kernel-settings.in
9d15b4
9d15b4
diff --git a/configure.ac b/configure.ac
ec4acb
index 3c4b9a19b10..40be70019e2 100644
9d15b4
--- a/configure.ac
9d15b4
+++ b/configure.ac
9d15b4
@@ -58,6 +58,7 @@ grub_TRANSFORM([grub-install])
9d15b4
 grub_TRANSFORM([grub-mkconfig])
9d15b4
 grub_TRANSFORM([grub-mkfont])
9d15b4
 grub_TRANSFORM([grub-mkimage])
9d15b4
+grub_TRANSFORM([grub-get-kernel-settings])
9d15b4
 grub_TRANSFORM([grub-glue-efi])
9d15b4
 grub_TRANSFORM([grub-mklayout])
9d15b4
 grub_TRANSFORM([grub-mkpasswd-pbkdf2])
9d15b4
@@ -75,6 +76,7 @@ grub_TRANSFORM([grub-file])
9d15b4
 grub_TRANSFORM([grub-bios-setup.3])
9d15b4
 grub_TRANSFORM([grub-editenv.1])
9d15b4
 grub_TRANSFORM([grub-fstest.3])
9d15b4
+grub_TRANSFORM([grub-get-kernel-settings.3])
9d15b4
 grub_TRANSFORM([grub-glue-efi.3])
9d15b4
 grub_TRANSFORM([grub-install.1])
9d15b4
 grub_TRANSFORM([grub-kbdcomp.3])
ec4acb
diff --git a/Makefile.util.def b/Makefile.util.def
ec4acb
index 226c46b2d79..ed27351eb47 100644
ec4acb
--- a/Makefile.util.def
ec4acb
+++ b/Makefile.util.def
ec4acb
@@ -713,6 +713,13 @@ script = {
ec4acb
   installdir = sbin;
ec4acb
 };
ec4acb
 
ec4acb
+script = {
ec4acb
+  name = grub-get-kernel-settings;
ec4acb
+  common = util/grub-get-kernel-settings.in;
ec4acb
+  mansection = 3;
ec4acb
+  installdir = sbin;
ec4acb
+};
ec4acb
+
ec4acb
 script = {
ec4acb
   name = grub-set-default;
ec4acb
   common = util/grub-set-default.in;
ec4acb
diff --git a/.gitignore b/.gitignore
ec4acb
index 387e3554cd5..9f258735aaa 100644
ec4acb
--- a/.gitignore
ec4acb
+++ b/.gitignore
ec4acb
@@ -64,6 +64,8 @@ grub-*.tar.*
ec4acb
 /grub*-fs-tester
ec4acb
 /grub*-fstest
ec4acb
 /grub*-fstest.1
ec4acb
+/grub*-get-kernel-settings
ec4acb
+/grub*-get-kernel-settings.3
ec4acb
 /grub*-glue-efi
ec4acb
 /grub*-glue-efi.1
ec4acb
 /grub*-install
9d15b4
diff --git a/util/bash-completion.d/grub-completion.bash.in b/util/bash-completion.d/grub-completion.bash.in
ec4acb
index 44bf135b9f8..5c4acd496d4 100644
9d15b4
--- a/util/bash-completion.d/grub-completion.bash.in
9d15b4
+++ b/util/bash-completion.d/grub-completion.bash.in
da63b3
@@ -264,6 +264,28 @@ have ${__grub_sparc64_setup_program} && \
da63b3
 unset __grub_sparc64_setup_program
9d15b4
 
9d15b4
 
da63b3
+#
9d15b4
+# grub-get-kernel-settings
9d15b4
+#
9d15b4
+_grub_get_kernel_settings () {
9d15b4
+    local cur
9d15b4
+
9d15b4
+    COMPREPLY=()
9d15b4
+    cur=`_get_cword`
9d15b4
+
9d15b4
+    if [[ "$cur" == -* ]]; then
9d15b4
+        __grubcomp "$(__grub_get_options_from_help)"
9d15b4
+    else
9d15b4
+        # Default complete with a filename
9d15b4
+        _filedir
9d15b4
+    fi
9d15b4
+}
9d15b4
+__grub_get_kernel_settings_program="@grub_get_kernel_settings@"
9d15b4
+have ${__grub_get_kernel_settings_program} && \
9d15b4
+ complete -F _grub_get_kernel_settings -o filenames ${__grub_get_kernel_settings_program}
9d15b4
+unset __grub_get_kernel_settings_program
9d15b4
+
9d15b4
+
da63b3
 #
9d15b4
 # grub-install
9d15b4
 #
9d15b4
diff --git a/util/grub-get-kernel-settings.3 b/util/grub-get-kernel-settings.3
9d15b4
new file mode 100644
ec4acb
index 00000000000..ba33330e28d
9d15b4
--- /dev/null
9d15b4
+++ b/util/grub-get-kernel-settings.3
9d15b4
@@ -0,0 +1,20 @@
9d15b4
+.TH GRUB-GET-KERNEL-SETTINGS 3 "Thu Jun 25 2015"
9d15b4
+.SH NAME
9d15b4
+\fBgrub-get-kernel-settings\fR \(em Evaluate the system's kernel installation settings for use while making a grub configuration file.
9d15b4
+
9d15b4
+.SH SYNOPSIS
9d15b4
+\fBgrub-get-kernel-settings\fR [OPTION]
9d15b4
+
9d15b4
+.SH DESCRIPTION
9d15b4
+\fBgrub-get-kernel-settings\fR reads the kernel installation settings on the host system, and emits a set of grub settings suitable for use when creating a grub configuration file.
9d15b4
+
9d15b4
+.SH OPTIONS
9d15b4
+.TP
9d15b4
+-h, --help
9d15b4
+Display program usage and exit.
9d15b4
+.TP
9d15b4
+-v, --version
9d15b4
+Display the current version.
9d15b4
+
9d15b4
+.SH SEE ALSO
9d15b4
+.BR "info grub"
9d15b4
diff --git a/util/grub-get-kernel-settings.in b/util/grub-get-kernel-settings.in
9d15b4
new file mode 100644
ec4acb
index 00000000000..12046219878
9d15b4
--- /dev/null
9d15b4
+++ b/util/grub-get-kernel-settings.in
9d15b4
@@ -0,0 +1,78 @@
9d15b4
+#!/bin/sh
9d15b4
+set -e
9d15b4
+
9d15b4
+# Evaluate new-kernel-pkg's configuration file.
9d15b4
+# Copyright (C) 2016 Free Software Foundation, Inc.
9d15b4
+#
9d15b4
+# GRUB is free software: you can redistribute it and/or modify
9d15b4
+# it under the terms of the GNU General Public License as published by
9d15b4
+# the Free Software Foundation, either version 3 of the License, or
9d15b4
+# (at your option) any later version.
9d15b4
+#
9d15b4
+# GRUB is distributed in the hope that it will be useful,
9d15b4
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
9d15b4
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9d15b4
+# GNU General Public License for more details.
9d15b4
+#
9d15b4
+# You should have received a copy of the GNU General Public License
9d15b4
+# along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
9d15b4
+
9d15b4
+PACKAGE_NAME=@PACKAGE_NAME@
9d15b4
+PACKAGE_VERSION=@PACKAGE_VERSION@
9d15b4
+datadir="@datadir@"
9d15b4
+if [ "x$pkgdatadir" = x ]; then
9d15b4
+    pkgdatadir="${datadir}/@PACKAGE@"
9d15b4
+fi
9d15b4
+
9d15b4
+self=`basename $0`
9d15b4
+
9d15b4
+export TEXTDOMAIN=@PACKAGE@
9d15b4
+export TEXTDOMAINDIR="@localedir@"
9d15b4
+
9d15b4
+. "${pkgdatadir}/grub-mkconfig_lib"
9d15b4
+
9d15b4
+# Usage: usage
9d15b4
+# Print the usage.
9d15b4
+usage () {
9d15b4
+    gettext_printf "Usage: %s [OPTION]\n" "$self"
9d15b4
+    gettext "Evaluate new-kernel-pkg configuration"; echo
9d15b4
+    echo
9d15b4
+    print_option_help "-h, --help" "$(gettext "print this message and exit")"
9d15b4
+    print_option_help "-v, --version" "$(gettext "print the version information and exit")"
9d15b4
+    echo
9d15b4
+}
9d15b4
+
9d15b4
+# Check the arguments.
9d15b4
+while test $# -gt 0
9d15b4
+do
9d15b4
+    option=$1
9d15b4
+    shift
9d15b4
+
9d15b4
+    case "$option" in
9d15b4
+    -h | --help)
9d15b4
+	usage
9d15b4
+	exit 0 ;;
9d15b4
+    -v | --version)
9d15b4
+	echo "$self (${PACKAGE_NAME}) ${PACKAGE_VERSION}"
9d15b4
+	exit 0 ;;
9d15b4
+    -*)
9d15b4
+	gettext_printf "Unrecognized option \`%s'\n" "$option" 1>&2
9d15b4
+	usage
9d15b4
+	exit 1
9d15b4
+	;;
9d15b4
+    # Explicitly ignore non-option arguments, for compatibility.
9d15b4
+    esac
9d15b4
+done
9d15b4
+
9d15b4
+if test -f /etc/sysconfig/kernel ; then
9d15b4
+    . /etc/sysconfig/kernel
9d15b4
+fi
9d15b4
+
9d15b4
+if [ "$MAKEDEBUG" = "yes" ]; then
9d15b4
+    echo GRUB_LINUX_MAKE_DEBUG=true
9d15b4
+    echo export GRUB_LINUX_MAKE_DEBUG
9d15b4
+    echo GRUB_CMDLINE_LINUX_DEBUG=\"systemd.log_level=debug systemd.log_target=kmsg\"
9d15b4
+    echo export GRUB_CMDLINE_LINUX_DEBUG
9d15b4
+    echo GRUB_LINUX_DEBUG_TITLE_POSTFIX=\" with debugging\"
9d15b4
+    echo export GRUB_LINUX_DEBUG_TITLE_POSTFIX
9d15b4
+fi
9d15b4
diff --git a/util/grub-mkconfig.in b/util/grub-mkconfig.in
ec4acb
index fb8724708b5..e32de5ef361 100644
9d15b4
--- a/util/grub-mkconfig.in
9d15b4
+++ b/util/grub-mkconfig.in
9d15b4
@@ -45,6 +45,7 @@ grub_probe="${sbindir}/@grub_probe@"
9d15b4
 grub_file="${bindir}/@grub_file@"
9d15b4
 grub_editenv="${bindir}/@grub_editenv@"
9d15b4
 grub_script_check="${bindir}/@grub_script_check@"
9d15b4
+grub_get_kernel_settings="${sbindir}/@grub_get_kernel_settings@"
9d15b4
 
9d15b4
 export TEXTDOMAIN=@PACKAGE@
9d15b4
 export TEXTDOMAINDIR="@localedir@"
9d15b4
@@ -151,6 +152,8 @@ if test -f ${sysconfdir}/default/grub ; then
9d15b4
   . ${sysconfdir}/default/grub
9d15b4
 fi
9d15b4
 
9d15b4
+eval "$("${grub_get_kernel_settings}")" || true
9d15b4
+
9d15b4
 if [ "x$GRUB_DISABLE_UUID" != "xtrue" -a -z "$GRUB_DEVICE_UUID" ]; then
9d15b4
   GRUB_DEVICE_UUID="$GRUB_DEVICE_UUID_GENERATED"
9d15b4
 fi
9d15b4
diff --git a/util/grub.d/10_linux.in b/util/grub.d/10_linux.in
ec4acb
index 584ad1f4639..c9bf223f273 100644
9d15b4
--- a/util/grub.d/10_linux.in
9d15b4
+++ b/util/grub.d/10_linux.in
bc092b
@@ -101,7 +101,8 @@ linux_entry ()
9d15b4
   os="$1"
9d15b4
   version="$2"
9d15b4
   type="$3"
9d15b4
-  args="$4"
9d15b4
+  isdebug="$4"
9d15b4
+  args="$5"
9d15b4
 
9d15b4
   sixteenbit=""
9d15b4
   linuxefi="linux"
bc092b
@@ -128,6 +129,9 @@ linux_entry ()
9d15b4
 	  quoted="$(echo "$GRUB_ACTUAL_DEFAULT" | grub_quote)"
9d15b4
 	  title_correction_code="${title_correction_code}if [ \"x\$default\" = '$quoted' ]; then default='$(echo "$replacement_title" | grub_quote)'; fi;"
9d15b4
       fi
9d15b4
+      if [ x$isdebug = xdebug ]; then
9d15b4
+	  title="$title${GRUB_LINUX_DEBUG_TITLE_POSTFIX}"
9d15b4
+      fi
9d15b4
       echo "menuentry '$(echo "$title" | grub_quote)' ${CLASS} \$menuentry_id_option 'gnulinux-$version-$type-$boot_device_id' {" | sed "s/^/$submenu_indentation/"
9d15b4
   else
9d15b4
       echo "menuentry '$(echo "$os" | grub_quote)' ${CLASS} \$menuentry_id_option 'gnulinux-simple-$boot_device_id' {" | sed "s/^/$submenu_indentation/"
bc092b
@@ -285,11 +289,15 @@ while [ "x$list" != "x" ] ; do
9d15b4
   fi
9d15b4
 
9d15b4
   if [ "x$is_top_level" = xtrue ] && [ "x${GRUB_DISABLE_SUBMENU}" != xtrue ]; then
9d15b4
-    linux_entry "${OS}" "${version}" simple \
9d15b4
+    linux_entry "${OS}" "${version}" simple standard \
9d15b4
     "${GRUB_CMDLINE_LINUX} ${GRUB_CMDLINE_LINUX_DEFAULT}"
9d15b4
+    if [ "x$GRUB_LINUX_MAKE_DEBUG" = "xtrue" ]; then
9d15b4
+      linux_entry "${OS}" "${version}" simple debug \
9d15b4
+        "${GRUB_CMDLINE_LINUX} ${GRUB_CMDLINE_LINUX_DEFAULT} ${GRUB_CMDLINE_LINUX_DEBUG}"
9d15b4
+    fi
9d15b4
 
9d15b4
     submenu_indentation="$grub_tab"
9d15b4
-    
9d15b4
+
9d15b4
     if [ -z "$boot_device_id" ]; then
9d15b4
 	boot_device_id="$(grub_get_device_id "${GRUB_DEVICE}")"
9d15b4
     fi
bc092b
@@ -298,10 +306,15 @@ while [ "x$list" != "x" ] ; do
9d15b4
     is_top_level=false
9d15b4
   fi
9d15b4
 
9d15b4
-  linux_entry "${OS}" "${version}" advanced \
9d15b4
+  linux_entry "${OS}" "${version}" advanced standard \
9d15b4
               "${GRUB_CMDLINE_LINUX} ${GRUB_CMDLINE_LINUX_DEFAULT}"
9d15b4
+  if [ "x$GRUB_LINUX_MAKE_DEBUG" = "xtrue" ]; then
9d15b4
+    linux_entry "${OS}" "${version}" advanced debug \
9d15b4
+                "${GRUB_CMDLINE_LINUX} ${GRUB_CMDLINE_LINUX_DEFAULT} ${GRUB_CMDLINE_LINUX_DEBUG}"
9d15b4
+  fi
9d15b4
+
9d15b4
   if [ "x${GRUB_DISABLE_RECOVERY}" != "xtrue" ]; then
9d15b4
-    linux_entry "${OS}" "${version}" recovery \
9d15b4
+    linux_entry "${OS}" "${version}" recovery standard \
9d15b4
                 "single ${GRUB_CMDLINE_LINUX}"
9d15b4
   fi
9d15b4