Skip to content

Commit 151fd0f

Browse files
btrfs: accept btrfs.size from storage.conf and apply default on RW layers
storage.conf documents [storage.options.btrfs] size, and pkg/config already emits btrfs.size=... for the graph driver. The btrfs driver parseOptions only handled min_space and rejected size, so storage init failed with an unknown option error. Parse size like min_space (RAMInBytes, enable quota when set). For read-write layers, if there is no explicit storage-opt size, apply the configured default inside create() without mutating the caller's StorageOpt map and without formatting the default to a string only to parse it again. Add a small unit test for parseOptions. Fixes: #718 Signed-off-by: Akhil Ammu <akhil.ammu@aiven.io>
1 parent 8af7873 commit 151fd0f

2 files changed

Lines changed: 46 additions & 8 deletions

File tree

storage/drivers/btrfs/btrfs.go

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,13 @@ func parseOptions(opt []string) (btrfsOptions, bool, error) {
114114
}
115115
userDiskQuota = true
116116
options.minSpace = uint64(minSpace)
117+
case "size":
118+
size, err := units.RAMInBytes(val)
119+
if err != nil {
120+
return options, userDiskQuota, err
121+
}
122+
userDiskQuota = true
123+
options.size = uint64(size)
117124
case "mountopt":
118125
return options, userDiskQuota, fmt.Errorf("btrfs driver does not support mount options")
119126
default:
@@ -476,11 +483,20 @@ func (d *Driver) CreateFromTemplate(id, template string, templateIDMappings *idt
476483
// CreateReadWrite creates a layer that is writable for use as a container
477484
// file system.
478485
func (d *Driver) CreateReadWrite(id, parent string, opts *graphdriver.CreateOpts) error {
479-
return d.Create(id, parent, opts)
486+
var def *uint64
487+
if d.options.size > 0 {
488+
s := d.options.size
489+
def = &s
490+
}
491+
return d.create(id, parent, opts, def)
480492
}
481493

482494
// Create the filesystem with given id.
483495
func (d *Driver) Create(id, parent string, opts *graphdriver.CreateOpts) error {
496+
return d.create(id, parent, opts, nil)
497+
}
498+
499+
func (d *Driver) create(id, parent string, opts *graphdriver.CreateOpts, defaultSize *uint64) error {
484500
quotas := d.quotasDir()
485501
subvolumes := d.subvolumesDir()
486502
if err := os.MkdirAll(subvolumes, 0o700); err != nil {
@@ -512,19 +528,31 @@ func (d *Driver) Create(id, parent string, opts *graphdriver.CreateOpts) error {
512528
storageOpt = opts.StorageOpt
513529
}
514530

515-
if _, ok := storageOpt["size"]; ok {
516-
driver := &Driver{}
517-
if err := d.parseStorageOpt(storageOpt, driver); err != nil {
518-
return err
531+
var quotaSize uint64
532+
var needQuota bool
533+
if storageOpt != nil {
534+
if _, ok := storageOpt["size"]; ok {
535+
driver := &Driver{}
536+
if err := d.parseStorageOpt(storageOpt, driver); err != nil {
537+
return err
538+
}
539+
quotaSize = driver.options.size
540+
needQuota = true
519541
}
520-
521-
if err := d.setStorageSize(path.Join(subvolumes, id), driver); err != nil {
542+
}
543+
if !needQuota && defaultSize != nil && *defaultSize > 0 {
544+
quotaSize = *defaultSize
545+
needQuota = true
546+
}
547+
if needQuota {
548+
layerDriver := &Driver{options: btrfsOptions{size: quotaSize}}
549+
if err := d.setStorageSize(path.Join(subvolumes, id), layerDriver); err != nil {
522550
return err
523551
}
524552
if err := os.MkdirAll(quotas, 0o700); err != nil {
525553
return err
526554
}
527-
if err := os.WriteFile(path.Join(quotas, id), []byte(fmt.Sprint(driver.options.size)), 0o644); err != nil {
555+
if err := os.WriteFile(path.Join(quotas, id), []byte(fmt.Sprint(quotaSize)), 0o644); err != nil {
528556
return err
529557
}
530558
}

storage/drivers/btrfs/btrfs_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@ import (
1111
"go.podman.io/storage/drivers/graphtest"
1212
)
1313

14+
func TestParseOptionsBtrfsSize(t *testing.T) {
15+
opts, wantQuota, err := parseOptions([]string{"btrfs.size=10m"})
16+
if err != nil {
17+
t.Fatal(err)
18+
}
19+
if !wantQuota || opts.size == 0 {
20+
t.Fatalf("expected size and quota, got size=%d quota=%v", opts.size, wantQuota)
21+
}
22+
}
23+
1424
// This avoids creating a new driver for each test if all tests are run
1525
// Make sure to put new tests between TestBtrfsSetup and TestBtrfsTeardown
1626
func TestBtrfsSetup(t *testing.T) {

0 commit comments

Comments
 (0)