From bd01c91970128fb631a8bfeae5cf77a86a7ae4f5 Mon Sep 17 00:00:00 2001 From: "Matthieu Baerts (NGI0)" Date: Fri, 29 Nov 2024 18:58:43 +0100 Subject: [PATCH] vsock: socat service for remote console When virtme.vsockexec=`` is set, socat is started in the background, listening to a VSock connection on the port 1024: once connected, a pty console is started with the given , e.g. 'bash -i'. This allows a simple remote control. Link: https://github.com/arighi/virtme-ng/discussions/151 Signed-off-by: Matthieu Baerts (NGI0) --- src/main.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/main.rs b/src/main.rs index 82e3cb6..8198d0b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1023,6 +1023,28 @@ fn run_snapd() { } } +fn extract_vsock_exec(cmdline: &str) -> Option { + let start_marker = "virtme.vsockexec=`"; + let end_marker = '`'; + + let (_before, remaining) = cmdline.split_once(start_marker)?; + let (encoded_cmd, _after) = remaining.split_once(end_marker)?; + Some(encoded_cmd.to_string()) +} + +fn setup_socat_console() { + if let Ok(cmdline) = std::fs::read_to_string("/proc/cmdline") { + if let Some(exec) = extract_vsock_exec(&cmdline) { + thread::spawn(move || { + let from = "VSOCK-LISTEN:1024,reuseaddr,fork"; + let to = format!("EXEC:\"{}\",pty,stderr,setsid,sigint,sane,echo=0", exec); + let args = vec![from, &to]; + utils::run_cmd("socat", &args); + }); + } + } +} + fn run_misc_services() -> thread::JoinHandle<()> { thread::spawn(|| { symlink_fds(); @@ -1061,6 +1083,9 @@ fn main() { mount_kernel_modules(); run_systemd_tmpfiles(); + // Service running in the background for later + setup_socat_console(); + // Service initialization (some services can be parallelized here). let mut handles = vec![run_udevd(), Some(run_misc_services())]; handles.append(&mut setup_network());