Skip to content

Commit 567b3b3

Browse files
authored
Merge pull request #4070 from rh-jugraham/set_default_kernel
utils_test: created function to update default kernel
2 parents 3ef8320 + db5887b commit 567b3b3

1 file changed

Lines changed: 103 additions & 0 deletions

File tree

virttest/utils_test/__init__.py

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,109 @@ 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 the full kernel name.
268+
get_available_kernel_paths(session, pattern) is a useful helper function.
269+
:param reboot: Whether to reboot the VM and update the kernel used or not.
270+
:param guest_arch_name: Guest architecture, e.g. x86_64, s390x, aarch64.
271+
:param timeout: Timeout for login and reboot.
272+
:param serial_login: Login guest via serial session.
273+
:raise NotImplementedError: Raised if Windows guest, not Linux.
274+
:raise exceptions.TestError: Raised if failed to update the guest kernel.
275+
"""
276+
if vm.params.get("os_type") == "windows":
277+
raise NotImplementedError(
278+
"update_vm_default_kernel() is supported only for Linux guest"
279+
)
280+
281+
timeout = int(timeout) if timeout else int(vm.params.get("login_timeout"))
282+
session = vm.wait_for_login(
283+
timeout=timeout, serial=serial_login, restart_network=True
284+
)
285+
try:
286+
if not utils_package.package_install("grubby", session=session):
287+
raise exceptions.TestError("Failed to install grubby package")
288+
289+
cmd = "grubby --default-kernel"
290+
status, output = session.cmd_status_output(cmd)
291+
if status != 0:
292+
LOG.error(output)
293+
else:
294+
if kernel_version in output:
295+
LOG.info(
296+
"%s is already the default kernel version (%s)"
297+
% (kernel_version, output)
298+
)
299+
return
300+
301+
msg = "Set default guest kernel"
302+
cmd = 'grubby --set-default="%s"' % kernel_version
303+
__run_cmd_and_handle_error(
304+
msg, cmd, session, "Failed to set default guest kernel"
305+
)
306+
307+
if guest_arch_name == "s390x":
308+
msg = "Update boot media with zipl"
309+
cmd = "zipl"
310+
__run_cmd_and_handle_error(
311+
msg, cmd, session, "Failed to update boot media with zipl"
312+
)
313+
314+
if reboot:
315+
LOG.info("Rebooting guest ...")
316+
session = vm.reboot(session=session, timeout=timeout, serial=serial_login)
317+
318+
finally:
319+
if session:
320+
session.close()
321+
322+
323+
def get_available_kernel_paths(session, pattern):
324+
"""
325+
Get the full kernel paths given an identifying pattern
326+
327+
:params session: the vm session
328+
:params pattern: regex pattern to identify specific kernel
329+
:returns: list of kernel paths
330+
:raises exceptions.TestError: raised if no matching kernels could be found
331+
"""
332+
try:
333+
if not utils_package.package_install("grubby", session=session):
334+
raise exceptions.TestError("Failed to install grubby package")
335+
cmd = "grubby --info=ALL"
336+
lines = session.cmd_output(cmd).splitlines()
337+
338+
kernel_paths = []
339+
for line in lines:
340+
if line.startswith("kernel=") and re.match(pattern, line):
341+
kernel = line.split("=")[1].strip('"')
342+
kernel_paths.append(kernel)
343+
LOG.info("kernel path(s) found: %s" % kernel_paths)
344+
345+
if not kernel_paths:
346+
LOG.debug("Full %s output: %s" % (cmd, lines))
347+
raise exceptions.TestError(
348+
"No kernels with %s pattern could be found" % pattern
349+
)
350+
return kernel_paths
351+
except:
352+
raise exceptions.TestError(
353+
"No kernels with %s pattern could be found" % pattern
354+
)
355+
356+
254357
def stop_windows_service(session, service, timeout=120):
255358
"""
256359
Stop a Windows service using sc.

0 commit comments

Comments
 (0)