Skip to content
This repository has been archived by the owner on Apr 20, 2023. It is now read-only.

Commit

Permalink
Merge pull request moby#2384 from crazy-max/shmsize
Browse files Browse the repository at this point in the history
Add support for shm size
  • Loading branch information
crazy-max committed Oct 1, 2021
2 parents b8e4ed1 + 6094339 commit 42aefeb
Show file tree
Hide file tree
Showing 11 changed files with 281 additions and 142 deletions.
32 changes: 32 additions & 0 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ func TestIntegration(t *testing.T) {
testParallelLocalBuilds,
testSecretMounts,
testExtraHosts,
testShmSize,
testNetworkMode,
testFrontendMetadataReturn,
testFrontendUseSolveResults,
Expand Down Expand Up @@ -528,6 +529,37 @@ func testExtraHosts(t *testing.T, sb integration.Sandbox) {
require.NoError(t, err)
}

func testShmSize(t *testing.T, sb integration.Sandbox) {
c, err := New(sb.Context(), sb.Address())
require.NoError(t, err)
defer c.Close()

st := llb.Image("busybox:latest").
Run(llb.Shlex(`sh -c 'mount | grep /dev/shm > /out/out'`), llb.WithShmSize(128*1024))

out := st.AddMount("/out", llb.Scratch())
def, err := out.Marshal(sb.Context())
require.NoError(t, err)

destDir, err := ioutil.TempDir("", "buildkit")
require.NoError(t, err)
defer os.RemoveAll(destDir)

_, err = c.Solve(sb.Context(), def, SolveOpt{
Exports: []ExportEntry{
{
Type: ExporterLocal,
OutputDir: destDir,
},
},
}, nil)
require.NoError(t, err)

dt, err := ioutil.ReadFile(filepath.Join(destDir, "out"))
require.NoError(t, err)
require.Contains(t, string(dt), `size=131072k`)
}

func testNetworkMode(t *testing.T, sb integration.Sandbox) {
c, err := New(sb.Context(), sb.Address())
require.NoError(t, err)
Expand Down
15 changes: 15 additions & 0 deletions client/llb/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ func (e *ExecOp) Marshal(ctx context.Context, c *Constraints) (digest.Digest, []
User: user,
Hostname: hostname,
}

extraHosts, err := getExtraHosts(e.base)(ctx, c)
if err != nil {
return "", nil, nil, nil, err
Expand All @@ -204,6 +205,14 @@ func (e *ExecOp) Marshal(ctx context.Context, c *Constraints) (digest.Digest, []
meta.ExtraHosts = hosts
}

shmSize, err := getShmSize(e.base)(ctx, c)
if err != nil {
return "", nil, nil, nil, err
}
if shmSize != nil {
meta.ShmSize = *shmSize
}

network, err := getNetwork(e.base)(ctx, c)
if err != nil {
return "", nil, nil, nil, err
Expand Down Expand Up @@ -498,6 +507,12 @@ func AddExtraHost(host string, ip net.IP) RunOption {
})
}

func WithShmSize(kb int64) RunOption {
return runOptionFunc(func(ei *ExecInfo) {
ei.State = ei.State.WithShmSize(kb)
})
}

func With(so ...StateOption) RunOption {
return runOptionFunc(func(ei *ExecInfo) {
ei.State = ei.State.With(so...)
Expand Down
21 changes: 21 additions & 0 deletions client/llb/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var (
keyUser = contextKeyT("llb.exec.user")
keyHostname = contextKeyT("llb.exec.hostname")
keyExtraHost = contextKeyT("llb.exec.extrahost")
keyShmSize = contextKeyT("llb.exec.shmsize")
keyPlatform = contextKeyT("llb.platform")
keyNetwork = contextKeyT("llb.network")
keySecurity = contextKeyT("llb.security")
Expand Down Expand Up @@ -232,6 +233,26 @@ type HostIP struct {
IP net.IP
}

func shmSize(kb int64) StateOption {
return func(s State) State {
return s.WithValue(keyShmSize, kb)
}
}

func getShmSize(s State) func(context.Context, *Constraints) (*int64, error) {
return func(ctx context.Context, c *Constraints) (*int64, error) {
v, err := s.getValue(keyShmSize)(ctx, c)
if err != nil {
return nil, err
}
if v != nil {
kb := v.(int64)
return &kb, nil
}
return nil, nil
}
}

func Network(v pb.NetMode) StateOption {
return func(s State) State {
return s.WithValue(keyNetwork, v)
Expand Down
4 changes: 4 additions & 0 deletions client/llb/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,10 @@ func (s State) AddExtraHost(host string, ip net.IP) State {
return extraHost(host, ip)(s)
}

func (s State) WithShmSize(kb int64) State {
return shmSize(kb)(s)
}

func (s State) isFileOpCopyInput() {}

type output struct {
Expand Down
1 change: 1 addition & 0 deletions executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Meta struct {
Tty bool
ReadonlyRootFS bool
ExtraHosts []HostIP
ShmSize int64
NetMode pb.NetMode
SecurityMode pb.SecurityMode
}
Expand Down
4 changes: 4 additions & 0 deletions executor/oci/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ func GenerateSpec(ctx context.Context, meta executor.Meta, mounts []executor.Mou
oci.WithHostname(hostname),
)

if meta.ShmSize > 0 {
opts = append(opts, oci.WithDevShmSize(meta.ShmSize))
}

s, err := oci.GenerateSpec(ctx, nil, c, opts...)
if err != nil {
return nil, nil, err
Expand Down
18 changes: 18 additions & 0 deletions frontend/dockerfile/builder/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ const (
keyNameDockerfile = "dockerfilekey"
keyNoCache = "no-cache"
keyOverrideCopyImage = "override-copy-image" // remove after CopyOp implemented
keyShmSize = "shm-size"
keyTargetPlatform = "platform"

// Don't forget to update frontend documentation if you add
Expand Down Expand Up @@ -116,6 +117,11 @@ func Build(ctx context.Context, c client.Client) (*client.Result, error) {
return nil, errors.Wrap(err, "failed to parse additional hosts")
}

shmSize, err := parseShmSize(opts[keyShmSize])
if err != nil {
return nil, errors.Wrap(err, "failed to parse shm size")
}

defaultNetMode, err := parseNetMode(opts[keyForceNetwork])
if err != nil {
return nil, err
Expand Down Expand Up @@ -427,6 +433,7 @@ func Build(ctx context.Context, c client.Client) (*client.Result, error) {
ImageResolveMode: resolveMode,
PrefixPlatform: exportMap,
ExtraHosts: extraHosts,
ShmSize: shmSize,
ForceNetMode: defaultNetMode,
OverrideCopyImage: opts[keyOverrideCopyImage],
LLBCaps: &caps,
Expand Down Expand Up @@ -675,6 +682,17 @@ func parseExtraHosts(v string) ([]llb.HostIP, error) {
return out, nil
}

func parseShmSize(v string) (int64, error) {
if len(v) == 0 {
return 0, nil
}
kb, err := strconv.ParseInt(v, 10, 64)
if err != nil {
return 0, err
}
return kb, nil
}

func parseNetMode(v string) (pb.NetMode, error) {
if v == "" {
return llb.NetModeSandbox, nil
Expand Down
6 changes: 6 additions & 0 deletions frontend/dockerfile/dockerfile2llb/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ type ConvertOpt struct {
BuildPlatforms []ocispecs.Platform
PrefixPlatform bool
ExtraHosts []llb.HostIP
ShmSize int64
ForceNetMode pb.NetMode
OverrideCopyImage string
LLBCaps *apicaps.CapSet
Expand Down Expand Up @@ -389,6 +390,7 @@ func Dockerfile2LLB(ctx context.Context, dt []byte, opt ConvertOpt) (*llb.State,
buildPlatforms: platformOpt.buildPlatforms,
targetPlatform: platformOpt.targetPlatform,
extraHosts: opt.ExtraHosts,
shmSize: opt.ShmSize,
copyImage: opt.OverrideCopyImage,
llbCaps: opt.LLBCaps,
sourceMap: opt.SourceMap,
Expand Down Expand Up @@ -519,6 +521,7 @@ type dispatchOpt struct {
targetPlatform ocispecs.Platform
buildPlatforms []ocispecs.Platform
extraHosts []llb.HostIP
shmSize int64
copyImage string
llbCaps *apicaps.CapSet
sourceMap *llb.SourceMap
Expand Down Expand Up @@ -798,6 +801,9 @@ func dispatchRun(d *dispatchState, c *instructions.RunCommand, proxy *llb.ProxyE
for _, h := range dopt.extraHosts {
opt = append(opt, llb.AddExtraHost(h.Host, h.IP))
}
if dopt.shmSize > 0 {
opt = append(opt, llb.WithShmSize(dopt.shmSize))
}
d.state = d.state.Run(opt...).Root()
return commitToHistory(&d.image, "RUN "+runCommandString(args, d.buildArgs, shell.BuildEnvs(env)), true, &d.state)
}
Expand Down
1 change: 1 addition & 0 deletions solver/llbsolver/ops/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ func (e *execOp) Exec(ctx context.Context, g session.Group, inputs []solver.Resu
Hostname: e.op.Meta.Hostname,
ReadonlyRootFS: p.ReadonlyRootFS,
ExtraHosts: extraHosts,
ShmSize: e.op.Meta.ShmSize,
NetMode: e.op.Network,
SecurityMode: e.op.Security,
}
Expand Down
Loading

0 comments on commit 42aefeb

Please sign in to comment.