Skip to content

Commit

Permalink
virtme-ng: introduce --console and --ssh shortcuts
Browse files Browse the repository at this point in the history
Simplify the remote console and ssh options for the vng front-end:

 - to start a console or ssh server:
   $ vng --console [PORT]
   $ vng --ssh [PORT]

 - to start a console or ssh client session:
   $ vng --console-client [PORT]
   $ vng --ssh-client [PORT]

Signed-off-by: Andrea Righi <[email protected]>
  • Loading branch information
arighi committed Dec 28, 2024
1 parent 077b6af commit 7b53b12
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 36 deletions.
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,9 @@ Requirements
* Optionally, you may need virtiofsd 1.7.0 (or higher) for better filesystem
performance inside the virtme-ng guests.

* Optionally, you may need `socat` for the `--server` and `--client` options,
and the host's kernel should support VSOCK (`CONFIG_VHOST_VSOCK`).
* Optionally, you may need `socat` for the `--console` and
`--console-client` options, and the host's kernel should support VSOCK
(`CONFIG_VHOST_VSOCK`).

Examples
========
Expand Down Expand Up @@ -382,26 +383,26 @@ Examples
- Connect to a simple remote shell (`socat` is required, VSOCK will be used):
```
# Start the vng instance with server support:
$ vng --server
$ vng --console
# In a separate terminal run the following command to connect to a remote shell:
$ vng --client
$ vng --console-client
```

- Enable ssh in the vng guest:
```
# Start the vng instance with ssh server support:
$ vng --server ssh
$ vng --ssh
# Connect to the vng guest from the host via ssh:
$ vng --client ssh
$ vng --ssh-client
```

- Generate some results inside the vng guest and copy them back to the
host using scp:
```
# Start the vng instance with SSH server support:
arighi@host~> vng --server ssh
arighi@host~> vng --ssh
...
arighi@virtme-ng~> ./run.sh > result.txt
Expand Down
89 changes: 60 additions & 29 deletions virtme_ng/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,32 +487,46 @@ def make_parser():
)

g_remote = parser.add_argument_group(title="Remote Console")
cli_srv_choices = ["console", "ssh"]

g_remote.add_argument(
"--server",
"--console",
action="store",
const=cli_srv_choices[0],
nargs="?",
choices=cli_srv_choices,
help="Enable a server to communicate later from the host to the device using '--client'. "
type=int,
const=2222,
metavar="PORT",
help="Enable a server to communicate later from the host using '--console-client'. "
+ "By default, a simple console will be offered using a VSOCK connection, and 'socat' for the proxy."
)

g_remote.add_argument(
"--client",
"--console-client",
action="store",
nargs="?",
type=int,
const=2222,
metavar="PORT",
help="Connect to a VM launched with the '--console' option for a remote control.",
)

g_remote.add_argument(
"--ssh",
action="store",
const=cli_srv_choices[0],
nargs="?",
choices=cli_srv_choices,
help="Connect to a VM launched with the '--server' option for a remote control.",
type=int,
const=2222,
metavar="PORT",
help="Enable SSH server to communicate later from the host to using '--ssh-client'."
)

g_remote.add_argument(
"--port",
"--ssh-client",
action="store",
nargs="?",
type=int,
help="Unique port to communicate with a VM.",
const=2222,
metavar="PORT",
help="Connect to a VM launched with the '--ssh' option for a remote control.",
)

g_remote.add_argument(
Expand Down Expand Up @@ -992,23 +1006,38 @@ def _get_virtme_net_mac_address(self, args):
else:
self.virtme_param["net_mac_address"] = ""

def _get_virtme_server(self, args):
if args.server is not None:
self.virtme_param["server"] = "--server " + args.server
def _get_virtme_console(self, args):
if args.console is not None and args.ssh is not None:
arg_fail('--console cannot be used with --ssh', show_usage=False)

if args.console is not None:
self.virtme_param["console"] = f"--server console --port {args.console}"
else:
self.virtme_param["server"] = ""
self.virtme_param["console"] = ""

def _get_virtme_console_client(self, args):
if args.console_client is not None:
self.virtme_param["console_client"] = f"--client console --port {args.console_client}"
else:
self.virtme_param["console_client"] = ""

def _get_virtme_ssh(self, args):
if args.console is not None and args.ssh is not None:
arg_fail('--console cannot be used with --ssh', show_usage=False)

if args.ssh is not None and args.root is not None:
arg_fail('--ssh is not currently compatible with --root', show_usage=False)

def _get_virtme_client(self, args):
if args.client is not None:
self.virtme_param["client"] = "--client " + args.client
if args.ssh is not None:
self.virtme_param["ssh"] = f"--server ssh --port {args.ssh}"
else:
self.virtme_param["client"] = ""
self.virtme_param["ssh"] = ""

def _get_virtme_port(self, args):
if args.port is not None:
self.virtme_param["port"] = "--port " + str(args.port)
def _get_virtme_ssh_client(self, args):
if args.ssh_client is not None:
self.virtme_param["ssh_client"] = f"--client ssh --port {args.ssh_client}"
else:
self.virtme_param["port"] = ""
self.virtme_param["ssh_client"] = ""

def _get_virtme_remote_cmd(self, args):
if args.remote_cmd is not None:
Expand Down Expand Up @@ -1190,9 +1219,10 @@ def run(self, args):
self._get_virtme_mods(args)
self._get_virtme_network(args)
self._get_virtme_net_mac_address(args)
self._get_virtme_server(args)
self._get_virtme_client(args)
self._get_virtme_port(args)
self._get_virtme_console(args)
self._get_virtme_console_client(args)
self._get_virtme_ssh(args)
self._get_virtme_ssh_client(args)
self._get_virtme_remote_cmd(args)
self._get_virtme_disk(args)
self._get_virtme_sound(args)
Expand Down Expand Up @@ -1234,9 +1264,10 @@ def run(self, args):
+ f'{self.virtme_param["mods"]} '
+ f'{self.virtme_param["network"]} '
+ f'{self.virtme_param["net_mac_address"]} '
+ f'{self.virtme_param["server"]} '
+ f'{self.virtme_param["client"]} '
+ f'{self.virtme_param["port"]} '
+ f'{self.virtme_param["console"]} '
+ f'{self.virtme_param["console_client"]} '
+ f'{self.virtme_param["ssh"]} '
+ f'{self.virtme_param["ssh_client"]} '
+ f'{self.virtme_param["remote_cmd"]} '
+ f'{self.virtme_param["disk"]} '
+ f'{self.virtme_param["sound"]} '
Expand Down

0 comments on commit 7b53b12

Please sign in to comment.