Skip to content

Commit 4086a0f

Browse files
giusepperh-atomic-bot
authored andcommitted
podman: use a different store for the rootless case
so that the user has rw access to it. Signed-off-by: Giuseppe Scrivano <[email protected]> Closes: #871 Approved by: mheon
1 parent a1ec674 commit 4086a0f

File tree

5 files changed

+84
-7
lines changed

5 files changed

+84
-7
lines changed

cmd/podman/create.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"strings"
1010
"syscall"
1111

12-
"github.com/containers/storage"
1312
"github.com/docker/docker/api/types/container"
1413
"github.com/docker/docker/pkg/signal"
1514
"github.com/docker/go-connections/nat"
@@ -81,7 +80,10 @@ func createCmd(c *cli.Context) error {
8180
if err != nil {
8281
return err
8382
}
84-
storageOpts := storage.DefaultStoreOptions
83+
storageOpts, err := libpodruntime.GetDefaultStoreOptions()
84+
if err != nil {
85+
return err
86+
}
8587
storageOpts.UIDMap = mappings.UIDMap
8688
storageOpts.GIDMap = mappings.GIDMap
8789

cmd/podman/libpodruntime/runtime.go

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,55 @@
11
package libpodruntime
22

33
import (
4+
"fmt"
5+
"os"
6+
"path/filepath"
7+
48
"github.com/containers/storage"
59
"github.com/projectatomic/libpod/libpod"
610
"github.com/urfave/cli"
711
)
812

913
// GetRuntime generates a new libpod runtime configured by command line options
1014
func GetRuntime(c *cli.Context) (*libpod.Runtime, error) {
11-
storageOpts := storage.DefaultStoreOptions
15+
storageOpts, err := GetDefaultStoreOptions()
16+
if err != nil {
17+
return nil, err
18+
}
1219
return GetRuntimeWithStorageOpts(c, &storageOpts)
1320
}
1421

22+
func GetRootlessStorageOpts() (storage.StoreOptions, error) {
23+
var opts storage.StoreOptions
24+
25+
opts.RunRoot = filepath.Join(libpod.GetRootlessRuntimeDir(), "run")
26+
27+
dataDir := os.Getenv("XDG_DATA_DIR")
28+
if dataDir != "" {
29+
opts.GraphRoot = filepath.Join(dataDir, "containers", "storage")
30+
} else {
31+
home := os.Getenv("HOME")
32+
if home == "" {
33+
return opts, fmt.Errorf("HOME not specified")
34+
}
35+
opts.GraphRoot = filepath.Join(home, ".containers", "storage")
36+
}
37+
opts.GraphDriverName = "vfs"
38+
return opts, nil
39+
}
40+
41+
func GetDefaultStoreOptions() (storage.StoreOptions, error) {
42+
storageOpts := storage.DefaultStoreOptions
43+
if os.Getuid() != 0 {
44+
var err error
45+
storageOpts, err = GetRootlessStorageOpts()
46+
if err != nil {
47+
return storageOpts, err
48+
}
49+
}
50+
return storageOpts, nil
51+
}
52+
1553
// GetRuntime generates a new libpod runtime configured by command line options
1654
func GetRuntimeWithStorageOpts(c *cli.Context, storageOpts *storage.StoreOptions) (*libpod.Runtime, error) {
1755
options := []libpod.RuntimeOption{}

cmd/podman/run.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"strconv"
1010
"strings"
1111

12-
"github.com/containers/storage"
1312
"github.com/pkg/errors"
1413
"github.com/projectatomic/libpod/cmd/podman/libpodruntime"
1514
"github.com/projectatomic/libpod/libpod"
@@ -54,7 +53,10 @@ func runCmd(c *cli.Context) error {
5453
}
5554
}
5655

57-
storageOpts := storage.DefaultStoreOptions
56+
storageOpts, err := libpodruntime.GetDefaultStoreOptions()
57+
if err != nil {
58+
return err
59+
}
5860
mappings, err := util.ParseIDMapping(c.StringSlice("uidmap"), c.StringSlice("gidmap"), c.String("subuidmap"), c.String("subgidmap"))
5961
if err != nil {
6062
return err

libpod/container_internal.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1296,7 +1296,9 @@ func (c *Container) generateSpec(ctx context.Context) (*spec.Spec, error) {
12961296
g.AddProcessEnv("container", "libpod")
12971297
}
12981298

1299-
if c.runtime.config.CgroupManager == SystemdCgroupsManager {
1299+
if os.Getuid() != 0 {
1300+
g.SetLinuxCgroupsPath("")
1301+
} else if c.runtime.config.CgroupManager == SystemdCgroupsManager {
13001302
// When runc is set to use Systemd as a cgroup manager, it
13011303
// expects cgroups to be passed as follows:
13021304
// slice:prefix:name

libpod/runtime.go

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ package libpod
22

33
import (
44
"bytes"
5+
"fmt"
56
"io/ioutil"
67
"os"
78
"path/filepath"
89
"sync"
10+
"syscall"
911

1012
"github.com/BurntSushi/toml"
1113
is "github.com/containers/image/storage"
@@ -164,14 +166,45 @@ var (
164166
CgroupManager: CgroupfsCgroupsManager,
165167
HooksDir: hooks.DefaultDir,
166168
StaticDir: filepath.Join(storage.DefaultStoreOptions.GraphRoot, "libpod"),
167-
TmpDir: "/var/run/libpod",
169+
TmpDir: getDefaultTmpDir(),
168170
MaxLogSize: -1,
169171
NoPivotRoot: false,
170172
CNIConfigDir: "/etc/cni/net.d/",
171173
CNIPluginDir: []string{"/usr/libexec/cni", "/usr/lib/cni", "/opt/cni/bin"},
172174
}
173175
)
174176

177+
// GetRootlessRuntimeDir returns the runtime directory when running as non root
178+
func GetRootlessRuntimeDir() string {
179+
hasNoEnv := false
180+
runtimeDir := os.Getenv("XDG_RUNTIME_DIR")
181+
if runtimeDir == "" {
182+
hasNoEnv = true
183+
tmpDir := filepath.Join(os.TempDir(), "user", fmt.Sprintf("%d", os.Getuid()))
184+
os.MkdirAll(tmpDir, 0700)
185+
st, err := os.Stat(tmpDir)
186+
if err == nil && int(st.Sys().(*syscall.Stat_t).Uid) == os.Getuid() && st.Mode().Perm() == 0700 {
187+
runtimeDir = tmpDir
188+
}
189+
}
190+
if runtimeDir == "" {
191+
runtimeDir = filepath.Join(os.Getenv("HOME"), "rundir")
192+
}
193+
if hasNoEnv {
194+
os.Setenv("XDG_RUNTIME_DIR", runtimeDir)
195+
}
196+
return runtimeDir
197+
}
198+
199+
func getDefaultTmpDir() string {
200+
if os.Getuid() == 0 {
201+
return "/var/run/libpod"
202+
}
203+
204+
rootlessRuntimeDir := GetRootlessRuntimeDir()
205+
return filepath.Join(rootlessRuntimeDir, "libpod", "tmp")
206+
}
207+
175208
// NewRuntime creates a new container runtime
176209
// Options can be passed to override the default configuration for the runtime
177210
func NewRuntime(options ...RuntimeOption) (runtime *Runtime, err error) {

0 commit comments

Comments
 (0)