4a74218
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
4a74218
From: Javier Martinez Canillas <javierm@redhat.com>
4a74218
Date: Wed, 13 May 2020 19:40:10 +0200
4a74218
Subject: [PATCH] 10_linux.in: Store cmdline in BLS snippets instead of using a
4a74218
 variable
4a74218
4a74218
The kernel cmdline was stored as a kernelopts variable in the grubenv file
4a74218
and the BLS snippets used that. But this turned out to be fragile since the
4a74218
grubenv file could be removed or get corrupted easily.
4a74218
4a74218
To prevent the entries to not have a cmdline if the grubenv can't be read,
4a74218
a fallback variable was set in the GRUB config file. But this still caused
4a74218
issues since the config needs to be re-generated to change the parameters.
4a74218
4a74218
Instead, let's store the cmdline in the BLS snippets. This will make the
4a74218
configuration more robust, since it will work even without the grubenv
4a74218
file and the BLS entries will contain all the information needed to boot.
4a74218
4a74218
Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
4a74218
---
4a74218
 util/grub-switch-to-blscfg.in | 30 ++++++++++--------------------
4a74218
 util/grub.d/10_linux.in       | 41 +++++++++++++++++++++++++++++++----------
4a74218
 2 files changed, 41 insertions(+), 30 deletions(-)
