diff --git a/README-suse.md b/README-suse.md index ef1f34e..3130077 100644 --- a/README-suse.md +++ b/README-suse.md @@ -10,7 +10,7 @@ All boot entries share one kernel command line, so `options` need `kernel: ALL` Options go to `GRUB_CMDLINE_LINUX_DEFAULT`, which the recovery entry does not read; setting a new value also takes the name out of `GRUB_CMDLINE_LINUX`, and a removal applies to both, so the recovery entry loses them. `previous: replaced` replaces `GRUB_CMDLINE_LINUX_DEFAULT` only. A value that is already on the command line is left as it is, together with the other values of its name, so an option such as `console` must list every value it should have, or be removed with `state: absent` first. -The role fails instead of guessing: when an option name has different values in the two variables, when a name or value contains whitespace, quotes, or any of `$`, `` ` ``, `;`, `\`, `{`, `}`, `|`, `&`, `<`, `>` and `#`, or when a variable it reads in `/etc/default/grub` uses shell expansion or escapes, or a command line it changes holds a quoted kernel argument. +The role fails instead of guessing: when an option name has different values in the two variables, when a name or value contains whitespace, quotes, or any of `$`, `` ` ``, `;`, `\`, `{`, `}`, `|`, `&`, `<`, `>` and `#`, or when a variable it reads in `/etc/default/grub` uses shell expansion or escapes (except the line tuned's bootloader plugin adds for `$tuned_params`, which it leaves in place; the options tuned sets are not seen by the role), or a command line it changes holds a quoted kernel argument. ## Default entry, timeout and password diff --git a/module_utils/bootloader_lsr/grub.py b/module_utils/bootloader_lsr/grub.py index 5e98a53..7cb048e 100644 --- a/module_utils/bootloader_lsr/grub.py +++ b/module_utils/bootloader_lsr/grub.py @@ -23,6 +23,16 @@ MENU_HEADER = re.compile(r"\s*(menuentry|submenu)\s+(['\"])(.*?)\2(.*)\{\s*$") MENU_ID = re.compile(r"\$menuentry_id_option\s+['\"]([^'\"]*)['\"]") +# tuned's bootloader plugin appends this line and removes it by this exact text +TUNED_LINE = ( + 'GRUB_CMDLINE_LINUX_DEFAULT="${GRUB_CMDLINE_LINUX_DEFAULT:+' + '$GRUB_CMDLINE_LINUX_DEFAULT }\\$tuned_params"' +) + + +def is_assignment(line, name): + """Whether line assigns NAME, leaving out tuned's own line""" + return bool(re.match(r"\s*" + name + r"=", line)) and line.strip() != TUNED_LINE def get_shell_var(text, name): @@ -30,7 +40,7 @@ def get_shell_var(text, name): value = None for line in text.splitlines(): match = re.match(r"\s*" + name + r"=(.*)$", line) - if match: + if match and line.strip() != TUNED_LINE: if re.search(r"[$`\\]", match.group(1)): raise ValueError("%s uses shell expansion or escapes" % name) value = " ".join(shlex.split(match.group(1), comments=True)) @@ -42,11 +52,12 @@ def set_shell_var(text, name, value): new_line = '%s="%s"' % (name, value) lines = text.splitlines() for i in reversed(range(len(lines))): - if re.match(r"\s*" + name + r"=", lines[i]): + if is_assignment(lines[i], name): lines[i] = new_line break else: - lines.append(new_line) + tuned = [i for i, line in enumerate(lines) if line.strip() == TUNED_LINE] + lines.insert(tuned[0] if tuned else len(lines), new_line) return "\n".join(lines) + "\n" diff --git a/tests/unit/test_grub.py b/tests/unit/test_grub.py index 9e36600..9259ff7 100644 --- a/tests/unit/test_grub.py +++ b/tests/unit/test_grub.py @@ -120,6 +120,32 @@ def test_shell_vars(self): grub.get_grubenv("# GRUB Environment Block\nsaved_entry=1\n####"), ) + def test_shell_vars_tuned(self): + tuned = ( + 'GRUB_CMDLINE_LINUX_DEFAULT="${GRUB_CMDLINE_LINUX_DEFAULT:+' + '$GRUB_CMDLINE_LINUX_DEFAULT }\\$tuned_params"' + ) + name = "GRUB_CMDLINE_LINUX_DEFAULT" + text = 'GRUB_CMDLINE_LINUX_DEFAULT="quiet"\n' + tuned + "\n" + self.assertEqual("quiet", grub.get_shell_var(text, name)) + self.assertIsNone(grub.get_shell_var(tuned + "\n", name)) + edited = grub.set_shell_var(text, name, "quiet audit=1") + self.assertEqual( + 'GRUB_CMDLINE_LINUX_DEFAULT="quiet audit=1"\n' + tuned + "\n", edited + ) + self.assertEqual("quiet audit=1", grub.get_shell_var(edited, name)) + self.assertEqual( + 'X=1\nGRUB_CMDLINE_LINUX_DEFAULT="audit=1"\n' + tuned + "\n", + grub.set_shell_var("X=1\n" + tuned + "\n", name, "audit=1"), + ) + later = tuned + '\nGRUB_CMDLINE_LINUX_DEFAULT="a"\n' + self.assertEqual( + tuned + '\nGRUB_CMDLINE_LINUX_DEFAULT="a b"\n', + grub.set_shell_var(later, name, "a b"), + ) + with self.assertRaises(ValueError): + grub.get_shell_var(text.replace("tuned_params", "other_params"), name) + class RunningSystem(unittest.TestCase): """test what the pbl backend reads from the running system"""