@@ -38,6 +38,7 @@ import (
38
38
"github.com/containers/podman/v5/pkg/errorhandling"
39
39
"github.com/containers/podman/v5/pkg/rootless"
40
40
"github.com/containers/storage"
41
+ "github.com/containers/storage/pkg/unshare"
41
42
"github.com/containers/storage/types"
42
43
"github.com/opencontainers/go-digest"
43
44
imgspecv1 "github.com/opencontainers/image-spec/specs-go/v1"
@@ -157,6 +158,28 @@ func (ir *ImageEngine) Mount(ctx context.Context, nameOrIDs []string, opts entit
157
158
listMountsOnly := false
158
159
var images []* libimage.Image
159
160
var err error
161
+
162
+ hasCapSysAdmin , err := unshare .HasCapSysAdmin ()
163
+ if err != nil {
164
+ return nil , err
165
+ }
166
+
167
+ if os .Geteuid () != 0 || ! hasCapSysAdmin {
168
+ if driver := ir .Libpod .StorageConfig ().GraphDriverName ; driver != "vfs" {
169
+ // Do not allow to mount a graphdriver that is not vfs if we are creating the userns as part
170
+ // of the mount command.
171
+ return nil , fmt .Errorf ("cannot mount using driver %s in rootless mode" , driver )
172
+ }
173
+
174
+ became , ret , err := rootless .BecomeRootInUserNS ("" )
175
+ if err != nil {
176
+ return nil , err
177
+ }
178
+ if became {
179
+ os .Exit (ret )
180
+ }
181
+ }
182
+
160
183
switch {
161
184
case opts .All && len (nameOrIDs ) > 0 :
162
185
return nil , errors .New ("cannot mix --all with images" )
@@ -178,22 +201,6 @@ func (ir *ImageEngine) Mount(ctx context.Context, nameOrIDs []string, opts entit
178
201
}
179
202
}
180
203
181
- if os .Geteuid () != 0 {
182
- if driver := ir .Libpod .StorageConfig ().GraphDriverName ; driver != "vfs" {
183
- // Do not allow to mount a graphdriver that is not vfs if we are creating the userns as part
184
- // of the mount command.
185
- return nil , fmt .Errorf ("cannot mount using driver %s in rootless mode" , driver )
186
- }
187
-
188
- became , ret , err := rootless .BecomeRootInUserNS ("" )
189
- if err != nil {
190
- return nil , err
191
- }
192
- if became {
193
- os .Exit (ret )
194
- }
195
- }
196
-
197
204
mountReports := []* entities.ImageMountReport {}
198
205
for _ , i := range images {
199
206
var mountPoint string
0 commit comments