15a2072
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
15a2072
From: Peter Jones <pjones@redhat.com>
15a2072
Date: Fri, 9 Dec 2016 15:40:29 -0500
15a2072
Subject: [PATCH] Add BLS support to grub-mkconfig
15a2072
15a2072
GRUB now has BootLoaderSpec support, the user can choose to use this by
15a2072
setting GRUB_ENABLE_BLSCFG to true in /etc/default/grub. On this setup,
15a2072
the boot menu entries are not added to the grub.cfg, instead BLS config
15a2072
files are parsed by blscfg command and the entries created dynamically.
15a2072
---
15a2072
 util/grub-mkconfig.in     |  3 ++-
15a2072
 util/grub-mkconfig_lib.in | 29 +++++++++++++++++++----------
15a2072
 util/grub.d/10_linux.in   | 37 +++++++++++++++++++++++++++++++++++--
15a2072
 3 files changed, 56 insertions(+), 13 deletions(-)
15a2072
15a2072
diff --git a/util/grub-mkconfig.in b/util/grub-mkconfig.in
15a2072
index 4248b9341ab..c20171919d9 100644
15a2072
--- a/util/grub-mkconfig.in
15a2072
+++ b/util/grub-mkconfig.in
15a2072
@@ -259,7 +259,8 @@ export GRUB_DEFAULT \
15a2072
   GRUB_OS_PROBER_SKIP_LIST \
15a2072
   GRUB_DISABLE_SUBMENU \
15a2072
   GRUB_DEFAULT_DTB \
15a2072
-  SUSE_BTRFS_SNAPSHOT_BOOTING
15a2072
+  SUSE_BTRFS_SNAPSHOT_BOOTING \
15a2072
+  GRUB_ENABLE_BLSCFG
15a2072
 
15a2072
 if test "x${grub_cfg}" != "x"; then
15a2072
   rm -f "${grub_cfg}.new"
15a2072
diff --git a/util/grub-mkconfig_lib.in b/util/grub-mkconfig_lib.in
15a2072
index b3aae534ddc..1acc1d01c39 100644
15a2072
--- a/util/grub-mkconfig_lib.in
15a2072
+++ b/util/grub-mkconfig_lib.in
15a2072
@@ -30,6 +30,9 @@ fi
15a2072
 if test "x$grub_file" = x; then
15a2072
   grub_file="${bindir}/@grub_file@"
15a2072
 fi
15a2072
+if test "x$grub_editenv" = x; then
15a2072
+  grub_editenv="${bindir}/@grub_editenv@"
15a2072
+fi
15a2072
 if test "x$grub_mkrelpath" = x; then
15a2072
   grub_mkrelpath="${bindir}/@grub_mkrelpath@"
15a2072
 fi
15a2072
@@ -127,10 +130,16 @@ EOF
15a2072
 
15a2072
 prepare_grub_to_access_device ()
15a2072
 {
15a2072
+  local device=$1 && shift
15a2072
+  if [ "$#" -gt 0 ]; then
15a2072
+    local variable=$1 && shift
15a2072
+  else
15a2072
+    local variable=root
15a2072
+  fi
15a2072
   old_ifs="$IFS"
15a2072
   IFS='
15a2072
 '
15a2072
-  partmap="`"${grub_probe}" --device $@ --target=partmap`"
15a2072
+  partmap="`"${grub_probe}" --device ${device} --target=partmap`"
15a2072
   for module in ${partmap} ; do
15a2072
     case "${module}" in
15a2072
       netbsd | openbsd)
15a2072
@@ -141,34 +150,34 @@ prepare_grub_to_access_device ()
15a2072
   done
15a2072
 
15a2072
   # Abstraction modules aren't auto-loaded.
15a2072
-  abstraction="`"${grub_probe}" --device $@ --target=abstraction`"
15a2072
+  abstraction="`"${grub_probe}" --device ${device} --target=abstraction`"
15a2072
   for module in ${abstraction} ; do
15a2072
     echo "insmod ${module}"
15a2072
   done
15a2072
 
15a2072
-  fs="`"${grub_probe}" --device $@ --target=fs`"
15a2072
+  fs="`"${grub_probe}" --device ${device} --target=fs`"
15a2072
   for module in ${fs} ; do
15a2072
     echo "insmod ${module}"
15a2072
   done
15a2072
 
15a2072
   if [ x$GRUB_ENABLE_CRYPTODISK = xy ]; then
15a2072
-      for uuid in `"${grub_probe}" --device $@ --target=cryptodisk_uuid`; do
15a2072
+      for uuid in `"${grub_probe}" --device ${device} --target=cryptodisk_uuid`; do
15a2072
 	  echo "cryptomount -u $uuid"
15a2072
       done
15a2072
   fi
15a2072
 
15a2072
   # If there's a filesystem UUID that GRUB is capable of identifying, use it;
15a2072
   # otherwise set root as per value in device.map.
15a2072
-  fs_hint="`"${grub_probe}" --device $@ --target=compatibility_hint`"
15a2072
+  fs_hint="`"${grub_probe}" --device ${device} --target=compatibility_hint`"
15a2072
   if [ "x$fs_hint" != x ]; then
