@@ -89,7 +89,7 @@ func loadDefaultStoreOptions() {
89
89
90
90
_ , err := os .Stat (defaultOverrideConfigFile )
91
91
if err == nil {
92
- // The DefaultConfigFile(rootless ) function returns the path
92
+ // The DefaultConfigFile() function returns the path
93
93
// of the used storage.conf file, by returning defaultConfigFile
94
94
// If override exists containers/storage uses it by default.
95
95
defaultConfigFile = defaultOverrideConfigFile
@@ -111,21 +111,41 @@ func loadDefaultStoreOptions() {
111
111
setDefaults ()
112
112
}
113
113
114
- // defaultStoreOptionsIsolated is an internal implementation detail of DefaultStoreOptions to allow testing.
115
- // Everyone but the tests this is intended for should only call DefaultStoreOptions, never this function.
116
- func defaultStoreOptionsIsolated (rootless bool , rootlessUID int , storageConf string ) (StoreOptions , error ) {
114
+ // loadStoreOptions returns the default storage ops for containers
115
+ func loadStoreOptions () (StoreOptions , error ) {
116
+ storageConf , err := DefaultConfigFile ()
117
+ if err != nil {
118
+ return defaultStoreOptions , err
119
+ }
120
+ return loadStoreOptionsFromConfFile (storageConf )
121
+ }
122
+
123
+ // usePerUserStorage returns whether the user private storage must be used.
124
+ // We cannot simply use the unshare.IsRootless() condition, because
125
+ // that checks only if the current process needs a user namespace to
126
+ // work and it would break cases where the process is already created
127
+ // in a user namespace (e.g. nested Podman/Buildah) and the desired
128
+ // behavior is to use system paths instead of user private paths.
129
+ func usePerUserStorage () bool {
130
+ return unshare .IsRootless () && unshare .GetRootlessUID () != 0
131
+ }
132
+
133
+ // loadStoreOptionsFromConfFile is an internal implementation detail of DefaultStoreOptions to allow testing.
134
+ // Everyone but the tests this is intended for should only call loadStoreOptions, never this function.
135
+ func loadStoreOptionsFromConfFile (storageConf string ) (StoreOptions , error ) {
117
136
var (
118
137
defaultRootlessRunRoot string
119
138
defaultRootlessGraphRoot string
120
139
err error
121
140
)
141
+
122
142
defaultStoreOptionsOnce .Do (loadDefaultStoreOptions )
123
143
if loadDefaultStoreOptionsErr != nil {
124
144
return StoreOptions {}, loadDefaultStoreOptionsErr
125
145
}
126
146
storageOpts := defaultStoreOptions
127
- if rootless && rootlessUID != 0 {
128
- storageOpts , err = getRootlessStorageOpts (rootlessUID , storageOpts )
147
+ if usePerUserStorage () {
148
+ storageOpts , err = getRootlessStorageOpts (storageOpts )
129
149
if err != nil {
130
150
return storageOpts , err
131
151
}
@@ -139,7 +159,7 @@ func defaultStoreOptionsIsolated(rootless bool, rootlessUID int, storageConf str
139
159
defaultRootlessGraphRoot = storageOpts .GraphRoot
140
160
storageOpts = StoreOptions {}
141
161
reloadConfigurationFileIfNeeded (storageConf , & storageOpts )
142
- if rootless && rootlessUID != 0 {
162
+ if usePerUserStorage () {
143
163
// If the file did not specify a graphroot or runroot,
144
164
// set sane defaults so we don't try and use root-owned
145
165
// directories
@@ -158,6 +178,7 @@ func defaultStoreOptionsIsolated(rootless bool, rootlessUID int, storageConf str
158
178
if storageOpts .RunRoot == "" {
159
179
return storageOpts , fmt .Errorf ("runroot must be set" )
160
180
}
181
+ rootlessUID := unshare .GetRootlessUID ()
161
182
runRoot , err := expandEnvPath (storageOpts .RunRoot , rootlessUID )
162
183
if err != nil {
163
184
return storageOpts , err
@@ -188,26 +209,17 @@ func defaultStoreOptionsIsolated(rootless bool, rootlessUID int, storageConf str
188
209
return storageOpts , nil
189
210
}
190
211
191
- // loadStoreOptions returns the default storage ops for containers
192
- func loadStoreOptions (rootless bool , rootlessUID int ) (StoreOptions , error ) {
193
- storageConf , err := DefaultConfigFile (rootless && rootlessUID != 0 )
194
- if err != nil {
195
- return defaultStoreOptions , err
196
- }
197
- return defaultStoreOptionsIsolated (rootless , rootlessUID , storageConf )
198
- }
199
-
200
212
// UpdateOptions should be called iff container engine received a SIGHUP,
201
213
// otherwise use DefaultStoreOptions
202
- func UpdateStoreOptions (rootless bool , rootlessUID int ) (StoreOptions , error ) {
203
- storeOptions , storeError = loadStoreOptions (rootless , rootlessUID )
214
+ func UpdateStoreOptions () (StoreOptions , error ) {
215
+ storeOptions , storeError = loadStoreOptions ()
204
216
return storeOptions , storeError
205
217
}
206
218
207
219
// DefaultStoreOptions returns the default storage ops for containers
208
- func DefaultStoreOptions (rootless bool , rootlessUID int ) (StoreOptions , error ) {
220
+ func DefaultStoreOptions () (StoreOptions , error ) {
209
221
once .Do (func () {
210
- storeOptions , storeError = loadStoreOptions (rootless , rootlessUID )
222
+ storeOptions , storeError = loadStoreOptions ()
211
223
})
212
224
return storeOptions , storeError
213
225
}
@@ -272,9 +284,11 @@ func isRootlessDriver(driver string) bool {
272
284
}
273
285
274
286
// getRootlessStorageOpts returns the storage opts for containers running as non root
275
- func getRootlessStorageOpts (rootlessUID int , systemOpts StoreOptions ) (StoreOptions , error ) {
287
+ func getRootlessStorageOpts (systemOpts StoreOptions ) (StoreOptions , error ) {
276
288
var opts StoreOptions
277
289
290
+ rootlessUID := unshare .GetRootlessUID ()
291
+
278
292
dataDir , err := homedir .GetDataHome ()
279
293
if err != nil {
280
294
return opts , err
@@ -355,12 +369,6 @@ func getRootlessStorageOpts(rootlessUID int, systemOpts StoreOptions) (StoreOpti
355
369
return opts , nil
356
370
}
357
371
358
- // DefaultStoreOptionsAutoDetectUID returns the default storage ops for containers
359
- func DefaultStoreOptionsAutoDetectUID () (StoreOptions , error ) {
360
- uid := unshare .GetRootlessUID ()
361
- return DefaultStoreOptions (uid != 0 , uid )
362
- }
363
-
364
372
var prevReloadConfig = struct {
365
373
storeOptions * StoreOptions
366
374
mod time.Time
@@ -530,8 +538,8 @@ func Options() (StoreOptions, error) {
530
538
}
531
539
532
540
// Save overwrites the tomlConfig in storage.conf with the given conf
533
- func Save (conf TomlConfig , rootless bool ) error {
534
- configFile , err := DefaultConfigFile (rootless )
541
+ func Save (conf TomlConfig ) error {
542
+ configFile , err := DefaultConfigFile ()
535
543
if err != nil {
536
544
return err
537
545
}
@@ -549,10 +557,10 @@ func Save(conf TomlConfig, rootless bool) error {
549
557
}
550
558
551
559
// StorageConfig is used to retrieve the storage.conf toml in order to overwrite it
552
- func StorageConfig (rootless bool ) (* TomlConfig , error ) {
560
+ func StorageConfig () (* TomlConfig , error ) {
553
561
config := new (TomlConfig )
554
562
555
- configFile , err := DefaultConfigFile (rootless )
563
+ configFile , err := DefaultConfigFile ()
556
564
if err != nil {
557
565
return nil , err
558
566
}
0 commit comments