Skip to content

Commit b592a4c

Browse files
committed
oci: fix WithDevShmSize
Signed-off-by: Iceber Gu <[email protected]>
1 parent a5d17eb commit b592a4c

2 files changed

Lines changed: 87 additions & 39 deletions

File tree

oci/spec_opts.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,16 +1228,16 @@ var ErrNoShmMount = errors.New("no /dev/shm mount specified")
12281228
//
12291229
// The size value is specified in kb, kilobytes.
12301230
func WithDevShmSize(kb int64) SpecOpts {
1231-
return func(ctx context.Context, _ Client, c *containers.Container, s *Spec) error {
1232-
for _, m := range s.Mounts {
1233-
if m.Source == "shm" && m.Type == "tmpfs" {
1234-
for i, o := range m.Options {
1235-
if strings.HasPrefix(o, "size=") {
1236-
m.Options[i] = fmt.Sprintf("size=%dk", kb)
1237-
return nil
1231+
return func(ctx context.Context, _ Client, _ *containers.Container, s *Spec) error {
1232+
for i, m := range s.Mounts {
1233+
if filepath.Clean(m.Destination) == "/dev/shm" && m.Source == "shm" && m.Type == "tmpfs" {
1234+
for i := 0; i < len(m.Options); i++ {
1235+
if strings.HasPrefix(m.Options[i], "size=") {
1236+
m.Options = append(m.Options[:i], m.Options[i+1:]...)
1237+
i--
12381238
}
12391239
}
1240-
m.Options = append(m.Options, fmt.Sprintf("size=%dk", kb))
1240+
s.Mounts[i].Options = append(m.Options, fmt.Sprintf("size=%dk", kb))
12411241
return nil
12421242
}
12431243
}

oci/spec_opts_test.go

Lines changed: 79 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"io/ioutil"
2626
"log"
2727
"os"
28+
"path/filepath"
2829
"reflect"
2930
"runtime"
3031
"strings"
@@ -551,53 +552,100 @@ func TestWithImageConfigArgs(t *testing.T) {
551552

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

560-
err := populateDefaultUnixSpec(ctx, &s, c.ID)
561-
if err != nil {
562-
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+
},
563593
}
564594

565595
expected := "1024k"
566-
if err := WithDevShmSize(1024)(nil, nil, nil, &s); err != nil {
567-
t.Fatal(err)
568-
}
569-
m := getShmMount(&s)
570-
if m == nil {
571-
t.Fatal("no shm mount found")
572-
}
573-
o := getShmSize(m.Options)
574-
if o == "" {
575-
t.Fatal("shm size not specified")
576-
}
577-
parts := strings.Split(o, "=")
578-
if len(parts) != 2 {
579-
t.Fatal("invalid size format")
580-
}
581-
size := parts[1]
582-
if size != expected {
583-
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+
}
584619
}
585620
}
586621

587-
func getShmMount(s *Spec) *specs.Mount {
622+
func getDevShmMount(s *Spec) *specs.Mount {
588623
for _, m := range s.Mounts {
589-
if m.Source == "shm" && m.Type == "tmpfs" {
624+
if filepath.Clean(m.Destination) == "/dev/shm" && m.Source == "shm" && m.Type == "tmpfs" {
590625
return &m
591626
}
592627
}
593628
return nil
594629
}
595630

596-
func getShmSize(opts []string) string {
631+
func getShmSize(opts []string) (string, error) {
632+
// linux will use the last size option
633+
var so string
597634
for _, o := range opts {
598635
if strings.HasPrefix(o, "size=") {
599-
return o
636+
if so != "" {
637+
return "", errors.New("contains multiple size options")
638+
}
639+
so = o
600640
}
601641
}
602-
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
603651
}

0 commit comments

Comments
 (0)