|
25 | 25 | import logging |
26 | 26 | import os |
27 | 27 | import re |
| 28 | +import shlex |
28 | 29 | import shutil |
29 | 30 | import signal |
30 | 31 | import subprocess |
|
53 | 54 | utils_misc, |
54 | 55 | utils_net, |
55 | 56 | utils_package, |
| 57 | + utils_sys, |
56 | 58 | virt_vm, |
57 | 59 | ) |
58 | 60 |
|
@@ -214,16 +216,73 @@ def update_boot_option( |
214 | 216 | req_remove_args, session=session, remove_args=True |
215 | 217 | ) |
216 | 218 | 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 |
227 | 286 | if req_remove_args or req_args: |
228 | 287 | __run_cmd_and_handle_error( |
229 | 288 | msg, cmd, session, "Failed to modify guest kernel option" |
|
0 commit comments