Skip to content

Commit

Permalink
Merge pull request #19879 from rhatdan/ulimits
Browse files Browse the repository at this point in the history
Support passing of Ulimits as -1 to mean max
  • Loading branch information
openshift-merge-bot[bot] authored Nov 10, 2023
2 parents b5b9a2b + 18d6bb4 commit 7d107b9
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 3 deletions.
3 changes: 3 additions & 0 deletions docs/source/markdown/options/ulimit.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ Ulimit options. Sets the ulimits values inside of the container.
$ podman run --ulimit nofile=1024:1024 --rm ubi9 ulimit -n
1024

Set -1 for the soft or hard limit to set the limit to the maximum limit of the current
process. In rootful mode this is often unlimited.

Use **host** to copy the current configuration from the host.

Don't use nproc with the ulimit flag as Linux uses nproc to set the
Expand Down
3 changes: 2 additions & 1 deletion libpod/container_internal_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,8 @@ func (c *Container) generateSpec(ctx context.Context) (s *spec.Spec, cleanupFunc
for _, rlimit := range c.config.Spec.Process.Rlimits {
if rlimit.Type == "RLIMIT_NOFILE" {
nofileSet = true
} else if rlimit.Type == "RLIMIT_NPROC" {
}
if rlimit.Type == "RLIMIT_NPROC" {
nprocSet = true
}
}
Expand Down
1 change: 0 additions & 1 deletion libpod/oci_conmon_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,5 @@ func GetLimits(resource *spec.LinuxResources) (runcconfig.Resources, error) {

// Unified state
final.Unified = resource.Unified

return *final, nil
}
1 change: 1 addition & 0 deletions pkg/specgen/generate/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func addRlimits(s *specgen.SpecGenerator, g *generate.Generator) {

for _, u := range s.Rlimits {
name := "RLIMIT_" + strings.ToUpper(u.Type)
u = subNegativeOne(u)
g.AddProcessRlimits(name, u.Hard, u.Soft)
}
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/specgen/generate/oci_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/containers/podman/v4/libpod"
"github.com/containers/podman/v4/libpod/define"
"github.com/containers/podman/v4/pkg/specgen"
"github.com/opencontainers/runtime-spec/specs-go"
spec "github.com/opencontainers/runtime-spec/specs-go"
"github.com/opencontainers/runtime-tools/generate"
)
Expand Down Expand Up @@ -172,3 +173,7 @@ func WeightDevices(wtDevices map[string]spec.LinuxWeightDevice) ([]spec.LinuxWei
devs := []spec.LinuxWeightDevice{}
return devs, nil
}

func subNegativeOne(u specs.POSIXRlimit) specs.POSIXRlimit {
return u
}
36 changes: 36 additions & 0 deletions pkg/specgen/generate/oci_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ import (
"github.com/containers/podman/v4/libpod/define"
"github.com/containers/podman/v4/pkg/rootless"
"github.com/containers/podman/v4/pkg/specgen"
"github.com/docker/go-units"
spec "github.com/opencontainers/runtime-spec/specs-go"
"github.com/opencontainers/runtime-tools/generate"
"github.com/sirupsen/logrus"
"golang.org/x/sys/unix"
)

Expand Down Expand Up @@ -357,3 +359,37 @@ func WeightDevices(wtDevices map[string]spec.LinuxWeightDevice) ([]spec.LinuxWei
}
return devs, nil
}

// subNegativeOne translates Hard or soft limits of -1 to the current
// processes Max limit
func subNegativeOne(u spec.POSIXRlimit) spec.POSIXRlimit {
if !rootless.IsRootless() ||
(int64(u.Hard) != -1 && int64(u.Soft) != -1) {
return u
}

ul, err := units.ParseUlimit(fmt.Sprintf("%s=%d:%d", u.Type, int64(u.Soft), int64(u.Hard)))
if err != nil {
logrus.Warnf("Failed to check %s ulimit %q", u.Type, err)
return u
}
rl, err := ul.GetRlimit()
if err != nil {
logrus.Warnf("Failed to check %s ulimit %q", u.Type, err)
return u
}

var rlimit unix.Rlimit

if err := unix.Getrlimit(rl.Type, &rlimit); err != nil {
logrus.Warnf("Failed to return RLIMIT_NOFILE ulimit %q", err)
return u
}
if int64(u.Hard) == -1 {
u.Hard = rlimit.Max
}
if int64(u.Soft) == -1 {
u.Soft = rlimit.Max
}
return u
}
2 changes: 1 addition & 1 deletion test/e2e/inspect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ var _ = Describe("Podman inspect", func() {
Expect(inspect[0].NetworkSettings.Networks).To(HaveLen(1))
})

It("Container inspect with unlimited uilimits should be -1", func() {
It("Container inspect with unlimited ulimits should be -1", func() {
ctrName := "testctr"
session := podmanTest.Podman([]string{"run", "-d", "--ulimit", "core=-1:-1", "--name", ctrName, ALPINE, "top"})
session.WaitWithDefaultTimeout()
Expand Down
17 changes: 17 additions & 0 deletions test/system/030-run.bats
Original file line number Diff line number Diff line change
Expand Up @@ -1149,6 +1149,23 @@ EOF
assert "$output" =~ " ${nofile2} * ${nofile2} * files"
}

@test "podman run ulimit with -1" {
max=unlimited
if is_rootless; then
run ulimit -c -H
max=$output
fi

run_podman run --ulimit core=-1:-1 --rm $IMAGE grep core /proc/self/limits
assert "$output" =~ " ${max} * ${max} * bytes"

run_podman run --ulimit core=1000:-1 --rm $IMAGE grep core /proc/self/limits
assert "$output" =~ " 1000 * ${max} * bytes"

run_podman 125 run --ulimit core=-1:1000 --rm $IMAGE grep core /proc/self/limits
is "$output" "Error: ulimit option \"core=-1:1000\" requires name=SOFT:HARD, failed to be parsed: ulimit soft limit must be less than or equal to hard limit: soft: -1 (unlimited), hard: 1000"
}

@test "podman run bad --name" {
randomname=$(random_string 30)
run_podman 125 create --name "$randomname/bad" $IMAGE
Expand Down

0 comments on commit 7d107b9

Please sign in to comment.