Skip to content

Commit 303b751

Browse files
author
Mrunal Patel
authored
Merge pull request #271 from Mashimiao/generate-add-rlimits-options
Generate add rlimits options
2 parents 476f1fb + aa80849 commit 303b751

File tree

4 files changed

+102
-0
lines changed

4 files changed

+102
-0
lines changed

cmd/oci-runtime-tool/generate.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ var generateFlags = []cli.Flag{
6565
cli.StringFlag{Name: "rootfs-path", Value: "rootfs", Usage: "path to the root filesystem"},
6666
cli.StringFlag{Name: "rootfs-propagation", Usage: "mount propagation for rootfs"},
6767
cli.BoolFlag{Name: "rootfs-readonly", Usage: "make the container's rootfs readonly"},
68+
cli.StringSliceFlag{Name: "rlimits-add", Usage: "specifies resource limits for processes inside the container. "},
69+
cli.StringSliceFlag{Name: "rlimits-remove", Usage: "remove specified resource limits for processes inside the container. "},
70+
cli.BoolFlag{Name: "rlimits-remove-all", Usage: "remove all resource limits for processes inside the container. "},
6871
cli.StringFlag{Name: "seccomp-allow", Usage: "specifies syscalls to respond with allow"},
6972
cli.StringFlag{Name: "seccomp-arch", Usage: "specifies additional architectures permitted to be used for system calls"},
7073
cli.StringFlag{Name: "seccomp-default", Usage: "specifies default action to be used for system calls and removes existing rules with specified action"},
@@ -445,6 +448,31 @@ func setupSpec(g *generate.Generator, context *cli.Context) error {
445448
}
446449
}
447450

451+
if context.IsSet("rlimits-add") {
452+
rlimits := context.StringSlice("rlimits-add")
453+
for _, rlimit := range rlimits {
454+
rType, rHard, rSoft, err := parseRlimit(rlimit)
455+
if err != nil {
456+
return err
457+
}
458+
g.AddProcessRlimits(rType, rHard, rSoft)
459+
}
460+
}
461+
462+
if context.IsSet("rlimits-remove") {
463+
rlimits := context.StringSlice("rlimits-remove")
464+
for _, rlimit := range rlimits {
465+
err := g.RemoveProcessRlimits(rlimit)
466+
if err != nil {
467+
return err
468+
}
469+
}
470+
}
471+
472+
if context.IsSet("rlimits-remove-all") {
473+
g.ClearProcessRlimits()
474+
}
475+
448476
err := addSeccomp(context, g)
449477
return err
450478
}
@@ -548,6 +576,25 @@ func parseBindMount(s string) (string, string, []string, error) {
548576
return source, dest, options, nil
549577
}
550578

579+
func parseRlimit(rlimit string) (string, uint64, uint64, error) {
580+
parts := strings.Split(rlimit, ":")
581+
if len(parts) != 3 {
582+
return "", 0, 0, fmt.Errorf("invalid rlimits value: %s", rlimit)
583+
}
584+
585+
hard, err := strconv.Atoi(parts[1])
586+
if err != nil {
587+
return "", 0, 0, err
588+
}
589+
590+
soft, err := strconv.Atoi(parts[2])
591+
if err != nil {
592+
return "", 0, 0, err
593+
}
594+
595+
return parts[0], uint64(hard), uint64(soft), nil
596+
}
597+
551598
func addSeccomp(context *cli.Context, g *generate.Generator) error {
552599

553600
// Set the DefaultAction of seccomp

completions/bash/oci-runtime-tool

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,9 @@ _oci-runtime-tool_generate() {
303303
--readonly-paths
304304
--rootfs-path
305305
--rootfs-propagation
306+
--rlimits-add
307+
--rlimits-remove
308+
--rlimits-remove-all
306309
--seccomp-allow
307310
--seccomp-arch
308311
--seccomp-default

generate/generate.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,47 @@ func (g *Generator) AddProcessEnv(env string) {
337337
g.spec.Process.Env = append(g.spec.Process.Env, env)
338338
}
339339

340+
// AddProcessRlimits adds rlimit into g.spec.Process.Rlimits.
341+
func (g *Generator) AddProcessRlimits(rType string, rHard uint64, rSoft uint64) {
342+
g.initSpec()
343+
for i, rlimit := range g.spec.Process.Rlimits {
344+
if rlimit.Type == rType {
345+
g.spec.Process.Rlimits[i].Hard = rHard
346+
g.spec.Process.Rlimits[i].Soft = rSoft
347+
return
348+
}
349+
}
350+
351+
newRlimit := rspec.Rlimit{
352+
Type: rType,
353+
Hard: rHard,
354+
Soft: rSoft,
355+
}
356+
g.spec.Process.Rlimits = append(g.spec.Process.Rlimits, newRlimit)
357+
}
358+
359+
// RemoveProcessRlimits removes a rlimit from g.spec.Process.Rlimits.
360+
func (g *Generator) RemoveProcessRlimits(rType string) error {
361+
if g.spec == nil {
362+
return nil
363+
}
364+
for i, rlimit := range g.spec.Process.Rlimits {
365+
if rlimit.Type == rType {
366+
g.spec.Process.Rlimits = append(g.spec.Process.Rlimits[:i], g.spec.Process.Rlimits[i+1:]...)
367+
return nil
368+
}
369+
}
370+
return nil
371+
}
372+
373+
// ClearProcessRlimits clear g.spec.Process.Rlimits.
374+
func (g *Generator) ClearProcessRlimits() {
375+
if g.spec == nil {
376+
return
377+
}
378+
g.spec.Process.Rlimits = []rspec.Rlimit{}
379+
}
380+
340381
// ClearProcessAdditionalGids clear g.spec.Process.AdditionalGids.
341382
func (g *Generator) ClearProcessAdditionalGids() {
342383
if g.spec == nil {

man/oci-runtime-tool-generate.1.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,17 @@ read the configuration from `config.json`.
237237

238238
By default a container will have its root filesystem writable allowing processes to write files anywhere. By specifying the `--rootfs-readonly` flag the container will have its root filesystem mounted as read only prohibiting any writes.
239239

240+
**--rlimits-add**=[]
241+
Specifies resource limits, format is RLIMIT:HARD:SOFT. e.g. --rlimits-add=RLIMIT_NOFILE:1024:1024
242+
This option can be specified multiple times. When same RLIMIT specified over once, the last one make sense.
243+
244+
**--rlimits-remove**=[]
245+
Remove the specified resource limits for process inside the container.
246+
This option can be specified multiple times.
247+
248+
**--rlimits-remove-all**=true|false
249+
Remove all resource limits for process inside the container. The default is *false*.
250+
240251
**--seccomp-allow**=SYSCALL
241252
Specifies syscalls to be added to the ALLOW list.
242253
See --seccomp-syscalls for setting limits on arguments.

0 commit comments

Comments
 (0)