3
3
use std:: process:: Stdio ;
4
4
5
5
use anyhow:: anyhow;
6
+ use fallible_collections:: tryformat;
6
7
use tokio:: process:: Command ;
7
8
use tokio_seqpacket:: UnixSeqpacket ;
8
9
use uuid:: Uuid ;
@@ -27,7 +28,16 @@ pub struct Container {
27
28
impl Container {
28
29
/// Spawns a new container with the given `id` from the `rt` OCI bundle.
29
30
pub async fn create ( id : & str , rt : OciBundle ) -> anyhow:: Result < Self > {
31
+ let id = tryformat ! ( 64 , "{}" , id) . map_err ( |e| anyhow ! ( "OOM error: {:?}" , e) ) ?;
30
32
let uuid = Uuid :: new_v4 ( ) ;
33
+ let uuid_str = tryformat ! ( 36 , "{}" , uuid) . map_err ( |e| anyhow ! ( "OOM error: {:?}" , e) ) ?;
34
+
35
+ let bundle_dir = rt. bundle_dir . to_str ( ) . expect ( "$TMPDIR is invalid UTF-8" ) ;
36
+ let exits_dir = rt. exits_dir . to_str ( ) . expect ( "$TMPDIR is invalid UTF-8" ) ;
37
+ let log_file = rt. log_file . to_str ( ) . expect ( "$TMPDIR is invalid UTF-8" ) ;
38
+ let pid_file = rt. pid_file . to_str ( ) . expect ( "$TMPDIR is invalid UTF-8" ) ;
39
+ let sock_dir = rt. base_dir ( ) . to_str ( ) . expect ( "$TMPDIR is invalid UTF-8" ) ;
40
+
31
41
let start_pipe = StartPipe :: new ( ) ?;
32
42
let mut sync_pipe = SyncPipe :: new ( ) ?;
33
43
@@ -38,15 +48,15 @@ impl Container {
38
48
. args ( & [ "--syslog" , "--log-level=debug" ] )
39
49
. arg ( "--terminal" ) // Passes `--console-sock` to `crun`.
40
50
. args ( & [ "--cid" , & id] )
41
- . args ( & [ "--cuuid" , & uuid . to_string ( ) ] )
51
+ . args ( & [ "--cuuid" , & uuid_str ] )
42
52
. args ( & [ "--name" , & id] )
43
53
. args ( & [ "--runtime" , RUNTIME_BIN ] )
44
54
. args ( & [ "--runtime-arg" , "--rootless=true" ] )
45
- . args ( & [ "--bundle" , & rt . bundle_dir . display ( ) . to_string ( ) ] )
46
- . args ( & [ "--exit-dir" , & rt . exits_dir . display ( ) . to_string ( ) ] )
47
- . args ( & [ "--log-path" , & rt . log_file . display ( ) . to_string ( ) ] )
48
- . args ( & [ "--container-pidfile" , & rt . pid_file . display ( ) . to_string ( ) ] )
49
- . args ( & [ "--socket-dir-path" , & rt . base_dir ( ) . display ( ) . to_string ( ) ] )
55
+ . args ( & [ "--bundle" , bundle_dir] )
56
+ . args ( & [ "--exit-dir" , exits_dir] )
57
+ . args ( & [ "--log-path" , log_file] )
58
+ . args ( & [ "--container-pidfile" , pid_file] )
59
+ . args ( & [ "--socket-dir-path" , sock_dir ] )
50
60
. inherit_oci_pipes ( & start_pipe, & sync_pipe)
51
61
. spawn ( ) ?;
52
62
@@ -86,12 +96,12 @@ impl Container {
86
96
eprintln ! ( "received PID {}, connecting to console socket..." , pid) ;
87
97
88
98
// Setup is complete, so connect to the console socket.
89
- let sock_path = rt. base_dir ( ) . join ( uuid . to_string ( ) ) . join ( "attach" ) ;
99
+ let sock_path = rt. base_dir ( ) . join ( uuid_str ) . join ( "attach" ) ;
90
100
let console_sock = UnixSeqpacket :: connect ( sock_path) . await ?;
91
101
eprintln ! ( "connected to console socket!" ) ;
92
102
93
103
Ok ( Container {
94
- id : id . to_string ( ) ,
104
+ id,
95
105
uuid,
96
106
pid,
97
107
console_sock,
0 commit comments