Skip to content

Commit 619f316

Browse files
rhvgoyalroot
authored andcommitted
BACKPORT: overlay2: add support for --storage-opt size
Upstream commit: 05bac45 Allow passing --storage-opt size=X to docker create/run commands for the `overlay2` graphriver. The size option is only available if the backing fs is xfs that is mounted with the `pquota` mount option. The user can pass any size less then the backing fs size. Signed-off-by: Amir Goldstein <amir73il@aquasec.com>
1 parent 9c396a0 commit 619f316

5 files changed

Lines changed: 88 additions & 19 deletions

File tree

daemon/graphdriver/overlay2/overlay.go

Lines changed: 62 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/docker/docker/pkg/parsers"
2525
"github.com/docker/docker/pkg/parsers/kernel"
2626
"github.com/docker/docker/pkg/system"
27+
"github.com/docker/go-units"
2728
"github.com/opencontainers/runc/libcontainer/label"
2829
)
2930

@@ -75,15 +76,25 @@ const (
7576
idLength = 26
7677
)
7778

79+
type overlayOptions struct {
80+
overrideKernelCheck bool
81+
quota graphdriver.Quota
82+
}
83+
7884
// Driver contains information about the home directory and the list of active mounts that are created using this driver.
7985
type Driver struct {
80-
home string
81-
uidMaps []idtools.IDMap
82-
gidMaps []idtools.IDMap
83-
ctr *graphdriver.RefCounter
86+
home string
87+
uidMaps []idtools.IDMap
88+
gidMaps []idtools.IDMap
89+
ctr *graphdriver.RefCounter
90+
quotaCtl *graphdriver.QuotaCtl
91+
options overlayOptions
8492
}
8593

86-
var backingFs = "<unknown>"
94+
var (
95+
backingFs = "<unknown>"
96+
projectQuotaSupported = false
97+
)
8798

8899
func init() {
89100
graphdriver.Register(driverName, Init)
@@ -151,11 +162,16 @@ func Init(home string, options []string, uidMaps, gidMaps []idtools.IDMap) (grap
151162
ctr: graphdriver.NewRefCounter(graphdriver.NewFsChecker(graphdriver.FsMagicOverlay)),
152163
}
153164

154-
return d, nil
155-
}
165+
if backingFs == "xfs" {
166+
// Try to enable project quota support over xfs.
167+
if d.quotaCtl, err = graphdriver.NewQuotaCtl(home); err == nil {
168+
projectQuotaSupported = true
169+
}
170+
}
156171

157-
type overlayOptions struct {
158-
overrideKernelCheck bool
172+
logrus.Debugf("backingFs=%s, projectQuotaSupported=%v", backingFs, projectQuotaSupported)
173+
174+
return d, nil
159175
}
160176

