Skip to content

Commit

Permalink
virtme-ng: redirect kernel log to stderr in interactive mode
Browse files Browse the repository at this point in the history
Redirect kernel messages to stderr when running in interactive mode,
similar to what we do in command/script mode.

In this way it is possible, for example, to save kernel logs to a file
even when running in interactive mode, such as:

 $ vng -vr 2>/tmp/kernel.log

NOTE: unfortunately we can't redirect early boot kernel messages,
earlyprintk doesn't seem to work with virconsole devices. So, early
messages will be still sent to stdout, in this way we don't break the
old behavior.

This addresses discussion #60.

Signed-off-by: Andrea Righi <[email protected]>
  • Loading branch information
Andrea Righi committed Feb 4, 2024
1 parent 00caf60 commit ecd8006
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 14 deletions.
8 changes: 4 additions & 4 deletions virtme/architectures.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def earlyconsole_args():

@staticmethod
def serial_console_args():
return ["console=ttyS0"]
return ["virtme_console=ttyS0"]

@staticmethod
def config_base():
Expand Down Expand Up @@ -225,7 +225,7 @@ def earlyconsole_args():

@staticmethod
def serial_console_args():
return ["console=ttyAMA0"]
return ["virtme_console=ttyAMA0"]

def kimg_path(self):
return "arch/arm/boot/zImage"
Expand Down Expand Up @@ -274,7 +274,7 @@ def earlyconsole_args():

@staticmethod
def serial_console_args():
return ["console=ttyAMA0"]
return ["virtme_console=ttyAMA0"]

def kimg_path(self):
return "arch/arm64/boot/Image"
Expand Down Expand Up @@ -334,7 +334,7 @@ def qemuargs(is_native, use_kvm):

@staticmethod
def serial_console_args():
return ["console=ttyS0"]
return ["virtme_console=ttyS0"]

def kimg_path(self):
return "arch/riscv/boot/Image"
Expand Down
31 changes: 23 additions & 8 deletions virtme/commands/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -986,23 +986,38 @@ def do_it() -> int:
qemuargs.extend(["-net", "none"])

if args.graphics is None and not args.script_sh and not args.script_exec:
# It would be nice to use virtconsole, but it's terminally broken
# in current kernels. Nonetheless, I'm configuring the console
# manually to make it easier to tweak in the future.
qemuargs.extend(["-echr", "1"])
qemuargs.extend(["-serial", "none"])
qemuargs.extend(["-chardev", "stdio,id=console,signal=off,mux=on"])

qemuargs.extend(arch.qemu_serial_console_args())
if args.verbose:
# Check if we have permission to access the current stderr.
if not can_access_file("/proc/self/fd/2"):
print(
"ERROR: not a valid pts, try to run vng inside tmux or screen",
file=sys.stderr,
)
sys.exit(1)

qemuargs.extend(["-mon", "chardev=console"])
# Redirect kernel messages to stderr, creating a separate console
qemuargs.extend(["-chardev", f"file,path=/proc/self/fd/2,id=dmesg"])
qemuargs.extend(["-device", arch.virtio_dev_type("serial")])
qemuargs.extend(["-device", "virtconsole,chardev=dmesg"])
kernelargs.extend(["console=hvc0"])

if args.verbose:
# Unfortunately we can't use hvc0 to redirect early console
# messages to stderr, so just send them to the main console, in
# this way we don't lose early printk's in verbose mode and we can
# catch potential boot issues.
kernelargs.extend(arch.earlyconsole_args())
qemuargs.extend(arch.qemu_nodisplay_args())

qemuargs.extend(["-chardev", "stdio,id=console,signal=off,mux=on"])
qemuargs.extend(["-serial", "chardev:console"])
qemuargs.extend(["-mon", "chardev=console"])

kernelargs.extend(arch.serial_console_args())

qemuargs.extend(arch.qemu_nodisplay_args())

# PS/2 probing is slow; give the kernel a hint to speed it up.
kernelargs.extend(["psmouse.proto=exps"])

Expand Down
10 changes: 9 additions & 1 deletion virtme/guest/virtme-init
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,11 @@ if [[ -n "${user_cmd}" ]]; then
fi

# Figure out what the main console is
consdev="`grep ' ... (.C' /proc/consoles |cut -d' ' -f1`"
if [[ -n "${virtme_console}" ]]; then
consdev=${virtme_console}
else
consdev="`grep ' ... (.C' /proc/consoles |cut -d' ' -f1`"
fi
if [[ -z "$consdev" ]]; then
log "can't deduce console device"
exec bash --login # At least try to be helpful
Expand All @@ -335,6 +339,10 @@ if [[ ! -e "/dev/$consdev" ]]; then
exec bash --login
fi

# Redirect current stdout/stderr to consdev
exec 1>/dev/${consdev}
exec 2>&1

# Parameters that start with virtme_ shouldn't pollute the environment
for p in "${!virtme_@}"; do export -n "$p"; done

Expand Down
2 changes: 1 addition & 1 deletion virtme_ng_init
Submodule virtme_ng_init updated 1 files
+40 −1 src/main.rs

0 comments on commit ecd8006

Please sign in to comment.