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

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