161177
func parseOptions(options []string) (*overlayOptions, error) {
@@ -172,6 +188,7 @@ func parseOptions(options []string) (*overlayOptions, error) {
172188
if err != nil {
173189
return nil, err
174190
}
191+
175192
default:
176193
return nil, fmt.Errorf("overlay2: Unknown option %s\n", key)
177194
}
@@ -254,8 +271,8 @@ func (d *Driver) CreateReadWrite(id, parent, mountLabel string, storageOpt map[s
254271
// The parent filesystem is used to configure these directories for the overlay.
255272
func (d *Driver) Create(id, parent, mountLabel string, storageOpt map[string]string) (retErr error) {
256273

257-
if len(storageOpt) != 0 {
258-
return fmt.Errorf("--storage-opt is not supported for overlay")
274+
if len(storageOpt) != 0 && !projectQuotaSupported {
275+
return fmt.Errorf("--storage-opt is supported only for overlay over xfs with 'pquota' mount option")
259276
}
260277

261278
dir := d.dir(id)
@@ -278,6 +295,20 @@ func (d *Driver) Create(id, parent, mountLabel string, storageOpt map[string]str
278295
}
279296
}()
280297

298+
if len(storageOpt) > 0 {
299+
driver := &Driver{}
300+
if err := d.parseStorageOpt(storageOpt, driver); err != nil {
301+
return err
302+
}
303+
304+
if driver.options.quota.Size > 0 {
305+
// Set container disk quota limit
306+
if err := d.quotaCtl.SetQuota(dir, driver.options.quota); err != nil {
307+
return err
308+
}
309+
}
310+
}
311+
281312
if err := idtools.MkdirAs(path.Join(dir, "diff"), 0755, rootUID, rootGID); err != nil {
282313
return err
283314
}
@@ -317,6 +348,26 @@ func (d *Driver) Create(id, parent, mountLabel string, storageOpt map[string]str
317348
return nil
318349
}
319350

351+
// Parse overlay storage options
352+
func (d *Driver) parseStorageOpt(storageOpt map[string]string, driver *Driver) error {
353+
// Read size to set the disk project quota per container
354+
for key, val := range storageOpt {
355+
key := strings.ToLower(key)
356+
switch key {
357+
case "size":
358+
size, err := units.RAMInBytes(val)
359+
if err != nil {
360+
return err
361+
}
362+
driver.options.quota.Size = uint64(size)
363+
default:
364+
return fmt.Errorf("Unknown option %s", key)
365+
}
366+
}
367+
368+
return nil
369+
}
370+
320371
func (d *Driver) getLower(parent string) (string, error) {
321372
parentDir := d.dir(parent)
322373

docs/reference/commandline/create.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,14 @@ Set storage driver options per container.
167167
$ docker create -it --storage-opt size=120G fedora /bin/bash
168168

169169
This (size) will allow to set the container rootfs size to 120G at creation time.
170-
User cannot pass a size less than the Default BaseFS Size. This option is only
171-
available for the `devicemapper`, `btrfs`, and `zfs` graph drivers.
170+
171+
This option is only available for the `devicemapper`, `btrfs`, `overlay2`,
172+
`windowsfilter` and `zfs` graph drivers.
173+
For the `devicemapper`, `btrfs`, `windowsfilter` and `zfs` graph drivers,
174+
user cannot pass a size less than the Default BaseFS Size.
175+
For the `overlay2` storage driver, the size option is only available if the
176+
backing fs is `xfs` and mounted with the `pquota` mount option.
177+
Under these conditions, user can pass any size less then the backing fs size.
172178

173179
### Specify isolation technology for container (--isolation)
174180

docs/reference/commandline/run.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,14 @@ The `-w` lets the command being executed inside directory given, here
195195
$ docker run -it --storage-opt size=120G fedora /bin/bash
196196

197197
This (size) will allow to set the container rootfs size to 120G at creation time.
198-
User cannot pass a size less than the Default BaseFS Size. This option is only
199-
available for the `devicemapper`, `btrfs`, and `zfs` graph drivers.
198+
199+
This option is only available for the `devicemapper`, `btrfs`, `overlay2`,
200+
`windowsfilter` and `zfs` graph drivers.
201+
For the `devicemapper`, `btrfs`, `windowsfilter` and `zfs` graph drivers,
202+
user cannot pass a size less than the Default BaseFS Size.
203+
For the `overlay2` storage driver, the size option is only available if the
204+
backing fs is `xfs` and mounted with the `pquota` mount option.
205+
Under these conditions, user can pass any size less then the backing fs size.
200206

201207
### Mount tmpfs (--tmpfs)
202208

man/docker-create.1.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,11 @@ unit, `b` is used. Set LIMIT to `-1` to enable unlimited swap.
339339

340340
$ docker create -it --storage-opt size=120G fedora /bin/bash
341341

342-
This (size) will allow to set the container rootfs size to 120G at creation time. User cannot pass a size less than the Default BaseFS Size.
343-
This option is only available for the `devicemapper`, `btrfs`, and `zfs` graph drivers.
342+
This (size) will allow to set the container rootfs size to 120G at creation time.
343+
This option is only available for the `devicemapper`, `btrfs`, `overlay2` and `zfs` graph drivers.
344+
For the `devicemapper`, `btrfs` and `zfs` storage drivers, user cannot pass a size less than the Default BaseFS Size.
345+
For the `overlay2` storage driver, the size option is only available if the backing fs is `xfs` and mounted with the `pquota` mount option.
346+
Under these conditions, user can pass any size less then the backing fs size.
344347

345348
**--stop-signal**=*SIGTERM*
346349
Signal to stop a container. Default is SIGTERM.

man/docker-run.1.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -491,8 +491,11 @@ its root filesystem mounted as read only prohibiting any writes.
491491

492492
$ docker run -it --storage-opt size=120G fedora /bin/bash
493493

494-
This (size) will allow to set the container rootfs size to 120G at creation time. User cannot pass a size less than the Default BaseFS Size.
495-
This option is only available for the `devicemapper`, `btrfs`, and `zfs` graph drivers.
494+
This (size) will allow to set the container rootfs size to 120G at creation time.
495+
This option is only available for the `devicemapper`, `btrfs`, `overlay2` and `zfs` graph drivers.
496+
For the `devicemapper`, `btrfs` and `zfs` storage drivers, user cannot pass a size less than the Default BaseFS Size.
497+
For the `overlay2` storage driver, the size option is only available if the backing fs is `xfs` and mounted with the `pquota` mount option.
498+
Under these conditions, user can pass any size less then the backing fs size.
496499

497500
**--stop-signal**=*SIGTERM*
498501
Signal to stop a container. Default is SIGTERM.

0 commit comments

Comments
 (0)