Skip to content

Commit d0f64e1

Browse files
utils_test: add support for updating kernel args for image mode
Extend 'update_boot_option' to handle updating kernel args in image mode Signed-off-by: Liang Cong <lcong@redhat.com>
1 parent a3cfa5a commit d0f64e1

1 file changed

Lines changed: 68 additions & 10 deletions

File tree

virttest/utils_test/__init__.py

Lines changed: 68 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import logging
2626
import os
2727
import re
28+
import shlex
2829
import shutil
2930
import signal
3031
import subprocess
@@ -53,6 +54,7 @@
5354
utils_misc,
5455
utils_net,
5556
utils_package,
57+
utils_sys,
5658
virt_vm,
5759
)
5860

@@ -214,16 +216,72 @@ def update_boot_option(
214216
req_remove_args, session=session, remove_args=True
215217
)
216218
else:
217-
if not utils_package.package_install("grubby", session=session):
218-
raise exceptions.TestError("Failed to install grubby package")
219-
msg = "Update guest kernel option. "
220-
cmd = "grubby --update-kernel=`grubby --default-kernel` "
221-
if req_remove_args:
222-
msg += " remove args: %s" % req_remove_args
223-
cmd += '--remove-args="%s" ' % req_remove_args
224-
if req_args:
225-
msg += " add args: %s" % req_args
226-
cmd += '--args="%s"' % req_args
219+
if utils_sys.is_image_mode(session=session):
220+
get_ostree_cmd = "cat /proc/cmdline | grep -o 'ostree=[^ ]*'"
221+
current_ostree = session.cmd_output(get_ostree_cmd).strip()
222+
if not current_ostree:
223+
raise exceptions.TestError("System not booted via ostree/bootc.")
224+
225+
get_entry_cmd = (
226+
"grep -l '%s' /boot/loader/entries/*.conf | grep -v rescue | head -n 1"
227+
% shlex.quote(current_ostree)
228+
)
229+
target_entry = session.cmd_output(get_entry_cmd).strip()
230+
if not target_entry:
231+
raise exceptions.TestError(
232+
"Fail to find active entry for %s" % current_ostree
233+
)
234+
235+
content = session.cmd_output("cat %s" % shlex.quote(target_entry))
236+
lines = content.splitlines()
237+
new_lines = []
238+
found_options = False
239+
for line in lines:
240+
if line.startswith("options "):
241+
found_options = True
242+
parts = line.split()
243+
args = parts[1:]
244+
245+
if req_remove_args:
246+
remove_list = req_remove_args.split()
247+
args = [a for a in args if a not in remove_list]
248+
249+
if req_args:
250+
add_list = req_args.split()
251+
for arg in add_list:
252+
if arg not in args:
253+
args.append(arg)
254+
255+
new_lines.append("options %s" % " ".join(args))
256+
else:
257+
new_lines.append(line)
258+
259+
if not found_options:
260+
raise exceptions.TestError("Could not find 'options' line in %s" % target_entry)
261+
262+
updated_content = "\n".join(new_lines)
263+
cmd = (
264+
"unshare -m /bin/bash -c '"
265+
"mount -o remount,rw /boot && "
266+
"cat <<EOF > %s\n%s\nEOF\n"
267+
"mount -o remount,ro /boot'"
268+
% (shlex.quote(target_entry), updated_content)
269+
)
270+
msg = "Modifying Bootc kernel args in %s by cmd '%s'" % (
271+
target_entry,
272+
cmd,
273+
)
274+
else:
275+
if not utils_package.package_install("grubby", session=session):
276+
raise exceptions.TestError("Failed to install grubby package")
277+
msg = "Update guest kernel option. "
278+
cmd = "grubby --update-kernel=`grubby --default-kernel` "
279+
if req_remove_args:
280+
msg += " remove args: %s" % req_remove_args
281+
cmd += '--remove-args="%s" ' % req_remove_args
282+
if req_args:
283+
msg += " add args: %s" % req_args
284+
cmd += '--args="%s"' % req_args
227285
if req_remove_args or req_args:
228286
__run_cmd_and_handle_error(
229287
msg, cmd, session, "Failed to modify guest kernel option"

0 commit comments

Comments
 (0)