Skip to content

Commit c25a5c4

Browse files
committed
Option --overlay-size
1 parent ab0097c commit c25a5c4

File tree

4 files changed

+85
-5
lines changed

4 files changed

+85
-5
lines changed

runsc/boot/vfs.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,12 @@ func (c *containerMounter) configureOverlay(ctx context.Context, conf *config.Co
585585
// filesystem specific options.
586586
upperOpts := *lowerOpts
587587
upperOpts.GetFilesystemOptions = vfs.GetFilesystemOptions{InternalMount: true}
588+
if conf.GetOverlay2().Size() != "" {
589+
if upperOpts.GetFilesystemOptions.Data != "" {
590+
upperOpts.GetFilesystemOptions.Data += ","
591+
}
592+
upperOpts.GetFilesystemOptions.Data += "size=" + conf.GetOverlay2().Size()
593+
}
588594

589595
overlayOpts := *lowerOpts
590596
overlayOpts.GetFilesystemOptions = vfs.GetFilesystemOptions{InternalMount: true}

runsc/config/config.go

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -906,24 +906,37 @@ type Overlay2 struct {
906906
rootMount bool
907907
subMounts bool
908908
medium OverlayMedium
909+
// Size of overlay upper layer.
910+
// Passed as is to tmpfs mount, as `size={size}`.
911+
// Empty means use default.
912+
// Size is applied to each overlay independently and not shared by overlays.
913+
size string
909914
}
910915

911916
func defaultOverlay2() *Overlay2 {
912917
// Rootfs overlay is enabled by default and backed by a file in rootfs itself.
913918
return &Overlay2{rootMount: true, subMounts: false, medium: SelfOverlay}
914919
}
915920

921+
func setOverlay2Err(v string) error {
922+
return fmt.Errorf("expected format is --overlay2={mount}:{medium}[:size={size}], got %q", v)
923+
}
924+
925+
// `--overlay2=...` param `size=`.
926+
const overlaySizeEq = "size="
927+
916928
// Set implements flag.Value. Set(String()) should be idempotent.
917929
func (o *Overlay2) Set(v string) error {
918930
if v == "none" {
919931
o.rootMount = false
920932
o.subMounts = false
921933
o.medium = NoOverlay
934+
o.size = ""
922935
return nil
923936
}
924937
vs := strings.Split(v, ":")
925-
if len(vs) != 2 {
926-
return fmt.Errorf("expected format is --overlay2={mount}:{medium}, got %q", v)
938+
if len(vs) < 2 {
939+
return setOverlay2Err(v)
927940
}
928941

929942
switch mount := vs[0]; mount {
@@ -936,7 +949,24 @@ func (o *Overlay2) Set(v string) error {
936949
return fmt.Errorf("unexpected mount specifier for --overlay2: %q", mount)
937950
}
938951

939-
return o.medium.Set(vs[1])
952+
err := o.medium.Set(vs[1])
953+
if err != nil {
954+
return err
955+
}
956+
957+
if len(vs) == 2 {
958+
o.size = ""
959+
} else if len(vs) == 3 {
960+
sizeArg := vs[2]
961+
if !strings.HasPrefix(sizeArg, overlaySizeEq) {
962+
return setOverlay2Err(v)
963+
}
964+
o.size = strings.TrimPrefix(sizeArg, overlaySizeEq)
965+
} else {
966+
return setOverlay2Err(v)
967+
}
968+
969+
return nil
940970
}
941971

942972
// Get implements flag.Value.
@@ -958,7 +988,13 @@ func (o Overlay2) String() string {
958988
default:
959989
panic("invalid state of subMounts = true and rootMount = false")
960990
}
961-
return res + ":" + o.medium.String()
991+
992+
var sizeSuffix string
993+
if o.size != "" {
994+
sizeSuffix = fmt.Sprintf(":%s%s", overlaySizeEq, o.size)
995+
}
996+
997+
return res + ":" + o.medium.String() + sizeSuffix
962998
}
963999

9641000
// Enabled returns true if the overlay option is enabled for any mounts.
@@ -987,6 +1023,11 @@ func (o Overlay2) Medium() OverlayMedium {
9871023
return o.medium
9881024
}
9891025

1026+
// Size returns the overlay upper layer size, or empty string if default should be used.
1027+
func (o Overlay2) Size() string {
1028+
return o.size
1029+
}
1030+
9901031
// HostSettingsPolicy dictates how host settings should be handled.
9911032
type HostSettingsPolicy int
9921033

runsc/config/config_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,11 @@ func TestInvalidFlags(t *testing.T) {
231231
value: "root:dir=tmp",
232232
error: "overlay host file directory should be an absolute path, got \"tmp\"",
233233
},
234+
{
235+
name: "overlay2",
236+
value: "root:memory:sz=sdg",
237+
error: "expected format is --overlay2",
238+
},
234239
} {
235240
t.Run(tc.name, func(t *testing.T) {
236241
testFlags := flag.NewFlagSet("test", flag.ContinueOnError)
@@ -778,3 +783,26 @@ root = "%s"
778783
}
779784

780785
}
786+
787+
func TestParseOverlay2(t *testing.T) {
788+
t.Run("Parse without size", func(t *testing.T) {
789+
o := Overlay2{}
790+
err := o.Set("all:memory")
791+
if err != nil {
792+
t.Fatalf("Set failed: %v", err)
793+
}
794+
if o.Size() != "" {
795+
t.Fatalf("Size mismatch, expecting empty string, got %q", o.Size())
796+
}
797+
})
798+
t.Run("Parse with size", func(t *testing.T) {
799+
o := Overlay2{}
800+
err := o.Set("all:memory:size=1g")
801+
if err != nil {
802+
t.Fatalf("Set failed: %v", err)
803+
}
804+
if o.Size() != "1g" {
805+
t.Fatalf("Size mismatch, expecting 1g, got %q", o.Size())
806+
}
807+
})
808+
}

runsc/config/flags.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,12 @@ func RegisterFlags(flagSet *flag.FlagSet) {
116116
flagSet.Var(fileAccessTypePtr(FileAccessExclusive), "file-access", "specifies which filesystem validation to use for the root mount: exclusive (default), shared.")
117117
flagSet.Var(fileAccessTypePtr(FileAccessShared), "file-access-mounts", "specifies which filesystem validation to use for volumes other than the root mount: shared (default), exclusive.")
118118
flagSet.Bool("overlay", false, "DEPRECATED: use --overlay2=all:memory to achieve the same effect")
119-
flagSet.Var(defaultOverlay2(), flagOverlay2, "wrap mounts with overlayfs. Format is {mount}:{medium}, where 'mount' can be 'root' or 'all' and medium can be 'memory', 'self' or 'dir=/abs/dir/path' in which filestore will be created. 'none' will turn overlay mode off.")
119+
flagSet.Var(defaultOverlay2(), flagOverlay2, "wrap mounts with overlayfs. Format is\n"+
120+
"* 'none' to turn overlay mode off\n"+
121+
"* {mount}:{medium}[size={size}], where\n"+
122+
" 'mount' can be 'root' or 'all'\n"+
123+
" 'medium' can be 'memory', 'self' or 'dir=/abs/dir/path' in which filestore will be created\n"+
124+
" 'size' optional parameter overrides default overlay upper layer size\n")
120125
flagSet.Bool("fsgofer-host-uds", false, "DEPRECATED: use host-uds=all")
121126
flagSet.Var(hostUDSPtr(HostUDSNone), flagHostUDS, "controls permission to access host Unix-domain sockets. Values: none|open|create|all, default: none")
122127
flagSet.Var(hostFifoPtr(HostFifoNone), "host-fifo", "controls permission to access host FIFOs (or named pipes). Values: none|open, default: none")

0 commit comments

Comments
 (0)