11package main
22
33import (
4+ "fmt"
45 "os"
56 "runtime"
7+ "strconv"
8+ "strings"
69
710 "github.com/opencontainers/ocitools/generate"
811 rspec "github.com/opencontainers/runtime-spec/specs-go"
@@ -117,9 +120,11 @@ func setupSpec(g *generate.Generator, context *cli.Context) error {
117120 if context .IsSet ("label" ) {
118121 annotations := context .StringSlice ("label" )
119122 for _ , s := range annotations {
120- if err := g .AddAnnotation (s ); err != nil {
121- return err
123+ pair := strings .Split (s , "=" )
124+ if len (pair ) != 2 {
125+ return fmt .Errorf ("incorrectly specified annotation: %s" , s )
122126 }
127+ g .AddAnnotation (pair [0 ], pair [1 ])
123128 }
124129 }
125130
@@ -169,7 +174,11 @@ func setupSpec(g *generate.Generator, context *cli.Context) error {
169174 if context .IsSet ("groups" ) {
170175 groups := context .StringSlice ("groups" )
171176 for _ , group := range groups {
172- g .AddProcessAdditionalGid (group )
177+ groupID , err := strconv .Atoi (group )
178+ if err != nil {
179+ return err
180+ }
181+ g .AddProcessAdditionalGid (uint32 (groupID ))
173182 }
174183 }
175184
@@ -184,7 +193,11 @@ func setupSpec(g *generate.Generator, context *cli.Context) error {
184193 if context .IsSet ("sysctl" ) {
185194 sysctls := context .StringSlice ("sysctl" )
186195 for _ , s := range sysctls {
187- g .AddLinuxSysctl (s )
196+ pair := strings .Split (s , "=" )
197+ if len (pair ) != 2 {
198+ return fmt .Errorf ("incorrectly specified sysctl: %s" , s )
199+ }
200+ g .AddLinuxSysctl (pair [0 ], pair [1 ])
188201 }
189202 }
190203
@@ -239,9 +252,11 @@ func setupSpec(g *generate.Generator, context *cli.Context) error {
239252 if context .IsSet ("tmpfs" ) {
240253 tmpfsSlice := context .StringSlice ("tmpfs" )
241254 for _ , s := range tmpfsSlice {
242- if err := g .AddTmpfsMount (s ); err != nil {
255+ dest , options , err := parseTmpfsMount (s )
256+ if err != nil {
243257 return err
244258 }
259+ g .AddTmpfsMount (dest , options )
245260 }
246261 }
247262
@@ -253,36 +268,35 @@ func setupSpec(g *generate.Generator, context *cli.Context) error {
253268 if context .IsSet ("bind" ) {
254269 binds := context .StringSlice ("bind" )
255270 for _ , bind := range binds {
256- if err := g .AddBindMount (bind ); err != nil {
271+ source , dest , options , err := parseBindMount (bind )
272+ if err != nil {
257273 return err
258274 }
275+ g .AddBindMount (source , dest , options )
259276 }
260277 }
261278
262279 if context .IsSet ("prestart" ) {
263280 preStartHooks := context .StringSlice ("prestart" )
264281 for _ , hook := range preStartHooks {
265- if err := g .AddPreStartHook (hook ); err != nil {
266- return err
267- }
282+ path , args := parseHook (hook )
283+ g .AddPreStartHook (path , args )
268284 }
269285 }
270286
271287 if context .IsSet ("poststop" ) {
272288 postStopHooks := context .StringSlice ("poststop" )
273289 for _ , hook := range postStopHooks {
274- if err := g .AddPostStopHook (hook ); err != nil {
275- return err
276- }
290+ path , args := parseHook (hook )
291+ g .AddPostStopHook (path , args )
277292 }
278293 }
279294
280295 if context .IsSet ("poststart" ) {
281296 postStartHooks := context .StringSlice ("poststart" )
282297 for _ , hook := range postStartHooks {
283- if err := g .AddPostStartHook (hook ); err != nil {
284- return err
285- }
298+ path , args := parseHook (hook )
299+ g .AddPostStartHook (path , args )
286300 }
287301 }
288302
@@ -294,15 +308,21 @@ func setupSpec(g *generate.Generator, context *cli.Context) error {
294308 }
295309
296310 for _ , uidMap := range uidMaps {
297- if err := g .AddLinuxUIDMapping (uidMap ); err != nil {
311+ hid , cid , size , err := parseIDMapping (uidMap )
312+ if err != nil {
298313 return err
299314 }
315+
316+ g .AddLinuxUIDMapping (hid , cid , size )
300317 }
301318
302319 for _ , gidMap := range gidMaps {
303- if err := g .AddLinuxGIDMapping (gidMap ); err != nil {
320+ hid , cid , size , err := parseIDMapping (gidMap )
321+ if err != nil {
304322 return err
305323 }
324+
325+ g .AddLinuxGIDMapping (hid , cid , size )
306326 }
307327
308328 var sd string
@@ -386,3 +406,73 @@ func setupLinuxNamespaces(g *generate.Generator, needsNewUser bool, nsMaps map[s
386406 g .AddOrReplaceLinuxNamespace (nsName , nsPath )
387407 }
388408}
409+
410+ func parseIDMapping (idms string ) (uint32 , uint32 , uint32 , error ) {
411+ idm := strings .Split (idms , ":" )
412+ if len (idm ) != 3 {
413+ return 0 , 0 , 0 , fmt .Errorf ("idmappings error: %s" , idms )
414+ }
415+
416+ hid , err := strconv .Atoi (idm [0 ])
417+ if err != nil {
418+ return 0 , 0 , 0 , err
419+ }
420+
421+ cid , err := strconv .Atoi (idm [1 ])
422+ if err != nil {
423+ return 0 , 0 , 0 , err
424+ }
425+
426+ size , err := strconv .Atoi (idm [2 ])
427+ if err != nil {
428+ return 0 , 0 , 0 , err
429+ }
430+
431+ return uint32 (hid ), uint32 (cid ), uint32 (size ), nil
432+ }
433+
434+ func parseHook (s string ) (string , []string ) {
435+ parts := strings .Split (s , ":" )
436+ args := []string {}
437+ path := parts [0 ]
438+ if len (parts ) > 1 {
439+ args = parts [1 :]
440+ }
441+ return path , args
442+ }
443+
444+ func parseTmpfsMount (s string ) (string , []string , error ) {
445+ var dest string
446+ var options []string
447+ var err error
448+
449+ parts := strings .Split (s , ":" )
450+ if len (parts ) == 2 {
451+ dest = parts [0 ]
452+ options = strings .Split (parts [1 ], "," )
453+ } else if len (parts ) == 1 {
454+ dest = parts [0 ]
455+ options = []string {"rw" , "noexec" , "nosuid" , "nodev" , "size=65536k" }
456+ } else {
457+ err = fmt .Errorf ("invalid value for --tmpfs" )
458+ }
459+
460+ return dest , options , err
461+ }
462+
463+ func parseBindMount (s string ) (string , string , string , error ) {
464+ var source , dest string
465+ options := "ro"
466+
467+ bparts := strings .SplitN (s , ":" , 3 )
468+ switch len (bparts ) {
469+ case 2 :
470+ source , dest = bparts [0 ], bparts [1 ]
471+ case 3 :
472+ source , dest , options = bparts [0 ], bparts [1 ], bparts [2 ]
473+ default :
474+ return source , dest , options , fmt .Errorf ("--bind should have format src:dest:[options]" )
475+ }
476+
477+ return source , dest , options , nil
478+ }
0 commit comments