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>
15a2072
---
15a2072
 Makefile.util.def             |   7 ++
15a2072
 .gitignore                    |   2 +
15a2072
 util/grub-switch-to-blscfg.8  |  25 ++++
15a2072
 util/grub-switch-to-blscfg.in | 262 ++++++++++++++++++++++++++++++++++++++++++
15a2072
 4 files changed, 296 insertions(+)
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
15a2072
index 879e8eb98a4..f4fbd250630 100644
15a2072
--- a/Makefile.util.def
15a2072
+++ b/Makefile.util.def
15a2072
@@ -1348,6 +1348,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;
15a2072
diff --git a/.gitignore b/.gitignore
3e07ee7
index 54795fa60be..42475592123 100644
15a2072
--- a/.gitignore
15a2072
+++ b/.gitignore
3e07ee7
@@ -121,6 +121,8 @@ grub-*.tar.*
15a2072
 /grub*-sparc64-setup.8
15a2072
 /grub*-syslinux2cfg
15a2072
 /grub*-syslinux2cfg.1
15a2072
+/grub*-switch-to-blscfg
15a2072
+/grub*-switch-to-blscfg.8
15a2072
 /grub_fstest.pp
15a2072
 /grub_fstest_init.c
15a2072
 /grub_fstest_init.lst
15a2072
diff --git a/util/grub-switch-to-blscfg.8 b/util/grub-switch-to-blscfg.8
15a2072
new file mode 100644
15a2072
index 00000000000..134dfc62a7b
15a2072
--- /dev/null
15a2072
+++ b/util/grub-switch-to-blscfg.8
15a2072
@@ -0,0 +1,25 @@
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
+
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
15a2072
index 00000000000..3ae5e4ea8d0
15a2072
--- /dev/null
15a2072
+++ b/util/grub-switch-to-blscfg.in
15a2072
@@ -0,0 +1,262 @@
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@
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
+
15a2072
+grub_editenv=${bindir}/@grub_editenv@
15a2072
+etcdefaultgrub=/etc/default/grub
15a2072
+
15a2072
+EFIDIR=$(grep ^ID= /etc/os-release | sed -e 's/^ID=//' -e 's/rhel/redhat/')
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
+    blsdir=`echo "/@bootdirname@/efi/EFI/${EFIDIR}/loader/entries" | sed 's,//*,/,g'`
15a2072
+else
15a2072
+    startlink=/etc/grub2.cfg
15a2072
+    grubdir=`echo "/@bootdirname@/@grubdirname@" | sed 's,//*,/,g'`
15a2072
+    blsdir=`echo "/@bootdirname@" | sed 's,//*,/,g'`
15a2072
+fi
15a2072
+
15a2072
+backupsuffix=.bak
15a2072
+
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=""
15a2072
+    while [[ -e "${candidate}" || $# -gt 0 ]]
15a2072
+    do
15a2072
+        if [[ ! -e "${candidate}" ]] ; then
15a2072
+            candidate="$1"
15a2072
+            shift
15a2072
+        fi
15a2072
+
15a2072
+        if [[ -L "${candidate}" ]]; then
15a2072
+            candidate="$(realpath "${candidate}")"
15a2072
+        fi
15a2072
+
15a2072
+        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
+
15a2072
+if [[ ! -d "${blsdir}" ]]; then
15a2072
+    install -m 700 -d "${blsdir}"
15a2072
+fi
15a2072
+
15a2072
+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
15a2072
+
15a2072
+    local debugname=""
15a2072
+    local flavor=""
15a2072
+
15a2072
+    if [[ "$kernelver" == *\+* ]] ; then
15a2072
+        local flavor=-"${kernelver##*+}"
15a2072
+        if [[ "${flavor}" == "-debug" ]]; then
15a2072
+            local debugname=" with debugging"
15a2072
+        fi
15a2072
+    fi
15a2072
+    (
15a2072
+        source /etc/os-release
15a2072
+
15a2072
+        cat <
15a2072
+title ${NAME} (${kernelver}) ${VERSION}${debugname}
15a2072
+linux /vmlinuz-${kernelver}
15a2072
+initrd /initramfs-${kernelver}.img
15a2072
+options \$kernelopts
15a2072
+id ${ID}-${datetime}-${kernelver}
15a2072
+grub_users \$grub_users
15a2072
+grub_arg --unrestricted
15a2072
+grub_class kernel${flavor}
15a2072
+EOF
15a2072
+    ) | cat
15a2072
+}
15a2072
+
15a2072
+for kernelver in $(cd /lib/modules/ ; ls -1) "" ; do
15a2072
+    if [[ ! -d "/lib/modules/${kernelver}" ]] ; then
15a2072
+        continue
15a2072
+    fi
15a2072
+    if [[ ! -f "/boot/vmlinuz-${kernelver}" ]]; then
15a2072
+        continue
15a2072
+    fi
15a2072
+    bls_target="${blsdir}/${MACHINE_ID}-${kernelver}.conf"
15a2072
+    kernel_dir="/lib/modules/${kernelver}"
15a2072
+    if [[ -f "${kernel_dir}/bls.conf" ]]; then
15a2072
+        cp -af "${kernel_dir}/bls.conf" "${bls_target}"
15a2072
+    else
15a2072
+        mkbls "${kernelver}" \
15a2072
+            "$(date -u +%Y%m%d%H%M%S -d "$(stat -c '%y' "${kernel_dir}")")" \
15a2072
+            >"${bls_target}"
15a2072
+    fi
15a2072
+done
15a2072
+
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
+
15a2072
+if [[ "${GENERATE}" -eq 1 ]] ; then
15a2072
+    cp -af "${GRUB_CONFIG_FILE}" "${GRUB_CONFIG_FILE}${backupsuffix}"
15a2072
+    if ! grub2-mkconfig -o "${GRUB_CONFIG_FILE}" ; then
15a2072
+        cp -af "${GRUB_CONFIG_FILE}${backupsuffix}" "${GRUB_CONFIG_FILE}"
15a2072
+        sed -i"${backupsuffix}" \
15a2072
+            -e 's,^GRUB_ENABLE_BLSCFG=.*,GRUB_ENABLE_BLSCFG=false,' \
15a2072
+            /etc/default/grub
15a2072
+        gettext_printf "Updating %s failed\n" "${GRUB_CONFIG_FILE}"
15a2072
+        exit 1
15a2072
+    fi
15a2072
+fi
15a2072
+
15a2072
+# Bye.
15a2072
+exit 0