Blame SOURCES/0077-Add-grub2-switch-to-blscfg.patch

8e15ce
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
8e15ce
From: Peter Jones <pjones@redhat.com>
8e15ce
Date: Thu, 15 Mar 2018 14:12:40 -0400
8e15ce
Subject: [PATCH] Add grub2-switch-to-blscfg
8e15ce
8e15ce
Signed-off-by: Peter Jones <pjones@redhat.com>
8e15ce
Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
8e15ce
[jhlavac: Use ${etcdefaultgrub} instead of /etc/default/grub]
8e15ce
Signed-off-by: Jan Hlavac <jhlavac@redhat.com>
8e15ce
---
8e15ce
 Makefile.util.def             |   7 +
8e15ce
 util/grub-set-password.in     |   2 +-
8e15ce
 util/grub-switch-to-blscfg.8  |  33 +++++
8e15ce
 util/grub-switch-to-blscfg.in | 317 ++++++++++++++++++++++++++++++++++++++++++
8e15ce
 4 files changed, 358 insertions(+), 1 deletion(-)
8e15ce
 create mode 100644 util/grub-switch-to-blscfg.8
8e15ce
 create mode 100644 util/grub-switch-to-blscfg.in
8e15ce
8e15ce
diff --git a/Makefile.util.def b/Makefile.util.def
8e15ce
index cdd2f51fe4b..afc4d7b0c3e 100644
8e15ce
--- a/Makefile.util.def
8e15ce
+++ b/Makefile.util.def
8e15ce
@@ -1364,6 +1364,13 @@ program = {
8e15ce
   ldadd = '$(LIBINTL) $(LIBDEVMAPPER) $(LIBZFS) $(LIBNVPAIR) $(LIBGEOM)';
8e15ce
 };
8e15ce
 
