Skip to content

Commit 2d92f65

Browse files
authored
Merge pull request #288 from cyphar/namespace-flags
cmd: generate: add --linux-namespace-* family of flags
2 parents 06e17ee + eb189d5 commit 2d92f65

File tree

3 files changed

+133
-132
lines changed

3 files changed

+133
-132
lines changed

cmd/oci-runtime-tool/generate.go

Lines changed: 47 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ var generateFlags = []cli.Flag{
2424
cli.StringSliceFlag{Name: "bind", Usage: "bind mount directories src:dest[:options...]"},
2525
cli.StringSliceFlag{Name: "cap-add", Usage: "add Linux capabilities"},
2626
cli.StringSliceFlag{Name: "cap-drop", Usage: "drop Linux capabilities"},
27-
cli.StringFlag{Name: "cgroup", Usage: "cgroup namespace"},
2827
cli.StringFlag{Name: "cgroups-path", Usage: "specify the path to the cgroups"},
2928
cli.StringFlag{Name: "cwd", Value: "/", Usage: "current working directory for the process"},
3029
cli.BoolFlag{Name: "disable-oom-kill", Usage: "disable OOM Killer"},
@@ -34,7 +33,6 @@ var generateFlags = []cli.Flag{
3433
cli.StringSliceFlag{Name: "gidmappings", Usage: "add GIDMappings e.g HostID:ContainerID:Size"},
3534
cli.StringSliceFlag{Name: "groups", Usage: "supplementary groups for the process"},
3635
cli.StringFlag{Name: "hostname", Usage: "hostname value for the container"},
37-
cli.StringFlag{Name: "ipc", Usage: "ipc namespace"},
3836
cli.StringSliceFlag{Name: "label", Usage: "add annotations to the configuration e.g. key=value"},
3937
cli.Uint64Flag{Name: "linux-cpu-shares", Usage: "the relative share of CPU time available to the tasks in a cgroup"},
4038
cli.Uint64Flag{Name: "linux-cpu-period", Usage: "the CPU period to be used for hardcapping (in usecs)"},
@@ -47,21 +45,21 @@ var generateFlags = []cli.Flag{
4745
cli.Uint64Flag{Name: "linux-mem-swap", Usage: "total memory limit (memory + swap) (in bytes)"},
4846
cli.Uint64Flag{Name: "linux-mem-swappiness", Usage: "how aggressive the kernel will swap memory pages (Range from 0 to 100)"},
4947
cli.StringFlag{Name: "linux-mems", Usage: "list of memory nodes in the cpuset (default is to use any available memory node)"},
48+
cli.StringSliceFlag{Name: "linux-namespace-add", Usage: "adds a namespace to the set of namespaces to create or join of the form 'ns[:path]'"},
49+
cli.StringSliceFlag{Name: "linux-namespace-remove", Usage: "removes a namespace from the set of namespaces to create or join of the form 'ns'"},
50+
cli.BoolFlag{Name: "linux-namespace-remove-all", Usage: "removes all namespaces from the set of namespaces created or joined"},
5051
cli.IntFlag{Name: "linux-network-classid", Usage: "specifies class identifier tagged by container's network packets"},
5152
cli.StringSliceFlag{Name: "linux-network-priorities", Usage: "specifies priorities of network traffic"},
5253
cli.Int64Flag{Name: "linux-pids-limit", Usage: "maximum number of PIDs"},
5354
cli.Uint64Flag{Name: "linux-realtime-period", Usage: "CPU period to be used for realtime scheduling (in usecs)"},
5455
cli.Uint64Flag{Name: "linux-realtime-runtime", Usage: "the time realtime scheduling may use (in usecs)"},
5556
cli.StringSliceFlag{Name: "masked-paths", Usage: "specifies paths can not be read inside container"},
56-
cli.StringFlag{Name: "mount", Usage: "mount namespace"},
5757
cli.StringFlag{Name: "mount-cgroups", Value: "no", Usage: "mount cgroups (rw,ro,no)"},
5858
cli.StringFlag{Name: "mount-label", Usage: "selinux mount context label"},
59-
cli.StringFlag{Name: "network", Usage: "network namespace"},
6059
cli.BoolFlag{Name: "no-new-privileges", Usage: "set no new privileges bit for the container process"},
6160
cli.IntFlag{Name: "oom-score-adj", Usage: "oom_score_adj for the container"},
6261
cli.StringFlag{Name: "os", Value: runtime.GOOS, Usage: "operating system the container is created for"},
6362
cli.StringFlag{Name: "output", Usage: "output file (defaults to stdout)"},
64-
cli.StringFlag{Name: "pid", Usage: "pid namespace"},
6563
cli.StringSliceFlag{Name: "poststart", Usage: "set command to run in poststart hooks"},
6664
cli.StringSliceFlag{Name: "poststop", Usage: "set command to run in poststop hooks"},
6765
cli.StringSliceFlag{Name: "prestart", Usage: "set command to run in prestart hooks"},
@@ -91,8 +89,6 @@ var generateFlags = []cli.Flag{
9189
cli.BoolFlag{Name: "tty", Usage: "allocate a new tty for the container process"},
9290
cli.IntFlag{Name: "uid", Usage: "uid for the process"},
9391
cli.StringSliceFlag{Name: "uidmappings", Usage: "add UIDMappings e.g HostID:ContainerID:Size"},
94-
cli.StringFlag{Name: "user", Usage: "user namespace"},
95-
cli.StringFlag{Name: "uts", Usage: "uts namespace"},
9692
}
9793

9894
var generateCommand = cli.Command{
@@ -280,8 +276,6 @@ func setupSpec(g *generate.Generator, context *cli.Context) error {
280276
}
281277
}
282278

283-
needsNewUser := false
284-
285279
var uidMaps, gidMaps []string
286280

287281
if context.IsSet("uidmappings") {
@@ -292,12 +286,11 @@ func setupSpec(g *generate.Generator, context *cli.Context) error {
292286
gidMaps = context.StringSlice("gidmappings")
293287
}
294288

289+
// Add default user namespace.
295290
if len(uidMaps) > 0 || len(gidMaps) > 0 {
296-
needsNewUser = true
291+
g.AddOrReplaceLinuxNamespace("user", "")
297292
}
298293

299-
setupLinuxNamespaces(context, g, needsNewUser)
300-
301294
if context.IsSet("tmpfs") {
302295
tmpfsSlice := context.StringSlice("tmpfs")
303296
for _, s := range tmpfsSlice {
@@ -457,6 +450,32 @@ func setupSpec(g *generate.Generator, context *cli.Context) error {
457450
}
458451
}
459452

453+
if context.IsSet("linux-namespace-add") {
454+
namespaces := context.StringSlice("linux-namespace-add")
455+
for _, ns := range namespaces {
456+
name, path, err := parseNamespace(ns)
457+
if err != nil {
458+
return err
459+
}
460+
if err := g.AddOrReplaceLinuxNamespace(name, path); err != nil {
461+
return err
462+
}
463+
}
464+
}
465+
466+
if context.IsSet("linux-namespace-remove") {
467+
namespaces := context.StringSlice("linux-namespace-remove")
468+
for _, name := range namespaces {
469+
if err := g.RemoveLinuxNamespace(name); err != nil {
470+
return err
471+
}
472+
}
473+
}
474+
475+
if context.Bool("linux-namespace-remove-all") {
476+
g.ClearLinuxNamespaces()
477+
}
478+
460479
if context.IsSet("rlimits-add") {
461480
rlimits := context.StringSlice("rlimits-add")
462481
for _, rlimit := range rlimits {
@@ -486,20 +505,6 @@ func setupSpec(g *generate.Generator, context *cli.Context) error {
486505
return err
487506
}
488507

489-
func setupLinuxNamespaces(context *cli.Context, g *generate.Generator, needsNewUser bool) {
490-
for _, nsName := range generate.Namespaces {
491-
if !context.IsSet(nsName) && !(needsNewUser && nsName == "user") {
492-
continue
493-
}
494-
nsPath := context.String(nsName)
495-
if nsPath == "host" {
496-
g.RemoveLinuxNamespace(nsName)
497-
continue
498-
}
499-
g.AddOrReplaceLinuxNamespace(nsName, nsPath)
500-
}
501-
}
502-
503508
func parseIDMapping(idms string) (uint32, uint32, uint32, error) {
504509
idm := strings.Split(idms, ":")
505510
if len(idm) != 3 {
@@ -604,6 +609,22 @@ func parseRlimit(rlimit string) (string, uint64, uint64, error) {
604609
return parts[0], uint64(hard), uint64(soft), nil
605610
}
606611

612+
func parseNamespace(ns string) (string, string, error) {
613+
parts := strings.SplitN(ns, ":", 2)
614+
if len(parts) == 0 || parts[0] == "" {
615+
return "", "", fmt.Errorf("invalid namespace value: %s", ns)
616+
}
617+
618+
nsType := parts[0]
619+
nsPath := ""
620+
621+
if len(parts) == 2 {
622+
nsPath = parts[1]
623+
}
624+
625+
return nsType, nsPath, nil
626+
}
627+
607628
func addSeccomp(context *cli.Context, g *generate.Generator) error {
608629

609630
// Set the DefaultAction of seccomp

completions/bash/oci-runtime-tool

Lines changed: 71 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -297,72 +297,80 @@ _oci-runtime-tool_help() {
297297

298298
_oci-runtime-tool_generate() {
299299
local options_with_args="
300-
--arch
301-
--apparmor
302-
--args
303-
--bind
304-
--cap-add
305-
--cap-drop
306-
--cgroup
307-
--cgroup-path
308-
--cwd
309-
--disable-oom-kill
310-
--env
311-
--env-file
312-
--gid
313-
--gidmappings
314-
--groups
315-
--hostname
316-
--help
317-
--ipc
318-
--label
319-
--linux-network-classid
320-
--linux-network-priorities
321-
--linux-pids-limit
322-
--masked-paths
323-
--mount
324-
--mount-cgroups
325-
--mount-label
326-
--network
327-
--os
328-
--output
329-
--pid
330-
--poststart
331-
--poststop
332-
--prestart
333-
--readonly-paths
334-
--rootfs-path
335-
--rootfs-propagation
336-
--rlimits-add
337-
--rlimits-remove
338-
--rlimits-remove-all
339-
--seccomp-allow
340-
--seccomp-arch
341-
--seccomp-default
342-
--seccomp-default-force
343-
--seccomp-errno
344-
--seccomp-kill
345-
--seccomp-only
346-
--seccomp-remove
347-
--seccomp-remove-all
348-
--seccomp-trace
349-
--seccomp-trap
350-
--seccomp-syscalls
351-
--selinux-label
352-
--sysctl
353-
--tmplate
354-
--tmpfs
355-
--uid
356-
--uidmappings
357-
--user
358-
--uts
300+
--apparmor
301+
--arch
302+
--args
303+
--bind
304+
--cap-add
305+
--cap-drop
306+
--cgroups-path
307+
--cwd
308+
--env
309+
--env-file
310+
--gid
311+
--gidmappings
312+
--groups
313+
--hostname
314+
--label
315+
--linux-cpu-shares
316+
--linux-cpu-period
317+
--linux-cpu-quota
318+
--linux-cpus
319+
--linux-mem-kernel-limit
320+
--linux-mem-kernel-tcp
321+
--linux-mem-limit
322+
--linux-mem-reservation
323+
--linux-mem-swap
324+
--linux-mem-swappiness
325+
--linux-mems
326+
--linux-namespace-add
327+
--linux-namespace-remove
328+
--linux-network-classid
329+
--linux-network-priorities
330+
--linux-pids-limit
331+
--linux-realtime-period
332+
--linux-realtime-runtime
333+
--masked-paths
334+
--mount-cgroups
335+
--mount-label
336+
--oom-score-adj
337+
--os
338+
--output
339+
--poststart
340+
--poststop
341+
--prestart
342+
--readonly-paths
343+
--rootfs-path
344+
--rootfs-propagation
345+
--rlimits-add
346+
--rlimits-remove
347+
--seccomp-allow
348+
--seccomp-arch
349+
--seccomp-default
350+
--seccomp-default-force
351+
--seccomp-errno
352+
--seccomp-kill
353+
--seccomp-remove
354+
--seccomp-trace
355+
--seccomp-trap
356+
--selinux-label
357+
--sysctl
358+
--template
359+
--tmpfs
360+
--uid
361+
--uidmappings
359362
"
360363

361364
local boolean_options="
362-
--no-new-privileges
363-
--privileged
364-
--rootfs-readonly
365-
--tty
365+
--disable-oom-kill
366+
--linux-namespace-remove-all
367+
--no-new-privileges
368+
--privileged
369+
--rlimits-remove-all
370+
--rootfs-readonly
371+
--seccomp-only
372+
--seccomp-remove-all
373+
--tty
366374
"
367375

368376
local all_options="$options_with_args $boolean_options"

0 commit comments

Comments
 (0)