From 86995ee24a8601b15a503c2af66e96a2c9ad6284 Mon Sep 17 00:00:00 2001 From: Andrea Righi Date: Fri, 2 Feb 2024 09:52:22 +0100 Subject: [PATCH] virtme-ng: print an explicit error when a valid pts is not found When running in script-mode we need to channel the stdin/stdout/stderr of the guest command into the host's stdin/stdout/stderr. However, if we don't have read/write permission to operate on the current pts we may get the following (quite obscure) error: qemu-system-x86_64: -chardev file,id=stdout,path=/proc/self/fd/1: Could not open '/proc/self/fd/1': Permission denied For example, it is really easy to reproduce this problem switching to a different user and then running vng: arighi@gpd$ sudo su - sbuild sbuild@gpd:~$ vng -r -- uname -r qemu-system-x86_64: -chardev file,id=stdout,path=/proc/self/fd/1: Could not open '/proc/self/fd/1': Permission denied Instead of printing the qemu error, let's check directly the current stdin/stdout/stderr and if they don't exist or if we don't have the proper permissions to access them, report a more clear error (with a short hint on how to solve this issue): sbuild@gpd:~$ vng -r -- uname -r ERROR: not a valid pts, try to run vng inside tmux or screen This can help to save some time for those that may run virtme-ng after switching to a different user or any other similar scenarios, as discussed in #60. Signed-off-by: Andrea Righi --- virtme/commands/run.py | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/virtme/commands/run.py b/virtme/commands/run.py index 762c6d8..87dadcc 100644 --- a/virtme/commands/run.py +++ b/virtme/commands/run.py @@ -725,13 +725,11 @@ def sanitize_disk_args(func: str, arg: str) -> Tuple[str, str]: return name, fn -def can_use_kvm(args): - if args.disable_kvm: - return False - if not os.path.exists("/dev/kvm"): +def can_access_file(path): + if not os.path.exists(path): return False try: - fd = os.open('/dev/kvm', os.O_RDWR | os.O_CLOEXEC) + fd = os.open(path, os.O_RDWR | os.O_CLOEXEC) os.close(fd) return True @@ -739,6 +737,12 @@ def can_use_kvm(args): return False +def can_use_kvm(args): + if args.disable_kvm: + return False + return can_access_file("/dev/kvm") + + def can_use_microvm(args): return not args.disable_microvm and not args.numa and args.arch == "x86_64" and can_use_kvm(args) @@ -1084,6 +1088,16 @@ def do_script(shellcmd: str, ret_path=None, show_boot_console=False) -> None: # Turn off default I/O qemuargs.extend(arch.qemu_nodisplay_args()) + # Check if we can redirect stdin/stdout/stderr. + if not can_access_file("/proc/self/fd/0") or \ + not can_access_file("/proc/self/fd/1") or \ + 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) + # Configure kernel console output if show_boot_console: output = "/proc/self/fd/2"