Skip to content

Commit 9cf857b

Browse files
committed
utils_test: created function to update default kernel
Signed-off-by: Julia Graham <jugraham@redhat.com>
1 parent 1144cf5 commit 9cf857b

1 file changed

Lines changed: 66 additions & 0 deletions

File tree

virttest/utils_test/__init__.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,72 @@ def update_boot_option(
251251
session.close()
252252

253253

254+
def update_vm_default_kernel(
255+
vm,
256+
kernel_version,
257+
reboot,
258+
guest_arch_name,
259+
timeout="",
260+
serial_login=False,
261+
):
262+
"""
263+
Update guest default kernel.
264+
265+
:param vm: The VM object.
266+
:param kernel_version: The full kernel version that should be set as the new default.
267+
Can use the entire kernel path or just be the full kernel name.
268+
:param reboot: Whether to reboot the VM and update the kernel used or not.
269+
:param guest_arch_name: Guest architecture, e.g. x86_64, s390x, aarch64.
270+
:param timeout: Timeout for login and reboot.
271+
:param serial_login: Login guest via serial session.
272+
:raise NotImplementedError: Raised if Windows guest, not Linux.
273+
:raise exceptions.TestError: Raised if failed to update the guest kernel.
274+
"""
275+
if vm.params.get("os_type") == "windows":
276+
raise NotImplementedError("update_vm_default_kernel() is supported only for Linux guest")
277+
278+
timeout = int(timeout) if timeout else int(vm.params.get("login_timeout"))
279+
session = vm.wait_for_login(
280+
timeout=timeout, serial=serial_login, restart_network=True
281+
)
282+
try:
283+
if not utils_package.package_install("grubby", session=session):
284+
raise exceptions.TestError("Failed to install grubby package")
285+
286+
cmd = "grubby --default-kernel"
287+
status, output = session.cmd_status_output(cmd)
288+
if status != 0:
289+
LOG.error(output)
290+
else:
291+
if kernel_version in output:
292+
LOG.info(
293+
"%s is already the default kernel version (%s)"
294+
% (kernel_version, output)
295+
)
296+
return
297+
298+
msg = "Set default guest kernel"
299+
cmd = 'grubby --set-default="%s"' % kernel_version
300+
__run_cmd_and_handle_error(
301+
msg, cmd, session, "Failed to set default guest kernel"
302+
)
303+
304+
if guest_arch_name == "s390x":
305+
msg = "Update boot media with zipl"
306+
cmd = "zipl"
307+
__run_cmd_and_handle_error(
308+
msg, cmd, session, "Failed to update boot media with zipl"
309+
)
310+
311+
if reboot:
312+
LOG.info("Rebooting guest ...")
313+
session = vm.reboot(session=session, timeout=timeout, serial=serial_login)
314+
315+
finally:
316+
if session:
317+
session.close()
318+
319+
254320
def stop_windows_service(session, service, timeout=120):
255321
"""
256322
Stop a Windows service using sc.

0 commit comments

Comments
 (0)