Skip to content

Commit

Permalink
Merge pull request #27 from slp/use-set-env
Browse files Browse the repository at this point in the history
Use krun_set_env to set env vars when no command is configured
  • Loading branch information
slp authored Aug 4, 2022
2 parents 1f7a7d1 + 81444b5 commit a99b7b9
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "krunvm"
version = "0.1.6"
version = "0.2.0"
authors = ["Sergio Lopez <[email protected]>"]
description = "Create microVMs from OCI images"
repository = "https://github.com/containers/krunvm"
Expand Down
1 change: 1 addition & 0 deletions src/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ extern "C" {
argv: *const *const i8,
envp: *const *const i8,
) -> i32;
pub fn krun_set_env(ctx: u32, envp: *const *const i8) -> i32;
pub fn krun_start_enter(ctx: u32) -> i32;
}
24 changes: 14 additions & 10 deletions src/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,23 +111,21 @@ unsafe fn exec_vm(vmcfg: &VmConfig, rootfs: &str, cmd: Option<&str>, args: Vec<C
}
}

let hostname = CString::new(format!("HOSTNAME={}", vmcfg.name)).unwrap();
let home = CString::new("HOME=/root").unwrap();
let env: [*const i8; 3] = [
hostname.as_ptr() as *const i8,
home.as_ptr() as *const i8,
std::ptr::null(),
];

if let Some(cmd) = cmd {
let mut argv: Vec<*const i8> = Vec::new();
for a in args.iter() {
argv.push(a.as_ptr() as *const i8);
}
argv.push(std::ptr::null());

let hostname = CString::new(format!("HOSTNAME={}", vmcfg.name)).unwrap();
let home = CString::new("HOME=/root").unwrap();
let path = CString::new("PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin").unwrap();
let env: [*const i8; 4] = [
hostname.as_ptr() as *const i8,
home.as_ptr() as *const i8,
path.as_ptr() as *const i8,
std::ptr::null(),
];

let c_cmd = CString::new(cmd).unwrap();
let ret = bindings::krun_set_exec(
ctx,
Expand All @@ -139,6 +137,12 @@ unsafe fn exec_vm(vmcfg: &VmConfig, rootfs: &str, cmd: Option<&str>, args: Vec<C
println!("Error setting VM config");
std::process::exit(-1);
}
} else {
let ret = bindings::krun_set_env(ctx, env.as_ptr() as *const *const i8);
if ret < 0 {
println!("Error setting VM environment variables");
std::process::exit(-1);
}
}

let ret = bindings::krun_start_enter(ctx);
Expand Down

0 comments on commit a99b7b9

Please sign in to comment.