From 7efefde3bcb998f37bd2fcde4f004c5bbee13dd5 Mon Sep 17 00:00:00 2001 From: Giuseppe Scrivano Date: Fri, 15 Mar 2019 17:05:03 +0100 Subject: rootless: write the custom config file before reload so that when we do a rootlessReload we inherit the correct settings from the command line. Signed-off-by: Giuseppe Scrivano --- libpod/runtime.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/libpod/runtime.go b/libpod/runtime.go index 9836b7aab..f7ca6b135 100644 --- a/libpod/runtime.go +++ b/libpod/runtime.go @@ -505,10 +505,6 @@ func newRuntimeFromConfig(userConfigPath string, options ...RuntimeOption) (runt return nil, errors.Wrapf(err, "error configuring runtime") } } - if err := makeRuntime(runtime); err != nil { - return nil, err - } - if !foundConfig && rootlessConfigPath != "" { os.MkdirAll(filepath.Dir(rootlessConfigPath), 0755) file, err := os.OpenFile(rootlessConfigPath, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0666) @@ -523,6 +519,9 @@ func newRuntimeFromConfig(userConfigPath string, options ...RuntimeOption) (runt } } } + if err := makeRuntime(runtime); err != nil { + return nil, err + } return runtime, nil } -- cgit v1.2.3-54-g00ecf From ea0b36bcbb047634944f8fe9693db03bdf7c6a90 Mon Sep 17 00:00:00 2001 From: Giuseppe Scrivano Date: Sat, 16 Mar 2019 15:17:30 +0100 Subject: utils: avoid too long tmp directory or we will easily pass the 108 chars limits for unix paths. Signed-off-by: Giuseppe Scrivano --- pkg/util/utils.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/util/utils.go b/pkg/util/utils.go index 73dddf2ac..8562afab1 100644 --- a/pkg/util/utils.go +++ b/pkg/util/utils.go @@ -195,7 +195,7 @@ func GetRootlessRuntimeDir() (string, error) { } } if runtimeDir == "" { - tmpDir := filepath.Join(os.TempDir(), fmt.Sprintf("libpod-rundir-%s", uid)) + tmpDir := filepath.Join(os.TempDir(), fmt.Sprintf("run-%s", uid)) os.MkdirAll(tmpDir, 0700) st, err := os.Stat(tmpDir) if err == nil && int(st.Sys().(*syscall.Stat_t).Uid) == os.Geteuid() && st.Mode().Perm() == 0700 { -- cgit v1.2.3-54-g00ecf From 232b46a3741267d8dd77a81196699d551c7a682e Mon Sep 17 00:00:00 2001 From: Giuseppe Scrivano Date: Sun, 17 Mar 2019 10:44:04 +0100 Subject: utils: split generation and writing of storage.conf split the generation for the default storage.conf and when we write it if not existing for a rootless user. This is necessary because during the startup we might be overriding the default configuration through --storage-driver and --storage-opt, that would not be written down to the storage.conf file we generated. Closes: https://github.com/containers/libpod/issues/2659 Signed-off-by: Giuseppe Scrivano --- libpod/runtime.go | 30 ++++++++++++++++++++---------- pkg/util/utils.go | 51 ++++++++++++++++++++++++++------------------------- 2 files changed, 46 insertions(+), 35 deletions(-) diff --git a/libpod/runtime.go b/libpod/runtime.go index f7ca6b135..b3b75d791 100644 --- a/libpod/runtime.go +++ b/libpod/runtime.go @@ -505,17 +505,27 @@ func newRuntimeFromConfig(userConfigPath string, options ...RuntimeOption) (runt return nil, errors.Wrapf(err, "error configuring runtime") } } - if !foundConfig && rootlessConfigPath != "" { - 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) { - return nil, errors.Wrapf(err, "cannot open file %s", rootlessConfigPath) + if rootlessConfigPath != "" { + // storage.conf + storageConfFile := util.StorageConfigFile() + if _, err := os.Stat(storageConfFile); os.IsNotExist(err) { + if err := util.WriteStorageConfigFile(&runtime.config.StorageConfig, storageConfFile); err != nil { + return nil, errors.Wrapf(err, "cannot write config file %s", storageConfFile) + } } - if err == nil { - defer file.Close() - enc := toml.NewEncoder(file) - if err := enc.Encode(runtime.config); err != nil { - os.Remove(rootlessConfigPath) + + if !foundConfig { + 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) { + return nil, errors.Wrapf(err, "cannot open file %s", rootlessConfigPath) + } + if err == nil { + defer file.Close() + enc := toml.NewEncoder(file) + if err := enc.Encode(runtime.config); err != nil { + os.Remove(rootlessConfigPath) + } } } } diff --git a/pkg/util/utils.go b/pkg/util/utils.go index 8562afab1..a408ad34b 100644 --- a/pkg/util/utils.go +++ b/pkg/util/utils.go @@ -311,36 +311,37 @@ func GetDefaultStoreOptions() (storage.StoreOptions, error) { storageOpts = storage.StoreOptions{} storage.ReloadConfigurationFile(storageConf, &storageOpts) } - - if rootless.IsRootless() { - if os.IsNotExist(err) { - os.MkdirAll(filepath.Dir(storageConf), 0755) - file, err := os.OpenFile(storageConf, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0666) - if err != nil { - return storageOpts, errors.Wrapf(err, "cannot open %s", storageConf) - } - - tomlConfiguration := getTomlStorage(&storageOpts) - defer file.Close() - enc := toml.NewEncoder(file) - if err := enc.Encode(tomlConfiguration); err != nil { - os.Remove(storageConf) - } - } else if err == nil { - // If the file did not specify a graphroot or runroot, - // set sane defaults so we don't try and use root-owned - // directories - if storageOpts.RunRoot == "" { - storageOpts.RunRoot = defaultRootlessRunRoot - } - if storageOpts.GraphRoot == "" { - storageOpts.GraphRoot = defaultRootlessGraphRoot - } + if rootless.IsRootless() && err == nil { + // If the file did not specify a graphroot or runroot, + // set sane defaults so we don't try and use root-owned + // directories + if storageOpts.RunRoot == "" { + storageOpts.RunRoot = defaultRootlessRunRoot + } + if storageOpts.GraphRoot == "" { + storageOpts.GraphRoot = defaultRootlessGraphRoot } } return storageOpts, nil } +// WriteStorageConfigFile writes the configuration to a file +func WriteStorageConfigFile(storageOpts *storage.StoreOptions, storageConf string) error { + os.MkdirAll(filepath.Dir(storageConf), 0755) + file, err := os.OpenFile(storageConf, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0666) + if err != nil { + return errors.Wrapf(err, "cannot open %s", storageConf) + } + tomlConfiguration := getTomlStorage(storageOpts) + defer file.Close() + enc := toml.NewEncoder(file) + if err := enc.Encode(tomlConfiguration); err != nil { + os.Remove(storageConf) + return err + } + return nil +} + // StorageConfigFile returns the path to the storage config file used func StorageConfigFile() string { if rootless.IsRootless() { -- cgit v1.2.3-54-g00ecf