Skip to content

Use krun_set_env to set env vars when no command is configured #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 4, 2022
Merged
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
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