Skip to content

Commit a99b7b9

Browse files
authored
Merge pull request #27 from slp/use-set-env
Use krun_set_env to set env vars when no command is configured
2 parents 1f7a7d1 + 81444b5 commit a99b7b9

File tree

4 files changed

+17
-12
lines changed

4 files changed

+17
-12
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "krunvm"
3-
version = "0.1.6"
3+
version = "0.2.0"
44
authors = ["Sergio Lopez <[email protected]>"]
55
description = "Create microVMs from OCI images"
66
repository = "https://github.com/containers/krunvm"

src/bindings.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ extern "C" {
1717
argv: *const *const i8,
1818
envp: *const *const i8,
1919
) -> i32;
20+
pub fn krun_set_env(ctx: u32, envp: *const *const i8) -> i32;
2021
pub fn krun_start_enter(ctx: u32) -> i32;
2122
}

src/start.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -111,23 +111,21 @@ unsafe fn exec_vm(vmcfg: &VmConfig, rootfs: &str, cmd: Option<&str>, args: Vec<C
111111
}
112112
}
113113

114+
let hostname = CString::new(format!("HOSTNAME={}", vmcfg.name)).unwrap();
115+
let home = CString::new("HOME=/root").unwrap();
116+
let env: [*const i8; 3] = [
117+
hostname.as_ptr() as *const i8,
118+
home.as_ptr() as *const i8,
119+
std::ptr::null(),
120+
];
121+
114122
if let Some(cmd) = cmd {
115123
let mut argv: Vec<*const i8> = Vec::new();
116124
for a in args.iter() {
117125
argv.push(a.as_ptr() as *const i8);
118126
}
119127
argv.push(std::ptr::null());
120128

121-
let hostname = CString::new(format!("HOSTNAME={}", vmcfg.name)).unwrap();
122-
let home = CString::new("HOME=/root").unwrap();
123-
let path = CString::new("PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin").unwrap();
124-
let env: [*const i8; 4] = [
125-
hostname.as_ptr() as *const i8,
126-
home.as_ptr() as *const i8,
127-
path.as_ptr() as *const i8,
128-
std::ptr::null(),
129-
];
130-
131129
let c_cmd = CString::new(cmd).unwrap();
132130
let ret = bindings::krun_set_exec(
133131
ctx,
@@ -139,6 +137,12 @@ unsafe fn exec_vm(vmcfg: &VmConfig, rootfs: &str, cmd: Option<&str>, args: Vec<C
139137
println!("Error setting VM config");
140138
std::process::exit(-1);
141139
}
140+
} else {
141+
let ret = bindings::krun_set_env(ctx, env.as_ptr() as *const *const i8);
142+
if ret < 0 {
143+
println!("Error setting VM environment variables");
144+
std::process::exit(-1);
145+
}
142146
}
143147

144148
let ret = bindings::krun_start_enter(ctx);

0 commit comments

Comments
 (0)