@@ -57,39 +57,39 @@ command(s) that get executed on start, edit the args parameter of the spec. See
5757 Usage : "[ignore on runv] do not use pivot root to jail process inside rootfs. This should be used whenever the rootfs is on top of a ramdisk" ,
5858 },
5959 },
60- Action : func (context * cli.Context ) {
61- runContainer (context , true )
60+ Action : func (context * cli.Context ) error {
61+ if err := runContainer (context , true ); err != nil {
62+ return cli .NewExitError (fmt .Sprintf ("Run Container error: %v" , err ), - 1 )
63+ }
64+ return nil
6265 },
6366}
6467
65- func runContainer (context * cli.Context , createOnly bool ) {
68+ func runContainer (context * cli.Context , createOnly bool ) error {
6669 root := context .GlobalString ("root" )
6770 bundle := context .String ("bundle" )
6871 container := context .Args ().First ()
6972 ocffile := filepath .Join (bundle , specConfig )
7073 spec , err := loadSpec (ocffile )
7174 if err != nil {
72- fmt .Fprintf (os .Stderr , "load config failed: %v\n " , err )
73- os .Exit (- 1 )
75+ return fmt .Errorf ("load config failed: %v" , err )
7476 }
7577 if spec .Linux == nil {
76- fmt .Fprintf (os .Stderr , "it is not linux container config\n " )
77- os .Exit (- 1 )
78+ return fmt .Errorf ("it is not linux container config" )
7879 }
7980 if os .Geteuid () != 0 {
80- fmt .Fprintf (os .Stderr , "runv should be run as root\n " )
81- os .Exit (- 1 )
81+ return fmt .Errorf ("runv should be run as root" )
8282 }
8383 if container == "" {
84- fmt .Fprintf (os .Stderr , "no container id provided\n " )
85- os .Exit (- 1 )
84+ return fmt .Errorf ("no container id provided" )
8685 }
8786 _ , err = os .Stat (filepath .Join (root , container ))
8887 if err == nil {
89- fmt .Fprintf (os .Stderr , "Container %q exists\n " , container )
90- os .Exit (- 1 )
88+ return fmt .Errorf ("container %q exists" , container )
89+ }
90+ if err = checkConsole (context , & spec .Process , createOnly ); err != nil {
91+ return err
9192 }
92- checkConsole (context , & spec .Process , createOnly )
9393
9494 var sharedContainer string
9595 if containerType , ok := spec .Annotations ["ocid/container_type" ]; ok {
@@ -100,24 +100,20 @@ func runContainer(context *cli.Context, createOnly bool) {
100100 for _ , ns := range spec .Linux .Namespaces {
101101 if ns .Path != "" {
102102 if strings .Contains (ns .Path , "/" ) {
103- fmt .Fprintf (os .Stderr , "Runv doesn't support path to namespace file, it supports containers name as shared namespaces only\n " )
104- os .Exit (- 1 )
103+ return fmt .Errorf ("Runv doesn't support path to namespace file, it supports containers name as shared namespaces only" )
105104 }
106105 if ns .Type == "mount" {
107106 // TODO support it!
108- fmt .Fprintf (os .Stderr , "Runv doesn't support shared mount namespace currently\n " )
109- os .Exit (- 1 )
107+ return fmt .Errorf ("Runv doesn't support shared mount namespace currently" )
110108 }
111109 sharedContainer = ns .Path
112110 _ , err = os .Stat (filepath .Join (root , sharedContainer , stateJson ))
113111 if err != nil {
114- fmt .Fprintf (os .Stderr , "The container %q is not existing or not ready\n " , sharedContainer )
115- os .Exit (- 1 )
112+ return fmt .Errorf ("The container %q is not existing or not ready" , sharedContainer )
116113 }
117114 _ , err = os .Stat (filepath .Join (root , sharedContainer , "namespace" ))
118115 if err != nil {
119- fmt .Fprintf (os .Stderr , "The container %q is not ready\n " , sharedContainer )
120- os .Exit (- 1 )
116+ return fmt .Errorf ("The container %q is not ready" , sharedContainer )
121117 }
122118 }
123119 }
@@ -129,26 +125,22 @@ func runContainer(context *cli.Context, createOnly bool) {
129125 namespace = filepath .Join (root , sharedContainer , "namespace" )
130126 namespace , err = os .Readlink (namespace )
131127 if err != nil {
132- fmt .Fprintf (os .Stderr , "Cannot get namespace link of the shared container: %v\n " , err )
133- os .Exit (- 1 )
128+ return fmt .Errorf ("cannot get namespace link of the shared container: %v" , err )
134129 }
135130 } else {
136131 path , err := osext .Executable ()
137132 if err != nil {
138- fmt .Fprintf (os .Stderr , "cannot find self executable path for %s: %v\n " , os .Args [0 ], err )
139- os .Exit (- 1 )
133+ return fmt .Errorf ("cannot find self executable path for %s: %v" , os .Args [0 ], err )
140134 }
141135
142136 kernel , initrd , bios , cbfs , err := getKernelFiles (context , spec .Root .Path )
143137 if err != nil {
144- fmt .Fprintf (os .Stderr , "Can't find kernel/initrd/bios/cbfs files" )
145- os .Exit (- 1 )
138+ return fmt .Errorf ("can't find kernel/initrd/bios/cbfs files" )
146139 }
147140
148141 namespace , err = ioutil .TempDir ("/run" , "runv-namespace-" )
149142 if err != nil {
150- fmt .Fprintf (os .Stderr , "Failed to create runv namespace path: %v" , err )
151- os .Exit (- 1 )
143+ return fmt .Errorf ("failed to create runv namespace path: %v" , err )
152144 }
153145
154146 args := []string {
@@ -173,8 +165,7 @@ func runContainer(context *cli.Context, createOnly bool) {
173165 if context .GlobalIsSet (goption ) {
174166 abs_path , err := filepath .Abs (context .GlobalString (goption ))
175167 if err != nil {
176- fmt .Fprintf (os .Stderr , "Cannot get abs path for %s: %v\n " , goption , err )
177- os .Exit (- 1 )
168+ return fmt .Errorf ("Cannot get abs path for %s: %v\n " , goption , err )
178169 }
179170 args = append (args , "--" + goption , abs_path )
180171 }
@@ -195,8 +186,7 @@ func runContainer(context *cli.Context, createOnly bool) {
195186 }
196187 err = cmd .Start ()
197188 if err != nil {
198- fmt .Fprintf (os .Stderr , "failed to launch runv containerd: %v\n " , err )
199- os .Exit (- 1 )
189+ return fmt .Errorf ("failed to launch runv containerd: %v" , err )
200190 }
201191 if _ , err = os .Stat (filepath .Join (namespace , "namespaced.sock" )); os .IsNotExist (err ) {
202192 time .Sleep (3 * time .Second )
@@ -205,34 +195,31 @@ func runContainer(context *cli.Context, createOnly bool) {
205195
206196 err = createContainer (context , container , namespace , spec )
207197 if err != nil {
208- fmt .Fprintf (os .Stderr , "error %v\n " , err )
209198 cmd .Process .Signal (syscall .SIGINT )
210- os . Exit ( - 1 )
199+ return fmt . Errorf ( "failed to create container: %v" , err )
211200 }
212201 if ! createOnly {
213202 address := filepath .Join (namespace , "namespaced.sock" )
214203 startContainer (context , bundle , container , address , spec , context .Bool ("detach" ))
215204 }
216- os . Exit ( 0 )
205+ return nil
217206}
218207
219- func checkConsole (context * cli.Context , p * specs.Process , createOnly bool ) {
208+ func checkConsole (context * cli.Context , p * specs.Process , createOnly bool ) error {
220209 if context .String ("console" ) != "" && context .String ("console-socket" ) != "" {
221- fmt .Fprintf (os .Stderr , "only one of --console & --console-socket can be specified" )
222- os .Exit (- 1 )
210+ return fmt .Errorf ("only one of --console & --console-socket can be specified" )
223211 }
224212 detach := createOnly
225213 if ! createOnly {
226214 detach = context .Bool ("detach" )
227215 }
228216 if (context .String ("console" ) != "" || context .String ("console-socket" ) != "" ) && ! detach {
229- fmt .Fprintf (os .Stderr , "--console[-socket] should be used on detached mode\n " )
230- os .Exit (- 1 )
217+ return fmt .Errorf ("--console[-socket] should be used on detached mode\n " )
231218 }
232219 if (context .String ("console" ) != "" || context .String ("console-socket" ) != "" ) && ! p .Terminal {
233- fmt .Fprintf (os .Stderr , "--console[-socket] should be used on tty mode\n " )
234- os .Exit (- 1 )
220+ return fmt .Errorf ("--console[-socket] should be used on tty mode\n " )
235221 }
222+ return nil
236223}
237224
238225// Shared namespaces multiple containers suppurt
@@ -250,7 +237,10 @@ func checkConsole(context *cli.Context, p *specs.Process, createOnly bool) {
250237
251238func createContainer (context * cli.Context , container , namespace string , config * specs.Spec ) error {
252239 address := filepath .Join (namespace , "namespaced.sock" )
253- c := getClient (address )
240+ c , err := getClient (address )
241+ if err != nil {
242+ return err
243+ }
254244
255245 return ociCreate (context , container , func (stdin , stdout , stderr string ) error {
256246 r := & types.CreateContainerRequest {
@@ -279,8 +269,7 @@ func createContainer(context *cli.Context, container, namespace string, config *
279269func ociCreate (context * cli.Context , container string , createFunc func (stdin , stdout , stderr string ) error ) error {
280270 path , err := osext .Executable ()
281271 if err != nil {
282- fmt .Fprintf (os .Stderr , "cannot find self executable path for %s: %v\n " , os .Args [0 ], err )
283- os .Exit (- 1 )
272+ return fmt .Errorf ("cannot find self executable path for %s: %v\n " , os .Args [0 ], err )
284273 }
285274
286275 var stdin , stdout , stderr string
0 commit comments