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

d9d99f
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
d9d99f
From: Peter Jones <pjones@redhat.com>
d9d99f
Date: Thu, 15 Mar 2018 14:12:40 -0400
d9d99f
Subject: [PATCH] Add grub2-switch-to-blscfg
d9d99f
d9d99f
Signed-off-by: Peter Jones <pjones@redhat.com>
d9d99f
---
d9d99f
 Makefile.util.def             |   7 ++
d9d99f
 .gitignore                    |   2 +
d9d99f
 util/grub-switch-to-blscfg.8  |  25 ++++
d9d99f
 util/grub-switch-to-blscfg.in | 262 ++++++++++++++++++++++++++++++++++++++++++
d9d99f
 4 files changed, 296 insertions(+)
d9d99f
 create mode 100644 util/grub-switch-to-blscfg.8
d9d99f
 create mode 100644 util/grub-switch-to-blscfg.in
d9d99f
d9d99f
diff --git a/Makefile.util.def b/Makefile.util.def
d9d99f
index 879e8eb98a4..f4fbd250630 100644
d9d99f
--- a/Makefile.util.def
d9d99f
+++ b/Makefile.util.def
d9d99f
@@ -1348,6 +1348,13 @@ program = {
d9d99f
   ldadd = '$(LIBINTL) $(LIBDEVMAPPER) $(LIBZFS) $(LIBNVPAIR) $(LIBGEOM)';
d9d99f
 };
d9d99f
 
