feat: implement exec and forward commands for running commands in sandboxes
- Added `exec` command to run a shell or command in a running sandbox, utilizing a new exec server. - Introduced `forward` command to expose ports of a running sandbox on the host's localhost. - Updated Cargo.toml and Cargo.lock to reflect version bump and new dependencies. - Enhanced README.md with usage instructions for new commands. - Refactored build.rs to include new guest helper files for exec functionality. - Created new source files for exec and forward logic, including protocol handling and connection management.
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
//! Booting a sandbox: run script, vfkit invocation, guest results.
|
||||
use crate::disk::create_disk;
|
||||
use crate::exec;
|
||||
use crate::kernel::kernel;
|
||||
use crate::oci;
|
||||
use crate::sandbox::lock_stopped;
|
||||
@@ -17,7 +18,7 @@ use std::process::{Command, Stdio};
|
||||
|
||||
const INIT: &str = include_str!("init.sh");
|
||||
const DHCP: &str = include_str!("dhcp.sh");
|
||||
const FWD: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/wsbx-fwd"));
|
||||
const GUEST: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/wsbx-guest"));
|
||||
|
||||
/// Agent-specific guest preparation.
|
||||
#[derive(Default)]
|
||||
@@ -97,7 +98,7 @@ pub fn run(h: &Path, o: Opts, image: &str, cmd: Vec<String>, extra: &[String], g
|
||||
let mut s = String::from("B=/.wsbx/busybox\n");
|
||||
s += &format!("$B date -s @{} >/dev/null\n", now());
|
||||
s += &format!("$B hostname {name}; echo {name} > /etc/hostname; $B grep -q ' {name}$' /etc/hosts 2>/dev/null || echo '127.0.1.1 {name}' >> /etc/hosts\n");
|
||||
if let Some((rows @ 1.., cols @ 1..)) = term_size() {
|
||||
if let Some((rows, cols)) = term_size() {
|
||||
s += &format!("$B stty rows {rows} cols {cols}\n");
|
||||
}
|
||||
if let Some(ws) = &ws {
|
||||
@@ -132,30 +133,39 @@ pub fn run(h: &Path, o: Opts, image: &str, cmd: Vec<String>, extra: &[String], g
|
||||
}
|
||||
s += &g.setup;
|
||||
|
||||
// Host tunnel: guest wsbx-fwd listens on localhost:PORT -> vsock -> vfkit -> unix socket -> host localhost:PORT
|
||||
s += &format!("cd {} 2>/dev/null || cd /\n", sq(&wd));
|
||||
|
||||
// Guest helper (guest/*.go), started after the cd so exec sessions inherit run.sh's env and working dir.
|
||||
// `wsbx exec`: host -> vfkit listening on exec.sock -> vsock -> helper (runs the command, pty or pipes)
|
||||
// Host tunnel: helper listens on guest localhost:PORT -> vsock -> vfkit -> unix socket -> host localhost:PORT
|
||||
fs::write(share.join("guest"), GUEST)?;
|
||||
fs::set_permissions(share.join("guest"), fs::Permissions::from_mode(0o755))?;
|
||||
let (mut vsock, mut args) = (Vec::new(), Vec::new());
|
||||
let exec_sock = dir.join("exec.sock");
|
||||
let _ = fs::remove_file(&exec_sock); // left over if vfkit was killed; vfkit can't listen on it then
|
||||
if exec_sock.as_os_str().len() <= 100 {
|
||||
// `connect`: vfkit listens on the unix socket and dials the guest (vfkit defaults to `listen`)
|
||||
vsock.push(format!("virtio-vsock,port={},socketURL={},connect", exec::VPORT, vf(&exec_sock)?));
|
||||
args.push(format!("-exec {}", exec::VPORT));
|
||||
} else {
|
||||
eprintln!("wsbx: note: `wsbx exec` unavailable, socket path too long (set WSBX_HOME to something shorter)");
|
||||
}
|
||||
let mut ports = o.host_ports.clone();
|
||||
ports.sort();
|
||||
ports.dedup();
|
||||
let mut vsock = Vec::new();
|
||||
for (i, &p) in ports.iter().enumerate() {
|
||||
let vport = 10000 + i as u32;
|
||||
let sock = dir.join(format!("p{p}.sock"));
|
||||
tunnel(&sock, p)?;
|
||||
vsock.push(format!("virtio-vsock,port={vport},socketURL={},listen", vf(&sock)?));
|
||||
args.push(format!("{p}={vport}"));
|
||||
}
|
||||
if !ports.is_empty() {
|
||||
let fwd = share.join("fwd");
|
||||
fs::write(&fwd, FWD)?;
|
||||
fs::set_permissions(&fwd, fs::Permissions::from_mode(0o755))?;
|
||||
let mut specs = Vec::new();
|
||||
for (i, &p) in ports.iter().enumerate() {
|
||||
let vport = 10000 + i as u32;
|
||||
let sock = dir.join(format!("p{p}.sock"));
|
||||
tunnel(&sock, p)?;
|
||||
vsock.push(format!("virtio-vsock,port={vport},socketURL={},listen", vf(&sock)?));
|
||||
specs.push(format!("{p}={vport}"));
|
||||
}
|
||||
s += "$B mkfifo /run/wsbx-fwd\n";
|
||||
s += &format!("/.wsbx/host/fwd {} >/run/wsbx-fwd 2>/.wsbx/host/fwd.log &\n", specs.join(" "));
|
||||
s += "read _ </run/wsbx-fwd\n";
|
||||
eprintln!("wsbx: host localhost:{{{}}} tunneled into the VM", ports.iter().map(u16::to_string).collect::<Vec<_>>().join(","));
|
||||
}
|
||||
|
||||
s += &format!("cd {} 2>/dev/null || cd /\n", sq(&wd));
|
||||
s += "$B mkfifo /run/wsbx-guest\n";
|
||||
s += &format!("/.wsbx/host/guest {} >/run/wsbx-guest 2>/.wsbx/host/guest.log &\n", args.join(" "));
|
||||
s += "read _ </run/wsbx-guest\n";
|
||||
let argv: Vec<String> = argv.iter().map(|a| sq(a)).collect();
|
||||
if u.is_empty() || u == "root" || u == "0" {
|
||||
s += &format!("exec {}\n", argv.join(" "));
|
||||
@@ -253,15 +263,17 @@ fn out_path() -> PathBuf {
|
||||
fs::read_link("/dev/fd/1").unwrap_or("/dev/stdout".into())
|
||||
}
|
||||
|
||||
fn stty(args: &[&str]) -> Option<String> {
|
||||
pub fn stty(args: &[&str]) -> Option<String> {
|
||||
let out = Command::new("stty").args(args).stdin(Stdio::inherit()).stderr(Stdio::null()).output().ok()?;
|
||||
out.status.success().then(|| String::from_utf8_lossy(&out.stdout).into_owned())
|
||||
}
|
||||
|
||||
fn term_size() -> Option<(u32, u32)> {
|
||||
let s = stty(&["size"])?;
|
||||
let mut it = s.split_whitespace().map(|x| x.parse().ok());
|
||||
Some((it.next()??, it.next()??))
|
||||
/// Rows and columns of the terminal on stdin.
|
||||
pub fn term_size() -> Option<(u16, u16)> {
|
||||
// SAFETY: TIOCGWINSZ only writes a winsize struct.
|
||||
let mut ws: libc::winsize = unsafe { std::mem::zeroed() };
|
||||
let ok = unsafe { libc::ioctl(0, libc::TIOCGWINSZ, &mut ws) } == 0;
|
||||
(ok && ws.ws_row > 0 && ws.ws_col > 0).then_some((ws.ws_row, ws.ws_col))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
Reference in New Issue
Block a user