15a2072
-    echo "set root='$fs_hint'"
15a2072
+    echo "set ${variable}='$fs_hint'"
15a2072
   fi
15a2072
-  if [ "x$GRUB_DISABLE_UUID" != "xtrue" ] && fs_uuid="`"${grub_probe}" --device $@ --target=fs_uuid 2> /dev/null`" ; then
15a2072
-    hints="`"${grub_probe}" --device $@ --target=hints_string 2> /dev/null`" || hints=
15a2072
+  if [ "x$GRUB_DISABLE_UUID" != "xtrue" ] && fs_uuid="`"${grub_probe}" --device ${device} --target=fs_uuid 2> /dev/null`" ; then
15a2072
+    hints="`"${grub_probe}" --device ${device} --target=hints_string 2> /dev/null`" || hints=
15a2072
     echo "if [ x\$feature_platform_search_hint = xy ]; then"
15a2072
-    echo "  search --no-floppy --fs-uuid --set=root ${hints} ${fs_uuid}"
15a2072
+    echo "  search --no-floppy --fs-uuid --set=${variable} ${hints} ${fs_uuid}"
15a2072
     echo "else"
15a2072
-    echo "  search --no-floppy --fs-uuid --set=root ${fs_uuid}"
15a2072
+    echo "  search --no-floppy --fs-uuid --set=${variable} ${fs_uuid}"
15a2072
     echo "fi"
15a2072
   fi
15a2072
   IFS="$old_ifs"
15a2072
diff --git a/util/grub.d/10_linux.in b/util/grub.d/10_linux.in
15a2072
index d9a05937e46..839f1fdb655 100644
15a2072
--- a/util/grub.d/10_linux.in
15a2072
+++ b/util/grub.d/10_linux.in
15a2072
@@ -121,6 +121,34 @@ linux_entry ()
15a2072
   if [ -z "$boot_device_id" ]; then
15a2072
       boot_device_id="$(grub_get_device_id "${GRUB_DEVICE}")"
15a2072
   fi
15a2072
+
15a2072
+  if [ "x${GRUB_ENABLE_BLSCFG}" = "xtrue" ]; then
15a2072
+    if [ x$dirname = x/ ]; then
15a2072
+      if [ -z "${prepare_root_cache}" ]; then
15a2072
+        prepare_grub_to_access_device ${GRUB_DEVICE}
15a2072
+      fi
15a2072
+    else
15a2072
+      if [ -z "${prepare_boot_cache}" ]; then
15a2072
+        prepare_grub_to_access_device ${GRUB_DEVICE_BOOT}
15a2072
+      fi
15a2072
+    fi
15a2072
+
15a2072
+    bootefi_device="`${grub_probe} --target=device /boot/efi/`"
15a2072
+    prepare_grub_to_access_device ${bootefi_device} boot
15a2072
+
15a2072
+    cat << EOF
15a2072
+insmod blscfg
15a2072
+blscfg
15a2072
+if [ -s \$prefix/grubenv ]; then
15a2072
+  load_env
15a2072
+fi
15a2072
+EOF
15a2072
+
15a2072
+    ${grub_editenv} - set kernelopts="root=${linux_root_device_thisversion} ro ${args}"
15a2072
+
15a2072
+    exit 0
15a2072
+  fi
15a2072
+
15a2072
   if [ x$type != xsimple ] ; then
15a2072
       title=$(mktitle "$type" "$version")
15a2072
       if [ x"$title" = x"$GRUB_ACTUAL_DEFAULT" ] || [ x"Previous Linux versions>$title" = x"$GRUB_ACTUAL_DEFAULT" ]; then
15a2072
@@ -223,7 +251,10 @@ submenu_indentation=""
15a2072
 is_top_level=true
15a2072
 while [ "x$list" != "x" ] ; do
15a2072
   linux=`version_find_latest $list`
15a2072
-  gettext_printf "Found linux image: %s\n" "$linux" >&2
15a2072
+  if [ "x${GRUB_ENABLE_BLSCFG}" != "xtrue" ]; then
15a2072
+    gettext_printf "Found linux image: %s\n" "$linux" >&2
15a2072
+  fi
15a2072
+
15a2072
   basename=`basename $linux`
15a2072
   dirname=`dirname $linux`
15a2072
   rel_dirname=`make_system_path_relative_to_its_root $dirname`
15a2072
@@ -262,7 +293,9 @@ while [ "x$list" != "x" ] ; do
15a2072
     for i in ${initrd}; do
15a2072
       initrd_display="${initrd_display} ${dirname}/${i}"
15a2072
     done
15a2072
-    gettext_printf "Found initrd image: %s\n" "$(echo $initrd_display)" >&2
15a2072
+    if [ "x${GRUB_ENABLE_BLSCFG}" != "xtrue" ]; then
15a2072
+      gettext_printf "Found initrd image: %s\n" "$(echo $initrd_display)" >&2
15a2072
+    fi
15a2072
   fi
15a2072
 
15a2072
   fdt=