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