diff options
author | Daniel J Walsh <dwalsh@redhat.com> | 2019-06-14 09:21:51 -0400 |
---|---|---|
committer | Daniel J Walsh <dwalsh@redhat.com> | 2019-06-15 09:51:47 -0400 |
commit | 53c3720de940ace048ca888caa3ab50d82b2f178 (patch) | |
tree | a75873acf6b9066d2bd97042977000e20a05e355 | |
parent | 670fc030886938b4ee93aa2ddd5db6c947bd975a (diff) | |
download | podman-53c3720de940ace048ca888caa3ab50d82b2f178.tar.gz podman-53c3720de940ace048ca888caa3ab50d82b2f178.tar.bz2 podman-53c3720de940ace048ca888caa3ab50d82b2f178.zip |
Correctly identify the defaults for cgroup-manager
Currently we report cgroupmanager default as systemd, even if the user modified
the libpod.conf. Also cgroupmanager does not work in rootless mode. This
PR correctly identifies the default cgroup manager or reports it is not supported.
Also add homeDir to correctly get the homedir if the $HOME is not set. Will
attempt to get Homedir out of /etc/passwd.
Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
-rw-r--r-- | cmd/podman/main_local.go | 14 | ||||
-rw-r--r-- | docs/podman.1.md | 5 | ||||
-rw-r--r-- | libpod/oci_linux.go | 6 | ||||
-rw-r--r-- | libpod/runtime.go | 91 |
4 files changed, 95 insertions, 21 deletions
diff --git a/cmd/podman/main_local.go b/cmd/podman/main_local.go index 132f35ab5..7a062cb4b 100644 --- a/cmd/podman/main_local.go +++ b/cmd/podman/main_local.go @@ -12,6 +12,7 @@ import ( "github.com/containers/libpod/cmd/podman/cliconfig" "github.com/containers/libpod/cmd/podman/libpodruntime" + "github.com/containers/libpod/libpod" "github.com/containers/libpod/pkg/rootless" "github.com/containers/libpod/pkg/tracing" "github.com/containers/libpod/pkg/util" @@ -25,8 +26,17 @@ import ( const remote = false func init() { - - rootCmd.PersistentFlags().StringVar(&MainGlobalOpts.CGroupManager, "cgroup-manager", "", "Cgroup manager to use (cgroupfs or systemd, default systemd)") + cgroupManager := libpod.SystemdCgroupsManager + if runtimeConfig, err := libpod.DefaultRuntimeConfig(); err == nil { + cgroupManager = runtimeConfig.CgroupManager + } + cgroupHelp := "Cgroup manager to use (cgroupfs or systemd)" + cgroupv2, _ := util.IsCgroup2UnifiedMode() + if rootless.IsRootless() && !cgroupv2 { + cgroupManager = "" + cgroupHelp = "Cgroup manager is not supported in rootless mode" + } + rootCmd.PersistentFlags().StringVar(&MainGlobalOpts.CGroupManager, "cgroup-manager", cgroupManager, cgroupHelp) // -c is deprecated due to conflict with -c on subcommands rootCmd.PersistentFlags().StringVar(&MainGlobalOpts.CpuProfile, "cpu-profile", "", "Path for the cpu profiling results") rootCmd.PersistentFlags().StringVar(&MainGlobalOpts.Config, "config", "", "Path of a libpod config file detailing container server configuration options") diff --git a/docs/podman.1.md b/docs/podman.1.md index 84e695d23..3b682b10d 100644 --- a/docs/podman.1.md +++ b/docs/podman.1.md @@ -27,7 +27,10 @@ Print usage statement **--cgroup-manager**=*manager* -CGroup manager to use for container cgroups. Supported values are cgroupfs or systemd (default). Setting this flag can cause certain commands to break when called on containers created by the other CGroup manager type. +CGroup manager to use for container cgroups. Supported values are cgroupfs or systemd. Default is systemd unless overridden in the libpod.conf file. + +Note: Setting this flag can cause certain commands to break when called on containers previously created by the other CGroup manager type. +Note: CGroup manager is not supported in rootless mode when using CGroups Version V1. **--cpu-profile**=*path* diff --git a/libpod/oci_linux.go b/libpod/oci_linux.go index 9bbefdb06..be7210bd2 100644 --- a/libpod/oci_linux.go +++ b/libpod/oci_linux.go @@ -294,7 +294,11 @@ func (r *OCIRuntime) createOCIContainer(ctr *Container, cgroupParent string, res cmd.Env = append(cmd.Env, fmt.Sprintf("XDG_RUNTIME_DIR=%s", runtimeDir)) cmd.Env = append(cmd.Env, fmt.Sprintf("_CONTAINERS_USERNS_CONFIGURED=%s", os.Getenv("_CONTAINERS_USERNS_CONFIGURED"))) cmd.Env = append(cmd.Env, fmt.Sprintf("_CONTAINERS_ROOTLESS_UID=%s", os.Getenv("_CONTAINERS_ROOTLESS_UID"))) - cmd.Env = append(cmd.Env, fmt.Sprintf("HOME=%s", os.Getenv("HOME"))) + home, err := homeDir() + if err != nil { + return err + } + cmd.Env = append(cmd.Env, fmt.Sprintf("HOME=%s", home)) if r.reservePorts && !ctr.config.NetMode.IsSlirp4netns() { ports, err := bindPorts(ctr.config.PortMappings) diff --git a/libpod/runtime.go b/libpod/runtime.go index 2c50fce85..82e7338aa 100644 --- a/libpod/runtime.go +++ b/libpod/runtime.go @@ -5,6 +5,7 @@ import ( "fmt" "io/ioutil" "os" + "os/user" "path/filepath" "sync" "syscall" @@ -379,6 +380,68 @@ func NewRuntimeFromConfig(ctx context.Context, userConfigPath string, options .. return newRuntimeFromConfig(ctx, userConfigPath, options...) } +func homeDir() (string, error) { + home := os.Getenv("HOME") + if home == "" { + usr, err := user.Current() + if err != nil { + return "", errors.Wrapf(err, "unable to resolve HOME directory") + } + home = usr.HomeDir + } + return home, nil +} + +func getRootlessConfigPath() (string, error) { + home, err := homeDir() + if err != nil { + return "", err + } + + return filepath.Join(home, ".config/containers/libpod.conf"), nil +} + +func getConfigPath() string { + if rootless.IsRootless() { + rootlessConfigPath, err := getRootlessConfigPath() + if err != nil { + if _, err := os.Stat(rootlessConfigPath); err == nil { + return rootlessConfigPath + } + } + } + if _, err := os.Stat(OverrideConfigPath); err == nil { + // Use the override configuration path + return OverrideConfigPath + } + if _, err := os.Stat(ConfigPath); err == nil { + return ConfigPath + } + return "" +} + +// DefaultRuntimeConfig reads default config path and returns the RuntimeConfig +func DefaultRuntimeConfig() (*RuntimeConfig, error) { + configPath := getConfigPath() + + contents, err := ioutil.ReadFile(configPath) + if err != nil { + return nil, errors.Wrapf(err, "error reading configuration file %s", configPath) + } + + // This is ugly, but we need to decode twice. + // Once to check if libpod static and tmp dirs were explicitly + // set (not enough to check if they're not the default value, + // might have been explicitly configured to the default). + // A second time to actually get a usable config. + tmpConfig := new(RuntimeConfig) + if _, err := toml.Decode(string(contents), tmpConfig); err != nil { + return nil, errors.Wrapf(err, "error decoding configuration file %s", + configPath) + } + return tmpConfig, nil +} + func newRuntimeFromConfig(ctx context.Context, userConfigPath string, options ...RuntimeOption) (runtime *Runtime, err error) { runtime = new(Runtime) runtime.config = new(RuntimeConfig) @@ -407,11 +470,13 @@ func newRuntimeFromConfig(ctx context.Context, userConfigPath string, options .. runtime.config.StaticDir = filepath.Join(storageConf.GraphRoot, "libpod") runtime.config.VolumePath = filepath.Join(storageConf.GraphRoot, "volumes") - configPath := ConfigPath - foundConfig := true + configPath := getConfigPath() rootlessConfigPath := "" if rootless.IsRootless() { - home := os.Getenv("HOME") + home, err := homeDir() + if err != nil { + return nil, err + } if runtime.config.SignaturePolicyPath == "" { newPath := filepath.Join(home, ".config/containers/policy.json") if _, err := os.Stat(newPath); err == nil { @@ -419,7 +484,10 @@ func newRuntimeFromConfig(ctx context.Context, userConfigPath string, options .. } } - rootlessConfigPath = filepath.Join(home, ".config/containers/libpod.conf") + rootlessConfigPath, err = getRootlessConfigPath() + if err != nil { + return nil, err + } runtimeDir, err := util.GetRootlessRuntimeDir() if err != nil { @@ -441,21 +509,10 @@ func newRuntimeFromConfig(ctx context.Context, userConfigPath string, options .. // when it doesn't exist return nil, errors.Wrapf(err, "cannot stat %s", configPath) } - } else if rootless.IsRootless() { - configPath = rootlessConfigPath - if _, err := os.Stat(configPath); err != nil { - foundConfig = false - } - } else if _, err := os.Stat(OverrideConfigPath); err == nil { - // Use the override configuration path - configPath = OverrideConfigPath - } else if _, err := os.Stat(ConfigPath); err != nil { - // Both stat checks failed, no config found - foundConfig = false } // If we have a valid configuration file, load it in - if foundConfig { + if configPath != "" { contents, err := ioutil.ReadFile(configPath) if err != nil { return nil, errors.Wrapf(err, "error reading configuration file %s", configPath) @@ -564,7 +621,7 @@ func newRuntimeFromConfig(ctx context.Context, userConfigPath string, options .. } } - if !foundConfig { + if configPath != "" { os.MkdirAll(filepath.Dir(rootlessConfigPath), 0755) file, err := os.OpenFile(rootlessConfigPath, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0666) if err != nil && !os.IsExist(err) { |