Blame SOURCES/0074-Add-BLS-support-to-grub-mkconfig.patch

8e15ce
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
8e15ce
From: Peter Jones <pjones@redhat.com>
8e15ce
Date: Fri, 9 Dec 2016 15:40:29 -0500
8e15ce
Subject: [PATCH] Add BLS support to grub-mkconfig
8e15ce
8e15ce
GRUB now has BootLoaderSpec support, the user can choose to use this by
8e15ce
setting GRUB_ENABLE_BLSCFG to true in /etc/default/grub. On this setup,
8e15ce
the boot menu entries are not added to the grub.cfg, instead BLS config
8e15ce
files are parsed by blscfg command and the entries created dynamically.
8e15ce
8e15ce
A 10_linux_bls grub.d snippet to generate menu entries from BLS files
8e15ce
is also added that can be used on platforms where the bootloader doesn't
8e15ce
have BLS support and only can parse a normal grub configuration file.
8e15ce
8e15ce
Portions of the 10_linux_bls were taken from the ostree-grub-generator
8e15ce
script that's included in the OSTree project.
8e15ce
8e15ce
Fixes to support multi-devices and generate a BLS section even if no
8e15ce
kernels are found in the boot directory were proposed by Yclept Nemo
8e15ce
and Tom Gundersen respectively.
8e15ce
8e15ce
Signed-off-by: Peter Jones <pjones@redhat.com>
8e15ce
Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
8e15ce
---
8e15ce
 util/grub-mkconfig.8      |   4 +
8e15ce
 util/grub-mkconfig.in     |   9 +-
8e15ce
 util/grub-mkconfig_lib.in |  22 ++++-
8e15ce
 util/grub.d/10_linux.in   | 223 +++++++++++++++++++++++++++++++++++++++++++++-
8e15ce
 4 files changed, 252 insertions(+), 6 deletions(-)
8e15ce
8e15ce
diff --git a/util/grub-mkconfig.8 b/util/grub-mkconfig.8
8e15ce
index a2d1f577b9b..434fa4deda4 100644
8e15ce
--- a/util/grub-mkconfig.8
8e15ce
+++ b/util/grub-mkconfig.8
8e15ce
@@ -13,5 +13,9 @@
8e15ce
 \fB--output\fR=\fIFILE\fR
8e15ce
 Write generated output to \fIFILE\fR.
8e15ce
 
8e15ce
+.TP
8e15ce
+\fB--no-grubenv-update\fR
8e15ce
+Do not update variables in the grubenv file.
8e15ce
+
8e15ce
 .SH SEE ALSO
8e15ce
 .BR "info grub"
8e15ce
diff --git a/util/grub-mkconfig.in b/util/grub-mkconfig.in
8e15ce
index 535c0f02499..f55339a3f64 100644
8e15ce
--- a/util/grub-mkconfig.in
8e15ce
+++ b/util/grub-mkconfig.in
8e15ce
@@ -50,6 +50,8 @@ grub_get_kernel_settings="${sbindir}/@grub_get_kernel_settings@"
8e15ce
 export TEXTDOMAIN=@PACKAGE@
8e15ce
 export TEXTDOMAINDIR="@localedir@"
8e15ce
 
8e15ce
+export GRUB_GRUBENV_UPDATE="yes"
8e15ce
+
8e15ce
 . "${pkgdatadir}/grub-mkconfig_lib"
8e15ce
 
8e15ce
 # Usage: usage
8e15ce
@@ -59,6 +61,7 @@ usage () {
8e15ce
     gettext "Generate a grub config file"; echo
8e15ce
     echo
8e15ce
     print_option_help "-o, --output=$(gettext FILE)" "$(gettext "output generated config to FILE [default=stdout]")"
8e15ce
+    print_option_help "--no-grubenv-update" "$(gettext "do not update variables in the grubenv file")"
8e15ce
     print_option_help "-h, --help" "$(gettext "print this message and exit")"
8e15ce
     print_option_help "-V, --version" "$(gettext "print the version information and exit")"
8e15ce
     echo
8e15ce
@@ -94,6 +97,9 @@ do
8e15ce
     --output=*)
8e15ce
 	grub_cfg=`echo "$option" | sed 's/--output=//'`
8e15ce
 	;;
8e15ce
+    --no-grubenv-update)
8e15ce
+	GRUB_GRUBENV_UPDATE="no"
8e15ce
+	;;
8e15ce
     -*)
8e15ce
 	gettext_printf "Unrecognized option \`%s'\n" "$option" 1>&2
8e15ce
 	usage
