-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmd: move handling of ID mapping to a new file
Signed-off-by: Felix Abecassis <fabecassis@nvidia.com>
- Loading branch information
Showing
3 changed files
with
95 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* umoci: Umoci Modifies Open Containers' Images | ||
* Copyright (C) 2018 SUSE LLC. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/apex/log" | ||
"github.com/openSUSE/umoci/pkg/idtools" | ||
"github.com/pkg/errors" | ||
"github.com/urfave/cli" | ||
) | ||
|
||
func remappingCommand(cmd cli.Command) cli.Command { | ||
cmd.Flags = append(cmd.Flags, []cli.Flag{ | ||
cli.StringSliceFlag{ | ||
Name: "uid-map", | ||
Usage: "specifies a uid mapping to use (container:host:size)", | ||
}, | ||
cli.StringSliceFlag{ | ||
Name: "gid-map", | ||
Usage: "specifies a gid mapping to use (container:host:size)", | ||
}, | ||
cli.BoolFlag{ | ||
Name: "rootless", | ||
Usage: "enable rootless command support", | ||
}, | ||
}...) | ||
|
||
return cmd | ||
} | ||
|
||
// parseIdmapOptions sets up the mapping options for UmociMeta, using | ||
// the arguments specified on the command line | ||
func parseIdmapOptions(meta *UmociMeta, ctx *cli.Context) error { | ||
// We need to set mappings if we're in rootless mode. | ||
meta.MapOptions.Rootless = ctx.Bool("rootless") | ||
if meta.MapOptions.Rootless { | ||
if !ctx.IsSet("uid-map") { | ||
ctx.Set("uid-map", fmt.Sprintf("0:%d:1", os.Geteuid())) | ||
} | ||
if !ctx.IsSet("gid-map") { | ||
ctx.Set("gid-map", fmt.Sprintf("0:%d:1", os.Getegid())) | ||
} | ||
} | ||
|
||
for _, uidmap := range ctx.StringSlice("uid-map") { | ||
idMap, err := idtools.ParseMapping(uidmap) | ||
if err != nil { | ||
return errors.Wrapf(err, "failure parsing --uid-map %s: %s", uidmap) | ||
} | ||
meta.MapOptions.UIDMappings = append(meta.MapOptions.UIDMappings, idMap) | ||
} | ||
for _, gidmap := range ctx.StringSlice("gid-map") { | ||
idMap, err := idtools.ParseMapping(gidmap) | ||
if err != nil { | ||
return errors.Wrapf(err, "failure parsing --gid-map %s: %s", gidmap) | ||
} | ||
meta.MapOptions.GIDMappings = append(meta.MapOptions.GIDMappings, idMap) | ||
} | ||
|
||
log.WithFields(log.Fields{ | ||
"map.uid": meta.MapOptions.UIDMappings, | ||
"map.gid": meta.MapOptions.GIDMappings, | ||
}).Debugf("parsed mappings") | ||
|
||
return nil | ||
} | ||
|