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