8e15ce
@@ -253,7 +259,8 @@ export GRUB_DEFAULT \
8e15ce
   GRUB_OS_PROBER_SKIP_LIST \
8e15ce
   GRUB_DISABLE_SUBMENU \
8e15ce
   GRUB_DEFAULT_DTB \
8e15ce
-  SUSE_BTRFS_SNAPSHOT_BOOTING
8e15ce
+  SUSE_BTRFS_SNAPSHOT_BOOTING \
8e15ce
+  GRUB_ENABLE_BLSCFG
8e15ce
 
8e15ce
 if test "x${grub_cfg}" != "x"; then
8e15ce
   rm -f "${grub_cfg}.new"
8e15ce
diff --git a/util/grub-mkconfig_lib.in b/util/grub-mkconfig_lib.in
8e15ce
index fafeac95061..d8bb4069360 100644
8e15ce
--- a/util/grub-mkconfig_lib.in
8e15ce
+++ b/util/grub-mkconfig_lib.in
8e15ce
@@ -30,6 +30,9 @@ fi
8e15ce
 if test "x$grub_file" = x; then
8e15ce
   grub_file="${bindir}/@grub_file@"
8e15ce
 fi
8e15ce
+if test "x$grub_editenv" = x; then
8e15ce
+  grub_editenv="${bindir}/@grub_editenv@"
8e15ce
+fi
8e15ce
 if test "x$grub_mkrelpath" = x; then
8e15ce
   grub_mkrelpath="${bindir}/@grub_mkrelpath@"
8e15ce
 fi
8e15ce
@@ -125,8 +128,19 @@ EOF
8e15ce
   fi
8e15ce
 }
8e15ce
 
8e15ce
+prepare_grub_to_access_device_with_variable ()
8e15ce
+{
8e15ce
+  device_variable="$1"
8e15ce
+  shift
8e15ce
+  prepare_grub_to_access_device "$@"
8e15ce
+  unset "device_variable"
8e15ce
+}
8e15ce
+
8e15ce
 prepare_grub_to_access_device ()
