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

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