Skip to content

Commit da0a1c4

Browse files
committed
Map volumes using bind-mount on Linux
As we expect to be running inside a "buildah unshare" session, which is a user namespace, use bind-mount for mounting volumes on Linux. Signed-off-by: Sergio Lopez <[email protected]>
1 parent 77e299a commit da0a1c4

File tree

1 file changed

+50
-15
lines changed

1 file changed

+50
-15
lines changed

src/start.rs

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,45 @@
33

44
use std::ffi::CString;
55
use std::fs::File;
6+
use std::io::{Error, ErrorKind};
67
use std::os::unix::io::AsRawFd;
8+
#[cfg(target_os = "macos")]
79
use std::path::Path;
810

911
use super::bindings;
1012
use super::utils::{mount_container, umount_container};
1113
use crate::{ArgMatches, KrunvmConfig, VmConfig};
1214

13-
unsafe fn exec_vm(vmcfg: &VmConfig, rootfs: &str, cmd: &str, args: Vec<CString>) {
14-
//bindings::krun_set_log_level(9);
15-
16-
let ctx = bindings::krun_create_ctx() as u32;
17-
18-
let ret = bindings::krun_set_vm_config(ctx, vmcfg.cpus as u8, vmcfg.mem);
19-
if ret < 0 {
20-
println!("Error setting VM config");
21-
std::process::exit(-1);
22-
}
15+
#[cfg(target_os = "linux")]
16+
fn map_volumes(_ctx: u32, vmcfg: &VmConfig, rootfs: &str) {
17+
for (host_path, guest_path) in vmcfg.mapped_volumes.iter() {
18+
let host_dir = CString::new(host_path.to_string()).unwrap();
19+
let guest_dir = CString::new(format!("{}{}", rootfs, guest_path)).unwrap();
2320

24-
let c_rootfs = CString::new(rootfs).unwrap();
25-
let ret = bindings::krun_set_root(ctx, c_rootfs.as_ptr());
26-
if ret < 0 {
27-
println!("Error setting VM rootfs");
28-
std::process::exit(-1);
21+
let ret = unsafe { libc::mkdir(guest_dir.as_ptr(), 0o755) };
22+
if ret < 0 && Error::last_os_error().kind() != ErrorKind::AlreadyExists {
23+
println!("Error creating directory {:?}", guest_dir);
24+
std::process::exit(-1);
25+
}
26+
unsafe { libc::umount(guest_dir.as_ptr()) };
27+
let ret = unsafe {
28+
libc::mount(
29+
host_dir.as_ptr(),
30+
guest_dir.as_ptr(),
31+
std::ptr::null(),
32+
libc::MS_BIND | libc::MS_REC,
33+
std::ptr::null(),
34+
)
35+
};
36+
if ret < 0 {
37+
println!("Error mounting volume {}", guest_path);
38+
std::process::exit(-1);
39+
}
2940
}
41+
}
3042

43+
#[cfg(target_os = "macos")]
44+
fn map_volumes(ctx: u32, vmcfg: &VmConfig, _rootfs: &str) {
3145
let mut volumes = Vec::new();
3246
for (host_path, guest_path) in vmcfg.mapped_volumes.iter() {
3347
let full_guest = format!("{}{}", &rootfs, guest_path);
@@ -49,6 +63,27 @@ unsafe fn exec_vm(vmcfg: &VmConfig, rootfs: &str, cmd: &str, args: Vec<CString>)
4963
println!("Error setting VM mapped volumes");
5064
std::process::exit(-1);
5165
}
66+
}
67+
68+
unsafe fn exec_vm(vmcfg: &VmConfig, rootfs: &str, cmd: &str, args: Vec<CString>) {
69+
//bindings::krun_set_log_level(9);
70+
71+
let ctx = bindings::krun_create_ctx() as u32;
72+
73+
let ret = bindings::krun_set_vm_config(ctx, vmcfg.cpus as u8, vmcfg.mem);
74+
if ret < 0 {
75+
println!("Error setting VM config");
76+
std::process::exit(-1);
77+
}
78+
79+
let c_rootfs = CString::new(rootfs).unwrap();
80+
let ret = bindings::krun_set_root(ctx, c_rootfs.as_ptr());
81+
if ret < 0 {
82+
println!("Error setting VM rootfs");
83+
std::process::exit(-1);
84+
}
85+
86+
map_volumes(ctx, &vmcfg, rootfs);
5287

5388
let mut ports = Vec::new();
5489
for (host_port, guest_port) in vmcfg.mapped_ports.iter() {

0 commit comments

Comments
 (0)