Skip to content

Commit f595d37

Browse files
committed
generate: Add --user and consolidate user-namespace handling
Put this in setupNamespaces with the other namespaces. This commit allows users to: * Join an existing user namespace with --user=path/to/ns. * Create a new user namespace without mapping IDs (although this is likely not very useful). * Clear a templated user namespace with --user=host (although without being able to clear the ID mappings, this may not be very useful). I haven't checked for likely-invalid configuration like: --uidmappings=1000:0:1 --user=path/to/ns We can add that in a follow-up commit if we want. Signed-off-by: W. Trevor King <[email protected]>
1 parent 5a20d1d commit f595d37

2 files changed

Lines changed: 15 additions & 8 deletions

File tree

generate.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ var generateFlags = []cli.Flag{
3131
cli.StringFlag{Name: "mount", Usage: "mount namespace"},
3232
cli.StringFlag{Name: "pid", Usage: "pid namespace"},
3333
cli.StringFlag{Name: "ipc", Usage: "ipc namespace"},
34+
cli.StringFlag{Name: "user", Usage: "user namespace"},
3435
cli.StringFlag{Name: "uts", Usage: "uts namespace"},
3536
cli.StringFlag{Name: "selinux-label", Usage: "process selinux label"},
3637
cli.StringFlag{Name: "mount-label", Usage: "selinux mount context label"},
@@ -463,10 +464,6 @@ func addIDMappings(spec *rspec.Spec, context *cli.Context) error {
463464
}
464465
}
465466

466-
if len(context.StringSlice("uidmappings")) > 0 || len(context.StringSlice("gidmappings")) > 0 {
467-
spec.Linux.Namespaces = append(spec.Linux.Namespaces, rspec.Namespace{Type: "user"})
468-
}
469-
470467
return nil
471468
}
472469

@@ -660,9 +657,14 @@ func mapStrToNamespace(ns string, path string) rspec.Namespace {
660657
}
661658

662659
func setupNamespaces(spec *rspec.Spec, context *cli.Context) {
663-
namespaces := []string{"network", "pid", "mount", "ipc", "uts"}
660+
var needsNewUser = false
661+
if len(context.StringSlice("uidmappings")) > 0 || len(context.StringSlice("gidmappings")) > 0 {
662+
needsNewUser = true
663+
}
664+
665+
namespaces := []string{"network", "pid", "mount", "ipc", "uts", "user"}
664666
for _, nsName := range namespaces {
665-
if !context.IsSet(nsName) {
667+
if !context.IsSet(nsName) && !(needsNewUser && nsName == "user") {
666668
continue
667669
}
668670
nsPath := context.String(nsName)

man/ocitools-generate.1.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ inside of the container.
5757
Gid for the process inside of container
5858

5959
**--gidmappings**=GIDMAPPINGS
60-
Add GIDMappings e.g HostID:ContainerID:Size for use with User Namespace
60+
Add GIDMappings e.g HostID:ContainerID:Size. Implies **-user=**.
6161

6262
**--groups**=GROUP
6363
Supplementary groups for the processes inside of container
@@ -191,7 +191,12 @@ inside of the container.
191191
Sets the UID used within the container.
192192

193193
**--uidmappings**
194-
Add UIDMappings e.g HostUID:ContainerID:Size for use with User Namespace
194+
Add UIDMappings e.g HostUID:ContainerID:Size. Implies **--user=**.
195+
196+
**--user**=[*PATH*]
197+
Use a user namespace. If *PATH* is set, join that namespace. If it
198+
is unset, create a new namespace. The special *PATH* `host` removes
199+
any existing user namespace from the configuration.
195200

196201
**--uts**=[*PATH*]
197202
Use a UTS namespace. If *PATH* is set, join that namespace. If it

0 commit comments

Comments
 (0)