Skip to content

Commit 20d3fae

Browse files
committed
Add Opt for modifying shm size
Closes #3654 Signed-off-by: Michael Crosby <[email protected]>
1 parent 87bff67 commit 20d3fae

3 files changed

Lines changed: 80 additions & 1 deletion

File tree

oci/spec_opts.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,3 +1222,28 @@ func WithEnvFile(path string) SpecOpts {
12221222
return WithEnv(vars)(nil, nil, nil, s)
12231223
}
12241224
}
1225+
1226+
// ErrNoShmMount is returned when there is no /dev/shm mount specified in the config
1227+
// and an Opts was trying to set a configuration value on the mount.
1228+
var ErrNoShmMount = errors.New("no /dev/shm mount specified")
1229+
1230+
// WithDevShmSize sets the size of the /dev/shm mount for the container.
1231+
//
1232+
// The size value is specified in kb, kilobytes.
1233+
func WithDevShmSize(kb int64) SpecOpts {
1234+
return func(ctx context.Context, _ Client, c *containers.Container, s *Spec) error {
1235+
for _, m := range s.Mounts {
1236+
if m.Source == "shm" && m.Type == "tmpfs" {
1237+
for i, o := range m.Options {
1238+
if strings.HasPrefix(o, "size=") {
1239+
m.Options[i] = fmt.Sprintf("size=%dk", kb)
1240+
return nil
1241+
}
1242+
}
1243+
m.Options = append(m.Options, fmt.Sprintf("size=%dk", kb))
1244+
return nil
1245+
}
1246+
}
1247+
return ErrNoShmMount
1248+
}
1249+
}

oci/spec_opts_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"os"
2828
"reflect"
2929
"runtime"
30+
"strings"
3031
"testing"
3132

3233
"github.com/containerd/containerd/content"
@@ -506,3 +507,56 @@ func TestDropCaps(t *testing.T) {
506507
}
507508
}
508509
}
510+
511+
func TestDevShmSize(t *testing.T) {
512+
t.Parallel()
513+
var (
514+
s Spec
515+
c = containers.Container{ID: t.Name()}
516+
ctx = namespaces.WithNamespace(context.Background(), "test")
517+
)
518+
519+
err := populateDefaultUnixSpec(ctx, &s, c.ID)
520+
if err != nil {
521+
t.Fatal(err)
522+
}
523+
524+
expected := "1024k"
525+
if err := WithDevShmSize(1024)(nil, nil, nil, &s); err != nil {
526+
t.Fatal(err)
527+
}
528+
m := getShmMount(&s)
529+
if m == nil {
530+
t.Fatal("no shm mount found")
531+
}
532+
o := getShmSize(m.Options)
533+
if o == "" {
534+
t.Fatal("shm size not specified")
535+
}
536+
parts := strings.Split(o, "=")
537+
if len(parts) != 2 {
538+
t.Fatal("invalid size format")
539+
}
540+
size := parts[1]
541+
if size != expected {
542+
t.Fatalf("size %s not equal %s", size, expected)
543+
}
544+
}
545+
546+
func getShmMount(s *Spec) *specs.Mount {
547+
for _, m := range s.Mounts {
548+
if m.Source == "shm" && m.Type == "tmpfs" {
549+
return &m
550+
}
551+
}
552+
return nil
553+
}
554+
555+
func getShmSize(opts []string) string {
556+
for _, o := range opts {
557+
if strings.HasPrefix(o, "size=") {
558+
return o
559+
}
560+
}
561+
return ""
562+
}

oci/spec_opts_unix_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func TestWithImageConfigNoEnv(t *testing.T) {
3232
t.Parallel()
3333
var (
3434
s Spec
35-
c = containers.Container{ID: "TestWithImageConfigNoEnv"}
35+
c = containers.Container{ID: t.Name()}
3636
ctx = namespaces.WithNamespace(context.Background(), "test")
3737
)
3838

0 commit comments

Comments
 (0)