Skip to content

Commit

Permalink
Rewrite loader linux/initrd entry relabelling without regex to improv…
Browse files Browse the repository at this point in the history
…e reliability
  • Loading branch information
KaliumPuceon committed Oct 27, 2023
1 parent 232b691 commit 6da873b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
33 changes: 19 additions & 14 deletions kiwi/bootloader/config/grub2.py
Original file line number Diff line number Diff line change
Expand Up @@ -1459,22 +1459,27 @@ def _fix_grub_loader_entries_linux_and_initrd_paths(self):
)
for menu_entry_file in glob.iglob(loader_entries_pattern):
with open(menu_entry_file) as grub_menu_entry_file:
menu_entry = grub_menu_entry_file.read()
if self.bootpartition:
menu_entry = re.sub(
r'(linux|initrd) .*/boot(.*)',
r'\1 \2',
menu_entry
)
else:
menu_entry = re.sub(
r'(linux|initrd) .*(/boot.*)',
r'\1 \2',
menu_entry
)
menu_entry = grub_menu_entry_file.read().split("\n")

for line_number, menu_entry_line in enumerate(menu_entry):
if bool(re.match(r'^(linux|initrd) .*', menu_entry_line)):

log.debug("Existing loader entry: %s", menu_entry_line)
config_path = menu_entry_line.split(" ",1)[0]

basename = os.path.basename(menu_entry_line)
if self.bootpartition:
config_path += (" /%s" % basename)
else:
config_path += (" /boot/%s" % basename)

menu_entry[line_number] = config_path
log.debug("Updated loader entry: %s", config_path)

menu_entry = "\n".join(menu_entry)
with open(menu_entry_file, 'w') as grub_menu_entry_file:
grub_menu_entry_file.write(menu_entry)

def _get_partition_start(self, disk_device):
if self.target_table_type == 'dasd':
blocks = self._get_dasd_disk_geometry_element(
Expand Down
15 changes: 15 additions & 0 deletions test/unit/bootloader/config/grub2_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1132,6 +1132,21 @@ def test_setup_install_image_config_substitute_error(self):
with raises(KiwiTemplateError):
self.bootloader.setup_install_image_config(self.mbrid)

self.bootloader.bootpartition = True
file_handle_menu.reset_mock()

self.bootloader.setup_disk_image_config(
boot_options={
'root_device': 'rootdev', 'boot_device': 'bootdev'
}
)

assert 'linux /vmlinuz' in \
file_handle_menu.write.call_args_list[1][0][0].split(os.linesep)
assert 'initrd /initrd' in \
file_handle_menu.write.call_args_list[1][0][0].split(os.linesep)


@patch('kiwi.bootloader.config.grub2.Command.run')
@patch('kiwi.bootloader.config.base.BootLoaderConfigBase.get_boot_path')
@patch('os.path.exists')
Expand Down

0 comments on commit 6da873b

Please sign in to comment.