Skip to content

Commit ab963e1

Browse files
authored
Merge pull request #5063 from Iceber/fix-with-dev-shm-size
oci: fix WithDevShmSize
2 parents 1431679 + b592a4c commit ab963e1

2 files changed

Lines changed: 86 additions & 39 deletions

File tree

oci/spec_opts.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,16 +1248,16 @@ var ErrNoShmMount = errors.New("no /dev/shm mount specified")
12481248
//
12491249
// The size value is specified in kb, kilobytes.
12501250
func WithDevShmSize(kb int64) SpecOpts {
1251-
return func(ctx context.Context, _ Client, c *containers.Container, s *Spec) error {
1252-
for _, m := range s.Mounts {
1253-
if m.Source == "shm" && m.Type == "tmpfs" {
1254-
for i, o := range m.Options {
1255-
if strings.HasPrefix(o, "size=") {
1256-
m.Options[i] = fmt.Sprintf("size=%dk", kb)
1257-
return nil
1251+
return func(ctx context.Context, _ Client, _ *containers.Container, s *Spec) error {
1252+
for i, m := range s.Mounts {
1253+
if filepath.Clean(m.Destination) == "/dev/shm" && m.Source == "shm" && m.Type == "tmpfs" {
1254+
for i := 0; i < len(m.Options); i++ {
1255+
if strings.HasPrefix(m.Options[i], "size=") {
1256+
m.Options = append(m.Options[:i], m.Options[i+1:]...)
1257+
i--
12581258
}
12591259
}
1260-
m.Options = append(m.Options, fmt.Sprintf("size=%dk", kb))
1260+
s.Mounts[i].Options = append(m.Options, fmt.Sprintf("size=%dk", kb))
12611261
return nil
12621262
}
12631263
}

oci/spec_opts_test.go

Lines changed: 78 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -552,55 +552,102 @@ func TestWithImageConfigArgs(t *testing.T) {
552552

553553
func TestDevShmSize(t *testing.T) {
554554
t.Parallel()
555-
var (
556-
s Spec
557-
c = containers.Container{ID: t.Name()}
558-
ctx = namespaces.WithNamespace(context.Background(), "test")
559-
)
560555

561-
err := populateDefaultUnixSpec(ctx, &s, c.ID)
562-
if err != nil {
563-
t.Fatal(err)
556+
ss := []Spec{
557+
{
558+
Mounts: []specs.Mount{
559+
{
560+
Destination: "/dev/shm",
561+
Type: "tmpfs",
562+
Source: "shm",
563+
Options: []string{"nosuid", "noexec", "nodev", "mode=1777"},
564+
},
565+
},
566+
},
567+
{
568+
Mounts: []specs.Mount{
569+
{
570+
Destination: "/test/shm",
571+
Type: "tmpfs",
572+
Source: "shm",
573+
Options: []string{"nosuid", "noexec", "nodev", "mode=1777", "size=65536k"},
574+
},
575+
},
576+
},
577+
{
578+
Mounts: []specs.Mount{
579+
{
580+
Destination: "/test/shm",
581+
Type: "tmpfs",
582+
Source: "shm",
583+
Options: []string{"nosuid", "noexec", "nodev", "mode=1777", "size=65536k"},
584+
},
585+
{
586+
Destination: "/dev/shm",
587+
Type: "tmpfs",
588+
Source: "shm",
589+
Options: []string{"nosuid", "noexec", "nodev", "mode=1777", "size=65536k", "size=131072k"},
590+
},
591+
},
592+
},
564593
}
565594

566595
expected := "1024k"
567-
if err := WithDevShmSize(1024)(nil, nil, nil, &s); err != nil {
568-
t.Fatal(err)
569-
}
570-
m := getShmMount(&s)
571-
if m == nil {
572-
t.Fatal("no shm mount found")
573-
}
574-
o := getShmSize(m.Options)
575-
if o == "" {
576-
t.Fatal("shm size not specified")
577-
}
578-
parts := strings.Split(o, "=")
579-
if len(parts) != 2 {
580-
t.Fatal("invalid size format")
581-
}
582-
size := parts[1]
583-
if size != expected {
584-
t.Fatalf("size %s not equal %s", size, expected)
596+
for _, s := range ss {
597+
if err := WithDevShmSize(1024)(nil, nil, nil, &s); err != nil {
598+
if err != ErrNoShmMount {
599+
t.Fatal(err)
600+
}
601+
602+
if getDevShmMount(&s) == nil {
603+
continue
604+
}
605+
t.Fatal("excepted nil /dev/shm mount")
606+
}
607+
608+
m := getDevShmMount(&s)
609+
if m == nil {
610+
t.Fatal("no shm mount found")
611+
}
612+
size, err := getShmSize(m.Options)
613+
if err != nil {
614+
t.Fatal(err)
615+
}
616+
if size != expected {
617+
t.Fatalf("size %s not equal %s", size, expected)
618+
}
585619
}
586620
}
587621

588-
func getShmMount(s *Spec) *specs.Mount {
622+
func getDevShmMount(s *Spec) *specs.Mount {
589623
for _, m := range s.Mounts {
590-
if m.Source == "shm" && m.Type == "tmpfs" {
624+
if filepath.Clean(m.Destination) == "/dev/shm" && m.Source == "shm" && m.Type == "tmpfs" {
591625
return &m
592626
}
593627
}
594628
return nil
595629
}
596630

597-
func getShmSize(opts []string) string {
631+
func getShmSize(opts []string) (string, error) {
632+
// linux will use the last size option
633+
var so string
598634
for _, o := range opts {
599635
if strings.HasPrefix(o, "size=") {
600-
return o
636+
if so != "" {
637+
return "", errors.New("contains multiple size options")
638+
}
639+
so = o
601640
}
602641
}
603-
return ""
642+
if so == "" {
643+
return "", errors.New("shm size not specified")
644+
}
645+
646+
parts := strings.Split(so, "=")
647+
if len(parts) != 2 {
648+
return "", errors.New("invalid size format")
649+
}
650+
return parts[1], nil
604651
}
605652

606653
func TestWithoutMounts(t *testing.T) {

0 commit comments

Comments
 (0)