8e15ce
+script = {
8e15ce
+  name = grub-switch-to-blscfg;
8e15ce
+  common = util/grub-switch-to-blscfg.in;
8e15ce
+  mansection = 8;
8e15ce
+  installdir = sbin;
8e15ce
+};
8e15ce
+
8e15ce
 program = {
8e15ce
   name = grub-glue-efi;
8e15ce
   mansection = 1;
8e15ce
diff --git a/util/grub-set-password.in b/util/grub-set-password.in
8e15ce
index 5ebf50576d6..c0b5ebbfdc5 100644
8e15ce
--- a/util/grub-set-password.in
8e15ce
+++ b/util/grub-set-password.in
8e15ce
@@ -1,6 +1,6 @@
8e15ce
 #!/bin/sh -e
8e15ce
 
8e15ce
-EFIDIR=$(grep ^ID= /etc/os-release | sed -e 's/^ID=//' -e 's/rhel/redhat/')
8e15ce
+EFIDIR=$(grep ^ID= /etc/os-release | sed -e 's/^ID=//' -e 's/rhel/redhat/' -e 's/\"//g')
8e15ce
 if [ -d /sys/firmware/efi/efivars/ ]; then
8e15ce
     grubdir=`echo "/@bootdirname@/efi/EFI/${EFIDIR}/" | sed 's,//*,/,g'`
8e15ce
 else
8e15ce
diff --git a/util/grub-switch-to-blscfg.8 b/util/grub-switch-to-blscfg.8
8e15ce
new file mode 100644
8e15ce
index 00000000000..9a886282976
8e15ce
--- /dev/null
8e15ce
+++ b/util/grub-switch-to-blscfg.8
8e15ce
@@ -0,0 +1,33 @@
8e15ce
+.TH GRUB-SWITCH-TO-BLSCFG 1 "Wed Feb 26 2014"
8e15ce
+.SH NAME
8e15ce
+\fBgrub-switch-to-blscfg\fR \(em Switch to using BLS config files.
8e15ce
+
8e15ce
+.SH SYNOPSIS
8e15ce
+\fBgrub-switch-to-blscfg\fR [--grub-directory=\fIDIR\fR] [--config-file=\fIFILE\fR] [--grub-defaults=\fIFILE\fR]
8e15ce
+
8e15ce
+.SH DESCRIPTION
8e15ce
+\fBgrub-switch-to-blscfg\fR reconfigures grub-mkconfig to use BLS-style config files, and then regenerates the GRUB configuration.
8e15ce
+
8e15ce
+.SH OPTIONS
8e15ce
+.TP
8e15ce
+--grub-directory=\fIDIR\fR
8e15ce
+Search for grub.cfg under \fIDIR\fR.  The default value is \fI/boot/efi/EFI/\fBVENDOR\fR on UEFI machines and \fI/boot/grub2\fR elsewhere.
8e15ce
+
8e15ce
+.TP
8e15ce
+--config-file=\fIFILE\fR
8e15ce
+The grub config file to use.  The default value is \fI/etc/grub2-efi.cfg\fR on UEFI machines and \fI/etc/grub2.cfg\fR elsewhere.  Symbolic links will be followed.
8e15ce
+
8e15ce
+.TP
8e15ce
+--grub-defaults=\fIFILE\fR
8e15ce
+The defaults file for grub-mkconfig.  The default value is \fI/etc/default/grub\fR.
8e15ce
+
8e15ce
+.TP
8e15ce
+--bls-directory=\fIDIR\fR
8e15ce
+Create BootLoaderSpec fragments in \fIDIR\fR.  The default value is \fI/boot/loader/entries\fR.
8e15ce
+
8e15ce
+.TP
8e15ce
+--backup-suffix=\fSUFFIX\fR
8e15ce
+The suffix to use for saved backup files.  The default value is \fI.bak\fR.
8e15ce
+
8e15ce
+.SH SEE ALSO
8e15ce
+.BR "info grub"
8e15ce
diff --git a/util/grub-switch-to-blscfg.in b/util/grub-switch-to-blscfg.in
8e15ce
new file mode 100644
8e15ce
index 00000000000..a851424beb2
8e15ce
--- /dev/null
8e15ce
+++ b/util/grub-switch-to-blscfg.in
8e15ce
@@ -0,0 +1,317 @@
8e15ce
+#! /bin/sh
8e15ce
+#
8e15ce
+# Set a default boot entry for GRUB.
8e15ce
+# Copyright (C) 2004,2009  Free Software Foundation, Inc.
8e15ce
+#
8e15ce
+# GRUB is free software: you can redistribute it and/or modify
8e15ce
+# it under the terms of the GNU General Public License as published by
8e15ce
+# the Free Software Foundation, either version 3 of the License, or
8e15ce
+# (at your option) any later version.
8e15ce
+#
8e15ce
+# GRUB is distributed in the hope that it will be useful,
8e15ce
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
8e15ce
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8e15ce
+# GNU General Public License for more details.
8e15ce
+#
8e15ce
+# You should have received a copy of the GNU General Public License
8e15ce
+# along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
8e15ce
+
8e15ce
+#set -eu
8e15ce
+
8e15ce
+# Initialize some variables.
8e15ce
+prefix=@prefix@
8e15ce
+exec_prefix=@exec_prefix@
8e15ce
+sbindir=@sbindir@
8e15ce
+bindir=@bindir@
8e15ce
+sysconfdir="@sysconfdir@"
8e15ce
+PACKAGE_NAME=@PACKAGE_NAME@
8e15ce
+PACKAGE_VERSION=@PACKAGE_VERSION@
8e15ce
+datarootdir="@datarootdir@"
8e15ce
+datadir="@datadir@"
8e15ce
+if [ ! -v pkgdatadir ]; then
8e15ce
+    pkgdatadir="${datadir}/@PACKAGE@"
8e15ce
+fi
8e15ce
+
8e15ce
+self=`basename $0`
8e15ce
+
8e15ce
+grub_get_kernel_settings="${sbindir}/@grub_get_kernel_settings@"
8e15ce
+grub_editenv=${bindir}/@grub_editenv@
8e15ce
+etcdefaultgrub=/etc/default/grub
8e15ce
+
8e15ce
+eval "$("${grub_get_kernel_settings}")" || true
8e15ce
+
8e15ce
+EFIDIR=$(grep ^ID= /etc/os-release | sed -e 's/^ID=//' -e 's/rhel/redhat/' -e 's/\"//g')
8e15ce
+if [ -d /sys/firmware/efi/efivars/ ]; then
8e15ce
+    startlink=/etc/grub2-efi.cfg
8e15ce
+    grubdir=`echo "/@bootdirname@/efi/EFI/${EFIDIR}/" | sed 's,//*,/,g'`
8e15ce
+else
8e15ce
+    startlink=/etc/grub2.cfg
8e15ce
+    grubdir=`echo "/@bootdirname@/@grubdirname@" | sed 's,//*,/,g'`
8e15ce
+fi
8e15ce
+
8e15ce
+blsdir=`echo "/@bootdirname@/loader/entries" | sed 's,//*,/,g'`
8e15ce
+
8e15ce
+backupsuffix=.bak
8e15ce
+
8e15ce
+arch="$(uname -m)"
8e15ce
+
8e15ce
+export TEXTDOMAIN=@PACKAGE@
8e15ce
+export TEXTDOMAINDIR="@localedir@"
8e15ce
+
8e15ce
+. "${pkgdatadir}/grub-mkconfig_lib"
8e15ce
+
8e15ce
+# Usage: usage
8e15ce
+# Print the usage.
8e15ce
+usage () {
8e15ce
+    gettext_printf "Usage: %s\n" "$self"
8e15ce
+    gettext "Switch to BLS config files.\n"; echo
8e15ce
+    echo
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
+    print_option_help "--backup-suffix=$(gettext "SUFFIX")" "$backupsuffix"
8e15ce
+    print_option_help "--bls-directory=$(gettext "DIR")" "$blsdir"
8e15ce
+    print_option_help "--config-file=$(gettext "FILE")" "$startlink"
8e15ce
+    print_option_help "--grub-defaults=$(gettext "FILE")" "$etcdefaultgrub"
8e15ce
+    print_option_help "--grub-directory=$(gettext "DIR")" "$grubdir"
8e15ce
+    # echo
8e15ce
+    # gettext "Report bugs to <bug-grub@gnu.org>."; echo
8e15ce
+}
8e15ce
+
8e15ce
+argument () {
8e15ce
+    opt=$1
8e15ce
+    shift
8e15ce
+
8e15ce
+    if test $# -eq 0; then
8e15ce
+        gettext_printf "%s: option requires an argument -- \`%s'\n" "$self" "$opt" 1>&2
8e15ce
+        exit 1
8e15ce
+    fi
8e15ce
+    echo $1
8e15ce
+}
8e15ce
+
8e15ce
+# Check the arguments.
8e15ce
+while test $# -gt 0
8e15ce
+do
8e15ce
+    option=$1
8e15ce
+    shift
8e15ce
+
8e15ce
+    case "$option" in
8e15ce
+    -h | --help)
8e15ce
+        usage
8e15ce
+        exit 0 ;;
8e15ce
+    -V | --version)
8e15ce
+        echo "$self (${PACKAGE_NAME}) ${PACKAGE_VERSION}"
8e15ce
+        exit 0 ;;
8e15ce
+
8e15ce
+    --backup-suffix)
8e15ce
+        backupsuffix=`argument $option "$@"`
8e15ce
+        shift
8e15ce
+        ;;
8e15ce
+    --backup-suffix=*)
8e15ce
+        backupsuffix=`echo "$option" | sed 's/--backup-suffix=//'`
8e15ce
+        ;;
8e15ce
+
8e15ce
+    --bls-directory)
8e15ce
+        blsdir=`argument $option "$@"`
8e15ce
+        shift
8e15ce
+        ;;
8e15ce
+    --bls-directory=*)
8e15ce
+        blsdir=`echo "$option" | sed 's/--bls-directory=//'`
8e15ce
+        ;;
8e15ce
+
8e15ce
+    --config-file)
8e15ce
+        startlink=`argument $option "$@"`
8e15ce
+        shift
8e15ce
+        ;;
8e15ce
+    --config-file=*)
8e15ce
+        startlink=`echo "$option" | sed 's/--config-file=//'`
8e15ce
+        ;;
8e15ce
+
8e15ce
+    --grub-defaults)
8e15ce
+        etcdefaultgrub=`argument $option "$@"`
8e15ce
+        shift
8e15ce
+        ;;
8e15ce
+    --grub-defaults=*)
8e15ce
+        etcdefaultgrub=`echo "$option" | sed 's/--grub-defaults=//'`
8e15ce
+        ;;
8e15ce
+
8e15ce
+    --grub-directory)
8e15ce
+        grubdir=`argument $option "$@"`
8e15ce
+        shift
8e15ce
+        ;;
8e15ce
+    --grub-directory=*)
8e15ce
+        grubdir=`echo "$option" | sed 's/--grub-directory=//'`
8e15ce
+        ;;
8e15ce
+
8e15ce
+    *)
8e15ce
+        gettext_printf "Unrecognized option \`%s'\n" "$option" 1>&2
8e15ce
+        usage
8e15ce
+        exit 1
8e15ce
+        ;;
8e15ce
+    esac
8e15ce
+done
8e15ce
+
8e15ce
+find_grub_cfg() {
8e15ce
+    local candidate=""
8e15ce
+    while [ -e "${candidate}" -o $# -gt 0 ]
8e15ce
+    do
8e15ce
+        if [ ! -e "${candidate}" ] ; then
8e15ce
+            candidate="$1"
8e15ce
+            shift
8e15ce
+        fi
8e15ce
+
8e15ce
+        if [ -L "${candidate}" ]; then
8e15ce
+            candidate="$(realpath "${candidate}")"
8e15ce
+        fi
8e15ce
+
8e15ce
+        if [ -f "${candidate}" ]; then
8e15ce
+            export GRUB_CONFIG_FILE="${candidate}"
8e15ce
+            return 0
8e15ce
+        fi
8e15ce
+    done
8e15ce
+    return 1
8e15ce
+}
8e15ce
+
8e15ce
+if ! find_grub_cfg ${startlink} ${grubdir}/grub.cfg ; then
8e15ce
+  gettext_printf "Couldn't find config file\n" 1>&2
8e15ce
+  exit 1
8e15ce
+fi
8e15ce
+
8e15ce
+if [ ! -d "${blsdir}" ]; then
8e15ce
+    install -m 700 -d "${blsdir}"
8e15ce
+fi
8e15ce
+
8e15ce
+if [ -f /etc/machine-id ]; then
8e15ce
+    MACHINE_ID=$(cat /etc/machine-id)
8e15ce
+else
8e15ce
+    MACHINE_ID=$(dmesg | sha256sum)
8e15ce
+fi
8e15ce
+
8e15ce
+mkbls() {
8e15ce
+    local kernelver=$1 && shift
8e15ce
+    local datetime=$1 && shift
8e15ce
+    local kernelopts=$1 && shift
8e15ce
+
8e15ce
+    local debugname=""
8e15ce
+    local debugid=""
8e15ce
+    local flavor=""
8e15ce
+
8e15ce
+    if [ "$kernelver" == *\+* ] ; then
8e15ce
+        local flavor=-"${kernelver##*+}"
8e15ce
+        if [ "${flavor}" == "-debug" ]; then
8e15ce
+            local debugname=" with debugging"
8e15ce
+            local debugid="-debug"
8e15ce
+        fi
8e15ce
+    fi
8e15ce
+    (
8e15ce
+        source /etc/os-release
8e15ce
+
8e15ce
+        cat <
8e15ce
+title ${NAME} (${kernelver}) ${VERSION}${debugname}
8e15ce
+version ${kernelver}${debugid}
8e15ce
+linux /vmlinuz-${kernelver}
8e15ce
+initrd /initramfs-${kernelver}.img
8e15ce
+options ${kernelopts}
8e15ce
+grub_users \$grub_users
8e15ce
+grub_arg --unrestricted
8e15ce
+grub_class kernel${flavor}
8e15ce
+EOF
8e15ce
+    ) | cat
8e15ce
+}
8e15ce
+
8e15ce
+copy_bls() {
8e15ce
+    for kernelver in $(cd /lib/modules/ ; ls -1) "" ; do
8e15ce
+	bls_target="${blsdir}/${MACHINE_ID}-${kernelver}.conf"
8e15ce
+	linux="/vmlinuz-${kernelver}"
8e15ce
+	linux_path="/boot${linux}"
8e15ce
+	kernel_dir="/lib/modules/${kernelver}"
8e15ce
+
8e15ce
+	if [ ! -d "${kernel_dir}" ] ; then
8e15ce
+            continue
8e15ce
+	fi
8e15ce
+	if [ ! -f "${linux_path}" ]; then
8e15ce
+            continue
8e15ce
+	fi
8e15ce
+
8e15ce
+	linux_relpath="$("${grub_mkrelpath}" "${linux_path}")"
8e15ce
+	bootprefix="${linux_relpath%%"${linux}"}"
8e15ce
+	cmdline="root=${LINUX_ROOT_DEVICE} ro ${GRUB_CMDLINE_LINUX} ${GRUB_CMDLINE_LINUX_DEFAULT}"
8e15ce
+
8e15ce
+	mkbls "${kernelver}" \
8e15ce
+	      "$(date -u +%Y%m%d%H%M%S -d "$(stat -c '%y' "${kernel_dir}")")" \
8e15ce
+	      "${bootprefix}" "${cmdline}" >"${bls_target}"
8e15ce
+
8e15ce
+	if [ "x$GRUB_LINUX_MAKE_DEBUG" = "xtrue" ]; then
8e15ce
+            bls_debug="$(echo ${bls_target} | sed -e "s/${kernelver}/${kernelver}~debug/")"
8e15ce
+            cp -aT  "${bls_target}" "${bls_debug}"
8e15ce
+            title="$(grep '^title[ \t]' "${bls_debug}" | sed -e 's/^title[ \t]*//')"
8e15ce
+            options="$(echo "${cmdline} ${GRUB_CMDLINE_LINUX_DEBUG}" | sed -e 's/\//\\\//g')"
8e15ce
+            sed -i -e "s/^title.*/title ${title}${GRUB_LINUX_DEBUG_TITLE_POSTFIX}/" "${bls_debug}"
8e15ce
+            sed -i -e "s/^options.*/options ${options}/" "${bls_debug}"
8e15ce
+	fi
8e15ce
+    done
8e15ce
+
8e15ce
+    if [ -f "/boot/vmlinuz-0-rescue-${MACHINE_ID}" ]; then
8e15ce
+	mkbls "0-rescue-${MACHINE_ID}" "0" "${bootprefix}" >"${blsdir}/${MACHINE_ID}-0-rescue.conf"
8e15ce
+    fi
8e15ce
+}
8e15ce
+
8e15ce
+# The grub2 EFI binary is not copied to the ESP as a part of an ostree
8e15ce
+# transaction. Make sure a grub2 version with BLS support is installed
8e15ce
+# but only do this if the blsdir is not set, to make sure that the BLS
8e15ce
+# parsing module will search for the BLS snippets in the default path.
8e15ce
+if test -f /run/ostree-booted && test -d /sys/firmware/efi/efivars && \
8e15ce
+   ! ${grub_editenv} - list | grep -q blsdir && \
8e15ce
+   mountpoint -q /boot; then
8e15ce
+    grub_binary="$(find /usr/lib/ostree-boot/efi/EFI/${EFIDIR}/ -name grub*.efi)"
8e15ce
+    install -m 700 ${grub_binary} ${grubdir} || exit 1
8e15ce
+    # Create a hidden file to indicate that grub2 now has BLS support.
8e15ce
+    touch /boot/grub2/.grub2-blscfg-supported
8e15ce
+fi
8e15ce
+
8e15ce
+GENERATE=0
8e15ce
+if grep '^GRUB_ENABLE_BLSCFG=.*' "${etcdefaultgrub}" \
8e15ce
+        | grep -vq '^GRUB_ENABLE_BLSCFG="*true"*\s*$' ; then
8e15ce
+    if ! sed -i"${backupsuffix}" \
8e15ce
+            -e 's,^GRUB_ENABLE_BLSCFG=.*,GRUB_ENABLE_BLSCFG=true,' \
8e15ce
+            "${etcdefaultgrub}" ; then
8e15ce
+        gettext_printf "Updating %s failed\n" "${etcdefaultgrub}"
8e15ce
+        exit 1
8e15ce
+    fi
8e15ce
+    GENERATE=1
8e15ce
+elif ! grep -q '^GRUB_ENABLE_BLSCFG=.*' "${etcdefaultgrub}" ; then
8e15ce
+    if ! echo 'GRUB_ENABLE_BLSCFG=true' >> "${etcdefaultgrub}" ; then
8e15ce
+        gettext_printf "Updating %s failed\n" "${etcdefaultgrub}"
8e15ce
+        exit 1
8e15ce
+    fi
8e15ce
+    GENERATE=1
8e15ce
+fi
8e15ce
+
8e15ce
+if [ "${GENERATE}" -eq 1 ] ; then
8e15ce
+    copy_bls
8e15ce
+
8e15ce
+    if [ $arch = "x86_64" ] && [ ! -d /sys/firmware/efi ]; then
8e15ce
+	mod_dir="i386-pc"
8e15ce
+    elif [ $arch = "ppc64" -o $arch = "ppc64le" ] && [ ! -d /sys/firmware/opal ]; then
8e15ce
+	mod_dir="powerpc-ieee1275"
8e15ce
+    fi
8e15ce
+
8e15ce
+    if [ -n "${mod_dir}" ]; then
8e15ce
+	for mod in blscfg increment; do
8e15ce
+	    install -m 700 ${prefix}/lib/grub/${mod_dir}/${mod}.mod ${grubdir}/$mod_dir/ || exit 1
8e15ce
+	done
8e15ce
+    fi
8e15ce
+
8e15ce
+    cp -af "${GRUB_CONFIG_FILE}" "${GRUB_CONFIG_FILE}${backupsuffix}"
8e15ce
+    if ! grub2-mkconfig -o "${GRUB_CONFIG_FILE}" ; then
8e15ce
+        install -m 700 "${GRUB_CONFIG_FILE}${backupsuffix}" "${GRUB_CONFIG_FILE}"
8e15ce
+        sed -i"${backupsuffix}" \
8e15ce
+            -e 's,^GRUB_ENABLE_BLSCFG=.*,GRUB_ENABLE_BLSCFG=false,' \
8e15ce
+            "${etcdefaultgrub}"
8e15ce
+        gettext_printf "Updating %s failed\n" "${GRUB_CONFIG_FILE}"
8e15ce
+        exit 1
8e15ce
+    fi
8e15ce
+fi
8e15ce
+
8e15ce
+# Bye.
8e15ce
+exit 0