Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ extern "C" {
pub fn krun_set_port_map(ctx: u32, port_map: *const *const c_char) -> i32;
pub fn krun_set_workdir(ctx: u32, workdir_path: *const c_char) -> i32;
pub fn krun_add_virtiofs(ctx: u32, tag: *const c_char, path: *const c_char) -> i32;
pub fn krun_add_vsock_port(ctx: u32, port: u32, filepath: *const c_char) -> i32;
pub fn krun_set_exec(
ctx: u32,
exec_path: *const c_char,
Expand Down
46 changes: 45 additions & 1 deletion src/commands/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ pub struct StartCmd {
/// env(s) in format "key=value" to be exposed to the VM
#[arg(long = "env")]
envs: Option<Vec<String>>,

/// vsock port to host Unix socket mapping(s) in format
/// "PORT:/host/path.sock". Guest connections to AF_VSOCK
/// (CID 2, PORT) are proxied to the Unix socket on the host.
#[arg(long = "vsock")]
vsocks: Option<Vec<String>>,
}

impl StartCmd {
Expand Down Expand Up @@ -76,11 +82,39 @@ impl StartCmd {
Vec::new()
};

let vsock_ports: Vec<(u32, String)> = self
.vsocks
.unwrap_or_default()
.into_iter()
.map(|val| match val.split_once(':') {
Some((port, path)) => match port.parse::<u32>() {
Ok(port) => (port, path.to_string()),
Err(_) => {
println!("Invalid vsock port in mapping: {}", val);
std::process::exit(-1);
}
},
None => {
println!("Invalid vsock mapping (expected PORT:PATH): {}", val);
std::process::exit(-1);
}
})
.collect();

set_rlimits();

let _file = set_lock(&rootfs);

unsafe { exec_vm(vmcfg, &rootfs, self.command.as_deref(), vm_args, env_pairs) };
unsafe {
exec_vm(
vmcfg,
&rootfs,
self.command.as_deref(),
vm_args,
env_pairs,
vsock_ports,
)
};

umount_container(cfg, vmcfg).expect("Error unmounting container");
}
Expand Down Expand Up @@ -143,6 +177,7 @@ unsafe fn exec_vm(
cmd: Option<&str>,
args: Vec<CString>,
env_pairs: Vec<CString>,
vsock_ports: Vec<(u32, String)>,
) {
//bindings::krun_set_log_level(9);

Expand Down Expand Up @@ -185,6 +220,15 @@ unsafe fn exec_vm(
std::process::exit(-1);
}

for (port, path) in vsock_ports.iter() {
let c_path = CString::new(path.as_str()).unwrap();
let ret = bindings::krun_add_vsock_port(ctx, *port, c_path.as_ptr());
if ret < 0 {
println!("Error adding vsock port {} -> {}", port, path);
std::process::exit(-1);
}
}

if !vmcfg.workdir.is_empty() {
let c_workdir = CString::new(vmcfg.workdir.clone()).unwrap();
let ret = bindings::krun_set_workdir(ctx, c_workdir.as_ptr());
Expand Down