From 41bd607c120dbd8b315eb30feb0fe63e079c821d Mon Sep 17 00:00:00 2001 From: TomSweeneyRedHat Date: Wed, 27 Jun 2018 13:34:10 -0400 Subject: Allow multiple containers and all for umount Signed-off-by: TomSweeneyRedHat Closes: #1012 Approved by: rhatdan --- cmd/podman/umount.go | 78 +++++++++++++++++++++++++++++++++++++++++++------ completions/bash/podman | 2 ++ docs/podman-umount.1.md | 15 ++++++++-- test/e2e/mount_test.go | 48 ++++++++++++++++++++++++++++++ 4 files changed, 131 insertions(+), 12 deletions(-) diff --git a/cmd/podman/umount.go b/cmd/podman/umount.go index 803cf034a..0fd7ff144 100644 --- a/cmd/podman/umount.go +++ b/cmd/podman/umount.go @@ -1,17 +1,29 @@ package main import ( + "fmt" + "github.com/pkg/errors" "github.com/projectatomic/libpod/cmd/podman/libpodruntime" + "github.com/projectatomic/libpod/libpod" + "github.com/sirupsen/logrus" "github.com/urfave/cli" ) var ( + umountFlags = []cli.Flag{ + cli.BoolFlag{ + Name: "all, a", + Usage: "umount all of the currently mounted containers", + }, + } + umountCommand = cli.Command{ Name: "umount", Aliases: []string{"unmount"}, - Usage: "Unmount a working container's root filesystem", - Description: "Unmounts a working container's root filesystem", + Usage: "Unmounts working container's root filesystem", + Description: "Unmounts working container's root filesystem", + Flags: umountFlags, Action: umountCmd, ArgsUsage: "CONTAINER-NAME-OR-ID", } @@ -24,18 +36,66 @@ func umountCmd(c *cli.Context) error { } defer runtime.Shutdown(false) + umountAll := c.Bool("all") args := c.Args() - if len(args) == 0 { + if len(args) == 0 && !umountAll { return errors.Errorf("container ID must be specified") } - if len(args) > 1 { - return errors.Errorf("too many arguments specified") + if len(args) > 0 && umountAll { + return errors.Errorf("when using the --all switch, you may not pass any container IDs") } - ctr, err := runtime.LookupContainer(args[0]) - if err != nil { - return errors.Wrapf(err, "error looking up container %q", args[0]) + umountContainerErrStr := "error unmounting container" + var lastError error + if len(args) > 0 { + for _, name := range args { + ctr, err := runtime.LookupContainer(name) + if err != nil { + if lastError != nil { + logrus.Error(lastError) + } + lastError = errors.Wrapf(err, "%s %s", umountContainerErrStr, name) + continue + } + + if err = unmountContainer(ctr); err != nil { + if lastError != nil { + logrus.Error(lastError) + } + lastError = errors.Wrapf(err, "%s %s", umountContainerErrStr, name) + continue + } + fmt.Printf("%s\n", ctr.ID()) + } + } else { + containers, err := runtime.GetContainers() + if err != nil { + return errors.Wrapf(err, "error reading Containers") + } + for _, ctr := range containers { + ctrState, err := ctr.State() + if ctrState == libpod.ContainerStateRunning || err != nil { + continue + } + + if err = unmountContainer(ctr); err != nil { + if lastError != nil { + logrus.Error(lastError) + } + lastError = errors.Wrapf(err, "%s %s", umountContainerErrStr, ctr.ID()) + continue + } + fmt.Printf("%s\n", ctr.ID()) + } } + return lastError +} - return ctr.Unmount() +func unmountContainer(ctr *libpod.Container) error { + if mounted, err := ctr.Mounted(); mounted { + return ctr.Unmount() + } else { + return err + } + return errors.Errorf("container is not mounted") } diff --git a/completions/bash/podman b/completions/bash/podman index 7abdd346e..051853e9e 100644 --- a/completions/bash/podman +++ b/completions/bash/podman @@ -1316,6 +1316,8 @@ _podman_unmount() { _podman_umount() { local boolean_options=" + --all + -a --help -h " diff --git a/docs/podman-umount.1.md b/docs/podman-umount.1.md index 05c7bb774..2fe3da443 100644 --- a/docs/podman-umount.1.md +++ b/docs/podman-umount.1.md @@ -1,17 +1,26 @@ % podman-umount "1" ## NAME -podman\-umount - Unmount a working container's root file system +podman\-umount - Unmount the specified working containers' root file system. ## SYNOPSIS -**podman** **umount** **containerID** +**podman** **umount** **containerID [...]** ## DESCRIPTION -Unmounts the specified container's root file system. +Unmounts the specified containers' root file system. + +## OPTIONS +**--all, -a** + +All of the currently mounted containers will be unmounted. ## EXAMPLE podman umount containerID +podman umount containerID1 containerID2 containerID3 + +podman umount --all + ## SEE ALSO podman(1), podman-mount(1) diff --git a/test/e2e/mount_test.go b/test/e2e/mount_test.go index cc1cc8edf..26eb5a7d2 100644 --- a/test/e2e/mount_test.go +++ b/test/e2e/mount_test.go @@ -61,4 +61,52 @@ var _ = Describe("Podman mount", func() { umount.WaitWithDefaultTimeout() Expect(umount.ExitCode()).To(Equal(0)) }) + + It("podman umount many", func() { + setup1 := podmanTest.Podman([]string{"create", ALPINE, "ls"}) + setup1.WaitWithDefaultTimeout() + Expect(setup1.ExitCode()).To(Equal(0)) + cid1 := setup1.OutputToString() + + setup2 := podmanTest.Podman([]string{"create", ALPINE, "ls"}) + setup2.WaitWithDefaultTimeout() + Expect(setup2.ExitCode()).To(Equal(0)) + cid2 := setup2.OutputToString() + + mount1 := podmanTest.Podman([]string{"mount", cid1}) + mount1.WaitWithDefaultTimeout() + Expect(mount1.ExitCode()).To(Equal(0)) + + mount2 := podmanTest.Podman([]string{"mount", cid2}) + mount2.WaitWithDefaultTimeout() + Expect(mount2.ExitCode()).To(Equal(0)) + + umount := podmanTest.Podman([]string{"umount", cid1, cid2}) + umount.WaitWithDefaultTimeout() + Expect(umount.ExitCode()).To(Equal(0)) + }) + + It("podman umount all", func() { + setup1 := podmanTest.Podman([]string{"create", ALPINE, "ls"}) + setup1.WaitWithDefaultTimeout() + Expect(setup1.ExitCode()).To(Equal(0)) + cid1 := setup1.OutputToString() + + setup2 := podmanTest.Podman([]string{"create", ALPINE, "ls"}) + setup2.WaitWithDefaultTimeout() + Expect(setup2.ExitCode()).To(Equal(0)) + cid2 := setup2.OutputToString() + + mount1 := podmanTest.Podman([]string{"mount", cid1}) + mount1.WaitWithDefaultTimeout() + Expect(mount1.ExitCode()).To(Equal(0)) + + mount2 := podmanTest.Podman([]string{"mount", cid2}) + mount2.WaitWithDefaultTimeout() + Expect(mount2.ExitCode()).To(Equal(0)) + + umount := podmanTest.Podman([]string{"umount", "--all"}) + umount.WaitWithDefaultTimeout() + Expect(umount.ExitCode()).To(Equal(0)) + }) }) -- cgit v1.2.3-54-g00ecf