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