diff --git a/library/bootloader_facts.py b/library/bootloader_facts.py index ccb3928..781e6a1 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 88880c2..377b1c4 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 @@ -103,6 +104,7 @@ """ import re +import shlex from ansible.module_utils.basic import AnsibleModule @@ -110,29 +112,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_extra_boot_prefix_paths, + get_facts, + strip_extra_boot_prefix, +) def get_dict_same_keys(dict1, dict2): @@ -171,26 +156,43 @@ 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_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": + # 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): +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": - 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 +224,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_paths=frozenset() +): """Validate that user passes bootloader_setting correctly""" kernel_action = "" kernel = "" @@ -278,10 +282,22 @@ def validate_kernels(module, bootloader_setting, bootloader_facts): 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) - kernel = get_single_kernel(bootloader_setting["kernel"]) + kernel = get_single_kernel( + bootloader_setting["kernel"], extra_boot_prefix_paths + ) kernel_action = "modify" if state == "present" else "remove" return kernel_action, kernel @@ -313,13 +329,15 @@ 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_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) + kernel = get_single_kernel(kernel_to_mod, extra_boot_prefix_paths) return kernel_action, kernel @@ -483,11 +501,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,9 +525,29 @@ 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() +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. 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 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): """Get the sorted list of desired arg tokens for a replacement check.""" tokens = [] @@ -582,9 +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 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 + module, bootloader_setting, bootloader_facts, extra_boot_prefix_paths ) # Remove all existing boot settings @@ -592,7 +641,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=" + get_info_kernel(kernel) + ) if needs_replacement(bootloader_setting.get("options", []), kernel_info): rm_boot_args(module, result, kernel_info, kernel) @@ -602,8 +655,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=" + get_info_kernel(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 0000000..0eec36b --- /dev/null +++ b/module_utils/bootloader_lsr/bootloader_common.py @@ -0,0 +1,124 @@ +# -*- 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. 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/" + + +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 get_extra_boot_prefix_paths(kernels_info): + """Get the real kernel paths that grubby reports with an extra /boot prefix + + 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. + """ + 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): + """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, --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. + 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 + 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 3792d7a..5e14bcf 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 d86fb33..1b83a0c 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 @@ -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 0000000..c2c7fd9 --- /dev/null +++ b/tests/unit/test_bootloader_common.py @@ -0,0 +1,294 @@ +# -*- 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 +""" + +# 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", + "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_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"}, + ) + # 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.assertEqual( + bootloader_common.get_extra_boot_prefix_paths( + 'kernel="/boot/vmlinuz-6.5.12-100.fc37.x86_64"\n' + ), + set(), + ) + 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): + # 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 9441b02..0fa7f39 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 0ebe417..029915b 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""" @@ -9,6 +10,7 @@ __metaclass__ = type +import copy import unittest try: @@ -170,6 +172,110 @@ 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, + }, +] + +# 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"] @@ -1196,3 +1302,552 @@ 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): + # 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, 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"}, extra_boot_prefix_paths + ), + "TITLE='Fedora Linux'", + ) + self.assertEqual( + bootloader_settings.get_single_kernel( + {"index": 1}, extra_boot_prefix_paths + ), + "1", + ) + + def test_get_create_kernel_extra_boot_prefix(self): + # 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", + "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, {"/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'", + ) + + 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, + 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() + + # 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, + EXTRA_BOOT_PREFIX_PATHS, + ) + 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, + EXTRA_BOOT_PREFIX_PATHS, + ) + 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_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, + 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() + + # 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, + frozenset(), + ) + 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, + 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 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, + EXTRA_BOOT_PREFIX_PATHS, + ) + 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, + 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 = ( + "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_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", + ) + # 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'"), + "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 = ''' +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": + # 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( + 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='"): + # 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 == "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 + 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" + ], + ), + ( + # 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" + ], + ), + ( + # 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" + ], + ), + ( + # 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/vmlinuz-6.19.10-300.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"])