From 4e31c8136dffd2272e49715b83fba092621ffd2a Mon Sep 17 00:00:00 2001 From: Daniel J Walsh Date: Thu, 9 Jul 2020 14:53:46 -0400 Subject: Cleanup handling of podman mount/unmount We should default to the user name unmount rather then the internal name of umount. Also User namespace was not being handled correctly. We want to inform the user that if they do a mount when in rootless mode that they have to be first in the podman unshare state. Signed-off-by: Daniel J Walsh --- cmd/podman/containers/mount.go | 17 +++++++++++++---- cmd/podman/containers/unmount.go | 41 ++++++++++++++++++++-------------------- cmd/podman/main.go | 18 +++++++++++++----- cmd/podman/registry/config.go | 3 ++- 4 files changed, 49 insertions(+), 30 deletions(-) (limited to 'cmd/podman') diff --git a/cmd/podman/containers/mount.go b/cmd/podman/containers/mount.go index 37ee92177..e2603a55c 100644 --- a/cmd/podman/containers/mount.go +++ b/cmd/podman/containers/mount.go @@ -10,6 +10,7 @@ import ( "github.com/containers/libpod/v2/cmd/podman/utils" "github.com/containers/libpod/v2/cmd/podman/validate" "github.com/containers/libpod/v2/pkg/domain/entities" + "github.com/pkg/errors" "github.com/spf13/cobra" "github.com/spf13/pflag" ) @@ -31,7 +32,8 @@ var ( return validate.CheckAllLatestAndCIDFile(cmd, args, true, false) }, Annotations: map[string]string{ - registry.ParentNSRequired: "", + registry.UnshareNSRequired: "", + registry.ParentNSRequired: "", }, } @@ -51,7 +53,7 @@ var ( func mountFlags(flags *pflag.FlagSet) { flags.BoolVarP(&mountOpts.All, "all", "a", false, "Mount all containers") - flags.StringVar(&mountOpts.Format, "format", "", "Change the output format to Go template") + flags.StringVar(&mountOpts.Format, "format", "", "Print the mounted containers in specified format (json)") flags.BoolVar(&mountOpts.NoTruncate, "notruncate", false, "Do not truncate output") } @@ -90,14 +92,21 @@ func mount(_ *cobra.Command, args []string) error { } return errs.PrintErrors() } - if mountOpts.Format == "json" { + + switch mountOpts.Format { + case "json": return printJSON(reports) + case "": + // do nothing + default: + return errors.Errorf("unknown --format argument: %s", mountOpts.Format) } + mrs := make([]mountReporter, 0, len(reports)) for _, r := range reports { mrs = append(mrs, mountReporter{r}) } - row := "{{.ID}} {{.Path}}" + row := "{{.ID}} {{.Path}}\n" format := "{{range . }}" + row + "{{end}}" tmpl, err := template.New("mounts").Parse(format) if err != nil { diff --git a/cmd/podman/containers/unmount.go b/cmd/podman/containers/unmount.go index c40c2be7e..6556a6510 100644 --- a/cmd/podman/containers/unmount.go +++ b/cmd/podman/containers/unmount.go @@ -18,31 +18,32 @@ var ( An unmount can be forced with the --force flag. ` - umountCommand = &cobra.Command{ - Use: "umount [flags] CONTAINER [CONTAINER...]", - Aliases: []string{"unmount"}, + unmountCommand = &cobra.Command{ + Use: "unmount [flags] CONTAINER [CONTAINER...]", + Aliases: []string{"umount"}, Short: "Unmounts working container's root filesystem", Long: description, RunE: unmount, Args: func(cmd *cobra.Command, args []string) error { return validate.CheckAllLatestAndCIDFile(cmd, args, false, false) }, - Example: `podman umount ctrID - podman umount ctrID1 ctrID2 ctrID3 - podman umount --all`, + Example: `podman unmount ctrID + podman unmount ctrID1 ctrID2 ctrID3 + podman unmount --all`, } containerUnmountCommand = &cobra.Command{ - Use: umountCommand.Use, - Short: umountCommand.Short, - Long: umountCommand.Long, - RunE: umountCommand.RunE, + Use: unmountCommand.Use, + Short: unmountCommand.Short, + Aliases: unmountCommand.Aliases, + Long: unmountCommand.Long, + RunE: unmountCommand.RunE, Args: func(cmd *cobra.Command, args []string) error { return validate.CheckAllLatestAndCIDFile(cmd, args, false, false) }, - Example: `podman container umount ctrID - podman container umount ctrID1 ctrID2 ctrID3 - podman container umount --all`, + Example: `podman container unmount ctrID + podman container unmount ctrID1 ctrID2 ctrID3 + podman container unmount --all`, } ) @@ -50,25 +51,25 @@ var ( unmountOpts entities.ContainerUnmountOptions ) -func umountFlags(flags *pflag.FlagSet) { - flags.BoolVarP(&unmountOpts.All, "all", "a", false, "Umount all of the currently mounted containers") - flags.BoolVarP(&unmountOpts.Force, "force", "f", false, "Force the complete umount all of the currently mounted containers") +func unmountFlags(flags *pflag.FlagSet) { + flags.BoolVarP(&unmountOpts.All, "all", "a", false, "Unmount all of the currently mounted containers") + flags.BoolVarP(&unmountOpts.Force, "force", "f", false, "Force the complete unmount of the specified mounted containers") } func init() { registry.Commands = append(registry.Commands, registry.CliCommand{ Mode: []entities.EngineMode{entities.ABIMode}, - Command: umountCommand, + Command: unmountCommand, }) - umountFlags(umountCommand.Flags()) - validate.AddLatestFlag(umountCommand, &unmountOpts.Latest) + unmountFlags(unmountCommand.Flags()) + validate.AddLatestFlag(unmountCommand, &unmountOpts.Latest) registry.Commands = append(registry.Commands, registry.CliCommand{ Mode: []entities.EngineMode{entities.ABIMode}, Command: containerUnmountCommand, Parent: containerCmd, }) - umountFlags(containerUnmountCommand.Flags()) + unmountFlags(containerUnmountCommand.Flags()) validate.AddLatestFlag(containerUnmountCommand, &unmountOpts.Latest) } diff --git a/cmd/podman/main.go b/cmd/podman/main.go index 5f740a006..4bdb020af 100644 --- a/cmd/podman/main.go +++ b/cmd/podman/main.go @@ -39,13 +39,21 @@ func main() { for _, m := range c.Mode { if cfg.EngineMode == m { // Command cannot be run rootless - _, found := c.Command.Annotations[registry.ParentNSRequired] - if rootless.IsRootless() && found { - c.Command.RunE = func(cmd *cobra.Command, args []string) error { - return fmt.Errorf("cannot run command %q in rootless mode", cmd.CommandPath()) + _, found := c.Command.Annotations[registry.UnshareNSRequired] + if found { + if rootless.IsRootless() && found && os.Getuid() != 0 { + c.Command.RunE = func(cmd *cobra.Command, args []string) error { + return fmt.Errorf("cannot run command %q in rootless mode, must execute `podman unshare` first", cmd.CommandPath()) + } + } + } else { + _, found = c.Command.Annotations[registry.ParentNSRequired] + if rootless.IsRootless() && found { + c.Command.RunE = func(cmd *cobra.Command, args []string) error { + return fmt.Errorf("cannot run command %q in rootless mode", cmd.CommandPath()) + } } } - parent := rootCmd if c.Parent != nil { parent = c.Parent diff --git a/cmd/podman/registry/config.go b/cmd/podman/registry/config.go index 24e728bad..ea33e3a4e 100644 --- a/cmd/podman/registry/config.go +++ b/cmd/podman/registry/config.go @@ -15,7 +15,8 @@ import ( ) const ( - ParentNSRequired = "ParentNSRequired" + ParentNSRequired = "ParentNSRequired" + UnshareNSRequired = "UnshareNSRequired" ) var ( -- cgit v1.2.3-54-g00ecf