Skip to content

Commit

Permalink
virtme-ng: return 255 on panic in script mode instead of hanging
Browse files Browse the repository at this point in the history
If we trigger a kernel panic when running in script mode the guest would
just hangs indefinitely.

Test case:

 $ vng -vr -- "echo c > /proc/sysrq-trigger"

This can be a bit problematic in a CI scenario, since the indefinite
hang can block all the next tests or actions.

A possible solution is to use the `timeout` command with `vng`, but we
may also want to distinguish actual timeout conditions from kernel
panics.

For this reason always boot the guest with "panic=-1" when running in
script mode; this, together with qemu`-no-reboot` will trigger an
immediate exit, that can be detected by vng and report the special exit
code 255.

This allows to explicitly catch kernel panic conditions, by checking if
the return code of vng is 255.

Example:

 $ vng -vr -- "echo c > /proc/sysrq-trigger" 2>/tmp/kernel.log
 $ [ $? == 255 ] && grep "Kernel panic" /tmp/kernel.log
 [    3.260927] Kernel panic - not syncing: sysrq triggered crash

NOTE: we don't want to change the behavior with interactive mode. In
that case it is fine to hang indefinitely by default, because we may
want to attach a debugger, or trigger a memory dump, etc. So this change
should only affect script mode for now.

Link: linux-netdev/nipa#11
Signed-off-by: Andrea Righi <[email protected]>
  • Loading branch information
Andrea Righi committed Feb 13, 2024
1 parent eacf6bb commit d48f841
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions virtme/commands/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -1167,8 +1167,10 @@ def do_script(shellcmd: str, ret_path=None, show_boot_console=False) -> None:
qemuargs.extend(["-device", arch.virtio_dev_type("serial")])
qemuargs.extend(["-device", "virtserialport,name=virtme.ret,chardev=ret"])

# Scripts shouldn't reboot
# Scripts shouldn't reboot and shouldn't hang on panic: make sure to
# force an exit condition if a panic happens.
qemuargs.extend(["-no-reboot"])
kernelargs.append("panic=-1")

# Nasty issue: QEMU will set O_NONBLOCK on fds 0, 1, and 2.
# This isn't inherently bad, but it can cause a problem if
Expand Down Expand Up @@ -1371,7 +1373,12 @@ def do_script(shellcmd: str, ret_path=None, show_boot_console=False) -> None:
ret = fetch_script_retcode()
if ret is not None:
return ret
return status
if not args.script_sh and not args.script_exec:
return status
else:
# Return special error code 255 in case of unexpected exit
# (e.g., kernel panic).
return 255

except KeyboardInterrupt:
sys.stderr.write("Interrupted.")
Expand Down

0 comments on commit d48f841

Please sign in to comment.