8e15ce
 {
8e15ce
+  if [ -z "$device_variable" ]; then
8e15ce
+    device_variable="root"
8e15ce
+  fi
8e15ce
   old_ifs="$IFS"
8e15ce
   IFS='
8e15ce
 '
8e15ce
@@ -161,18 +175,18 @@ prepare_grub_to_access_device ()
8e15ce
   # otherwise set root as per value in device.map.
8e15ce
   fs_hint="`"${grub_probe}" --device $@ --target=compatibility_hint`"
8e15ce
   if [ "x$fs_hint" != x ]; then
8e15ce
-    echo "set root='$fs_hint'"
8e15ce
+    echo "set ${device_variable}='$fs_hint'"
8e15ce
   fi
8e15ce
   if [ "x${GRUB_DISABLE_UUID}" != "xtrue" ] && fs_uuid="`"${grub_probe}" --device $@ --target=fs_uuid 2> /dev/null`" ; then
8e15ce
     hints="`"${grub_probe}" --device $@ --target=hints_string 2> /dev/null`" || hints=
8e15ce
     if [ "x$hints" != x ]; then
8e15ce
       echo "if [ x\$feature_platform_search_hint = xy ]; then"
8e15ce
-      echo "  search --no-floppy --fs-uuid --set=root ${hints} ${fs_uuid}"
8e15ce
+      echo "  search --no-floppy --fs-uuid --set=${device_variable} ${hints} ${fs_uuid}"
8e15ce
       echo "else"
8e15ce
-      echo "  search --no-floppy --fs-uuid --set=root ${fs_uuid}"
8e15ce
+      echo "  search --no-floppy --fs-uuid --set=${device_variable} ${fs_uuid}"
8e15ce
       echo "fi"
8e15ce
     else
8e15ce
-      echo "search --no-floppy --fs-uuid --set=root ${fs_uuid}"
8e15ce
+      echo "search --no-floppy --fs-uuid --set=${device_variable} ${fs_uuid}"
8e15ce
     fi
8e15ce
   fi
8e15ce
   IFS="$old_ifs"
8e15ce
diff --git a/util/grub.d/10_linux.in b/util/grub.d/10_linux.in
8e15ce
index cbfaca34cc7..68adb55d893 100644
8e15ce
--- a/util/grub.d/10_linux.in
8e15ce
+++ b/util/grub.d/10_linux.in
8e15ce
@@ -82,6 +82,223 @@ case x"$GRUB_FS" in
8e15ce
 	;;
8e15ce
 esac
8e15ce
 
8e15ce
+populate_header_warn()
8e15ce
+{
8e15ce
+if [ "x${BLS_POPULATE_MENU}" = "xtrue" ]; then
8e15ce
+  bls_parser="10_linux script"
8e15ce
+else
8e15ce
+  bls_parser="blscfg command"
8e15ce
+fi
8e15ce
+cat <
8e15ce
+
8e15ce
+# This section was generated by a script. Do not modify the generated file - all changes
8e15ce
+# will be lost the next time file is regenerated. Instead edit the BootLoaderSpec files.
8e15ce
+#
8e15ce
+# The $bls_parser parses the BootLoaderSpec files stored in /boot/loader/entries and
8e15ce
+# populates the boot menu. Please refer to the Boot Loader Specification documentation
8e15ce
+# for the files format: https://www.freedesktop.org/wiki/Specifications/BootLoaderSpec/.
8e15ce
+
8e15ce
+EOF
8e15ce
+}
8e15ce
+
8e15ce
+read_config()
8e15ce
+{
8e15ce
+    config_file=${1}
8e15ce
+    title=""
8e15ce
+    initrd=""
8e15ce
+    options=""
8e15ce
+    linux=""
8e15ce
+    grub_arg=""
8e15ce
+
8e15ce
+    while read -r line
8e15ce
+    do
8e15ce
+        record=$(echo ${line} | cut -f 1 -d ' ')
8e15ce
+        value=$(echo ${line} | cut -s -f2- -d ' ')
8e15ce
+        case "${record}" in
8e15ce
+            "title")
8e15ce
+                title=${value}
8e15ce
+                ;;
8e15ce
+            "initrd")
8e15ce
+                initrd=${value}
8e15ce
+                ;;
8e15ce
+            "linux")
8e15ce
+                linux=${value}
8e15ce
+                ;;
8e15ce
+            "options")
8e15ce
+                options=${value}
8e15ce
+                ;;
8e15ce
+            "grub_arg")
8e15ce
+                grub_arg=${value}
8e15ce
+                ;;
8e15ce
+        esac
8e15ce
+    done < ${config_file}
8e15ce
+}
8e15ce
+
8e15ce
+blsdir="/boot/loader/entries"
8e15ce
+
8e15ce
+get_sorted_bls()
8e15ce
+{
8e15ce
+    if ! [ -d "${blsdir}" ] || ! [ -e /etc/machine-id ]; then
8e15ce
+        return
8e15ce
+    fi
8e15ce
+
8e15ce
+    read machine_id < /etc/machine-id
8e15ce
+    if [ -z "${machine_id}" ]; then
8e15ce
+        return
8e15ce
+    fi
8e15ce
+
8e15ce
+    local IFS=$'\n'
8e15ce
+
8e15ce
+    files=($(for bls in ${blsdir}/${machine_id}-*.conf; do
8e15ce
+        if ! [[ -e "${bls}" ]] ; then
8e15ce
+            continue
8e15ce
+        fi
8e15ce
+        bls="${bls%.conf}"
8e15ce
+        bls="${bls##*/}"
8e15ce
+        echo "${bls}"
8e15ce
+    done | ${kernel_sort} 2>/dev/null | tac)) || :
8e15ce
+
8e15ce
+    echo "${files[@]}"
8e15ce
+}
8e15ce
+
8e15ce
+update_bls_cmdline()
8e15ce
+{
8e15ce
+    local cmdline="root=${LINUX_ROOT_DEVICE} ro ${GRUB_CMDLINE_LINUX} ${GRUB_CMDLINE_LINUX_DEFAULT}"
8e15ce
+    local -a files=($(get_sorted_bls))
8e15ce
+
8e15ce
+    for bls in "${files[@]}"; do
8e15ce
+        local options="${cmdline}"
8e15ce
+        if [ -z "${bls##*debug*}" ]; then
8e15ce
+            options="${options} ${GRUB_CMDLINE_LINUX_DEBUG}"
8e15ce
+        fi
8e15ce
+        options="$(echo "${options}" | sed -e 's/\//\\\//g')"
8e15ce
+        sed -i -e "s/^options.*/options ${options}/" "${blsdir}/${bls}.conf"
8e15ce
+    done
8e15ce
+}
8e15ce
+
8e15ce
+populate_menu()
8e15ce
+{
8e15ce
+    local -a files=($(get_sorted_bls))
8e15ce
+
8e15ce
+    gettext_printf "Generating boot entries from BLS files...\n" >&2
8e15ce
+
8e15ce
+    for bls in "${files[@]}"; do
8e15ce
+        read_config "${blsdir}/${bls}.conf"
8e15ce
+
8e15ce
+        menu="${menu}menuentry '${title}' ${grub_arg} --id=${bls} {\n"
8e15ce
+        menu="${menu}\t linux ${linux} ${options}\n"
8e15ce
+        if [ -n "${initrd}" ] ; then
8e15ce
+            menu="${menu}\t initrd ${boot_prefix}${initrd}\n"
8e15ce
+        fi
8e15ce
+        menu="${menu}}\n\n"
8e15ce
+    done
8e15ce
+    # The printf command seems to be more reliable across shells for special character (\n, \t) evaluation
8e15ce
+    printf "$menu"
8e15ce
+}
8e15ce
+
8e15ce
+# Make BLS the default if GRUB_ENABLE_BLSCFG was not set and grubby is not installed.
8e15ce
+if [ -z "${GRUB_ENABLE_BLSCFG}" ] && [ -z "$(which new-kernel-pkg 2> /dev/null)" ]; then
8e15ce
+	  GRUB_ENABLE_BLSCFG="true"
8e15ce
+fi
8e15ce
+
8e15ce
+if [ "x${GRUB_ENABLE_BLSCFG}" = "xtrue" ]; then
8e15ce
+  if [ x$dirname = x/ ]; then
8e15ce
+    if [ -z "${prepare_root_cache}" ]; then
8e15ce
+      prepare_grub_to_access_device ${GRUB_DEVICE}
8e15ce
+    fi
8e15ce
+  else
8e15ce
+    if [ -z "${prepare_boot_cache}" ]; then
8e15ce
+      prepare_grub_to_access_device ${GRUB_DEVICE_BOOT}
8e15ce
+    fi
8e15ce
+  fi
8e15ce
+
8e15ce
+  if [ -d /sys/firmware/efi ]; then
8e15ce
+      bootefi_device="`${grub_probe} --target=device /boot/efi/`"
8e15ce
+      prepare_grub_to_access_device_with_variable boot ${bootefi_device}
8e15ce
+  else
8e15ce
+      boot_device="`${grub_probe} --target=device /boot/`"
8e15ce
+      prepare_grub_to_access_device_with_variable boot ${boot_device}
8e15ce
+  fi
8e15ce
+
8e15ce
+  arch="$(uname -m)"
8e15ce
+  if [ "x${arch}" = "xppc64le" ] && [ -d /sys/firmware/opal ]; then
8e15ce
+
8e15ce
+      BLS_POPULATE_MENU="true"
8e15ce
+      petitboot_path="/sys/firmware/devicetree/base/ibm,firmware-versions/petitboot"
8e15ce
+
8e15ce
+      if test -e ${petitboot_path}; then
8e15ce
+          read -r -d '' petitboot_version < ${petitboot_path}
8e15ce
+          petitboot_version="$(echo ${petitboot_version//v})"
8e15ce
+
8e15ce
+	  if test -n ${petitboot_version}; then
8e15ce
+              major_version="$(echo ${petitboot_version} | cut -d . -f1)"
8e15ce
+              minor_version="$(echo ${petitboot_version} | cut -d . -f2)"
8e15ce
+
8e15ce
+              re='^[0-9]+$'
8e15ce
+              if [[ $major_version =~ $re ]] && [[ $minor_version =~ $re ]] &&
8e15ce
+                 ([[ ${major_version} -gt 1 ]] ||
8e15ce
+                  [[ ${major_version} -eq 1 &&
8e15ce
+                     ${minor_version} -ge 8  ]]); then
8e15ce
+                  BLS_POPULATE_MENU="false"
8e15ce
+              fi
8e15ce
+          fi
8e15ce
+      fi
8e15ce
+  fi
8e15ce
+
8e15ce
+  populate_header_warn
8e15ce
+
8e15ce
+  cat << EOF
8e15ce
+# The kernelopts variable should be defined in the grubenv file. But to ensure that menu
8e15ce
+# entries populated from BootLoaderSpec files that use this variable work correctly even
8e15ce
+# without a grubenv file, define a fallback kernelopts variable if this has not been set.
8e15ce
+#
8e15ce
+# The kernelopts variable in the grubenv file can be modified using the grubby tool or by
8e15ce
+# executing the grub2-mkconfig tool. For the latter, the values of the GRUB_CMDLINE_LINUX
8e15ce
+# and GRUB_CMDLINE_LINUX_DEFAULT options from /etc/default/grub file are used to set both
8e15ce
+# the kernelopts variable in the grubenv file and the fallback kernelopts variable.
8e15ce
+if [ -z "\${kernelopts}" ]; then
8e15ce
+  set kernelopts="root=${LINUX_ROOT_DEVICE} ro ${GRUB_CMDLINE_LINUX} ${GRUB_CMDLINE_LINUX_DEFAULT}"
8e15ce
+fi
8e15ce
+EOF
8e15ce
+
8e15ce
+  update_bls_cmdline
8e15ce
+
8e15ce
+  if [ "x${BLS_POPULATE_MENU}" = "xtrue" ]; then
8e15ce
+      populate_menu
8e15ce
+  else
8e15ce
+      cat << EOF
8e15ce
+
8e15ce
+insmod blscfg
8e15ce
+blscfg
8e15ce
+EOF
8e15ce
+  fi
8e15ce
+
8e15ce
+  if [ "x${GRUB_GRUBENV_UPDATE}" = "xyes" ]; then
8e15ce
+      blsdir="/boot/loader/entries"
8e15ce
+      [ -d "${blsdir}" ] && GRUB_BLS_FS="$(${grub_probe} --target=fs ${blsdir})"
8e15ce
+      if [ "x${GRUB_BLS_FS}" = "xbtrfs" ] || [ "x${GRUB_BLS_FS}" = "xzfs" ]; then
8e15ce
+          blsdir=$(make_system_path_relative_to_its_root "${blsdir}")
8e15ce
+          if [ "x${blsdir}" != "x/loader/entries" ] && [ "x${blsdir}" != "x/boot/loader/entries" ]; then
8e15ce
+              ${grub_editenv} - set blsdir="${blsdir}"
8e15ce
+          fi
8e15ce
+      fi
8e15ce
+
8e15ce
+      if [ -n "${GRUB_EARLY_INITRD_LINUX_CUSTOM}" ]; then
8e15ce
+          ${grub_editenv} - set early_initrd="${GRUB_EARLY_INITRD_LINUX_CUSTOM}"
8e15ce
+      fi
8e15ce
+
8e15ce
+      if [ -n "${GRUB_DEFAULT_DTB}" ]; then
8e15ce
+          ${grub_editenv} - set devicetree="${GRUB_DEFAULT_DTB}"
8e15ce
+      fi
8e15ce
+
8e15ce
+      if [ -n "${GRUB_SAVEDEFAULT}" ]; then
8e15ce
+           ${grub_editenv} - set save_default="${GRUB_SAVEDEFAULT}"
8e15ce
+      fi
8e15ce
+  fi
8e15ce
+
8e15ce
+  exit 0
8e15ce
+fi
8e15ce
+
8e15ce
 mktitle ()