d9d99f
+script = {
d9d99f
+  name = grub-switch-to-blscfg;
d9d99f
+  common = util/grub-switch-to-blscfg.in;
d9d99f
+  mansection = 8;
d9d99f
+  installdir = sbin;
d9d99f
+};
d9d99f
+
d9d99f
 program = {
d9d99f
   name = grub-glue-efi;
d9d99f
   mansection = 1;
d9d99f
diff --git a/.gitignore b/.gitignore
d9d99f
index 54795fa60be..42475592123 100644
d9d99f
--- a/.gitignore
d9d99f
+++ b/.gitignore
d9d99f
@@ -121,6 +121,8 @@ grub-*.tar.*
d9d99f
 /grub*-sparc64-setup.8
d9d99f
 /grub*-syslinux2cfg
d9d99f
 /grub*-syslinux2cfg.1
d9d99f
+/grub*-switch-to-blscfg
d9d99f
+/grub*-switch-to-blscfg.8
d9d99f
 /grub_fstest.pp
d9d99f
 /grub_fstest_init.c
d9d99f
 /grub_fstest_init.lst
d9d99f
diff --git a/util/grub-switch-to-blscfg.8 b/util/grub-switch-to-blscfg.8
d9d99f
new file mode 100644
d9d99f
index 00000000000..134dfc62a7b
d9d99f
--- /dev/null
d9d99f
+++ b/util/grub-switch-to-blscfg.8
d9d99f
@@ -0,0 +1,25 @@
d9d99f
+.TH GRUB-SWITCH-TO-BLSCFG 1 "Wed Feb 26 2014"
d9d99f
+.SH NAME
d9d99f
+\fBgrub-switch-to-blscfg\fR \(em Switch to using BLS config files.
d9d99f
+
d9d99f
+.SH SYNOPSIS
d9d99f
+\fBgrub-switch-to-blscfg\fR [--grub-directory=\fIDIR\fR] [--config-file=\fIFILE\fR] [--grub-defaults=\fIFILE\fR]
d9d99f
+
d9d99f
+.SH DESCRIPTION
d9d99f
+\fBgrub-switch-to-blscfg\fR reconfigures grub-mkconfig to use BLS-style config files, and then regenerates the GRUB configuration.
d9d99f
+
d9d99f
+.SH OPTIONS
d9d99f
+.TP
d9d99f
+--grub-directory=\fIDIR\fR
d9d99f
+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.
d9d99f
+
d9d99f
+.TP
d9d99f
+--config-file=\fIFILE\fR
d9d99f
+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.
d9d99f
+
d9d99f
+.TP
d9d99f
+--grub-defaults=\fIFILE\fR
d9d99f
+The defaults file for grub-mkconfig.  The default value is \fI/etc/default/grub\fR.
d9d99f
+
d9d99f
+.SH SEE ALSO
d9d99f
+.BR "info grub"
d9d99f
diff --git a/util/grub-switch-to-blscfg.in b/util/grub-switch-to-blscfg.in
d9d99f
new file mode 100644
d9d99f
index 00000000000..3ae5e4ea8d0
d9d99f
--- /dev/null
d9d99f
+++ b/util/grub-switch-to-blscfg.in
d9d99f
@@ -0,0 +1,262 @@
d9d99f
+#! /bin/sh
d9d99f
+#
d9d99f
+# Set a default boot entry for GRUB.
d9d99f
+# Copyright (C) 2004,2009  Free Software Foundation, Inc.
d9d99f
+#
d9d99f
+# GRUB is free software: you can redistribute it and/or modify
d9d99f
+# it under the terms of the GNU General Public License as published by
d9d99f
+# the Free Software Foundation, either version 3 of the License, or
d9d99f
+# (at your option) any later version.
d9d99f
+#
d9d99f
+# GRUB is distributed in the hope that it will be useful,
d9d99f
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
d9d99f
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
d9d99f
+# GNU General Public License for more details.
d9d99f
+#
d9d99f
+# You should have received a copy of the GNU General Public License
d9d99f
+# along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
d9d99f
+
d9d99f
+#set -eu
d9d99f
+
d9d99f
+# Initialize some variables.
d9d99f
+prefix=@prefix@
d9d99f
+exec_prefix=@exec_prefix@
d9d99f
+bindir=@bindir@
d9d99f
+sysconfdir="@sysconfdir@"
d9d99f
+PACKAGE_NAME=@PACKAGE_NAME@
d9d99f
+PACKAGE_VERSION=@PACKAGE_VERSION@
d9d99f
+datarootdir="@datarootdir@"
d9d99f
+datadir="@datadir@"
d9d99f
+if [ ! -v pkgdatadir ]; then
d9d99f
+    pkgdatadir="${datadir}/@PACKAGE@"
d9d99f
+fi
d9d99f
+
d9d99f
+self=`basename $0`
d9d99f
+
d9d99f
+grub_editenv=${bindir}/@grub_editenv@
d9d99f
+etcdefaultgrub=/etc/default/grub
d9d99f
+
d9d99f
+EFIDIR=$(grep ^ID= /etc/os-release | sed -e 's/^ID=//' -e 's/rhel/redhat/')
d9d99f
+if [ -d /sys/firmware/efi/efivars/ ]; then
d9d99f
+    startlink=/etc/grub2-efi.cfg
d9d99f
+    grubdir=`echo "/@bootdirname@/efi/EFI/${EFIDIR}/" | sed 's,//*,/,g'`
d9d99f
+    blsdir=`echo "/@bootdirname@/efi/EFI/${EFIDIR}/loader/entries" | sed 's,//*,/,g'`
d9d99f
+else
d9d99f
+    startlink=/etc/grub2.cfg
d9d99f
+    grubdir=`echo "/@bootdirname@/@grubdirname@" | sed 's,//*,/,g'`
d9d99f
+    blsdir=`echo "/@bootdirname@" | sed 's,//*,/,g'`
d9d99f
+fi
d9d99f
+
d9d99f
+backupsuffix=.bak
d9d99f
+
d9d99f
+export TEXTDOMAIN=@PACKAGE@
d9d99f
+export TEXTDOMAINDIR="@localedir@"
d9d99f
+
d9d99f
+. "${pkgdatadir}/grub-mkconfig_lib"
d9d99f
+
d9d99f
+# Usage: usage
d9d99f
+# Print the usage.
d9d99f
+usage () {
d9d99f
+    gettext_printf "Usage: %s\n" "$self"
d9d99f
+    gettext "Switch to BLS config files.\n"; echo
d9d99f
+    echo
d9d99f
+    print_option_help "-h, --help" "$(gettext "print this message and exit")"
d9d99f
+    print_option_help "-V, --version" "$(gettext "print the version information and exit")"
d9d99f
+    echo
d9d99f
+    print_option_help "--backup-suffix=$(gettext "SUFFIX")" "$backupsuffix"
d9d99f
+    print_option_help "--bls-directory=$(gettext "DIR")" "$blsdir"
d9d99f
+    print_option_help "--config-file=$(gettext "FILE")" "$startlink"
d9d99f
+    print_option_help "--grub-defaults=$(gettext "FILE")" "$etcdefaultgrub"
d9d99f
+    print_option_help "--grub-directory=$(gettext "DIR")" "$grubdir"
d9d99f
+    # echo
d9d99f
+    # gettext "Report bugs to <bug-grub@gnu.org>."; echo
d9d99f
+}
d9d99f
+
d9d99f
+argument () {
d9d99f
+    opt=$1
d9d99f
+    shift
d9d99f
+
d9d99f
+    if test $# -eq 0; then
d9d99f
+        gettext_printf "%s: option requires an argument -- \`%s'\n" "$self" "$opt" 1>&2
d9d99f
+        exit 1
d9d99f
+    fi
d9d99f
+    echo $1
d9d99f
+}
d9d99f
+
d9d99f
+# Check the arguments.
d9d99f
+while test $# -gt 0
d9d99f
+do
d9d99f
+    option=$1
d9d99f
+    shift
d9d99f
+
d9d99f
+    case "$option" in
d9d99f
+    -h | --help)
d9d99f
+        usage
d9d99f
+        exit 0 ;;
d9d99f
+    -V | --version)
d9d99f
+        echo "$self (${PACKAGE_NAME}) ${PACKAGE_VERSION}"
d9d99f
+        exit 0 ;;
d9d99f
+
d9d99f
+    --backup-suffix)
d9d99f
+        backupsuffix=`argument $option "$@"`
d9d99f
+        shift
d9d99f
+        ;;
d9d99f
+    --backup-suffix=*)
d9d99f
+        backupsuffix=`echo "$option" | sed 's/--backup-suffix=//'`
d9d99f
+        ;;
d9d99f
+
d9d99f
+    --bls-directory)
d9d99f
+        blsdir=`argument $option "$@"`
d9d99f
+        shift
d9d99f
+        ;;
d9d99f
+    --bls-directory=*)
d9d99f
+        blsdir=`echo "$option" | sed 's/--bls-directory=//'`
d9d99f
+        ;;
d9d99f
+
d9d99f
+    --config-file)
d9d99f
+        startlink=`argument $option "$@"`
d9d99f
+        shift
d9d99f
+        ;;
d9d99f
+    --config-file=*)
d9d99f
+        startlink=`echo "$option" | sed 's/--config-file=//'`
d9d99f
+        ;;
d9d99f
+
d9d99f
+    --grub-defaults)
d9d99f
+        etcdefaultgrub=`argument $option "$@"`
d9d99f
+        shift
d9d99f
+        ;;
d9d99f
+    --grub-defaults=*)
d9d99f
+        etcdefaultgrub=`echo "$option" | sed 's/--grub-defaults=//'`
d9d99f
+        ;;
d9d99f
+
d9d99f
+    --grub-directory)
d9d99f
+        grubdir=`argument $option "$@"`
d9d99f
+        shift
d9d99f
+        ;;
d9d99f
+    --grub-directory=*)
d9d99f
+        grubdir=`echo "$option" | sed 's/--grub-directory=//'`
d9d99f
+        ;;
d9d99f
+
d9d99f
+    *)
d9d99f
+        gettext_printf "Unrecognized option \`%s'\n" "$option" 1>&2
d9d99f
+        usage
d9d99f
+        exit 1
d9d99f
+        ;;
d9d99f
+    esac
d9d99f
+done
d9d99f
+
d9d99f
+find_grub_cfg() {
d9d99f
+    local candidate=""
d9d99f
+    while [[ -e "${candidate}" || $# -gt 0 ]]
d9d99f
+    do
d9d99f
+        if [[ ! -e "${candidate}" ]] ; then
d9d99f
+            candidate="$1"
d9d99f
+            shift
d9d99f
+        fi
d9d99f
+
d9d99f
+        if [[ -L "${candidate}" ]]; then
d9d99f
+            candidate="$(realpath "${candidate}")"
d9d99f
+        fi
d9d99f
+
d9d99f
+        if [[ -f "${candidate}" ]]; then
d9d99f
+            export GRUB_CONFIG_FILE="${candidate}"
d9d99f
+            return 0
d9d99f
+        fi
d9d99f
+    done
d9d99f
+    return 1
d9d99f
+}
d9d99f
+
d9d99f
+if ! find_grub_cfg ${startlink} ${grubdir}/grub.cfg ; then
d9d99f
+  gettext_printf "Couldn't find config file\n" 1>&2
d9d99f
+  exit 1
d9d99f
+fi
d9d99f
+
d9d99f
+if [[ ! -d "${blsdir}" ]]; then
d9d99f
+    install -m 700 -d "${blsdir}"
d9d99f
+fi
d9d99f
+
d9d99f
+if [[ -f /etc/machine-id ]]; then
d9d99f
+    MACHINE_ID=$(cat /etc/machine-id)
d9d99f
+else
d9d99f
+    MACHINE_ID=$(dmesg | sha256sum)
d9d99f
+fi
d9d99f
+
d9d99f
+mkbls() {
d9d99f
+    local kernelver=$1 && shift
d9d99f
+    local datetime=$1 && shift
d9d99f
+
d9d99f
+    local debugname=""
d9d99f
+    local flavor=""
d9d99f
+
d9d99f
+    if [[ "$kernelver" == *\+* ]] ; then
d9d99f
+        local flavor=-"${kernelver##*+}"
d9d99f
+        if [[ "${flavor}" == "-debug" ]]; then
d9d99f
+            local debugname=" with debugging"
d9d99f
+        fi
d9d99f
+    fi
d9d99f
+    (
d9d99f
+        source /etc/os-release
d9d99f
+
d9d99f
+        cat <
d9d99f
+title ${NAME} (${kernelver}) ${VERSION}${debugname}
d9d99f
+linux /vmlinuz-${kernelver}
d9d99f
+initrd /initramfs-${kernelver}.img
d9d99f
+options \$kernelopts
d9d99f
+id ${ID}-${datetime}-${kernelver}
d9d99f
+grub_users \$grub_users
d9d99f
+grub_arg --unrestricted
d9d99f
+grub_class kernel${flavor}
d9d99f
+EOF
d9d99f
+    ) | cat
d9d99f
+}
d9d99f
+
d9d99f
+for kernelver in $(cd /lib/modules/ ; ls -1) "" ; do
d9d99f
+    if [[ ! -d "/lib/modules/${kernelver}" ]] ; then
d9d99f
+        continue
d9d99f
+    fi
d9d99f
+    if [[ ! -f "/boot/vmlinuz-${kernelver}" ]]; then
d9d99f
+        continue
d9d99f
+    fi
d9d99f
+    bls_target="${blsdir}/${MACHINE_ID}-${kernelver}.conf"
d9d99f
+    kernel_dir="/lib/modules/${kernelver}"
d9d99f
+    if [[ -f "${kernel_dir}/bls.conf" ]]; then
d9d99f
+        cp -af "${kernel_dir}/bls.conf" "${bls_target}"
d9d99f
+    else
d9d99f
+        mkbls "${kernelver}" \
d9d99f
+            "$(date -u +%Y%m%d%H%M%S -d "$(stat -c '%y' "${kernel_dir}")")" \
d9d99f
+            >"${bls_target}"
d9d99f
+    fi
d9d99f
+done
d9d99f
+
d9d99f
+GENERATE=0
d9d99f
+if grep '^GRUB_ENABLE_BLSCFG=.*' "${etcdefaultgrub}" \
d9d99f
+        | grep -vq '^GRUB_ENABLE_BLSCFG="*true"*\s*$' ; then
d9d99f
+    if ! sed -i"${backupsuffix}" \
d9d99f
+            -e 's,^GRUB_ENABLE_BLSCFG=.*,GRUB_ENABLE_BLSCFG=true,' \
d9d99f
+            "${etcdefaultgrub}" ; then
d9d99f
+        gettext_printf "Updating %s failed\n" "${etcdefaultgrub}"
d9d99f
+        exit 1
d9d99f
+    fi
d9d99f
+    GENERATE=1
d9d99f
+elif ! grep -q '^GRUB_ENABLE_BLSCFG=.*' "${etcdefaultgrub}" ; then
d9d99f
+    if ! echo 'GRUB_ENABLE_BLSCFG=true' >> "${etcdefaultgrub}" ; then
d9d99f
+        gettext_printf "Updating %s failed\n" "${etcdefaultgrub}"
d9d99f
+        exit 1
d9d99f
+    fi
d9d99f
+    GENERATE=1
d9d99f
+fi
d9d99f
+
d9d99f
+if [[ "${GENERATE}" -eq 1 ]] ; then
d9d99f
+    cp -af "${GRUB_CONFIG_FILE}" "${GRUB_CONFIG_FILE}${backupsuffix}"
d9d99f
+    if ! grub2-mkconfig -o "${GRUB_CONFIG_FILE}" ; then
d9d99f
+        cp -af "${GRUB_CONFIG_FILE}${backupsuffix}" "${GRUB_CONFIG_FILE}"
d9d99f
+        sed -i"${backupsuffix}" \
d9d99f
+            -e 's,^GRUB_ENABLE_BLSCFG=.*,GRUB_ENABLE_BLSCFG=false,' \
d9d99f
+            /etc/default/grub
d9d99f
+        gettext_printf "Updating %s failed\n" "${GRUB_CONFIG_FILE}"
d9d99f
+        exit 1
d9d99f
+    fi
d9d99f
+fi
d9d99f
+
d9d99f
+# Bye.
d9d99f
+exit 0