mirror of
https://github.com/containers/podman
synced 2024-10-19 08:44:11 +00:00
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 <gscrivan@redhat.com>
This commit is contained in:
parent
ea0b36bcbb
commit
232b46a374
|
@ -505,17 +505,27 @@ func newRuntimeFromConfig(userConfigPath string, options ...RuntimeOption) (runt
|
||||||
return nil, errors.Wrapf(err, "error configuring runtime")
|
return nil, errors.Wrapf(err, "error configuring runtime")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !foundConfig && rootlessConfigPath != "" {
|
if rootlessConfigPath != "" {
|
||||||
os.MkdirAll(filepath.Dir(rootlessConfigPath), 0755)
|
// storage.conf
|
||||||
file, err := os.OpenFile(rootlessConfigPath, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0666)
|
storageConfFile := util.StorageConfigFile()
|
||||||
if err != nil && !os.IsExist(err) {
|
if _, err := os.Stat(storageConfFile); os.IsNotExist(err) {
|
||||||
return nil, errors.Wrapf(err, "cannot open file %s", rootlessConfigPath)
|
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()
|
if !foundConfig {
|
||||||
enc := toml.NewEncoder(file)
|
os.MkdirAll(filepath.Dir(rootlessConfigPath), 0755)
|
||||||
if err := enc.Encode(runtime.config); err != nil {
|
file, err := os.OpenFile(rootlessConfigPath, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0666)
|
||||||
os.Remove(rootlessConfigPath)
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -311,36 +311,37 @@ func GetDefaultStoreOptions() (storage.StoreOptions, error) {
|
||||||
storageOpts = storage.StoreOptions{}
|
storageOpts = storage.StoreOptions{}
|
||||||
storage.ReloadConfigurationFile(storageConf, &storageOpts)
|
storage.ReloadConfigurationFile(storageConf, &storageOpts)
|
||||||
}
|
}
|
||||||
|
if rootless.IsRootless() && err == nil {
|
||||||
if rootless.IsRootless() {
|
// If the file did not specify a graphroot or runroot,
|
||||||
if os.IsNotExist(err) {
|
// set sane defaults so we don't try and use root-owned
|
||||||
os.MkdirAll(filepath.Dir(storageConf), 0755)
|
// directories
|
||||||
file, err := os.OpenFile(storageConf, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0666)
|
if storageOpts.RunRoot == "" {
|
||||||
if err != nil {
|
storageOpts.RunRoot = defaultRootlessRunRoot
|
||||||
return storageOpts, errors.Wrapf(err, "cannot open %s", storageConf)
|
}
|
||||||
}
|
if storageOpts.GraphRoot == "" {
|
||||||
|
storageOpts.GraphRoot = defaultRootlessGraphRoot
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return storageOpts, nil
|
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
|
// StorageConfigFile returns the path to the storage config file used
|
||||||
func StorageConfigFile() string {
|
func StorageConfigFile() string {
|
||||||
if rootless.IsRootless() {
|
if rootless.IsRootless() {
|
||||||
|
|
Loading…
Reference in a new issue