4ff5f8
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
4ff5f8
From: Javier Martinez Canillas <javierm@redhat.com>
4ff5f8
Date: Tue, 4 Dec 2018 10:48:45 +0100
4ff5f8
Subject: [PATCH] BLS files should only be copied by grub-switch-to-blscfg if
4ff5f8
 BLS isn't set
4ff5f8
4ff5f8
Currently the grub-switch-to-blscfg script doesn't update the grub.cfg if
4ff5f8
GRUB_ENABLE_BLSCFG=true is already set in /etc/default/grub. But it still
4ff5f8
copies the BLS files which may overwrite fields modified by the user.
4ff5f8
4ff5f8
Related: rhbz#1638117
4ff5f8
4ff5f8
Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
4ff5f8
---
4ff5f8
 util/grub-switch-to-blscfg.in | 80 +++++++++++++++++++++++--------------------
4ff5f8
 1 file changed, 42 insertions(+), 38 deletions(-)
4ff5f8
4ff5f8
diff --git a/util/grub-switch-to-blscfg.in b/util/grub-switch-to-blscfg.in
4ff5f8
index d353370cc51..eeea1307706 100644
4ff5f8
--- a/util/grub-switch-to-blscfg.in
4ff5f8
+++ b/util/grub-switch-to-blscfg.in
4ff5f8
@@ -220,49 +220,51 @@ EOF
4ff5f8
     ) | cat
4ff5f8
 }
4ff5f8
 
