Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make buildah's handling of ulimits match podman #5590

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 27 additions & 5 deletions run_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -1003,28 +1003,50 @@ func addRlimits(ulimit []string, g *generate.Generator, defaultUlimits []string)
g.AddProcessRlimits("RLIMIT_"+strings.ToUpper(ul.Name), uint64(ul.Hard), uint64(ul.Soft))
}
if !nofileSet {
max := define.RLimitDefaultValue
// For nofile, podman first tries to set both the hard and soft limits for the current
// process to RLimitDefaultValue - this will be successful in most (but not all)
// non-rootless environments. If this fails (e.g. in a rootless environment) it will ensure
// that the soft limit for the current process is increased to match the hard limit (see
// cmd/podman/early_init_linux.go).
var rlimit unix.Rlimit
rlimit.Cur = define.RLimitDefaultValue
rlimit.Max = define.RLimitDefaultValue
err := unix.Setrlimit(unix.RLIMIT_NOFILE, &rlimit)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the limits are set higher than this on entry, are we lowering them for the calling process? I don't know that I want that as a side-effect.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the limits are currently greater than define.RLimitDefaultValue then yes, they will be lowered (but for the current buildah process, not for any calling process).

There are three reasons why I don't think this is an issue:

  • define.RLimitDefaultValue is 1048576, which matches the default kernel hard limit;
  • the purpose of Make buildah match podman for handling of ulimits #5275 (which this PR is a continuation of) was to match the behaviour of podman (and this is what podman does);
  • if a user does need a higher limit for some reason, they can simply set the --ulimit option.

if err != nil {
// We don't really care whether there's an error here, because if it fails we
// effectively handle setting soft to hard in the call to AddProcessRlimits() later on.
logrus.Debugf("Failed to set RLIMIT_NOFILE ulimit %q", err)
}

// Set both hard and soft limits to min(hard limit, RLimitDefaultValue) regardless of
// rootlessness.
max := define.RLimitDefaultValue
if err := unix.Getrlimit(unix.RLIMIT_NOFILE, &rlimit); err == nil {
if max < rlimit.Max || unshare.IsRootless() {
if rlimit.Max < max {
max = rlimit.Max
}
} else {
logrus.Warnf("Failed to return RLIMIT_NOFILE ulimit %q", err)
}
g.AddProcessRlimits("RLIMIT_NOFILE", max, max)
}
if !nprocSet {
if !nprocSet && unshare.IsRootless() {
// For nproc, podman only sets limits for rootless containers (handling hard and soft limits
// independently). As before, these are set to min(soft/hard limit, RLimitDefaultValue).
current := define.RLimitDefaultValue
max := define.RLimitDefaultValue
var rlimit unix.Rlimit
if err := unix.Getrlimit(unix.RLIMIT_NPROC, &rlimit); err == nil {
if max < rlimit.Max || unshare.IsRootless() {
if rlimit.Max < max {
max = rlimit.Max
}
if rlimit.Cur < current {
current = rlimit.Cur
}
} else {
logrus.Warnf("Failed to return RLIMIT_NPROC ulimit %q", err)
}
g.AddProcessRlimits("RLIMIT_NPROC", max, max)
g.AddProcessRlimits("RLIMIT_NPROC", max, current)
}

return nil
Expand Down
10 changes: 7 additions & 3 deletions tests/run.bats
Original file line number Diff line number Diff line change
Expand Up @@ -524,18 +524,22 @@ function configure_and_check_user() {

_prefetch alpine

run podman run --rm alpine sh -c "awk '/open files/{print \$4 \"/\" \$5}' /proc/self/limits"
podman_files=$output
# drop limits prior to tests - this tests the ability of non-rootless containers to increase
# file limits to match those of podman
ulimit -S -n 1024
ulimit -H -n 1024

run_buildah from --quiet --pull=false $WITH_POLICY_JSON alpine
cid=$output
run podman run --rm alpine sh -c "awk '/open files/{print \$4 \"/\" \$5}' /proc/self/limits"
podman_files=$output
run_buildah run $cid awk '/open files/{print $4 "/" $5}' /proc/self/limits
expect_output "${podman_files}" "limits: podman and buildah should agree on open files"

run podman run --rm alpine sh -c "awk '/processes/{print \$3 \"/\" \$4}' /proc/self/limits"
podman_processes=$output
run_buildah run $cid awk '/processes/{print $3 "/" $4}' /proc/self/limits
expect_output ${podman_processes} "processes should match podman"
expect_output "${podman_processes}" "limits: podman and buildah should agree on processes"
run_buildah rm $cid

run_buildah from --quiet --ulimit nofile=300:400 --pull=false $WITH_POLICY_JSON alpine
Expand Down
Loading