diff options
-rw-r--r-- | cmd/podman/common.go | 8 | ||||
-rw-r--r-- | cmd/podman/libpodruntime/runtime.go | 54 | ||||
-rw-r--r-- | cmd/podman/login.go | 9 | ||||
-rw-r--r-- | cmd/podman/logout.go | 6 | ||||
-rw-r--r-- | cmd/podman/rm.go | 35 | ||||
-rw-r--r-- | docs/podman.1.md | 8 | ||||
-rw-r--r-- | libpod/oci.go | 21 | ||||
-rw-r--r-- | libpod/runtime.go | 43 | ||||
-rw-r--r-- | pkg/util/utils.go | 84 | ||||
-rw-r--r-- | test/e2e/exec_test.go | 1 | ||||
-rw-r--r-- | test/e2e/run_test.go | 2 |
11 files changed, 151 insertions, 120 deletions
diff --git a/cmd/podman/common.go b/cmd/podman/common.go index e342659ed..8ae1c9e0f 100644 --- a/cmd/podman/common.go +++ b/cmd/podman/common.go @@ -465,3 +465,11 @@ func getAuthFile(authfile string) string { } return os.Getenv("REGISTRY_AUTH_FILE") } + +// scrubServer removes 'http://' or 'https://' from the front of the +// server/registry string if either is there. This will be mostly used +// for user input from 'podman login' and 'podman logout'. +func scrubServer(server string) string { + server = strings.TrimPrefix(server, "https://") + return strings.TrimPrefix(server, "http://") +} diff --git a/cmd/podman/libpodruntime/runtime.go b/cmd/podman/libpodruntime/runtime.go index a0d497e8e..df422eb81 100644 --- a/cmd/podman/libpodruntime/runtime.go +++ b/cmd/podman/libpodruntime/runtime.go @@ -1,21 +1,16 @@ package libpodruntime import ( - "fmt" - "os" - "path/filepath" - "github.com/containers/libpod/libpod" "github.com/containers/libpod/pkg/rootless" "github.com/containers/libpod/pkg/util" "github.com/containers/storage" - "github.com/pkg/errors" "github.com/urfave/cli" ) // GetRuntime generates a new libpod runtime configured by command line options func GetRuntime(c *cli.Context) (*libpod.Runtime, error) { - storageOpts, err := GetDefaultStoreOptions() + storageOpts, err := util.GetDefaultStoreOptions() if err != nil { return nil, err } @@ -28,7 +23,7 @@ func GetContainerRuntime(c *cli.Context) (*libpod.Runtime, error) { if err != nil { return nil, err } - storageOpts, err := GetDefaultStoreOptions() + storageOpts, err := util.GetDefaultStoreOptions() if err != nil { return nil, err } @@ -37,51 +32,6 @@ func GetContainerRuntime(c *cli.Context) (*libpod.Runtime, error) { return GetRuntimeWithStorageOpts(c, &storageOpts) } -func GetRootlessStorageOpts() (storage.StoreOptions, error) { - var opts storage.StoreOptions - - rootlessRuntime, err := libpod.GetRootlessRuntimeDir() - if err != nil { - return opts, err - } - opts.RunRoot = filepath.Join(rootlessRuntime, "run") - - dataDir := os.Getenv("XDG_DATA_HOME") - if dataDir == "" { - home := os.Getenv("HOME") - if home == "" { - return opts, fmt.Errorf("neither XDG_DATA_HOME nor HOME was set non-empty") - } - // runc doesn't like symlinks in the rootfs path, and at least - // on CoreOS /home is a symlink to /var/home, so resolve any symlink. - resolvedHome, err := filepath.EvalSymlinks(home) - if err != nil { - return opts, errors.Wrapf(err, "cannot resolve %s", home) - } - dataDir = filepath.Join(resolvedHome, ".local", "share") - } - opts.GraphRoot = filepath.Join(dataDir, "containers", "storage") - opts.GraphDriverName = "vfs" - return opts, nil -} - -func GetDefaultStoreOptions() (storage.StoreOptions, error) { - storageOpts := storage.DefaultStoreOptions - if rootless.IsRootless() { - var err error - storageOpts, err = GetRootlessStorageOpts() - if err != nil { - return storageOpts, err - } - - storageConf := filepath.Join(os.Getenv("HOME"), ".config/containers/storage.conf") - if _, err := os.Stat(storageConf); err == nil { - storage.ReloadConfigurationFile(storageConf, &storageOpts) - } - } - return storageOpts, nil -} - // GetRuntime generates a new libpod runtime configured by command line options func GetRuntimeWithStorageOpts(c *cli.Context, storageOpts *storage.StoreOptions) (*libpod.Runtime, error) { options := []libpod.RuntimeOption{} diff --git a/cmd/podman/login.go b/cmd/podman/login.go index 76f0f50ff..aa26d1466 100644 --- a/cmd/podman/login.go +++ b/cmd/podman/login.go @@ -60,10 +60,7 @@ func loginCmd(c *cli.Context) error { if len(args) == 0 { return errors.Errorf("registry must be given") } - var server string - if len(args) == 1 { - server = args[0] - } + server := scrubServer(args[0]) authfile := getAuthFile(c.String("authfile")) sc := common.GetSystemContext("", authfile, false) @@ -113,6 +110,10 @@ func getUserAndPass(username, password, userFromAuthFile string) (string, string if err != nil { return "", "", errors.Wrapf(err, "error reading username") } + // If no username provided, use userFromAuthFile instead. + if strings.TrimSpace(username) == "" { + username = userFromAuthFile + } } if password == "" { fmt.Print("Password: ") diff --git a/cmd/podman/logout.go b/cmd/podman/logout.go index 099464e4f..3cdb606b5 100644 --- a/cmd/podman/logout.go +++ b/cmd/podman/logout.go @@ -44,7 +44,7 @@ func logoutCmd(c *cli.Context) error { } var server string if len(args) == 1 { - server = args[0] + server = scrubServer(args[0]) } authfile := getAuthFile(c.String("authfile")) @@ -54,14 +54,14 @@ func logoutCmd(c *cli.Context) error { if err := config.RemoveAllAuthentication(sc); err != nil { return err } - fmt.Println("Remove login credentials for all registries") + fmt.Println("Removed login credentials for all registries") return nil } err := config.RemoveAuthentication(sc, server) switch err { case nil: - fmt.Printf("Remove login credentials for %s\n", server) + fmt.Printf("Removed login credentials for %s\n", server) return nil case config.ErrNotLoggedIn: return errors.Errorf("Not logged into %s\n", server) diff --git a/cmd/podman/rm.go b/cmd/podman/rm.go index f64eca6f4..38b1546ff 100644 --- a/cmd/podman/rm.go +++ b/cmd/podman/rm.go @@ -3,6 +3,7 @@ package main import ( "fmt" "os" + rt "runtime" "github.com/containers/libpod/cmd/podman/libpodruntime" "github.com/containers/libpod/libpod" @@ -45,6 +46,12 @@ Running containers will not be removed without the -f option. // saveCmd saves the image to either docker-archive or oci func rmCmd(c *cli.Context) error { + var ( + delContainers []*libpod.Container + lastError error + deleteFuncs []workerInput + ) + ctx := getContext() if err := validateFlags(c, rmFlags); err != nil { return err @@ -65,8 +72,6 @@ func rmCmd(c *cli.Context) error { return errors.Errorf("specify one or more containers to remove") } - var delContainers []*libpod.Container - var lastError error if c.Bool("all") { delContainers, err = runtime.GetContainers() if err != nil { @@ -89,16 +94,26 @@ func rmCmd(c *cli.Context) error { delContainers = append(delContainers, container) } } + for _, container := range delContainers { - err = runtime.RemoveContainer(ctx, container, c.Bool("force")) - if err != nil { - if lastError != nil { - fmt.Fprintln(os.Stderr, lastError) - } - lastError = errors.Wrapf(err, "failed to delete container %v", container.ID()) - } else { - fmt.Println(container.ID()) + f := func() error { + return runtime.RemoveContainer(ctx, container, c.Bool("force")) + } + + deleteFuncs = append(deleteFuncs, workerInput{ + containerID: container.ID(), + parallelFunc: f, + }) + } + + deleteErrors := parallelExecuteWorkerPool(rt.NumCPU()*3, deleteFuncs) + for cid, result := range deleteErrors { + if result != nil { + fmt.Println(result.Error()) + lastError = result + continue } + fmt.Println(cid) } return lastError } diff --git a/docs/podman.1.md b/docs/podman.1.md index 3a0943d6b..085af97ff 100644 --- a/docs/podman.1.md +++ b/docs/podman.1.md @@ -42,11 +42,13 @@ When namespace is set, created containers and pods will join the given namespace **--root**=**value** -Path to the root directory in which data, including images, is stored +Storage root dir in which data, including images, is stored (default: "/var/lib/containers/storage" for UID 0, "$HOME/.local/share/containers/storage" for other users). +Default root dir is configured in /etc/containers/storage.conf. **--runroot**=**value** -Path to the 'run directory' where all state information is stored +Storage state directory where all state information is stored (default: "/var/run/containers/storage" for UID 0, "/var/run/user/$UID/run" for other users). +Default state dir is configured in /etc/containers/storage.conf. **--runtime**=**value** @@ -73,7 +75,7 @@ Print the version ## Exit Status -The exit code from `podman gives information about why the container +The exit code from `podman` gives information about why the container failed to run or why it exited. When `podman` commands exit with a non-zero code, the exit codes follow the `chroot` standard, see below: diff --git a/libpod/oci.go b/libpod/oci.go index f6d320017..2257cd42f 100644 --- a/libpod/oci.go +++ b/libpod/oci.go @@ -17,6 +17,7 @@ import ( "github.com/containers/libpod/pkg/ctime" "github.com/containers/libpod/pkg/rootless" + "github.com/containers/libpod/pkg/util" "github.com/coreos/go-systemd/activation" "github.com/cri-o/ocicni/pkg/ocicni" spec "github.com/opencontainers/runtime-spec/specs-go" @@ -230,7 +231,7 @@ func bindPorts(ports []ocicni.PortMapping) ([]*os.File, error) { func (r *OCIRuntime) createOCIContainer(ctr *Container, cgroupParent string, restoreContainer bool) (err error) { var stderrBuf bytes.Buffer - runtimeDir, err := GetRootlessRuntimeDir() + runtimeDir, err := util.GetRootlessRuntimeDir() if err != nil { return err } @@ -377,6 +378,7 @@ func (r *OCIRuntime) createOCIContainer(ctr *Container, cgroupParent string, res childPipe.Close() return err } + defer cmd.Wait() // We don't need childPipe on the parent side childPipe.Close() @@ -446,7 +448,7 @@ func (r *OCIRuntime) createOCIContainer(ctr *Container, cgroupParent string, res func (r *OCIRuntime) updateContainerStatus(ctr *Container) error { state := new(spec.State) - runtimeDir, err := GetRootlessRuntimeDir() + runtimeDir, err := util.GetRootlessRuntimeDir() if err != nil { return err } @@ -477,6 +479,7 @@ func (r *OCIRuntime) updateContainerStatus(ctr *Container) error { } return errors.Wrapf(err, "error getting container %s state. stderr/out: %s", ctr.ID(), out) } + defer cmd.Wait() errPipe.Close() out, err := ioutil.ReadAll(outPipe) @@ -556,7 +559,7 @@ func (r *OCIRuntime) updateContainerStatus(ctr *Container) error { // Sets time the container was started, but does not save it. func (r *OCIRuntime) startContainer(ctr *Container) error { // TODO: streams should probably *not* be our STDIN/OUT/ERR - redirect to buffers? - runtimeDir, err := GetRootlessRuntimeDir() + runtimeDir, err := util.GetRootlessRuntimeDir() if err != nil { return err } @@ -573,7 +576,7 @@ func (r *OCIRuntime) startContainer(ctr *Container) error { // killContainer sends the given signal to the given container func (r *OCIRuntime) killContainer(ctr *Container, signal uint) error { logrus.Debugf("Sending signal %d to container %s", signal, ctr.ID()) - runtimeDir, err := GetRootlessRuntimeDir() + runtimeDir, err := util.GetRootlessRuntimeDir() if err != nil { return err } @@ -636,7 +639,7 @@ func (r *OCIRuntime) stopContainer(ctr *Container, timeout uint) error { args = []string{"kill", "--all", ctr.ID(), "KILL"} } - runtimeDir, err := GetRootlessRuntimeDir() + runtimeDir, err := util.GetRootlessRuntimeDir() if err != nil { return err } @@ -667,7 +670,7 @@ func (r *OCIRuntime) deleteContainer(ctr *Container) error { // pauseContainer pauses the given container func (r *OCIRuntime) pauseContainer(ctr *Container) error { - runtimeDir, err := GetRootlessRuntimeDir() + runtimeDir, err := util.GetRootlessRuntimeDir() if err != nil { return err } @@ -677,7 +680,7 @@ func (r *OCIRuntime) pauseContainer(ctr *Container) error { // unpauseContainer unpauses the given container func (r *OCIRuntime) unpauseContainer(ctr *Container) error { - runtimeDir, err := GetRootlessRuntimeDir() + runtimeDir, err := util.GetRootlessRuntimeDir() if err != nil { return err } @@ -698,7 +701,7 @@ func (r *OCIRuntime) execContainer(c *Container, cmd, capAdd, env []string, tty return nil, errors.Wrapf(ErrEmptyID, "must provide a session ID for exec") } - runtimeDir, err := GetRootlessRuntimeDir() + runtimeDir, err := util.GetRootlessRuntimeDir() if err != nil { return nil, err } @@ -780,7 +783,7 @@ func (r *OCIRuntime) execStopContainer(ctr *Container, timeout uint) error { if len(execSessions) == 0 { return nil } - runtimeDir, err := GetRootlessRuntimeDir() + runtimeDir, err := util.GetRootlessRuntimeDir() if err != nil { return err } diff --git a/libpod/runtime.go b/libpod/runtime.go index 985af2849..f012d66c2 100644 --- a/libpod/runtime.go +++ b/libpod/runtime.go @@ -1,13 +1,11 @@ package libpod import ( - "fmt" "io/ioutil" "os" "os/exec" "path/filepath" "sync" - "syscall" "github.com/BurntSushi/toml" is "github.com/containers/image/storage" @@ -17,6 +15,7 @@ import ( "github.com/containers/libpod/pkg/hooks" sysreg "github.com/containers/libpod/pkg/registries" "github.com/containers/libpod/pkg/rootless" + "github.com/containers/libpod/pkg/util" "github.com/containers/storage" "github.com/cri-o/ocicni/pkg/ocicni" "github.com/docker/docker/pkg/namesgenerator" @@ -215,46 +214,12 @@ var ( } ) -// GetRootlessRuntimeDir returns the runtime directory when running as non root -func GetRootlessRuntimeDir() (string, error) { - runtimeDir := os.Getenv("XDG_RUNTIME_DIR") - uid := fmt.Sprintf("%d", rootless.GetRootlessUID()) - if runtimeDir == "" { - tmpDir := filepath.Join("/run", "user", uid) - os.MkdirAll(tmpDir, 0700) - st, err := os.Stat(tmpDir) - if err == nil && int(st.Sys().(*syscall.Stat_t).Uid) == os.Getuid() && st.Mode().Perm() == 0700 { - runtimeDir = tmpDir - } - } - if runtimeDir == "" { - tmpDir := filepath.Join(os.TempDir(), "user", uid) - os.MkdirAll(tmpDir, 0700) - st, err := os.Stat(tmpDir) - if err == nil && int(st.Sys().(*syscall.Stat_t).Uid) == os.Getuid() && st.Mode().Perm() == 0700 { - runtimeDir = tmpDir - } - } - if runtimeDir == "" { - home := os.Getenv("HOME") - if home == "" { - return "", fmt.Errorf("neither XDG_RUNTIME_DIR nor HOME was set non-empty") - } - resolvedHome, err := filepath.EvalSymlinks(home) - if err != nil { - return "", errors.Wrapf(err, "cannot resolve %s", home) - } - runtimeDir = filepath.Join(resolvedHome, "rundir") - } - return runtimeDir, nil -} - func getDefaultTmpDir() (string, error) { if !rootless.IsRootless() { return "/var/run/libpod", nil } - rootlessRuntimeDir, err := GetRootlessRuntimeDir() + rootlessRuntimeDir, err := util.GetRootlessRuntimeDir() if err != nil { return "", err } @@ -269,7 +234,7 @@ func SetXdgRuntimeDir(val string) error { } if val == "" { var err error - val, err = GetRootlessRuntimeDir() + val, err = util.GetRootlessRuntimeDir() if err != nil { return err } @@ -309,7 +274,7 @@ func NewRuntime(options ...RuntimeOption) (runtime *Runtime, err error) { foundConfig = false } - runtimeDir, err := GetRootlessRuntimeDir() + runtimeDir, err := util.GetRootlessRuntimeDir() if err != nil { return nil, err } diff --git a/pkg/util/utils.go b/pkg/util/utils.go index 28dd015bd..9107eec5c 100644 --- a/pkg/util/utils.go +++ b/pkg/util/utils.go @@ -3,10 +3,13 @@ package util import ( "fmt" "os" + "path/filepath" "strconv" "strings" + "syscall" "github.com/containers/image/types" + "github.com/containers/libpod/pkg/rootless" "github.com/containers/storage" "github.com/containers/storage/pkg/idtools" "github.com/opencontainers/image-spec/specs-go/v1" @@ -210,3 +213,84 @@ func ParseIDMapping(UIDMapSlice, GIDMapSlice []string, subUIDMap, subGIDMap stri } return &options, nil } + +// GetRootlessRuntimeDir returns the runtime directory when running as non root +func GetRootlessRuntimeDir() (string, error) { + runtimeDir := os.Getenv("XDG_RUNTIME_DIR") + uid := fmt.Sprintf("%d", rootless.GetRootlessUID()) + if runtimeDir == "" { + tmpDir := filepath.Join("/run", "user", uid) + os.MkdirAll(tmpDir, 0700) + st, err := os.Stat(tmpDir) + if err == nil && int(st.Sys().(*syscall.Stat_t).Uid) == os.Getuid() && st.Mode().Perm() == 0700 { + runtimeDir = tmpDir + } + } + if runtimeDir == "" { + tmpDir := filepath.Join(os.TempDir(), "user", uid) + os.MkdirAll(tmpDir, 0700) + st, err := os.Stat(tmpDir) + if err == nil && int(st.Sys().(*syscall.Stat_t).Uid) == os.Getuid() && st.Mode().Perm() == 0700 { + runtimeDir = tmpDir + } + } + if runtimeDir == "" { + home := os.Getenv("HOME") + if home == "" { + return "", fmt.Errorf("neither XDG_RUNTIME_DIR nor HOME was set non-empty") + } + resolvedHome, err := filepath.EvalSymlinks(home) + if err != nil { + return "", errors.Wrapf(err, "cannot resolve %s", home) + } + runtimeDir = filepath.Join(resolvedHome, "rundir") + } + return runtimeDir, nil +} + +// GetRootlessStorageOpts returns the storage ops for containers running as non root +func GetRootlessStorageOpts() (storage.StoreOptions, error) { + var opts storage.StoreOptions + + rootlessRuntime, err := GetRootlessRuntimeDir() + if err != nil { + return opts, err + } + opts.RunRoot = filepath.Join(rootlessRuntime, "run") + + dataDir := os.Getenv("XDG_DATA_HOME") + if dataDir == "" { + home := os.Getenv("HOME") + if home == "" { + return opts, fmt.Errorf("neither XDG_DATA_HOME nor HOME was set non-empty") + } + // runc doesn't like symlinks in the rootfs path, and at least + // on CoreOS /home is a symlink to /var/home, so resolve any symlink. + resolvedHome, err := filepath.EvalSymlinks(home) + if err != nil { + return opts, errors.Wrapf(err, "cannot resolve %s", home) + } + dataDir = filepath.Join(resolvedHome, ".local", "share") + } + opts.GraphRoot = filepath.Join(dataDir, "containers", "storage") + opts.GraphDriverName = "vfs" + return opts, nil +} + +// GetDefaultStoreOptions returns the storage ops for containers +func GetDefaultStoreOptions() (storage.StoreOptions, error) { + storageOpts := storage.DefaultStoreOptions + if rootless.IsRootless() { + var err error + storageOpts, err = GetRootlessStorageOpts() + if err != nil { + return storageOpts, err + } + + storageConf := filepath.Join(os.Getenv("HOME"), ".config/containers/storage.conf") + if _, err := os.Stat(storageConf); err == nil { + storage.ReloadConfigurationFile(storageConf, &storageOpts) + } + } + return storageOpts, nil +} diff --git a/test/e2e/exec_test.go b/test/e2e/exec_test.go index cfdc819a6..250e08704 100644 --- a/test/e2e/exec_test.go +++ b/test/e2e/exec_test.go @@ -111,6 +111,7 @@ var _ = Describe("Podman exec", func() { }) It("podman exec with user only in container", func() { + podmanTest.RestoreArtifact(fedoraMinimal) testUser := "test123" setup := podmanTest.Podman([]string{"run", "--name", "test1", "-d", fedoraMinimal, "sleep", "60"}) setup.WaitWithDefaultTimeout() diff --git a/test/e2e/run_test.go b/test/e2e/run_test.go index 1cd2fdf2c..cb436ccca 100644 --- a/test/e2e/run_test.go +++ b/test/e2e/run_test.go @@ -577,6 +577,7 @@ USER mail` }) It("podman run findmnt nothing shared", func() { + podmanTest.RestoreArtifact(fedoraMinimal) vol1 := filepath.Join(podmanTest.TempDir, "vol-test1") err := os.MkdirAll(vol1, 0755) Expect(err).To(BeNil()) @@ -592,6 +593,7 @@ USER mail` }) It("podman run findmnt shared", func() { + podmanTest.RestoreArtifact(fedoraMinimal) vol1 := filepath.Join(podmanTest.TempDir, "vol-test1") err := os.MkdirAll(vol1, 0755) Expect(err).To(BeNil()) |