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