3
3
4
4
use std:: ffi:: CString ;
5
5
use std:: fs:: File ;
6
+ use std:: io:: { Error , ErrorKind } ;
6
7
use std:: os:: unix:: io:: AsRawFd ;
8
+ #[ cfg( target_os = "macos" ) ]
7
9
use std:: path:: Path ;
8
10
9
11
use super :: bindings;
10
12
use super :: utils:: { mount_container, umount_container} ;
11
13
use crate :: { ArgMatches , KrunvmConfig , VmConfig } ;
12
14
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 ( ) ;
23
20
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
+ }
29
40
}
41
+ }
30
42
43
+ #[ cfg( target_os = "macos" ) ]
44
+ fn map_volumes ( ctx : u32 , vmcfg : & VmConfig , _rootfs : & str ) {
31
45
let mut volumes = Vec :: new ( ) ;
32
46
for ( host_path, guest_path) in vmcfg. mapped_volumes . iter ( ) {
33
47
let full_guest = format ! ( "{}{}" , & rootfs, guest_path) ;
@@ -49,6 +63,27 @@ unsafe fn exec_vm(vmcfg: &VmConfig, rootfs: &str, cmd: &str, args: Vec<CString>)
49
63
println ! ( "Error setting VM mapped volumes" ) ;
50
64
std:: process:: exit ( -1 ) ;
51
65
}
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) ;
52
87
53
88
let mut ports = Vec :: new ( ) ;
54
89
for ( host_port, guest_port) in vmcfg. mapped_ports . iter ( ) {
0 commit comments