Blob Blame History Raw
From a0fd433e874a0fbdd8c43ae42d25969550311b9c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jaroslav=20=C5=A0karvada?= <jskarvad@redhat.com>
Date: Sun, 29 Nov 2020 21:34:15 +0100
Subject: [PATCH] bootloader: fixed cmdline duplication with BLS and
 grub2-mkconfig
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

After application of this patch in order to get rid of the duplicated
kernel command line options (which was duplicated due to the usage of
the grub2-mkconfig), the grub2-mkconfig needs to be run again.
We do not run it automatically in the spec file %post not to break
users not using grub2-mkconfig.

This fix expects that user has not modified the line containing the
GRUB_CMDLINE_LINUX_DEFAULT option and the $tuned_params in the
/etc/default/grub file. If user modified this line, the option
duplication may still occure. In such case, please remove the
$tuned_params from the /etc/default/grub by hand.

Resolves: rhbz#1777874

Signed-off-by: Jaroslav Škarvada <jskarvad@redhat.com>
---
 tuned/plugins/plugin_bootloader.py | 34 +++++++++++++++++++++++++++++-
 1 file changed, 33 insertions(+), 1 deletion(-)

diff --git a/tuned/plugins/plugin_bootloader.py b/tuned/plugins/plugin_bootloader.py
index 8ca5b8a1..416df7d6 100644
--- a/tuned/plugins/plugin_bootloader.py
+++ b/tuned/plugins/plugin_bootloader.py
@@ -35,6 +35,7 @@ def _instance_init(self, instance):
 		self._cmdline_val = ""
 		self._initrd_val = ""
 		self._grub2_cfg_file_names = self._get_grub2_cfg_files()
+		self._bls = self._bls_enabled()
 
 	def _instance_cleanup(self, instance):
 		pass
@@ -88,6 +89,15 @@ def _get_grub2_cfg_files(self):
 				cfg_files.append(f)
 		return cfg_files
 
+	def _bls_enabled(self):
+		grub2_default_env = self._cmd.read_file(consts.GRUB2_DEFAULT_ENV_FILE)
+		if len(grub2_default_env) <= 0:
+			log.info("cannot read '%s'" % consts.GRUB2_DEFAULT_ENV_FILE)
+			return False
+
+		return re.search(r"^\s*GRUB_ENABLE_BLSCFG\s*=\s*\"?\s*[tT][rR][uU][eE]\s*\"?\s*$", grub2_default_env,
+			flags = re.MULTILINE) is not None
+
 	def _patch_bootcmdline(self, d):
 		return self._cmd.add_modify_option_in_file(consts.BOOT_CMDLINE_FILE, d)
 
@@ -154,6 +164,25 @@ def _grub2_default_env_patch(self):
 			self._cmd.write_to_file(consts.GRUB2_DEFAULT_ENV_FILE, grub2_default_env)
 		return True
 
+	def _grub2_default_env_unpatch(self):
+		grub2_default_env = self._cmd.read_file(consts.GRUB2_DEFAULT_ENV_FILE)
+		if len(grub2_default_env) <= 0:
+			log.info("cannot read '%s'" % consts.GRUB2_DEFAULT_ENV_FILE)
+			return False
+
+		write = False
+		if re.search(r"^GRUB_CMDLINE_LINUX_DEFAULT=\"\$\{GRUB_CMDLINE_LINUX_DEFAULT:\+\$GRUB_CMDLINE_LINUX_DEFAULT \}\\\$" +
+			consts.GRUB2_TUNED_VAR + "\"$", grub2_default_env, flags = re.MULTILINE):
+				write = True
+				cfg = re.sub(r"^GRUB_CMDLINE_LINUX_DEFAULT=\"\$\{GRUB_CMDLINE_LINUX_DEFAULT:\+\$GRUB_CMDLINE_LINUX_DEFAULT \}\\\$" +
+					consts.GRUB2_TUNED_VAR + "\"$\n", "", grub2_default_env, flags = re.MULTILINE)
+				if cfg[-1] != "\n":
+					cfg += "\n"
+		if write:
+			log.debug("unpatching '%s'" % consts.GRUB2_DEFAULT_ENV_FILE)
+			self._cmd.write_to_file(consts.GRUB2_DEFAULT_ENV_FILE, cfg)
+		return True
+
 	def _grub2_cfg_patch(self, d):
 		log.debug("patching grub.cfg")
 		if not self._grub2_cfg_file_names:
@@ -180,7 +209,10 @@ def _grub2_cfg_patch(self, d):
 			if patch_initial:
 				grub2_cfg_new = self._grub2_cfg_patch_initial(self._grub2_cfg_unpatch(grub2_cfg), d)
 			self._cmd.write_to_file(f, grub2_cfg_new)
-		self._grub2_default_env_patch()
+		if self._bls:
+			self._grub2_default_env_unpatch()
+		else:
+			self._grub2_default_env_patch()
 		return True
 
 	def _grub2_update(self):