From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Javier Martinez Canillas <javierm@redhat.com>
Date: Tue, 4 Dec 2018 10:48:45 +0100
Subject: [PATCH] BLS files should only be copied by grub-switch-to-blscfg if
 BLS isn't set

Currently the grub-switch-to-blscfg script doesn't update the grub.cfg if
GRUB_ENABLE_BLSCFG=true is already set in /etc/default/grub. But it still
copies the BLS files which may overwrite fields modified by the user.

Related: rhbz#1638117

Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
---
 util/grub-switch-to-blscfg.in | 80 +++++++++++++++++++++++--------------------
 1 file changed, 42 insertions(+), 38 deletions(-)

diff --git a/util/grub-switch-to-blscfg.in b/util/grub-switch-to-blscfg.in
index d353370cc51..eeea1307706 100644
--- a/util/grub-switch-to-blscfg.in
+++ b/util/grub-switch-to-blscfg.in
@@ -220,49 +220,51 @@ EOF
     ) | cat
 }
 
-for kernelver in $(cd /lib/modules/ ; ls -1) "" ; do
-    bls_target="${blsdir}/${MACHINE_ID}-${kernelver}.conf"
-    linux="/vmlinuz-${kernelver}"
-    linux_path="/boot${linux}"
-    kernel_dir="/lib/modules/${kernelver}"
+copy_bls() {
+    for kernelver in $(cd /lib/modules/ ; ls -1) "" ; do
+	bls_target="${blsdir}/${MACHINE_ID}-${kernelver}.conf"
+	linux="/vmlinuz-${kernelver}"
+	linux_path="/boot${linux}"
+	kernel_dir="/lib/modules/${kernelver}"
 
-    if [ ! -d "${kernel_dir}" ] ; then
-        continue
-    fi
-    if [ ! -f "${linux_path}" ]; then
-        continue
-    fi
+	if [ ! -d "${kernel_dir}" ] ; then
+            continue
+	fi
+	if [ ! -f "${linux_path}" ]; then
+            continue
+	fi
 
-    linux_relpath="$("${grub_mkrelpath}" "${linux_path}")"
-    bootprefix="${linux_relpath%%"${linux}"}"
+	linux_relpath="$("${grub_mkrelpath}" "${linux_path}")"
+	bootprefix="${linux_relpath%%"${linux}"}"
 
-    if [ -f "${kernel_dir}/bls.conf" ] ; then
-        cp -af "${kernel_dir}/bls.conf" "${bls_target}"
-        if [ -n "${bootprefix}" ]; then
-            sed -i -e "s,^\(linux[^ \t]*[ \t]\+\).*,\1${bootprefix}${linux},g" "${bls_target}"
-            sed -i -e "/^initrd/ s,\([ \t]\+\)\([^ \t]\+\),\1${bootprefix}\2,g" "${bls_target}"
-        fi
-    else
-        mkbls "${kernelver}" \
-            "$(date -u +%Y%m%d%H%M%S -d "$(stat -c '%y' "${kernel_dir}")")" \
-            "${bootprefix}" \
-            >"${bls_target}"
-    fi
+	if [ -f "${kernel_dir}/bls.conf" ] ; then
+            cp -af "${kernel_dir}/bls.conf" "${bls_target}"
+            if [ -n "${bootprefix}" ]; then
+		sed -i -e "s,^\(linux[^ \t]*[ \t]\+\).*,\1${bootprefix}${linux},g" "${bls_target}"
+		sed -i -e "/^initrd/ s,\([ \t]\+\)\([^ \t]\+\),\1${bootprefix}\2,g" "${bls_target}"
+            fi
+	else
+            mkbls "${kernelver}" \
+		  "$(date -u +%Y%m%d%H%M%S -d "$(stat -c '%y' "${kernel_dir}")")" \
+		  "${bootprefix}" \
+		  >"${bls_target}"
+	fi
 
-    if [ "x$GRUB_LINUX_MAKE_DEBUG" = "xtrue" ]; then
-        bls_debug="$(echo ${bls_target} | sed -e "s/${kernelver}/${kernelver}~debug/")"
-        cp -aT  "${bls_target}" "${bls_debug}"
-        title="$(grep '^title[ \t]' "${bls_debug}" | sed -e 's/^title[ \t]*//')"
-        blsid="$(grep '^id[ \t]' "${bls_debug}" | sed -e "s/\.${ARCH}/-debug.${arch}/")"
-        sed -i -e "s/^title.*/title ${title}${GRUB_LINUX_DEBUG_TITLE_POSTFIX}/" "${bls_debug}"
-        sed -i -e "s/^id.*/${blsid}/" "${bls_debug}"
-        sed -i -e "s/^options.*/options \$kernelopts ${GRUB_CMDLINE_LINUX_DEBUG}/" "${bls_debug}"
-    fi
-done
+	if [ "x$GRUB_LINUX_MAKE_DEBUG" = "xtrue" ]; then
+            bls_debug="$(echo ${bls_target} | sed -e "s/${kernelver}/${kernelver}~debug/")"
+            cp -aT  "${bls_target}" "${bls_debug}"
+            title="$(grep '^title[ \t]' "${bls_debug}" | sed -e 's/^title[ \t]*//')"
+            blsid="$(grep '^id[ \t]' "${bls_debug}" | sed -e "s/\.${ARCH}/-debug.${arch}/")"
+            sed -i -e "s/^title.*/title ${title}${GRUB_LINUX_DEBUG_TITLE_POSTFIX}/" "${bls_debug}"
+            sed -i -e "s/^id.*/${blsid}/" "${bls_debug}"
+            sed -i -e "s/^options.*/options \$kernelopts ${GRUB_CMDLINE_LINUX_DEBUG}/" "${bls_debug}"
+	fi
+    done
 
-if [ -f "/boot/vmlinuz-0-rescue-${MACHINE_ID}" ]; then
-    mkbls "0-rescue-${MACHINE_ID}" "0" "${bootprefix}" >"${blsdir}/${MACHINE_ID}-0-rescue.conf"
-fi
+    if [ -f "/boot/vmlinuz-0-rescue-${MACHINE_ID}" ]; then
+	mkbls "0-rescue-${MACHINE_ID}" "0" "${bootprefix}" >"${blsdir}/${MACHINE_ID}-0-rescue.conf"
+    fi
+}
 
 GENERATE=0
 if grep '^GRUB_ENABLE_BLSCFG=.*' "${etcdefaultgrub}" \
@@ -283,6 +285,8 @@ elif ! grep -q '^GRUB_ENABLE_BLSCFG=.*' "${etcdefaultgrub}" ; then
 fi
 
 if [ "${GENERATE}" -eq 1 ] ; then
+    copy_bls
+
     if [ $arch = "x86_64" ] && [ ! -d /sys/firmware/efi ]; then
 	if ! cp ${prefix}/lib/grub//i386-pc/blscfg.mod ${grubdir}/i386-pc/ ; then
 	    exit 1