8e15ce
 {
8e15ce
   local title_type
8e15ce
@@ -121,6 +338,7 @@ linux_entry ()
8e15ce
   if [ -z "$boot_device_id" ]; then
8e15ce
       boot_device_id="$(grub_get_device_id "${GRUB_DEVICE}")"
8e15ce
   fi
8e15ce
+
8e15ce
   if [ x$type != xsimple ] ; then
8e15ce
       title=$(mktitle "$type" "$version")
8e15ce
       if [ x"$title" = x"$GRUB_ACTUAL_DEFAULT" ] || [ x"Previous Linux versions>$title" = x"$GRUB_ACTUAL_DEFAULT" ]; then
8e15ce
@@ -224,6 +442,7 @@ is_top_level=true
8e15ce
 while [ "x$list" != "x" ] ; do
8e15ce
   linux=`version_find_latest $list`
8e15ce
   gettext_printf "Found linux image: %s\n" "$linux" >&2
8e15ce
+
8e15ce
   basename=`basename $linux`
8e15ce
   dirname=`dirname $linux`
8e15ce
   rel_dirname=`make_system_path_relative_to_its_root $dirname`
8e15ce
@@ -262,7 +481,9 @@ while [ "x$list" != "x" ] ; do
8e15ce
     for i in ${initrd}; do
8e15ce
       initrd_display="${initrd_display} ${dirname}/${i}"
8e15ce
     done
8e15ce
-    gettext_printf "Found initrd image: %s\n" "$(echo $initrd_display)" >&2
8e15ce
+    if [ "x${GRUB_ENABLE_BLSCFG}" != "xtrue" ]; then
8e15ce
+      gettext_printf "Found initrd image: %s\n" "$(echo $initrd_display)" >&2
8e15ce
+    fi
8e15ce
   fi
8e15ce
 
8e15ce
   fdt=