Skip to content

Commit

Permalink
virtme-ng: print an explicit error when a valid pts is not found
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
Andrea Righi committed Feb 2, 2024
1 parent abfca37 commit 86995ee
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions virtme/commands/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -725,20 +725,24 @@ 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

except OSError:
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)

Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 86995ee

Please sign in to comment.