4a74218
4a74218
diff --git a/util/grub-switch-to-blscfg.in b/util/grub-switch-to-blscfg.in
4a74218
index 3333a620c28..cb229126128 100644
4a74218
--- a/util/grub-switch-to-blscfg.in
4a74218
+++ b/util/grub-switch-to-blscfg.in
4a74218
@@ -190,7 +190,7 @@ fi
4a74218
 mkbls() {
4a74218
     local kernelver=$1 && shift
4a74218
     local datetime=$1 && shift
4a74218
-    local bootprefix=$1 && shift
4a74218
+    local kernelopts=$1 && shift
4a74218
 
4a74218
     local debugname=""
4a74218
     local debugid=""
4a74218
@@ -209,10 +209,9 @@ mkbls() {
4a74218
         cat <
4a74218
 title ${NAME} (${kernelver}) ${VERSION}${debugname}
4a74218
 version ${kernelver}${debugid}
4a74218
-linux ${bootprefix}/vmlinuz-${kernelver}
4a74218
-initrd ${bootprefix}/initramfs-${kernelver}.img
4a74218
-options \$kernelopts
4a74218
-id ${ID}-${datetime}-${kernelver}
4a74218
+linux /vmlinuz-${kernelver}
4a74218
+initrd /initramfs-${kernelver}.img
4a74218
+options ${kernelopts}
4a74218
 grub_users \$grub_users
4a74218
 grub_arg --unrestricted
4a74218
 grub_class kernel${flavor}
4a74218
@@ -236,28 +235,19 @@ copy_bls() {
4a74218
 
4a74218
 	linux_relpath="$("${grub_mkrelpath}" "${linux_path}")"
4a74218
 	bootprefix="${linux_relpath%%"${linux}"}"
4a74218
+	cmdline="root=${LINUX_ROOT_DEVICE} ro ${GRUB_CMDLINE_LINUX} ${GRUB_CMDLINE_LINUX_DEFAULT}"
4a74218
 
4a74218
-	if [ -f "${kernel_dir}/bls.conf" ] ; then
4a74218
-            cp -af "${kernel_dir}/bls.conf" "${bls_target}"
4a74218
-            if [ -n "${bootprefix}" ]; then
4a74218
-		sed -i -e "s,^\(linux[^ \t]*[ \t]\+\).*,\1${bootprefix}${linux},g" "${bls_target}"
4a74218
-		sed -i -e "/^initrd/ s,\([ \t]\+\)\([^ \t]\+\),\1${bootprefix}\2,g" "${bls_target}"
4a74218
-            fi
4a74218
-	else
4a74218
-            mkbls "${kernelver}" \
4a74218
-		  "$(date -u +%Y%m%d%H%M%S -d "$(stat -c '%y' "${kernel_dir}")")" \
4a74218
-		  "${bootprefix}" \
4a74218
-		  >"${bls_target}"
4a74218
-	fi
4a74218
+	mkbls "${kernelver}" \
4a74218
+	      "$(date -u +%Y%m%d%H%M%S -d "$(stat -c '%y' "${kernel_dir}")")" \
4a74218
+	      "${bootprefix}" "${cmdline}" >"${bls_target}"
4a74218
 
4a74218
 	if [ "x$GRUB_LINUX_MAKE_DEBUG" = "xtrue" ]; then
4a74218
             bls_debug="$(echo ${bls_target} | sed -e "s/${kernelver}/${kernelver}~debug/")"
4a74218
             cp -aT  "${bls_target}" "${bls_debug}"
4a74218
             title="$(grep '^title[ \t]' "${bls_debug}" | sed -e 's/^title[ \t]*//')"
4a74218
-            blsid="$(grep '^id[ \t]' "${bls_debug}" | sed -e "s/\.${ARCH}/-debug.${arch}/")"
4a74218
+            options="$(echo "${cmdline} ${GRUB_CMDLINE_LINUX_DEBUG}" | sed -e 's/\//\\\//g')"
4a74218
             sed -i -e "s/^title.*/title ${title}${GRUB_LINUX_DEBUG_TITLE_POSTFIX}/" "${bls_debug}"
4a74218
-            sed -i -e "s/^id.*/${blsid}/" "${bls_debug}"
4a74218
-            sed -i -e "s/^options.*/options \$kernelopts ${GRUB_CMDLINE_LINUX_DEBUG}/" "${bls_debug}"
4a74218
+            sed -i -e "s/^options.*/options ${options}/" "${bls_debug}"
4a74218
 	fi
4a74218
     done
4a74218
 
4a74218
diff --git a/util/grub.d/10_linux.in b/util/grub.d/10_linux.in
4a74218
index 09adfce80fd..80299ecaf00 100644
4a74218
--- a/util/grub.d/10_linux.in
4a74218
+++ b/util/grub.d/10_linux.in
4a74218
@@ -134,23 +134,43 @@ read_config()
4a74218
     done < ${config_file}
4a74218
 }
4a74218
 
4a74218
-populate_menu()
4a74218
+blsdir="/boot/loader/entries"
4a74218
+
4a74218
+get_sorted_bls()
4a74218
 {
4a74218
-    blsdir="/boot/loader/entries"
4a74218
-    local -a files
4a74218
     local IFS=$'\n'
4a74218
-    gettext_printf "Generating boot entries from BLS files...\n" >&2
4a74218
 
4a74218
-    files=($(for bls in ${blsdir}/*.conf ; do
4a74218
-        if ! [[ -e "${bls}" ]] ; then
4a74218
-            continue
4a74218
-        fi
4a74218
+    files=($(for bls in ${blsdir}/*.conf; do
4a74218
         bls="${bls%.conf}"
4a74218
         bls="${bls##*/}"
4a74218
         echo "${bls}"
4a74218
     done | ${kernel_sort} | tac)) || :
4a74218
 
4a74218
-    for bls in "${files[@]}" ; do
4a74218
+    echo "${files[@]}"
4a74218
+}
4a74218
+
4a74218
+update_bls_cmdline()
4a74218
+{
4a74218
+    local cmdline="root=${LINUX_ROOT_DEVICE} ro ${GRUB_CMDLINE_LINUX} ${GRUB_CMDLINE_LINUX_DEFAULT}"
4a74218
+    local -a files=($(get_sorted_bls))
4a74218
+
4a74218
+    for bls in "${files[@]}"; do
4a74218
+        local options="${cmdline}"
4a74218
+        if [ -z "${bls##*debug*}" ]; then
4a74218
+            options="${options} ${GRUB_CMDLINE_LINUX_DEBUG}"
4a74218
+        fi
4a74218
+        options="$(echo "${options}" | sed -e 's/\//\\\//g')"
4a74218
+        sed -i -e "s/^options.*/options ${options}/" "${blsdir}/${bls}.conf"
4a74218
+    done
4a74218
+}
4a74218
+
4a74218
+populate_menu()
4a74218
+{
4a74218
+    local -a files=($(get_sorted_bls))
4a74218
+
4a74218
+    gettext_printf "Generating boot entries from BLS files...\n" >&2
4a74218
+
4a74218
+    for bls in "${files[@]}"; do
4a74218
         read_config "${blsdir}/${bls}.conf"
4a74218
 
4a74218
         menu="${menu}menuentry '${title}' ${grub_arg} --id=${bls} {\n"
4a74218
@@ -224,6 +244,8 @@ if [ -z "\${kernelopts}" ]; then
4a74218
 fi
4a74218
 EOF
4a74218
 
4a74218
+  update_bls_cmdline
4a74218
+
4a74218
   if [ "x${BLS_POPULATE_MENU}" = "xtrue" ]; then
4a74218
       populate_menu
4a74218
   else
4a74218
@@ -244,7 +266,6 @@ EOF
4a74218
           fi
4a74218
       fi
4a74218
 
4a74218
-      ${grub_editenv} - set kernelopts="root=${LINUX_ROOT_DEVICE} ro ${GRUB_CMDLINE_LINUX} ${GRUB_CMDLINE_LINUX_DEFAULT}"
4a74218
       if [ -n "${GRUB_EARLY_INITRD_LINUX_CUSTOM}" ]; then
4a74218
           ${grub_editenv} - set early_initrd="${GRUB_EARLY_INITRD_LINUX_CUSTOM}"
4a74218
       fi