From bf096ece6e77cee422cd751fcc46fb6c993c0fdd Mon Sep 17 00:00:00 2001 From: Andrea Righi Date: Mon, 23 Dec 2024 14:47:01 +0100 Subject: [PATCH] virtme-ng: introduce --console and --ssh shortcuts 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 --- README.md | 18 ++++++---- virtme_ng/run.py | 92 +++++++++++++++++++++++++++++++++--------------- 2 files changed, 74 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index 0b46a3f..cc695d8 100644 --- a/README.md +++ b/README.md @@ -129,8 +129,12 @@ 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`). + + * Optionally, you may need `sshd` installed for the `--ssh` and + `--ssh-client` options. Examples ======== @@ -382,26 +386,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 diff --git a/virtme_ng/run.py b/virtme_ng/run.py index 9d824fd..a8e720e 100644 --- a/virtme_ng/run.py +++ b/virtme_ng/run.py @@ -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", - 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="Connect to a VM launched with the '--console' option for a remote control.", ) g_remote.add_argument( - "--port", + "--ssh", action="store", + nargs="?", type=int, - help="Unique port to communicate with a VM.", + const=2222, + metavar="PORT", + help="Enable SSH server to communicate later from the host to using '--ssh-client'." + ) + + g_remote.add_argument( + "--ssh-client", + action="store", + nargs="?", + type=int, + const=2222, + metavar="PORT", + help="Connect to a VM launched with the '--ssh' option for a remote control.", ) g_remote.add_argument( @@ -992,23 +1006,41 @@ 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: + self.virtme_param["console"] = f"--server console --port {args.console}" else: - self.virtme_param["server"] = "" + self.virtme_param["console"] = "" - def _get_virtme_client(self, args): - if args.client is not None: - self.virtme_param["client"] = "--client " + args.client + 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["client"] = "" + 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) + + if args.ssh is not None and args.rw: + arg_fail('--ssh is not currently compatible with --rw', show_usage=False) + + if args.ssh is not None: + self.virtme_param["ssh"] = f"--server ssh --port {args.ssh}" + else: + self.virtme_param["ssh"] = "" + + def _get_virtme_ssh_client(self, args): + if args.console_client is not None and args.ssh_client is not None: + arg_fail('--console-client cannot be used with --ssh-client', show_usage=False) - def _get_virtme_port(self, args): - if args.port is not None: - self.virtme_param["port"] = "--port " + str(args.port) + 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: @@ -1190,9 +1222,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) @@ -1234,9 +1267,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"]} '