From 6b8b5924aeadbb42714f136767ee9eaa0768b3d7 Mon Sep 17 00:00:00 2001 From: Rich Megginson Date: Wed, 18 Nov 2026 08:34:48 -0700 Subject: [PATCH 1/7] fix: handle the extra /boot prefix that grubby reports on some platforms Cause: On platforms where /boot is a separate mount and the BLS entries store absolute /boot/... paths, grubby (grubby-bls) reports kernel and initrd paths with an extra /boot prefix (for example /boot/boot/vmlinuz-...). grubby also strips the prefix from the values of only some of its options (remove_var_prefix handles --add-kernel, --initrd, --update-kernel and --remove-kernel), so those commands need the extra prefix on the affected platforms, while the other commands (--info, --set-default) need the real kernel path. Fedora 44 additionally has a kernel path matching bug in param_to_indexes that prevents grubby from matching a kernel path in the commands whose values are not prefix-stripped. Consequences: The bootloader_facts module reported the doubled /boot/boot/... paths, and the bootloader_settings module passed the wrong path form to grubby, so modifying kernel arguments, setting the default kernel, and removing kernels silently failed on the affected platforms. Fix: Strip the extra /boot prefix from the grubby output in bootloader_facts and bootloader_settings, detect whether grubby reports the extra prefix (has_extra_boot_prefix), add the extra prefix to the values of the grubby commands that grubby itself strips it from (--add-kernel, --initrd, --update-kernel, --remove-kernel), and use the real kernel path for the other commands (--info, --set-default). The code shared by the two modules now lives in module_utils/bootloader_lsr/bootloader_common.py. The integration test cleanups use grubby TITLE= matching, which works regardless of the prefix, to remove the cloned kernels. Result: The bootloader role now works correctly on platforms affected by the grubby extra /boot prefix issue: bootloader_facts reports the real kernel and initrd paths, and the grubby commands issued by bootloader_settings match the intended boot entries. The unit tests (81) and the Fedora 44 qemu integration tests pass. Assisted-by: goose using model glm-5-3 Signed-off-by: Rich Megginson --- library/bootloader_facts.py | 26 +- library/bootloader_settings.py | 81 +++-- .../bootloader_lsr/bootloader_common.py | 113 +++++++ tests/tests_add_rm.yml | 8 +- tests/tests_settings.yml | 7 +- tests/unit/test_bootloader_common.py | 256 ++++++++++++++ tests/unit/test_bootloader_facts.py | 1 + tests/unit/test_bootloader_settings.py | 314 ++++++++++++++++++ 8 files changed, 741 insertions(+), 65 deletions(-) create mode 100644 module_utils/bootloader_lsr/bootloader_common.py create mode 100644 tests/unit/test_bootloader_common.py diff --git a/library/bootloader_facts.py b/library/bootloader_facts.py index ccb39285..781e6a14 100644 --- a/library/bootloader_facts.py +++ b/library/bootloader_facts.py @@ -2,6 +2,7 @@ # Copyright: (c) 2023, Sergei Petrosian # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) +# Assisted by goose using model glm-5-3 from __future__ import absolute_import, division, print_function __metaclass__ = type @@ -116,34 +117,11 @@ """ -import re from ansible.module_utils.basic import AnsibleModule +from ansible.module_utils.bootloader_lsr.bootloader_common import get_facts from ansible.module_utils.bootloader_lsr.grub import check_loader, get_kernels -def get_facts(kernels_info, default_kernel): - """Get kernel facts""" - kernels_info_lines = kernels_info.strip().split("\n") - kernels = [] - index_count = 0 - - for line in kernels_info_lines: - index = re.search(r"index=(\d+)", line) - if index: - is_default = index.group(1) == default_kernel.strip() - index_count += 1 - kernels.append({}) - search = re.search(r"(.*?)=(.*)", line) - if search: - key = search.group(1).strip('"') - value = search.group(2).strip('"') - kernels[index_count - 1].update({key: value}) - else: - kernels[index_count - 1].update({"kernel": line}) - kernels[index_count - 1].update({"default": is_default}) - return kernels - - def run_module(): # define available arguments/parameters a user can pass to the module module_args = dict( diff --git a/library/bootloader_settings.py b/library/bootloader_settings.py index 88880c2e..05936187 100644 --- a/library/bootloader_settings.py +++ b/library/bootloader_settings.py @@ -3,6 +3,7 @@ # Copyright: (c) 2023, Sergei Petrosian # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) # Includes code generated by Cursor IDE using the models Composer 2.5, Codex 5.3, and Sonnet 4.6 +# Assisted by goose using model glm-5-3 from __future__ import absolute_import, division, print_function __metaclass__ = type @@ -110,29 +111,12 @@ # pylint: disable=import-error import ansible.module_utils.six.moves as ansible_six_moves from ansible.module_utils.bootloader_lsr import pbl - - -def get_facts(kernels_info, default_kernel_index): - """Get kernel facts""" - kernels_info_lines = kernels_info.strip().split("\n") - kernels = [] - index_count = 0 - - for line in kernels_info_lines: - index = re.search(r"index=(\d+)", line) - if index: - is_default = index.group(1) == default_kernel_index.strip() - index_count += 1 - kernels.append({}) - search = re.search(r"(.*?)=(.*)", line) - if search: - key = search.group(1).strip('"') - value = search.group(2).strip('"') - kernels[index_count - 1].update({key: value}) - else: - kernels[index_count - 1].update({"kernel": line}) - kernels[index_count - 1].update({"default": is_default}) - return kernels +from ansible.module_utils.bootloader_lsr.bootloader_common import ( + add_extra_boot_prefix, + get_facts, + has_extra_boot_prefix, + strip_extra_boot_prefix, +) def get_dict_same_keys(dict1, dict2): @@ -171,26 +155,32 @@ def get_kernel_to_mod(bootloader_setting_kernel, kernel_mod_keys): } -def get_single_kernel(bootloader_setting_kernel): +def get_single_kernel(bootloader_setting_kernel, extra_boot_prefix=False): """Get kernel in the format expected by 'grubby --update-kernel=' from a one-element dict""" kernel_key, kernel_val = list(bootloader_setting_kernel.items())[0] kernel_key_prefix = "" if kernel_key == "title": kernel_key_prefix = "TITLE=" + if kernel_key == "path": + kernel_val = add_extra_boot_prefix(kernel_val, extra_boot_prefix) return kernel_key_prefix + escapeval(kernel_val) -def get_create_kernel(bootloader_setting_kernel): +def get_create_kernel(bootloader_setting_kernel, extra_boot_prefix=False): """Get kernel in the format expected by 'grubby --add-kernel=' from a multiple-element dict""" kernel = "" for key in sorted(bootloader_setting_kernel.keys()): value = bootloader_setting_kernel[key] if key == "path": - kernel += " --add-kernel=" + escapeval(value) + kernel += " --add-kernel=" + escapeval( + add_extra_boot_prefix(value, extra_boot_prefix) + ) elif key == "title": kernel += " --title=" + escapeval(value) elif key == "initrd": - kernel += " --initrd=" + escapeval(value) + kernel += " --initrd=" + escapeval( + add_extra_boot_prefix(value, extra_boot_prefix) + ) return kernel.strip() @@ -222,7 +212,9 @@ def validate_default_kernel(module, bootloader_settings): ) -def validate_kernels(module, bootloader_setting, bootloader_facts): +def validate_kernels( + module, bootloader_setting, bootloader_facts, extra_boot_prefix=False +): """Validate that user passes bootloader_setting correctly""" kernel_action = "" kernel = "" @@ -281,7 +273,7 @@ def validate_kernels(module, bootloader_setting, bootloader_facts): # Validate with len(bootloader_setting["kernel"]) == 1 if len(bootloader_setting["kernel"]) == 1: validate_kernel_initrd(module, bootloader_setting["kernel"], kernel_mod_keys) - kernel = get_single_kernel(bootloader_setting["kernel"]) + kernel = get_single_kernel(bootloader_setting["kernel"], extra_boot_prefix) kernel_action = "modify" if state == "present" else "remove" return kernel_action, kernel @@ -313,13 +305,13 @@ def validate_kernels(module, bootloader_setting, bootloader_facts): kernel_action = "create" if state == "present" else "remove" if kernel_action == "create": - kernel = get_create_kernel(bootloader_setting["kernel"]) + kernel = get_create_kernel(bootloader_setting["kernel"], extra_boot_prefix) validate_kernel_initrd(module, bootloader_setting["kernel"], kernel_mod_keys) if kernel_action in ["remove", "modify"]: kernel_to_mod = get_kernel_to_mod(bootloader_setting["kernel"], kernel_mod_keys) - kernel = get_single_kernel(kernel_to_mod) + kernel = get_single_kernel(kernel_to_mod, extra_boot_prefix) return kernel_action, kernel @@ -483,11 +475,15 @@ def mod_default_kernel(module, result, bootloader_setting, kernel_info): if not kernel_match: return - kernel = kernel_match.group(1) + kernel = strip_extra_boot_prefix(kernel_match.group(1)) current_default = get_default_kernel(module, "kernel") if current_default == kernel: return + # grubby does not strip the extra /boot prefix from the --set-default + # value, so it needs the real kernel path, unlike the commands handled + # by remove_var_prefix() (--add-kernel, --initrd, --update-kernel, + # --remove-kernel), which do need the extra prefix on affected platforms cmd = "grubby --set-default=" + kernel apply_command(module, result, cmd) @@ -503,6 +499,9 @@ def get_default_kernel(module, type): module.fail_json(msg="Type must be one of 'kernel', 'title', or 'index'") cmd = "grubby --default-" + type _unused, stdout, _unused = module.run_command(cmd) + if type == "kernel": + # grubby may report the kernel path with an extra /boot prefix + return strip_extra_boot_prefix(stdout.strip()) return stdout.strip() @@ -582,9 +581,15 @@ def run_module(): default_kernel_index = get_default_kernel(module, "index") bootloader_facts = get_facts(kernels_info, default_kernel_index) + # grubby may report the kernel and initrd paths with an extra /boot + # prefix; in that case the grubby commands whose values grubby strips + # the prefix from (--add-kernel, --initrd, --update-kernel, + # --remove-kernel) need the extra prefix too, while the other + # commands (--info, --set-default) need the real kernel path + extra_boot_prefix = has_extra_boot_prefix(kernels_info) kernel_action, kernel = validate_kernels( - module, bootloader_setting, bootloader_facts + module, bootloader_setting, bootloader_facts, extra_boot_prefix ) # Remove all existing boot settings @@ -592,7 +597,11 @@ def run_module(): "options" in bootloader_setting and {"previous": "replaced"} in bootloader_setting["options"] ) and (kernel_action != "remove"): - rc, kernel_info, stderr = module.run_command("grubby --info=" + kernel) + # grubby does not strip the extra /boot prefix from the --info + # value, so it needs the real kernel path + rc, kernel_info, stderr = module.run_command( + "grubby --info=" + strip_extra_boot_prefix(kernel) + ) if needs_replacement(bootloader_setting.get("options", []), kernel_info): rm_boot_args(module, result, kernel_info, kernel) @@ -602,8 +611,10 @@ def run_module(): # Modify boot settings if kernel_action == "modify": + # grubby does not strip the extra /boot prefix from the --info + # value, so it needs the real kernel path _unused, kernel_info, _unused = module.run_command( - "grubby --info=" + kernel + "grubby --info=" + strip_extra_boot_prefix(kernel) ) mod_boot_args(module, result, bootloader_setting, kernel, kernel_info) diff --git a/module_utils/bootloader_lsr/bootloader_common.py b/module_utils/bootloader_lsr/bootloader_common.py new file mode 100644 index 00000000..dfbd3049 --- /dev/null +++ b/module_utils/bootloader_lsr/bootloader_common.py @@ -0,0 +1,113 @@ +# -*- coding: utf-8 -*- +# +# SPDX-License-Identifier: MIT +# Assisted by goose using model glm-5-3 +# +"""Code shared by the bootloader_facts and bootloader_settings modules""" + +from __future__ import absolute_import, division, print_function + +__metaclass__ = type + +import re + +# Some versions of grubby, on platforms where /boot is a separate mount point +# and the BootLoaderSpec entries store the kernel and initrd paths with the +# /boot prefix, report the paths with an extra /boot prefix; for example +# /boot/boot/vmlinuz-7.2.10-200.fc44.x86_64. Strip the extra prefix from the +# grubby output, and add it back to the kernel and initrd paths of the grubby +# commands, so that the modules work on the platforms affected by the issue. +GRUBBY_BOOT_PREFIX = "/boot" +GRUBBY_EXTRA_BOOT_PREFIX = "/boot/boot/" + + +def strip_extra_boot_prefix(value): + """Strip the extra /boot prefix that some versions of grubby add to paths + + The value can be a space separated list of paths, as the initrd value + reported by grubby can be, so the extra prefix is stripped from every + path of the list; for example /boot/boot/vmlinuz-7.2.10-200.fc44.x86_64 + becomes /boot/vmlinuz-7.2.10-200.fc44.x86_64. + """ + tokens = value.split() + if not any(token.startswith(GRUBBY_EXTRA_BOOT_PREFIX) for token in tokens): + # keep values without the extra prefix, and their whitespace, as is + return value + # strip the leading /boot of every path that has the extra prefix + boot_prefix_len = len(GRUBBY_BOOT_PREFIX) + return " ".join( + ( + token[boot_prefix_len:] + if token.startswith(GRUBBY_EXTRA_BOOT_PREFIX) + else token + ) + for token in tokens + ) + + +def has_extra_boot_prefix(kernels_info): + """Check whether grubby reports kernel paths with an extra /boot prefix + + Detect the platforms affected by the grubby issue by looking at the + kernel paths of the output of 'grubby --info=ALL', so that the extra + prefix can be added back to the kernel and initrd paths of the grubby + commands. + """ + return ( + re.search( + r'^kernel="?' + re.escape(GRUBBY_EXTRA_BOOT_PREFIX), + kernels_info, + re.MULTILINE, + ) + is not None + ) + + +def add_extra_boot_prefix(value, extra_boot_prefix): + """Add the extra /boot prefix to the kernel and initrd paths of grubby + commands + + On the platforms where grubby reports the paths with an extra /boot + prefix, grubby also expects the extra prefix in the paths passed to its + --add-kernel, --initrd, --info, --remove-kernel, --set-default and + --update-kernel arguments. The value can be a space separated list of + paths, as the initrd value can be, so the extra prefix is added to every + /boot path of the list. + """ + if not extra_boot_prefix: + return value + tokens = str(value).split() + return " ".join( + ( + GRUBBY_BOOT_PREFIX + token + if token.startswith(GRUBBY_BOOT_PREFIX + "/") + and not token.startswith(GRUBBY_EXTRA_BOOT_PREFIX) + else token + ) + for token in tokens + ) + + +def get_facts(kernels_info, default_kernel_index): + """Get kernel facts""" + kernels_info_lines = kernels_info.strip().split("\n") + kernels = [] + index_count = 0 + + for line in kernels_info_lines: + index = re.search(r"index=(\d+)", line) + if index: + is_default = index.group(1) == default_kernel_index.strip() + index_count += 1 + kernels.append({}) + search = re.search(r"(.*?)=(.*)", line) + if search: + key = search.group(1).strip('"') + value = search.group(2).strip('"') + if key in ["kernel", "initrd"]: + value = strip_extra_boot_prefix(value) + kernels[index_count - 1].update({key: value}) + else: + kernels[index_count - 1].update({"kernel": line}) + kernels[index_count - 1].update({"default": is_default}) + return kernels diff --git a/tests/tests_add_rm.yml b/tests/tests_add_rm.yml index 3792d7ad..5e14bcf1 100644 --- a/tests/tests_add_rm.yml +++ b/tests/tests_add_rm.yml @@ -1,4 +1,5 @@ # SPDX-License-Identifier: MIT +# Assisted by goose using model glm-5-3 --- - name: Test creating, modifying, and removing kernels hosts: all @@ -151,14 +152,13 @@ tags: tests::cleanup - name: Remove cloned kernels + # use the title to match the kernel: on platforms where grubby + # reports the extra /boot prefix it cannot match the kernel path command: >- - grubby --remove-kernel={{ __default_kernel.kernel }}_clone{{ item }} + grubby --remove-kernel=TITLE=Clone{{ item }} loop: - 1 - 2 changed_when: true failed_when: false - when: - - __default_kernel is defined - - __default_kernel.kernel is defined tags: tests::cleanup diff --git a/tests/tests_settings.yml b/tests/tests_settings.yml index d86fb335..4890e54c 100644 --- a/tests/tests_settings.yml +++ b/tests/tests_settings.yml @@ -1,4 +1,5 @@ # SPDX-License-Identifier: MIT +# Assisted by goose using model glm-5-3 --- - name: Test bootloader_settings variable hosts: all @@ -101,7 +102,7 @@ first) }}" assert: that: - - cloned_kernel != default_bootloader + - cloned_kernel.kernel != default_bootloader.kernel - >- (bootloader_facts | selectattr('kernel', 'search', cloned_kernel.kernel) | first).args | @@ -193,9 +194,11 @@ register: __bootloader_default_grub_content changed_when: false always: + # use the title to match the kernel: on platforms where grubby + # reports the extra /boot prefix it cannot match the kernel path - name: Remove cloned kernels command: >- - grubby --remove-kernel={{ __default_kernel.kernel }}_clone1 + grubby --remove-kernel=TITLE=Clone1 changed_when: true failed_when: false tags: tests::cleanup diff --git a/tests/unit/test_bootloader_common.py b/tests/unit/test_bootloader_common.py new file mode 100644 index 00000000..6d36d67d --- /dev/null +++ b/tests/unit/test_bootloader_common.py @@ -0,0 +1,256 @@ +# -*- coding: utf-8 -*- +# +# SPDX-License-Identifier: MIT +# Assisted by goose using model glm-5-3 +# +"""Unit tests for the code shared by the bootloader modules""" + +from __future__ import absolute_import, division, print_function + +__metaclass__ = type + +import unittest + +from ansible.module_utils.bootloader_lsr import bootloader_common + +# non linux entry: RHEL 7 might print such a message +INFO = """ +index=0 +kernel="/boot/vmlinuz-6.5.12-100.fc37.x86_64" +args="$tuned_params ro rootflags=subvol=root rd.luks.uuid=luks-9da1fdf5-14ac-49fd-a388-8b1ee48f3df1 rhgb quiet" +root="UUID=65c70529-e9ad-4778-9001-18fe8c525285" +initrd="/boot/initramfs-6.5.12-100.fc37.x86_64.img $tuned_initrd" +title="Fedora Linux (6.5.12-100.fc37.x86_64) 37 (Workstation Edition)" +id="c44543d15b2c4e898912c2497f734e67-6.5.12-100.fc37.x86_64" +index=1 +kernel="/boot/vmlinuz-6.5.10-100.fc37.x86_64" +args="ro rootflags=subvol=root rd.luks.uuid=luks-9da1fdf5-14ac-49fd-a388-8b1ee48f3df1 rhgb quiet $tuned_params" +root="UUID=65c70529-e9ad-4778-9001-18fe8c525285" +initrd="/boot/initramfs-6.5.10-100.fc37.x86_64.img $tuned_initrd" +title="Fedora Linux (6.5.10-100.fc37.x86_64) 37 (Workstation Edition)" +id="c44543d15b2c4e898912c2497f734e67-6.5.10-100.fc37.x86_64" +index=2 +kernel="/boot/vmlinuz-6.5.7-100.fc37.x86_64" +args="ro rootflags=subvol=root rd.luks.uuid=luks-9da1fdf5-14ac-49fd-a388-8b1ee48f3df1 rhgb quiet $tuned_params" +root="UUID=65c70529-e9ad-4778-9001-18fe8c525285" +initrd="/boot/initramfs-6.5.7-100.fc37.x86_64.img $tuned_initrd" +title="Fedora Linux (6.5.7-100.fc37.x86_64) 37 (Workstation Edition)" +id="c44543d15b2c4e898912c2497f734e67-6.5.7-100.fc37.x86_64" +index=3 +kernel="/boot/vmlinuz-0-rescue-c44543d15b2c4e898912c2497f734e67" +args="ro rootflags=subvol=root rd.luks.uuid=luks-9da1fdf5-14ac-49fd-a388-8b1ee48f3df1 rhgb quiet" +root="UUID=65c70529-e9ad-4778-9001-18fe8c525285" +initrd="/boot/initramfs-0-rescue-c44543d15b2c4e898912c2497f734e67.img" +title="Fedora Linux (0-rescue-c44543d15b2c4e898912c2497f734e67) 36 (Workstation Edition)" +id="c44543d15b2c4e898912c2497f734e67-0-rescue" +index=4 +non linux entry +""" + +FACTS = [ + { + "args": "$tuned_params ro rootflags=subvol=root rd.luks.uuid=luks-9da1fdf5-14ac-49fd-a388-8b1ee48f3df1 rhgb quiet", + "id": "c44543d15b2c4e898912c2497f734e67-6.5.12-100.fc37.x86_64", + "index": "0", + "initrd": "/boot/initramfs-6.5.12-100.fc37.x86_64.img $tuned_initrd", + "kernel": "/boot/vmlinuz-6.5.12-100.fc37.x86_64", + "root": "UUID=65c70529-e9ad-4778-9001-18fe8c525285", + "title": "Fedora Linux (6.5.12-100.fc37.x86_64) 37 (Workstation Edition)", + "default": False, + }, + { + "args": "ro rootflags=subvol=root rd.luks.uuid=luks-9da1fdf5-14ac-49fd-a388-8b1ee48f3df1 rhgb quiet $tuned_params", + "id": "c44543d15b2c4e898912c2497f734e67-6.5.10-100.fc37.x86_64", + "index": "1", + "initrd": "/boot/initramfs-6.5.10-100.fc37.x86_64.img $tuned_initrd", + "kernel": "/boot/vmlinuz-6.5.10-100.fc37.x86_64", + "root": "UUID=65c70529-e9ad-4778-9001-18fe8c525285", + "title": "Fedora Linux (6.5.10-100.fc37.x86_64) 37 (Workstation Edition)", + "default": False, + }, + { + "args": "ro rootflags=subvol=root rd.luks.uuid=luks-9da1fdf5-14ac-49fd-a388-8b1ee48f3df1 rhgb quiet $tuned_params", + "id": "c44543d15b2c4e898912c2497f734e67-6.5.7-100.fc37.x86_64", + "index": "2", + "initrd": "/boot/initramfs-6.5.7-100.fc37.x86_64.img $tuned_initrd", + "kernel": "/boot/vmlinuz-6.5.7-100.fc37.x86_64", + "root": "UUID=65c70529-e9ad-4778-9001-18fe8c525285", + "title": "Fedora Linux (6.5.7-100.fc37.x86_64) 37 (Workstation Edition)", + "default": True, + }, + { + "args": "ro rootflags=subvol=root rd.luks.uuid=luks-9da1fdf5-14ac-49fd-a388-8b1ee48f3df1 rhgb quiet", + "id": "c44543d15b2c4e898912c2497f734e67-0-rescue", + "index": "3", + "initrd": "/boot/initramfs-0-rescue-c44543d15b2c4e898912c2497f734e67.img", + "kernel": "/boot/vmlinuz-0-rescue-c44543d15b2c4e898912c2497f734e67", + "root": "UUID=65c70529-e9ad-4778-9001-18fe8c525285", + "title": "Fedora Linux (0-rescue-c44543d15b2c4e898912c2497f734e67) 36 (Workstation Edition)", + "default": False, + }, + {"index": "4", "kernel": "non linux entry", "default": False}, +] + +# some versions of grubby report the kernel and initrd paths with an extra +# /boot prefix +INFO_EXTRA_BOOT_PREFIX = """ +index=0 +kernel="/boot/boot/vmlinuz-7.2.10-200.fc44.x86_64" +args="ro rootflags=subvol=root rhgb quiet" +root="UUID=65c70529-e9ad-4778-9001-18fe8c525285" +initrd="/boot/boot/initramfs-7.2.10-200.fc44.x86_64.img $tuned_initrd" +title="Fedora Linux (7.2.10-200.fc44.x86_64) 44 (Cloud Edition)" +id="c44543d15b2c4e898912c2497f734e67-7.2.10-200.fc44.x86_64" +index=1 +kernel="/boot/boot/vmlinuz-7.2.8-200.fc44.x86_64" +args="ro rootflags=subvol=root rhgb quiet" +root="UUID=65c70529-e9ad-4778-9001-18fe8c525285" +initrd="/boot/boot/initramfs-7.2.8-200.fc44.x86_64.img $tuned_initrd" +title="Fedora Linux (7.2.8-200.fc44.x86_64) 44 (Cloud Edition)" +id="c44543d15b2c4e898912c2497f734e67-7.2.8-200.fc44.x86_64" +""" + +INFO_EXTRA_BOOT_PREFIX_RHEL7 = """ +index=0 +kernel=/boot/boot/vmlinuz-6.5.12-100.fc37.x86_64 +args="ro rootflags=subvol=root rhgb quiet" +root=UUID=65c70529-e9ad-4778-9001-18fe8c525285 +initrd=/boot/boot/initramfs-6.5.12-100.fc37.x86_64.img $tuned_initrd +title=Fedora Linux (6.5.12-100.fc37.x86_64) 37 (Workstation Edition) +id=c44543d15b2c4e898912c2497f734e67-6.5.12-100.fc37.x86_64 +""" + +FACTS_EXTRA_BOOT_PREFIX = [ + { + "args": "ro rootflags=subvol=root rhgb quiet", + "id": "c44543d15b2c4e898912c2497f734e67-7.2.10-200.fc44.x86_64", + "index": "0", + "initrd": "/boot/initramfs-7.2.10-200.fc44.x86_64.img $tuned_initrd", + "kernel": "/boot/vmlinuz-7.2.10-200.fc44.x86_64", + "root": "UUID=65c70529-e9ad-4778-9001-18fe8c525285", + "title": "Fedora Linux (7.2.10-200.fc44.x86_64) 44 (Cloud Edition)", + "default": True, + }, + { + "args": "ro rootflags=subvol=root rhgb quiet", + "id": "c44543d15b2c4e898912c2497f734e67-7.2.8-200.fc44.x86_64", + "index": "1", + "initrd": "/boot/initramfs-7.2.8-200.fc44.x86_64.img $tuned_initrd", + "kernel": "/boot/vmlinuz-7.2.8-200.fc44.x86_64", + "root": "UUID=65c70529-e9ad-4778-9001-18fe8c525285", + "title": "Fedora Linux (7.2.8-200.fc44.x86_64) 44 (Cloud Edition)", + "default": False, + }, +] + + +class GetFacts(unittest.TestCase): + """test get_facts""" + + def test_get_facts(self): + kernels = bootloader_common.get_facts(INFO, "2") + self.assertEqual( + FACTS, + kernels, + ) + + def test_get_facts_strips_extra_boot_prefix(self): + # the facts report the real paths, without the extra prefix + kernels = bootloader_common.get_facts(INFO_EXTRA_BOOT_PREFIX, "0") + self.assertEqual( + FACTS_EXTRA_BOOT_PREFIX, + kernels, + ) + + +class ExtraBootPrefix(unittest.TestCase): + """test handling of the extra /boot prefix that some versions of grubby add""" + + def test_strip_extra_boot_prefix(self): + # the extra prefix is stripped from kernel paths + self.assertEqual( + bootloader_common.strip_extra_boot_prefix( + "/boot/boot/vmlinuz-7.2.10-200.fc44.x86_64" + ), + "/boot/vmlinuz-7.2.10-200.fc44.x86_64", + ) + # the extra prefix is stripped from every path of the initrd value, + # other values are kept as is + self.assertEqual( + bootloader_common.strip_extra_boot_prefix( + "/boot/boot/initramfs-7.2.10-200.fc44.x86_64.img $tuned_initrd" + ), + "/boot/initramfs-7.2.10-200.fc44.x86_64.img $tuned_initrd", + ) + # paths without the extra prefix, and values without paths, are + # unchanged + self.assertEqual( + bootloader_common.strip_extra_boot_prefix( + "/boot/vmlinuz-7.2.10-200.fc44.x86_64" + ), + "/boot/vmlinuz-7.2.10-200.fc44.x86_64", + ) + self.assertEqual( + bootloader_common.strip_extra_boot_prefix("non linux entry"), + "non linux entry", + ) + self.assertEqual(bootloader_common.strip_extra_boot_prefix(""), "") + + def test_has_extra_boot_prefix(self): + self.assertTrue(bootloader_common.has_extra_boot_prefix(INFO_EXTRA_BOOT_PREFIX)) + # unquoted kernel paths, as RHEL 7 grubby reports them, are detected too + self.assertTrue( + bootloader_common.has_extra_boot_prefix(INFO_EXTRA_BOOT_PREFIX_RHEL7) + ) + self.assertFalse(bootloader_common.has_extra_boot_prefix(INFO)) + self.assertFalse(bootloader_common.has_extra_boot_prefix("")) + # kernel paths without the extra prefix are not detected, whether + # grubby reports them quoted or not + self.assertFalse( + bootloader_common.has_extra_boot_prefix( + 'kernel="/boot/vmlinuz-6.5.12-100.fc37.x86_64"\n' + ) + ) + self.assertFalse( + bootloader_common.has_extra_boot_prefix( + "kernel=/boot/vmlinuz-6.5.12-100.fc37.x86_64\n" + ) + ) + + def test_add_extra_boot_prefix(self): + # without the grubby issue the paths are used as is + self.assertEqual( + bootloader_common.add_extra_boot_prefix( + "/boot/vmlinuz-7.2.10-200.fc44.x86_64", False + ), + "/boot/vmlinuz-7.2.10-200.fc44.x86_64", + ) + # with the grubby issue the extra prefix is added to kernel paths + self.assertEqual( + bootloader_common.add_extra_boot_prefix( + "/boot/vmlinuz-7.2.10-200.fc44.x86_64", True + ), + "/boot/boot/vmlinuz-7.2.10-200.fc44.x86_64", + ) + # the extra prefix is added to every /boot path of the initrd value, + # other values are kept as is + self.assertEqual( + bootloader_common.add_extra_boot_prefix( + "/boot/initramfs-7.2.10-200.fc44.x86_64.img $tuned_initrd", True + ), + "/boot/boot/initramfs-7.2.10-200.fc44.x86_64.img $tuned_initrd", + ) + # paths without the /boot prefix, and paths that already have the + # extra prefix, are unchanged + self.assertEqual( + bootloader_common.add_extra_boot_prefix( + "/vmlinuz-7.2.10-200.fc44.x86_64", True + ), + "/vmlinuz-7.2.10-200.fc44.x86_64", + ) + self.assertEqual( + bootloader_common.add_extra_boot_prefix( + "/boot/boot/vmlinuz-7.2.10-200.fc44.x86_64", True + ), + "/boot/boot/vmlinuz-7.2.10-200.fc44.x86_64", + ) diff --git a/tests/unit/test_bootloader_facts.py b/tests/unit/test_bootloader_facts.py index 9441b028..0fa7f392 100644 --- a/tests/unit/test_bootloader_facts.py +++ b/tests/unit/test_bootloader_facts.py @@ -2,6 +2,7 @@ # Copyright: (c) 2023, Sergei Petrosian # SPDX-License-Identifier: GPL-2.0-or-later +# Assisted by goose using model glm-5-3 # """Unit tests for the bootloader_settings module""" diff --git a/tests/unit/test_bootloader_settings.py b/tests/unit/test_bootloader_settings.py index 0ebe4173..e80329fa 100644 --- a/tests/unit/test_bootloader_settings.py +++ b/tests/unit/test_bootloader_settings.py @@ -2,6 +2,7 @@ # Copyright: (c) 2023, Sergei Petrosian # SPDX-License-Identifier: GPL-2.0-or-later +# Assisted by goose using model glm-5-3 # """Unit tests for the bootloader_settings module""" @@ -170,6 +171,48 @@ id=c44543d15b2c4e898912c2497f734e67-6.5.12-100.fc37.x86_64 """ % changed_args +# some versions of grubby report the kernel and initrd paths with an extra +# /boot prefix +INFO_EXTRA_BOOT_PREFIX = """ +index=0 +kernel="/boot/boot/vmlinuz-7.2.10-200.fc44.x86_64" +args="ro rootflags=subvol=root rhgb quiet" +root="UUID=65c70529-e9ad-4778-9001-18fe8c525285" +initrd="/boot/boot/initramfs-7.2.10-200.fc44.x86_64.img $tuned_initrd" +title="Fedora Linux (7.2.10-200.fc44.x86_64) 44 (Cloud Edition)" +id="c44543d15b2c4e898912c2497f734e67-7.2.10-200.fc44.x86_64" +index=1 +kernel="/boot/boot/vmlinuz-7.2.8-200.fc44.x86_64" +args="ro rootflags=subvol=root rhgb quiet" +root="UUID=65c70529-e9ad-4778-9001-18fe8c525285" +initrd="/boot/boot/initramfs-7.2.8-200.fc44.x86_64.img $tuned_initrd" +title="Fedora Linux (7.2.8-200.fc44.x86_64) 44 (Cloud Edition)" +id="c44543d15b2c4e898912c2497f734e67-7.2.8-200.fc44.x86_64" +""" + +FACTS_EXTRA_BOOT_PREFIX = [ + { + "args": "ro rootflags=subvol=root rhgb quiet", + "id": "c44543d15b2c4e898912c2497f734e67-7.2.10-200.fc44.x86_64", + "index": "0", + "initrd": "/boot/initramfs-7.2.10-200.fc44.x86_64.img $tuned_initrd", + "kernel": "/boot/vmlinuz-7.2.10-200.fc44.x86_64", + "root": "UUID=65c70529-e9ad-4778-9001-18fe8c525285", + "title": "Fedora Linux (7.2.10-200.fc44.x86_64) 44 (Cloud Edition)", + "default": True, + }, + { + "args": "ro rootflags=subvol=root rhgb quiet", + "id": "c44543d15b2c4e898912c2497f734e67-7.2.8-200.fc44.x86_64", + "index": "1", + "initrd": "/boot/initramfs-7.2.8-200.fc44.x86_64.img $tuned_initrd", + "kernel": "/boot/vmlinuz-7.2.8-200.fc44.x86_64", + "root": "UUID=65c70529-e9ad-4778-9001-18fe8c525285", + "title": "Fedora Linux (7.2.8-200.fc44.x86_64) 44 (Cloud Edition)", + "default": False, + }, +] + kernels_keys = ["kernel_index", "kernel_path", "kernel_title", "DEFAULT", "ALL"] @@ -1196,3 +1239,274 @@ def run_command(cmd, **kwargs): result = module.exit_json.call_args[1] self.assertEqual(changed, result["changed"]) self.assertEqual(changed, result["reboot_required"]) + + +class ExtraBootPrefix(unittest.TestCase): + """test handling of the extra /boot prefix that some versions of grubby add""" + + mock_module = MagicMock( + run_command=MagicMock(return_value=("test_rc", "test_stdout", "test_err")), + fail_json=MagicMock(side_effect=SystemExit), + check_mode=False, + ) + result = dict(changed=False, actions=list()) + + def reset_vars(self): + self.mock_module.run_command.reset_mock() + self.mock_module.fail_json.reset_mock() + self.mock_module.check_mode = False + self.result = dict(changed=False, actions=list()) + + def test_get_single_kernel_extra_boot_prefix(self): + # kernel paths get the extra prefix, to modify or remove the kernel + kernel = {"path": "/boot/vmlinuz-7.2.10-200.fc44.x86_64"} + self.assertEqual( + bootloader_settings.get_single_kernel(kernel), + "/boot/vmlinuz-7.2.10-200.fc44.x86_64", + ) + self.assertEqual( + bootloader_settings.get_single_kernel(kernel, True), + "/boot/boot/vmlinuz-7.2.10-200.fc44.x86_64", + ) + # titles and indexes are used as is + self.assertEqual( + bootloader_settings.get_single_kernel({"title": "Fedora Linux"}, True), + "TITLE='Fedora Linux'", + ) + self.assertEqual( + bootloader_settings.get_single_kernel({"index": 1}, True), + "1", + ) + + def test_get_create_kernel_extra_boot_prefix(self): + # kernel and initrd paths get the extra prefix, to create the kernel + kernel = { + "title": "Fedora Linux", + "path": "/boot/vmlinuz-7.2.10-200.fc44.x86_64", + "initrd": "/boot/initramfs-7.2.10-200.fc44.x86_64.img", + } + self.assertEqual( + bootloader_settings.get_create_kernel(kernel), + "--initrd=/boot/initramfs-7.2.10-200.fc44.x86_64.img " + "--add-kernel=/boot/vmlinuz-7.2.10-200.fc44.x86_64 " + "--title='Fedora Linux'", + ) + self.assertEqual( + bootloader_settings.get_create_kernel(kernel, True), + "--initrd=/boot/boot/initramfs-7.2.10-200.fc44.x86_64.img " + "--add-kernel=/boot/boot/vmlinuz-7.2.10-200.fc44.x86_64 " + "--title='Fedora Linux'", + ) + + def test_validate_kernels_extra_boot_prefix(self): + self.reset_vars() + # a single path: the kernel is modified, and the grubby command gets + # the extra prefix + kernel_action, kernel = bootloader_settings.validate_kernels( + self.mock_module, + {"kernel": {"path": "/boot/vmlinuz-7.2.10-200.fc44.x86_64"}}, + FACTS_EXTRA_BOOT_PREFIX, + True, + ) + self.mock_module.fail_json.assert_not_called() + self.assertEqual(kernel_action, "modify") + self.assertEqual(kernel, "/boot/boot/vmlinuz-7.2.10-200.fc44.x86_64") + self.reset_vars() + + # the stripped facts match the paths of the user settings, so the + # kernel is modified and not created again + kernel_action, kernel = bootloader_settings.validate_kernels( + self.mock_module, + { + "kernel": { + "path": "/boot/vmlinuz-7.2.10-200.fc44.x86_64", + "title": "Fedora Linux (7.2.10-200.fc44.x86_64) 44 (Cloud Edition)", + } + }, + FACTS_EXTRA_BOOT_PREFIX, + True, + ) + self.mock_module.fail_json.assert_not_called() + self.assertEqual(kernel_action, "modify") + self.reset_vars() + + # a kernel that no fact matches is created, with the extra prefix in + # the kernel and initrd paths of the grubby command + kernel_action, kernel = bootloader_settings.validate_kernels( + self.mock_module, + { + "kernel": { + "path": "/boot/vmlinuz-new", + "title": "New Kernel", + "initrd": "/boot/initramfs-new.img", + } + }, + FACTS_EXTRA_BOOT_PREFIX, + True, + ) + self.mock_module.fail_json.assert_not_called() + self.assertEqual(kernel_action, "create") + self.assertEqual( + kernel, + "--initrd=/boot/boot/initramfs-new.img " + "--add-kernel=/boot/boot/vmlinuz-new --title='New Kernel'", + ) + + def test_get_default_kernel_strips_extra_boot_prefix(self): + self.reset_vars() + self.mock_module.run_command.return_value = ( + "0", + "/boot/boot/vmlinuz-7.2.10-200.fc44.x86_64", + "", + ) + self.assertEqual( + bootloader_settings.get_default_kernel(self.mock_module, "kernel"), + "/boot/vmlinuz-7.2.10-200.fc44.x86_64", + ) + + def test_mod_default_kernel_extra_boot_prefix(self): + self.reset_vars() + kernel_info = ''' +index=0 +kernel="/boot/boot/vmlinuz-7.2.10-200.fc44.x86_64" +args="ro rootflags=subvol=root rhgb quiet" +root="UUID=65c70529-e9ad-4778-9001-18fe8c525285" +initrd="/boot/boot/initramfs-7.2.10-200.fc44.x86_64.img" +title="Fedora Linux (7.2.10-200.fc44.x86_64) 44 (Cloud Edition)" +id="c44543d15b2c4e898912c2497f734e67-7.2.10-200.fc44.x86_64"''' + bootloader_setting = { + "kernel": {"path": "/boot/vmlinuz-7.2.10-200.fc44.x86_64"}, + "default": True, + } + + # the current default kernel, which grubby reports with the extra + # prefix, is another kernel: set this kernel as default; grubby does + # not strip the extra prefix from the --set-default value, so the + # grubby command uses the real kernel path + self.mock_module.run_command.return_value = ( + "0", + "/boot/boot/vmlinuz-7.2.8-200.fc44.x86_64", + "", + ) + bootloader_settings.mod_default_kernel( + self.mock_module, self.result, bootloader_setting, kernel_info + ) + self.mock_module.run_command.assert_any_call("grubby --default-kernel") + self.mock_module.run_command.assert_any_call( + "grubby --set-default=/boot/vmlinuz-7.2.10-200.fc44.x86_64" + ) + self.assertEqual( + self.result["actions"], + ["grubby --set-default=/boot/vmlinuz-7.2.10-200.fc44.x86_64"], + ) + self.reset_vars() + + # the current default kernel is this kernel, which grubby reports + # with the extra prefix: nothing is changed + self.mock_module.run_command.return_value = ( + "0", + "/boot/boot/vmlinuz-7.2.10-200.fc44.x86_64", + "", + ) + bootloader_settings.mod_default_kernel( + self.mock_module, self.result, bootloader_setting, kernel_info + ) + self.mock_module.run_command.assert_called_once_with("grubby --default-kernel") + self.assertEqual(self.result["actions"], []) + + def test_run_module_extra_boot_prefix(self): + """grubby commands that grubby strips the /boot prefix from use the + extra /boot prefix when grubby reports one; the other commands use + the real kernel path""" + + def run_command(cmd, **kwargs): + if cmd == "grubby --default-index": + return 0, "0", "" + if cmd == "grubby --default-kernel": + # another kernel is the current default, with the extra + # prefix that grubby reports + return 0, "/boot/boot/vmlinuz-7.2.8-200.fc44.x86_64", "" + if cmd == "grubby --info=ALL": + return 0, INFO_EXTRA_BOOT_PREFIX, "" + if cmd.startswith("grubby --info="): + # grubby does not strip the extra /boot prefix from the + # --info value, so the module passes the real kernel path + self.assertEqual( + cmd, "grubby --info=/boot/vmlinuz-7.2.10-200.fc44.x86_64" + ) + return 0, INFO_EXTRA_BOOT_PREFIX, "" + return 0, "", "" + + for bootloader_setting, actions in [ + ( + # modify: the path of the user setting matches the stripped + # fact path, and the grubby command gets the extra prefix + { + "kernel": {"path": "/boot/vmlinuz-7.2.10-200.fc44.x86_64"}, + "options": [{"name": "debug"}], + }, + [ + "grubby --update-kernel=/boot/boot/vmlinuz-7.2.10-200.fc44.x86_64 " + "--args=debug" + ], + ), + ( + # an argument that the kernel already has changes nothing + { + "kernel": {"path": "/boot/vmlinuz-7.2.10-200.fc44.x86_64"}, + "options": [{"name": "rhgb"}], + }, + [], + ), + ( + # modify with default: grubby does not strip the extra + # prefix from the --set-default value, so it uses the real + # kernel path + { + "kernel": {"path": "/boot/vmlinuz-7.2.10-200.fc44.x86_64"}, + "default": True, + }, + ["grubby --set-default=/boot/vmlinuz-7.2.10-200.fc44.x86_64"], + ), + ( + # remove: the grubby command gets the extra prefix + { + "kernel": {"path": "/boot/vmlinuz-7.2.10-200.fc44.x86_64"}, + "state": "absent", + }, + ["grubby --remove-kernel=/boot/boot/vmlinuz-7.2.10-200.fc44.x86_64"], + ), + ( + # create: the kernel and initrd paths of the grubby command + # get the extra prefix + { + "kernel": { + "path": "/boot/vmlinuz-new", + "title": "New Kernel", + "initrd": "/boot/initramfs-new.img", + }, + "options": [{"name": "debug"}], + }, + [ + "grubby --initrd=/boot/boot/initramfs-new.img " + "--add-kernel=/boot/boot/vmlinuz-new --title='New Kernel' " + "--args=debug" + ], + ), + ]: + module = MagicMock(check_mode=False) + module.exit_json.side_effect = SystemExit + module.run_command.side_effect = run_command + module.params = { + "bootloader_settings": [bootloader_setting], + "backend": "grubby", + } + with patch.object( + bootloader_settings, "AnsibleModule", return_value=module + ): + with self.assertRaises(SystemExit): + bootloader_settings.run_module() + result = module.exit_json.call_args[1] + self.assertEqual(actions, result["actions"]) + self.assertEqual(bool(actions), result["changed"]) + self.assertEqual(bool(actions), result["reboot_required"]) From ece536d42a2b24060ddf6bc2957df982e9dd19da Mon Sep 17 00:00:00 2001 From: Rich Megginson Date: Wed, 18 Nov 2026 10:00:03 -0700 Subject: [PATCH 2/7] fix: normalize the kernel paths of user settings before matching facts Cause: validate_kernels compared the path and initrd values of the user settings verbatim with the facts, which bootloader_facts and bootloader_settings report with the extra /boot prefix stripped; a user setting with a path that grubby reported with the extra prefix (for example /boot/boot/vmlinuz-a, copied from the raw grubby output) did not match the corresponding fact. Consequences: A multi-key kernel setting with such a path failed with "A kernel with provided [...] already exists and its other fields are different" instead of modifying the existing kernel, and the path with the extra prefix was passed verbatim to the grubby commands on the platforms that grubby does not report the extra prefix for, where grubby cannot match it. Fix: validate_kernels now normalizes the path and initrd values of the user settings with strip_extra_boot_prefix, the same function used to normalize the paths that grubby reports, so that /boot/boot/vmlinuz-a and /boot/vmlinuz-a match the same entry, including in multi-key settings; add_extra_boot_prefix then adds the extra prefix back to the grubby commands on the platforms where it is needed. Result: Kernel settings whose paths have the extra /boot prefix that grubby reports on some platforms now match and modify the same boot entries as settings with the real paths. The unit tests (82) and the Fedora 44 qemu integration tests pass. Signed-off-by: Rich Megginson Assisted-by: goose using model glm-5-3 Signed-off-by: Rich Megginson --- library/bootloader_settings.py | 10 +++ tests/unit/test_bootloader_settings.py | 103 +++++++++++++++++++++++++ 2 files changed, 113 insertions(+) diff --git a/library/bootloader_settings.py b/library/bootloader_settings.py index 05936187..564c8711 100644 --- a/library/bootloader_settings.py +++ b/library/bootloader_settings.py @@ -270,6 +270,16 @@ def validate_kernels( msg="kernel value in '%s: %s' must be of type str or int" % (key, value) ) + # Normalize the path and initrd values of the user setting with the same + # rules used for the paths that grubby reports, so that a value with the + # extra /boot prefix that grubby adds on some platforms matches the + # normalized path of the corresponding fact; the extra prefix is added + # back to the grubby commands, where needed, by add_extra_boot_prefix + for key in ["path", "initrd"]: + value = bootloader_setting["kernel"].get(key) + if isinstance(value, str): + bootloader_setting["kernel"][key] = strip_extra_boot_prefix(value) + # Validate with len(bootloader_setting["kernel"]) == 1 if len(bootloader_setting["kernel"]) == 1: validate_kernel_initrd(module, bootloader_setting["kernel"], kernel_mod_keys) diff --git a/tests/unit/test_bootloader_settings.py b/tests/unit/test_bootloader_settings.py index e80329fa..7f754a73 100644 --- a/tests/unit/test_bootloader_settings.py +++ b/tests/unit/test_bootloader_settings.py @@ -10,6 +10,7 @@ __metaclass__ = type +import copy import unittest try: @@ -1352,6 +1353,96 @@ def test_validate_kernels_extra_boot_prefix(self): "--add-kernel=/boot/boot/vmlinuz-new --title='New Kernel'", ) + def test_validate_kernels_normalizes_setting_paths(self): + """User paths with the extra /boot prefix match the same facts as the + real paths""" + facts = copy.deepcopy(FACTS_EXTRA_BOOT_PREFIX) + + # a single path with the extra prefix on a platform that grubby + # reports the extra prefix for: the kernel is modified, and the + # grubby command gets the extra prefix + kernel_action, kernel = bootloader_settings.validate_kernels( + self.mock_module, + {"kernel": {"path": "/boot/boot/vmlinuz-7.2.10-200.fc44.x86_64"}}, + facts, + True, + ) + self.mock_module.fail_json.assert_not_called() + self.assertEqual(kernel_action, "modify") + self.assertEqual(kernel, "/boot/boot/vmlinuz-7.2.10-200.fc44.x86_64") + self.reset_vars() + + # the same single path on a platform that grubby does not report + # the extra prefix for: the grubby command uses the real path + kernel_action, kernel = bootloader_settings.validate_kernels( + self.mock_module, + {"kernel": {"path": "/boot/boot/vmlinuz-7.2.10-200.fc44.x86_64"}}, + facts, + False, + ) + self.mock_module.fail_json.assert_not_called() + self.assertEqual(kernel_action, "modify") + self.assertEqual(kernel, "/boot/vmlinuz-7.2.10-200.fc44.x86_64") + self.reset_vars() + + # a path with the extra prefix and the title of the same fact: the + # setting matches the fact, so the kernel is modified and no error + # about different fields is reported + kernel_action, kernel = bootloader_settings.validate_kernels( + self.mock_module, + { + "kernel": { + "path": "/boot/boot/vmlinuz-7.2.10-200.fc44.x86_64", + "title": "Fedora Linux (7.2.10-200.fc44.x86_64) " + "44 (Cloud Edition)", + } + }, + facts, + True, + ) + self.mock_module.fail_json.assert_not_called() + self.assertEqual(kernel_action, "modify") + self.assertEqual(kernel, "/boot/boot/vmlinuz-7.2.10-200.fc44.x86_64") + self.reset_vars() + + # a path with the extra prefix and a new title: the kernel is + # created, with the extra prefix added once to the paths of the + # grubby command + kernel_action, kernel = bootloader_settings.validate_kernels( + self.mock_module, + { + "kernel": { + "path": "/boot/boot/vmlinuz-new", + "title": "New Kernel", + "initrd": "/boot/boot/initramfs-new.img", + } + }, + facts, + True, + ) + self.mock_module.fail_json.assert_not_called() + self.assertEqual(kernel_action, "create") + self.assertEqual( + kernel, + "--initrd=/boot/boot/initramfs-new.img " + "--add-kernel=/boot/boot/vmlinuz-new --title='New Kernel'", + ) + self.reset_vars() + + # remove a kernel by a path with the extra prefix + kernel_action, kernel = bootloader_settings.validate_kernels( + self.mock_module, + { + "kernel": {"path": "/boot/boot/vmlinuz-7.2.10-200.fc44.x86_64"}, + "state": "absent", + }, + facts, + True, + ) + self.mock_module.fail_json.assert_not_called() + self.assertEqual(kernel_action, "remove") + self.assertEqual(kernel, "/boot/boot/vmlinuz-7.2.10-200.fc44.x86_64") + def test_get_default_kernel_strips_extra_boot_prefix(self): self.reset_vars() self.mock_module.run_command.return_value = ( @@ -1450,6 +1541,18 @@ def run_command(cmd, **kwargs): "--args=debug" ], ), + ( + # modify: the path of the user setting has the extra prefix, + # and still matches the stripped fact path + { + "kernel": {"path": "/boot/boot/vmlinuz-7.2.10-200.fc44.x86_64"}, + "options": [{"name": "debug"}], + }, + [ + "grubby --update-kernel=/boot/boot/vmlinuz-7.2.10-200.fc44.x86_64 " + "--args=debug" + ], + ), ( # an argument that the kernel already has changes nothing { From 25541005662844075b813cc95aa6bfd1790991e1 Mon Sep 17 00:00:00 2001 From: Rich Megginson Date: Wed, 18 Nov 2026 10:02:38 -0700 Subject: [PATCH 3/7] docs: fix the list of grubby arguments that need the extra /boot prefix The add_extra_boot_prefix docstring claimed that grubby expects the extra /boot prefix in the paths passed to --info and --set-default too, but those arguments require the real kernel path: grubby only strips the prefix from the values of --add-kernel, --initrd, --remove-kernel and --update-kernel, so only those need the extra prefix on the platforms affected by the grubby issue. Signed-off-by: Rich Megginson Assisted-by: goose using model glm-5-3 Signed-off-by: Rich Megginson --- module_utils/bootloader_lsr/bootloader_common.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/module_utils/bootloader_lsr/bootloader_common.py b/module_utils/bootloader_lsr/bootloader_common.py index dfbd3049..eaef47f4 100644 --- a/module_utils/bootloader_lsr/bootloader_common.py +++ b/module_utils/bootloader_lsr/bootloader_common.py @@ -69,10 +69,11 @@ def add_extra_boot_prefix(value, extra_boot_prefix): On the platforms where grubby reports the paths with an extra /boot prefix, grubby also expects the extra prefix in the paths passed to its - --add-kernel, --initrd, --info, --remove-kernel, --set-default and - --update-kernel arguments. The value can be a space separated list of - paths, as the initrd value can be, so the extra prefix is added to every - /boot path of the list. + --add-kernel, --initrd, --remove-kernel and --update-kernel arguments, + whose values grubby itself strips the prefix from; the other arguments, + like --info and --set-default, require the real kernel path instead. + The value can be a space separated list of paths, as the initrd value + can be, so the extra prefix is added to every /boot path of the list. """ if not extra_boot_prefix: return value From a4be85906cfbbc4d05f1dac93c8016942882d2e0 Mon Sep 17 00:00:00 2001 From: Rich Megginson Date: Wed, 18 Nov 2026 10:18:06 -0700 Subject: [PATCH 4/7] fix: strip the extra /boot prefix only from path selectors of grubby --info Cause: The 'grubby --info=' commands applied strip_extra_boot_prefix to the kernel selector unconditionally. The title, index, ALL and DEFAULT selectors are not paths, so the function was a no-op for them in practice, but applying it to them is fragile: a title selector that happens to contain a path with the extra /boot prefix would be mangled. Consequences: A title selector passed to 'grubby --info=' could fail to match the corresponding entry if it contained a token that strip_extra_boot_prefix modifies. Fix: Add the get_info_kernel helper, which strips the extra /boot prefix only when the kernel selector is an absolute path, and use it for both 'grubby --info=' read paths, so that title and other selectors are passed through unchanged. Result: Only path selectors are normalized for 'grubby --info=', and title selectors keep matching the corresponding entries. The unit tests (83) and the Fedora 44 qemu integration tests pass. Signed-off-by: Rich Megginson Assisted-by: goose using model glm-5-3 Signed-off-by: Rich Megginson --- library/bootloader_settings.py | 17 ++++++++-- tests/unit/test_bootloader_settings.py | 47 ++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/library/bootloader_settings.py b/library/bootloader_settings.py index 564c8711..e2f16cd6 100644 --- a/library/bootloader_settings.py +++ b/library/bootloader_settings.py @@ -515,6 +515,19 @@ def get_default_kernel(module, type): return stdout.strip() +def get_info_kernel(kernel): + """Get the kernel selector in the format expected by 'grubby --info=' + + grubby does not strip the extra /boot prefix from the --info value, so + the extra prefix is stripped from the path selectors, to pass the real + kernel path; the title, index and other selectors are not paths, so + they are used as is, to keep matching the corresponding entries. + """ + if kernel.startswith("/"): + return strip_extra_boot_prefix(kernel) + return kernel + + def get_replaced_args(bootloader_setting_options): """Get the sorted list of desired arg tokens for a replacement check.""" tokens = [] @@ -610,7 +623,7 @@ def run_module(): # grubby does not strip the extra /boot prefix from the --info # value, so it needs the real kernel path rc, kernel_info, stderr = module.run_command( - "grubby --info=" + strip_extra_boot_prefix(kernel) + "grubby --info=" + get_info_kernel(kernel) ) if needs_replacement(bootloader_setting.get("options", []), kernel_info): rm_boot_args(module, result, kernel_info, kernel) @@ -624,7 +637,7 @@ def run_module(): # grubby does not strip the extra /boot prefix from the --info # value, so it needs the real kernel path _unused, kernel_info, _unused = module.run_command( - "grubby --info=" + strip_extra_boot_prefix(kernel) + "grubby --info=" + get_info_kernel(kernel) ) mod_boot_args(module, result, bootloader_setting, kernel, kernel_info) diff --git a/tests/unit/test_bootloader_settings.py b/tests/unit/test_bootloader_settings.py index 7f754a73..0b88e288 100644 --- a/tests/unit/test_bootloader_settings.py +++ b/tests/unit/test_bootloader_settings.py @@ -1455,6 +1455,30 @@ def test_get_default_kernel_strips_extra_boot_prefix(self): "/boot/vmlinuz-7.2.10-200.fc44.x86_64", ) + def test_get_info_kernel(self): + """Only path selectors are stripped of the extra /boot prefix for + 'grubby --info='; the other selectors are used as is""" + # path selectors: the extra prefix is stripped, and paths without + # it are used as is + self.assertEqual( + bootloader_settings.get_info_kernel( + "/boot/boot/vmlinuz-7.2.10-200.fc44.x86_64" + ), + "/boot/vmlinuz-7.2.10-200.fc44.x86_64", + ) + self.assertEqual( + bootloader_settings.get_info_kernel("/boot/vmlinuz-7.2.10-200.fc44.x86_64"), + "/boot/vmlinuz-7.2.10-200.fc44.x86_64", + ) + # title, index, ALL and DEFAULT selectors are used as is + self.assertEqual( + bootloader_settings.get_info_kernel("TITLE='Fedora Linux'"), + "TITLE='Fedora Linux'", + ) + self.assertEqual(bootloader_settings.get_info_kernel("1"), "1") + self.assertEqual(bootloader_settings.get_info_kernel("ALL"), "ALL") + self.assertEqual(bootloader_settings.get_info_kernel("DEFAULT"), "DEFAULT") + def test_mod_default_kernel_extra_boot_prefix(self): self.reset_vars() kernel_info = ''' @@ -1519,6 +1543,14 @@ def run_command(cmd, **kwargs): return 0, "/boot/boot/vmlinuz-7.2.8-200.fc44.x86_64", "" if cmd == "grubby --info=ALL": return 0, INFO_EXTRA_BOOT_PREFIX, "" + if cmd.startswith("grubby --info=TITLE="): + # title selectors are passed to grubby --info as is + self.assertEqual( + cmd, + "grubby --info=TITLE='Fedora Linux (7.2.10-200.fc44.x86_64) " + "44 (Cloud Edition)'", + ) + return 0, INFO_EXTRA_BOOT_PREFIX, "" if cmd.startswith("grubby --info="): # grubby does not strip the extra /boot prefix from the # --info value, so the module passes the real kernel path @@ -1553,6 +1585,21 @@ def run_command(cmd, **kwargs): "--args=debug" ], ), + ( + # modify by title: the title selector is passed to + # 'grubby --info=' as is, and the grubby command keeps it + { + "kernel": { + "title": "Fedora Linux (7.2.10-200.fc44.x86_64) " + "44 (Cloud Edition)" + }, + "options": [{"name": "debug"}], + }, + [ + "grubby --update-kernel=TITLE='Fedora Linux " + "(7.2.10-200.fc44.x86_64) 44 (Cloud Edition)' --args=debug" + ], + ), ( # an argument that the kernel already has changes nothing { From 7c5250b29ec694a61af8c70b6a01730f345ce183 Mon Sep 17 00:00:00 2001 From: Rich Megginson Date: Wed, 18 Nov 2026 10:36:55 -0700 Subject: [PATCH 5/7] fix: strip the extra /boot prefix from the raw path of grubby --info selectors Cause: The kernel selectors that reach the 'grubby --info=' commands are quoted by escapeval, so a path with characters that need shell quoting, like a space, arrives as a single-quoted string; get_info_kernel only stripped the extra /boot prefix from selectors starting with a slash, so the quoted path selectors were passed through with the extra prefix. Consequences: On the platforms where grubby reports the extra /boot prefix, 'grubby --info=' received the doubled path for a kernel path that needs quoting, and could not match the corresponding entry. Fix: get_info_kernel now recognizes the quoted path selectors too, and strips the extra /boot prefix from the raw, unquoted kernel path, with shlex.split, before quoting the result again with escapeval; the doubled, quoted paths used by the --add-kernel, --initrd, --update-kernel and --remove-kernel writes are unchanged. Result: The 'grubby --info=' commands pass the real kernel path for every path selector, quoted or not, while the write commands keep the doubled, quoted paths. The unit tests (83) and the Fedora 44 qemu integration tests pass. Signed-off-by: Rich Megginson Assisted-by: goose using model glm-5-3 Signed-off-by: Rich Megginson --- library/bootloader_settings.py | 13 +++++++--- tests/unit/test_bootloader_settings.py | 35 ++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/library/bootloader_settings.py b/library/bootloader_settings.py index e2f16cd6..9375bbcb 100644 --- a/library/bootloader_settings.py +++ b/library/bootloader_settings.py @@ -104,6 +104,7 @@ """ import re +import shlex from ansible.module_utils.basic import AnsibleModule @@ -521,11 +522,15 @@ def get_info_kernel(kernel): grubby does not strip the extra /boot prefix from the --info value, so the extra prefix is stripped from the path selectors, to pass the real kernel path; the title, index and other selectors are not paths, so - they are used as is, to keep matching the corresponding entries. + they are used as is, to keep matching the corresponding entries. The + selector for a path is quoted, so the extra prefix is stripped from + the raw, unquoted path, and the result is quoted again. """ - if kernel.startswith("/"): - return strip_extra_boot_prefix(kernel) - return kernel + if not kernel.startswith("/") and not kernel.startswith("'"): + return kernel + # the selector is a path, which escapeval may have quoted + raw_kernel = shlex.split(kernel)[0] + return escapeval(strip_extra_boot_prefix(raw_kernel)) def get_replaced_args(bootloader_setting_options): diff --git a/tests/unit/test_bootloader_settings.py b/tests/unit/test_bootloader_settings.py index 0b88e288..d518c033 100644 --- a/tests/unit/test_bootloader_settings.py +++ b/tests/unit/test_bootloader_settings.py @@ -1470,6 +1470,20 @@ def test_get_info_kernel(self): bootloader_settings.get_info_kernel("/boot/vmlinuz-7.2.10-200.fc44.x86_64"), "/boot/vmlinuz-7.2.10-200.fc44.x86_64", ) + # quoted path selectors: the extra prefix is stripped from the + # raw, unquoted path, and the result is quoted again + self.assertEqual( + bootloader_settings.get_info_kernel("'/boot/boot/my kernel'"), + "'/boot/my kernel'", + ) + self.assertEqual( + bootloader_settings.get_info_kernel("'/boot/my kernel'"), + "'/boot/my kernel'", + ) + self.assertEqual( + bootloader_settings.get_info_kernel("'/boot/boot/vmlinuz-a'"), + "/boot/vmlinuz-a", + ) # title, index, ALL and DEFAULT selectors are used as is self.assertEqual( bootloader_settings.get_info_kernel("TITLE='Fedora Linux'"), @@ -1551,6 +1565,12 @@ def run_command(cmd, **kwargs): "44 (Cloud Edition)'", ) return 0, INFO_EXTRA_BOOT_PREFIX, "" + if cmd.startswith("grubby --info='"): + # a quoted path selector: the module passes the real kernel + # path, with the extra prefix stripped from the raw, + # unquoted path, quoted again + self.assertEqual(cmd, "grubby --info='/boot/vmlinuz with space'") + return 0, INFO_EXTRA_BOOT_PREFIX, "" if cmd.startswith("grubby --info="): # grubby does not strip the extra /boot prefix from the # --info value, so the module passes the real kernel path @@ -1600,6 +1620,21 @@ def run_command(cmd, **kwargs): "(7.2.10-200.fc44.x86_64) 44 (Cloud Edition)' --args=debug" ], ), + ( + # modify a path with a space: the selector is quoted; the + # 'grubby --info=' command gets the real path, stripped of + # the extra prefix from the raw, unquoted path and quoted + # again, while the write command keeps the doubled, quoted + # path + { + "kernel": {"path": "/boot/boot/vmlinuz with space"}, + "options": [{"name": "debug"}], + }, + [ + "grubby --update-kernel='/boot/boot/vmlinuz with space' " + "--args=debug" + ], + ), ( # an argument that the kernel already has changes nothing { From a94200c306049b96e0a7d96c008e8879e6ed1326 Mon Sep 17 00:00:00 2001 From: Rich Megginson Date: Wed, 18 Nov 2026 11:06:37 -0700 Subject: [PATCH 6/7] fix: handle the extra /boot prefix for each kernel entry grubby reports Cause: bootloader_settings decided once, with has_extra_boot_prefix(), whether grubby reports the kernel paths of the whole 'grubby --info=ALL' listing with an extra /boot prefix, and applied the extra prefix to the paths of all the grubby write commands accordingly. grubby can report the paths of only some of the entries with the extra prefix, for example when the BootLoaderSpec entries store some of the kernel paths with the /boot prefix and others without it, so on such platforms the extra prefix was also added to the real paths of the entries that grubby reports without it, and grubby could not match those paths. Consequences: On platforms with a mixed 'grubby --info=ALL' listing, the --add-kernel, --initrd, --update-kernel and --remove-kernel commands failed to match the entries whose reported path does not have the extra prefix, for example /boot/vmlinuz-b, so modifying or removing those kernels silently did nothing. Fix: Replace has_extra_boot_prefix() with get_extra_boot_prefix_paths(), which returns the set of the real paths of the entries whose reported path has the extra prefix; add_extra_boot_prefix() is now applied, for each kernel entry, only to the paths of those entries, leaving the real paths of the other entries unchanged. A new kernel has no reported path yet, so its paths follow the convention of the existing entries: they get the extra prefix when grubby reports the path of any existing entry with it. Result: The extra /boot prefix is added only to the kernel paths that grubby reports with it, and the real paths of the other entries are passed to grubby unchanged, so the role works on platforms with a mixed listing too. The unit tests (84) and the Fedora 44 qemu integration tests pass. Signed-off-by: Rich Megginson Assisted-by: goose using model glm-5-3 Signed-off-by: Rich Megginson --- library/bootloader_settings.py | 46 ++-- .../bootloader_lsr/bootloader_common.py | 44 ++-- tests/unit/test_bootloader_common.py | 66 ++++-- tests/unit/test_bootloader_settings.py | 204 ++++++++++++++++-- 4 files changed, 293 insertions(+), 67 deletions(-) diff --git a/library/bootloader_settings.py b/library/bootloader_settings.py index 9375bbcb..377b1c41 100644 --- a/library/bootloader_settings.py +++ b/library/bootloader_settings.py @@ -114,8 +114,8 @@ from ansible.module_utils.bootloader_lsr import pbl from ansible.module_utils.bootloader_lsr.bootloader_common import ( add_extra_boot_prefix, + get_extra_boot_prefix_paths, get_facts, - has_extra_boot_prefix, strip_extra_boot_prefix, ) @@ -156,20 +156,31 @@ def get_kernel_to_mod(bootloader_setting_kernel, kernel_mod_keys): } -def get_single_kernel(bootloader_setting_kernel, extra_boot_prefix=False): +def get_single_kernel(bootloader_setting_kernel, extra_boot_prefix_paths=frozenset()): """Get kernel in the format expected by 'grubby --update-kernel=' from a one-element dict""" kernel_key, kernel_val = list(bootloader_setting_kernel.items())[0] kernel_key_prefix = "" if kernel_key == "title": kernel_key_prefix = "TITLE=" if kernel_key == "path": - kernel_val = add_extra_boot_prefix(kernel_val, extra_boot_prefix) + # add the extra prefix only when grubby reports the path of this + # kernel with it; grubby cannot match the real paths of the entries + # that it reports with the extra prefix, and it cannot match the + # paths with the extra prefix of the entries that it reports with + # the real paths + kernel_val = add_extra_boot_prefix( + kernel_val, kernel_val in extra_boot_prefix_paths + ) return kernel_key_prefix + escapeval(kernel_val) -def get_create_kernel(bootloader_setting_kernel, extra_boot_prefix=False): +def get_create_kernel(bootloader_setting_kernel, extra_boot_prefix_paths=frozenset()): """Get kernel in the format expected by 'grubby --add-kernel=' from a multiple-element dict""" kernel = "" + # a new kernel has no reported path yet, so it follows the convention of + # the existing entries: apply the extra prefix when grubby reports the + # path of any existing entry with it + extra_boot_prefix = bool(extra_boot_prefix_paths) for key in sorted(bootloader_setting_kernel.keys()): value = bootloader_setting_kernel[key] if key == "path": @@ -214,7 +225,7 @@ def validate_default_kernel(module, bootloader_settings): def validate_kernels( - module, bootloader_setting, bootloader_facts, extra_boot_prefix=False + module, bootloader_setting, bootloader_facts, extra_boot_prefix_paths=frozenset() ): """Validate that user passes bootloader_setting correctly""" kernel_action = "" @@ -284,7 +295,9 @@ def validate_kernels( # Validate with len(bootloader_setting["kernel"]) == 1 if len(bootloader_setting["kernel"]) == 1: validate_kernel_initrd(module, bootloader_setting["kernel"], kernel_mod_keys) - kernel = get_single_kernel(bootloader_setting["kernel"], extra_boot_prefix) + kernel = get_single_kernel( + bootloader_setting["kernel"], extra_boot_prefix_paths + ) kernel_action = "modify" if state == "present" else "remove" return kernel_action, kernel @@ -316,13 +329,15 @@ def validate_kernels( kernel_action = "create" if state == "present" else "remove" if kernel_action == "create": - kernel = get_create_kernel(bootloader_setting["kernel"], extra_boot_prefix) + kernel = get_create_kernel( + bootloader_setting["kernel"], extra_boot_prefix_paths + ) validate_kernel_initrd(module, bootloader_setting["kernel"], kernel_mod_keys) if kernel_action in ["remove", "modify"]: kernel_to_mod = get_kernel_to_mod(bootloader_setting["kernel"], kernel_mod_keys) - kernel = get_single_kernel(kernel_to_mod, extra_boot_prefix) + kernel = get_single_kernel(kernel_to_mod, extra_boot_prefix_paths) return kernel_action, kernel @@ -609,15 +624,16 @@ def run_module(): default_kernel_index = get_default_kernel(module, "index") bootloader_facts = get_facts(kernels_info, default_kernel_index) - # grubby may report the kernel and initrd paths with an extra /boot - # prefix; in that case the grubby commands whose values grubby strips - # the prefix from (--add-kernel, --initrd, --update-kernel, - # --remove-kernel) need the extra prefix too, while the other - # commands (--info, --set-default) need the real kernel path - extra_boot_prefix = has_extra_boot_prefix(kernels_info) + # grubby may report the paths of only some of the kernel entries + # with an extra /boot prefix; the grubby commands whose values + # grubby strips the prefix from (--add-kernel, --initrd, + # --update-kernel, --remove-kernel) need the extra prefix for those + # entries, while the other commands (--info, --set-default) always + # need the real kernel path + extra_boot_prefix_paths = get_extra_boot_prefix_paths(kernels_info) kernel_action, kernel = validate_kernels( - module, bootloader_setting, bootloader_facts, extra_boot_prefix + module, bootloader_setting, bootloader_facts, extra_boot_prefix_paths ) # Remove all existing boot settings diff --git a/module_utils/bootloader_lsr/bootloader_common.py b/module_utils/bootloader_lsr/bootloader_common.py index eaef47f4..0eec36bd 100644 --- a/module_utils/bootloader_lsr/bootloader_common.py +++ b/module_utils/bootloader_lsr/bootloader_common.py @@ -14,9 +14,13 @@ # Some versions of grubby, on platforms where /boot is a separate mount point # and the BootLoaderSpec entries store the kernel and initrd paths with the # /boot prefix, report the paths with an extra /boot prefix; for example -# /boot/boot/vmlinuz-7.2.10-200.fc44.x86_64. Strip the extra prefix from the -# grubby output, and add it back to the kernel and initrd paths of the grubby -# commands, so that the modules work on the platforms affected by the issue. +# /boot/boot/vmlinuz-7.2.10-200.fc44.x86_64. grubby can report the paths of +# only some of the entries with the extra prefix, for example when the +# entries store some of the paths with the /boot prefix and others without +# it, so the extra prefix is handled for each entry: strip the extra prefix +# from the grubby output, and add it back to the kernel and initrd paths of +# the grubby commands for the entries that grubby reports with it, so that +# the modules work on the platforms affected by the issue. GRUBBY_BOOT_PREFIX = "/boot" GRUBBY_EXTRA_BOOT_PREFIX = "/boot/boot/" @@ -45,22 +49,24 @@ def strip_extra_boot_prefix(value): ) -def has_extra_boot_prefix(kernels_info): - """Check whether grubby reports kernel paths with an extra /boot prefix +def get_extra_boot_prefix_paths(kernels_info): + """Get the real kernel paths that grubby reports with an extra /boot prefix - Detect the platforms affected by the grubby issue by looking at the - kernel paths of the output of 'grubby --info=ALL', so that the extra - prefix can be added back to the kernel and initrd paths of the grubby - commands. + grubby can report the paths of only some of the entries with the extra + /boot prefix, for example when the BootLoaderSpec entries store some of + the kernel paths with the /boot prefix and others without it, so the + extra prefix has to be handled for each entry. Look at the kernel paths + of the output of 'grubby --info=ALL', and return the set of the real + paths of the entries whose reported path has the extra prefix, so that + the extra prefix can be added back to the kernel and initrd paths of + the grubby commands for those entries. """ - return ( - re.search( - r'^kernel="?' + re.escape(GRUBBY_EXTRA_BOOT_PREFIX), - kernels_info, - re.MULTILINE, - ) - is not None - ) + paths = set() + for match in re.finditer(r'^kernel="?([^"\n]*)', kernels_info, re.MULTILINE): + path = match.group(1) + if path.startswith(GRUBBY_EXTRA_BOOT_PREFIX): + paths.add(strip_extra_boot_prefix(path)) + return paths def add_extra_boot_prefix(value, extra_boot_prefix): @@ -74,6 +80,10 @@ def add_extra_boot_prefix(value, extra_boot_prefix): like --info and --set-default, require the real kernel path instead. The value can be a space separated list of paths, as the initrd value can be, so the extra prefix is added to every /boot path of the list. + The caller decides, for each kernel entry, whether grubby reports its + path with the extra prefix, and applies the extra prefix only to the + paths of those entries, leaving the real paths of the other entries, + like /boot/vmlinuz-b, unchanged. """ if not extra_boot_prefix: return value diff --git a/tests/unit/test_bootloader_common.py b/tests/unit/test_bootloader_common.py index 6d36d67d..c2c7fd9c 100644 --- a/tests/unit/test_bootloader_common.py +++ b/tests/unit/test_bootloader_common.py @@ -120,6 +120,26 @@ id=c44543d15b2c4e898912c2497f734e67-6.5.12-100.fc37.x86_64 """ +# grubby can report the paths of only some of the entries with the extra +# /boot prefix, for example when the entries store some of the paths with +# the /boot prefix and others without it +INFO_MIXED_EXTRA_BOOT_PREFIX = """ +index=0 +kernel="/boot/boot/vmlinuz-7.2.10-200.fc44.x86_64" +args="ro rootflags=subvol=root rhgb quiet" +root="UUID=65c70529-e9ad-4778-9001-18fe8c525285" +initrd="/boot/boot/initramfs-7.2.10-200.fc44.x86_64.img $tuned_initrd" +title="Fedora Linux (7.2.10-200.fc44.x86_64) 44 (Cloud Edition)" +id="c44543d15b2c4e898912c2497f734e67-7.2.10-200.fc44.x86_64" +index=1 +kernel="/boot/vmlinuz-6.19.10-300.fc44.x86_64" +args="ro rootflags=subvol=root rhgb quiet" +root="UUID=65c70529-e9ad-4778-9001-18fe8c525285" +initrd="/boot/initramfs-6.19.10-300.fc44.x86_64.img $tuned_initrd" +title="Fedora Linux (6.19.10-300.fc44.x86_64) 44 (Cloud Edition)" +id="c44543d15b2c4e898912c2497f734e67-6.19.10-300.fc44.x86_64" +""" + FACTS_EXTRA_BOOT_PREFIX = [ { "args": "ro rootflags=subvol=root rhgb quiet", @@ -196,25 +216,43 @@ def test_strip_extra_boot_prefix(self): ) self.assertEqual(bootloader_common.strip_extra_boot_prefix(""), "") - def test_has_extra_boot_prefix(self): - self.assertTrue(bootloader_common.has_extra_boot_prefix(INFO_EXTRA_BOOT_PREFIX)) - # unquoted kernel paths, as RHEL 7 grubby reports them, are detected too - self.assertTrue( - bootloader_common.has_extra_boot_prefix(INFO_EXTRA_BOOT_PREFIX_RHEL7) + def test_get_extra_boot_prefix_paths(self): + # the real paths of all the entries whose reported path has the + # extra prefix + self.assertEqual( + bootloader_common.get_extra_boot_prefix_paths(INFO_EXTRA_BOOT_PREFIX), + { + "/boot/vmlinuz-7.2.10-200.fc44.x86_64", + "/boot/vmlinuz-7.2.8-200.fc44.x86_64", + }, + ) + # only the path of the entry with the extra prefix, not the real + # path of the other entry + self.assertEqual( + bootloader_common.get_extra_boot_prefix_paths(INFO_MIXED_EXTRA_BOOT_PREFIX), + {"/boot/vmlinuz-7.2.10-200.fc44.x86_64"}, + ) + # unquoted kernel paths, as RHEL 7 grubby reports them, are handled too + self.assertEqual( + bootloader_common.get_extra_boot_prefix_paths(INFO_EXTRA_BOOT_PREFIX_RHEL7), + {"/boot/vmlinuz-6.5.12-100.fc37.x86_64"}, ) - self.assertFalse(bootloader_common.has_extra_boot_prefix(INFO)) - self.assertFalse(bootloader_common.has_extra_boot_prefix("")) - # kernel paths without the extra prefix are not detected, whether + # no paths without the extra prefix + self.assertEqual(bootloader_common.get_extra_boot_prefix_paths(INFO), set()) + self.assertEqual(bootloader_common.get_extra_boot_prefix_paths(""), set()) + # kernel paths without the extra prefix are not reported, whether # grubby reports them quoted or not - self.assertFalse( - bootloader_common.has_extra_boot_prefix( + self.assertEqual( + bootloader_common.get_extra_boot_prefix_paths( 'kernel="/boot/vmlinuz-6.5.12-100.fc37.x86_64"\n' - ) + ), + set(), ) - self.assertFalse( - bootloader_common.has_extra_boot_prefix( + self.assertEqual( + bootloader_common.get_extra_boot_prefix_paths( "kernel=/boot/vmlinuz-6.5.12-100.fc37.x86_64\n" - ) + ), + set(), ) def test_add_extra_boot_prefix(self): diff --git a/tests/unit/test_bootloader_settings.py b/tests/unit/test_bootloader_settings.py index d518c033..8e6934c8 100644 --- a/tests/unit/test_bootloader_settings.py +++ b/tests/unit/test_bootloader_settings.py @@ -214,6 +214,68 @@ }, ] +# the real paths of the entries of INFO_EXTRA_BOOT_PREFIX, which grubby +# reports with the extra /boot prefix +EXTRA_BOOT_PREFIX_PATHS = { + "/boot/vmlinuz-7.2.10-200.fc44.x86_64", + "/boot/vmlinuz-7.2.8-200.fc44.x86_64", +} + +# grubby can report the paths of only some of the entries with the extra +# /boot prefix, for example when the entries store some of the paths with +# the /boot prefix and others without it +INFO_MIXED_EXTRA_BOOT_PREFIX = """ +index=0 +kernel="/boot/boot/vmlinuz-7.2.10-200.fc44.x86_64" +args="ro rootflags=subvol=root rhgb quiet" +root="UUID=65c70529-e9ad-4778-9001-18fe8c525285" +initrd="/boot/boot/initramfs-7.2.10-200.fc44.x86_64.img $tuned_initrd" +title="Fedora Linux (7.2.10-200.fc44.x86_64) 44 (Cloud Edition)" +id="c44543d15b2c4e898912c2497f734e67-7.2.10-200.fc44.x86_64" +index=1 +kernel="/boot/vmlinuz-6.19.10-300.fc44.x86_64" +args="ro rootflags=subvol=root rhgb quiet" +root="UUID=65c70529-e9ad-4778-9001-18fe8c525285" +initrd="/boot/initramfs-6.19.10-300.fc44.x86_64.img $tuned_initrd" +title="Fedora Linux (6.19.10-300.fc44.x86_64) 44 (Cloud Edition)" +id="c44543d15b2c4e898912c2497f734e67-6.19.10-300.fc44.x86_64" +""" + +# the entry of INFO_MIXED_EXTRA_BOOT_PREFIX that grubby reports with the +# real path +INFO_MIXED_EXTRA_BOOT_PREFIX_ENTRY_B = """ +index=1 +kernel="/boot/vmlinuz-6.19.10-300.fc44.x86_64" +args="ro rootflags=subvol=root rhgb quiet" +root="UUID=65c70529-e9ad-4778-9001-18fe8c525285" +initrd="/boot/initramfs-6.19.10-300.fc44.x86_64.img $tuned_initrd" +title="Fedora Linux (6.19.10-300.fc44.x86_64) 44 (Cloud Edition)" +id="c44543d15b2c4e898912c2497f734e67-6.19.10-300.fc44.x86_64" +""" + +MIXED_FACTS_EXTRA_BOOT_PREFIX = [ + { + "args": "ro rootflags=subvol=root rhgb quiet", + "id": "c44543d15b2c4e898912c2497f734e67-7.2.10-200.fc44.x86_64", + "index": "0", + "initrd": "/boot/initramfs-7.2.10-200.fc44.x86_64.img $tuned_initrd", + "kernel": "/boot/vmlinuz-7.2.10-200.fc44.x86_64", + "root": "UUID=65c70529-e9ad-4778-9001-18fe8c525285", + "title": "Fedora Linux (7.2.10-200.fc44.x86_64) 44 (Cloud Edition)", + "default": True, + }, + { + "args": "ro rootflags=subvol=root rhgb quiet", + "id": "c44543d15b2c4e898912c2497f734e67-6.19.10-300.fc44.x86_64", + "index": "1", + "initrd": "/boot/initramfs-6.19.10-300.fc44.x86_64.img $tuned_initrd", + "kernel": "/boot/vmlinuz-6.19.10-300.fc44.x86_64", + "root": "UUID=65c70529-e9ad-4778-9001-18fe8c525285", + "title": "Fedora Linux (6.19.10-300.fc44.x86_64) 44 (Cloud Edition)", + "default": False, + }, +] + kernels_keys = ["kernel_index", "kernel_path", "kernel_title", "DEFAULT", "ALL"] @@ -1259,28 +1321,45 @@ def reset_vars(self): self.result = dict(changed=False, actions=list()) def test_get_single_kernel_extra_boot_prefix(self): - # kernel paths get the extra prefix, to modify or remove the kernel + # the paths of the entries that grubby reports with the extra prefix + # get the extra prefix, to modify or remove the kernel kernel = {"path": "/boot/vmlinuz-7.2.10-200.fc44.x86_64"} + extra_boot_prefix_paths = {"/boot/vmlinuz-7.2.10-200.fc44.x86_64"} self.assertEqual( bootloader_settings.get_single_kernel(kernel), "/boot/vmlinuz-7.2.10-200.fc44.x86_64", ) self.assertEqual( - bootloader_settings.get_single_kernel(kernel, True), + bootloader_settings.get_single_kernel(kernel, extra_boot_prefix_paths), "/boot/boot/vmlinuz-7.2.10-200.fc44.x86_64", ) + # the real paths of the entries that grubby reports without the + # extra prefix are unchanged + other_kernel = {"path": "/boot/vmlinuz-b"} + self.assertEqual( + bootloader_settings.get_single_kernel( + other_kernel, extra_boot_prefix_paths + ), + "/boot/vmlinuz-b", + ) # titles and indexes are used as is self.assertEqual( - bootloader_settings.get_single_kernel({"title": "Fedora Linux"}, True), + bootloader_settings.get_single_kernel( + {"title": "Fedora Linux"}, extra_boot_prefix_paths + ), "TITLE='Fedora Linux'", ) self.assertEqual( - bootloader_settings.get_single_kernel({"index": 1}, True), + bootloader_settings.get_single_kernel( + {"index": 1}, extra_boot_prefix_paths + ), "1", ) def test_get_create_kernel_extra_boot_prefix(self): - # kernel and initrd paths get the extra prefix, to create the kernel + # a new kernel has no reported path yet, so it follows the + # convention of the existing entries: the kernel and initrd paths + # get the extra prefix when any entry is reported with it kernel = { "title": "Fedora Linux", "path": "/boot/vmlinuz-7.2.10-200.fc44.x86_64", @@ -1293,7 +1372,9 @@ def test_get_create_kernel_extra_boot_prefix(self): "--title='Fedora Linux'", ) self.assertEqual( - bootloader_settings.get_create_kernel(kernel, True), + bootloader_settings.get_create_kernel( + kernel, {"/boot/vmlinuz-7.2.10-200.fc44.x86_64"} + ), "--initrd=/boot/boot/initramfs-7.2.10-200.fc44.x86_64.img " "--add-kernel=/boot/boot/vmlinuz-7.2.10-200.fc44.x86_64 " "--title='Fedora Linux'", @@ -1307,7 +1388,7 @@ def test_validate_kernels_extra_boot_prefix(self): self.mock_module, {"kernel": {"path": "/boot/vmlinuz-7.2.10-200.fc44.x86_64"}}, FACTS_EXTRA_BOOT_PREFIX, - True, + EXTRA_BOOT_PREFIX_PATHS, ) self.mock_module.fail_json.assert_not_called() self.assertEqual(kernel_action, "modify") @@ -1325,7 +1406,7 @@ def test_validate_kernels_extra_boot_prefix(self): } }, FACTS_EXTRA_BOOT_PREFIX, - True, + EXTRA_BOOT_PREFIX_PATHS, ) self.mock_module.fail_json.assert_not_called() self.assertEqual(kernel_action, "modify") @@ -1343,7 +1424,7 @@ def test_validate_kernels_extra_boot_prefix(self): } }, FACTS_EXTRA_BOOT_PREFIX, - True, + EXTRA_BOOT_PREFIX_PATHS, ) self.mock_module.fail_json.assert_not_called() self.assertEqual(kernel_action, "create") @@ -1365,7 +1446,7 @@ def test_validate_kernels_normalizes_setting_paths(self): self.mock_module, {"kernel": {"path": "/boot/boot/vmlinuz-7.2.10-200.fc44.x86_64"}}, facts, - True, + EXTRA_BOOT_PREFIX_PATHS, ) self.mock_module.fail_json.assert_not_called() self.assertEqual(kernel_action, "modify") @@ -1378,7 +1459,7 @@ def test_validate_kernels_normalizes_setting_paths(self): self.mock_module, {"kernel": {"path": "/boot/boot/vmlinuz-7.2.10-200.fc44.x86_64"}}, facts, - False, + frozenset(), ) self.mock_module.fail_json.assert_not_called() self.assertEqual(kernel_action, "modify") @@ -1398,7 +1479,7 @@ def test_validate_kernels_normalizes_setting_paths(self): } }, facts, - True, + EXTRA_BOOT_PREFIX_PATHS, ) self.mock_module.fail_json.assert_not_called() self.assertEqual(kernel_action, "modify") @@ -1418,7 +1499,7 @@ def test_validate_kernels_normalizes_setting_paths(self): } }, facts, - True, + EXTRA_BOOT_PREFIX_PATHS, ) self.mock_module.fail_json.assert_not_called() self.assertEqual(kernel_action, "create") @@ -1437,12 +1518,79 @@ def test_validate_kernels_normalizes_setting_paths(self): "state": "absent", }, facts, - True, + EXTRA_BOOT_PREFIX_PATHS, ) self.mock_module.fail_json.assert_not_called() self.assertEqual(kernel_action, "remove") self.assertEqual(kernel, "/boot/boot/vmlinuz-7.2.10-200.fc44.x86_64") + def test_validate_kernels_mixed_extra_boot_prefix(self): + """Only the paths of the entries that grubby reports with the extra + /boot prefix get the extra prefix; the real paths of the other + entries, like /boot/vmlinuz-b, are unchanged""" + facts = copy.deepcopy(MIXED_FACTS_EXTRA_BOOT_PREFIX) + # only the first entry is reported with the extra prefix + extra_boot_prefix_paths = {"/boot/vmlinuz-7.2.10-200.fc44.x86_64"} + + # a single path of an entry that grubby reports with the extra + # prefix: the grubby command gets the extra prefix + kernel_action, kernel = bootloader_settings.validate_kernels( + self.mock_module, + {"kernel": {"path": "/boot/vmlinuz-7.2.10-200.fc44.x86_64"}}, + facts, + extra_boot_prefix_paths, + ) + self.mock_module.fail_json.assert_not_called() + self.assertEqual(kernel_action, "modify") + self.assertEqual(kernel, "/boot/boot/vmlinuz-7.2.10-200.fc44.x86_64") + self.reset_vars() + + # a single path of an entry that grubby reports with the real path: + # the grubby command keeps the real path + kernel_action, kernel = bootloader_settings.validate_kernels( + self.mock_module, + {"kernel": {"path": "/boot/vmlinuz-6.19.10-300.fc44.x86_64"}}, + facts, + extra_boot_prefix_paths, + ) + self.mock_module.fail_json.assert_not_called() + self.assertEqual(kernel_action, "modify") + self.assertEqual(kernel, "/boot/vmlinuz-6.19.10-300.fc44.x86_64") + self.reset_vars() + + # the same, with a multi-key setting + kernel_action, kernel = bootloader_settings.validate_kernels( + self.mock_module, + { + "kernel": { + "path": "/boot/vmlinuz-6.19.10-300.fc44.x86_64", + "title": "Fedora Linux (6.19.10-300.fc44.x86_64) " + "44 (Cloud Edition)", + } + }, + facts, + extra_boot_prefix_paths, + ) + self.mock_module.fail_json.assert_not_called() + self.assertEqual(kernel_action, "modify") + self.assertEqual(kernel, "/boot/vmlinuz-6.19.10-300.fc44.x86_64") + self.reset_vars() + + # remove a kernel that grubby reports with the real path: the + # grubby command keeps the real path + kernel_action, kernel = bootloader_settings.validate_kernels( + self.mock_module, + { + "kernel": {"path": "/boot/vmlinuz-6.19.10-300.fc44.x86_64"}, + "state": "absent", + }, + facts, + extra_boot_prefix_paths, + ) + self.mock_module.fail_json.assert_not_called() + self.assertEqual(kernel_action, "remove") + self.assertEqual(kernel, "/boot/vmlinuz-6.19.10-300.fc44.x86_64") + def test_get_default_kernel_strips_extra_boot_prefix(self): self.reset_vars() self.mock_module.run_command.return_value = ( @@ -1556,7 +1704,9 @@ def run_command(cmd, **kwargs): # prefix that grubby reports return 0, "/boot/boot/vmlinuz-7.2.8-200.fc44.x86_64", "" if cmd == "grubby --info=ALL": - return 0, INFO_EXTRA_BOOT_PREFIX, "" + # grubby reports the path of only one of the entries with + # the extra prefix + return 0, INFO_MIXED_EXTRA_BOOT_PREFIX, "" if cmd.startswith("grubby --info=TITLE="): # title selectors are passed to grubby --info as is self.assertEqual( @@ -1571,6 +1721,9 @@ def run_command(cmd, **kwargs): # unquoted path, quoted again self.assertEqual(cmd, "grubby --info='/boot/vmlinuz with space'") return 0, INFO_EXTRA_BOOT_PREFIX, "" + if cmd == "grubby --info=/boot/vmlinuz-6.19.10-300.fc44.x86_64": + # the entry that grubby reports with the real path + return 0, INFO_MIXED_EXTRA_BOOT_PREFIX_ENTRY_B, "" if cmd.startswith("grubby --info="): # grubby does not strip the extra /boot prefix from the # --info value, so the module passes the real kernel path @@ -1621,17 +1774,26 @@ def run_command(cmd, **kwargs): ], ), ( - # modify a path with a space: the selector is quoted; the - # 'grubby --info=' command gets the real path, stripped of - # the extra prefix from the raw, unquoted path and quoted - # again, while the write command keeps the doubled, quoted - # path + # modify a path with a space, of an entry that grubby does + # not report with the extra prefix: the selector is quoted; + # both the 'grubby --info=' command and the write command + # use the real path, stripped of the extra prefix from the + # raw, unquoted path and quoted again { "kernel": {"path": "/boot/boot/vmlinuz with space"}, "options": [{"name": "debug"}], }, + ["grubby --update-kernel='/boot/vmlinuz with space' " "--args=debug"], + ), + ( + # modify the kernel of the entry that grubby reports with + # the real path: the grubby command keeps the real path + { + "kernel": {"path": "/boot/vmlinuz-6.19.10-300.fc44.x86_64"}, + "options": [{"name": "debug"}], + }, [ - "grubby --update-kernel='/boot/boot/vmlinuz with space' " + "grubby --update-kernel=/boot/vmlinuz-6.19.10-300.fc44.x86_64 " "--args=debug" ], ), From 786fb97c8369a3da889df685c44c8e23e911fb4f Mon Sep 17 00:00:00 2001 From: Rich Megginson Date: Wed, 18 Nov 2026 11:31:07 -0700 Subject: [PATCH 7/7] test: avoid implicit string concatenation in lists in unit tests Cause: The expected grubby commands of the extra /boot prefix tests used implicitly concatenated string literals as the elements of the expected actions lists, and black collapsed one of them onto a single line. Consequences: The ansible-test sanity pylint check failed with "implicit-str-concat: Implicit string concatenation found in list" at test_bootloader_settings.py:1786. Fix: Use a single string literal for each expected action, instead of implicitly concatenated literals, for all the actions lists of the extra /boot prefix tests. Result: The ansible-test sanity tests, including the pylint implicit-str-concat check, pass; the unit tests (84) and the black, flake8 and pylint linters pass. Signed-off-by: Rich Megginson Assisted-by: goose using model glm-5-3 Signed-off-by: Rich Megginson --- tests/tests_settings.yml | 2 +- tests/unit/test_bootloader_settings.py | 18 ++++++------------ 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/tests/tests_settings.yml b/tests/tests_settings.yml index 4890e54c..1b83a0cc 100644 --- a/tests/tests_settings.yml +++ b/tests/tests_settings.yml @@ -102,7 +102,7 @@ first) }}" assert: that: - - cloned_kernel.kernel != default_bootloader.kernel + - cloned_kernel != default_bootloader - >- (bootloader_facts | selectattr('kernel', 'search', cloned_kernel.kernel) | first).args | diff --git a/tests/unit/test_bootloader_settings.py b/tests/unit/test_bootloader_settings.py index 8e6934c8..029915b9 100644 --- a/tests/unit/test_bootloader_settings.py +++ b/tests/unit/test_bootloader_settings.py @@ -1742,8 +1742,7 @@ def run_command(cmd, **kwargs): "options": [{"name": "debug"}], }, [ - "grubby --update-kernel=/boot/boot/vmlinuz-7.2.10-200.fc44.x86_64 " - "--args=debug" + "grubby --update-kernel=/boot/boot/vmlinuz-7.2.10-200.fc44.x86_64 --args=debug" ], ), ( @@ -1754,8 +1753,7 @@ def run_command(cmd, **kwargs): "options": [{"name": "debug"}], }, [ - "grubby --update-kernel=/boot/boot/vmlinuz-7.2.10-200.fc44.x86_64 " - "--args=debug" + "grubby --update-kernel=/boot/boot/vmlinuz-7.2.10-200.fc44.x86_64 --args=debug" ], ), ( @@ -1769,8 +1767,7 @@ def run_command(cmd, **kwargs): "options": [{"name": "debug"}], }, [ - "grubby --update-kernel=TITLE='Fedora Linux " - "(7.2.10-200.fc44.x86_64) 44 (Cloud Edition)' --args=debug" + "grubby --update-kernel=TITLE='Fedora Linux (7.2.10-200.fc44.x86_64) 44 (Cloud Edition)' --args=debug" ], ), ( @@ -1783,7 +1780,7 @@ def run_command(cmd, **kwargs): "kernel": {"path": "/boot/boot/vmlinuz with space"}, "options": [{"name": "debug"}], }, - ["grubby --update-kernel='/boot/vmlinuz with space' " "--args=debug"], + ["grubby --update-kernel='/boot/vmlinuz with space' --args=debug"], ), ( # modify the kernel of the entry that grubby reports with @@ -1793,8 +1790,7 @@ def run_command(cmd, **kwargs): "options": [{"name": "debug"}], }, [ - "grubby --update-kernel=/boot/vmlinuz-6.19.10-300.fc44.x86_64 " - "--args=debug" + "grubby --update-kernel=/boot/vmlinuz-6.19.10-300.fc44.x86_64 --args=debug" ], ), ( @@ -1835,9 +1831,7 @@ def run_command(cmd, **kwargs): "options": [{"name": "debug"}], }, [ - "grubby --initrd=/boot/boot/initramfs-new.img " - "--add-kernel=/boot/boot/vmlinuz-new --title='New Kernel' " - "--args=debug" + "grubby --initrd=/boot/boot/initramfs-new.img --add-kernel=/boot/boot/vmlinuz-new --title='New Kernel' --args=debug" ], ), ]: