@@ -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 {
@@ -176,8 +168,7 @@ func runContainer(context *cli.Context, createOnly bool) {
176168 if context .GlobalIsSet (goption ) {
177169 abs_path , err := filepath .Abs (context .GlobalString (goption ))
178170 if err != nil {
179- fmt .Fprintf (os .Stderr , "Cannot get abs path for %s: %v\n " , goption , err )
180- os .Exit (- 1 )
171+ return fmt .Errorf ("Cannot get abs path for %s: %v\n " , goption , err )
181172 }
182173 args = append (args , "--" + goption , abs_path )
183174 }
@@ -198,8 +189,7 @@ func runContainer(context *cli.Context, createOnly bool) {
198189 }
199190 err = cmd .Start ()
200191 if err != nil {
201- fmt .Fprintf (os .Stderr , "failed to launch runv containerd: %v\n " , err )
202- os .Exit (- 1 )
192+ return fmt .Errorf ("failed to launch runv containerd: %v" , err )
203193 }
204194 if _ , err = os .Stat (filepath .Join (namespace , "namespaced.sock" )); os .IsNotExist (err ) {
205195 time .Sleep (3 * time .Second )
@@ -208,34 +198,31 @@ func runContainer(context *cli.Context, createOnly bool) {
208198
209199 err = createContainer (context , container , namespace , spec )
210200 if err != nil {
211- fmt .Fprintf (os .Stderr , "error %v\n " , err )
212201 cmd .Process .Signal (syscall .SIGINT )
213- os . Exit ( - 1 )
202+ return fmt . Errorf ( "failed to create container: %v" , err )
214203 }
215204 if ! createOnly {
216205 address := filepath .Join (namespace , "namespaced.sock" )
217206 startContainer (context , bundle , container , address , spec , context .Bool ("detach" ))
218207 }
219- os . Exit ( 0 )
208+ return nil
220209}
221210
222- func checkConsole (context * cli.Context , p * specs.Process , createOnly bool ) {
211+ func checkConsole (context * cli.Context , p * specs.Process , createOnly bool ) error {
223212 if context .String ("console" ) != "" && context .String ("console-socket" ) != "" {
224- fmt .Fprintf (os .Stderr , "only one of --console & --console-socket can be specified" )
225- os .Exit (- 1 )
213+ return fmt .Errorf ("only one of --console & --console-socket can be specified" )
226214 }
227215 detach := createOnly
228216 if ! createOnly {
229217 detach = context .Bool ("detach" )
230218 }
231219 if (context .String ("console" ) != "" || context .String ("console-socket" ) != "" ) && ! detach {
232- fmt .Fprintf (os .Stderr , "--console[-socket] should be used on detached mode\n " )
233- os .Exit (- 1 )
220+ return fmt .Errorf ("--console[-socket] should be used on detached mode\n " )
234221 }
235222 if (context .String ("console" ) != "" || context .String ("console-socket" ) != "" ) && ! p .Terminal {
236- fmt .Fprintf (os .Stderr , "--console[-socket] should be used on tty mode\n " )
237- os .Exit (- 1 )
223+ return fmt .Errorf ("--console[-socket] should be used on tty mode\n " )
238224 }
225+ return nil
239226}
240227
241228// Shared namespaces multiple containers suppurt
@@ -253,7 +240,10 @@ func checkConsole(context *cli.Context, p *specs.Process, createOnly bool) {
253240
254241func createContainer (context * cli.Context , container , namespace string , config * specs.Spec ) error {
255242 address := filepath .Join (namespace , "namespaced.sock" )
256- c := getClient (address )
243+ c , err := getClient (address )
244+ if err != nil {
245+ return err
246+ }
257247
258248 return ociCreate (context , container , func (stdin , stdout , stderr string ) error {
259249 r := & types.CreateContainerRequest {
@@ -282,8 +272,7 @@ func createContainer(context *cli.Context, container, namespace string, config *
282272func ociCreate (context * cli.Context , container string , createFunc func (stdin , stdout , stderr string ) error ) error {
283273 path , err := osext .Executable ()
284274 if err != nil {
285- fmt .Fprintf (os .Stderr , "cannot find self executable path for %s: %v\n " , os .Args [0 ], err )
286- os .Exit (- 1 )
275+ return fmt .Errorf ("cannot find self executable path for %s: %v\n " , os .Args [0 ], err )
287276 }
288277
289278 var stdin , stdout , stderr string
0 commit comments