Skip to content

Commit

Permalink
GetStore modifications, Create GetLocalStorageConfig and SaveLocalSto…
Browse files Browse the repository at this point in the history
…rageConfig

If passing either just a runroot or both a runroot and a graphroot
GetStore() was returning when finding a match for JUST the graph root,
overriding the runroot and using a different location than specified

Also create two new helper functions which add the ability to retrieve and overwrite
the toml config in the user's storage.conf file

Signed-off-by: cdoern <cdoern@redhat.com>
  • Loading branch information
cdoern committed Jan 7, 2022
1 parent 375f77c commit 772d286
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 6 deletions.
18 changes: 12 additions & 6 deletions store.go
Original file line number Diff line number Diff line change
Expand Up @@ -647,17 +647,23 @@ func GetStore(options types.StoreOptions) (Store, error) {
storesLock.Lock()
defer storesLock.Unlock()

// return if BOTH run and graph root are matched, otherwise our run-root can be overriden if the graph is found first
for _, s := range stores {
if s.graphRoot == options.GraphRoot && (options.GraphDriverName == "" || s.graphDriverName == options.GraphDriverName) {
if (s.graphRoot == options.GraphRoot) && (s.runRoot == options.RunRoot) && (options.GraphDriverName == "" || s.graphDriverName == options.GraphDriverName) {
return s, nil
}
}

if options.GraphRoot == "" {
return nil, errors.Wrap(ErrIncompleteOptions, "no storage root specified")
}
if options.RunRoot == "" {
return nil, errors.Wrap(ErrIncompleteOptions, "no storage runroot specified")
// if passed a run-root or graph-root alone, the other should be defaulted only error if we have neither.
switch {
case options.RunRoot != "" && options.GraphRoot != "":
break
case options.GraphRoot == "" && options.RunRoot != "":
options.GraphRoot = types.Options().GraphRoot
case options.RunRoot == "" && options.GraphRoot != "":
options.RunRoot = types.Options().RunRoot
default:
return nil, errors.Wrap(ErrIncompleteOptions, "no storage runroot or graphroot specified")
}

if err := os.MkdirAll(options.RunRoot, 0700); err != nil {
Expand Down
38 changes: 38 additions & 0 deletions types/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/containers/storage/drivers/overlay"
cfg "github.com/containers/storage/pkg/config"
"github.com/containers/storage/pkg/idtools"
"github.com/containers/storage/pkg/unshare"
"github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -424,3 +425,40 @@ func ReloadConfigurationFile(configFile string, storeOptions *StoreOptions) {
func Options() StoreOptions {
return defaultStoreOptions
}

// SaveLocalStorageConfig overwrites the tomlConfig in storage.conf with the given conf
func SaveLocalStorageConfig(conf tomlConfig) error {
configFile, err := DefaultConfigFile(unshare.IsRootless())
if err != nil {
return err
}
if _, err := os.Stat(configFile); err == nil {
err = os.Remove(configFile)
if err != nil {
return err
}
}
f, err := os.Open(configFile)
if err != nil {
return err
}

return toml.NewEncoder(f).Encode(conf)
}

// GetLocalStorageConfig is used to retreive the storage.conf toml in order to overwrite it
func GetLocalStorageConfig() (*tomlConfig, error) {
config := new(tomlConfig)

configFile, err := DefaultConfigFile(unshare.IsRootless())
if err != nil {
return nil, err
}

_, err = toml.DecodeFile(configFile, &config)
if err != nil {
return nil, err
}

return config, nil
}

0 comments on commit 772d286

Please sign in to comment.