Skip to content

Commit 12e919e

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 12e919e

1 file changed

Lines changed: 69 additions & 10 deletions

File tree

virttest/utils_test/__init__.py

Lines changed: 69 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,73 @@ 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+
args = line.split()[1:]
243+
244+
if req_remove_args:
245+
remove_list = req_remove_args.split()
246+
args = [a for a in args if a not in remove_list]
247+
248+
if req_args:
249+
add_list = req_args.split()
250+
for arg in add_list:
251+
if arg not in args:
252+
args.append(arg)
253+
254+
new_lines.append("options %s" % " ".join(args))
255+
else:
256+
new_lines.append(line)
257+
258+
if not found_options:
259+
raise exceptions.TestError(
260+
"Could not find 'options' line in %s" % target_entry
261+
)
262+
263+
updated_content = "\n".join(new_lines)
264+
cmd = (
265+
"unshare -m /bin/bash -c '"
266+
"mount -o remount,rw /boot && "
267+
"cat <<EOF > %s\n%s\nEOF\n"
268+
"mount -o remount,ro /boot'"
269+
% (shlex.quote(target_entry), updated_content)
270+
)
271+
msg = "Modifying Bootc kernel args in %s by cmd '%s'" % (
272+
target_entry,
273+
cmd,
274+
)
275+
else:
276+
if not utils_package.package_install("grubby", session=session):
277+
raise exceptions.TestError("Failed to install grubby package")
278+
msg = "Update guest kernel option. "
279+
cmd = "grubby --update-kernel=`grubby --default-kernel` "
280+
if req_remove_args:
281+
msg += " remove args: %s" % req_remove_args
282+
cmd += '--remove-args="%s" ' % req_remove_args
283+
if req_args:
284+
msg += " add args: %s" % req_args
285+
cmd += '--args="%s"' % req_args
227286
if req_remove_args or req_args:
228287
__run_cmd_and_handle_error(
229288
msg, cmd, session, "Failed to modify guest kernel option"

0 commit comments

Comments
 (0)