4ff5f8
-for kernelver in $(cd /lib/modules/ ; ls -1) "" ; do
4ff5f8
-    bls_target="${blsdir}/${MACHINE_ID}-${kernelver}.conf"
4ff5f8
-    linux="/vmlinuz-${kernelver}"
4ff5f8
-    linux_path="/boot${linux}"
4ff5f8
-    kernel_dir="/lib/modules/${kernelver}"
4ff5f8
+copy_bls() {
4ff5f8
+    for kernelver in $(cd /lib/modules/ ; ls -1) "" ; do
4ff5f8
+	bls_target="${blsdir}/${MACHINE_ID}-${kernelver}.conf"
4ff5f8
+	linux="/vmlinuz-${kernelver}"
4ff5f8
+	linux_path="/boot${linux}"
4ff5f8
+	kernel_dir="/lib/modules/${kernelver}"
4ff5f8
 
4ff5f8
-    if [ ! -d "${kernel_dir}" ] ; then
4ff5f8
-        continue
4ff5f8
-    fi
4ff5f8
-    if [ ! -f "${linux_path}" ]; then
4ff5f8
-        continue
4ff5f8
-    fi
4ff5f8
+	if [ ! -d "${kernel_dir}" ] ; then
4ff5f8
+            continue
4ff5f8
+	fi
4ff5f8
+	if [ ! -f "${linux_path}" ]; then
4ff5f8
+            continue
4ff5f8
+	fi
4ff5f8
 
4ff5f8
-    linux_relpath="$("${grub_mkrelpath}" "${linux_path}")"
4ff5f8
-    bootprefix="${linux_relpath%%"${linux}"}"
4ff5f8
+	linux_relpath="$("${grub_mkrelpath}" "${linux_path}")"
4ff5f8
+	bootprefix="${linux_relpath%%"${linux}"}"
4ff5f8
 
4ff5f8
-    if [ -f "${kernel_dir}/bls.conf" ] ; then
4ff5f8
-        cp -af "${kernel_dir}/bls.conf" "${bls_target}"
4ff5f8
-        if [ -n "${bootprefix}" ]; then
4ff5f8
-            sed -i -e "s,^\(linux[^ \t]*[ \t]\+\).*,\1${bootprefix}${linux},g" "${bls_target}"
4ff5f8
-            sed -i -e "/^initrd/ s,\([ \t]\+\)\([^ \t]\+\),\1${bootprefix}\2,g" "${bls_target}"
4ff5f8
-        fi
4ff5f8
-    else
4ff5f8
-        mkbls "${kernelver}" \
4ff5f8
-            "$(date -u +%Y%m%d%H%M%S -d "$(stat -c '%y' "${kernel_dir}")")" \
4ff5f8
-            "${bootprefix}" \
4ff5f8
-            >"${bls_target}"
4ff5f8
-    fi
4ff5f8
+	if [ -f "${kernel_dir}/bls.conf" ] ; then
4ff5f8
+            cp -af "${kernel_dir}/bls.conf" "${bls_target}"
4ff5f8
+            if [ -n "${bootprefix}" ]; then
4ff5f8
+		sed -i -e "s,^\(linux[^ \t]*[ \t]\+\).*,\1${bootprefix}${linux},g" "${bls_target}"
4ff5f8
+		sed -i -e "/^initrd/ s,\([ \t]\+\)\([^ \t]\+\),\1${bootprefix}\2,g" "${bls_target}"
4ff5f8
+            fi
4ff5f8
+	else
4ff5f8
+            mkbls "${kernelver}" \
4ff5f8
+		  "$(date -u +%Y%m%d%H%M%S -d "$(stat -c '%y' "${kernel_dir}")")" \
4ff5f8
+		  "${bootprefix}" \
4ff5f8
+		  >"${bls_target}"
4ff5f8
+	fi
4ff5f8
 
4ff5f8
-    if [ "x$GRUB_LINUX_MAKE_DEBUG" = "xtrue" ]; then
4ff5f8
-        bls_debug="$(echo ${bls_target} | sed -e "s/${kernelver}/${kernelver}~debug/")"
4ff5f8
-        cp -aT  "${bls_target}" "${bls_debug}"
4ff5f8
-        title="$(grep '^title[ \t]' "${bls_debug}" | sed -e 's/^title[ \t]*//')"
4ff5f8
-        blsid="$(grep '^id[ \t]' "${bls_debug}" | sed -e "s/\.${ARCH}/-debug.${arch}/")"
4ff5f8
-        sed -i -e "s/^title.*/title ${title}${GRUB_LINUX_DEBUG_TITLE_POSTFIX}/" "${bls_debug}"
4ff5f8
-        sed -i -e "s/^id.*/${blsid}/" "${bls_debug}"
4ff5f8
-        sed -i -e "s/^options.*/options \$kernelopts ${GRUB_CMDLINE_LINUX_DEBUG}/" "${bls_debug}"
4ff5f8
-    fi
4ff5f8
-done
4ff5f8
+	if [ "x$GRUB_LINUX_MAKE_DEBUG" = "xtrue" ]; then
4ff5f8
+            bls_debug="$(echo ${bls_target} | sed -e "s/${kernelver}/${kernelver}~debug/")"
4ff5f8
+            cp -aT  "${bls_target}" "${bls_debug}"
4ff5f8
+            title="$(grep '^title[ \t]' "${bls_debug}" | sed -e 's/^title[ \t]*//')"
4ff5f8
+            blsid="$(grep '^id[ \t]' "${bls_debug}" | sed -e "s/\.${ARCH}/-debug.${arch}/")"
4ff5f8
+            sed -i -e "s/^title.*/title ${title}${GRUB_LINUX_DEBUG_TITLE_POSTFIX}/" "${bls_debug}"
4ff5f8
+            sed -i -e "s/^id.*/${blsid}/" "${bls_debug}"
4ff5f8
+            sed -i -e "s/^options.*/options \$kernelopts ${GRUB_CMDLINE_LINUX_DEBUG}/" "${bls_debug}"
4ff5f8
+	fi
4ff5f8
+    done
4ff5f8
 
4ff5f8
-if [ -f "/boot/vmlinuz-0-rescue-${MACHINE_ID}" ]; then
4ff5f8
-    mkbls "0-rescue-${MACHINE_ID}" "0" "${bootprefix}" >"${blsdir}/${MACHINE_ID}-0-rescue.conf"
4ff5f8
-fi
4ff5f8
+    if [ -f "/boot/vmlinuz-0-rescue-${MACHINE_ID}" ]; then
4ff5f8
+	mkbls "0-rescue-${MACHINE_ID}" "0" "${bootprefix}" >"${blsdir}/${MACHINE_ID}-0-rescue.conf"
4ff5f8
+    fi
4ff5f8
+}
4ff5f8
 
4ff5f8
 GENERATE=0
4ff5f8
 if grep '^GRUB_ENABLE_BLSCFG=.*' "${etcdefaultgrub}" \
4ff5f8
@@ -283,6 +285,8 @@ elif ! grep -q '^GRUB_ENABLE_BLSCFG=.*' "${etcdefaultgrub}" ; then
4ff5f8
 fi
4ff5f8
 
4ff5f8
 if [ "${GENERATE}" -eq 1 ] ; then
4ff5f8
+    copy_bls
4ff5f8
+
4ff5f8
     if [ $arch = "x86_64" ] && [ ! -d /sys/firmware/efi ]; then
4ff5f8
 	if ! cp ${prefix}/lib/grub//i386-pc/blscfg.mod ${grubdir}/i386-pc/ ; then
4ff5f8
 	    exit 1