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